作者 何书鹏

优化授权获取手机号

... ... @@ -136,7 +136,6 @@ class User extends Api
* @ApiTitle (用户授权获取手机号)
* @ApiMethod (POST)
* @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
* @ApiParams (name="sessionKey", type="string", required=true, description="小程序sessionKey")
* @ApiParams (name="iv", type="string", required=true, description="小程序iv")
* @ApiParams (name="encryptedData", type="string", required=true, description="小程序encryptedData")
* @ApiReturn ({
... ... @@ -151,26 +150,27 @@ class User extends Api
{
$param = $this->request->param();
$validate = new \think\Validate([
'sessionKey' => 'require',
'iv' => 'require',
'encryptedData' => 'require',
]);
$validate->message([
'sessionKey.require' => 'sessionKey参数错误!',
'iv.require' => 'iv参数错误!',
'encryptedData.require' => 'encryptData参数错误!',
]);
if (!$validate->check($param)) {
$this->error($validate->getError());
}
// 获取session_key
$user = $this->auth->getUser();
$third = \app\api\model\Third::where('user_id',$user['id'])->field('session_key')->find();
empty($third) && $this->error('请先登录');
// 获取小程序配置
$app = Factory::miniProgram([
'app_id' => Config::get('site.appid'),
'secret' => Config::get('site.secret'),
]);
$res = $app->encryptor->decryptData($param['sessionKey'], $param['iv'], $param['encryptedData']);
$res = $app->encryptor->decryptData($third['session_key'], $param['iv'], $param['encryptedData']);
// 更新手机号
$user = $this->auth->getUser();
$user->mobile = $res['purePhoneNumber'];
$user->save();
$this->success('授权成功',['mobile' => $res['purePhoneNumber']]);
... ...