User.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\util\ExcelUtil;
  4. use think\Db;
  5. use think\Exception;
  6. use tools\Phptree;
  7. class User extends Auth
  8. {
  9. public function index(){
  10. if(request()->isAjax()){
  11. //分页参数
  12. $length = input('rows',10,'intval'); //每页条数
  13. $page = input('page',1,'intval'); //第几页
  14. $start = ($page - 1) * $length; //分页开始位置
  15. //排序
  16. $sortRow = input('sidx','u.id','trim'); //排序列
  17. $sort = input('sord','desc','trim'); //排序方式
  18. $order = $sortRow.' '.$sort;
  19. $title = input('title','','trim');
  20. if($title){
  21. $map[] = ['u.account|u.real_name|u.mobile','like','%'.$title.'%'];
  22. }
  23. $enable = input('enable','','trim');
  24. if($enable != ''){
  25. $map[] = ['u.enable','=',$enable];
  26. }
  27. $depId = input('depId','','trim');
  28. if($depId != ''){
  29. // $map[] = ['ud.dep_id','=',$depId];
  30. $uids = Db::name('user_dep')->where('dep_id',$depId)->column('user_id');
  31. $map[] = ['u.id','in',$uids];
  32. }
  33. $rolesId = input('rolesId','','trim');
  34. if($rolesId != ''){
  35. $userRolesId = Db::name('user_roles')
  36. ->where('roles_id',$rolesId)
  37. ->column('user_id');
  38. $map[] = ['u.id','in',$userRolesId];
  39. }
  40. $map[] = ['u.del','=',0];
  41. $map[] = ['u.type','=',0];
  42. $map[] = ['uo.org_id','=',$this->orgId];
  43. $map= empty($map) ? true: $map;
  44. //数据查询
  45. // $lists = db('user')
  46. // ->alias('u')
  47. // ->join('user_dep ud','u.id=ud.user_id')
  48. // ->join('user_roles ur','u.id=ur.user_id')
  49. // ->join('user_org uo','u.id=uo.user_id')
  50. // ->field('u.*,ud.dep_id,ur.roles_id,uo.org_id')
  51. // ->where($map)
  52. // ->limit($start,$length)
  53. // ->order(['u.sort'=>'asc','u.id'=>'desc'])
  54. // ->select();
  55. // foreach ($lists as $k=>$v){
  56. // $lists[$k]['dep_name'] = Db::name('dep')->where('id',$v['dep_id'])->value('title');
  57. // $lists[$k]['roles_name'] = Db::name('roles')->where('id',$v['roles_id'])->value('name');
  58. // }
  59. $lists = db('user')
  60. ->alias('u')
  61. ->join('user_org uo','u.id=uo.user_id')
  62. ->where($map)
  63. ->field('u.*,uo.org_id')
  64. ->limit($start,$length)
  65. ->order(['u.sort'=>'asc','u.id'=>'desc'])
  66. ->select();
  67. foreach ($lists as $k=>$v){
  68. $lists[$k]['dep_name'] = Db::name('user_dep')
  69. ->alias('ud')
  70. ->join('dep d','d.id=ud.dep_id')
  71. ->where('ud.user_id',$v['id'])
  72. ->where('d.org_id',$v['org_id'])
  73. ->value('d.title');
  74. $lists[$k]['roles_name'] = Db::name('user_roles')
  75. ->alias('ur')
  76. ->join('roles r','r.id=ur.roles_id')
  77. ->where('ur.user_id',$v['id'])
  78. ->where('r.org_id',$v['org_id'])
  79. ->value('r.name');
  80. }
  81. //数据返回
  82. $totalCount = db('user')
  83. ->alias('u')
  84. ->join('user_org uo','u.id=uo.user_id')
  85. ->where($map)
  86. ->count();
  87. $totalPage = ceil($totalCount/$length);
  88. $result['page'] = $page;
  89. $result['total'] = $totalPage;
  90. $result['records'] = $totalCount;
  91. $result['rows'] = $lists;
  92. return json($result);
  93. }else{
  94. $deps = model('Dep')->getList();
  95. $this->assign('deps',$deps);
  96. $roles = model('Roles')->getList();
  97. $this->assign('roles',$roles);
  98. return $this->fetch();
  99. }
  100. }
  101. public function wxlist(){
  102. if(request()->isAjax()){
  103. //分页参数
  104. $length = input('rows',10,'intval'); //每页条数
  105. $page = input('page',1,'intval'); //第几页
  106. $start = ($page - 1) * $length; //分页开始位置
  107. //排序
  108. $sortRow = input('sidx','u.id','trim'); //排序列
  109. $sort = input('sord','desc','trim'); //排序方式
  110. $order = $sortRow.' '.$sort;
  111. $title = input('title','','trim');
  112. if($title){
  113. $map[] = ['u.account|u.real_name|u.mobile','like','%'.$title.'%'];
  114. }
  115. $enable = input('enable','','trim');
  116. if($enable != ''){
  117. $map[] = ['u.enable','=',$enable];
  118. }
  119. $depId = input('depId','','trim');
  120. if($depId != ''){
  121. $map[] = ['ud.dep_id','=',$depId];
  122. }
  123. $rolesId = input('rolesId','','trim');
  124. if($rolesId != ''){
  125. $map[] = ['ur.roles_id','=',$rolesId];
  126. }
  127. $map[] = ['u.del','=',0];
  128. $map[] = ['u.type','=',2];
  129. $map[] = ['uo.org_id','=',$this->orgId];
  130. $map= empty($map) ? true: $map;
  131. //数据查询
  132. $lists = db('user')
  133. ->alias('u')
  134. ->join('user_dep ud','u.id=ud.user_id')
  135. ->join('user_roles ur','u.id=ur.user_id','left')
  136. ->join('user_org uo','u.id=uo.user_id')
  137. ->field('u.*,ud.dep_id,ur.roles_id,uo.org_id')
  138. ->where($map)
  139. ->limit($start,$length)
  140. ->order(['u.sort'=>'asc','u.id'=>'desc'])
  141. ->select();
  142. foreach ($lists as $k=>$v){
  143. $lists[$k]['dep_name'] = Db::name('dep')->where('id',$v['dep_id'])->value('title');
  144. $lists[$k]['roles_name'] = Db::name('roles')->where('id',$v['roles_id'])->value('name');
  145. }
  146. //数据返回
  147. $totalCount = db('user')
  148. ->alias('u')
  149. ->join('user_dep ud','ud.user_id = u.id')
  150. ->join('user_roles ur','ur.user_id = u.id')
  151. ->join('user_org uo','uo.user_id = u.id')
  152. ->where($map)->count();
  153. $totalPage = ceil($totalCount/$length);
  154. $result['page'] = $page;
  155. $result['total'] = $totalPage;
  156. $result['records'] = $totalCount;
  157. $result['rows'] = $lists;
  158. return json($result);
  159. }else{
  160. $deps = model('Dep')->getList();
  161. $this->assign('deps',$deps);
  162. $roles = model('Roles')->getList();
  163. $this->assign('roles',$roles);
  164. return $this->fetch();
  165. }
  166. }
  167. /**
  168. * 新增/编辑
  169. */
  170. public function add($id=0){
  171. if(request()->isPost()){
  172. $res = model('User')->addUser();
  173. if($res){
  174. $this->success('操作成功',url('index'));
  175. }else{
  176. $this->error(model('User')->getError());
  177. }
  178. }else{
  179. if($id){
  180. $info = db('user')
  181. ->alias('u')
  182. ->join('user_dep ud','ud.user_id = u.id','left')
  183. ->join('user_roles ur','ur.user_id = u.id','left')
  184. ->field('u.*,ud.dep_id,ur.roles_id')
  185. ->where('u.id',$id)->find();
  186. $role = Db::name('roles')
  187. ->where('id',$info['roles_id'])
  188. ->find();
  189. $info['is_bx'] = ($role['parent_id']==7 || $info['roles_id']==7)?1:0;
  190. $this->assign('info',$info);
  191. }
  192. $deps = model('Dep')->getList();
  193. $this->assign('deps',$deps);
  194. $address = (new \app\common\model\Address())->getListByType(2);
  195. $roles = model('Roles')->getList();
  196. $this->assign('roles',$roles);
  197. $this->assign('address',$address);
  198. $orderType =model('OrderType')->list();
  199. $this->assign('orderType',$orderType);
  200. return $this->fetch();
  201. }
  202. }
  203. /**
  204. * 新增/编辑
  205. */
  206. public function wxadd($id=0){
  207. if(request()->isPost()){
  208. $res = model('User')->wxaddUser();
  209. if($res){
  210. $this->success('操作成功',url('index'));
  211. }else{
  212. $this->error(model('User')->getError());
  213. }
  214. }else{
  215. if($id){
  216. $info = db('user')
  217. ->alias('u')
  218. ->join('user_dep ud','ud.user_id = u.id')
  219. ->join('user_roles ur','ur.user_id = u.id','left')
  220. ->field('u.*,ud.dep_id,ur.roles_id')
  221. ->where('u.id',$id)->find();
  222. $this->assign('info',$info);
  223. }
  224. $deps = model('Dep')->getList();
  225. $this->assign('deps',$deps);
  226. // $address = (new \app\common\model\Address())->getListByType(2);
  227. $roles = model('Roles')->getList();
  228. $this->assign('roles',$roles);
  229. // $this->assign('address',$address);
  230. return $this->fetch();
  231. }
  232. }
  233. /**
  234. * 新增/编辑
  235. */
  236. public function wxzadd($id=0){
  237. if(request()->isPost()){
  238. $res = model('User')->addUser();
  239. if($res){
  240. $this->success('操作成功',url('index'));
  241. }else{
  242. $this->error(model('User')->getError());
  243. }
  244. }else{
  245. if($id){
  246. $info = db('user')
  247. ->alias('u')
  248. ->join('user_dep ud','ud.user_id = u.id')
  249. ->join('user_roles ur','ur.user_id = u.id','left')
  250. ->field('u.*,ud.dep_id,ur.roles_id')
  251. ->where('u.id',$id)->find();
  252. $this->assign('info',$info);
  253. }
  254. $deps = model('Dep')->getList();
  255. $this->assign('deps',$deps);
  256. $address = (new \app\common\model\Address())->getListByType(2);
  257. $roles = model('Roles')->getList();
  258. $this->assign('roles',$roles);
  259. $this->assign('address',$address);
  260. return $this->fetch();
  261. }
  262. }
  263. /**
  264. * 删除记录
  265. * @param int $id
  266. */
  267. public function del($id=0){
  268. if(!$id){
  269. $this->error('参数错误');
  270. }
  271. $res = db('user')->where('id',$id)->setField('del',1);
  272. if($res){
  273. $this->success('删除成功');
  274. }else{
  275. $this->error('删除失败');
  276. }
  277. }
  278. /**
  279. * 改变字段值
  280. * @param int $fv
  281. * @param string $fn
  282. * @param int $fv
  283. */
  284. public function changeField($id=0,$fn='',$fv=0){
  285. if(!$fn||!$id){
  286. $this->error('参数错误');
  287. }
  288. $res = db('user')->where('id',$id)->setField($fn,$fv);
  289. if($res){
  290. $this->success('操作成功');
  291. }else{
  292. $this->error('操作失败');
  293. }
  294. }
  295. public function company(){
  296. if(request()->isAjax()){
  297. //分页参数
  298. $length = input('rows',10,'intval'); //每页条数
  299. $page = input('page',1,'intval'); //第几页
  300. $start = ($page - 1) * $length; //分页开始位置
  301. //排序
  302. $sortRow = input('sidx','u.id','trim'); //排序列
  303. $sort = input('sord','desc','trim'); //排序方式
  304. $order = $sortRow.' '.$sort;
  305. $title = input('title','','trim');
  306. if($title){
  307. $map[] = ['u.account|u.real_name|u.mobile','like','%'.$title.'%'];
  308. }
  309. $enable = input('enable','','trim');
  310. if($enable != ''){
  311. $map[] = ['u.enable','=',$enable];
  312. }
  313. $rolesId = input('rolesId','','trim');
  314. if($rolesId != ''){
  315. $map[] = ['ur.roles_id','=',$rolesId];
  316. }
  317. $map[] = ['u.del','=',0];
  318. $map[] = ['u.type','=',1];
  319. $map= empty($map) ? true: $map;
  320. //数据查询
  321. $lists = db('user')
  322. ->alias('u')
  323. ->join('user_roles ur','ur.user_id = u.id')
  324. ->field('u.*,ur.roles_id')
  325. ->where($map)
  326. ->limit($start,$length)
  327. ->order($order)->select();
  328. foreach ($lists as $k=>$v){
  329. $lists[$k]['roles_name'] = Db::name('roles')->where('id',$v['roles_id'])->value('name');
  330. }
  331. //数据返回
  332. $totalCount = db('user')
  333. ->alias('u')
  334. ->join('user_roles ur','ur.user_id = u.id')
  335. ->where($map)->count();
  336. $totalPage = ceil($totalCount/$length);
  337. $result['page'] = $page;
  338. $result['total'] = $totalPage;
  339. $result['records'] = $totalCount;
  340. $result['rows'] = $lists;
  341. return json($result);
  342. }else{
  343. $roles = model('Roles')->getCommonList(2);
  344. $this->assign('roles',$roles);
  345. $type = Db::name('user')->where('id',$this->userId)->value('type');
  346. $this->assign('type',$type);
  347. return $this->fetch();
  348. }
  349. }
  350. /**
  351. * 新增/编辑
  352. */
  353. public function addcompany($id=0){
  354. if(request()->isPost()){
  355. $res = model('User')->addCompany();
  356. if($res){
  357. $this->success('操作成功',url('index'));
  358. }else{
  359. $this->error(model('User')->getError());
  360. }
  361. }else{
  362. if($id){
  363. $info = db('user')
  364. ->alias('u')
  365. ->join('user_roles ur','ur.user_id = u.id')
  366. ->field('u.*,ur.roles_id')
  367. ->where('u.id',$id)->find();
  368. if($info){
  369. $orgs = $info['orgs']?explode(',',$info['orgs']):[];
  370. $info['orgs'] = [];
  371. if($orgs){
  372. $ids = [];
  373. foreach ($orgs as $k=>$v){
  374. $t = $this->getAllLastId($v); //获取所有上级id 数组是倒序
  375. array_unshift($t,(int)$v); //把组织插入数组的开头
  376. $c = array_reverse($t); //数组反转
  377. $ids[] = $c;
  378. }
  379. $info['orgs'] = $ids;
  380. }
  381. }
  382. $this->assign('info',$info);
  383. }
  384. $orgs = model('Org')->getAllOrgs();
  385. $this->assign('orgs',$orgs);
  386. $tree = $this->orgTree();
  387. $this->assign('orgs',$tree);
  388. $roles = model('Roles')->getCommonList(2);
  389. $this->assign('roles',$roles);
  390. return $this->fetch();
  391. }
  392. }
  393. //获取所有上级id
  394. public function getAllLastId($id,$data=[]){
  395. $pids = DB::name('org')->where('id',$id)->value('parent_id');
  396. if($pids > 0){
  397. $data[] = $pids;
  398. $data = $this->getAllLastId($pids,$data);
  399. }
  400. if(count($data)>0){
  401. return $data;
  402. }else{
  403. return [];
  404. }
  405. }
  406. public function orgTree(){
  407. $lists = Db::name('org')
  408. ->field('id as value,name as label,parent_id')
  409. ->where('del',0)
  410. ->where('enable',1)
  411. ->order('sort asc,id asc')
  412. ->select();
  413. if (empty($lists)) {
  414. return array();
  415. }
  416. $tree = Phptree::makeTree(($lists), array(
  417. 'primary_key'=>'value',
  418. 'parent_key'=>'parent_id',
  419. 'expanded' => true
  420. ));
  421. return $tree;
  422. }
  423. public function modifyPass(){
  424. if(request()->isAjax()){
  425. $data = [
  426. 'oldpass' => input('oldpass','','trim'),
  427. 'pass' => input('pass','','trim'),
  428. 'pass_confirm' => input('pass_confirm','','trim'),
  429. 'id' => $this->userId
  430. ];
  431. $ret = model('User')->modifyPass($data);
  432. if(!$ret){
  433. $this->error(model('User')->getError());
  434. }
  435. // 清除登录信息,重新登录
  436. session('user_auth',null);
  437. session('user_auth_sign',null);
  438. $this->success('操作成功');
  439. }else{
  440. return $this->fetch();
  441. }
  442. }
  443. /*
  444. * 考勤设置*/
  445. public function set($id=0){
  446. if(request()->isPost()){
  447. $data = request()->post();
  448. if(!$data['kq_name']){
  449. $this->error('考勤姓名不能为空');
  450. }
  451. if(!isset($data['kq_img']) && empty($data['kq_img'])){
  452. $this->error('考勤图片不能为空');
  453. }
  454. $uid = $data['id'];
  455. unset($data['id']);
  456. db('user')->where('id',$uid)->update($data);
  457. $this->success('操作成功',url('index'));
  458. }else{
  459. if($id>0){
  460. $info = db('user')->where('id',$id)->find();
  461. $this->assign('info',$info);
  462. }
  463. }
  464. return $this->fetch();
  465. }
  466. /*
  467. * 重置设备编号*/
  468. public function reset($id=0){
  469. if($id < 1){
  470. $this->error('参数错误');
  471. }
  472. db('user')->where('id',$id)->update(['device_sn'=>'']);
  473. $this->success('重置成功',url('index'));
  474. }
  475. /**
  476. * 排序
  477. * @param int $id
  478. * @param int $sort
  479. */
  480. public function changeSort($id=0,$sort=0){
  481. if($id<0||$sort<0){
  482. $this->error('参数错误');
  483. }
  484. $res = db('user')->where('id',$id)->setField('sort',$sort);
  485. if($res){
  486. $this->success('操作成功');
  487. }else{
  488. $this->error('操作失败');
  489. }
  490. }
  491. public function batchSort(){
  492. $data = input('data','','trim');
  493. if(!$data){
  494. $this->error('参数错误');
  495. }
  496. $data = json_decode($data,true);
  497. if(!$data){
  498. $this->error('参数错误');
  499. }
  500. Db::startTrans();
  501. try{
  502. foreach ($data as $k=>$v){
  503. Db::name('user')->where('id',$v['id'])->setField('sort',$v['sort']);
  504. }
  505. Db::commit();
  506. }catch (Exception $e){
  507. Db::rollback();
  508. $this->error('操作失败');
  509. }
  510. $this->success('操作成功');
  511. }
  512. public function copyRoles(){
  513. if(request()->isPost()){
  514. $orgId = input('orgId',0);
  515. if($orgId < 1 ){
  516. $this->error('请选择复制的项目');
  517. }
  518. $rolesList = Db::name('roles')
  519. ->where('org_id',$orgId)
  520. ->where('enable',1)
  521. ->where('del',0)
  522. ->select();
  523. foreach ($rolesList as $k=>$v){
  524. $rolesInfo = Db::name('roles')
  525. ->where('org_id',$this->orgId)
  526. ->where('del',0)
  527. ->where('parent_id',$v['parent_id'])
  528. ->where('name',$v['name'])
  529. ->find();
  530. if(!$rolesInfo){
  531. $data = [
  532. 'name'=>$v['name'],
  533. 'content'=>$v['content'],
  534. 'org_id'=>$this->orgId,
  535. 'parent_id'=>$v['parent_id'],
  536. 'work_type_mode'=>$v['work_type_mode'],
  537. 'type'=>$v['type'],
  538. 'auths'=>$v['auths'],
  539. 'appauths'=>$v['appauths'],
  540. 'level'=>$v['level'],
  541. ];
  542. Db::name('roles')->insert($data);
  543. }
  544. }
  545. $this->success('操作成功',url('index'));
  546. }else{
  547. $orgList = Db::name('org')
  548. ->where('del',0)
  549. ->where('type',2)
  550. ->select();
  551. foreach ($orgList as $k=>$v){
  552. $orgList[$k]['title'] = $v['name'];
  553. }
  554. $this->assign('orgList',$orgList);
  555. return $this->fetch();
  556. }
  557. }
  558. public function workCard($id){
  559. $info = Db::name('user')->where('id',$id)->find();
  560. if (!$info) {
  561. exit('数据不存在');
  562. }
  563. $info['dep'] = Db::name('user_dep')
  564. ->alias('ud')
  565. ->join('dep d','d.id = ud.dep_id')
  566. ->where('ud.user_id',$info['id'])
  567. ->where('d.del',0)
  568. ->value('d.title');
  569. $orgName = Db::name('org')->where('id',$this->orgId)->value('name');
  570. $code = get_qrcode_str('work_card', $id);
  571. $this->assign('code',$code);
  572. $this->assign('info',$info);
  573. $this->assign('orgName',$orgName);
  574. return $this->fetch();
  575. }
  576. public function import(){
  577. return $this->fetch();
  578. }
  579. /**
  580. * 下载点模板
  581. */
  582. public function downloadtem(){
  583. set_time_limit(0);
  584. ini_set("memory_limit","512M");
  585. $header = [
  586. ['title' => '账号', 'name' => 'account','width'=>'30'],
  587. ['title' => '姓名', 'name' => 'real_name','width'=>'30'],
  588. ['title' => '手机号', 'name' => 'mobile','width'=>'30'],
  589. ['title' => '部门ID(默认0)', 'name' => 'depId','width'=>'30'],
  590. ['title' => '角色ID(默认0)', 'name' => 'rolesId','width'=>'30'],
  591. ];
  592. $filename = '用户导入';
  593. ExcelUtil::export($filename,$header,[]);
  594. }
  595. /**
  596. * 导入
  597. */
  598. public function importexcelBak(){
  599. set_time_limit(0);
  600. ini_set("memory_limit", -1);
  601. ob_flush();//清空缓存
  602. flush();//刷新缓存
  603. $orgId = $this->orgId;
  604. if(request()->file()) {
  605. $file = request()->file('file');
  606. // 移动到框架应用根目录/uploads/ 目录下
  607. $info = $file->validate([ 'size'=>50*1024*1024,'ext'=>'xls,xlsx' ])
  608. ->move(env('root_path') . 'public' . DIRECTORY_SEPARATOR . 'uploads'. DIRECTORY_SEPARATOR . 'temp');
  609. if(!$info){
  610. exit('文件上传失败');
  611. }
  612. $img = './uploads/temp/' . $info->getSaveName();
  613. $filePath = str_replace('\\', '/', $img);
  614. $excelArray = ExcelUtil::read($filePath,
  615. ['id','real_name','mobile','dep','roles']
  616. );
  617. unset($excelArray[0]);
  618. if(empty($excelArray)){
  619. exit('文件内容为空');
  620. }
  621. $succ = [];
  622. foreach ($excelArray as $k => $v) {
  623. $v['account'] = $v['mobile'];
  624. if(!$v['real_name']){
  625. echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,姓名为空,未导入</font><br />";
  626. continue;
  627. }
  628. if(!$v['mobile']){
  629. echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,手机号为空,未导入</font><br />";
  630. continue;
  631. }
  632. $v['id'] = 0;
  633. $result = validate('User')->check($v, [], 'bachAdd');
  634. if (true !== $result) {
  635. echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,".validate('User')->getError()."</font><br />";
  636. continue;
  637. }
  638. $v['depId'] = 119;
  639. $rolesId = Db::name('roles')
  640. ->where('name', $v['roles'])
  641. ->where('org_id', $this->orgId)
  642. ->value('id');
  643. if(empty($rolesId)){
  644. echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,角色不存在</font><br />";
  645. continue;
  646. }
  647. $rData = [
  648. 'account'=>$v['mobile'],
  649. 'real_name'=>$v['real_name'],
  650. 'mobile'=>$v['mobile'],
  651. 'ip'=>request()->ip(),
  652. 'password'=>password_hash('123456', PASSWORD_DEFAULT),
  653. 'create_time'=>getTime()
  654. ];
  655. $v['rolesId'] = $rolesId;
  656. $ret = Db::name('user')->insertGetId($rData);
  657. if($ret){
  658. $succ[] = $ret;
  659. $ids = $ret;
  660. Db::name('user_org')->insert(['user_id' => $ids, 'org_id' => $this->orgId]);
  661. if(!empty($v['depId'])){
  662. Db::name('user_dep')->insert(['user_id' => $ids, 'dep_id' => $v['depId']]);
  663. }
  664. if(!empty($v['rolesId'])){
  665. Db::name('user_roles')->insert(['user_id' => $ids, 'roles_id' => $v['rolesId']]);
  666. }
  667. echo "<font color=\"green\" style='margin-left:20px;font-size: 17px'>第".($k+1)."行,导入成功</font><br />";
  668. }else{
  669. echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,导入失败</font><br />";
  670. }
  671. }
  672. }else{
  673. exit('请上传文件');
  674. }
  675. }
  676. public function importexcel(){
  677. set_time_limit(0);
  678. ini_set("memory_limit", -1);
  679. ob_flush();//清空缓存
  680. flush();//刷新缓存
  681. $orgId = $this->orgId;
  682. if(request()->file()) {
  683. $file = request()->file('file');
  684. // 移动到框架应用根目录/uploads/ 目录下
  685. $info = $file->validate([ 'size'=>50*1024*1024,'ext'=>'xls,xlsx' ])
  686. ->move(env('root_path') . 'public' . DIRECTORY_SEPARATOR . 'uploads'. DIRECTORY_SEPARATOR . 'temp');
  687. if(!$info){
  688. exit('文件上传失败');
  689. }
  690. $img = './uploads/temp/' . $info->getSaveName();
  691. $filePath = str_replace('\\', '/', $img);
  692. $excelArray = ExcelUtil::read($filePath,
  693. ['account','real_name','mobile','depId','rolesId']
  694. );
  695. if(empty($excelArray)){
  696. exit('文件内容为空');
  697. }
  698. $succ = [];
  699. foreach ($excelArray as $k => $v) {
  700. if(!$v['account']){
  701. echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,账号为空,未导入</font><br />";
  702. continue;
  703. }
  704. if(!$v['real_name']){
  705. echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,姓名为空,未导入</font><br />";
  706. continue;
  707. }
  708. if(!$v['mobile']){
  709. echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,手机号为空,未导入</font><br />";
  710. continue;
  711. }
  712. $v['id'] = 0;
  713. $result = validate('User')->check($v, [], 'bachAdd');
  714. if (true !== $result) {
  715. echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,".validate('User')->getError()."</font><br />";
  716. continue;
  717. }
  718. if(!empty($v['depId'])){
  719. $checkDep = Db::name('dep')
  720. ->where('org_id',$this->orgId)
  721. ->where('id',$v['depId'])
  722. ->where('del',0)
  723. ->find();
  724. if(!$checkDep){
  725. echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,部门不存在</font><br />";
  726. continue;
  727. }
  728. }
  729. if(!empty($v['rolesId'])){
  730. $checkRoles = Db::name('roles')
  731. ->where('org_id',$this->orgId)
  732. ->where('id',$v['rolesId'])
  733. ->where('del',0)
  734. ->where('parent_id','>',0)
  735. ->find();
  736. if(!$checkRoles){
  737. echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,角色不存在</font><br />";
  738. continue;
  739. }
  740. }
  741. $rData = [
  742. 'account'=>$v['account'],
  743. 'real_name'=>$v['real_name'],
  744. 'mobile'=>$v['mobile'],
  745. 'ip'=>request()->ip(),
  746. 'password'=>password_hash('123456', PASSWORD_DEFAULT),
  747. 'create_time'=>getTime()
  748. ];
  749. $ret = Db::name('user')->insertGetId($rData);
  750. if($ret){
  751. $succ[] = $ret;
  752. $ids = $ret;
  753. Db::name('user_org')->insert(['user_id' => $ids, 'org_id' => $this->orgId]);
  754. if(!empty($v['depId'])){
  755. Db::name('user_dep')->insert(['user_id' => $ids, 'dep_id' => $v['depId']]);
  756. }
  757. if(!empty($v['rolesId'])){
  758. Db::name('user_roles')->insert(['user_id' => $ids, 'roles_id' => $v['rolesId']]);
  759. }
  760. $rolesParentId = Db::name('roles')
  761. ->where('id', $v['rolesId'])
  762. ->value('parent_id');
  763. if ($rolesParentId == 11) {
  764. Db::name('worker')
  765. ->where('user_id',$ids)
  766. ->delete();
  767. $a = [
  768. 'user_id' => $ids
  769. ];
  770. Db::name('worker')
  771. ->insertGetId($a);
  772. }
  773. echo "<font color=\"green\" style='margin-left:20px;font-size: 17px'>第".($k+1)."行,导入成功</font><br />";
  774. }else{
  775. echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,导入失败</font><br />";
  776. }
  777. }
  778. }else{
  779. exit('请上传文件');
  780. }
  781. }
  782. public function getRole(){
  783. $id = input('id','');
  784. $info = Db::name('roles')
  785. ->where('id',$id)
  786. ->find();
  787. $data = [
  788. 'show'=>($info['parent_id']==7 || $id==7)?1:0,
  789. ];
  790. $this->success('操作成功','',$data);
  791. }
  792. }