From 9e6afd066523daabadf9436265c1855076efccf6 Mon Sep 17 00:00:00 2001 From: tk Date: Thu, 20 Jun 2024 17:10:35 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E7=B1=BB=09=E5=9E=8B=E3=80=91?= =?UTF-8?q?=EF=BC=9Arefactor=20=E3=80=90=E4=B8=BB=09=E9=A2=98=E3=80=91?= =?UTF-8?q?=EF=BC=9A=E4=BF=AE=E6=94=B9maketkoen=20=E5=87=BD=E6=95=B0=20?= =?UTF-8?q?=E5=8F=8A=E4=B9=8B=E5=89=8D=E7=9A=84=E8=B0=83=E7=94=A8=20?= =?UTF-8?q?=E3=80=90=E6=8F=8F=09=E8=BF=B0=E3=80=91=EF=BC=9A=20=09[?= =?UTF-8?q?=E5=8E=9F=E5=9B=A0]=EF=BC=9A=E4=B9=8B=E5=89=8D=E5=8F=AA?= =?UTF-8?q?=E8=83=BD=E6=8C=87=E5=AE=9A=E5=8F=82=E6=95=B0=20=E6=8C=87?= =?UTF-8?q?=E5=AE=9A=E5=AD=97=E6=AE=B5=EF=BC=8C=E6=94=B9=E4=B8=BA=E6=95=B0?= =?UTF-8?q?=E7=BB=84=E6=9D=A5=E4=BC=A0=EF=BC=8C=E6=96=B9=E4=BE=BF=E7=81=B5?= =?UTF-8?q?=E6=B4=BB=20=09[=E8=BF=87=E7=A8=8B]=EF=BC=9A=E6=94=B9=E6=88=90?= =?UTF-8?q?=E8=BE=93=E5=87=BA=E4=BC=A0=E5=80=BC=20=09[=E5=BD=B1=E5=93=8D]?= =?UTF-8?q?=EF=BC=9A=20=E3=80=90=E7=BB=93=09=E6=9D=9F=E3=80=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # 类型 包含: # feat:新功能(feature) # fix:修补bug # docs:文档(documentation) # style: 格式(不影响代码运行的变动) # refactor:重构(即不是新增功能,也不是修改bug的代码变动) # test:增加测试 # chore:构建过程或辅助工具的变动 --- .../Api/Controller/LoginController.class.php | 2 +- .../Api/Controller/PayController.class.php | 3 +- .../Api/Controller/PublicController.class.php | 22 +++++++------ .../Controller/LoginController.class.php | 31 ++++++++++--------- 4 files changed, 33 insertions(+), 25 deletions(-) diff --git a/FlyCube/Api/Controller/LoginController.class.php b/FlyCube/Api/Controller/LoginController.class.php index 02296f9..610fc28 100644 --- a/FlyCube/Api/Controller/LoginController.class.php +++ b/FlyCube/Api/Controller/LoginController.class.php @@ -56,7 +56,7 @@ class LoginController extends PublicController if ($id = $userDb->data($data)->add()) { $user = $userDb->find($id); //获取刚刚插入的记录 //分发token等用户信息给前端 - $token = $this->makeToken($getAuth['openid'], $getAuth['session_key']); + $token = $this->makeToken(array('openid' => $getAuth['openid'], 'session_key' => $getAuth['session_key'])); $userInfo = array('token' => $token, 'name' => $user['name'], 'photo' => $user['photo'], 'sex' => $user['sex'], 'tel' => $user['tel'], 'topic_prefix' => makeTopicPrefix($getAuth['openid'])); } else { //数据库写入失败 diff --git a/FlyCube/Api/Controller/PayController.class.php b/FlyCube/Api/Controller/PayController.class.php index 18de8b8..cbbb0c0 100644 --- a/FlyCube/Api/Controller/PayController.class.php +++ b/FlyCube/Api/Controller/PayController.class.php @@ -59,9 +59,10 @@ class PayController extends PublicController $orderDb = D('order'); $order = $orderDb->where($where)->find(); //设置获取签名的订单参数 + $token = $this->makeToken(array('shop_id' => $order['shop_id'], 'exp' => strtotime('+100 years'))); $orderParameter = [ 'out_trade_no' => $order['order_sn'], - 'description' => $order['shop_id'], //这个字段用 商铺id 方便分类查询腾讯的支付订单,还有在回调时候发送“订单更新主题”的子主题区分 + 'description' => $token, //用jwt加密 shop_id商铺id 存放 'amount' => [ 'total' => $order['total_price'] * 100, //单位:分 'currency' => 'CNY', diff --git a/FlyCube/Api/Controller/PublicController.class.php b/FlyCube/Api/Controller/PublicController.class.php index 8a45133..69a2e75 100644 --- a/FlyCube/Api/Controller/PublicController.class.php +++ b/FlyCube/Api/Controller/PublicController.class.php @@ -57,25 +57,29 @@ class PublicController extends Controller } /** * @description: 构建token - * @param {*} openid 微信用户唯一凭证 + * @param {array} data 要存储在 JWT 中的数据,键值对形式 ps:这里可以传exp 来覆盖默认的过期时间 * @return {*} token */ - protected function makeToken($openid, $session_key) + protected function makeToken($data = []) { // 定义密钥,应该使用安全的随机字符串并妥善保管 $jwtKey = C('jwtKey'); $currtime = time(); - // 要存储在 JWT 中的数据 - $data = [ - 'iat' => $currtime, // 签发时间(时间戳) + + // 默认的数据 + $defaultData = [ + 'iat' => $currtime, // 签发时间(时间戳) 'iss' => 'jwt_admin', // 签发者 - 'nbf' => $currtime, // 在此时间之前不可用 (这里是2秒以内) + 'nbf' => $currtime, // 在此时间之前不可用 (这里是2秒以内) 'exp' => strtotime('tomorrow'), //过期时间 到第二天凌晨 - 'openid' => $openid, - 'session_key' => $session_key, + 'jti' => md5(uniqid('JWT') . $currtime), // JWT ID:令牌的唯一标识符 ]; + + // 合并默认数据和传递的数据 + $tokenData = array_merge($defaultData, $data); + // 使用密钥和 HS256 算法对数据进行编码生成 JWT - return JWT::encode($data, $jwtKey, 'HS256'); + return JWT::encode($tokenData, $jwtKey, 'HS256'); } /** * @description: 将手机号的倒数第 5 位到第 8 位替换成星号 diff --git a/FlyCube/MpApi/Controller/LoginController.class.php b/FlyCube/MpApi/Controller/LoginController.class.php index 48db682..3f43dab 100644 --- a/FlyCube/MpApi/Controller/LoginController.class.php +++ b/FlyCube/MpApi/Controller/LoginController.class.php @@ -50,7 +50,7 @@ class LoginController extends Controller //删除多余信息 unset($adminInfo['pwd']); //创建token - $token = $this->makeToken(); + $token = $this->makeToken(array('shop_id' => $this->shop_id)); //登陆成功 返回token echo json_encode(array('status' => 1, 'msg' => '登陆成功', 'adminInfo' => $adminInfo, 'token' => $token), JSON_UNESCAPED_UNICODE); } else { @@ -65,24 +65,27 @@ class LoginController extends Controller } } /** - * @Description: 构建token - * @Return: token + * @description: 构建token + * @param {array} data 要存储在 JWT 中的数据,键值对形式 ps:这里可以传exp 来覆盖默认的过期时间 + * @return {*} token */ - private function makeToken() + private function makeToken($data = []) { $jwtKey = C('jwtKey'); // jwt密钥 $currtime = time(); - // 要存储在 JWT 中的数据 - $data = [ - 'iat' => $currtime, // 签发时间(时间戳) - 'iss' => 'jwt_admin', // 签发者 - 'nbf' => $currtime, // 在此时间之前不可用 (这里是2秒以内) - 'exp' => strtotime('tomorrow'), //过期时间 到第二天凌晨 - 'jti' => md5(uniqid('JWT') . $currtime), - 'sub' => 'http://localhost:8080', - 'shop_id' => $this->shop_id, + // 默认的数据 + $defaultData = [ + 'iat' => $currtime, // 签发时间(时间戳) + 'iss' => 'jwt_admin', // 签发者 + 'nbf' => $currtime, // 在此时间之前不可用 (这里是2秒以内) + 'exp' => strtotime('tomorrow'), //过期时间 到第二天凌晨 + 'jti' => md5(uniqid('JWT') . $currtime), // JWT ID:令牌的唯一标识符 ]; + + // 合并默认数据和传递的数据 + $tokenData = array_merge($defaultData, $data); + // 使用密钥和 HS256 算法对数据进行编码生成 JWT - return JWT::encode($data, $jwtKey, 'HS256'); + return JWT::encode($tokenData, $jwtKey, 'HS256'); } }