| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- <?php
- namespace App\Service\Weixin;
- use App\Model\CustomerSupply;
- use App\Model\Employee;
- use App\Model\WxEmployeeOfficial;
- use App\Service\Service;
- class WxEmployeeService extends Service
- {
- public function setUser($data){
- if(empty($data['openid'])) return [false, 'openid不能为空'];
- if(empty($data['login_type']) || ! isset(WxEmployeeOfficial::$login_type_title[$data['login_type']])) return [false, '绑定类型不存在或错误'];
- $openid = $data['openid'];
- $appid = config("wx_msg.f_appid");
- $user = WxEmployeeOfficial::where('openid',$openid)
- ->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'],
- ]);
- }
- }
- }
- }
|