WxEmployeeService.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. namespace App\Service\Weixin;
  3. use App\Model\CustomerSupply;
  4. use App\Model\Employee;
  5. use App\Model\WxEmployeeOfficial;
  6. use App\Service\Service;
  7. class WxEmployeeService extends Service
  8. {
  9. public function setUser($data){
  10. if(empty($data['openid'])) return [false, 'openid不能为空'];
  11. if(empty($data['login_type']) || ! isset(WxEmployeeOfficial::$login_type_title[$data['login_type']])) return [false, '绑定类型不存在或错误'];
  12. $openid = $data['openid'];
  13. $appid = config("wx_msg.f_appid");
  14. $user = WxEmployeeOfficial::where('openid',$openid)
  15. ->where('type', $data['login_type'])
  16. ->where('appid', $appid)
  17. ->first();
  18. $state = 0;
  19. if(empty($user)) {
  20. $user = new WxEmployeeOfficial();
  21. $user->openid = $openid;
  22. $user->type = $data['login_type'];
  23. $user->appid = $appid;
  24. $user->save();
  25. }else{
  26. if(! empty($user->employee_id)) $state = 1;
  27. }
  28. return [true, ['openid'=>$openid, 'state'=>$state]];
  29. }
  30. public function login($data, $openid){
  31. list($status, $a_data) = $this->loginRule($data, $openid);
  32. if(! $status) return [false, $a_data];
  33. list($user, $customer) = $a_data;
  34. try{
  35. $user->employee_id = $customer['id'];
  36. $user->save();
  37. //更新绑定关系
  38. if($data['login_type'] == WxEmployeeOfficial::login_type_one){
  39. $this->updateWxEmployeeOfficial($customer['mobile'], $openid);
  40. }
  41. }catch (\Exception $exception){
  42. return [false, $exception->getMessage()];
  43. }
  44. return [true, ''];
  45. }
  46. public function loginRule($data, $openid){
  47. if(empty($data['account'])) return [false, '账号不能为空'];
  48. if(empty($data['password'])) return [false, '密码不能为空'];
  49. if(empty($openid) || $openid == null) return [false, 'ciphertext不能为空'];
  50. if(empty($data['login_type']) || ! isset(WxEmployeeOfficial::$login_type_title[$data['login_type']])) return [false, '绑定类型不存在或错误'];
  51. $appid = config("wx_msg.f_appid");
  52. $user = WxEmployeeOfficial::where('openid',$openid)
  53. ->where('type', $data['login_type'])
  54. ->where('appid',$appid)
  55. ->first();
  56. if(! empty($user)) {
  57. if(! empty($user->employee_id)) return [false, '微信用户已绑定系统账号'];
  58. }else{
  59. $user = new WxEmployeeOfficial();
  60. $user->openid = $openid;
  61. $user->type = $data['login_type'];
  62. $user->appid = $appid;
  63. $user->save();
  64. }
  65. if($data['login_type'] == WxEmployeeOfficial::login_type_one){
  66. //供应商
  67. $customer = CustomerSupply::where('del_time',0)
  68. ->where('account', $data['account'])
  69. ->where('password', $data['password'])
  70. ->first();
  71. if(empty($customer)) return [false, '账号或者密码错误,绑定失败'];
  72. $customer = $customer->toArray();
  73. }else{
  74. //企业内部员工
  75. $customer = Employee::where('del_time',0)
  76. ->where('account', $data['account'])
  77. ->first();
  78. if(empty($customer)) return [false,'账号不存在,绑定失败'];
  79. $customer = $customer->toArray();
  80. $lastFour = substr($customer['mobile'], -4);
  81. if($lastFour != $data['password']) return [false,'密码错误,绑定失败'];
  82. }
  83. $bool = WxEmployeeOfficial::where('employee_id',$customer['id'])
  84. ->where('type', $data['login_type'])
  85. ->where('appid',$appid)
  86. ->exists();
  87. if($bool) return [false, '系统账号已被其他微信用户绑定,绑定失败'];
  88. return [true, [$user, $customer]];
  89. }
  90. public function updateWxEmployeeOfficial($mobile, $openid){
  91. $other = CustomerSupply::where('mobile', $mobile)
  92. ->where('type',CustomerSupply::type_two)
  93. ->get()->toArray();
  94. foreach ($other as $value){
  95. WxEmployeeOfficial::firstOrCreate([
  96. 'appid' => config("wx_msg.f_appid"),
  97. 'type' => WxEmployeeOfficial::login_type_one,
  98. 'openid' => $openid,
  99. 'employee_id' => $value['id'],
  100. ]);
  101. }
  102. }
  103. public function updateWxEmployeeOfficial2($mobile){
  104. $other = CustomerSupply::where('mobile', $mobile)
  105. ->where('type',CustomerSupply::type_two)
  106. ->get()->toArray();
  107. $other_id = array_column($other,'id');
  108. $wx = WxEmployeeOfficial::whereIn('employee_id',$other_id)
  109. ->where('type', WxEmployeeOfficial::login_type_one)
  110. ->first();
  111. if(! empty($wx)){
  112. $openid = $wx->openid;
  113. foreach ($other as $value){
  114. WxEmployeeOfficial::firstOrCreate([
  115. 'appid' => config("wx_msg.f_appid"),
  116. 'type' => WxEmployeeOfficial::login_type_one,
  117. 'openid' => $openid,
  118. 'employee_id' => $value['id'],
  119. ]);
  120. }
  121. }
  122. }
  123. }