群控系统服务端开发模式-应用开发-业务架构逻辑开发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工具、数据库操作工具、及角色方法、菜单方法

相关推荐
BingoGo2 小时前
OpenSwoole 26.2.0 发布:支持 PHP 8.5、io_uring 后端及协程调试改进
后端·php
JaguarJack2 小时前
OpenSwoole 26.2.0 发布:支持 PHP 8.5、io_uring 后端及协程调试改进
后端·php·服务端
JaguarJack1 天前
推荐 PHP 属性(Attributes) 简洁读取 API 扩展包
后端·php·服务端
BingoGo1 天前
推荐 PHP 属性(Attributes) 简洁读取 API 扩展包
php
初次攀爬者2 天前
ZooKeeper 实现分布式锁的两种方式
分布式·后端·zookeeper
JaguarJack2 天前
告别 Laravel 缓慢的 Blade!Livewire Blaze 来了,为你的 Laravel 性能提速
后端·php·laravel
郑州光合科技余经理3 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
QQ5110082853 天前
python+springboot+django/flask的校园资料分享系统
spring boot·python·django·flask·node.js·php
WeiXin_DZbishe3 天前
基于django在线音乐数据采集的设计与实现-计算机毕设 附源码 22647
javascript·spring boot·mysql·django·node.js·php·html5
longxiangam3 天前
Composer 私有仓库搭建
php·composer