Ver Fonte

[智价云] 微信扫码授权绑定校验更新

tangyuanwang há 1 semana atrás
pai
commit
6b17bf580a
1 ficheiros alterados com 12 adições e 10 exclusões
  1. 12 10
      app/Http/Controllers/Manager/Login.php

+ 12 - 10
app/Http/Controllers/Manager/Login.php

@@ -329,25 +329,27 @@ class Login extends Manager
 	 * @param string 	open_code		微信扫码登录的code
 	 * 
 	 */
-	public function wechat_bind(Request $Request,EmployeeOpenidModel $EmployeeOpenidModel)
+	public function wechat_bind(Request $Request, EmployeeOpenidModel $EmployeeOpenidModel)
 	{
 		// 验证规则
 		$Request->scene('wechat_bind')->validate();
 		$uid             = request('access_token.uid', 0);
 		// 接收数据
-		$open_code    = $Request->input('open_code', '');
+		$open_code    = $Request->input('code', '');
 		$wechatApp = new WeChatWebApp();
 		$tokenData = $wechatApp->getAccessTokenByCode($open_code);
 		if (!$tokenData) return json_send(['code' => 'error', 'msg' => '获取微信用户信息失败']);
-		$user_open_data = $EmployeeOpenidModel->where(['openid' => $tokenData['openid'],'employee_id'=> $uid])->first();
+		$user_open_data = $EmployeeOpenidModel->where(['openid' => $tokenData['openid']])->first();
 		if ($user_open_data)   return json_send(['code' => 'error', 'msg' => '微信已绑定,无需重复绑定']);
 		//新增绑定记录
-		$EmployeeOpenidModel->create([
-			'openid' => $tokenData['openid'],
-			'unionid' => isset($tokenData['unionid']) ? $tokenData['unionid'] : '',
-			'employee_id' => $uid,
-			'insert_time' => time(),
-		]);
-		return json_send(['code' => 'success', 'msg' => '绑定成功','data'=>'']);
+		$user_info = $EmployeeOpenidModel->where(['id' => $uid])->first();
+		if (!$user_info) return json_send(['code' => 'error', 'msg' => '未找到用户信息']);
+		$user_info->openid = $tokenData['openid'];
+		$update_status = $user_info->save();
+		if ($update_status) {
+			return json_send(['code' => 'success', 'msg' => '绑定成功', 'data' => '']);
+		} else {
+			return json_send(['code' => 'error', 'msg' => '绑定失败', 'data' => '']);
+		}
 	}
 }