where('type', $data['login_type']) ->where('appid', $appid) ->first(); $state = 0; if(empty($user)) { $user = new WxEmployeeOfficial(); $user->openid = $openid; $user->type = $data['login_type']; $user->appid = $appid; $user->save(); }else{ if(! empty($user->employee_id)) $state = 1; } return [true, ['openid'=>$openid, 'state'=>$state]]; } public function login($data, $openid){ list($status, $a_data) = $this->loginRule($data, $openid); if(! $status) return [false, $a_data]; list($user, $customer) = $a_data; try{ $user->employee_id = $customer['id']; $user->save(); //更新绑定关系 if($data['login_type'] == WxEmployeeOfficial::login_type_one){ $this->updateWxEmployeeOfficial($customer['mobile'], $openid); } }catch (\Exception $exception){ return [false, $exception->getMessage()]; } return [true, '']; } public function loginRule($data, $openid){ if(empty($data['account'])) return [false, '账号不能为空']; if(empty($data['password'])) return [false, '密码不能为空']; if(empty($openid) || $openid == null) return [false, 'ciphertext不能为空']; if(empty($data['login_type']) || ! isset(WxEmployeeOfficial::$login_type_title[$data['login_type']])) return [false, '绑定类型不存在或错误']; $appid = config("wx_msg.f_appid"); $user = WxEmployeeOfficial::where('openid',$openid) ->where('type', $data['login_type']) ->where('appid',$appid) ->first(); if(! empty($user)) { if(! empty($user->employee_id)) return [false, '微信用户已绑定系统账号']; }else{ $user = new WxEmployeeOfficial(); $user->openid = $openid; $user->type = $data['login_type']; $user->appid = $appid; $user->save(); } if($data['login_type'] == WxEmployeeOfficial::login_type_one){ //供应商 $customer = CustomerSupply::where('del_time',0) ->where('account', $data['account']) ->where('password', $data['password']) ->first(); if(empty($customer)) return [false, '账号或者密码错误,绑定失败']; $customer = $customer->toArray(); }else{ //企业内部员工 $customer = Employee::where('del_time',0) ->where('account', $data['account']) ->first(); if(empty($customer)) return [false,'账号不存在,绑定失败']; $customer = $customer->toArray(); $lastFour = substr($customer['mobile'], -4); if($lastFour != $data['password']) return [false,'密码错误,绑定失败']; } $bool = WxEmployeeOfficial::where('employee_id',$customer['id']) ->where('type', $data['login_type']) ->where('appid',$appid) ->exists(); if($bool) return [false, '系统账号已被其他微信用户绑定,绑定失败']; return [true, [$user, $customer]]; } public function updateWxEmployeeOfficial($mobile, $openid){ $other = CustomerSupply::where('mobile', $mobile) ->where('type',CustomerSupply::type_two) ->get()->toArray(); foreach ($other as $value){ WxEmployeeOfficial::firstOrCreate([ 'appid' => config("wx_msg.f_appid"), 'type' => WxEmployeeOfficial::login_type_one, 'openid' => $openid, 'employee_id' => $value['id'], ]); } } public function updateWxEmployeeOfficial2($mobile){ $other = CustomerSupply::where('mobile', $mobile) ->where('type',CustomerSupply::type_two) ->get()->toArray(); $other_id = array_column($other,'id'); $wx = WxEmployeeOfficial::whereIn('employee_id',$other_id) ->where('type', WxEmployeeOfficial::login_type_one) ->first(); if(! empty($wx)){ $openid = $wx->openid; foreach ($other as $value){ WxEmployeeOfficial::firstOrCreate([ 'appid' => config("wx_msg.f_appid"), 'type' => WxEmployeeOfficial::login_type_one, 'openid' => $openid, 'employee_id' => $value['id'], ]); } } } }