User.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979
  1. <?php
  2. namespace app\common\model;
  3. use app\hander\HelpHander;
  4. use think\Db;
  5. use think\Exception;
  6. use think\Model;
  7. class User extends Model
  8. {
  9. public $sex = [
  10. "未知",
  11. "男",
  12. "女"
  13. ];
  14. protected function setPasswordAttr($value){
  15. if(trim($value)){
  16. return password_hash($value, PASSWORD_DEFAULT);
  17. }
  18. return '';
  19. }
  20. public function addUser(){
  21. $data = [
  22. 'id' => input('id/d',0),
  23. 'account' => input('account','','trim'),
  24. 'real_name' => input('real_name','','trim'),
  25. 'mobile' => input('mobile','','trim'),
  26. 'password' => input('password','','trim'),
  27. 'depId' => input('depId/d',0),
  28. 'rolesId' => input('rolesId/d',0),
  29. 'enable' => input('enable/d',1),
  30. 'addr_id' => input('addr_id/d',0),
  31. 'sort'=>input('sort',0,'trim'),
  32. 'type_id' => input('type_id/d', 0),
  33. 'sex' => input('sex/d', 0),
  34. ];
  35. $orgId = cur_org_id();
  36. $result = validate('User')->check($data,[],'add');
  37. if(true !== $result){
  38. $this->error = validate('User')->getError();
  39. return false;
  40. }
  41. Db::startTrans();
  42. try{
  43. $id = $data['id'];
  44. unset($data['id']);
  45. $depId = $data['depId'];
  46. $rolesId = $data['rolesId'];
  47. unset($data['depId']);
  48. unset($data['rolesId']);
  49. $role = Db::name('roles')
  50. ->where('id',$rolesId)
  51. ->find();
  52. if($rolesId!=7 && $role['parent_id']!=7){
  53. unset($data['type_id']);
  54. }
  55. if($id > 0){
  56. if(!$data['password']){
  57. unset($data['password']);
  58. }
  59. $data['update_time'] = date('Y-m-d H:i:s');
  60. $ret = $this->allowField(true)->save($data,['id'=>$id]);
  61. if(!$ret){
  62. \exception('操作失败');
  63. }
  64. $ud = Db::name('user_dep')->where('user_id',$id)->find();
  65. if(!$ud){
  66. Db::name('user_dep')->insert(['user_id'=>$id,'dep_id'=>$depId]);
  67. }else{
  68. Db::name('user_dep')->where('user_id',$id)->setField('dep_id',$depId);
  69. }
  70. $ur = Db::name('user_roles')->where('user_id',$id)->find();
  71. if(!$ur){
  72. Db::name('user_roles')->insert(['user_id'=>$id,'roles_id'=>$rolesId]);
  73. }else{
  74. Db::name('user_roles')->where('user_id',$id)->setField('roles_id',$rolesId);
  75. }
  76. $rolesParentId = Db::name('roles')
  77. ->where('id',$rolesId)
  78. ->value('parent_id');
  79. if($rolesParentId==11){
  80. $check = Db::name('worker')
  81. ->where('user_id',$id)->find();
  82. if(empty($check)){
  83. $a = [
  84. 'user_id'=>$id
  85. ];
  86. $res = Db::name('worker')
  87. ->insertGetId($a);
  88. if(!$res){
  89. \exception('护工保存失败');
  90. }
  91. }
  92. }
  93. }else{
  94. $data['password'] = password_hash($data['password'], PASSWORD_DEFAULT);
  95. $data['create_time'] = date('Y-m-d H:i:s');
  96. $data['ip'] = request()->ip();
  97. $ret = $this->insertGetId($data);
  98. $ids = $ret;
  99. if(!$ret){
  100. \exception('操作失败');
  101. }
  102. Db::name('user_org')->insert(['user_id'=>$ids,'org_id'=>$orgId]);
  103. Db::name('user_dep')->insert(['user_id'=>$ids,'dep_id'=>$depId]);
  104. Db::name('user_roles')->insert(['user_id'=>$ids,'roles_id'=>$rolesId]);
  105. $rolesParentId = Db::name('roles')
  106. ->where('id',$rolesId)
  107. ->value('parent_id');
  108. if($rolesParentId==11){
  109. Db::name('worker')
  110. ->where('user_id',$ids)
  111. ->delete();
  112. $a = [
  113. 'user_id'=>$ids
  114. ];
  115. $res = Db::name('worker')
  116. ->insertGetId($a);
  117. if(!$res){
  118. \exception('护工保存失败');
  119. }
  120. }
  121. }
  122. Db::commit();
  123. return true;
  124. }catch (Exception $e){
  125. Db::rollback();
  126. $this->error = $e->getMessage();
  127. return false;
  128. }
  129. }
  130. public function wxaddUser(){
  131. $data = [
  132. 'id' => input('id/d',0),
  133. 'real_name' => input('real_name','','trim'),
  134. 'enable' => input('enable/d',1),
  135. ];
  136. $result = validate('User')->check($data,[],'wxedit');
  137. if(true !== $result){
  138. $this->error = validate('User')->getError();
  139. return false;
  140. }
  141. Db::startTrans();
  142. try{
  143. $id = $data['id'];
  144. unset($data['id']);
  145. if($id > 0){
  146. $data['update_time'] = date('Y-m-d H:i:s');
  147. $ret = $this->allowField(true)->save($data,['id'=>$id]);
  148. if(!$ret){
  149. \exception('操作失败');
  150. }
  151. }else{
  152. }
  153. Db::commit();
  154. return true;
  155. }catch (Exception $e){
  156. Db::rollback();
  157. $this->error = $e->getMessage();
  158. return false;
  159. }
  160. }
  161. public function addCompany(){
  162. $data = [
  163. 'id' => input('id/d',0),
  164. 'account' => input('account','','trim'),
  165. 'real_name' => input('real_name','','trim'),
  166. 'mobile' => input('mobile','','trim'),
  167. 'password' => input('password','','trim'),
  168. 'orgs' => input('orgs','','trim'),
  169. 'rolesId' => input('rolesId/d',0),
  170. 'enable' => input('enable/d',1),
  171. 'type' => 1
  172. ];
  173. $result = validate('User')->check($data,[],'addCompany');
  174. if(true !== $result){
  175. $this->error = validate('User')->getError();
  176. return false;
  177. }
  178. Db::startTrans();
  179. try{
  180. $id = $data['id'];
  181. unset($data['id']);
  182. $rolesId = $data['rolesId'];
  183. unset($data['rolesId']);
  184. if($rolesId == 1){ //超级管理员
  185. $data['orgs'] = '';
  186. }
  187. if($id > 0){
  188. if(!$data['password']){
  189. unset($data['password']);
  190. }
  191. $data['update_time'] = date('Y-m-d H:i:s');
  192. $ret = $this->allowField(true)->save($data,['id'=>$id]);
  193. if(!$ret){
  194. \exception('操作失败');
  195. }
  196. Db::name('user_roles')->where('user_id',$id)->setField('roles_id',$rolesId);
  197. }else{
  198. $data['create_time'] = date('Y-m-d H:i:s');
  199. $data['ip'] = request()->ip();
  200. $ret = $this->allowField(true)->save($data);
  201. if(!$ret){
  202. \exception('操作失败');
  203. }
  204. Db::name('user_roles')->insert(['user_id'=>$this->id,'roles_id'=>$rolesId]);
  205. }
  206. Db::commit();
  207. return true;
  208. }catch (Exception $e){
  209. Db::rollback();
  210. $this->error = $e->getMessage();
  211. return false;
  212. }
  213. }
  214. // 修改密码
  215. public function modifyPass($data){
  216. $result = validate('User')->check($data,[],'modifyPass');
  217. if(true !== $result){
  218. $this->error = validate('User')->getError();
  219. return false;
  220. }
  221. $info = Db::name('user')->where('id',$data['id'])->where('del',0)->find();
  222. if(!$info){
  223. $this->error = '用户不存在';
  224. return false;
  225. }
  226. if (!password_verify($data['oldpass'], $info['password'])) {
  227. $this->error = "旧密码错误";
  228. return false;
  229. }
  230. $data['update_time'] = date('Y-m-d H:i:s');
  231. $sdata = [
  232. 'update_time' => date('Y-m-d H:i:s'),
  233. 'password' => $data['pass'],
  234. 'login' => 1
  235. ];
  236. $ret = $this->allowField(true)->save($sdata,['id'=>$data['id']]);
  237. if(!$ret){
  238. $this->error = "密码修改失败";
  239. return false;
  240. }
  241. return true;
  242. }
  243. //app登录
  244. public function login($accountName,$password){
  245. // 登录限流
  246. $ret = (new \app\common\util\ThrottlesUtil(config('app.login_throttles')))->tooManyAttempts($accountName);
  247. if($ret){
  248. $this->error ='账号已被锁定,请稍后重试';
  249. return false;
  250. }
  251. // 登录是根据账号和手机号组成,先查账号再查手机号
  252. $user = Db::name('user')
  253. ->where('account',$accountName)
  254. ->where('del',0)
  255. ->find();
  256. if(empty($user)){
  257. $user = Db::name('user')
  258. ->where('mobile',$accountName)
  259. ->where('del',0)
  260. ->find();
  261. }
  262. if(empty($user)){
  263. $this->error = '账号或密码错误';
  264. return false;
  265. }
  266. $pass = aes_decrypt($password);
  267. if(!password_verify($pass, $user['password'])){
  268. $this->error = '账号或密码错误';
  269. return false;
  270. }
  271. $token = $this->authLogin($user['id']);
  272. if(!$token){
  273. $this->error = '登录失败';
  274. return false;
  275. }
  276. // 登录成功,重置限流
  277. (new \app\common\util\ThrottlesUtil(config('app.login_throttles')))->resetAttempts($accountName);
  278. return ['user_id' => $user['id'],'token' => $token];
  279. }
  280. private function authLogin($userId,$openid=''){
  281. Db::startTrans();
  282. try{
  283. $res = Db::name('user')->where('id',$userId)->update([
  284. 'last_login_time' => date('Y-m-d H:i:s')
  285. ]);
  286. if(!$res){
  287. \exception('登录失败');
  288. }
  289. $token = model('Token')->updateToken($userId);
  290. if(!$token){
  291. \exception(model('Token')->getError());
  292. }
  293. if($openid){
  294. $ret = Db::name('user_oauth')->insert([
  295. 'user_id' => $userId,
  296. 'openid' => $openid,
  297. 'type' => 1,
  298. 'create_time' => date('Y-m-d H:i:s')
  299. ]);
  300. if (!$ret) {
  301. \exception('事务失败');
  302. }
  303. }
  304. Db::commit();
  305. return $token;
  306. }catch (Exception $e){
  307. Db::rollback();
  308. $this->error = $e->getMessage();
  309. return false;
  310. }
  311. }
  312. // 小程序手机号登录
  313. public function wxpLogin($phone,$orgId){
  314. // 1.检查用户是否存在,存在登录,不存在告诉用户进行下一步
  315. $user = Db::name('user')->where('mobile',$phone)->where('del',0)->find();
  316. if(!$user){ // 不存在注册
  317. HelpHander::error('用户不存在',2,['phone'=>$phone]); // 用户进行下一步
  318. Db::startTrans();
  319. try{
  320. $data = [
  321. 'account' => md5($phone.$orgId.get_unique_id().time()),
  322. 'real_name' => $phone,
  323. 'mobile' => $phone,
  324. 'password' => $phone,
  325. 'enable' => 1,
  326. 'type' => 2,
  327. 'last_login_time' => date('Y-m-d H:i:s'),
  328. ];
  329. $data['create_time'] = date('Y-m-d H:i:s');
  330. $data['ip'] = request()->ip();
  331. $ret = $this->allowField(true)->save($data);
  332. if(!$ret){
  333. \exception('操作失败');
  334. }
  335. $userId = $this->id;
  336. $token = model('Token')->updateToken($userId);
  337. if(!$token){
  338. \exception(model('Token')->getError());
  339. }
  340. Db::name('user_org')->insert([
  341. 'user_id' => $userId,
  342. 'org_id' => $orgId
  343. ]);
  344. Db::commit();
  345. }catch (Exception $e){
  346. Db::rollback();
  347. $this->error = $e->getMessage();
  348. return false;
  349. }
  350. return ['user_id' => $userId,'token' => $token,'org_id' => $orgId];
  351. }else{
  352. if($user['enable'] != 1){
  353. HelpHander::error('用户被禁用,请联系管理员');
  354. }
  355. // 获取用户组织
  356. $uo = Db::name('user_org')->where('user_id',$user['id'])->find();
  357. if(!$uo){
  358. Db::name('user_org')->insert([
  359. 'user_id' => $user['id'],
  360. 'org_id' => $orgId
  361. ]);
  362. $curOrgId = $orgId;
  363. }else{
  364. $curOrgId = $uo['org_id'];
  365. }
  366. $org = Db::name('org')->where('id',$curOrgId)->where('del',0)->where('enable',1)->where('type',2)->find();
  367. if(!$org){
  368. $this->error = "项目不存在或已禁用";
  369. return false;
  370. }
  371. Db::startTrans();
  372. try{
  373. $data = [
  374. 'last_login_time' => date('Y-m-d H:i:s'),
  375. ];
  376. $ret = Db::name('user')->where('id',$user['id'])->update($data);
  377. if(!$ret){
  378. \exception('操作失败');
  379. }
  380. $token = model('Token')->updateToken($user['id']);
  381. if(!$token){
  382. \exception(model('Token')->getError());
  383. }
  384. Db::commit();
  385. }catch (Exception $e){
  386. Db::rollback();
  387. $this->error = $e->getMessage();
  388. return false;
  389. }
  390. return ['user_id' => $user['id'],'token' => $token,'org_id' => $curOrgId];
  391. }
  392. }
  393. // 小程序登录
  394. public function wxLogin($openid,$nickname,$avatar,$orgId){
  395. $oauth = Db::name('user_oauth')
  396. ->where('openid',$openid)
  397. ->where('type',1)
  398. ->find();
  399. if($oauth){
  400. return $this->wxPreLogin($openid,$orgId);
  401. }
  402. $org = Db::name('org')->where('id',$orgId)->where('del',0)->where('enable',1)->where('type',2)->find();
  403. if(!$org){
  404. $this->error = "项目不存在";
  405. return false;
  406. }
  407. Db::startTrans();
  408. try{
  409. $data = [
  410. 'account' => $orgId.'_'.$openid,
  411. 'real_name' => $nickname,
  412. 'head_image' => $avatar,
  413. 'mobile' => '',
  414. 'password' => $openid,
  415. 'enable' => 1,
  416. 'type' => 2,
  417. 'last_login_time' => date('Y-m-d H:i:s'),
  418. ];
  419. $data['create_time'] = date('Y-m-d H:i:s');
  420. $data['ip'] = request()->ip();
  421. $ret = $this->allowField(true)->save($data);
  422. if(!$ret){
  423. \exception('操作失败');
  424. }
  425. $userId = $this->id;
  426. $token = model('Token')->updateToken($userId);
  427. if(!$token){
  428. \exception(model('Token')->getError());
  429. }
  430. if($openid){
  431. $ret = Db::name('user_oauth')->insert([
  432. 'user_id' => $userId,
  433. 'openid' => $openid,
  434. 'type' => 1,
  435. 'create_time' => date('Y-m-d H:i:s')
  436. ]);
  437. if (!$ret) {
  438. \exception('事务失败');
  439. }
  440. }
  441. Db::name('user_org')->insert([
  442. 'user_id' => $userId,
  443. 'org_id' => $orgId
  444. ]);
  445. Db::commit();
  446. }catch (Exception $e){
  447. Db::rollback();
  448. $this->error = $e->getMessage();
  449. return false;
  450. }
  451. return ['user_id' => $userId,'token' => $token];
  452. }
  453. // 微信小程序预登陆
  454. public function wxPreLogin($openid,$orgId){
  455. $org = Db::name('org')->where('id',$orgId)->where('del',0)->where('enable',1)->where('type',2)->find();
  456. if(!$org){
  457. $this->error = "项目不存在";
  458. return false;
  459. }
  460. $oauth = Db::name('user_oauth')
  461. ->where('openid',$openid)
  462. ->where('type',1)
  463. ->find();
  464. if(empty($oauth)){ // 账号不存在注册一个
  465. $data = [
  466. 'account' => $orgId.'_'.$openid,
  467. 'real_name' => "",
  468. 'mobile' => '',
  469. 'password' => $openid,
  470. 'head_image' => "",
  471. 'enable' => 1,
  472. 'type' => 2,
  473. ];
  474. $data['create_time'] = date('Y-m-d H:i:s');
  475. $data['ip'] = request()->ip();
  476. $ret = $this->allowField(true)->save($data);
  477. if(!$ret){
  478. \exception('操作失败');
  479. }
  480. $userId = $this->id;
  481. $uoData = [
  482. 'openid'=>$openid,
  483. 'user_id'=>$userId,
  484. 'create_time'=>date('Y-m-d H:i:s'),
  485. 'type'=>1
  486. ];
  487. $uo = Db::name('user_oauth')->insert($uoData);
  488. if(!$uo){
  489. $this->error = '登录失败';
  490. return false;
  491. }
  492. }else{
  493. $userId = $oauth['user_id'];
  494. }
  495. $user = Db::name('user')
  496. ->where('id',$userId)
  497. ->where('del',0)
  498. ->find();
  499. if(empty($user)){
  500. $this->error = '账号不存在';
  501. return false;
  502. }
  503. $uo = Db::name('user_org')->where('user_id',$user['id'])->where('org_id',$orgId)->find();
  504. if(!$uo){
  505. Db::name('user_org')->insert([
  506. 'user_id' => $user['id'],
  507. 'org_id' => $orgId
  508. ]);
  509. }
  510. $token = $this->authLogin($user['id'],'',$orgId);
  511. if(!$token){
  512. $this->error = '登录失败';
  513. return false;
  514. }
  515. return ['user_id' => $user['id'],'token' => $token,'org_name' => $org['name']];
  516. }
  517. /**
  518. * 专项保洁获取人员
  519. *
  520. * @author wst
  521. * @date 2021/9/6 10:45
  522. */
  523. public function getCleanWorker(){
  524. $list = (new WorkTypeMode())->getRolesUser(14,cur_org_id());
  525. $arr = [];
  526. foreach ($list as $k=>$v){
  527. if(!empty($v['user'])){
  528. foreach ($v['user'] as $k1=>$v1){
  529. $arr[] = [
  530. 'id'=>$v1['id'],
  531. 'title'=>$v1['real_name'],
  532. ];
  533. }
  534. }
  535. }
  536. return $arr;
  537. }
  538. /**
  539. * 设备台账获取人员
  540. *
  541. * @author wst
  542. * @date 2021/9/8 14:59
  543. * @return array
  544. */
  545. public function getDeviceWorker(){
  546. $list = (new WorkTypeMode())->getRolesUser(10,cur_org_id());
  547. $arr = [];
  548. foreach ($list as $k=>$v){
  549. if(!empty($v['user'])){
  550. foreach ($v['user'] as $k1=>$v1){
  551. $arr[] = [
  552. 'id'=>$v1['id'],
  553. 'title'=>$v1['real_name'],
  554. ];
  555. }
  556. }
  557. }
  558. return $arr;
  559. }
  560. /**
  561. * 巡更模块获取人员
  562. *
  563. * @author wst
  564. * @date 2021/9/8 15:00
  565. * @return array
  566. */
  567. public function getPatrolWorker($id){
  568. $list = (new WorkTypeMode())->getRolesUser($id,cur_org_id());
  569. $arr = [];
  570. foreach ($list as $k=>$v){
  571. if(!empty($v['user'])){
  572. foreach ($v['user'] as $k1=>$v1){
  573. $arr[] = [
  574. 'id'=>$v1['id'],
  575. 'title'=>$v1['real_name'],
  576. ];
  577. }
  578. }
  579. }
  580. return $arr;
  581. }
  582. /**
  583. * 医废获取人员
  584. *
  585. * @author wst
  586. * @date 2021/9/8 15:01
  587. * @param string $test
  588. * @return bool
  589. */
  590. public function getWasteWorker(){
  591. $list = (new WorkTypeMode())->getRolesUser(11,cur_org_id());
  592. $arr = [];
  593. foreach ($list as $k=>$v){
  594. if(!empty($v['user'])){
  595. foreach ($v['user'] as $k1=>$v1){
  596. $arr[] = [
  597. 'id'=>$v1['id'],
  598. 'title'=>$v1['real_name'],
  599. ];
  600. }
  601. }
  602. }
  603. return $arr;
  604. }
  605. /**
  606. * 获取医废未转运提示人员
  607. *
  608. * @author wst
  609. * @date 2021/9/8 15:01
  610. * @param string $test
  611. * @return bool
  612. */
  613. public function getWasteWorkerList(){
  614. $user = Db::name('user')
  615. ->alias('u')
  616. ->field('u.id,u.real_name as title')
  617. ->join('user_org uo','uo.user_id=u.id')
  618. ->where([
  619. 'uo.org_id'=>cur_org_id(),
  620. 'u.del'=>0,
  621. 'u.enable'=>1,
  622. ])
  623. ->select();
  624. return $user;
  625. }
  626. /**
  627. * 获取用户消息
  628. * @param $userId
  629. */
  630. public function info($userId,$orgId=0){
  631. $user = Db::name('user')
  632. ->where('id',$userId)
  633. ->where('del',0)
  634. ->where('enable',1)
  635. ->field('id,real_name,mobile,type,head_image,work,station,addr_id,device_sn,login')
  636. ->find();
  637. if(!$user){
  638. $this->error = '用户不存在或被禁用';
  639. return false;
  640. }
  641. $dep = Db::name('user_dep')
  642. ->alias('ud')
  643. ->join('dep d','d.id = ud.dep_id')
  644. ->field('d.id,d.title')
  645. ->where('ud.user_id',$userId)
  646. ->find();
  647. $user['dep_id'] = $dep?$dep['id']:0;
  648. $user['dep_name'] = $dep?$dep['title']:'';
  649. $addrTitle = Db::name('address')
  650. ->where('id',$user['addr_id'])
  651. ->value('title');
  652. $user['addr_title'] = $user['addr_id']>0?$addrTitle:'';
  653. $roles = Db::name('user_roles')
  654. ->alias('ur')
  655. ->join('roles r','r.id = ur.roles_id')
  656. ->field('r.id,r.name,r.parent_id')
  657. ->where('ur.user_id',$userId)
  658. ->find();
  659. $user['roles_id'] = $roles?$roles['id']:0;
  660. $user['roles_name'] = $roles?$roles['name']:'';
  661. $parentId = $roles?$roles['parent_id']:0;
  662. $user['userType'] = $parentId==3?1:2;//2其它1客户
  663. $user['workTypeMode'] = '';
  664. $user['isTwoDispatch'] = 2;
  665. if($user['roles_id'] >0){
  666. $rolesInfo = Db::name('roles')
  667. ->where('id',$user['roles_id'])
  668. ->find();
  669. $user['workTypeMode'] = $rolesInfo['work_type_mode'];
  670. $user['isTwoDispatch'] = $rolesInfo['level'];
  671. }
  672. return $user;
  673. }
  674. /*
  675. * 获取某组织下全部人员
  676. * @param $orgId
  677. */
  678. public function gerUserAll($orgId = 0){
  679. $user = Db::name('user')
  680. ->alias('u')
  681. ->field('u.id,u.real_name as title')
  682. ->join('user_org uo','u.id=uo.user_id')
  683. ->where('uo.org_id',$orgId)
  684. ->where('enable',1)
  685. ->where('del',0)
  686. ->select();
  687. return $user;
  688. }
  689. //获取二级调度角色下用户、
  690. public function get_two_dispatch_user($rolesId){
  691. $user = Db::name('user_roles')
  692. ->alias('a')
  693. ->join('user u','a.user_id=u.id')
  694. ->where('a.roles_id',$rolesId)
  695. ->where('u.del',0)
  696. ->where('u.enable',1)
  697. ->where('u.type',0)
  698. ->select();
  699. return $user;
  700. }
  701. //后台获取所有二级调度角色
  702. public function get_two_dispatch_role($orgId,$type){
  703. $map[] = ['enable','=',1];
  704. $map[] = ['level','=',2];
  705. $map[] = ['del','=',0];
  706. $map[] = ['parent_id','=',9];
  707. $map[] = ['org_id','=',$orgId];
  708. $map[] = ['','exp',Db::raw("FIND_IN_SET(".$type.",work_type_mode)")];
  709. $role = Db::name('roles')
  710. ->field('id,name as title')
  711. ->where($map)
  712. ->select();
  713. return $role;
  714. }
  715. //api获取所有二级调度角色
  716. public function get_two_dispatch_role1($orgId,$type){
  717. $map[] = ['enable','=',1];
  718. $map[] = ['level','=',2];
  719. $map[] = ['del','=',0];
  720. $map[] = ['parent_id','=',9];
  721. $map[] = ['org_id','=',$orgId];
  722. $map[] = ['','exp',Db::raw("FIND_IN_SET(".$type.",work_type_mode)")];
  723. $role = Db::name('roles')
  724. ->field('id,name as realName')
  725. ->where($map)
  726. ->select();
  727. return $role;
  728. }
  729. // 根据id获取用户信息
  730. public function getUserInfo($id){
  731. $info = Db::name('user')
  732. ->where('id',$id)
  733. ->where('del',0)
  734. ->find();
  735. return $info;
  736. }
  737. //获取所有调度
  738. public function get_dispatch_user($orgId){
  739. $roles = Db::name('roles')
  740. ->where('parent_id',9)
  741. ->where('org_id',$orgId)
  742. ->where('del',0)
  743. ->where('enable',1)
  744. ->column('id');
  745. if(empty($roles)){
  746. return [];
  747. }
  748. $user = Db::name('user')
  749. ->alias('a')
  750. ->join('user_roles u','a.id=u.user_id')
  751. ->where('u.roles_id','in',$roles)
  752. ->where('a.del',0)
  753. ->where('a.enable',1)
  754. ->where('a.type',0)
  755. ->select();
  756. return $user;
  757. }
  758. /**
  759. * 获取拥有陪护模块权限的人
  760. * @param $orgId
  761. */
  762. public function get_ph_dispatch_user($orgId){
  763. $appId = Db::name('app_icon')->where('mode',25)->value('id');
  764. if(!$appId){
  765. return [];
  766. }
  767. $map[] = ['org_id','=',$orgId];
  768. $map[] = ['del','=',0];
  769. $map[] = ['enable','=',1];
  770. $map[] = ['','exp',Db::raw("FIND_IN_SET('appauths','".$appId."')")];
  771. $roles = Db::name('roles')->where($map)->column('id');
  772. if(empty($roles)){
  773. return [];
  774. }
  775. $user = Db::name('user')
  776. ->alias('a')
  777. ->join('user_roles u','a.id=u.user_id')
  778. ->where('u.roles_id','in',$roles)
  779. ->where('a.del',0)
  780. ->where('a.enable',1)
  781. ->where('a.type',0)
  782. ->select();
  783. return $user;
  784. }
  785. /**
  786. * 运送获取人员
  787. *
  788. * @author wst
  789. * @date 2021/9/8 14:59
  790. * @return array
  791. */
  792. public function getConveyWorker(){
  793. $list = (new WorkTypeMode())->getRolesUser(6,cur_org_id());
  794. $arr = [];
  795. foreach ($list as $k=>$v){
  796. if(!empty($v['user'])){
  797. foreach ($v['user'] as $k1=>$v1){
  798. $arr[] = [
  799. 'id'=>$v1['id'],
  800. 'title'=>$v1['real_name'],
  801. ];
  802. }
  803. }
  804. }
  805. return $arr;
  806. }
  807. public function wxRegister($post){
  808. $post['id'] = 0;
  809. $result = validate('User')->check($post,[],'wxadd');
  810. if(true !== $result){
  811. $this->error = validate('User')->getError();
  812. return false;
  813. }
  814. $data = [
  815. 'mobile' => $post['mobile'],
  816. 'real_name' => $post['real_name'],
  817. 'password' => $post['password'],
  818. ];
  819. Db::startTrans();
  820. try{
  821. $data['account'] = md5($data['mobile'].$data['password'].get_unique_id().time());
  822. $data['password'] = password_hash($data['password'], PASSWORD_DEFAULT);
  823. $data['create_time'] = date('Y-m-d H:i:s');
  824. $data['ip'] = request()->ip();
  825. $data['update_time'] = date('Y-m-d H:i:s');
  826. $data['last_login_time'] = date('Y-m-d H:i:s');
  827. $data['type'] = 2;
  828. $data['enable'] = 1;
  829. $data['del'] = 0;
  830. $uid = Db::name('user')->insertGetId($data);
  831. if(!$uid){
  832. \exception('操作失败');
  833. }
  834. Db::name('user_org')->insert(['user_id'=>$uid,'org_id'=>$post['orgId']]);
  835. if($post['depId'] > 0){
  836. Db::name('user_dep')->insert(['user_id'=>$uid,'dep_id'=>$post['depId']]);
  837. }
  838. $token = model('Token')->updateToken($uid);
  839. if(!$token){
  840. \exception(model('Token')->getError());
  841. }
  842. Db::commit();
  843. return ['user_id' => $uid,'token' => $token,'org_id' => $post['orgId']];
  844. }catch (Exception $e){
  845. Db::rollback();
  846. $this->error = $e->getMessage();
  847. return false;
  848. }
  849. }
  850. /**
  851. * 绿化养护获取人员
  852. *
  853. * @author wst
  854. * @date 2021/9/6 10:45
  855. */
  856. public function getGreenWorker() {
  857. $list = (new WorkTypeMode())->getRolesUser(19, cur_org_id());
  858. $arr = [];
  859. foreach ($list as $k => $v) {
  860. if (!empty($v['user'])) {
  861. foreach ($v['user'] as $k1 => $v1) {
  862. $arr[] = [
  863. 'id' => $v1['id'],
  864. 'title' => $v1['real_name'],
  865. ];
  866. }
  867. }
  868. }
  869. return $arr;
  870. }
  871. public function getYsUser($orgId,$work=0){
  872. $list = (new WorkTypeMode())->getRolesUser(3,$orgId,1);
  873. $arr = [];
  874. foreach ($list as $k=>$v){
  875. if(!empty($v['user'])){
  876. foreach ($v['user'] as $k1=>$v1){
  877. $arr[] = [
  878. 'id'=>$v1['id'],
  879. 'real_name'=>$v1['real_name'],
  880. ];
  881. }
  882. }
  883. }
  884. return $arr;
  885. }
  886. public function getWorkTypeModeUser($id,$orgId,$work=0){
  887. $list = (new WorkTypeMode())->getRolesUser($id,$orgId,$work);
  888. $arr = [];
  889. foreach ($list as $k=>$v){
  890. if(!empty($v['user'])){
  891. foreach ($v['user'] as $k1=>$v1){
  892. $arr[] = [
  893. 'id'=>$v1['id'],
  894. 'title'=>$v1['real_name'],
  895. ];
  896. }
  897. }
  898. }
  899. return $arr;
  900. }
  901. }