0
0

User.php 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041
  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 importexcel_bak(){
  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'] = 130;
  639. $rolesId = 145;
  640. // $rolesId = Db::name('roles')
  641. // ->where('name', $v['roles'])
  642. // ->where('org_id', $this->orgId)
  643. // ->value('id');
  644. // if(empty($rolesId)){
  645. // echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,角色不存在</font><br />";
  646. // continue;
  647. // }
  648. $rData = [
  649. 'account'=>$v['mobile'],
  650. 'real_name'=>$v['real_name'],
  651. 'mobile'=>$v['mobile'],
  652. 'ip'=>request()->ip(),
  653. 'password'=>password_hash('123456', PASSWORD_DEFAULT),
  654. 'create_time'=>getTime()
  655. ];
  656. $v['rolesId'] = $rolesId;
  657. $ret = Db::name('user')->insertGetId($rData);
  658. if($ret){
  659. $succ[] = $ret;
  660. $ids = $ret;
  661. Db::name('user_org')->insert(['user_id' => $ids, 'org_id' => $this->orgId]);
  662. if(!empty($v['depId'])){
  663. Db::name('user_dep')->insert(['user_id' => $ids, 'dep_id' => $v['depId']]);
  664. }
  665. if(!empty($v['rolesId'])){
  666. Db::name('user_roles')->insert(['user_id' => $ids, 'roles_id' => $v['rolesId']]);
  667. }
  668. echo "<font color=\"green\" style='margin-left:20px;font-size: 17px'>第".($k+1)."行,导入成功</font><br />";
  669. }else{
  670. echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,导入失败</font><br />";
  671. }
  672. }
  673. }else{
  674. exit('请上传文件');
  675. }
  676. }
  677. public function importexcel(){
  678. set_time_limit(0);
  679. ini_set("memory_limit", -1);
  680. ob_flush();//清空缓存
  681. flush();//刷新缓存
  682. $orgId = $this->orgId;
  683. if(request()->file()) {
  684. $file = request()->file('file');
  685. // 移动到框架应用根目录/uploads/ 目录下
  686. $info = $file->validate([ 'size'=>50*1024*1024,'ext'=>'xls,xlsx' ])
  687. ->move(env('root_path') . 'public' . DIRECTORY_SEPARATOR . 'uploads'. DIRECTORY_SEPARATOR . 'temp');
  688. if(!$info){
  689. exit('文件上传失败');
  690. }
  691. $img = './uploads/temp/' . $info->getSaveName();
  692. $filePath = str_replace('\\', '/', $img);
  693. $excelArray = ExcelUtil::read($filePath,
  694. ['account','real_name','mobile','depId','rolesId']
  695. );
  696. if(empty($excelArray)){
  697. exit('文件内容为空');
  698. }
  699. $succ = [];
  700. foreach ($excelArray as $k => $v) {
  701. if(!$v['account']){
  702. echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,账号为空,未导入</font><br />";
  703. continue;
  704. }
  705. if(!$v['real_name']){
  706. echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,姓名为空,未导入</font><br />";
  707. continue;
  708. }
  709. if(!$v['mobile']){
  710. echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,手机号为空,未导入</font><br />";
  711. continue;
  712. }
  713. $v['id'] = 0;
  714. $result = validate('User')->check($v, [], 'bachAdd');
  715. if (true !== $result) {
  716. echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,".validate('User')->getError()."</font><br />";
  717. continue;
  718. }
  719. if(!empty($v['depId'])){
  720. $checkDep = Db::name('dep')
  721. ->where('org_id',$this->orgId)
  722. ->where('id',$v['depId'])
  723. ->where('del',0)
  724. ->find();
  725. if(!$checkDep){
  726. echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,部门不存在</font><br />";
  727. continue;
  728. }
  729. }
  730. if(!empty($v['rolesId'])){
  731. $checkRoles = Db::name('roles')
  732. ->where('org_id',$this->orgId)
  733. ->where('id',$v['rolesId'])
  734. ->where('del',0)
  735. ->where('parent_id','>',0)
  736. ->find();
  737. if(!$checkRoles){
  738. echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,角色不存在</font><br />";
  739. continue;
  740. }
  741. }
  742. $rData = [
  743. 'account'=>$v['account'],
  744. 'real_name'=>$v['real_name'],
  745. 'mobile'=>$v['mobile'],
  746. 'ip'=>request()->ip(),
  747. 'password'=>password_hash('123456', PASSWORD_DEFAULT),
  748. 'create_time'=>getTime()
  749. ];
  750. $ret = Db::name('user')->insertGetId($rData);
  751. if($ret){
  752. $succ[] = $ret;
  753. $ids = $ret;
  754. Db::name('user_org')->insert(['user_id' => $ids, 'org_id' => $this->orgId]);
  755. if(!empty($v['depId'])){
  756. Db::name('user_dep')->insert(['user_id' => $ids, 'dep_id' => $v['depId']]);
  757. }
  758. if(!empty($v['rolesId'])){
  759. Db::name('user_roles')->insert(['user_id' => $ids, 'roles_id' => $v['rolesId']]);
  760. }
  761. $rolesParentId = Db::name('roles')
  762. ->where('id', $v['rolesId'])
  763. ->value('parent_id');
  764. if ($rolesParentId == 11) {
  765. Db::name('worker')
  766. ->where('user_id',$ids)
  767. ->delete();
  768. $a = [
  769. 'user_id' => $ids
  770. ];
  771. Db::name('worker')
  772. ->insertGetId($a);
  773. }
  774. echo "<font color=\"green\" style='margin-left:20px;font-size: 17px'>第".($k+1)."行,导入成功</font><br />";
  775. }else{
  776. echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,导入失败</font><br />";
  777. }
  778. }
  779. }else{
  780. exit('请上传文件');
  781. }
  782. }
  783. public function getRole(){
  784. $id = input('id','');
  785. $info = Db::name('roles')
  786. ->where('id',$id)
  787. ->find();
  788. $data = [
  789. 'show'=>($info['parent_id']==7 || $id==7)?1:0,
  790. ];
  791. $this->success('操作成功','',$data);
  792. }
  793. public function importexcel1(){
  794. set_time_limit(0);
  795. ini_set("memory_limit", -1);
  796. ob_flush();//清空缓存
  797. flush();//刷新缓存
  798. $orgId = $this->orgId;
  799. if(request()->file()) {
  800. $file = request()->file('file');
  801. // 移动到框架应用根目录/uploads/ 目录下
  802. $info = $file->validate([ 'size'=>50*1024*1024,'ext'=>'xls,xlsx' ])
  803. ->move(env('root_path') . 'public' . DIRECTORY_SEPARATOR . 'uploads'. DIRECTORY_SEPARATOR . 'temp');
  804. if(!$info){
  805. exit('文件上传失败');
  806. }
  807. $img = './uploads/temp/' . $info->getSaveName();
  808. $filePath = str_replace('\\', '/', $img);
  809. $excelArray = ExcelUtil::read($filePath,
  810. ['','real_name','account','dep','roles']
  811. );
  812. if(empty($excelArray)){
  813. exit('文件内容为空');
  814. }
  815. $succ = [];
  816. foreach ($excelArray as $k => $v) {
  817. if($k < 1){
  818. continue;
  819. }
  820. if(!$v['account']){
  821. echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,账号为空,未导入</font><br />";
  822. continue;
  823. }
  824. if(!$v['real_name']){
  825. echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,姓名为空,未导入</font><br />";
  826. continue;
  827. }
  828. if(!$v['dep']){
  829. echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,部门为空,未导入</font><br />";
  830. continue;
  831. }
  832. $depId = Db::name('dep')
  833. ->where('org_id',$this->orgId)
  834. ->where('title',$v['dep'])
  835. ->where('del',0)
  836. ->value('id');
  837. if($depId < 1){
  838. $depId = Db::name('dep')
  839. ->insertGetId([
  840. 'title'=>$v['dep'],
  841. 'org_id'=>$orgId,
  842. 'del'=>0,
  843. 'sort'=>50,
  844. 'create_time'=>date('Y-m-d H:i:s')
  845. ]);
  846. }
  847. if(!$v['roles']){
  848. echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,角色为空,未导入</font><br />";
  849. continue;
  850. }
  851. $rPid = 0; //父级角色id
  852. $gl = ['']; //管理
  853. $zh = ['项目经理','项目副经理','经理','文员','后勤主任','公共事务主管','主任','电梯主管','班长','后勤主任','后勤干事','项目副经理/文员','电梯导乘','送检工','保障处助理','主管','总务科科长','分管物业,工会主席']; //综合 pid=4
  854. $bj = ['保洁主管','保洁班长','保洁工']; // pid=8
  855. $wx = ['维修主管','维修工']; // pid=7
  856. $kh = ['']; // 客户 pid=3
  857. $ys = ['运送主管','运送班长','垃圾运送']; //运送 pid=6
  858. if(in_array($v['roles'],$zh)){
  859. $rPid = 4;
  860. }
  861. if(in_array($v['roles'],$bj)){
  862. $rPid = 8;
  863. }
  864. if(in_array($v['roles'],$wx)){
  865. $rPid = 7;
  866. }
  867. if(in_array($v['roles'],$ys)){
  868. $rPid = 6;
  869. }
  870. $rolesId = Db::name('roles')
  871. ->where('org_id',$this->orgId)
  872. ->where('name',$v['roles'])
  873. ->where('del',0)
  874. ->where('parent_id',$rPid)
  875. ->value('id');
  876. if(!$rolesId){
  877. $rolesId = Db::name('roles')
  878. ->insertGetId([
  879. 'name'=>$v['roles'],
  880. 'org_id'=>$orgId,
  881. 'parent_id'=>$rPid,
  882. 'type'=>1,
  883. 'del'=>0,
  884. 'sort'=>0,
  885. ]);
  886. }
  887. $rData = [
  888. 'account'=>$v['account'],
  889. 'real_name'=>$v['real_name'],
  890. 'mobile'=>$v['account'],
  891. 'ip'=>request()->ip(),
  892. 'password'=>password_hash('123456', PASSWORD_DEFAULT),
  893. 'create_time'=>getTime()
  894. ];
  895. $ret = Db::name('user')->insertGetId($rData);
  896. if($ret){
  897. $succ[] = $ret;
  898. $ids = $ret;
  899. Db::name('user_org')->insert(['user_id' => $ids, 'org_id' => $this->orgId]);
  900. if(!empty($depId)){
  901. Db::name('user_dep')->insert(['user_id' => $ids, 'dep_id' => $depId]);
  902. }
  903. if(!empty($rolesId)){
  904. Db::name('user_roles')->insert(['user_id' => $ids, 'roles_id' => $rolesId]);
  905. }
  906. // $rolesParentId = Db::name('roles')
  907. // ->where('id', $v['rolesId'])
  908. // ->value('parent_id');
  909. // if ($rolesParentId == 11) {
  910. // Db::name('worker')
  911. // ->where('user_id',$ids)
  912. // ->delete();
  913. // $a = [
  914. // 'user_id' => $ids
  915. // ];
  916. // Db::name('worker')
  917. // ->insertGetId($a);
  918. // }
  919. // echo "<font color=\"green\" style='margin-left:20px;font-size: 17px'>第".($k+1)."行,导入成功</font><br />";
  920. }else{
  921. echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,导入失败</font><br />";
  922. }
  923. }
  924. }else{
  925. exit('请上传文件');
  926. }
  927. }
  928. }