WxEmployeeService.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. namespace App\Service\Wx;
  3. use App\Model\Employee;
  4. use App\Model\EmployeeDepartPermission;
  5. use App\Model\EmployeeFoursShop;
  6. use App\Model\EmployeeTeamPermission;
  7. use App\Model\FoursShop;
  8. use App\Model\StorehouseEmployee;
  9. use App\Model\TemporaryJs;
  10. use App\Model\WxEmployee;
  11. use App\Service\EmployeeService;
  12. use Illuminate\Support\Facades\DB;
  13. use Illuminate\Support\Facades\Hash;
  14. /**
  15. * 区域相关
  16. * @package App\Models
  17. */
  18. class WxEmployeeService extends Service
  19. {
  20. public function setUser($data){
  21. if(empty($data['code'])) return [false, '用户登录凭证(code)不能为空'];
  22. $code = $data['code'];
  23. $service = new WxService();
  24. list($status,$openid) = $service->getOpenid($code);
  25. if(!$status) return [false,$openid];
  26. $user = WxEmployee::where('openid',$openid)->first();
  27. if(empty($user)) {
  28. $user = new WxEmployee();
  29. $user->mobile = '';
  30. $user->openid = $openid;
  31. $user->appid = $service->appid;
  32. $user->save();
  33. $state = 0;
  34. }else{
  35. $state = 1;
  36. if(empty($user->mobile)) $state = 0;
  37. }
  38. return [true,['openid'=>$openid, 'state'=> $state]];
  39. }
  40. public function setMobile($data){
  41. if(empty($data['code'])) return [false, '用户登录凭证(code)不能为空'];
  42. if(empty($data['openid'])) return [false, 'openID不能为空'];
  43. $code = $data['code'];
  44. $openid = $data['openid'];
  45. $service = new WxService();
  46. list($status,$mobile) = $service->getMobile($code);
  47. if(!$status) return [false,$mobile];
  48. $user = WxEmployee::where('openid',$openid)->first();
  49. if(empty($user)) return [false,'openID在系统中暂无记录'];
  50. $user->mobile = $mobile;
  51. $user->save();
  52. return [true, ['mobile' => $mobile]];
  53. }
  54. public function login($data){
  55. if(empty($data['account'])) return [false, '账号不能为空'];
  56. if(empty($data['password'])) return [false, '密码不能为空'];
  57. if(empty($data['openid'])) return [false, 'openID不能为空'];
  58. $account = $data['account'];
  59. $password = $data['password'];
  60. $openid = $data['openid'];
  61. list($status,$data) = $this->loginRule([
  62. 'account' => $account,
  63. 'password' => $password,
  64. ]);
  65. if(!$status) return [false,$data];
  66. $user_id = $data['id'];
  67. $user = WxEmployee::where('openid',$openid)->first();
  68. if(empty($user)||empty($user->mobile)) return [false,'请先登陆'];
  69. $user->employee_id = $user_id;
  70. $user->save();
  71. return [true,$data];
  72. }
  73. public function loginRule($data){
  74. if($this->isEmpty($data,'account')) return [false,'账号不能为空!'];
  75. if($this->isEmpty($data,'password')) return [false,'密码不存在!'];
  76. $account = $data['account'];
  77. $res = Employee::where('del_time',0)
  78. ->where(function ($query)use($account) {
  79. $query->where('account', $account)
  80. ->orWhere('mobile', $account);
  81. })
  82. ->get()->toArray();
  83. if(empty($res)) return [false,'账号不存在或已被删除!'];
  84. if(count($res) > 1) return [false,'手机号绑定多个账户!'];
  85. $res = reset($res);
  86. if(! Hash::check($data['password'], $res['password'])) return [false,'密码错误!'];
  87. if($res['state'] == Employee::NOT_USE) return [false,'账号停用!'];
  88. return [true, ['id'=>$res['id'], 'name'=>$res['emp_name'], 'account' => $res['account']]];
  89. }
  90. public function checkWxUser($userId){
  91. $res = Employee::where('id', $userId)
  92. // ->where('del_time',0)
  93. ->where('state',Employee::USE)->get()->first();
  94. if(empty($res)) return [false, '该账号无法登录,请联系管理员!'];
  95. return [true, $res];
  96. }
  97. public function getTopMessage($data){
  98. if($this->isEmpty($data,'openid')) return [false,'openid不能为空!'];
  99. $employee = WxEmployee::where('openid',$data['openid'])->select('employee_id')->first();
  100. if(empty($employee) || empty($employee['employee_id'])) return [false,'未找到用户信息!'];
  101. $employee = $employee->toArray();
  102. $return = EmployeeService::getLoginMessage($employee['employee_id']);
  103. return [true,$return];
  104. }
  105. }