【类 型】:refactor

【主	题】:修改maketkoen 函数 及之前的调用
【描	述】:
	[原因]:之前只能指定参数 指定字段,改为数组来传,方便灵活
	[过程]:改成输出传值
	[影响]:
【结	束】

# 类型 包含:
# feat:新功能(feature)
# fix:修补bug
# docs:文档(documentation)
# style: 格式(不影响代码运行的变动)
# refactor:重构(即不是新增功能,也不是修改bug的代码变动)
# test:增加测试
# chore:构建过程或辅助工具的变动
This commit is contained in:
tk 2024-06-20 17:10:35 +08:00
parent 602da1570b
commit 9e6afd0665
4 changed files with 33 additions and 25 deletions

View File

@ -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 {
//数据库写入失败

View File

@ -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',

View File

@ -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 位替换成星号

View File

@ -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');
}
}