群控系统服务端开发模式-应用开发-业务架构逻辑开发BaseAPI

一、加密工具开发

首先在根目录下extend文件中创建Encipher文件夹,用于专门开发加解密工具,新建RSA算法控制器并命名为Encrypt.php。然后在根目录下config文件夹中创建rsa.php文件,用于配置RSA算法参数。

1、秘钥生成算法

复制代码
<?php
/**
 * RSA加密工具
 * User: 龙哥 三年风水
 * Date: 2024/10/27/0027
 * Time: 21:12
 */
namespace Encipher;
class Encrypt
{

    /**
     * 生成公私钥文件
     * @ return string
     */
    public static function generateRsa(){
        $config = array(
            'digest_alg' => config('rsa.digest_alg'),
            'private_key_bits' => config('rsa.private_key_bits'),
            'OPENSSL_KEYTYPE_RSA' => config('rsa.OPENSSL_KEYTYPE_RSA'),
        );
        $res = openssl_pkey_new($config);
        openssl_pkey_export($res, $private_key);
        $public_key = openssl_pkey_get_details($res);
        $public_key = $public_key["key"];
        file_put_contents("public.key", $public_key);
        file_put_contents("private.pem", $private_key);
        openssl_free_key($res);
        return "生成成功";
    }
}

2、秘钥加密算法

复制代码
<?php
/**
 * RSA加密工具
 * User: 龙哥 三年风水
 * Date: 2024/10/27/0027
 * Time: 21:12
 */
namespace Encipher;
class Encrypt
{
    /**
     * 加密算法-采用公钥加密后base64加码
     * @ param $string
     * @ return string
     */
    public static function encryptRsa($string){
        $ssl_public = file_get_contents("public.key");
        $pu_key = openssl_pkey_get_public($ssl_public);//这个函数可用来判断公钥是否是可用的
        if (false == $pu_key) return '证书错误';
        openssl_public_encrypt($string, $data, $pu_key);//公钥加密
        $data = base64_encode($data);
        return $data;
    }
}

3、秘钥解密算法

复制代码
<?php
/**
 * RSA加密工具
 * User: 龙哥 三年风水
 * Date: 2024/10/27/0027
 * Time: 21:12
 */
namespace Encipher;
class Encrypt
{

    /**
     * 解密算法-base64解码后采用私钥解密
     * @ param $string
     * @ return string
     */
    public static function decryptRsa($string){
        $ssl_private = file_get_contents("private.pem");
        $pi_key = openssl_pkey_get_private($ssl_private);//这个函数可用来判断私钥是否是可用的,可用返回资源id Resource id
        if (false == $pi_key) return '证书错误';
        openssl_private_decrypt(base64_decode($string), $data, $pi_key);//私钥解密
        return $data;
    }
}

4、调用测试-生成公私钥

a、一定要记得添加路由,在根目录下面route文件夹中的app.php文件中追加以下代码

复制代码
Route::post('index/generate_rsa','Index/generateRsa');

b、在根目录下app文件夹下的controller文件夹中,在Index.php控制器中追加以下代码

复制代码
<?php
namespace app\controller;
use Encipher\Encrypt;

class Index
{

    /**
     * 生成公私钥文件
     * @ return \think\response\Json
     */
    public function generateRsa(){
        $res = Encrypt::generateRsa();
        return succ($res);
    }
}

二、开发Base.php总控业务逻辑

我们根据以下草图可以发现,Base.php是控制登录者信息及验证业务逻辑。那么本控制主要做三件事情,登录后个人资料、角色、菜单:

1、登录信息

a、定义系统必要参数

复制代码
    protected $userId = 0;//用户编号,整个系统内部使用
    protected $avatar = '';//登录用户的头像
    protected $username = '';//登录用户的登录名称
    protected $realname = '';//登录用户的真实名称
    protected $ip = '';//登录用户授权组
    protected $departmentId = 0;//登录用户部门编号
    protected $gradeId = 0;//登录用户级别编号
    protected $isUnserialize = true;//Redis里面有按钮权限
    protected $token = '';//当前用户唯一编码
    protected $roleId = '';//登录用户的权限编号
    protected $key = '';//登录用户权限组标识
    protected $butts = [];//操作权限,整个系统内部使用
    protected $rules = [];//登录用户的权限组,内部使用

b、根据authorization头参数解析数据并查处结果

复制代码
    //初始化方法
    public function initialize(){
        parent::initialize(); // TODO: Change the autogenerated stub
        $this->isLoginAuth();//判断是否登录
    }

    //token验证
    private function isLoginAuth(){
        $headInfo = $this->request->header();
        if(!isset($headInfo['authorization']))return err('非法操作!');
        if(empty($headInfo['authorization']))return err('操作异常!');
        $tokenValue = explode('|', Encrypt::encryptRsa($headInfo['authorization']));//解密 --转换token
        if(count($tokenValue) != 2)return warn('登录认证权限错误');
        $token = sha1(sha1($tokenValue[0]).strtotime($tokenValue[1]));
        $res = Token::setToken($token, $headInfo['authorization']);
        if($res['status'] === false)return warn($res['info']);//赋值错误信息
        $this->userId = $res['data']['admin_id'];
        $this->avatar = $res['data']['avatar'];
        $this->username = $res['data']['username'];
        $this->realname = $res['data']['realname'];
        $this->ip = $res['data']['ip'];
        $this->departmentId = $res['data']['department_id'];
        $this->gradeId = $res['data']['grade_id'];
        $this->roleId = $res['data']['role_id'];
        $this->token = $token;
    }

三、提前说明

明天将封装Redis工具、数据库操作工具、及角色方法、菜单方法

相关推荐
两个人的幸福1 天前
Windows 桌面应用自研 PHP 队列(下):完整代码与六大工程化优化
php
BingoGo4 天前
PHP 泛型之殇 泛型 RFC 提案被拒绝
后端·php
JaguarJack4 天前
PHP 泛型之殇 泛型 RFC 提案被拒绝
后端·php
用户3074596982074 天前
PHP 扩展——从入门到理解
php
鹏仔先生5 天前
拷贝漫画APP下载页PHP程序,后台带免费AI写作
php
云水一下5 天前
从零开始学 PHP 系列(一):PHP 的前世今生与开发环境搭建
开发语言·php
xingpanvip5 天前
星盘接口开发文档:本命盘接口指南
android·开发语言·css·php·lua
酉鬼女又兒5 天前
零基础入门计算机网络运输层:端到端通信核心作用、端口号分类规则、复用分用工作机制及UDP与TCP协议全方位对比详解
网络·网络协议·tcp/ip·计算机网络·考研·udp·php
dog2505 天前
不要再继续优化 TCP
网络协议·tcp/ip·php
Channing Lewis5 天前
PHP 解析 Excel 的那些坑:一次“行号错位”引发的数据丢失
开发语言·php·excel