User.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\admin\controller;
  4. use app\admin\BaseController;use think\App;use think\Exception;use think\facade\Db;use think\facade\Validate;
  5. class User extends BaseController{
  6. public function __construct(App $app) {parent::__construct($app);}
  7. /**
  8. * 设置供应商信息
  9. * @return \think\response\Json|void
  10. */
  11. public function EditUserCompany(){
  12. $post = $this->request->only(["token"=>"","nickname"=>"","id"=>"","mobile"=>"","companyArr"=>[]],"post");
  13. $valide = $validate=Validate::rule([
  14. 'nickname|真实姓名' => 'require|max:255|min:2',
  15. 'id|账户ID' => 'require|number|gt:0',
  16. 'mobile|手机号' => 'require|number|mobile',
  17. 'companyArr|业务公司' => 'require|array',
  18. ]);
  19. if($valide->check($post)==false) return error_show(1004,$valide->getError());
  20. $isSuper = Db::name("user_role")->alias("a")
  21. ->leftJoin("role b","a.roleid=b.id")
  22. ->where(["a.uid"=>$post['id'],"a.is_del"=>0,"b.level"=>1])
  23. ->findOrEmpty();
  24. if(!empty($isSuper) && !empty($post['companyArr']))return error_show(1002,"超管账户不允许设置关联业务公司");
  25. Db::startTrans();
  26. try{
  27. $saveinfo =[
  28. "id"=>$post['id'],
  29. "nickname"=>$post['nickname'],
  30. "mobile"=>$post['mobile'],
  31. "token"=>$post['token']
  32. ];
  33. $saev= resetinfo($saveinfo);
  34. if(!isset($saev['code'])||$saev['code']!=0){
  35. throw new Exception($saev['message']);
  36. }
  37. if(!empty($post['companyArr'])){
  38. $CompArr=[];
  39. $is_delArr= array_column($post['companyArr'],"is_del");
  40. foreach ($post['companyArr'] as &$item){
  41. $company =Db::name("supplier_info")->where(["code"=>$item['companyCode']])->find();
  42. $item['company_type'] = $company["type"]==3?2:1;
  43. $res = Db::name("user_role")->where(["uid"=>$post['id'],"is_del"=>0,"companyNo"=>$item['companyCode']])->findOrEmpty();
  44. if(!empty($res)){
  45. if($item['is_del']!=$res['is_del'] || $item['is_main']!=$res['is_main'])
  46. {
  47. $CompArr[]=$item;
  48. if($item['is_del']==1){
  49. if($item['is_main']==1 && in_array(0,$is_delArr)) throw new Exception("默认企业不可删除");
  50. $re = Db::name("user_role")->where(["uid"=>$post['id'],"is_del"=>0,"companyNo"=>$item['companyCode']])
  51. ->update(["is_del"=>1,"is_main"=>0,"updatetime"=>date("Y-m-d H:i:s")]);
  52. }else{
  53. $re = Db::name("user_role")->where(["uid"=>$post['id'],"is_del"=>0,"companyNo"=>$item['companyCode']])
  54. ->update(["nickname"=>$post['nickname'],"is_main"=>$item['is_main'],"updatetime"=>date("Y-m-d H:i:s")]);
  55. }
  56. if($re==false)throw new Exception("关联企业保存失败");
  57. }
  58. }else{
  59. $CompArr[]=$item;
  60. $data=[
  61. "uid"=>$post['id'],
  62. "nickname"=>$post['nickname'],
  63. "roleid"=>0,
  64. "companyNo"=>$item['companyCode'],
  65. "company_type"=>$item['company_type'],
  66. "status"=>1,
  67. "is_main"=>$item['is_main']
  68. ];
  69. $instr=Db::name("user_role")->insert($data);
  70. if($instr==false) throw new Exception("关联企业保存失败");
  71. }
  72. }
  73. if(!empty($CompArr)){
  74. $companyinfo = ["id"=>$post['id'],"company"=>$CompArr,"token"=>$post['token']];
  75. $companyset =setUserCompany($companyinfo);
  76. if(!isset($companyset['code'])||$companyset['code']!=0){
  77. throw new Exception($companyset['message']);
  78. }
  79. }
  80. }
  81. Db::commit();
  82. return app_show(0,"设置成功");
  83. }catch (Exception $e){
  84. Db::rollback();
  85. return error_show(1005,$e->getMessage());
  86. }
  87. }
  88. /**
  89. * 用户列表
  90. */
  91. public function userList(){
  92. $post=$this->request->only(["token"=>"","nickname"=>"","relaComNo"=>"","islevel"=>"","username"=>"","companyNo"=>"","status"=>"","page"=>1,"size"=>10],"post");
  93. if($this->level==1){
  94. $companyNo = $post['companyNo']??"";
  95. }else{
  96. $companyNo = $post['relaComNo']??"";
  97. $post['companyNo'] = $post['relaComNo']??"";
  98. }
  99. $where=[["a.is_del","=",0]];
  100. unset($post['relaComNo']);
  101. if($post['companyNo']!=""){
  102. $where[]=["a.companyNo","=",$companyNo];
  103. }
  104. if($post['status']!==''){
  105. $uid =Db::name("user_role")->alias("a")->where(["status"=>1,"is_del"=>0])->where($where)->column("uid");
  106. if($post['status']==1){
  107. $where[]=["a.status","=",1];
  108. $post['uid']= $uid;
  109. }else{
  110. $post['nuid']=$uid;
  111. }
  112. unset($post['status']);
  113. }
  114. if($post["islevel"]!=""){
  115. $uid =Db::name("user_role")->alias("a")
  116. ->leftJoin("role b","a.roleid=b.id")->where([["b.level","=",1],["a.is_del","=",0]])->where($where)->column
  117. ("uid");
  118. if($post['islevel']==1){
  119. $post['uid']= $uid;
  120. }else{
  121. $post['nuid']=$uid;
  122. }
  123. unset($post["islevel"]);
  124. }
  125. // var_dump($post);
  126. $userinfo=GetUserList($post);
  127. if(empty($userinfo)||$userinfo['code']!=0){
  128. return app_show($userinfo['code'],$userinfo['msg']);
  129. }
  130. $data = $userinfo['data']['list'];
  131. $list=[];
  132. foreach ($data as $value){
  133. $roleArr = Db::name("user_role")->alias("a")
  134. ->leftJoin("role b","a.roleid=b.id")
  135. ->where(["uid"=>$value['id'],"a.companyNo"=>$value['companyCode'],"is_del"=>0])
  136. ->field("role_name,roleid,a.status,b.level")->findOrEmpty();
  137. $value['roleid']=$roleArr['roleid']??"";
  138. $value['status']=$value['status']==1?($roleArr['status']??0) :0;
  139. $value["role_name"] =$roleArr['role_name']??"";
  140. $value["role_level"] =$roleArr['level']??"";
  141. $list[]=$value;
  142. }
  143. return app_show(0,"获取成功",["list"=>$list,"count"=>$userinfo['data']["count"]]);
  144. }
  145. /**
  146. * 用户列表
  147. */
  148. public function userSingleList(){
  149. $post=$this->request->only(["token"=>"","nickname"=>"","relaComNo"=>"","islevel"=>"","username"=>"","companyNo"=>"","status"=>"","page"=>1,"size"=>10],"post");
  150. if($this->level==1){
  151. $companyNo = $post['companyNo']??"";
  152. }else{
  153. $companyNo = $post['relaComNo']??"";
  154. $post['companyNo'] = $post['relaComNo']??"";
  155. }
  156. $where=[["a.is_del","=",0]];
  157. unset($post['relaComNo']);
  158. if($post['companyNo']!=""){
  159. $where[]=["a.companyNo","=",$companyNo];
  160. }
  161. if($post["islevel"]!=""){
  162. $uid =Db::name("user_role")->alias("a")
  163. ->leftJoin("role b","a.roleid=b.id")->where([["b.level","=",1],["a.is_del","=",0]])->where($where)->column
  164. ("uid");
  165. if($post['islevel']==1){
  166. $post['uid']= $uid;
  167. }else{
  168. $post['nuid']=$uid;
  169. }
  170. unset($post["islevel"]);
  171. }
  172. $userinfo=GetList($post);
  173. if(empty($userinfo)||$userinfo['code']!=0){
  174. return app_show($userinfo['code'],$userinfo['msg']);
  175. }
  176. $data = $userinfo['data']['list'];
  177. $list=[];
  178. foreach ($data as $value){
  179. if(isset($value['company_relaton'])&& !empty($value['company_relaton'])){
  180. foreach ($value['company_relaton'] as &$item){
  181. $roleArr = Db::name("user_role")->alias("a")
  182. ->leftJoin("role b","a.roleid=b.id")
  183. ->where(["uid"=>$value['id'],"a.companyNo"=>$item['companyCode'],"is_del"=>0])
  184. ->field("role_name,roleid,a.status,b.level")->findOrEmpty();
  185. $item['roleid']=$roleArr['roleid']??"";
  186. $item['status']=$roleArr['status']??0;
  187. $item["role_name"] =$roleArr['role_name']??"";
  188. $item["role_level"] =$roleArr['level']??"";
  189. }
  190. }
  191. $value['roleid']=$roleArr['roleid']??"";
  192. $value['status']=$value['status']??0;
  193. $value["role_name"] =$roleArr['role_name']??"";
  194. $value["role_level"] =$roleArr['level']??"";
  195. $list[]=$value;
  196. }
  197. return app_show(0,"获取成功",["list"=>$list,"count"=>$userinfo['data']["count"]]);
  198. }
  199. /**获取当前访问用户的信息
  200. * @param token
  201. * @return \think\response\Json
  202. * @throws \think\exception\DbException
  203. */
  204. public function userInfo(){
  205. $post =$this->request->only(["token"=>"","relaComNo"=>""],"post","trim");
  206. $userinfo=GetUserInfo($post['token']);
  207. if(empty($userinfo)||$userinfo['code']!=0){
  208. return app_show(1002,"员工信息不存在");
  209. }
  210. $data = $userinfo['data'];
  211. $where=[["uid","=", $data['id']],["is_del","=",0]];
  212. if($post['relaComNo']!=""){
  213. $where[]=["companyNo","=",$post['relaComNo']];
  214. }else{
  215. $where[]=["is_main","=",1];
  216. }
  217. $role =Db::name("user_role")->where($where)->findOrEmpty();
  218. if(!empty($role)){
  219. $company_name = Db::name("supplier_info")->where(["code"=>$role['companyNo']])->value("name","");
  220. $roleinfo = Db::name("role")->where(["id"=>$role['roleid']])->field("role_name,level")->find();
  221. }else{
  222. return error_show(1004,'未开通登录权限');
  223. }
  224. if(!empty($data['company_relaton'])){
  225. $roleArr = Db::name("user_role")->alias("a")
  226. ->leftJoin("role b","a.roleid=b.id")
  227. ->where(["a.is_del"=>0,"a.uid"=>$data['id']])
  228. ->column("role_name,roleid,a.status,b.level","a.companyNo");
  229. foreach ($data['company_relaton'] as &$item){
  230. $item['roleid'] =isset($roleArr[$item['companyCode']])?$roleArr[$item['companyCode']]['roleid']:"";
  231. $item['role_name'] =isset($roleArr[$item['companyCode']])?$roleArr[$item['companyCode']]['role_name']:"";
  232. $item['role_status'] =isset($roleArr[$item['companyCode']])?$roleArr[$item['companyCode']]['status']:"";
  233. $item['role_level'] =isset($roleArr[$item['companyCode']])?$roleArr[$item['companyCode']]['level']:"";
  234. }
  235. }
  236. $data['role_name']=$roleinfo['role_name']??'';
  237. $data['company_name']=$company_name??'';
  238. $data['role_level']=$roleinfo['level']??'';
  239. $data['role_id']=$role['roleid']??'';
  240. $data['companyNo']=$role['companyNo']??'';
  241. $data['company_type']=$role['company_type']??'';
  242. return app_show(0,"获取成功",$data);
  243. }
  244. /**@param id int 账户id
  245. /**@param status int 状态 0/1
  246. * @return \think\response\Json|void
  247. */
  248. public function setStatus(){
  249. $post =$this->request->only(["id"=>"","status"=>"","token"=>""],"post");
  250. $valide =Validate::rule([
  251. 'id|账户ID' => 'require|number|gt:0',
  252. 'status|状态' => 'require|number|in:0,1',
  253. ]);
  254. if($valide->check($post)==false) return error_show(1004,$valide->getError());
  255. $message = $post['status']==1?"启用":"禁用";
  256. if($post['id']==$this->uid && $post['status']==0){
  257. return error_show(1004,'不可禁用当前账户');
  258. }
  259. Db::startTrans();
  260. try{
  261. $account = Db::name("user_role")->where(["uid"=>$post['id'],"is_del"=>0])->update(["status"=>$post['status'],"updatetime"=>date("Y-m-d H:i:s")]);
  262. if($account==false){
  263. throw new Exception("账户{$message}失败,账户未在系统中开通权限");
  264. }
  265. $condition=["id"=>$post['id'],"status"=>$post['status'],"token"=>$post['token']];
  266. $ex=setStatus($condition);
  267. if(!isset($ex['code'])||$ex['code']!=0){
  268. throw new Exception($ex['message']);
  269. }
  270. Db::commit();
  271. return app_show(0,"{$message}成功");
  272. }catch (\Exception $e){
  273. Db::rollback();
  274. return error_show(1006,$e->getMessage());
  275. }
  276. }
  277. public function setCompanyStatus(){
  278. $post =$this->request->only(["id"=>"","status"=>"","relaComNo"=>"","companyNo"=>"","token"=>""],"post");
  279. $valide =Validate::rule([
  280. 'id|账户ID' => 'require|number|gt:0',
  281. 'status|状态' => 'require|number|in:0,1',
  282. 'relaComNo|业务公司' => 'max:255',
  283. 'companyNo|公司编号' => 'require|max:255',
  284. ]);
  285. if($valide->check($post)==false) return error_show(1004,$valide->getError());
  286. $message = $post['status']==1?"启用":"禁用";
  287. if($post['id']==$this->uid &&$post['companyNo']==$post['relaComNo']&& $post['status']==0){
  288. return error_show(1004,'不可禁用当前账户');
  289. }
  290. Db::startTrans();
  291. try{
  292. $userinfo =Db::name("user_role")->where(["uid"=>$post['id'],"companyNo"=>$post['companyNo'],"is_del"=>0])->findOrEmpty();
  293. if(!empty($userinfo)){
  294. // if($userinfo['is_main']==1) throw new Exception("默认账户不可禁用!");
  295. $userinfo['status']=$post['status'];
  296. $userinfo['updatetime']=date("Y-m-d H:i:s");
  297. $sa=Db::name("user_role")->save($userinfo);
  298. if($sa==false) throw new Exception("状态更新失败!");
  299. }
  300. $condition=["account_id"=>$post['id'],"companyCode"=>$post['companyNo'],"status"=>$post['status'],"token"=>$post['token']];
  301. $effect=setCompanyStatus($condition);
  302. if(!isset($effect['code'])||$effect['code']!=0)throw new Exception($effect['message']);
  303. Db::commit();
  304. return app_show(0,"{$message}成功");
  305. }catch (\Exception $e){
  306. Db::rollback();
  307. return error_show(1004,$e->getMessage());
  308. }
  309. }
  310. //获取所有用户的信息
  311. public function userAll(){
  312. $post=$this->request->only(['relaComNo'=>"","companyNo"=>"","nickname"=>"","roleid"=>"","status"=>""],"post","trim");
  313. $condition = [["is_del","=",0]];
  314. $rolecond=" level in (1,2,3)";
  315. if($post['relaComNo']!=""){
  316. $condition[]=["companyNo","=",$post['relaComNo']];
  317. $rolecond = "level=1 or (level in (2,3) and companyNo ='{$post['relaComNo']}' )";
  318. }
  319. if($post['companyNo']!=""){
  320. $condition[]=["companyNo","=",$post['companyNo']];
  321. $rolecond = "level=1 or (level in (2,3) and companyNo ='{$post['companyNo']}' )";
  322. }
  323. if($post['roleid']!=""){
  324. $condition[]=["roleid","=",$post['roleid']];
  325. $rolecond = "id={$post['roleid']}";
  326. }
  327. if($post['status']!="")$condition[]=["status","=",$post['status']];
  328. if($post['nickname']!="")$condition[]=["nickname","like","%{$post['nickname']}%"];
  329. $list=Db::name("user_role")->where($condition)->select()->toArray();
  330. $role = Db::name("role")->whereRaw($rolecond)->column("role_name,level","id");
  331. foreach ($list as &$value){
  332. $value['role_name']= $role[$value['roleid']]['role_name']??"";
  333. $value['role_level']= $role[$value['roleid']]['level']??"";
  334. $value['companyName']= Db::name("supplier_info")->where(["code"=>$value['companyNo']])->value("name",'');
  335. }
  336. return app_show(0,"获取成功",$list);
  337. }
  338. /**
  339. * 修改用户基础信息
  340. */
  341. public function setRole(){
  342. $post=$this->request->only(["id"=>"","nickname"=>"","mobile"=>"","relaComNo"=>"","companyNo"=>"","roleid"=>"",
  343. "token"=>""],"post","trim");
  344. $valide=Validate::rule([
  345. "id|账户ID"=>"require|number|gt:0",
  346. "nickname|名称"=>"require|max:255",
  347. "mobile|手机号"=>"require|mobile",
  348. "relaComNo|业务公司"=>"max:255",
  349. "companyNo|业务公司"=>"max:255",
  350. "roleid|角色ID"=>"require|number",
  351. ]);
  352. if($valide->check($post)==false) return error_show(1004,$valide->getError());
  353. if($post['roleid']!=0){
  354. $roleinfo =Db::name("role")->where("id","=",$post['roleid'])->find();
  355. if(empty($roleinfo)){
  356. return error_show(1002,"角色不存在");
  357. }
  358. if($roleinfo['status']==0){
  359. return error_show(1002,"角色已禁用");
  360. }
  361. if($roleinfo['level']==1){
  362. return error_show(1002,"超管级别角色不允许设置");
  363. }
  364. }
  365. if($this->level==1){
  366. $companyNo = isset($post['companyNo'])&&$post['companyNo']!=='' ? trim($post['companyNo']) :"";
  367. }else{
  368. $companyNo = isset($post['relaComNo'])&&$post['relaComNo']!=='' ? trim($post['relaComNo']) :"";
  369. }
  370. if($companyNo==""){
  371. return error_show(1004,"业务公司编号不能为空");
  372. }
  373. $company =Db::name("supplier_info")->where(["code"=>$companyNo])->find();
  374. if($company==false){
  375. return error_show(1004,"业务公司不存在");
  376. }
  377. $isSuper = Db::name("user_role")->alias("a")
  378. ->leftJoin("role b","a.roleid=b.id")
  379. ->where(["a.uid"=>$post['id'],"a.is_del"=>0,"b.level"=>1])
  380. ->findOrEmpty();
  381. if(!empty($isSuper))return error_show(1002,"超管账户不允许设置其他角色");
  382. $userrole = Db::name("user_role")
  383. ->where(["uid"=>$post['id'],"companyNo"=>$companyNo,"is_del"=>0])
  384. ->find();
  385. $ismain =Db::name("user_role")->where(["uid"=>$post['id'],"is_main"=>1,"is_del"=>0])->find();
  386. Db::startTrans();
  387. try{
  388. if(empty($userrole)){
  389. $roledata =[
  390. "uid"=>$post['id'],
  391. "roleid"=>$post['roleid'],
  392. "nickname"=>$post['nickname'],
  393. "companyNo"=>$companyNo,
  394. "company_type"=>$company["type"]==3?2:1,
  395. "is_main"=>$ismain?0:1,
  396. "status"=>1,
  397. "addtime"=>date("Y-m-d H:i:s"),
  398. "updatetime"=>date("Y-m-d H:i:s")
  399. ];
  400. $companyarr=[
  401. [
  402. "companyCode"=>$company["code"],
  403. "companyName"=>$company["name"],
  404. "company_type"=>$company["type"]==3?2:1,
  405. "is_main"=>$ismain?0:1
  406. ]
  407. ];
  408. $companyinfo = ["id"=>$post['id'],"company"=>$companyarr,"token"=>$post['token']];
  409. $companyset =setUserCompany($companyinfo);
  410. if(!isset($companyset['code'])||$companyset['code']!=0){
  411. return error_show(1004,$companyset['message']);
  412. }
  413. $up= Db::name("user_role")->insert($roledata);
  414. }else{
  415. $userrole['roleid'] = $post['roleid'];
  416. $userrole['nickname'] = $post['nickname'];
  417. $userrole['companyNo'] = $companyNo;
  418. $userrole['company_type'] =$company["type"]==3?2:1;
  419. $userrole['updatetime'] = date("Y-m-d H:i:s");
  420. $up=Db::name("user_role")->where(["uid"=>$post['id'],"companyNo"=>$companyNo,"is_del"=>0])->save($userrole);
  421. }
  422. $saveinfo =[
  423. "id"=>$post['id'],
  424. "nickname"=>$post['nickname'],
  425. "mobile"=>$post['mobile'],
  426. "token"=>$post['token']
  427. ];
  428. $saev= resetinfo($saveinfo);
  429. if(!isset($saev['code'])||$saev['code']!=0){
  430. return error_show(1004,$saev['message']);
  431. }
  432. if($up==false){
  433. Db::rollback();
  434. return error_show(1004,'用户信息更新失败');
  435. }
  436. Db::commit();
  437. return app_show(0,'用户信息更新成功');
  438. }catch (\Exception $e){
  439. Db::rollback();
  440. return error_show(1004,$e->getMessage());
  441. }
  442. }
  443. /**获取用户关联业务公司
  444. * @return \think\response\Json|void
  445. * @throws \think\db\exception\DataNotFoundException
  446. * @throws \think\db\exception\DbException
  447. * @throws \think\db\exception\ModelNotFoundException
  448. */
  449. public function GetCompany(){
  450. $post=$this->request->only(["token"=>"","uid"=>"","companyName"=>"","page"=>1,"size"=>20],"post","trim");
  451. $where=[['a.is_del',"=",0]];
  452. if($post['uid']!=""){
  453. $where[]=["a.uid","=",$post['uid']];
  454. }else{
  455. $where[]=["a.uid","=",$this->uid];
  456. }
  457. if($post['companyName']!=""){
  458. $where[]=["b.name","like","%{$post['companyName']}%"];
  459. }
  460. $count = Db::name("user_role")->alias("a")
  461. ->leftJoin("supplier_info b","a.companyNo=b.code")
  462. ->where($where)
  463. ->count();
  464. $total =ceil($count/$post['size']);
  465. $page = $post['page']>$total ? intval($total):intval($post['page']);
  466. $list = Db::name("user_role")->alias("a")
  467. ->leftJoin("supplier_info b","a.companyNo=b.code")
  468. ->where($where)
  469. ->field("a.uid,a.nickname,roleid,companyNo,b.name as companyName,a.status,a.company_type")
  470. ->page($page,intval($post['size']))
  471. ->order("a.is_main desc")
  472. ->select()->toArray();
  473. foreach ($list as &$value){
  474. $value['role_name'] = Db::name("role")->where(["id"=>$value["roleid"]])->value("role_name","");
  475. }
  476. return app_show(0,'获取成功',["list"=>$list,"count"=>$count]);
  477. }
  478. //密码设置
  479. public function setPwd(){
  480. $post=$this->request->only(["id"=>"","password"=>""],"post","trim");
  481. $validate=Validate::rule([
  482. 'id|账户ID' => 'require|number',
  483. 'password|密码' => 'require|min:6|max:200',
  484. ]);
  485. if($validate->check($post)==false) return error_show(1004,$validate->getError());
  486. $data = resetpwd($post);
  487. if(empty($data)||$data['code']!=0){
  488. return error_show($data['code'],$data['message']);
  489. }else{
  490. return app_show(0,"密码修改成功");
  491. }
  492. }
  493. public function usersave(){
  494. $post=$this->request->only(["id"=>"","nickname"=>"","mobile"=>"","email"=>"","relaComNo"=>"","companyArr"=>[],"token"=>""],"post","trim");
  495. $validate=Validate::rule([
  496. 'id|账户ID' => 'number',
  497. 'nickname|真实姓名' => 'require|min:2|max:200',
  498. 'mobile|手机号' => 'require|number|length:11|mobile',
  499. 'email|邮箱' => 'email',
  500. 'companyArr|关联账户' => 'array',
  501. ]);
  502. if($validate->check($post)==false) return error_show(1004,$validate->getError());
  503. if(!empty($post['companyArr'])){
  504. foreach ($post['companyArr'] as &$item){
  505. $company =Db::name("supplier_info")->where(["code"=>$item['companyCode']])->find();
  506. $item['company_type'] = $company["type"]==3?2:1;
  507. }
  508. }
  509. if($post['id']!=""){
  510. $isSuper = Db::name("user_role")->alias("a")
  511. ->leftJoin("role b","a.roleid=b.id")
  512. ->where(["a.uid"=>$post['id'],"a.is_del"=>0,"b.level"=>1])
  513. ->findOrEmpty();
  514. if(!empty($isSuper) && !empty($post['companyArr']))return error_show(1002,"超管账户不允许设置关联业务公司");
  515. $saveinfo =[
  516. "id"=>$post['id'],
  517. "nickname"=>$post['nickname'],
  518. "mobile"=>$post['mobile'],
  519. "token"=>$post['token']
  520. ];
  521. $saev= resetinfo($saveinfo);
  522. if(!isset($saev['code'])||$saev['code']!=0){
  523. return error_show(1004,$saev['message']);
  524. }
  525. if(!empty($post['companyArr'])){
  526. $companyinfo = ["id"=>$post['id'],"company"=>$post['companyArr'],"token"=>$post['token']];
  527. $companyset =setUserCompany($companyinfo);
  528. if(!isset($companyset['code'])||$companyset['code']!=0){
  529. return error_show(1004,$companyset['message']);
  530. }
  531. }
  532. }else{
  533. $userrole=[];
  534. if($post['relaComNo']!=""){
  535. $company =Db::name("supplier_info")->where(["code"=>$post['relaComNo']])->find();
  536. if($company){
  537. $temp = [
  538. "companyCode"=>$company["code"],
  539. "companyName"=>$company["name"],
  540. "company_type"=>$company["type"]==3?2:1,
  541. "is_main"=>1,
  542. ];
  543. $post['companyArr'][]=$temp;
  544. }
  545. }else{
  546. if(!empty($post['companyArr'])){
  547. foreach ($post['companyArr'] as &$item){
  548. $company =Db::name("supplier_info")->where(["code"=>$item['companyCode']])->find();
  549. $item['company_type'] = $company["type"]==3?2:1;
  550. }
  551. }else{
  552. $post['companyArr'][]= [
  553. "companyCode"=>'',
  554. "companyName"=>'',
  555. "company_type"=>0,
  556. "is_main"=>1,
  557. ];
  558. $userrole=[
  559. "uid"=>0,
  560. "nickname"=>'',
  561. "roleid"=>1,
  562. "companyNo"=>'',
  563. "company_type"=>'',
  564. "status"=>1,
  565. "is_main"=>1,
  566. "is_del"=>0,
  567. "addtime"=>date("Y-m-d H:i:s"),
  568. "updatetime"=>date("Y-m-d H:i:s"),
  569. ];
  570. }
  571. }
  572. $nam = addacount($post);
  573. if(!isset($nam['code'])||$nam['code']!=0){
  574. return error_show(1004,$nam['message']);
  575. }
  576. if (!empty($userrole)){
  577. $userrole['uid'] = $nam['data']["userid"];
  578. $userrole['nickname'] = $nam['data']["nickname"];
  579. $up =Db::name("user_role")->insert($userrole);
  580. if($up==false){
  581. return error_show(1004,'账户超管角色分配失败');
  582. }
  583. }
  584. }
  585. return $post['id']==""?app_show(0,"账户新建成功"):app_show(0,"账户编辑成功");
  586. }
  587. //添加超管账号
  588. public function adminAccountAdd()
  589. {
  590. $post = $this->request->only(['nickname', 'mobile', 'email' => '', 'relaComNo' => ''], 'post', 'trim');
  591. $validate = Validate::rule([
  592. 'nickname|真实姓名' => 'require|min:2|max:200',
  593. 'mobile|手机号' => 'require|mobile',
  594. 'email|邮箱' => 'email|max:255',
  595. 'companyArr|关联账户' => 'array|max:100',
  596. ]);
  597. if ($validate->check($post) == false) return error_show(1004, $validate->getError());
  598. $post['companyArr'][] = [
  599. "companyCode" => '',
  600. "companyName" => '',
  601. "company_type" => 0,
  602. "is_main" => 1,
  603. ];
  604. $rs = add_admin_acount($post);
  605. if (!isset($rs['code']) || $rs['code'] != 0) return error_show(1004, $rs['message']);
  606. $up = Db::name("user_role")->insert([
  607. "uid" => $rs['data']["userid"],
  608. "nickname" => $rs['data']["nickname"],
  609. "roleid" => 1,
  610. "companyNo" => '',
  611. "company_type" => '',
  612. "status" => 1,
  613. "is_main" => 1,
  614. "is_del" => 0,
  615. "addtime" => date("Y-m-d H:i:s"),
  616. "updatetime" => date("Y-m-d H:i:s"),
  617. ]);
  618. if ($up == false) return error_show(1004, '超管账户分配角色失败');
  619. return app_show(0, '添加超管账号成功');
  620. }
  621. }