User.php 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115
  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. $mm = new \app\common\model\User();
  60. $lists = db('user')
  61. ->alias('u')
  62. ->join('user_org uo','u.id=uo.user_id')
  63. ->where($map)
  64. ->field('u.*,uo.org_id')
  65. ->limit($start,$length)
  66. ->order(['u.sort'=>'asc','u.id'=>'desc'])
  67. ->select();
  68. foreach ($lists as $k=>$v){
  69. $lists[$k]['dep_name'] = Db::name('user_dep')
  70. ->alias('ud')
  71. ->join('dep d','d.id=ud.dep_id')
  72. ->where('ud.user_id',$v['id'])
  73. ->where('d.org_id',$v['org_id'])
  74. ->value('d.title');
  75. $lists[$k]['roles_name'] = Db::name('user_roles')
  76. ->alias('ur')
  77. ->join('roles r','r.id=ur.roles_id')
  78. ->where('ur.user_id',$v['id'])
  79. ->where('r.org_id',$v['org_id'])
  80. ->value('r.name');
  81. $lists[$k]['sex_name'] = isset($mm->sex[$v['sex']])?$mm->sex[$v['sex']]:"";
  82. }
  83. //数据返回
  84. $totalCount = db('user')
  85. ->alias('u')
  86. ->join('user_org uo','u.id=uo.user_id')
  87. ->where($map)
  88. ->count();
  89. $totalPage = ceil($totalCount/$length);
  90. $result['page'] = $page;
  91. $result['total'] = $totalPage;
  92. $result['records'] = $totalCount;
  93. $result['rows'] = $lists;
  94. return json($result);
  95. }else{
  96. $deps = model('Dep')->getList();
  97. $this->assign('deps',$deps);
  98. $roles = model('Roles')->getList();
  99. $this->assign('roles',$roles);
  100. return $this->fetch();
  101. }
  102. }
  103. public function wxlist(){
  104. if(request()->isAjax()){
  105. //分页参数
  106. $length = input('rows',10,'intval'); //每页条数
  107. $page = input('page',1,'intval'); //第几页
  108. $start = ($page - 1) * $length; //分页开始位置
  109. //排序
  110. $sortRow = input('sidx','u.id','trim'); //排序列
  111. $sort = input('sord','desc','trim'); //排序方式
  112. $order = $sortRow.' '.$sort;
  113. $title = input('title','','trim');
  114. if($title){
  115. $map[] = ['u.account|u.real_name|u.mobile','like','%'.$title.'%'];
  116. }
  117. $enable = input('enable','','trim');
  118. if($enable != ''){
  119. $map[] = ['u.enable','=',$enable];
  120. }
  121. $depId = input('depId','','trim');
  122. if($depId != ''){
  123. $map[] = ['ud.dep_id','=',$depId];
  124. }
  125. $rolesId = input('rolesId','','trim');
  126. if($rolesId != ''){
  127. $map[] = ['ur.roles_id','=',$rolesId];
  128. }
  129. $map[] = ['u.del','=',0];
  130. $map[] = ['u.type','=',2];
  131. $map[] = ['uo.org_id','=',$this->orgId];
  132. $map= empty($map) ? true: $map;
  133. //数据查询
  134. $lists = db('user')
  135. ->alias('u')
  136. ->join('user_dep ud','u.id=ud.user_id')
  137. ->join('user_roles ur','u.id=ur.user_id','left')
  138. ->join('user_org uo','u.id=uo.user_id')
  139. ->field('u.*,ud.dep_id,ur.roles_id,uo.org_id')
  140. ->where($map)
  141. ->limit($start,$length)
  142. ->order(['u.sort'=>'asc','u.id'=>'desc'])
  143. ->select();
  144. foreach ($lists as $k=>$v){
  145. $lists[$k]['dep_name'] = Db::name('dep')->where('id',$v['dep_id'])->value('title');
  146. $lists[$k]['roles_name'] = Db::name('roles')->where('id',$v['roles_id'])->value('name');
  147. }
  148. //数据返回
  149. $totalCount = db('user')
  150. ->alias('u')
  151. ->join('user_dep ud','ud.user_id = u.id')
  152. ->join('user_roles ur','ur.user_id = u.id')
  153. ->join('user_org uo','uo.user_id = u.id')
  154. ->where($map)->count();
  155. $totalPage = ceil($totalCount/$length);
  156. $result['page'] = $page;
  157. $result['total'] = $totalPage;
  158. $result['records'] = $totalCount;
  159. $result['rows'] = $lists;
  160. return json($result);
  161. }else{
  162. $deps = model('Dep')->getList();
  163. $this->assign('deps',$deps);
  164. $roles = model('Roles')->getList();
  165. $this->assign('roles',$roles);
  166. return $this->fetch();
  167. }
  168. }
  169. /**
  170. * 新增/编辑
  171. */
  172. public function add($id=0){
  173. if(request()->isPost()){
  174. $res = model('User')->addUser();
  175. if($res){
  176. $this->success('操作成功',url('index'));
  177. }else{
  178. $this->error(model('User')->getError());
  179. }
  180. }else{
  181. if($id){
  182. $info = db('user')
  183. ->alias('u')
  184. ->join('user_dep ud','ud.user_id = u.id','left')
  185. ->join('user_roles ur','ur.user_id = u.id','left')
  186. ->field('u.*,ud.dep_id,ur.roles_id')
  187. ->where('u.id',$id)->find();
  188. $role = Db::name('roles')
  189. ->where('id',$info['roles_id'])
  190. ->find();
  191. $info['is_bx'] = ($role['parent_id']==7 || $info['roles_id']==7)?1:0;
  192. $this->assign('info',$info);
  193. }
  194. $deps = model('Dep')->getList();
  195. $this->assign('deps',$deps);
  196. $address = (new \app\common\model\Address())->getListByType(2);
  197. $roles = model('Roles')->getList();
  198. $this->assign('roles',$roles);
  199. $this->assign('address',$address);
  200. $orderType =model('OrderType')->list();
  201. $this->assign('orderType',$orderType);
  202. return $this->fetch();
  203. }
  204. }
  205. /**
  206. * 新增/编辑
  207. */
  208. public function wxadd($id=0){
  209. if(request()->isPost()){
  210. $res = model('User')->wxaddUser();
  211. if($res){
  212. $this->success('操作成功',url('index'));
  213. }else{
  214. $this->error(model('User')->getError());
  215. }
  216. }else{
  217. if($id){
  218. $info = db('user')
  219. ->alias('u')
  220. ->join('user_dep ud','ud.user_id = u.id')
  221. ->join('user_roles ur','ur.user_id = u.id','left')
  222. ->field('u.*,ud.dep_id,ur.roles_id')
  223. ->where('u.id',$id)->find();
  224. $this->assign('info',$info);
  225. }
  226. $deps = model('Dep')->getList();
  227. $this->assign('deps',$deps);
  228. // $address = (new \app\common\model\Address())->getListByType(2);
  229. $roles = model('Roles')->getList();
  230. $this->assign('roles',$roles);
  231. // $this->assign('address',$address);
  232. return $this->fetch();
  233. }
  234. }
  235. /**
  236. * 新增/编辑
  237. */
  238. public function wxzadd($id=0){
  239. if(request()->isPost()){
  240. $res = model('User')->addUser();
  241. if($res){
  242. $this->success('操作成功',url('index'));
  243. }else{
  244. $this->error(model('User')->getError());
  245. }
  246. }else{
  247. if($id){
  248. $info = db('user')
  249. ->alias('u')
  250. ->join('user_dep ud','ud.user_id = u.id')
  251. ->join('user_roles ur','ur.user_id = u.id','left')
  252. ->field('u.*,ud.dep_id,ur.roles_id')
  253. ->where('u.id',$id)->find();
  254. $this->assign('info',$info);
  255. }
  256. $deps = model('Dep')->getList();
  257. $this->assign('deps',$deps);
  258. $address = (new \app\common\model\Address())->getListByType(2);
  259. $roles = model('Roles')->getList();
  260. $this->assign('roles',$roles);
  261. $this->assign('address',$address);
  262. return $this->fetch();
  263. }
  264. }
  265. /**
  266. * 删除记录
  267. * @param int $id
  268. */
  269. public function del($id=0){
  270. if(!$id){
  271. $this->error('参数错误');
  272. }
  273. $res = db('user')->where('id',$id)->setField('del',1);
  274. if($res){
  275. $this->success('删除成功');
  276. }else{
  277. $this->error('删除失败');
  278. }
  279. }
  280. /**
  281. * 改变字段值
  282. * @param int $fv
  283. * @param string $fn
  284. * @param int $fv
  285. */
  286. public function changeField($id=0,$fn='',$fv=0){
  287. if(!$fn||!$id){
  288. $this->error('参数错误');
  289. }
  290. $res = db('user')->where('id',$id)->setField($fn,$fv);
  291. if($res){
  292. $this->success('操作成功');
  293. }else{
  294. $this->error('操作失败');
  295. }
  296. }
  297. public function company(){
  298. if(request()->isAjax()){
  299. //分页参数
  300. $length = input('rows',10,'intval'); //每页条数
  301. $page = input('page',1,'intval'); //第几页
  302. $start = ($page - 1) * $length; //分页开始位置
  303. //排序
  304. $sortRow = input('sidx','u.id','trim'); //排序列
  305. $sort = input('sord','desc','trim'); //排序方式
  306. $order = $sortRow.' '.$sort;
  307. $title = input('title','','trim');
  308. if($title){
  309. $map[] = ['u.account|u.real_name|u.mobile','like','%'.$title.'%'];
  310. }
  311. $enable = input('enable','','trim');
  312. if($enable != ''){
  313. $map[] = ['u.enable','=',$enable];
  314. }
  315. $rolesId = input('rolesId','','trim');
  316. if($rolesId != ''){
  317. $map[] = ['ur.roles_id','=',$rolesId];
  318. }
  319. $map[] = ['u.del','=',0];
  320. $map[] = ['u.type','=',1];
  321. $map= empty($map) ? true: $map;
  322. //数据查询
  323. $lists = db('user')
  324. ->alias('u')
  325. ->join('user_roles ur','ur.user_id = u.id')
  326. ->field('u.*,ur.roles_id')
  327. ->where($map)
  328. ->limit($start,$length)
  329. ->order($order)->select();
  330. foreach ($lists as $k=>$v){
  331. $lists[$k]['roles_name'] = Db::name('roles')->where('id',$v['roles_id'])->value('name');
  332. }
  333. //数据返回
  334. $totalCount = db('user')
  335. ->alias('u')
  336. ->join('user_roles ur','ur.user_id = u.id')
  337. ->where($map)->count();
  338. $totalPage = ceil($totalCount/$length);
  339. $result['page'] = $page;
  340. $result['total'] = $totalPage;
  341. $result['records'] = $totalCount;
  342. $result['rows'] = $lists;
  343. return json($result);
  344. }else{
  345. $roles = model('Roles')->getCommonList(2);
  346. $this->assign('roles',$roles);
  347. $type = Db::name('user')->where('id',$this->userId)->value('type');
  348. $this->assign('type',$type);
  349. return $this->fetch();
  350. }
  351. }
  352. /**
  353. * 新增/编辑
  354. */
  355. public function addcompany($id=0){
  356. if(request()->isPost()){
  357. $res = model('User')->addCompany();
  358. if($res){
  359. $this->success('操作成功',url('index'));
  360. }else{
  361. $this->error(model('User')->getError());
  362. }
  363. }else{
  364. if($id){
  365. $info = db('user')
  366. ->alias('u')
  367. ->join('user_roles ur','ur.user_id = u.id')
  368. ->field('u.*,ur.roles_id')
  369. ->where('u.id',$id)->find();
  370. if($info){
  371. $orgs = $info['orgs']?explode(',',$info['orgs']):[];
  372. $info['orgs'] = [];
  373. if($orgs){
  374. $ids = [];
  375. foreach ($orgs as $k=>$v){
  376. $t = $this->getAllLastId($v); //获取所有上级id 数组是倒序
  377. array_unshift($t,(int)$v); //把组织插入数组的开头
  378. $c = array_reverse($t); //数组反转
  379. $ids[] = $c;
  380. }
  381. $info['orgs'] = $ids;
  382. }
  383. }
  384. $this->assign('info',$info);
  385. }
  386. $orgs = model('Org')->getAllOrgs();
  387. $this->assign('orgs',$orgs);
  388. $tree = $this->orgTree();
  389. $this->assign('orgs',$tree);
  390. $roles = model('Roles')->getCommonList(2);
  391. $this->assign('roles',$roles);
  392. return $this->fetch();
  393. }
  394. }
  395. //获取所有上级id
  396. public function getAllLastId($id,$data=[]){
  397. $pids = DB::name('org')->where('id',$id)->value('parent_id');
  398. if($pids > 0){
  399. $data[] = $pids;
  400. $data = $this->getAllLastId($pids,$data);
  401. }
  402. if(count($data)>0){
  403. return $data;
  404. }else{
  405. return [];
  406. }
  407. }
  408. public function orgTree(){
  409. $lists = Db::name('org')
  410. ->field('id as value,name as label,parent_id')
  411. ->where('del',0)
  412. ->where('enable',1)
  413. ->order('sort asc,id asc')
  414. ->select();
  415. if (empty($lists)) {
  416. return array();
  417. }
  418. $tree = [];
  419. if($lists){
  420. $tree = Phptree::makeTree(($lists), array(
  421. 'primary_key'=>'value',
  422. 'parent_key'=>'parent_id',
  423. 'expanded' => true
  424. ));
  425. }
  426. return $tree;
  427. }
  428. public function modifyPass(){
  429. if(request()->isAjax()){
  430. $data = [
  431. 'oldpass' => input('oldpass','','trim'),
  432. 'pass' => input('pass','','trim'),
  433. 'pass_confirm' => input('pass_confirm','','trim'),
  434. 'id' => $this->userId
  435. ];
  436. $ret = model('User')->modifyPass($data);
  437. if(!$ret){
  438. $this->error(model('User')->getError());
  439. }
  440. // 清除登录信息,重新登录
  441. session('user_auth',null);
  442. session('user_auth_sign',null);
  443. $this->success('操作成功');
  444. }else{
  445. return $this->fetch();
  446. }
  447. }
  448. /*
  449. * 考勤设置*/
  450. public function set($id=0){
  451. if(request()->isPost()){
  452. $data = request()->post();
  453. if(!$data['kq_name']){
  454. $this->error('考勤姓名不能为空');
  455. }
  456. if(!isset($data['kq_img']) && empty($data['kq_img'])){
  457. $this->error('考勤图片不能为空');
  458. }
  459. $uid = $data['id'];
  460. unset($data['id']);
  461. db('user')->where('id',$uid)->update($data);
  462. $this->success('操作成功',url('index'));
  463. }else{
  464. if($id>0){
  465. $info = db('user')->where('id',$id)->find();
  466. $this->assign('info',$info);
  467. }
  468. }
  469. return $this->fetch();
  470. }
  471. /*
  472. * 重置设备编号*/
  473. public function reset($id=0){
  474. if($id < 1){
  475. $this->error('参数错误');
  476. }
  477. db('user')->where('id',$id)->update(['device_sn'=>'']);
  478. $this->success('重置成功',url('index'));
  479. }
  480. /**
  481. * 排序
  482. * @param int $id
  483. * @param int $sort
  484. */
  485. public function changeSort($id=0,$sort=0){
  486. if($id<0||$sort<0){
  487. $this->error('参数错误');
  488. }
  489. $res = db('user')->where('id',$id)->setField('sort',$sort);
  490. if($res){
  491. $this->success('操作成功');
  492. }else{
  493. $this->error('操作失败');
  494. }
  495. }
  496. public function batchSort(){
  497. $data = input('data','','trim');
  498. if(!$data){
  499. $this->error('参数错误');
  500. }
  501. $data = json_decode($data,true);
  502. if(!$data){
  503. $this->error('参数错误');
  504. }
  505. Db::startTrans();
  506. try{
  507. foreach ($data as $k=>$v){
  508. Db::name('user')->where('id',$v['id'])->setField('sort',$v['sort']);
  509. }
  510. Db::commit();
  511. }catch (Exception $e){
  512. Db::rollback();
  513. $this->error('操作失败');
  514. }
  515. $this->success('操作成功');
  516. }
  517. public function copyRoles(){
  518. if(request()->isPost()){
  519. $orgId = input('orgId',0);
  520. if($orgId < 1 ){
  521. $this->error('请选择复制的项目');
  522. }
  523. $rolesList = Db::name('roles')
  524. ->where('org_id',$orgId)
  525. ->where('enable',1)
  526. ->where('del',0)
  527. ->select();
  528. foreach ($rolesList as $k=>$v){
  529. $rolesInfo = Db::name('roles')
  530. ->where('org_id',$this->orgId)
  531. ->where('del',0)
  532. ->where('parent_id',$v['parent_id'])
  533. ->where('name',$v['name'])
  534. ->find();
  535. if(!$rolesInfo){
  536. $data = [
  537. 'name'=>$v['name'],
  538. 'content'=>$v['content'],
  539. 'org_id'=>$this->orgId,
  540. 'parent_id'=>$v['parent_id'],
  541. 'work_type_mode'=>$v['work_type_mode'],
  542. 'type'=>$v['type'],
  543. 'auths'=>$v['auths'],
  544. 'appauths'=>$v['appauths'],
  545. 'level'=>$v['level'],
  546. ];
  547. Db::name('roles')->insert($data);
  548. }
  549. }
  550. $this->success('操作成功',url('index'));
  551. }else{
  552. $orgList = Db::name('org')
  553. ->where('del',0)
  554. ->where('type',2)
  555. ->select();
  556. foreach ($orgList as $k=>$v){
  557. $orgList[$k]['title'] = $v['name'];
  558. }
  559. $this->assign('orgList',$orgList);
  560. return $this->fetch();
  561. }
  562. }
  563. public function workCard($id){
  564. $info = Db::name('user')->where('id',$id)->find();
  565. if (!$info) {
  566. exit('数据不存在');
  567. }
  568. $info['dep'] = Db::name('user_dep')
  569. ->alias('ud')
  570. ->join('dep d','d.id = ud.dep_id')
  571. ->where('ud.user_id',$info['id'])
  572. ->where('d.del',0)
  573. ->value('d.title');
  574. $orgName = Db::name('org')->where('id',$this->orgId)->value('name');
  575. $code = get_qrcode_str('work_card', $id);
  576. $this->assign('code',$code);
  577. $this->assign('info',$info);
  578. $this->assign('orgName',$orgName);
  579. return $this->fetch();
  580. }
  581. public function import(){
  582. return $this->fetch();
  583. }
  584. /**
  585. * 下载点模板
  586. */
  587. public function downloadtem(){
  588. set_time_limit(0);
  589. ini_set("memory_limit","512M");
  590. $header = [
  591. ['title' => '账号', 'name' => 'account','width'=>'30'],
  592. ['title' => '姓名', 'name' => 'real_name','width'=>'30'],
  593. ['title' => '性别', 'name' => 'sex_name','width'=>'30'],
  594. ['title' => '手机号', 'name' => 'mobile','width'=>'30'],
  595. ['title' => '部门ID(默认0)', 'name' => 'depId','width'=>'30'],
  596. ['title' => '角色ID(默认0)', 'name' => 'rolesId','width'=>'30'],
  597. ];
  598. $filename = '用户导入';
  599. ExcelUtil::export($filename,$header,[]);
  600. }
  601. /**
  602. * 导入
  603. */
  604. public function importexcel_bak(){
  605. set_time_limit(0);
  606. ini_set("memory_limit", -1);
  607. ob_flush();//清空缓存
  608. flush();//刷新缓存
  609. $orgId = $this->orgId;
  610. if(request()->file()) {
  611. $file = request()->file('file');
  612. // 移动到框架应用根目录/uploads/ 目录下
  613. $info = $file->validate([ 'size'=>50*1024*1024,'ext'=>'xls,xlsx' ])
  614. ->move(env('root_path') . 'public' . DIRECTORY_SEPARATOR . 'uploads'. DIRECTORY_SEPARATOR . 'temp');
  615. if(!$info){
  616. exit('文件上传失败');
  617. }
  618. $img = './uploads/temp/' . $info->getSaveName();
  619. $filePath = str_replace('\\', '/', $img);
  620. $excelArray = ExcelUtil::read($filePath,
  621. ['id','real_name','mobile','dep','roles']
  622. );
  623. unset($excelArray[0]);
  624. if(empty($excelArray)){
  625. exit('文件内容为空');
  626. }
  627. $succ = [];
  628. foreach ($excelArray as $k => $v) {
  629. $v['account'] = $v['mobile'];
  630. if(!$v['real_name']){
  631. echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,姓名为空,未导入</font><br />";
  632. continue;
  633. }
  634. if(!$v['mobile']){
  635. echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,手机号为空,未导入</font><br />";
  636. continue;
  637. }
  638. $v['id'] = 0;
  639. $result = validate('User')->check($v, [], 'bachAdd');
  640. if (true !== $result) {
  641. echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,".validate('User')->getError()."</font><br />";
  642. continue;
  643. }
  644. $v['depId'] = 130;
  645. $rolesId = 145;
  646. // $rolesId = Db::name('roles')
  647. // ->where('name', $v['roles'])
  648. // ->where('org_id', $this->orgId)
  649. // ->value('id');
  650. // if(empty($rolesId)){
  651. // echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,角色不存在</font><br />";
  652. // continue;
  653. // }
  654. $rData = [
  655. 'account'=>$v['mobile'],
  656. 'real_name'=>$v['real_name'],
  657. 'mobile'=>$v['mobile'],
  658. 'ip'=>request()->ip(),
  659. 'password'=>password_hash('Aa123456', PASSWORD_DEFAULT),
  660. 'create_time'=>getTime()
  661. ];
  662. $v['rolesId'] = $rolesId;
  663. $ret = Db::name('user')->insertGetId($rData);
  664. if($ret){
  665. $succ[] = $ret;
  666. $ids = $ret;
  667. Db::name('user_org')->insert(['user_id' => $ids, 'org_id' => $this->orgId]);
  668. if(!empty($v['depId'])){
  669. Db::name('user_dep')->insert(['user_id' => $ids, 'dep_id' => $v['depId']]);
  670. }
  671. if(!empty($v['rolesId'])){
  672. Db::name('user_roles')->insert(['user_id' => $ids, 'roles_id' => $v['rolesId']]);
  673. }
  674. echo "<font color=\"green\" style='margin-left:20px;font-size: 17px'>第".($k+1)."行,导入成功</font><br />";
  675. }else{
  676. echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,导入失败</font><br />";
  677. }
  678. }
  679. }else{
  680. exit('请上传文件');
  681. }
  682. }
  683. public function importexcel(){
  684. set_time_limit(0);
  685. ini_set("memory_limit", -1);
  686. ob_flush();//清空缓存
  687. flush();//刷新缓存
  688. $orgId = $this->orgId;
  689. if(request()->file()) {
  690. $file = request()->file('file');
  691. // 移动到框架应用根目录/uploads/ 目录下
  692. $info = $file->validate([ 'size'=>50*1024*1024,'ext'=>'xls,xlsx' ])
  693. ->move(env('root_path') . 'public' . DIRECTORY_SEPARATOR . 'uploads'. DIRECTORY_SEPARATOR . 'temp');
  694. if(!$info){
  695. exit('文件上传失败');
  696. }
  697. $img = './uploads/temp/' . $info->getSaveName();
  698. $filePath = str_replace('\\', '/', $img);
  699. $excelArray = ExcelUtil::read($filePath,
  700. ['account','real_name','sex_name','mobile','depId','rolesId']
  701. );
  702. if(empty($excelArray)){
  703. exit('文件内容为空');
  704. }
  705. $succ = [];
  706. $m = new \app\common\model\User();
  707. foreach ($excelArray as $k => $v) {
  708. if(!$v['account']){
  709. echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,账号为空,未导入</font><br />";
  710. continue;
  711. }
  712. if(!$v['real_name']){
  713. echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,姓名为空,未导入</font><br />";
  714. continue;
  715. }
  716. if(!$v['mobile']){
  717. echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,手机号为空,未导入</font><br />";
  718. continue;
  719. }
  720. $v['id'] = 0;
  721. $result = validate('User')->check($v, [], 'bachAdd');
  722. if (true !== $result) {
  723. echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,".validate('User')->getError()."</font><br />";
  724. continue;
  725. }
  726. if(!empty($v['depId'])){
  727. $checkDep = Db::name('dep')
  728. ->where('org_id',$this->orgId)
  729. ->where('id',$v['depId'])
  730. ->where('del',0)
  731. ->find();
  732. if(!$checkDep){
  733. echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,部门不存在</font><br />";
  734. continue;
  735. }
  736. }
  737. if(!empty($v['rolesId'])){
  738. $checkRoles = Db::name('roles')
  739. ->where('org_id',$this->orgId)
  740. ->where('id',$v['rolesId'])
  741. ->where('del',0)
  742. ->where('parent_id','>',0)
  743. ->find();
  744. if(!$checkRoles){
  745. echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,角色不存在</font><br />";
  746. continue;
  747. }
  748. }
  749. $s = array_flip($m->sex);
  750. $sex = isset($s[$v['sex_name']])?$s[$v['sex_name']]:0;
  751. $rData = [
  752. 'account'=>$v['account'],
  753. 'sex'=>$sex,
  754. 'real_name'=>$v['real_name'],
  755. 'mobile'=>$v['mobile'],
  756. 'ip'=>request()->ip(),
  757. 'password'=>password_hash('Aa123456', PASSWORD_DEFAULT),
  758. 'create_time'=>getTime()
  759. ];
  760. $ret = Db::name('user')->insertGetId($rData);
  761. if($ret){
  762. $succ[] = $ret;
  763. $ids = $ret;
  764. Db::name('user_org')->insert(['user_id' => $ids, 'org_id' => $this->orgId]);
  765. if(!empty($v['depId'])){
  766. Db::name('user_dep')->insert(['user_id' => $ids, 'dep_id' => $v['depId']]);
  767. }
  768. if(!empty($v['rolesId'])){
  769. Db::name('user_roles')->insert(['user_id' => $ids, 'roles_id' => $v['rolesId']]);
  770. }
  771. $rolesParentId = Db::name('roles')
  772. ->where('id', $v['rolesId'])
  773. ->value('parent_id');
  774. if ($rolesParentId == 11) {
  775. Db::name('worker')
  776. ->where('user_id',$ids)
  777. ->delete();
  778. $a = [
  779. 'user_id' => $ids
  780. ];
  781. Db::name('worker')
  782. ->insertGetId($a);
  783. }
  784. echo "<font color=\"green\" style='margin-left:20px;font-size: 17px'>第".($k+1)."行,导入成功</font><br />";
  785. }else{
  786. echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,导入失败</font><br />";
  787. }
  788. }
  789. }else{
  790. exit('请上传文件');
  791. }
  792. }
  793. public function getRole(){
  794. $id = input('id','');
  795. $info = Db::name('roles')
  796. ->where('id',$id)
  797. ->find();
  798. $data = [
  799. 'show'=>($info['parent_id']==7 || $id==7)?1:0,
  800. ];
  801. $this->success('操作成功','',$data);
  802. }
  803. public function importexcel1(){
  804. set_time_limit(0);
  805. ini_set("memory_limit", -1);
  806. ob_flush();//清空缓存
  807. flush();//刷新缓存
  808. $orgId = $this->orgId;
  809. if(request()->file()) {
  810. $file = request()->file('file');
  811. // 移动到框架应用根目录/uploads/ 目录下
  812. $info = $file->validate([ 'size'=>50*1024*1024,'ext'=>'xls,xlsx' ])
  813. ->move(env('root_path') . 'public' . DIRECTORY_SEPARATOR . 'uploads'. DIRECTORY_SEPARATOR . 'temp');
  814. if(!$info){
  815. exit('文件上传失败');
  816. }
  817. $img = './uploads/temp/' . $info->getSaveName();
  818. $filePath = str_replace('\\', '/', $img);
  819. $excelArray = ExcelUtil::read($filePath,
  820. ['','real_name','account','dep','roles']
  821. );
  822. if(empty($excelArray)){
  823. exit('文件内容为空');
  824. }
  825. $succ = [];
  826. foreach ($excelArray as $k => $v) {
  827. if($k < 1){
  828. continue;
  829. }
  830. if(!$v['account']){
  831. echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,账号为空,未导入</font><br />";
  832. continue;
  833. }
  834. if(!$v['real_name']){
  835. echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,姓名为空,未导入</font><br />";
  836. continue;
  837. }
  838. if(!$v['dep']){
  839. echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,部门为空,未导入</font><br />";
  840. continue;
  841. }
  842. $depId = Db::name('dep')
  843. ->where('org_id',$this->orgId)
  844. ->where('title',$v['dep'])
  845. ->where('del',0)
  846. ->value('id');
  847. if($depId < 1){
  848. $depId = Db::name('dep')
  849. ->insertGetId([
  850. 'title'=>$v['dep'],
  851. 'org_id'=>$orgId,
  852. 'del'=>0,
  853. 'sort'=>50,
  854. 'create_time'=>date('Y-m-d H:i:s')
  855. ]);
  856. }
  857. if(!$v['roles']){
  858. echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,角色为空,未导入</font><br />";
  859. continue;
  860. }
  861. $rPid = 0; //父级角色id
  862. $gl = ['']; //管理
  863. $zh = ['项目经理','项目副经理','经理','文员','公共事务主管','主任','电梯主管','班长','后勤主任','后勤干事','后勤主管','项目副经理/文员','电梯导乘','送检工','保障处助理','主管','总务科科长','分管物业,工会主席','科长','副科长','院长','客服','院方后勤主任','服务中心副经理','领班','项目主管','环境服务主管','服务中心经理','配送主管','公共事务主管兼文员','司梯主管','电梯礼仪','项目总经理','综合部主管','仓管','医辅部主管','护理主任']; //综合 pid=4
  864. $bj = ['保洁主管','保洁班长','保洁工','保洁员','外围保洁员','医废垃圾清运员','生活垃圾清运员','医废站管理员','专项保洁员','五院保洁']; // pid=8
  865. $wx = ['维修主管','维修工','院方后勤维修工','电工','水暖工','经理,维修工','工程维修员']; // pid=7
  866. $kh = ['']; // 客户 pid=3
  867. $ys = ['运送主管','运送班长','垃圾运送','配送','夜班配送']; //运送 pid=6
  868. if(in_array($v['roles'],$zh)){
  869. $rPid = 4;
  870. }
  871. if(in_array($v['roles'],$bj)){
  872. $rPid = 8;
  873. }
  874. if(in_array($v['roles'],$wx)){
  875. $rPid = 7;
  876. }
  877. if(in_array($v['roles'],$ys)){
  878. $rPid = 6;
  879. }
  880. // if($v['dep'] == '保洁部' && !in_array($v['roles'],$zh)){
  881. // $rPid = 8;
  882. // $v['roles'] = '保洁';
  883. // }
  884. // if($v['dep'] == '运送部' && !in_array($v['roles'],$ys)){
  885. // $rPid = 6;
  886. // $v['roles'] = '运送';
  887. // }
  888. //
  889. // if($v['dep'] == '导梯部' && !in_array($v['roles'],$zh)){
  890. // $rPid = 4;
  891. // $v['roles'] = '综合';
  892. // }
  893. $rolesId = Db::name('roles')
  894. ->where('org_id',$this->orgId)
  895. ->where('name',$v['roles'])
  896. ->where('del',0)
  897. ->where('parent_id',$rPid)
  898. ->value('id');
  899. if(!$rolesId){
  900. $rolesId = Db::name('roles')
  901. ->insertGetId([
  902. 'name'=>$v['roles'],
  903. 'org_id'=>$orgId,
  904. 'parent_id'=>$rPid,
  905. 'type'=>1,
  906. 'del'=>0,
  907. 'sort'=>0,
  908. ]);
  909. }
  910. $mobile = strlen($v['account']) == 11 ?$v['account']:'';
  911. $rData = [
  912. 'account'=>$v['account'],
  913. 'real_name'=>$v['real_name'],
  914. 'mobile'=>$mobile,
  915. 'ip'=>request()->ip(),
  916. 'password'=>password_hash('123456', PASSWORD_DEFAULT),
  917. 'create_time'=>getTime()
  918. ];
  919. $ret = Db::name('user')->insertGetId($rData);
  920. if($ret){
  921. $succ[] = $ret;
  922. $ids = $ret;
  923. Db::name('user_org')->insert(['user_id' => $ids, 'org_id' => $this->orgId]);
  924. if(!empty($depId)){
  925. Db::name('user_dep')->insert(['user_id' => $ids, 'dep_id' => $depId]);
  926. }
  927. if(!empty($rolesId)){
  928. Db::name('user_roles')->insert(['user_id' => $ids, 'roles_id' => $rolesId]);
  929. }
  930. // $rolesParentId = Db::name('roles')
  931. // ->where('id', $v['rolesId'])
  932. // ->value('parent_id');
  933. // if ($rolesParentId == 11) {
  934. // Db::name('worker')
  935. // ->where('user_id',$ids)
  936. // ->delete();
  937. // $a = [
  938. // 'user_id' => $ids
  939. // ];
  940. // Db::name('worker')
  941. // ->insertGetId($a);
  942. // }
  943. // echo "<font color=\"green\" style='margin-left:20px;font-size: 17px'>第".($k+1)."行,导入成功</font><br />";
  944. }else{
  945. echo "<font color=\"red\" style='margin-left: 20px;font-size: 17px'>第".($k+1)."行,导入失败</font><br />";
  946. }
  947. }
  948. }else{
  949. exit('请上传文件');
  950. }
  951. }
  952. //464
  953. public function delDepUser(){
  954. $userIds = Db::name('user')
  955. ->alias('u')
  956. ->join('user_dep ud','ud.user_id=u.id')
  957. ->where('ud.dep_id',464)
  958. ->where('u.del',0)
  959. ->where('u.enable',1)
  960. ->column('u.id');
  961. $ret = Db::name('user')->where('id','in',$userIds)->setField('del',1);
  962. echo $ret;
  963. }
  964. public function delOrgUser(){
  965. $userIds = Db::name('user')
  966. ->alias('u')
  967. ->join('user_org uo','uo.user_id=u.id')
  968. ->where('uo.org_id',85)
  969. ->where('u.del',0)
  970. ->column('u.id');
  971. $ret = Db::name('user')->where('id','in',$userIds)->setField('del',1);
  972. echo $ret;
  973. }
  974. public function saveUserPassWord(){
  975. $p = 'Md123456';
  976. $pw = password_hash($p, PASSWORD_DEFAULT);
  977. // $user = Db::name('user')
  978. // ->where('del',0)
  979. // ->update(['password'=>$pw]);
  980. //
  981. // echo $user;
  982. }
  983. }