瀏覽代碼

[智价云] 微信授权绑定更新

tangyuanwang 3 天之前
父節點
當前提交
e1c65d15f9
共有 2 個文件被更改,包括 19 次插入14 次删除
  1. 18 11
      app/Http/Controllers/Api/Login.php
  2. 1 3
      app/Http/Controllers/Manager/Login.php

+ 18 - 11
app/Http/Controllers/Api/Login.php

@@ -232,7 +232,6 @@ class Login extends Api
 	{
 		// 验证规则
 		$Request->scene('wechat_phone')->validate();
-		$company_id = request('access_token.company_id', '0');
 		// 接收数据
 		$auth_code	= request('auth_code', '');
 		$open_code    = $Request->input('open_code', '');
@@ -245,9 +244,10 @@ class Login extends Api
 		if (empty($result['purePhoneNumber'])) return json_send(['code' => 'error', 'msg' => '未获取到手机号', 'data' => ['error' => '未获取到手机号']]);
 		$phone	= $result['purePhoneNumber'];
 		// 查询用户
-		$phone_user_data = $EmployeeOpenidModel->where(['mobile' => $phone])->first(['company_id', 'id as uid', 'name as username', 'mobile as phone', 'status', 'password', 'insert_time', 'update_time']);
+		$phone_user_data = $EmployeeModel->where(['mobile' => $phone])->first(['company_id', 'id as uid', 'name as username', 'mobile as phone', 'status', 'password', 'insert_time', 'update_time']);
 		if (!$phone_user_data) return json_send(['code' => 'error', 'msg' => '手机号码未授权']);
 		if ($phone_user_data['status']) 				return json_send(['code' => 'error', 'msg' => '该账号已停用']);
+		$company_id = $phone_user_data->company_id;
 		$uid = $phone_user_data->employee_id;
 		//获取用户的openid
 		$tokenData							    = OpenPlat::jscode2session($open_code, $appId);
@@ -260,14 +260,22 @@ class Login extends Api
 			try {
 				$phone_user_data->band_wechat = 1; //绑定微信0=未绑定1=已绑定
 				$phone_user_data->save();
-				$EmployeeOpenidModel->insertGetId([
-					'company_id' => $company_id,
-					'employee_id' => $uid,
-					'unionid' => $tokenData['unionid'] ?? '',
-					'mini_openid' => $tokenData['openid'],
-					'type' => '3',
-					'insert_time' => time()
-				]);
+				$unionid = $tokenData['unionid'] ?? '';
+				$official_user_info = $EmployeeOpenidModel->where(['unionid' => $unionid])->first();
+				if ($official_user_info) {
+					$official_user_info->employee_id = $uid;
+					$official_user_info->mini_openid = $tokenData['openid'];
+					$official_user_info->update_time = time();
+					$official_user_info->save();
+				} else {
+					$EmployeeOpenidModel->insertGetId([
+						'company_id' => $company_id,
+						'employee_id' => $uid,
+						'unionid' => $tokenData['unionid'] ?? '',
+						'mini_openid' => $tokenData['openid'],
+						'insert_time' => time()
+					]);
+				}
 				DB::commit();
 			} catch (\Exception $e) {
 				DB::rollBack();
@@ -276,7 +284,6 @@ class Login extends Api
 		} else {
 			$user_open_data->unionid = $tokenData['unionid'] ?? '';
 			$user_open_data->mini_openid = $tokenData['openid'];
-			$user_open_data->type = '3';
 			$user_open_data->update_time = time();
 			$user_open_data->save();
 		}

+ 1 - 3
app/Http/Controllers/Manager/Login.php

@@ -356,7 +356,7 @@ class Login extends Manager
 			$user_info->band_wechat = 1; //绑定微信0=未绑定1=已绑定
 			$user_info->save();
 			//查询是否存在绑定记录
-			$band_wechat_data = $EmployeeOpenidModel->where(['employee_id' => $uid, 'type' => '3'])->first();
+			$band_wechat_data = $EmployeeOpenidModel->where(['employee_id' => $uid])->first();
 			if (!$band_wechat_data) {
 				//查询关注公众号记录
 				$unionid = $tokenData['unionid'] ?? '';
@@ -372,14 +372,12 @@ class Login extends Manager
 						'employee_id' => $uid,
 						'unionid' => $tokenData['unionid'] ?? '',
 						'openid' => $tokenData['openid'],
-						'type' => '3',
 						'insert_time' => time()
 					]);
 				}
 			} else {
 				$band_wechat_data->unionid = $tokenData['unionid'] ?? '';
 				$band_wechat_data->openid = $tokenData['openid'];
-				$band_wechat_data->type = '3';
 				$band_wechat_data->update_time = time();
 				$band_wechat_data->save();
 			}