123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Description: 用户
  4. // +----------------------------------------------------------------------
  5. // | Author: Michael_xu | gengxiaoxu@5kcrm.com
  6. // +----------------------------------------------------------------------
  7. namespace app\admin\model;
  8. use think\Db;
  9. use app\admin\model\Common;
  10. use com\verify\HonrayVerify;
  11. use think\Cache;
  12. use think\Request;
  13. class User extends Common
  14. {
  15. /**
  16. * 为了数据库的整洁,同时又不影响Model和Controller的名称
  17. * 我们约定每个模块的数据表都加上相同的前缀,比如CRM模块用crm作为数据表前缀
  18. */
  19. protected $name = 'admin_user';
  20. protected $createTime = 'create_time';
  21. protected $updateTime = false;
  22. protected $autoWriteTimestamp = true;
  23. protected $insert = [
  24. 'status' => 2,
  25. ];
  26. protected $statusArr = ['禁用','启用','未激活'];
  27. protected $dateFormat = 'Y-m-d';
  28. protected $type = [
  29. 'create_time' => 'timestamp',
  30. 'update_time' => 'timestamp',
  31. ];
  32. /**
  33. * 导入字段
  34. *
  35. * @var array
  36. * @author Ymob
  37. * @datetime 2019-10-25 15:35:25
  38. */
  39. public static $import_field_list = [
  40. [
  41. 'field' => 'username',
  42. 'name' => '手机号(登录名)',
  43. 'form_type' => 'mobile',
  44. 'is_null' => 1,
  45. // 'is_unique' => 1 //guogaobo 导入此字段会进行验重查询调用修改用户方法
  46. ],
  47. [
  48. 'field' => 'password',
  49. 'name' => '密码',
  50. 'form_type' => 'text',
  51. 'is_null' => 1,
  52. ],
  53. [
  54. 'field' => 'realname',
  55. 'name' => '姓名',
  56. 'form_type' => 'text',
  57. 'is_null' => 1,
  58. ],
  59. [
  60. 'field' => 'sex',
  61. 'name' => '性别',
  62. 'form_type' => 'select',
  63. 'setting' => ['男', '女'],
  64. 'is_null' => 0,
  65. ],
  66. [
  67. 'field' => 'email',
  68. 'name' => '邮箱',
  69. 'form_type' => 'email',
  70. 'is_null' => 0,
  71. ],
  72. [
  73. 'field' => 'post',
  74. 'name' => '岗位',
  75. 'form_type' => 'text',
  76. 'is_null' => 0,
  77. ]
  78. ];
  79. /**
  80. * 获取用户所属所有用户组
  81. * @param array $param [description]
  82. */
  83. public function groups()
  84. {
  85. return $this->belongsToMany('group', 'admin_access', 'group_id', 'user_id');
  86. }
  87. public function structureList($structure_id,$str)
  88. {
  89. $str_ids = structureList($structure_id,$str);
  90. return $str_ids;
  91. }
  92. /**
  93. * [getDataList 列表]
  94. * @AuthorHTL
  95. * @param [string] $map [查询条件]
  96. * @param [number] $page [当前页数]
  97. * @param [number] $limit [每页数量]
  98. * @return [description]
  99. */
  100. public function getDataList($request)
  101. {
  102. $request = $this->fmtRequest( $request );
  103. $fieldarray = ['search','group_id','structure_id','status','type','page','limit','pageType'];
  104. $map = $request['map'] ? : [];
  105. if (isset($map['search']) && $map['search']) {
  106. $map['user.username|user.realname'] = ['like', '%'.$map['search'].'%'];
  107. }
  108. unset($map['search']);
  109. //角色员工
  110. if ($map['group_id']) {
  111. $group_user_ids = db('admin_access')->where(['group_id' => $map['group_id']])->column('user_id');
  112. if ($map['group_id'] == 1 && !$group_user_ids) {
  113. $group_user_ids = ['1'];
  114. }
  115. $map['user.id'] = array('in',$group_user_ids);
  116. }
  117. $exp = new \think\db\Expression('field(user.status,1,2,0)');
  118. // 默认除去超级管理员
  119. // $map['user.id'] = array('neq', 1);
  120. if($map['structure_id']){
  121. //获取部门下员工列表
  122. $str_ids = structureList($map['structure_id'],'');
  123. $new_str_ids = rtrim($str_ids,',');
  124. $map['user.structure_id'] = ['in',$new_str_ids]; //$map['structure_id'];
  125. }
  126. unset($map['structure_id']);
  127. if ($map['status'] || $map['group_id']) {
  128. if ($map['status'] != 3) {
  129. $map['user.status'] = ($map['status'] !== 'all') ? ($map['status'] ? : ['gt',0]) : ['egt',0];
  130. } else {
  131. $map['user.create_time'] = ['gt', time() - 86400 * 7];
  132. }
  133. } else {
  134. $map['user.status'] = 0;
  135. }
  136. unset($map['status']);
  137. $map['user.type'] = 1;
  138. if(isset($map['type'])) $map['user.type'] == ($map['type'] == '0') ? 0 : 1;
  139. //过滤字段
  140. foreach($fieldarray as $value){
  141. unset($map[$value]);
  142. }
  143. //获取列表
  144. $dataCount = db('admin_user')
  145. ->alias('user')
  146. ->join('__ADMIN_STRUCTURE__ structure', 'structure.id = user.structure_id', 'LEFT')
  147. // ->join('HrmUserDet hud','hud.user_id = user.id','LEFT')
  148. ->where($map)
  149. ->count();
  150. $list = db('admin_user')
  151. ->alias('user')
  152. ->join('__ADMIN_STRUCTURE__ structure', 'structure.id = user.structure_id', 'LEFT')
  153. // ->join('HrmUserDet hud','hud.user_id = user.id','LEFT')
  154. ->limit(($request['page']-1)*$request['limit'], $request['limit'])
  155. ->where($map)
  156. ->field('user.id,user.username,user.img,user.thumb_img,user.realname,user.num,user.email,user.mobile,user.sex,user.structure_id,user.post,user.status,user.parent_id,user.type,user.create_time,structure.name as s_name')
  157. ->order($exp)
  158. ->order('user.id asc')
  159. ->select();
  160. foreach ($list as $k=>$v) {
  161. //直属上级
  162. $list[$k]['status_name'] = $v['status']=='1'?'启用':'禁用';
  163. $parentInfo = [];
  164. $parentInfo = $this->getUserById($v['parent_id']);
  165. $list[$k]['parent_name'] = $v['parent_id'] ? $parentInfo['realname'] : '';
  166. $list[$k]['status_name'] = $v['status'] ? $this->statusArr[$v['status']] : '停用';
  167. //角色
  168. $groupsArr = $this->get($v['id'])->groups;
  169. $groups = [];
  170. $groupids = [];
  171. foreach ($groupsArr as $key=>$val) {
  172. $groups[] = $val['title'];
  173. $groupids[] = $val['id'];
  174. }
  175. $list[$k]['groups'] = $groups ? implode(',',$groups) : '';
  176. $list[$k]['groupids'] = $groupids ? implode(',',$groupids) : '';
  177. $list[$k]['img'] = $v['img'] ? getFullPath($v['img']) : '';
  178. $list[$k]['thumb_img'] = $v['thumb_img'] ? getFullPath($v['thumb_img']) : '';
  179. $list[$k]['create_time'] = $v['create_time'] ? date('Y-m-d H:i:s', $v['create_time']) : '';
  180. $list[$k]['s_name'] = !empty($v['s_name']) ? $v['s_name'] : '';
  181. $list[$k]['structure_id'] = !empty($v['structure_id']) ? $v['structure_id'] : '';
  182. }
  183. $data = [];
  184. $data['list'] = $list;
  185. $data['dataCount'] = $dataCount;
  186. return $data;
  187. }
  188. /*
  189. *根据字符串展示参与人 use by work
  190. */
  191. public function getDataByStr($idstr)
  192. {
  193. $idArr = stringToArray($idstr);
  194. if (!$idArr) {
  195. return [];
  196. }
  197. $list = $this->field('id,username,realname,thumb_img')->where(['id' => ['in',$idArr]])->select();
  198. foreach($list as $key=>$value){
  199. $list[$key]['thumb_img'] = $value['thumb_img']?getFullPath($value['thumb_img']):'';
  200. }
  201. return $list;
  202. }
  203. /**
  204. * [getDataById 根据主键获取详情]
  205. * @param string $id [主键]
  206. * @return
  207. */
  208. public function getDataById($id = '')
  209. {
  210. $data = Db::name('AdminUser')->find($id);
  211. if (!$data) {
  212. $this->error = '暂无此数据';
  213. return false;
  214. }
  215. unset($data['password']);
  216. unset($data['authkey']);
  217. if($data['structure_id']) {
  218. $structureDet = Db::name('AdminStructure')->field('id,name')->where('id = '.$data['structure_id'].'')->find();
  219. $data['structure_name'] = $structureDet['name'];
  220. } else {
  221. $data['structure_name'] = '暂无';
  222. }
  223. if($data['parent_id']) {
  224. $parentDet = Db::name('AdminUser')->field('id,realname')->where('id = '.$data['parent_id'].'')->find();
  225. $data['parent_name'] = $parentDet['realname'];
  226. } else {
  227. $data['parent_name'] = '暂无';
  228. }
  229. $data['thumb_img'] = getFullPath($data['thumb_img']);
  230. $data['img'] = getFullPath($data['img']);
  231. //$data['groups'] = $this->get($id)->groups();
  232. return $data;
  233. }
  234. /**
  235. * 创建用户
  236. * @param array $param [description]
  237. */
  238. public function createData($param)
  239. {
  240. // 非导入数据
  241. if (request()->action() == 'import') {
  242. $temp = [];
  243. foreach (self::$import_field_list as $key => $val) {
  244. $temp[$val['field']] = $param[$val['field']];
  245. }
  246. $param = $temp;
  247. $param['structure_id'] = 0;
  248. if(db('admin_user')->where('username',$param['username'])->find()){
  249. $this->error = '手机号已存在';
  250. return false;
  251. }
  252. } else {
  253. if (empty($param['group_id']) || !is_array($param['group_id'])) {
  254. $this->error = '请至少勾选一个用户组';
  255. return false;
  256. }
  257. }
  258. // 验证
  259. $validate = validate($this->name);
  260. if (!$validate->check($param)) {
  261. $this->error = $validate->getError();
  262. return false;
  263. }
  264. $syncModel = new \app\admin\model\Sync();
  265. $this->startTrans();
  266. try {
  267. $salt = substr(md5(time()),0,4);
  268. $param['salt'] = $salt;
  269. if (!$param['password']) {
  270. $password = $param['username'];
  271. } else {
  272. $password = $param['password'];
  273. }
  274. $param['password'] = user_md5($password, $salt, $param['username']);
  275. $param['type'] = 1;
  276. $param['mobile'] = $param['username'];
  277. $this->data($param)->allowField(true)->isUpdate(false)->save();
  278. $user_id = (int) $this->getLastInsId();
  279. //员工档案
  280. $data['user_id'] = $param['user_id'];
  281. unset($param['user_id']);
  282. $data['user_id'] = $user_id;
  283. $data['mobile'] = $param['username'];
  284. $data['email'] = $param['email'] ? : '';
  285. $data['sex'] = $param['sex'] ? : '';
  286. $data['create_time'] = time();
  287. Db::name('HrmUserDet')->insert($data);
  288. $userGroups = [];
  289. foreach ($param['group_id'] as $k => $v) {
  290. $userGroup['user_id'] = $user_id;
  291. $userGroup['group_id'] = $v;
  292. $userGroups[] = $userGroup;
  293. }
  294. if ($userGroups) {
  295. Db::name('admin_access')->insertAll($userGroups);
  296. }
  297. $this->commit();
  298. $param['user_id'] = $data['user_id'];
  299. $resSync = $syncModel->syncData($param);
  300. return true;
  301. } catch(\Exception $e) {
  302. $this->rollback();
  303. $this->error = '添加失败';
  304. return false;
  305. }
  306. }
  307. //导入成为正式用户
  308. public function beusers($request)
  309. {
  310. if ($request['userlist']&&is_array($request['userlist'])) {
  311. $flag = true;
  312. foreach ($request['userlist'] as $value) {
  313. $userInfo = Db::name('AdminUser')->where('id = '.$value.'')->find();
  314. $userDet = Db::name('HrmUserDet')->where('user_id = '.$value.'')->find();
  315. $temp['status'] = 1;
  316. $temp['type'] = 1;
  317. $temp['username'] = $userDet['mobile'];
  318. $salt = substr(md5(time()),0,4);
  319. $temp['salt'] = $salt;
  320. $password = $userDet['mobile'];
  321. $temp['password'] = user_md5($password, $salt, $temp['username']);
  322. $flag = $flag && Db::name('AdminUser')->where('id ='.$value)->update($temp);
  323. }
  324. if ($flag) {
  325. return true;
  326. } else {
  327. $this->error = '操作失败';
  328. return false;
  329. }
  330. } else {
  331. $this->error = '参数错误';
  332. return false;
  333. }
  334. }
  335. /**
  336. * 通过id修改用户
  337. * @param array
  338. */
  339. public function updateDataById($param, $id)
  340. {
  341. if ($param['user_id']) {
  342. //修改个人信息
  343. $data['email'] = $param['email'];
  344. $data['sex'] = $param['sex'];
  345. // $data['mobile'] = $param['username'];
  346. if (db('admin_user')->where(['username' => $param['username'],'id' => ['neq',$param['user_id']]])->find()) {
  347. $this->error = '手机号已存在';
  348. return false;
  349. }
  350. Db::name('HrmUserDet')->where(['user_id' => $param['user_id']])->update($data);
  351. $data['realname'] = $param['realname'];
  352. $data['post'] = $param['post'];
  353. $flag = $this->where(['id' => $param['user_id']])->update($data);
  354. if ($flag==0 || $flag==1) {
  355. return true;
  356. } else {
  357. $this->error = '保存失败';
  358. return false;
  359. }
  360. } else {
  361. // 不能操作超级管理员
  362. // if ($id == 1) {
  363. // $this->error = '非法操作';
  364. // return false;
  365. // }
  366. $checkData = $this->get($id);
  367. $userInfo = $checkData->data;
  368. if (!$checkData) {
  369. $this->error = '暂无此数据';
  370. return false;
  371. }
  372. if (request()->action() != 'import') {
  373. if (empty($param['group_id'])) {
  374. $this->error = '请至少勾选一个用户组';
  375. return false;
  376. }
  377. }
  378. $subUserId = getSubUserId(true, 0, $id);
  379. if ((int)$param['parent_id'] == (int)$id) {
  380. $this->error = '直属上级不能是自己';
  381. return false;
  382. }
  383. if ((int)$param['parent_id'] !== 1 && in_array($param['parent_id'],$subUserId)) {
  384. $this->error = '直属上级不能是自己或下属';
  385. return false;
  386. }
  387. if (db('admin_user')->where(['id' => ['neq',$id],'username' => $param['username']])->find()) {
  388. $this->error = '手机号已存在';
  389. return false;
  390. }
  391. $this->startTrans();
  392. try {
  393. $accessModel = model('Access');
  394. if ($param['group_id']) {
  395. //角色员工关系处理
  396. $accessModel->userGroup($id, $param['group_id'], 'update');
  397. }
  398. if (!empty($param['password'])) {
  399. $salt = $userInfo['salt'];
  400. $param['password'] = user_md5($param['password'], $salt, $param['username']);
  401. }
  402. $this->allowField(true)->save($param, ['id' => $id]);
  403. $this->commit();
  404. Cache::rm('user_info' . $id);
  405. // $data['mobile'] = $param['username'];
  406. $data['email'] = $param['email'];
  407. $data['sex'] = $param['sex'];
  408. $data['update_time'] = time();
  409. $flagg = Db::name('HrmUserDet')->where('user_id = '.$id)->update($data);
  410. return true;
  411. } catch(\Exception $e) {
  412. $this->rollback();
  413. $this->error = '编辑失败';
  414. return false;
  415. }
  416. }
  417. }
  418. /**
  419. * [login 登录]
  420. * @AuthorHTL
  421. * @DateTime
  422. * @param [string] $u_username [账号]
  423. * @param [string] $u_pwd [密码]
  424. * @param [string] $verifyCode [验证码]
  425. * @param Boolean $isRemember [是否记住密码]
  426. * @param Boolean $type [是否重复登录]
  427. * @param array $paramArr
  428. * @return [type] [description]
  429. */
  430. public function login($username, $password, $verifyCode = '', $isRemember = false, $type = false, $authKey = '', $paramArr = [])
  431. {
  432. if ($paramArr['dingCode']) {
  433. $dingtalkModel = new \app\admin\model\Dingtalk();
  434. $username = $dingtalkModel->sign($paramArr['dingCode']);
  435. if (!$username) {
  436. $this->error = $dingtalkModel->getError();;
  437. return false;
  438. }
  439. } else {
  440. if (!$password){
  441. $this->error = '密码不能为空';
  442. return false;
  443. }
  444. }
  445. if (config('IDENTIFYING_CODE') && !$type) {
  446. if (!$verifyCode) {
  447. $this->error = '验证码不能为空';
  448. return false;
  449. }
  450. $captcha = new HonrayVerify(config('captcha'));
  451. if (!$captcha->check($verifyCode)) {
  452. $this->error = '验证码错误';
  453. return false;
  454. }
  455. }
  456. $map['username'] = $username;
  457. $map['type'] = 1;
  458. $userInfo = $this->where($map)->find();
  459. if (!$userInfo) {
  460. $this->error = '帐号不存在';
  461. return false;
  462. }
  463. // 登录记录
  464. $login_record = new LoginRecord();
  465. $login_record->user_id = $userInfo['id'];
  466. // 三次出错,十五分钟禁止登录
  467. if (!$login_record->verify()) {
  468. $this->error = $login_record->error;
  469. return false;
  470. }
  471. $userInfo['thumb_img'] = $userInfo['thumb_img'] ? getFullPath($userInfo['thumb_img']) : '';
  472. if (user_md5($password, $userInfo['salt'], $userInfo['username']) !== $userInfo['password'] && !$paramArr['dingCode']) {
  473. $this->error = '账号或密码错误!';
  474. $login_record->createRecord(LoginRecord::TYPE_PWD_ERROR);
  475. return false;
  476. }
  477. if ($userInfo['status'] === 0) {
  478. $this->error = '帐号已被禁用';
  479. $login_record->createRecord(LoginRecord::TYPE_USER_BANNED);
  480. return false;
  481. }
  482. $login_record->createRecord(LoginRecord::TYPE_SUCCESS);
  483. // 获取菜单和权限
  484. $dataList = $this->getMenuAndRule($userInfo['id']);
  485. if ($isRemember || $type) {
  486. $secret['username'] = $username;
  487. $secret['password'] = $password;
  488. $data['rememberKey'] = encrypt($secret);
  489. }
  490. //登录有效时间
  491. $cacheConfig = config('cache');
  492. $loginExpire = !empty($cacheConfig['expire']) ? $cacheConfig['expire'] : 86400 * 30;
  493. // 保存缓存
  494. session_start();
  495. $info['userInfo'] = $userInfo;
  496. $info['sessionId'] = session_id();
  497. $authKey = user_md5($userInfo['username'].$userInfo['password'].$info['sessionId'], $userInfo['salt']);
  498. // $info['_AUTH_LIST_'] = $dataList['rulesList'];
  499. $info['authKey'] = $authKey;
  500. $platform = $paramArr['platform'] ? '_'.$paramArr['platform'] : ''; //请求平台(mobile,ding)
  501. //删除旧缓存
  502. if (Cache::get('Auth_'.$userInfo['authkey'].$platform)) {
  503. Cache::rm('Auth_'.$userInfo['authkey'].$platform);
  504. }
  505. Cache::set('Auth_'.$authKey.$platform, $info, $loginExpire);
  506. unset($userInfo['authkey']);
  507. // 返回信息
  508. $data['authKey'] = $authKey;
  509. $data['sessionId'] = $info['sessionId'];
  510. $data['userInfo'] = $userInfo;
  511. $data['authList'] = $dataList['authList'];
  512. $data['menusList'] = $dataList['menusList'];
  513. $data['loginExpire'] = $loginExpire;
  514. //保存authKey信息
  515. $userData = [];
  516. $userData['authkey'] = $authKey;
  517. $userData['authkey_time'] = time()+$loginExpire;
  518. //把状态未激活至为启用
  519. if ($userInfo['status'] == 2) {
  520. $userData['status'] = 1;
  521. }
  522. $this->where(['id' => $userInfo['id']])->update($userData);
  523. return $data;
  524. }
  525. /**
  526. * 修改密码
  527. * @param array $param [description]
  528. */
  529. public function updatePaw($userInfo, $old_pwd, $new_pwd)
  530. {
  531. if (!$old_pwd) {
  532. $this->error = '请输入旧密码';
  533. return false;
  534. }
  535. if (!$new_pwd) {
  536. $this->error = '请输入新密码';
  537. return false;
  538. }
  539. if ($new_pwd == $old_pwd) {
  540. $this->error = '新旧密码不能一致';
  541. return false;
  542. }
  543. //登录有效时间
  544. $cacheConfig = config('cache');
  545. $loginExpire = $cacheConfig['expire'] ? : 86400 * 3;
  546. $password = $this->where('id', $userInfo['id'])->value('password');
  547. if (user_md5($old_pwd, $userInfo['salt'], $userInfo['username']) != $password) {
  548. $this->error = '原密码错误';
  549. return false;
  550. }
  551. if (user_md5($new_pwd, $userInfo['salt'], $userInfo['username']) == $password) {
  552. $this->error = '密码没改变';
  553. return false;
  554. }
  555. if ($this->where('id', $userInfo['id'])->setField('password', user_md5($new_pwd, $userInfo['salt'], $userInfo['username']))) {
  556. $syncData = [];
  557. $syncModel = new \app\admin\model\Sync();
  558. $syncData['user_id'] = $userInfo['id'];
  559. $syncData['salt'] = $userInfo['salt'];
  560. $syncData['password'] = user_md5($new_pwd, $userInfo['salt'], $userInfo['username']);
  561. $resSync = $syncModel->syncData($syncData);
  562. $userInfo = $this->where('id', $userInfo['id'])->find();
  563. // 重新设置缓存
  564. session_start();
  565. $cache['userInfo'] = $userInfo;
  566. $cache['authKey'] = user_md5($userInfo['username'].$userInfo['password'].session_id(), $userInfo['salt']);
  567. cache('Auth_'.$cache['authKey'], null);
  568. cache('Auth_'.$cache['authKey'], $cache, $loginExpire);
  569. return $cache['authKey'];//把auth_key传回给前端
  570. }
  571. $this->error = '修改失败';
  572. return false;
  573. }
  574. //根据IDs批量设置密码
  575. public function updatePwdById($param)
  576. {
  577. $syncModel = new \app\admin\model\Sync();
  578. $flag = true;
  579. foreach ($param['id'] as $value) {
  580. $password = '';
  581. $userInfo = db('admin_user')->where(['id' => $value])->find();;
  582. $salt = substr(md5(time()),0,4);
  583. $temp['salt'] = $salt;
  584. $temp['password']= $password = user_md5($param['password'], $salt, $userInfo['username']);
  585. $flag = $flag && Db::name('AdminUser')->where('id ='.$value)->update($temp);
  586. $syncData = [];
  587. $syncData['user_id'] = $value;
  588. $syncData['salt'] = $salt;
  589. $syncData['password'] = $password;
  590. $resSync = $syncModel->syncData($syncData);
  591. }
  592. if ($flag) {
  593. return $flag;
  594. } else {
  595. $this->error ='修改失败,请稍后重试';
  596. return false;
  597. }
  598. }
  599. /**
  600. * 获取菜单和权限 protected
  601. *
  602. * @param $u_id
  603. * @return array
  604. * @throws \think\db\exception\DataNotFoundException
  605. * @throws \think\db\exception\ModelNotFoundException
  606. * @throws \think\exception\DbException
  607. */
  608. public function getMenuAndRule($u_id)
  609. {
  610. $menusList = [];
  611. $ruleMap = [];
  612. $adminTypes = adminGroupTypes($u_id);
  613. if (in_array(1,$adminTypes)) {
  614. $map['status'] = 1;
  615. $menusList = Db::name('admin_menu')->where($map)->order('sort asc')->select();
  616. } else {
  617. $groups = $this->get($u_id)->groups;
  618. $ruleIds = [];
  619. foreach ($groups as $k => $v) {
  620. if (stringToArray($v['rules'])) {
  621. $ruleIds = array_merge($ruleIds, stringToArray($v['rules']));
  622. }
  623. }
  624. $ruleIds = array_unique($ruleIds);
  625. $ruleMap['id'] = array('in', $ruleIds);
  626. $ruleMap['status'] = 1;
  627. }
  628. $newRuleIds = [];
  629. // 重新设置ruleIds,除去部分已删除或禁用的权限。
  630. $rules = Db::name('admin_rule')->where($ruleMap)->select();
  631. // $ruless = Db::name('admin_rule')->where($ruleMap)->where(['level'=>2,'pid'=>0])->column('name');
  632. foreach ($rules as $k => $v) {
  633. $newRuleIds[] = $v['id'];
  634. $rules[$k]['name'] = strtolower($v['name']);
  635. }
  636. //菜单管理(弃用)
  637. // $menuMap['status'] = 1;
  638. // $menuMap['rule_id'] = array('in',$newRuleIds);
  639. // $menusList = Db::name('admin_menu')->where($menuMap)->order('sort asc')->select();
  640. $ret = [];
  641. //处理菜单成树状
  642. $tree = new \com\Tree();
  643. //处理规则成树状
  644. $rulesList = $tree->list_to_tree($rules, 'id', 'pid', 'child', 0, true, array('pid'));
  645. //权限数组
  646. $authList = rulesListToArray($rulesList, $newRuleIds);
  647. //应用控制
  648. $adminConfig = db('admin_config')->where(['pid' => 0,'status' => 1])->column('module');
  649. $adminConfig = $adminConfig ? array_merge($adminConfig,['bi','admin']) : ['bi','admin'];
  650. # 通讯录
  651. if (in_array('book', $adminConfig) && !empty($authList['oa']['addresslist']['index'])) {
  652. $authList['oa']['book']['index'] = true;
  653. }
  654. # 商业智能权限细化
  655. if ($authList['bi']) {
  656. if (!in_array('taskExamine',$adminConfig) && !in_array('crm',$adminConfig)) {
  657. unset($authList['bi']);
  658. } else {
  659. foreach ($authList['bi'] as $key=>$val) {
  660. if (!in_array('taskExamine',$adminConfig)) {
  661. unset($authList['bi']['oa']);
  662. }
  663. if (!in_array('crm',$adminConfig)) {
  664. unset($authList['bi']['customer']);
  665. unset($authList['bi']['business']);
  666. unset($authList['bi']['product']);
  667. unset($authList['bi']['achievement']);
  668. unset($authList['bi']['contract']);
  669. unset($authList['bi']['portrait']);
  670. unset($authList['bi']['ranking']);
  671. }
  672. }
  673. }
  674. } else {
  675. unset($authList['bi']);
  676. }
  677. # 任务审批
  678. if (in_array('taskExamine', $adminConfig) ) {
  679. $authList['oa']['taskExamine'] = (Object)[];
  680. }
  681. # 项目
  682. if (in_array('work', $adminConfig)) {
  683. $authList['project']['projectLabelManage']['projectLabelAdd'] = !empty($authList['work']['work']['save']);
  684. $authList['project']['projectLabelManage']['projectLabelDelete'] = !empty($authList['work']['work']['save']);
  685. $authList['project']['projectLabelManage']['projectLabelUpdate'] = !empty($authList['work']['work']['save']);
  686. $authList['project']['projectManage']['save'] = !empty($authList['work']['work']['save']);
  687. }
  688. # 日志
  689. if (in_array('log', $adminConfig)) {
  690. $authList['oa']['log'] = (Object)[];
  691. }
  692. # 日历
  693. if (in_array('calendar', $adminConfig)) {
  694. $authList['oa']['calendar'] = (Object)[];
  695. }
  696. $ret['authList'] = $this->resetAuthorityFiled($authList);
  697. $res['manage']=$rules;
  698. return $ret;
  699. }
  700. /**
  701. * todo 应前端要求修改部分权限字段,与java的权限字段保持一致。
  702. *
  703. * @param $authList
  704. * @return mixed
  705. */
  706. private function resetAuthorityFiled($authList)
  707. {
  708. # 客户
  709. if (isset($authList['crm']['customer']['deal_status'])) {
  710. $authList['crm']['customer']['dealStatus'] = $authList['crm']['customer']['deal_status'];
  711. unset($authList['crm']['customer']['deal_status']);
  712. }
  713. if (isset($authList['crm']['customer']['nearby'])) {
  714. $authList['crm']['customer']['nearbyCustomer'] = $authList['crm']['customer']['nearby'];
  715. unset($authList['crm']['customer']['nearby']);
  716. }
  717. # 跟进记录
  718. $authList['crm']['followRecord'] = $authList['crm']['activity'];
  719. # 公海
  720. if (isset($authList['crm']['customer']['pool'])) {
  721. $authList['crm']['pool']['index'] = $authList['crm']['customer']['pool'] ? true : false;
  722. }
  723. if (isset($authList['crm']['customer']['distribute'])) {
  724. $authList['crm']['pool']['distribute'] = $authList['crm']['customer']['distribute'] ? true : false;
  725. }
  726. if (isset($authList['crm']['customer']['receive'])) {
  727. $authList['crm']['pool']['receive'] = $authList['crm']['customer']['receive'] ? true : false;
  728. }
  729. if (isset($authList['crm']['customer']['poolexcelexport'])) {
  730. $authList['crm']['pool']['excelexport'] = $authList['crm']['customer']['poolexcelexport'] ? true : false;
  731. }
  732. if (isset($authList['crm']['customer']['pooldelete'])) {
  733. $authList['crm']['pool']['delete'] = $authList['crm']['customer']['pooldelete'] ? true : false;
  734. }
  735. # 合同
  736. if (isset($authList['crm']['contract']['discard'])) {
  737. $authList['crm']['contract']['discard'] = false;
  738. }
  739. # 发票
  740. if (isset($authList['crm']['invoice']['setinvoice'])) {
  741. $authList['crm']['invoice']['updateInvoiceStatus'] = $authList['crm']['invoice']['setinvoice'];
  742. }
  743. # 发票抬头权限
  744. if (!empty($authList['crm']['invoice']['index'])) {
  745. $authList['crm']['invoiceTitle']['index'] = true;
  746. }
  747. unset($authList['work']['work']);
  748. unset($authList['work']['task']);
  749. unset($authList['work']['taskclass']);
  750. # 跟进记录
  751. if (!empty($authList['crm']['record']['index'])) {
  752. $authList['crm']['followRecord']['delete'] = true;
  753. $authList['crm']['followRecord']['read'] = true;
  754. $authList['crm']['followRecord']['save'] = true;
  755. $authList['crm']['followRecord']['update'] = true;
  756. }
  757. unset($authList['crm']['record']);
  758. # admin:system
  759. if (!empty($authList['admin']['system']['index'])) {
  760. $authList['admin']['system']['read'] = $authList['admin']['system']['index'];
  761. unset($authList['admin']['system']['index']);
  762. }
  763. if (!empty($authList['admin']['system']['save'])) {
  764. $authList['admin']['system']['update'] = $authList['admin']['system']['save'];
  765. }
  766. # admin:configSet
  767. if (!empty($authList['admin']['configset']['index'])) {
  768. $authList['admin']['configSet']['read'] = $authList['admin']['configset']['index'];
  769. }
  770. if (!empty($authList['admin']['configset']['update'])) {
  771. $authList['admin']['configSet']['update'] = $authList['admin']['configset']['update'];
  772. }
  773. unset($authList['admin']['configset']);
  774. # admin:users
  775. if (!empty($authList['admin']['users']['index'])) {
  776. $authList['admin']['users']['read'] = $authList['admin']['users']['index'];
  777. unset($authList['admin']['users']['index']);
  778. }
  779. if (!empty($authList['admin']['users']['enables'])) {
  780. $authList['admin']['users']['userEnables'] = $authList['admin']['users']['enables'];
  781. unset($authList['admin']['users']['enables']);
  782. }
  783. if (!empty($authList['admin']['users']['save'])) {
  784. $authList['admin']['users']['userSave'] = $authList['admin']['users']['save'];
  785. unset($authList['admin']['users']['save']);
  786. }
  787. if (!empty($authList['admin']['users']['update'])) {
  788. $authList['admin']['users']['userUpdate'] = $authList['admin']['users']['update'];
  789. unset($authList['admin']['users']['update']);
  790. }
  791. if (!empty($authList['admin']['users']['structures_save'])) {
  792. $authList['admin']['users']['deptSave'] = $authList['admin']['users']['structures_save'];
  793. unset($authList['admin']['users']['structures_save']);
  794. }
  795. if (!empty($authList['admin']['users']['structures_update'])) {
  796. $authList['admin']['users']['deptUpdate'] = $authList['admin']['users']['structures_update'];
  797. unset($authList['admin']['users']['structures_update']);
  798. }
  799. if (!empty($authList['admin']['users']['structures_delete'])) {
  800. $authList['admin']['users']['deptDelete'] = $authList['admin']['users']['structures_delete'];
  801. unset($authList['admin']['users']['structures_delete']);
  802. }
  803. # admin:group 角色权限管理
  804. if (!empty($authList['admin']['groups'])) {
  805. $authList['admin']['permission'] = $authList['admin']['groups'];
  806. unset($authList['admin']['groups']);
  807. }
  808. # admin:examine_flow
  809. if (!empty($authList['admin']['examine_flow'])) {
  810. $authList['admin']['examineFlow'] = $authList['admin']['examine_flow'];
  811. unset($authList['admin']['examine_flow']);
  812. }
  813. # admin:printing
  814. if (!empty($authList['admin']['printing'])) {
  815. $authList['admin']['print'] = $authList['admin']['printing'];
  816. unset($authList['admin']['printing']);
  817. }
  818. # admin:work
  819. if (!empty($authList['admin']['work']['work'])) {
  820. $authList['admin']['work']['update'] = $authList['admin']['work']['work'];
  821. unset($authList['admin']['work']['work']);
  822. }
  823. # admin:log
  824. unset($authList['admin']['loginrecord']);
  825. unset($authList['admin']['log']);
  826. # admin:initialize
  827. if (!empty($authList['admin']['initialize'])) {
  828. $authList['admin']['init']['initData'] = $authList['admin']['initialize']['update'];
  829. $authList['admin']['init']['index'] = $authList['admin']['initialize']['index'];
  830. unset($authList['admin']['initialize']);
  831. }
  832. # admin
  833. if (!empty($authList['admin'])) {
  834. $authList['manage'] = $authList['admin'];
  835. $adminAuth = [
  836. 'configSet.read', 'crm.achievement', 'crm.field', 'crm.pool', 'crm.setting',
  837. 'examineFlow.index', 'init.initData', 'oa.examine', 'system.read', 'users.read',
  838. 'work.update','permission.update'
  839. ];
  840. foreach ($authList['manage'] AS $key1 => $value1) {
  841. foreach ($value1 AS $key2 => $value2) {
  842. if (in_array($key1.'.'.$key2, $adminAuth)) {
  843. $authList['manage']['other_rule'] = [
  844. 'setwelcome' => true,
  845. 'setworklogrule' => true,
  846. 'welcome' => true,
  847. 'worklogrule' => true
  848. ];
  849. }
  850. }
  851. }
  852. }
  853. if (empty($authList['manage']['other_rule'])) unset($authList['manage']);
  854. unset($authList['admin']);
  855. # 通讯录
  856. $authList['email'] = (Object)[];
  857. $authList['hrm'] = (Object)[];
  858. $authList['jxc'] = (Object)[];
  859. $authList['knowledge'] = (Object)[];
  860. return $authList;
  861. }
  862. /**
  863. * 获取权限结构数组
  864. * @param
  865. */
  866. public function getRulesList($uid)
  867. {
  868. $ruleMap = [];
  869. $adminTypes = adminGroupTypes($uid);
  870. if (in_array(1,$adminTypes)) {
  871. $map['status'] = 1;
  872. } else {
  873. $groups = $this->get($uid)->groups;
  874. $ruleIds = [];
  875. foreach($groups as $k => $v) {
  876. if (stringToArray($v['rules'])) {
  877. $ruleIds = array_merge($ruleIds, stringToArray($v['rules']));
  878. }
  879. }
  880. $ruleIds = array_unique($ruleIds);
  881. $ruleMap['id'] = array('in', $ruleIds);
  882. $ruleMap['status'] = 1;
  883. }
  884. $newRuleIds = [];
  885. // 重新设置ruleIds,除去部分已删除或禁用的权限。
  886. $rules = Db::name('admin_rule')->where($ruleMap)->select();
  887. foreach ($rules as $k => $v) {
  888. $newRuleIds[] = $v['id'];
  889. $rules[$k]['name'] = strtolower($v['name']);
  890. }
  891. //处理规则成树状
  892. $tree = new \com\Tree();
  893. $rulesList = $tree->list_to_tree($rules, 'id', 'pid', 'child', 0, true, array('pid'));
  894. $rulesList = rulesDeal($rulesList);
  895. return $rulesList ? : [];
  896. }
  897. /**
  898. * 获取用户所属角色(用户组)
  899. * @param
  900. */
  901. public function getGroupTypeByAction($uid, $m, $c, $a)
  902. {
  903. //根据$m,$c,$a 获取对应的$a 的rule_id
  904. $rulesList = $this->getRulesList($uid);
  905. if (!in_array($m.'-'.$c.'-'.$a, $rulesList)) {
  906. return false;
  907. }
  908. $mRuleId = db('admin_rule')->where(['name'=>$m,'level'=>1])->value('id');
  909. $cRuleId = db('admin_rule')->where(['name'=>$c,'level'=>2,'pid'=>$mRuleId])->value('id');
  910. $aRuleId = db('admin_rule')->where(['name'=>$a,'level'=>3,'pid'=>$cRuleId])->value('id');
  911. //获取用户组
  912. $groups = $this->get($uid)->groups;
  913. if (!$groups) {
  914. return false;
  915. }
  916. $groupTypes = [];
  917. foreach ($groups as $g) {
  918. if (in_array($aRuleId, explode(',', trim($g['rules'], ',')))) {
  919. $groupTypes[] = $g['type'];
  920. }
  921. }
  922. return $groupTypes ? : [];
  923. }
  924. /**
  925. * 获取有此权限的角色
  926. * @param
  927. */
  928. public function getAllUserByAction($m, $c, $a)
  929. {
  930. $mRuleId = db('admin_rule')->where(['name'=>$m,'level'=>1])->value('id');
  931. $cRuleId = db('admin_rule')->where(['name'=>$c,'level'=>2,'pid'=>$mRuleId])->value('id');
  932. $aRuleId = db('admin_rule')->where(['name'=>$a,'level'=>3,'pid'=>$cRuleId])->value('id');
  933. $groups = db('admin_group')->where(['rules' => ['in',$aRuleId]])->column('id');
  934. $userIds = db('admin_access')->where(['group_id' => ['in',$groups]])->column('user_id');
  935. if (!$userIds) {
  936. //查询管理员
  937. $userIds = db('admin_user')->where(['id' => 1])->column('id');
  938. }
  939. return $userIds;
  940. }
  941. /**
  942. * 根据部门获取部门的userId
  943. * @param $strId 部门ID
  944. * @param $type 2时包含所有下属部门
  945. */
  946. public function getSubUserByStr($structure_id, $type = 1)
  947. {
  948. $allStrIds = (array) $structure_id;
  949. if ($type == 2) {
  950. $structureModel = new \app\admin\model\Structure();
  951. foreach ($allStrIds as $v) {
  952. $allSubStrIds = [];
  953. $allSubStrIds = $structureModel->getAllChild($v);
  954. if ($allSubStrIds) {
  955. $allStrIds = array_merge($allStrIds, $allSubStrIds); //全部关联部门(包含下属部门)
  956. }
  957. }
  958. }
  959. $userIds = db('admin_user')->where(['structure_id' => ['in',$allStrIds]])->column('id');
  960. return $userIds ? : [];
  961. }
  962. /**
  963. * [getUserById 根据主键获取详情]
  964. * @param
  965. * @return
  966. */
  967. public function getUserById($id = '')
  968. {
  969. $data = Db::name('AdminUser')
  970. ->alias('user')
  971. ->join('__ADMIN_STRUCTURE__ structure', 'structure.id = user.structure_id', 'LEFT')
  972. ->where(['user.id' => $id])
  973. ->field([
  974. 'user.id',
  975. 'username',
  976. 'img',
  977. 'thumb_img',
  978. 'realname',
  979. 'parent_id',
  980. 'structure.name' => 'structure_name',
  981. 'structure.id' => 'structure_id'
  982. ])
  983. ->cache('user_info' . $id, null, 'user_info')
  984. ->find();
  985. $data['img'] = $data['img'] ? getFullPath($data['img']) : '';
  986. $data['thumb_img'] = $data['thumb_img'] ? getFullPath($data['thumb_img']) : '';
  987. return $data ? : [];
  988. }
  989. /**
  990. * [getUserNameById 根据主键获取详情]
  991. * @param
  992. * @return
  993. */
  994. public function getUserNameById($id = '')
  995. {
  996. $data = $this->where(['id' => $id])->value('realname');
  997. return $data ? : '查看详情';
  998. }
  999. /**
  1000. * [getUserNameByArr 根据主键获取详情]
  1001. * @param
  1002. * @return
  1003. */
  1004. public function getUserNameByArr($ids = [])
  1005. {
  1006. if (!is_array($ids)) {
  1007. $idArr[] = $ids;
  1008. } else {
  1009. $idArr = $ids;
  1010. }
  1011. $data = $this->where(['id' => array('in', $idArr)])->column('realname');
  1012. return $data ? : [];
  1013. }
  1014. /**
  1015. * [getAdminId 获取管理员ID]
  1016. * @param
  1017. * @return
  1018. */
  1019. public function getAdminId()
  1020. {
  1021. $adminGroupUser = db('admin_access')->where(['group_id' => 1])->column('user_id');
  1022. $userIDs = $adminGroupUser ? array_merge($adminGroupUser, [1]) : [1];
  1023. return $userIDs ? : [1];
  1024. }
  1025. /**
  1026. * [getUserByIdArr 根据ID数组获取列表]
  1027. * @param
  1028. * @return
  1029. */
  1030. public function getUserByIdArr($ids = [])
  1031. {
  1032. $list = $this
  1033. ->alias('user')
  1034. ->join('__ADMIN_STRUCTURE__ structure', 'structure.id = user.structure_id', 'LEFT')
  1035. ->where(['user.id' => ['in', $id]])->field('user.id,username,img,thumb_img,realname,parent_id,structure.name as structure_name,structure.id as structure_id')->select();
  1036. return $list ? : [];
  1037. }
  1038. /**
  1039. * [getUserByPer 获取权限范围的user_id]
  1040. * @param
  1041. * @return
  1042. */
  1043. public function getUserByPer($m = '', $c = '', $a = ''){
  1044. $request = Request::instance();
  1045. $header = $request->header();
  1046. $authKey = $header['authkey'];
  1047. $m = $m ? strtolower($m) : strtolower($request->module());
  1048. $c = $c ? strtolower($c) : strtolower($request->controller());
  1049. $a = $a ? strtolower($a) : strtolower($request->action());
  1050. $cache = cache('Auth_'.$authKey);
  1051. if (!$cache) {
  1052. return false;
  1053. }
  1054. $userInfo = $cache['userInfo'];
  1055. //用户所属用户组类别(数组)
  1056. $groupTypes = $this->getGroupTypeByAction($userInfo['id'], $m, $c, $a);
  1057. //数组去重
  1058. $groupTypes = $groupTypes ? array_unique($groupTypes) : [];
  1059. //用户组类别(1本人,2本人及下属,3本部门,4本部门及下属部门,5全部)
  1060. $adminIds = $this->getAdminId();
  1061. $userIds = [];
  1062. if (in_array($userInfo['id'],$adminIds)) {
  1063. $userIds = getSubUserId(true, 1);
  1064. } else {
  1065. if (!$groupTypes) {
  1066. return [];
  1067. }
  1068. if (in_array(5, $groupTypes)) {
  1069. $userIds = getSubUserId(true, 1);
  1070. } else {
  1071. foreach ($groupTypes as $v) {
  1072. if ($v == 1) {
  1073. $userIds = [$userInfo['id']];
  1074. } elseif ($v == 2) {
  1075. $userIds = getSubUserId();
  1076. } elseif ($v == 3) {
  1077. $userIds = $this->getSubUserByStr($userInfo['structure_id']);
  1078. } elseif ($v == 4) {
  1079. $userIds = $this->getSubUserByStr($userInfo['structure_id'], 2);
  1080. }
  1081. }
  1082. }
  1083. }
  1084. return $userIds ? : [];
  1085. }
  1086. /*
  1087. *根据部门ID获取员工列表
  1088. *
  1089. */
  1090. public function getUserListByStructureId($structure_id='')
  1091. {
  1092. $map =array();
  1093. if($structure_id){
  1094. $map['structure_id'] = $structure_id;
  1095. }
  1096. $list = Db::name('AdminUser')->field('id as user_id,realname,post,structure_id')->where($map)->select();
  1097. return $list ? : [];
  1098. }
  1099. /*
  1100. *根据字符串返回数组
  1101. *
  1102. */
  1103. public function getListByStr($str)
  1104. {
  1105. $idArr = stringToArray($str);
  1106. $list = db('admin_user')->field('id,username,realname,thumb_img')->where(['id' => ['in',$idArr]])->select();
  1107. return $list;
  1108. }
  1109. /*
  1110. *读写权限
  1111. *
  1112. */
  1113. public function rwPre($user_id, $ro_user_id, $rw_user_id, $action = 'read')
  1114. {
  1115. if ($action == 'update') {
  1116. if (!in_array($user_id, stringToArray($rw_user_id))) {
  1117. return false;
  1118. }
  1119. } else {
  1120. if (!in_array($user_id, stringToArray($ro_user_id))) {
  1121. return false;
  1122. }
  1123. }
  1124. return true;
  1125. }
  1126. /**
  1127. * [getUserThree 员工第三方扩展信息]
  1128. * @param key 分类
  1129. * @author Michael_xu
  1130. * @return [array]
  1131. */
  1132. public function getUserThree($key, $user_id)
  1133. {
  1134. $resValue = db('admin_user_threeparty')->where(['key' => $key,'user_id' => $user_id])->value('value');
  1135. return $resValue ? : '';
  1136. }
  1137. /**
  1138. * 获取当前登录用户信息
  1139. *
  1140. * @param string $key 默认返回所有信息
  1141. * @return mixed
  1142. * @author Ymob
  1143. * @datetime 2019-10-22 14:38:07
  1144. */
  1145. public static function userInfo($key = '')
  1146. {
  1147. $request = Request::instance();
  1148. $header = $request->header();
  1149. $authKey = $header['authkey'];
  1150. $sessionId = $header['sessionid'];
  1151. $paramArr = $request->param();
  1152. $platform = $paramArr['platform'] ? '_' . $paramArr['platform'] : ''; //请求平台(mobile,ding)
  1153. $cache = cache('Auth_' . $authKey . $platform);
  1154. if ($cache) {
  1155. if ($key) {
  1156. return $cache['userInfo'][$key];
  1157. } else {
  1158. return $cache['userInfo'];
  1159. }
  1160. } else {
  1161. return false;
  1162. }
  1163. }
  1164. /**
  1165. * 判断用户是否拥有 某(些) 角色
  1166. *
  1167. * @param array $group_list
  1168. * @param integer $user_id
  1169. * @return bool
  1170. * @author Ymob
  1171. * @datetime 2019-10-25 15:50:48
  1172. */
  1173. public static function checkUserGroup($group_list = [], $user_id = 0)
  1174. {
  1175. $user_id = $user_id ?: self::userInfo('id');
  1176. if (empty($group_list))
  1177. return !!Access::where(['user_id' => $user_id, 'group_id' => ['IN', $group]])->value('user_id');
  1178. }
  1179. /**
  1180. * 顶部菜单栏显示
  1181. * @param $param
  1182. * @return array
  1183. */
  1184. public function sortList($param){
  1185. $list=Db::name('admin_sort')->where('user_id',$param['user_id'])->field('value')->find();
  1186. $list=unserialize($list['value']);
  1187. return $list?:[];
  1188. }
  1189. /**
  1190. * 修改顶部菜单显示
  1191. * @param $param
  1192. */
  1193. public function updateSort($param){
  1194. $list=Db::name('admin_sort')->where('user_id',$param['user_id'])->field('value')->select();
  1195. if($list){
  1196. $data= Db::name('admin_sort')->where('user_id',$param['user_id'])->update(['value'=>serialize($param['value'])]);
  1197. }else{
  1198. $data= Db::name('admin_sort')->insert(['user_id'=>$param['user_id'],'value'=>serialize($param['value'])]);
  1199. }
  1200. return $data;
  1201. }
  1202. /**
  1203. * 复制员工角色
  1204. *
  1205. * @param $param
  1206. * @return bool
  1207. */
  1208. public function copyRole($param)
  1209. {
  1210. $userIds = !empty($param['user_id']) ? $param['user_id'] : [];
  1211. $structureIds = !empty($param['structure_id']) ? $param['structure_id'] : [];
  1212. $groupIds = !empty($param['group_id']) ? $param['group_id'] : [];
  1213. # 员工与角色关联数据
  1214. $userGroup = [];
  1215. # 查询部门下的员工ID
  1216. if (!empty($structureIds)) {
  1217. $userIds = Db::name('admin_user')->whereIn('structure_id', $param['structure_id'])->column('id');
  1218. $userIds = array_unique((array)$userIds);
  1219. }
  1220. Db::startTrans();
  1221. try{
  1222. # 删除员工角色关联数据
  1223. Db::name('admin_access')->whereIn('user_id', $userIds)->delete();
  1224. # 重新设置员工角色
  1225. foreach ($userIds AS $key => $value) {
  1226. # 默认跳过超级管理员
  1227. if ($value == 1) continue;
  1228. foreach ($groupIds AS $k => $v) {
  1229. $userGroup[] = [
  1230. 'user_id' => $value,
  1231. 'group_id' => $v
  1232. ];
  1233. }
  1234. }
  1235. if (!empty($userGroup)) Db::name('admin_access')->insertAll($userGroup);
  1236. Db::commit();
  1237. return true;
  1238. } catch (\Exception $e) {
  1239. Db::rollback();
  1240. return false;
  1241. }
  1242. }
  1243. }