0
0

User.php 29 KB

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