作者 何书鹏

优化授权获取手机号

@@ -136,7 +136,6 @@ class User extends Api @@ -136,7 +136,6 @@ class User extends Api
136 * @ApiTitle (用户授权获取手机号) 136 * @ApiTitle (用户授权获取手机号)
137 * @ApiMethod (POST) 137 * @ApiMethod (POST)
138 * @ApiHeaders (name=token, type=string, required=true, description="请求的Token") 138 * @ApiHeaders (name=token, type=string, required=true, description="请求的Token")
139 - * @ApiParams (name="sessionKey", type="string", required=true, description="小程序sessionKey")  
140 * @ApiParams (name="iv", type="string", required=true, description="小程序iv") 139 * @ApiParams (name="iv", type="string", required=true, description="小程序iv")
141 * @ApiParams (name="encryptedData", type="string", required=true, description="小程序encryptedData") 140 * @ApiParams (name="encryptedData", type="string", required=true, description="小程序encryptedData")
142 * @ApiReturn ({ 141 * @ApiReturn ({
@@ -151,26 +150,27 @@ class User extends Api @@ -151,26 +150,27 @@ class User extends Api
151 { 150 {
152 $param = $this->request->param(); 151 $param = $this->request->param();
153 $validate = new \think\Validate([ 152 $validate = new \think\Validate([
154 - 'sessionKey' => 'require',  
155 'iv' => 'require', 153 'iv' => 'require',
156 'encryptedData' => 'require', 154 'encryptedData' => 'require',
157 ]); 155 ]);
158 $validate->message([ 156 $validate->message([
159 - 'sessionKey.require' => 'sessionKey参数错误!',  
160 'iv.require' => 'iv参数错误!', 157 'iv.require' => 'iv参数错误!',
161 'encryptedData.require' => 'encryptData参数错误!', 158 'encryptedData.require' => 'encryptData参数错误!',
162 ]); 159 ]);
163 if (!$validate->check($param)) { 160 if (!$validate->check($param)) {
164 $this->error($validate->getError()); 161 $this->error($validate->getError());
165 } 162 }
  163 + // 获取session_key
  164 + $user = $this->auth->getUser();
  165 + $third = \app\api\model\Third::where('user_id',$user['id'])->field('session_key')->find();
  166 + empty($third) && $this->error('请先登录');
166 // 获取小程序配置 167 // 获取小程序配置
167 $app = Factory::miniProgram([ 168 $app = Factory::miniProgram([
168 'app_id' => Config::get('site.appid'), 169 'app_id' => Config::get('site.appid'),
169 'secret' => Config::get('site.secret'), 170 'secret' => Config::get('site.secret'),
170 ]); 171 ]);
171 - $res = $app->encryptor->decryptData($param['sessionKey'], $param['iv'], $param['encryptedData']); 172 + $res = $app->encryptor->decryptData($third['session_key'], $param['iv'], $param['encryptedData']);
172 // 更新手机号 173 // 更新手机号
173 - $user = $this->auth->getUser();  
174 $user->mobile = $res['purePhoneNumber']; 174 $user->mobile = $res['purePhoneNumber'];
175 $user->save(); 175 $user->save();
176 $this->success('授权成功',['mobile' => $res['purePhoneNumber']]); 176 $this->success('授权成功',['mobile' => $res['purePhoneNumber']]);