GroupUser.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. <?php
  2. namespace app\api\controller;
  3. use app\api\service\auth\GroupMysqlAdapter;
  4. use app\api\service\auth\ApiAuthGroupService;
  5. use app\api\validate\User as UserValidate;
  6. use think\exception\ValidateException;
  7. use app\common\model\GroupUser as GroupUserModel;
  8. use app\common\model\User as UserModel;
  9. use app\common\model\WorkerOut as WorkerOutModel;
  10. use \think\facade\Filesystem;
  11. use app\admin\controller\user\GroupUser as AdminGroupUser;
  12. class GroupUser extends Base
  13. {
  14. protected $noNeedLogin = ['login'];
  15. public function userinfo(GroupUserModel $userModel)
  16. {
  17. $user = $userModel->find($this->userinfo['id']);
  18. return $this->success('ok', $user);
  19. }
  20. //编辑用户
  21. public function edit(GroupUserModel $userModel)
  22. {
  23. $data = $this->request->post();
  24. try {
  25. validate(UserValidate::class)->scene('edit')->check($data);
  26. $user = $userModel->find($this->userinfo['id']);
  27. if (!$user) {
  28. return $this->error('用户不存在');
  29. }
  30. $user->save($data);
  31. return $this->success('ok');
  32. } catch (ValidateException $e) {
  33. return $this->error($e->getError());
  34. }
  35. }
  36. //修改密码
  37. public function password(GroupUserModel $userModel)
  38. {
  39. $data = $this->request->post();
  40. try {
  41. validate(UserValidate::class)->scene('password')->check($data);
  42. $user = $userModel->find($this->userinfo['id']);
  43. if (!$user) {
  44. return $this->error('用户不存在');
  45. }
  46. if ($user->password != md5(md5($data['oldpassword'] . $user->salt))) {
  47. return $this->error('旧密码错误');
  48. }
  49. $user->password = md5(md5($data['password'] . $user->salt));
  50. $user->save();
  51. return $this->success('ok');
  52. } catch (ValidateException $e) {
  53. return $this->error($e->getError());
  54. }
  55. }
  56. public function login(ApiAuthGroupService $authService, GroupUserModel $userModel)
  57. {
  58. $data = $this->request->post();
  59. try {
  60. validate(UserValidate::class)->scene('login')->check($data);
  61. $terminal = $this->request->post('terminal/d', 0);
  62. $user = $authService->login($data['username'], $data['password'], $terminal);
  63. } catch (ValidateException $e) {
  64. return $this->error($e->getError());
  65. } catch (\Exception $e) {
  66. return $this->error($e->getMessage());
  67. }
  68. return $this->success('ok', $user);
  69. }
  70. //退出登录
  71. public function logout()
  72. {
  73. GroupMysqlAdapter::logout($this->userinfo['id']);
  74. return $this->success('ok');
  75. }
  76. //获取全部人员
  77. public function getAllUser(GroupUserModel $userModel)
  78. {
  79. $list = $userModel->where('status', 'normal')->field('id,nickname,avatar')->select();
  80. return $this->success('ok', $list);
  81. }
  82. //组员列表
  83. public function group_list(GroupUserModel $userModel)
  84. {
  85. $list = $userModel->where('status', 'normal')->where('pid', $this->userinfo['id'])->select()->each(function ($item, $key) {
  86. $item['avatar'] = request()->domain() . '/' . $item['avatar'];
  87. return $item;
  88. });;
  89. return $this->success('ok', $list);
  90. }
  91. //添加组员
  92. public function add_group_user(GroupUserModel $groupUserModel, UserModel $userModel)
  93. {
  94. $file = request()->file('avatar');
  95. $param = $this->request->post();
  96. try {
  97. validate(UserValidate::class)->scene('add_group_user')->check($param);
  98. // $todayTime = strtotime('today');
  99. //判断手机号和身份证号是否注册今天是否添加到该团队
  100. $where_item_id_card = [];
  101. $where_item_id_card[] = ['pid', '=', $this->userinfo['id']];
  102. $where_item_id_card[] = ['id_card', '=', $param['id_card']];
  103. // $where_item_id_card[]=['createtime', '>=',$todayTime];
  104. $group_user_data = $groupUserModel->where($where_item_id_card)->find();
  105. if (!empty($group_user_data)) return $this->error('该身份证号已添加');
  106. $where_item_mobile = [];
  107. $where_item_mobile[] = ['pid', '=', $this->userinfo['id']];
  108. $where_item_mobile[] = ['mobile', '=', $param['mobile']];
  109. // $where_item_mobile[]=['createtime', '>=',$todayTime];
  110. $group_user_data = $groupUserModel->where($where_item_mobile)->find();
  111. if (!empty($group_user_data)) return $this->error('该手机号已添加');
  112. $user = $userModel->find($this->userinfo['id']);
  113. $pid = $user['id'];
  114. $time = time();
  115. $add_data = [
  116. 'pid' => $pid,
  117. 'nickname' => $param['nickname'],
  118. 'id_card' => $param['id_card'],
  119. 'avatar' => '',
  120. 'mobile' => $param['mobile'],
  121. 'sex' => $param['sex'],
  122. 'role' => '1',
  123. 'status' => 'normal',
  124. 'joinip' => request()->ip(),
  125. 'jointime' => $time
  126. ];
  127. if (!empty($file)) {
  128. // 文件夹
  129. $folder = strtotime('today');
  130. $fileName = time() . '_' . mt_rand(1000, 9999) . '.png';
  131. $avatar = $file->move('uploads/avatar/' . $folder, $fileName);
  132. //检查目标文件是否存在
  133. if (!empty($avatar)) {
  134. //有图片上传,进行添加用户
  135. $add_data['avatar'] = $avatar;
  136. $res = $groupUserModel->save($add_data);
  137. if ($res) return $this->success('添加成功');
  138. } else {
  139. return $this->error('头像上传失败');
  140. }
  141. } else {
  142. return $this->error('请上传头像');
  143. }
  144. } catch (ValidateException $e) {
  145. return $this->error($e->getError());
  146. }
  147. return $this->error('添加失败');
  148. }
  149. //修改组员
  150. public function edit_group_user(GroupUserModel $groupUserModel, UserModel $userModel)
  151. {
  152. $file = request()->file('avatar');
  153. $param = $this->request->post();
  154. try {
  155. validate(UserValidate::class)->scene('edit_group_user')->check($param);
  156. $user = $userModel->find($this->userinfo['id']);
  157. $edit_data = $groupUserModel->find($param['id']);
  158. if ($edit_data['pid'] != $this->userinfo['id']) return $this->error('无该用户');
  159. // $todayTime = strtotime('today');
  160. //判断手机号和身份证号是否注册今天是否添加到该团队
  161. $where_item_id_card = [];
  162. $where_item_id_card[] = ['id', '!=', $edit_data['id']];
  163. $where_item_id_card[] = ['pid', '=', $this->userinfo['id']];
  164. $where_item_id_card[] = ['id_card', '=', $param['id_card']];
  165. // $where_item_id_card[]=['createtime', '>=',$todayTime];
  166. $group_user_data = $groupUserModel->where($where_item_id_card)->find();
  167. if (!empty($group_user_data)) return $this->error('该身份证号已添加');
  168. $where_item_mobile = [];
  169. $where_item_mobile[] = ['id', '!=', $edit_data['id']];
  170. $where_item_mobile[] = ['pid', '=', $this->userinfo['id']];
  171. $where_item_mobile[] = ['mobile', '=', $param['mobile']];
  172. // $where_item_mobile[]=['createtime', '>=',$todayTime];
  173. $group_user_data = $groupUserModel->where($where_item_mobile)->find();
  174. if (!empty($group_user_data)) return $this->error('该手机号已添加');
  175. $pid = $user['id'];
  176. $time = time();
  177. $add_data = [
  178. 'pid' => $pid,
  179. 'nickname' => $param['nickname'],
  180. 'id_card' => $param['id_card'],
  181. 'mobile' => $param['mobile'],
  182. 'sex' => $param['sex'],
  183. 'role' => '1',
  184. 'status' => 'normal',
  185. 'joinip' => request()->ip(),
  186. 'updatetime' => $time
  187. ];
  188. if (!empty($file)) {
  189. // 文件夹
  190. $folder = strtotime('today');
  191. $fileName = time() . '_' . mt_rand(1000, 9999) . '.png';
  192. $avatar = $file->move('uploads/avatar/' . $folder, $fileName);
  193. //检查目标文件是否存在
  194. if (!empty($avatar)) {
  195. if (!empty($edit_data['avatar'])) {
  196. $avatar_file = root_path() . 'public' . DS . $edit_data['avatar'];
  197. if (file_exists($avatar_file)) {
  198. unlink($avatar_file);
  199. }
  200. }
  201. //有图片上传,进行添加用户
  202. $add_data['avatar'] = $avatar;
  203. $res = $edit_data->save($add_data);
  204. if ($res) return $this->success('修改成功');
  205. }
  206. } else {
  207. $res = $edit_data->save($add_data);
  208. if ($res) return $this->success('修改成功');
  209. }
  210. } catch (ValidateException $e) {
  211. return $this->error($e->getError());
  212. }
  213. return $this->error('修改失败');
  214. }
  215. //获取今日出工人员列表
  216. public function get_worker_out_list(GroupUserModel $groupUserModel, UserModel $userModel, WorkerOutModel $workerOutModel)
  217. {
  218. try {
  219. $workerOutModel=new WorkerOutModel();
  220. $todayTime = strtotime('today');
  221. $pid=$this->userinfo['id'];
  222. $where=[];
  223. $where[] = ['pid', '=',$pid];
  224. $list = $groupUserModel->where($where)
  225. ->select()->each(function ($item, $key) {
  226. $state=$this->get_worker_out_state($item['id'],$this->userinfo['id']);
  227. $state=$state==0?false:true;
  228. $item['selected']=$state;
  229. $item['avatar']= $this->startsWithHttp($item['avatar'])?$item['avatar']:request()->domain().'/' . $item['avatar'];
  230. return $item;
  231. });
  232. return $this->success('出工人员列表',$list);
  233. } catch (ValidateException $e) {
  234. return $this->error($e->getError());
  235. }
  236. }
  237. //添加今日出工人员
  238. public function set_worker_out(GroupUserModel $groupUserModel, UserModel $userModel, WorkerOutModel $workerOutModel)
  239. {
  240. $param = $this->request->post();
  241. try {
  242. validate(UserValidate::class)->scene('set_worker_out')->check($param);
  243. $list = $this->comma_str_array($param['list']);
  244. if(empty($list)) return $this->error('请添加出工人员');;
  245. $add_worker=[];
  246. $pid=$this->userinfo['id'];
  247. $time=time();
  248. foreach ($list as $key => $value) {
  249. $state=$this->get_worker_out_state((int)$value,$pid);
  250. if($state>0) continue;
  251. $item=[
  252. 'user_id'=>(int)$value,
  253. 'pid'=>$pid,
  254. 'createtime'=>$time
  255. ];
  256. $add_worker[]=$item;
  257. }
  258. $res=$workerOutModel->saveAll($add_worker);
  259. if($res) return $this->success('添加出工人员成功');
  260. } catch (ValidateException $e) {
  261. return $this->error($e->getError());
  262. }
  263. return $this->error('添加出工人员失败');
  264. }
  265. //清除今日出工人员
  266. public function clear_worker_out(GroupUserModel $groupUserModel, UserModel $userModel, WorkerOutModel $workerOutModel)
  267. {
  268. $param = $this->request->post();
  269. try {
  270. validate(UserValidate::class)->scene('set_worker_out')->check($param);
  271. $list = $this->comma_str_array($param['list']);
  272. if(empty($list)) return $this->error('请添加取消人员');
  273. $add_worker=[];
  274. $pid=$this->userinfo['id'];
  275. $time=time();
  276. foreach ($list as $key => $value) {
  277. $id=$this->get_worker_out_id((int)$value,$pid);
  278. if(empty($id)) continue;
  279. $item=[
  280. 'id'=>$id,
  281. 'state'=>0,
  282. 'updatetime'=>$time
  283. ];
  284. $add_worker[]=$item;
  285. }
  286. $res=$workerOutModel->saveAll($add_worker);
  287. if($res) return $this->success('取消成功');
  288. } catch (ValidateException $e) {
  289. return $this->error($e->getError());
  290. }
  291. return $this->error('取消失败');
  292. }
  293. //获取今天出工人员状态,0=未出个,1=出工
  294. public function get_worker_out_state($id,$pid)
  295. {
  296. $groupUserModel=new GroupUserModel();
  297. $count=$groupUserModel->where('id',$id)->where('pid',$pid)->count();
  298. if($count==0) return 1;
  299. $workerOutModel=new WorkerOutModel();
  300. $todayTime = strtotime('today');
  301. $where=[];
  302. $where[] = ['user_id', '=',$id];
  303. $where[] = ['pid', '=',$pid];
  304. $where[] = ['state', '=',1];
  305. $where[]=['createtime', '>=',$todayTime];
  306. return $workerOutModel->where($where)->count();
  307. }
  308. //获取今天出工人员id
  309. public function get_worker_out_id($id,$pid)
  310. {
  311. $workerOutModel=new WorkerOutModel();
  312. $todayTime = strtotime('today');
  313. $where=[];
  314. $where[] = ['user_id', '=',$id];
  315. $where[] = ['pid', '=',$pid];
  316. $where[] = ['state', '=',1];
  317. $where[]=['createtime', '>=',$todayTime];
  318. $count=$workerOutModel->where($where)->count();
  319. if($count==0) return null;
  320. $id=$workerOutModel->where($where)->value('id');
  321. return $id;
  322. }
  323. //逗号分隔转数组
  324. public function comma_str_array($arr_string)
  325. {
  326. // 1. 清除所有空格(含全角)
  327. $array = str_replace([' ', ' '], '', $arr_string);
  328. // 2. 清除开头/结尾的逗号(关键改进)
  329. $array = trim($array, ',');
  330. $array=explode(',', $array);
  331. if(empty($array)) return null;
  332. $arr=[];
  333. foreach ($array as &$item) {
  334. if(empty($item)){
  335. continue;
  336. }
  337. $arr[]=$item;
  338. }
  339. return $arr;
  340. }
  341. // 方法1.1:使用strpos()精准匹配
  342. public function startsWithHttp($url)
  343. {
  344. return strpos($url, 'https://') === 0
  345. || strpos($url, 'http://') === 0;
  346. }
  347. }