WxEmployeeService.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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 Illuminate\Support\Facades\DB;
  12. use Illuminate\Support\Facades\Hash;
  13. /**
  14. * 区域相关
  15. * @package App\Models
  16. */
  17. class WxEmployeeService extends Service
  18. {
  19. public function setUser($data){
  20. $code = $data['code'];
  21. $service = new WxService();
  22. list($status,$openid) = $service->getOpenid($code);
  23. if(!$status) return [false,$openid];
  24. $user = WxEmployee::where('openid',$openid)->first();
  25. if(empty($user)) {
  26. $user = new WxEmployee();
  27. $user->mobile = '';
  28. $user->openid = $openid;
  29. $user->appid = $service->appid;
  30. $user->save();
  31. $state = 0;
  32. }else{
  33. $state = 1;
  34. if(empty($user->mobile) || empty($user->employee_id)) $state = 0;
  35. }
  36. return [true,['openid'=>$openid,'state'=>$state ]];
  37. }
  38. public function setMobile($data){
  39. $code = $data['code'];
  40. $openid = $data['openid'];
  41. $service = new WxService();
  42. list($status,$mobile) = $service->getMobile($code);
  43. if(!$status) return [false,$mobile];
  44. $user = WxEmployee::where('openid',$openid)->first();
  45. if(empty($user)) return [false,'请先登陆'];
  46. $user->mobile = $mobile;
  47. $user->save();
  48. return [true, $mobile];
  49. }
  50. public function login($data){
  51. $account = $data['account'];
  52. $password = $data['password'];
  53. $openid = $data['openid'];
  54. list($status,$data) = $this->loginRule([
  55. 'account' => $account,
  56. 'password' => $password,
  57. ]);
  58. if(!$status) return [false,$data];
  59. $user_id = $data['id'];
  60. $user = WxEmployee::where('openid',$openid)->first();
  61. if(empty($user)||empty($user->mobile)) return [false,'请先登陆'];
  62. $user->employee_id = $user_id;
  63. $user->save();
  64. return [true,$data];
  65. }
  66. public function loginRule($data){
  67. if($this->isEmpty($data,'account')) return [false,'账号不能为空!'];
  68. if($this->isEmpty($data,'password')) return [false,'密码不存在!'];
  69. $account = $data['account'];
  70. $res = Employee::where('del_time',0)
  71. ->where(function ($query)use($account) {
  72. $query->where('account', $account)
  73. ->orWhere('mobile', $account);
  74. })
  75. ->get()->toArray();
  76. if(empty($res)) return [false,'账号不存在或已被删除!'];
  77. if(count($res) > 1) return [false,'手机号绑定多个账户!'];
  78. $res = reset($res);
  79. if(! Hash::check($data['password'], $res['password'])) return [false,'密码错误!'];
  80. if($res['state'] == Employee::NOT_USE) return [false,'账号停用!'];
  81. list($status,$construction) = $this->isConstruction($res['id']);
  82. if(! $status) return [false,'账号授权的门店不存在或者过期!'];
  83. return [true, ['id'=>$res['id'], 'name'=>$res['emp_name'], 'account' => $res['account'],'construction' => $construction ?? []]];
  84. }
  85. //是否施工技师
  86. public function isConstruction($emp_id){
  87. if(empty($emp_id)) return [false,''];
  88. //管理员
  89. if(Employee::SPECIAL_ADMIN == $emp_id){
  90. $foursShop = FoursShop::where('del_time',0)
  91. ->select('id')
  92. ->get()->toArray();
  93. return [true, array_column($foursShop,'id')];
  94. }
  95. $result = EmployeeFoursShop::where('employee_id', $emp_id)
  96. ->select('four_shop_id','use','expires_time')
  97. ->where('del_time',0)
  98. ->get()->toArray();
  99. $return = [];
  100. if(! empty($result)){
  101. date_default_timezone_set("PRC");
  102. $now = date("Y-m-d");
  103. foreach ($result as $value){
  104. if($value['use']){
  105. if(($value['expires_time'] == 0) || ($value['expires_time'] > 0 && ($now <= date("Y-m-d",$value['expires_time'])))){
  106. $return[] = $value['four_shop_id'];
  107. }
  108. }
  109. }
  110. if(empty($return)) return [false,''];
  111. return [true,$return];
  112. }
  113. return [false,''];
  114. }
  115. public function checkWxUser($userId){
  116. $res = Employee::where('id', $userId)
  117. ->where('del_time',0)
  118. ->where('state',Employee::USE)->get()->first();
  119. if(empty($res)) return [false, '该账号无法登录,请联系管理员!'];
  120. list($status,$msg) = $this->isConstruction($userId);
  121. if(! $status) return [false,'账号授权的门店不存在或者过期!'];
  122. return [true, $res];
  123. }
  124. public function createTemporaryJs($data,$user){
  125. list($status,$msg) = $this->createTemporaryJsRule($data,$user);
  126. if(! $status) return [false,$msg];
  127. try {
  128. DB::beginTransaction();
  129. $emp = Employee::where('del_time',0)
  130. ->where('mobile',$data['mobile'])
  131. ->first();
  132. if(! empty($emp)){
  133. Employee::where('id',$emp->id)
  134. ->update(['state' => Employee::STATUS_ONE]);
  135. $employee_id = $emp->id;
  136. }else{
  137. $model = new Employee();
  138. $model->emp_name = '外包技师:' . $data['mobile'];
  139. $model->state = Employee::STATUS_ONE;
  140. $model->is_wb = 1;
  141. $model->crt_id = $user['id'];
  142. $model->mobile = $data['mobile'];
  143. $model->password = Hash::make(Employee::DEFAULT_PASS);
  144. $model->entry_time = date("Y-m-d");
  145. $model->save();
  146. $employee_id = $model->id;
  147. $permisson_model = new EmployeeDepartPermission();
  148. $permisson_model->employee_id = $employee_id;
  149. $permisson_model->depart_id = 72;
  150. $permisson_model->save();
  151. }
  152. EmployeeFoursShop::where('employee_id',$employee_id)->update(['del_time' => time()]);
  153. $insert = [];
  154. foreach ($data['four_shop'] as $value){
  155. $insert[] = [
  156. "employee_id" => $employee_id,
  157. "four_shop_id" => $value['four_shop_id'],
  158. "use" => 1,
  159. 'expires_time' => $value['expires_time'],
  160. 'crt_time' => time(),
  161. 'upd_time' => time(),
  162. ];
  163. }
  164. EmployeeFoursShop::insert($insert);
  165. DB::commit();
  166. }catch (\Exception $e){
  167. DB::rollBack();
  168. return [false,$e->getMessage()];
  169. }
  170. return [true, ['account' => $data['mobile'], 'password' => Employee::DEFAULT_PASS]];
  171. }
  172. public function createTemporaryJsRule($data,$user){
  173. $key = TemporaryJs::LimitationSign . $user['id'];
  174. list($status,$msg) = $this->limitingSendRequestBackg($key,10);
  175. if(! $status) return [false,$msg];
  176. if(empty($data['mobile'])) return [false, '外包技师手机号不能为空'];
  177. $bool = Employee::where('del_time',0)
  178. ->where('mobile',$data['mobile'])
  179. ->where('is_wb',0)
  180. ->exists();
  181. if($bool) return [false, '手机号已绑定内部员工!'];
  182. if(empty($data['four_shop'])) return [false, '绑定4s店信息不能为空'];
  183. //验证重复项
  184. $four_shop_id = array_column($data['four_shop'],'four_shop_id');
  185. foreach ($four_shop_id as $value){
  186. if(! $value) return [false,'4s店不能为空!'];
  187. }
  188. $four_shop_id = array_map(function($val) {
  189. return $val !== null ? $val : 0;
  190. }, $four_shop_id);
  191. $arr = array_count_values($four_shop_id);
  192. foreach ($arr as $value){
  193. if($value > 1) return [false,'4s店信息不能重复'];
  194. }
  195. if($user['id'] != Employee::SPECIAL_ADMIN){
  196. $area_manager = $this->getAreaManagerBy4S();
  197. if(! in_array($user['id'], $area_manager)) return [false,'非区域经理,操作失败'];
  198. }
  199. return [true, ''];
  200. }
  201. public function wxCheck($data,$user){
  202. return [true,''];
  203. }
  204. public function getAreaManagerBy4S(){
  205. $result = FoursShop::where('del_time',0)
  206. ->select('regional_manager')
  207. ->distinct()
  208. ->get()->toArray();
  209. return array_column($result,'regional_manager');
  210. }
  211. public function getAreaManagerByStore(){
  212. $result = StorehouseEmployee::select('employee_id')
  213. ->distinct()
  214. ->where('del_time',0)
  215. ->get()->toArray();
  216. return array_column($result,'employee_id');
  217. }
  218. public function is_area_manager($user_id){
  219. $is_area = [
  220. 'by_4s' => 0,
  221. 'by_store' => 0
  222. ];
  223. $return = $this->getAreaManagerBy4S();
  224. if(in_array($user_id,$return)) $is_area['by_4s'] = 1;
  225. $return = $this->getAreaManagerByStore();
  226. if(in_array($user_id,$return)) $is_area['by_store'] = 1;
  227. return $is_area;
  228. }
  229. public function is_check($user_id){
  230. if($user_id == Employee::SPECIAL_ADMIN) return 1;
  231. $bool = EmployeeTeamPermission::where('employee_id',$user_id)
  232. ->exists();
  233. return $bool;
  234. }
  235. }