123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- <?php
- namespace App\Service\Weixin;
- use App\Model\Employee;
- use App\Model\WxEmployeeOfficial;
- use App\Service\Service;
- use App\Model\WxEmployee;
- use Illuminate\Support\Facades\Hash;
- class WxEmployeeService extends Service
- {
- public function setUser($data){
- if(empty($data['openid'])) return [false, 'openId不能为空!'];
- $openid = $data['openid'];
- $user = WxEmployeeOfficial::where('openid',$openid)->first();
- if(empty($user)) {
- $user = new WxEmployeeOfficial();
- $user->openid = $openid;
- $user->appid = WeixinService::APPID;
- $user->save();
- $state = 0;
- }else{
- $state = 1;
- if(empty($user->employee_id)) $state = 0;
- }
- return [true,['openid'=>$openid, 'state'=>$state ]];
- }
- public function login($data,$openid){
- if(empty($data['account'])) return [false, '账号不能为空'];
- if(empty($data['password'])) return [false, '密码不能为空'];
- if(empty($openid) || $openid == null) return [false, 'ciphertext不能为空'];
- $account = $data['account'];
- $password = $data['password'];
- list($status,$data) = $this->loginRule([
- 'account' => $account,
- 'password' => $password,
- ]);
- if(! $status) return [false, $data];
- $user_id = $data['id'];
- $user = WxEmployeeOfficial::where('openid',$openid)->first();
- if(empty($user)) {
- $bool = WxEmployeeOfficial::where('employee_id',$user_id)->exists();
- if($bool) return [false,'该账号已经与其他微信用户绑定!'];
- $user = new WxEmployeeOfficial();
- $user->openid = $openid;
- $user->appid = WeixinService::APPID;
- $user->employee_id = $user_id;
- $user->save();
- }else{
- if(empty($user->employee_id)){
- $bool = WxEmployeeOfficial::where('employee_id',$user_id)->exists();
- if($bool) return [false,'该账号已经与其他微信用户绑定!'];
- $user->employee_id = $user_id;
- $user->save();
- }
- if(! empty($user->employee_id) && $user->employee_id != $user_id) return [false,'该账号已经与其他微信用户绑定!'];
- }
- return [true, $data];
- }
- public function loginRule($data){
- if($this->isEmpty($data,'account')) return [false,'账号不能为空!'];
- if($this->isEmpty($data,'password')) return [false,'密码不存在!'];
- $account = $data['account'];
- $res = Employee::where('del_time',0)
- ->where(function ($query)use($account) {
- $query->where('account', $account)
- ->orWhere('mobile', $account);
- })
- ->get()->toArray();
- if(empty($res)) return [false,'账号不存在或已被删除!'];
- if(count($res) > 1) return [false,'手机号绑定多个账户!'];
- $res = reset($res);
- if($res['state'] == Employee::NOT_USE) return [false,'账号停用!'];
- if(empty($res['password'])){
- if(empty($res['mobile'])) return [false, '用户手机号码信息不能为空'];
- $lastFour = substr($res['mobile'], -4);
- if($lastFour != $data['password']) return [false,'密码错误!'];
- Employee::where('id', $res['id'])
- ->update(['password' => Hash::make($data['password'])]);
- }else{
- if(! Hash::check($data['password'], $res['password'])) return [false,'密码错误!'];
- }
- return [true, ['id'=>$res['id'], 'name'=>$res['emp_name'], 'account' => $res['account']]];
- }
- }
|