WxEmployeeService.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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)){
  37. $state = 0;
  38. }else{
  39. $bool = Employee::where('del_time',0)
  40. ->where('mobile',$user->mobile)
  41. ->where('state',Employee::USE)
  42. ->exists();
  43. if(! $bool) $state = 0;
  44. }
  45. }
  46. return [true,['openid'=>$openid, 'state'=> $state]];
  47. }
  48. public function setMobile($data){
  49. if(empty($data['code'])) return [false, '用户登录凭证(code)不能为空'];
  50. if(empty($data['openid'])) return [false, 'openID不能为空'];
  51. $code = $data['code'];
  52. $openid = $data['openid'];
  53. $service = new WxService();
  54. list($status,$mobile) = $service->getMobile($code);
  55. if(!$status) return [false,$mobile];
  56. $user = WxEmployee::where('openid',$openid)->first();
  57. if(empty($user)) return [false,'openID在系统中暂无记录'];
  58. $user->mobile = $mobile;
  59. $user->save();
  60. return [true, ['mobile' => $mobile]];
  61. }
  62. public function login($data){
  63. if(empty($data['account'])) return [false, '账号不能为空'];
  64. if(empty($data['password'])) return [false, '密码不能为空'];
  65. if(empty($data['openid'])) return [false, 'openID不能为空'];
  66. $account = $data['account'];
  67. $password = $data['password'];
  68. $openid = $data['openid'];
  69. list($status,$data) = $this->loginRule([
  70. 'account' => $account,
  71. 'password' => $password,
  72. ]);
  73. if(!$status) return [false,$data];
  74. $user_id = $data['id'];
  75. $user = WxEmployee::where('openid',$openid)->first();
  76. if(empty($user)||empty($user->mobile)) return [false,'请先登陆'];
  77. $user->employee_id = $user_id;
  78. $user->save();
  79. return [true,$data];
  80. }
  81. public function loginRule($data){
  82. if($this->isEmpty($data,'account')) return [false,'账号不能为空!'];
  83. if($this->isEmpty($data,'password')) return [false,'密码不存在!'];
  84. $account = $data['account'];
  85. $res = Employee::where('del_time',0)
  86. ->where(function ($query)use($account) {
  87. $query->where('account', $account)
  88. ->orWhere('mobile', $account);
  89. })
  90. ->get()->toArray();
  91. if(empty($res)) return [false,'账号不存在或已被删除!'];
  92. if(count($res) > 1) return [false,'手机号绑定多个账户!'];
  93. $res = reset($res);
  94. if(! Hash::check($data['password'], $res['password'])) return [false,'密码错误!'];
  95. if($res['state'] == Employee::NOT_USE) return [false,'账号停用!'];
  96. return [true, ['id'=>$res['id'], 'name'=>$res['emp_name'], 'account' => $res['account']]];
  97. }
  98. public function checkWxUser($userId){
  99. $res = Employee::where('id', $userId)
  100. // ->where('del_time',0)
  101. ->where('state',Employee::USE)->get()->first();
  102. if(empty($res)) return [false, '该账号无法登录,请联系管理员!'];
  103. return [true, $res];
  104. }
  105. public function getTopMessage($data){
  106. if($this->isEmpty($data,'openid')) return [false,'openid不能为空!'];
  107. $employee = WxEmployee::where('openid',$data['openid'])->first();
  108. if(empty($employee)) return [false,'未找到用户信息!'];
  109. $employee = $employee->toArray();
  110. if(empty($employee['employee_id'])) {
  111. //找到对应的账号
  112. $emp = Employee::where('del_time',0)
  113. ->where('mobile',$employee['mobile'])
  114. ->where('state',Employee::USE)
  115. ->select('id')
  116. ->first();
  117. if(empty($emp)) return [0,'用户手机信息未匹配到系统账号,请录入手机号!'];
  118. $emp = $emp->toArray();
  119. WxEmployee::where('id',$employee['id'])->update(['employee_id' => $emp['id']]);
  120. $employee['employee_id'] = $emp['id'];
  121. }
  122. $return = EmployeeService::getLoginMessage($employee['employee_id']);
  123. return [true, $return];
  124. }
  125. }