# hx-admin **Repository Path**: code_yu/hx-admin ## Basic Information - **Project Name**: hx-admin - **Description**: No description available - **Primary Language**: Unknown - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 9 - **Forks**: 10 - **Created**: 2019-11-19 - **Last Updated**: 2025-02-17 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README HX-Admin =============== >基于ThinkPHP6.0开发 ## 运行环境 * 服务:`apche2.4+` 或 `Nginx` * 开发语言:`PHP7.1+` ,必须开启的扩展有: `openssl` * 数据库: `MYSQL 8.0+` ## 使用 1. 为项目添加一个站点 2. 导入数据文件mysql.sql(文件在data/mysql/mysql.sql) ### 上传文件配置 Nginx 挂载虚拟目录:修改 Nginx.htaccess ```aidl location /file/ { # 此处为你的真实路径 alias E:/hx/hx-admin/file/; } ``` Apche 挂载虚拟目录:[传送门](https://blog.csdn.net/qq_36308324/article/details/88086907) ## 返回的基本状态码 | Code | 说明 | |-------------|---------------------| | -2 | 缺少token | | -1 | 未登录 | | 0 | 正常 | | 9999 | 异常错误 | | >=1000 | 缺少请求参数错误 | | >=2000 | 普通错误 | ## 跨域 使用nginx实现跨域 ```js Nginx server 里添加 add_header Access-Control-Allow-Origin *; add_header Access-Control-Allow-Methods 'GET, POST, PATCH, PUT, DELETE, OPTIONS'; add_header Access-Control-Allow-Headers 'token, Authorization, Content-Type, If-Match, If-Modified-Since, If-None-Match, If-Unmodified-Since, X-CSRF-TOKEN, X-Requested-With'; if ($request_method = 'OPTIONS') { return 204; } ``` ##### 例子 ```js server { listen 80; server_name 127.0.0.1 127.0.0.1; root "E:/hx/hx-admin/public"; add_header Access-Control-Allow-Origin *; add_header Access-Control-Allow-Methods 'GET, POST, PATCH, PUT, DELETE, OPTIONS'; add_header Access-Control-Allow-Headers 'token, Authorization, Content-Type, If-Match, If-Modified-Since, If-None-Match, If-Unmodified-Since, X-CSRF-TOKEN, X-Requested-With'; if ($request_method = 'OPTIONS') { return 204; } location / { index index.php index.html error/index.html; error_page 400 /error/400.html; error_page 403 /error/403.html; error_page 404 /error/404.html; include E:/hx/hx-admin/public/nginx.htaccess; autoindex off; } location ~ \.php(.*)$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_split_path_info ^((?U).+\.php)(/?.+)$; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info; include fastcgi_params; } } ``` ## 传递的数据使用`crypto-js`来加密/解密 * 思路: * 1.用户访问登录页 * 2.后台随机生成key和iv并缓存起来 * 3.用户点击登录时用key和iv加密数据 * 4.用户提交数据至后台 * 5.后台用缓存的key和iv解密数据 * 6.验证数据并清除key和iv缓存 * 7.登录成功或失败(失败后从新生成key和iv并返回) ##### JS中使用 >安装`npm`包 `npm install crypto-js` ```javascript //加密代码 const CryptoJS = require("crypto-js"); const key = CryptoJS.enc.Latin1.parse('1234567812345678'); const iv = CryptoJS.enc.Latin1.parse('1234567812345678'); const encoded = CryptoJS.AES.encrypt('hahaha', key, { iv: iv, mode: CryptoJS.mode.CBC,//模式 adding: CryptoJS.pad.ZeroPadding }).toString() console.log('encoded', encoded) //输出如下 //encoded 6bcgYd4f4ZgNOQH/3tqMpg== //js解密代码 const CryptoJS = require("crypto-js"); const key = CryptoJS.enc.Latin1.parse('123456781234567812345678'); const iv = CryptoJS.enc.Latin1.parse('1234567812345678'); const decoded = CryptoJS.AES.decrypt(encoded, key, { iv: iv, mode: CryptoJS.mode.CBC, //模式 adding: CryptoJS.pad.ZeroPadding }).toString(CryptoJS.enc.Utf8) console.log('decoded', decoded); //输出如下 //decoded hahaha ``` ##### PHP 中解码 ```php // 1.php 直接使用openssl解密即可,代码如下: $encoded = '6bcgYd4f4ZgNOQH/3tqMpg=='; $key = '123456781234567812345678'; $iv = '1234567812345678'; var_dump(openssl_decrypt($encoded, 'AES-192-CBC', $key, 0,$iv)); // 注意事项: // 1、AES加密位数跟密钥key有关, 以下是密钥位数和加密对应关系 // 16 => AES-128 // 24 => AES-192 // 32 => AES-256 // 2、iv是初始化向量. 超过16字节或者不足16字节都会被补足16字节或者截断到16字节。由于AES是块加密,铭文被分割成固定长度的块(一般是16字节长度),所以iv也是16字节。 // 3、CBC是加密模式 ```