WxEmployeeService.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. namespace App\Service\Weixin;
  3. use App\Model\Employee;
  4. use App\Model\WxEmployeeOfficial;
  5. use App\Service\Service;
  6. use App\Model\WxEmployee;
  7. use Illuminate\Support\Facades\Hash;
  8. class WxEmployeeService extends Service
  9. {
  10. public function setUser($data){
  11. if(empty($data['openid'])) return [false, 'openId不能为空!'];
  12. $openid = $data['openid'];
  13. $user = WxEmployeeOfficial::where('openid',$openid)->first();
  14. if(empty($user)) {
  15. $user = new WxEmployeeOfficial();
  16. $user->openid = $openid;
  17. $user->appid = WeixinService::APPID;
  18. $user->save();
  19. $state = 0;
  20. }else{
  21. $state = 1;
  22. if(empty($user->employee_id)) $state = 0;
  23. }
  24. return [true,['openid'=>$openid, 'state'=>$state ]];
  25. }
  26. public function login($data,$openid){
  27. if(empty($data['account'])) return [false, '账号不能为空'];
  28. if(empty($data['password'])) return [false, '密码不能为空'];
  29. if(empty($openid) || $openid == null) return [false, 'ciphertext不能为空'];
  30. $account = $data['account'];
  31. $password = $data['password'];
  32. list($status,$data) = $this->loginRule([
  33. 'account' => $account,
  34. 'password' => $password,
  35. ]);
  36. if(! $status) return [false, $data];
  37. $user_id = $data['id'];
  38. $user = WxEmployeeOfficial::where('openid',$openid)->first();
  39. if(empty($user)) {
  40. $bool = WxEmployeeOfficial::where('employee_id',$user_id)->exists();
  41. if($bool) return [false,'该账号已经与其他微信用户绑定!'];
  42. $user = new WxEmployeeOfficial();
  43. $user->openid = $openid;
  44. $user->appid = WeixinService::APPID;
  45. $user->employee_id = $user_id;
  46. $user->save();
  47. }else{
  48. if(empty($user->employee_id)){
  49. $bool = WxEmployeeOfficial::where('employee_id',$user_id)->exists();
  50. if($bool) return [false,'该账号已经与其他微信用户绑定!'];
  51. $user->employee_id = $user_id;
  52. $user->save();
  53. }
  54. if(! empty($user->employee_id) && $user->employee_id != $user_id) return [false,'该账号已经与其他微信用户绑定!'];
  55. }
  56. return [true, $data];
  57. }
  58. public function loginRule($data){
  59. if($this->isEmpty($data,'account')) return [false,'账号不能为空!'];
  60. if($this->isEmpty($data,'password')) return [false,'密码不存在!'];
  61. $account = $data['account'];
  62. $res = Employee::where('del_time',0)
  63. ->where(function ($query)use($account) {
  64. $query->where('account', $account)
  65. ->orWhere('mobile', $account);
  66. })
  67. ->get()->toArray();
  68. if(empty($res)) return [false,'账号不存在或已被删除!'];
  69. if(count($res) > 1) return [false,'手机号绑定多个账户!'];
  70. $res = reset($res);
  71. if($res['state'] == Employee::NOT_USE) return [false,'账号停用!'];
  72. if(empty($res['password'])){
  73. if(empty($res['mobile'])) return [false, '用户手机号码信息不能为空'];
  74. $lastFour = substr($res['mobile'], -4);
  75. if($lastFour != $data['password']) return [false,'密码错误!'];
  76. Employee::where('id', $res['id'])
  77. ->update(['password' => Hash::make($data['password'])]);
  78. }else{
  79. if(! Hash::check($data['password'], $res['password'])) return [false,'密码错误!'];
  80. }
  81. return [true, ['id'=>$res['id'], 'name'=>$res['emp_name'], 'account' => $res['account']]];
  82. }
  83. }