Kaynağa Gözat

[智价云] 扫码绑定信息更新

tangyuanwang 1 hafta önce
ebeveyn
işleme
d5063e376a

+ 32 - 19
app/Http/Controllers/Manager/Login.php

@@ -12,6 +12,7 @@ use App\Models\Manager\Personnel\EmployeeOpenid as EmployeeOpenidModel;
 use App\Servers\Wechat\WeChatWebApp;
 use App\Models\Manager\Personnel\RolesAuthRule as RolesAuthRuleModel;
 use Illuminate\Support\Facades\Cache;
+use Illuminate\Support\Facades\DB;
 
 /**
  * 管理后台登录控制器
@@ -32,7 +33,7 @@ class Login extends Manager
 	 * @param string 	password	登录密码
 	 * 
 	 * */
-	public function index(Request $Request, AdminUser $AdminUser, AuthRule $AuthRule, EmployeeModel $EmployeeModel,RolesAuthRuleModel $RolesAuthRuleModel)
+	public function index(Request $Request, AdminUser $AdminUser, AuthRule $AuthRule, EmployeeModel $EmployeeModel, RolesAuthRuleModel $RolesAuthRuleModel)
 	{
 		// 验证规则
 		$Request->scene('login')->validate();
@@ -60,7 +61,7 @@ class Login extends Manager
 			$accessToken['is_system_admin']			= 1;
 			// 获取权限列表
 		} else {
-			$admin    	= $EmployeeModel->where('employee_code', $username)->first(['company_id','id as uid', 'name as username', 'mobile as phone', 'status', 'password', 'insert_time', 'update_time']);
+			$admin    	= $EmployeeModel->where('employee_code', $username)->first(['company_id', 'id as uid', 'name as username', 'mobile as phone', 'status', 'password', 'insert_time', 'update_time']);
 			// 用户不存在
 			if (!$admin)	return json_send(['code' => 'error', 'msg' => '密码错误或账号不存在']);
 			// 用户不存在
@@ -70,7 +71,7 @@ class Login extends Manager
 			// 比对密码
 			if (md5($password) != $admin['password']) return json_send(['code' => 'error', 'msg' => '密码错误或账号不存在']);
 			// 登录
-			$accessToken 						= $EmployeeModel->Login($admin['uid'],$admin['company_id'], 'manager');
+			$accessToken 						= $EmployeeModel->Login($admin['uid'], $admin['company_id'], 'manager');
 			// 比对密码
 			if (isset($accessToken['error'])) 	return json_send(['code' => 'error', 'msg' => '登录失败', 'data' => $accessToken['data']]);
 			// 获取权限列表
@@ -154,7 +155,7 @@ class Login extends Manager
 		// 接收数据
 		$password    = $Request->input('password', '');
 		// 查询用户
-		$admin    	= $EmployeeModel->where('mobile', $phone)->first(['company_id','id as uid', 'name as username', 'mobile as phone', 'status', 'password', 'insert_time', 'update_time']);
+		$admin    	= $EmployeeModel->where('mobile', $phone)->first(['company_id', 'id as uid', 'name as username', 'mobile as phone', 'status', 'password', 'insert_time', 'update_time']);
 		// 用户不存在
 		if (!$admin)	return json_send(['code' => 'error', 'msg' => '密码错误或账号不存在']);
 		// 用户不存在
@@ -164,7 +165,7 @@ class Login extends Manager
 		// 比对密码
 		if (md5($password) != $admin['password']) return json_send(['code' => 'error', 'msg' => '密码错误或账号不存在']);
 		// 登录
-		$accessToken 						= $EmployeeModel->Login($admin['uid'],$admin['company_id'], 'manager');
+		$accessToken 						= $EmployeeModel->Login($admin['uid'], $admin['company_id'], 'manager');
 		// 比对密码
 		if (isset($accessToken['error'])) 	return json_send(['code' => 'error', 'msg' => '登录失败', 'data' => $accessToken['data']]);
 		// 获取权限列表
@@ -222,7 +223,7 @@ class Login extends Manager
 		// 接收数据
 		$password    = $Request->input('password', '');
 		// 查询用户
-		$admin    	= $EmployeeModel->where('email', $email)->first(['company_id','id as uid', 'name as username', 'mobile as phone', 'status', 'password', 'insert_time', 'update_time']);
+		$admin    	= $EmployeeModel->where('email', $email)->first(['company_id', 'id as uid', 'name as username', 'mobile as phone', 'status', 'password', 'insert_time', 'update_time']);
 		// 用户不存在
 		if (!$admin)	return json_send(['code' => 'error', 'msg' => '密码错误或账号不存在']);
 		// 用户不存在
@@ -232,7 +233,7 @@ class Login extends Manager
 		// 比对密码
 		if (md5($password) != $admin['password']) return json_send(['code' => 'error', 'msg' => '密码错误或账号不存在']);
 		// 登录
-		$accessToken 						= $EmployeeModel->Login($admin['uid'],$admin['company_id'], 'manager');
+		$accessToken 						= $EmployeeModel->Login($admin['uid'], $admin['company_id'], 'manager');
 		// 比对密码
 		if (isset($accessToken['error'])) 	return json_send(['code' => 'error', 'msg' => '登录失败', 'data' => $accessToken['data']]);
 		// 获取权限列表
@@ -264,7 +265,7 @@ class Login extends Manager
 		if (!$session)                     return json_send(['code' => 'error', 'msg' => '请先获取手机号验证码']);
 		if ($session['code'] != $code || $session['mobile'] != $phone) return json_send(['code' => 'error', 'msg' => '验证码错误']);
 		// 查询用户
-		$admin    	= $EmployeeModel->where('mobile', $phone)->first(['company_id','id as uid', 'name as username', 'mobile as phone', 'status', 'password', 'insert_time', 'update_time']);
+		$admin    	= $EmployeeModel->where('mobile', $phone)->first(['company_id', 'id as uid', 'name as username', 'mobile as phone', 'status', 'password', 'insert_time', 'update_time']);
 		// 用户不存在
 		if (!$admin)	return json_send(['code' => 'error', 'msg' => '账号不存在']);
 		// 用户不存在
@@ -272,7 +273,7 @@ class Login extends Manager
 		// 转数组
 		$admin    	= $admin->toArray();
 		// 登录
-		$accessToken 						= $EmployeeModel->Login($admin['uid'],$admin['company_id'], 'manager');
+		$accessToken 						= $EmployeeModel->Login($admin['uid'], $admin['company_id'], 'manager');
 		// 比对密码
 		if (isset($accessToken['error'])) 	return json_send(['code' => 'error', 'msg' => '登录失败', 'data' => $accessToken['data']]);
 		// 获取权限列表
@@ -303,7 +304,7 @@ class Login extends Manager
 		$user_open_data = $EmployeeOpenidModel->where(['openid' => $tokenData['openid']])->first();
 		if (!$user_open_data)   return json_send(['code' => 'error', 'msg' => '未绑定账号']);
 		// 查询用户
-		$admin    	= $EmployeeModel->where('id', $user_open_data->employee_id)->first(['company_id','id as uid', 'name as username', 'mobile as phone', 'status', 'password', 'insert_time', 'update_time']);
+		$admin    	= $EmployeeModel->where('id', $user_open_data->employee_id)->first(['company_id', 'id as uid', 'name as username', 'mobile as phone', 'status', 'password', 'insert_time', 'update_time']);
 		// 用户不存在
 		if (!$admin)	return json_send(['code' => 'error', 'msg' => '账号不存在']);
 		// 用户不存在
@@ -311,7 +312,7 @@ class Login extends Manager
 		// 转数组
 		$admin    	= $admin->toArray();
 		// 登录
-		$accessToken 						= $EmployeeModel->Login($admin['uid'],$admin['company_id'], 'manager');
+		$accessToken 						= $EmployeeModel->Login($admin['uid'], $admin['company_id'], 'manager');
 		// 比对密码
 		if (isset($accessToken['error'])) 	return json_send(['code' => 'error', 'msg' => '登录失败', 'data' => $accessToken['data']]);
 		// 获取权限列表
@@ -329,7 +330,7 @@ class Login extends Manager
 	 * @param string 	open_code		微信扫码登录的code
 	 * 
 	 */
-	public function wechat_bind(Request $Request, EmployeeOpenidModel $EmployeeOpenidModel)
+	public function wechat_bind(Request $Request, EmployeeModel $EmployeeModel, EmployeeOpenidModel $EmployeeOpenidModel)
 	{
 		// 验证规则
 		$Request->scene('wechat_bind')->validate();
@@ -342,15 +343,27 @@ class Login extends Manager
 		$user_open_data = $EmployeeOpenidModel->where(['openid' => $tokenData['openid']])->first();
 		if ($user_open_data)   return json_send(['code' => 'error', 'msg' => '微信已绑定,无需重复绑定']);
 		//新增绑定记录
-		$user_info = $EmployeeOpenidModel->where(['id' => $uid])->first();
+		$user_info = $EmployeeModel->where(['id' => $uid])->first();
 		if (!$user_info) return json_send(['code' => 'error', 'msg' => '未找到用户信息']);
-		$user_info->openid = $tokenData['openid'];
-		$user_info->band_wechat = 1;//绑定微信0=未绑定1=已绑定
-		$update_status = $user_info->save();
-		if ($update_status) {
+		DB::beginTransaction();
+		try {
+			$user_info->band_wechat = 1; //绑定微信0=未绑定1=已绑定
+			$user_info->save();
+			//查询是否存在绑定记录
+			$band_wechat_data = $EmployeeOpenidModel->where(['employee_id' => $uid, 'type' => '3'])->first();
+			if (!$band_wechat_data) {
+				$EmployeeOpenidModel->insertGetId(['employee_id' => $uid, 'openid' => $tokenData['openid'], 'type' => '3', 'insert_time' => time()]);
+			} else {
+				$band_wechat_data->openid = $tokenData['openid'];
+				$band_wechat_data->type = '3';
+				$band_wechat_data->update_time = time();
+				$band_wechat_data->save();
+			}
+			DB::commit();
 			return json_send(['code' => 'success', 'msg' => '绑定成功', 'data' => '']);
-		} else {
-			return json_send(['code' => 'error', 'msg' => '绑定失败', 'data' => '']);
+		} catch (\Exception $e) {
+			DB::rollBack();
+			return json_send(['code' => 'error', 'msg' => '绑定失败', 'data' => $e->getMessage()]);
 		}
 	}
 }

+ 0 - 2
app/Models/Manager/Personnel/Employee.php

@@ -236,8 +236,6 @@ class Employee extends Model
             // 成功处理...
         } catch (\Exception $e) {
             DB::rollBack();
-            print_r($e->getMessage());
-            exit;
             // 错误处理...
             return false;
         }