User.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\admin\controller;
  4. use think\App;use think\Exception;use think\facade\Db;use think\facade\Validate;
  5. class User extends Base{
  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,'level'=>''],"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. switch ($value['level']) {
  134. case 1:
  135. $role_arr_where = ["uid" => $value['id'], "is_del" => 0];
  136. break;
  137. case 2:
  138. $role_arr_where = ["uid" => $value['id'], "a.companyNo" => $value['companyCode'], "is_del" => 0];
  139. break;
  140. case 3:
  141. $role_arr_where = ["uid" => $value['id'], "a.companyNo" => [$value['companyCode'] , ''], "is_del" => 0];
  142. break;
  143. }
  144. $roleArr = Db::name("user_role")
  145. ->alias("a")
  146. ->field("a.id,role_name,roleid,a.status,b.level")
  147. ->leftJoin("role b", "a.roleid=b.id")
  148. ->where($role_arr_where)
  149. ->order(['a.companyNo' => 'desc'])
  150. ->findOrEmpty();
  151. $value['roleid']=$roleArr['roleid']??"";
  152. $value['status']=$value['status']==1?($roleArr['status']??0) :0;
  153. $value["role_name"] =$roleArr['role_name']??"";
  154. $value["role_level"] =$roleArr['level']??"";
  155. $list[]=$value;
  156. }
  157. return app_show(0,"获取成功",["list"=>$list,"count"=>$userinfo['data']["count"]]);
  158. }
  159. /**
  160. * 用户列表
  161. */
  162. public function userSingleList(){
  163. $post=$this->request->only(["token"=>"","nickname"=>"","relaComNo"=>"","islevel"=>"","username"=>"","companyNo"=>"","status"=>"","page"=>1,"size"=>10,'level'=>''],"post");
  164. if($this->level==1){
  165. $companyNo = $post['companyNo']??"";
  166. }else{
  167. $companyNo = $post['relaComNo']??"";
  168. $post['companyNo'] = $post['relaComNo']??"";
  169. }
  170. $where=[["a.is_del","=",0]];
  171. unset($post['relaComNo']);
  172. if($post['companyNo']!=""){
  173. $where[]=["a.companyNo","=",$companyNo];
  174. }
  175. if($post["islevel"]!=""){
  176. $uid =Db::name("user_role")->alias("a")
  177. ->leftJoin("role b","a.roleid=b.id")->where([["b.level","=",1],["a.is_del","=",0]])->where($where)->column
  178. ("uid");
  179. if($post['islevel']==1){
  180. $post['uid']= $uid;
  181. }else{
  182. $post['nuid']=$uid;
  183. }
  184. unset($post["islevel"]);
  185. }
  186. $userinfo=GetList($post);
  187. if(empty($userinfo)||$userinfo['code']!=0){
  188. return app_show($userinfo['code'],$userinfo['msg']);
  189. }
  190. $data = $userinfo['data']['list'];
  191. $list=[];
  192. foreach ($data as $value){
  193. if(isset($value['company_relaton'])&& !empty($value['company_relaton'])){
  194. foreach ($value['company_relaton'] as &$item){
  195. $roleArr = Db::name("user_role")->alias("a")
  196. ->leftJoin("role b","a.roleid=b.id")
  197. ->where(["uid"=>$value['id'],"a.companyNo"=>$item['companyCode'],"is_del"=>0])
  198. ->field("role_name,roleid,a.status,b.level")->findOrEmpty();
  199. $item['roleid']=$roleArr['roleid']??"";
  200. $item['status']=$roleArr['status']??0;
  201. $item["role_name"] =$roleArr['role_name']??"";
  202. $item["role_level"] =$roleArr['level']??"";
  203. }
  204. }
  205. $value['roleid']=$roleArr['roleid']??"";
  206. $value['status']=$value['status']??0;
  207. $value["role_name"] =$roleArr['role_name']??"";
  208. $value["role_level"] =$roleArr['level']??"";
  209. $list[]=$value;
  210. }
  211. return app_show(0,"获取成功",["list"=>$list,"count"=>$userinfo['data']["count"]]);
  212. }
  213. /**获取当前访问用户的信息
  214. * @param token
  215. * @return \think\response\Json
  216. * @throws \think\exception\DbException
  217. */
  218. public function userInfo(){
  219. $post =$this->request->only(["token"=>"","relaComNo"=>""],"post","trim");
  220. $userinfo=GetUserInfo($post['token']);
  221. if(empty($userinfo)||$userinfo['code']!=0){
  222. return app_show(1002,"员工信息不存在");
  223. }
  224. $data = $userinfo['data'];
  225. $where=[["uid","=", $data['id']],["is_del","=",0]];
  226. if($post['relaComNo']!=""){
  227. $where[]=["companyNo","=",$post['relaComNo']];
  228. }else{
  229. $where[]=["is_main","=",1];
  230. }
  231. $role =Db::name("user_role")->where($where)->findOrEmpty();
  232. if(!empty($role)){
  233. $company_name = Db::name("supplier_info")->where(["code"=>$role['companyNo']])->value("name","");
  234. $roleinfo = Db::name("role")->where(["id"=>$role['roleid']])->field("role_name,level")->find();
  235. }else{
  236. return error_show(1004,'未开通登录权限');
  237. }
  238. if(!empty($data['company_relaton'])){
  239. $roleArr = Db::name("user_role")->alias("a")
  240. ->leftJoin("role b","a.roleid=b.id")
  241. ->where(["a.is_del"=>0,"a.uid"=>$data['id']])
  242. ->column("role_name,roleid,a.status,b.level","a.companyNo");
  243. foreach ($data['company_relaton'] as &$item){
  244. $item['roleid'] =isset($roleArr[$item['companyCode']])?$roleArr[$item['companyCode']]['roleid']:"";
  245. $item['role_name'] =isset($roleArr[$item['companyCode']])?$roleArr[$item['companyCode']]['role_name']:"";
  246. $item['role_status'] =isset($roleArr[$item['companyCode']])?$roleArr[$item['companyCode']]['status']:"";
  247. $item['role_level'] =isset($roleArr[$item['companyCode']])?$roleArr[$item['companyCode']]['level']:"";
  248. }
  249. }
  250. $data['role_name']=$roleinfo['role_name']??'';
  251. $data['company_name']=$company_name??'';
  252. $data['role_level']=$roleinfo['level']??'';
  253. $data['role_id']=$role['roleid']??'';
  254. $data['companyNo']=$role['companyNo']??'';
  255. $data['company_type']=$role['company_type']??'';
  256. return app_show(0,"获取成功",$data);
  257. }
  258. /**@param id int 账户id
  259. /**@param status int 状态 0/1
  260. * @return \think\response\Json|void
  261. */
  262. public function setStatus(){
  263. $post =$this->request->only(["id"=>"","status"=>"","token"=>""],"post");
  264. $valide =Validate::rule([
  265. 'id|账户ID' => 'require|number|gt:0',
  266. 'status|状态' => 'require|number|in:0,1',
  267. ]);
  268. if($valide->check($post)==false) return error_show(1004,$valide->getError());
  269. $message = $post['status']==1?"启用":"禁用";
  270. if($post['id']==$this->uid && $post['status']==0){
  271. return error_show(1004,'不可禁用当前账户');
  272. }
  273. Db::startTrans();
  274. try{
  275. $account = Db::name("user_role")->where(["uid"=>$post['id'],"is_del"=>0])->update(["status"=>$post['status'],"updatetime"=>date("Y-m-d H:i:s")]);
  276. if($account==false){
  277. throw new Exception("账户{$message}失败,账户未在系统中开通权限");
  278. }
  279. $condition=["id"=>$post['id'],"status"=>$post['status'],"token"=>$post['token']];
  280. $ex=setStatus($condition);
  281. if(!isset($ex['code'])||$ex['code']!=0){
  282. throw new Exception($ex['message']);
  283. }
  284. Db::commit();
  285. return app_show(0,"{$message}成功");
  286. }catch (\Exception $e){
  287. Db::rollback();
  288. return error_show(1006,$e->getMessage());
  289. }
  290. }
  291. public function setCompanyStatus(){
  292. $post =$this->request->only(["id"=>"","status"=>"","relaComNo"=>"","companyNo"=>"","token"=>""],"post");
  293. $valide =Validate::rule([
  294. 'id|账户ID' => 'require|number|gt:0',
  295. 'status|状态' => 'require|number|in:0,1',
  296. 'relaComNo|业务公司' => 'max:255',
  297. 'companyNo|公司编号' => 'require|max:255',
  298. ]);
  299. if($valide->check($post)==false) return error_show(1004,$valide->getError());
  300. $message = $post['status']==1?"启用":"禁用";
  301. if($post['id']==$this->uid &&$post['companyNo']==$post['relaComNo']&& $post['status']==0){
  302. return error_show(1004,'不可禁用当前账户');
  303. }
  304. Db::startTrans();
  305. try{
  306. $userinfo =Db::name("user_role")->where(["uid"=>$post['id'],"companyNo"=>$post['companyNo'],"is_del"=>0])->findOrEmpty();
  307. if(!empty($userinfo)){
  308. // if($userinfo['is_main']==1) throw new Exception("默认账户不可禁用!");
  309. $userinfo['status']=$post['status'];
  310. $userinfo['updatetime']=date("Y-m-d H:i:s");
  311. $sa=Db::name("user_role")->save($userinfo);
  312. if($sa==false) throw new Exception("状态更新失败!");
  313. }
  314. $condition=["account_id"=>$post['id'],"companyCode"=>$post['companyNo'],"status"=>$post['status'],"token"=>$post['token']];
  315. $effect=setCompanyStatus($condition);
  316. if(!isset($effect['code'])||$effect['code']!=0)throw new Exception($effect['message']);
  317. Db::commit();
  318. return app_show(0,"{$message}成功");
  319. }catch (\Exception $e){
  320. Db::rollback();
  321. return error_show(1004,$e->getMessage());
  322. }
  323. }
  324. //获取所有用户的信息
  325. public function userAll(){
  326. $post=$this->request->only(['relaComNo'=>"","companyNo"=>"","nickname"=>"","roleid"=>"","status"=>""],"post","trim");
  327. $condition = [["is_del","=",0]];
  328. $rolecond=" level in (1,2,3)";
  329. if($post['relaComNo']!=""){
  330. $condition[]=["companyNo","=",$post['relaComNo']];
  331. $rolecond = "level=1 or (level in (2,3) and companyNo ='{$post['relaComNo']}' )";
  332. }
  333. if($post['companyNo']!=""){
  334. $condition[]=["companyNo","=",$post['companyNo']];
  335. $rolecond = "level=1 or (level in (2,3) and companyNo ='{$post['companyNo']}' )";
  336. }
  337. if($post['roleid']!=""){
  338. $condition[]=["roleid","=",$post['roleid']];
  339. $rolecond = "id={$post['roleid']}";
  340. }
  341. if($post['status']!="")$condition[]=["status","=",$post['status']];
  342. if($post['nickname']!="")$condition[]=["nickname","like","%{$post['nickname']}%"];
  343. $list=Db::name("user_role")->where($condition)->select()->toArray();
  344. $role = Db::name("role")->whereRaw($rolecond)->column("role_name,level","id");
  345. foreach ($list as &$value){
  346. $value['role_name']= $role[$value['roleid']]['role_name']??"";
  347. $value['role_level']= $role[$value['roleid']]['level']??"";
  348. $value['companyName']= Db::name("supplier_info")->where(["code"=>$value['companyNo']])->value("name",'');
  349. }
  350. return app_show(0,"获取成功",$list);
  351. }
  352. /**
  353. * 修改用户基础信息
  354. */
  355. public function setRole()
  356. {
  357. $post = $this->request->only(["id", 'nickname', 'mobile', "relaComNo" => "", "companyNo" => "", "roleid", "token" => ""], "post", "trim");
  358. $valide = Validate::rule([
  359. "id|账户ID" => "require|number|gt:0",
  360. "nickname|名称" => "require|max:255",
  361. "mobile|手机号" => "require|mobile",
  362. "relaComNo|业务公司" => "max:255",
  363. "companyNo|业务公司" => "max:255",
  364. "roleid|角色ID" => "require|number",
  365. ]);
  366. if ($valide->check($post) == false) return error_show(1004, $valide->getError());
  367. if ($post['roleid'] != 0) {
  368. $roleinfo = Db::name("role")
  369. ->field('id,status,level')
  370. ->where("id", $post['roleid'])
  371. ->find();
  372. if (empty($roleinfo)) return error_show(1002, "角色不存在");
  373. if ($roleinfo['status'] == 0) return error_show(1002, "角色已禁用");
  374. if ($roleinfo['level'] == 1) return error_show(1002, "超管级别角色不允许设置");
  375. }
  376. //查询该账户的详情
  377. $userinfo = GetInfoById($post['token'],['id'=>$post['id']]);
  378. if (!isset($userinfo['code']) || $userinfo['code'] != 0) return error_show($userinfo['code'], $userinfo['message'], $userinfo['data']);
  379. $level = $userinfo['data']['level'];
  380. if ($level == 1) return error_show(1004, '超管账号不允许设置角色');
  381. // if ($level == 1) {
  382. $companyNo = isset($post['companyNo']) && $post['companyNo'] !== '' ? trim($post['companyNo']) : "";
  383. // } else {
  384. // $companyNo = isset($post['relaComNo']) && $post['relaComNo'] !== '' ? trim($post['relaComNo']) : "";
  385. // }
  386. $company_type = 0;
  387. if ($level== 2) {
  388. if ($companyNo == "") return error_show(1004, "业务公司编号不能为空");
  389. $company = Db::name("company_info")->field('id,companyNo code,company_name name')->where(["companyNo" => $companyNo])->find();
  390. if ($company == false) return error_show(1004, "业务公司不存在");
  391. $company_type = 1;
  392. }elseif ($level==3){
  393. if ($companyNo == "") return error_show(1004, "供应商编号不能为空");
  394. $company = Db::name("supplier_info")->field('id,code,type,name')->where(["code" => $companyNo])->find();
  395. if ($company == false) return error_show(1004, "供应商公司不存在");
  396. $company_type = 2;
  397. }
  398. // $isSuper = Db::name("user_role")->alias("a")
  399. // ->leftJoin("role b", "a.roleid=b.id")
  400. // ->where(["a.uid" => $post['id'], "a.is_del" => 0, "b.level" => 1])
  401. // ->findOrEmpty();
  402. // if (!empty($isSuper)) return error_show(1002, "超管账户不允许设置其他角色");
  403. $userrole_where = ["uid" => $post['id'], "is_del" => 0, 'companyNo' => $companyNo];
  404. // if ($level == 2) $userrole_where['companyNo'] = $companyNo;
  405. $userrole = Db::name("user_role")
  406. ->field('id')
  407. ->where($userrole_where)
  408. ->find();
  409. $ismain = Db::name("user_role")->field('id')->where(["uid" => $post['id'], "is_main" => 1, "is_del" => 0])->find();
  410. Db::startTrans();
  411. try {
  412. if (empty($userrole)) {
  413. $roledata = [
  414. "uid" => $post['id'],
  415. "roleid" => $post['roleid'],
  416. "nickname" => $post['nickname'],
  417. "companyNo" => $companyNo,
  418. "company_type" => $company_type,
  419. "is_main" => $ismain ? 0 : 1,
  420. "status" => 1,
  421. "addtime" => date("Y-m-d H:i:s"),
  422. "updatetime" => date("Y-m-d H:i:s")
  423. ];
  424. //level 为1,3时,角色不和公司关联,所以不需要操作
  425. if ($level == 2) {
  426. $companyarr = [
  427. [
  428. "companyCode" => $company["code"],
  429. "companyName" => $company["name"],
  430. "company_type" => $company_type,
  431. "is_main" => $ismain ? 0 : 1
  432. ]
  433. ];
  434. $companyinfo = ["id" => $post['id'], "company" => $companyarr, "token" => $post['token']];
  435. $companyset = setUserCompany($companyinfo);
  436. if (!isset($companyset['code']) || $companyset['code'] != 0) {
  437. return error_show(1004, $companyset['message']);
  438. }
  439. }
  440. $up = Db::name("user_role")->insert($roledata);
  441. } else {
  442. $userrole['roleid'] = $post['roleid'];
  443. $userrole['nickname'] = $post['nickname'];
  444. $userrole['companyNo'] = $companyNo;
  445. $userrole['company_type'] = $company_type;
  446. $userrole['updatetime'] = date("Y-m-d H:i:s");
  447. $userrole['status'] = 1;
  448. $up = Db::name("user_role")->where(["id" => $userrole['id'], "is_del" => 0])->save($userrole);
  449. }
  450. // $saveinfo = [
  451. // "id" => $post['id'],
  452. // "nickname" => $post['nickname'],
  453. // "mobile" => $post['mobile'],
  454. // "token" => $post['token']
  455. // ];
  456. // $saev = resetinfo($saveinfo);
  457. // if (!isset($saev['code']) || $saev['code'] != 0) {
  458. // return error_show(1004, $saev['message']);
  459. // }
  460. if ($up == false) {
  461. Db::rollback();
  462. return error_show(1004, '用户信息更新失败');
  463. }
  464. Db::commit();
  465. return app_show(0, '用户信息更新成功');
  466. } catch (\Exception $e) {
  467. Db::rollback();
  468. return error_show(1004, $e->getMessage());
  469. }
  470. }
  471. /**获取用户关联业务公司
  472. * @return \think\response\Json|void
  473. * @throws \think\db\exception\DataNotFoundException
  474. * @throws \think\db\exception\DbException
  475. * @throws \think\db\exception\ModelNotFoundException
  476. */
  477. public function GetCompany()
  478. {
  479. $post = $this->request->only(["uid" => 0, "companyCode" => "", "companyName" => "", "page" => 1, "size" => 20], "post", "trim");
  480. if ($post['uid'] == 0) $post['uid'] = $this->uid;
  481. $rs = get_company_list($post);
  482. // return app_show($rs['code'], $rs['message'], $rs['data']);
  483. // $where = [['a.is_del', "=", 0]];
  484. // if ($post['uid'] != "") {
  485. // $where[] = ["a.uid", "=", $post['uid']];
  486. // } else {
  487. // $where[] = ["a.uid", "=", $this->uid];
  488. // }
  489. // if ($post['companyName'] != "") {
  490. // $where[] = ["b.name", "like", "%{$post['companyName']}%"];
  491. // }
  492. // $count = Db::name("user_role")
  493. // ->alias("a")
  494. // ->leftJoin("supplier_info b", "a.companyNo=b.code")
  495. // ->where($where)
  496. // ->count();
  497. // $total = ceil($count / $post['size']);
  498. // $page = $post['page'] > $total ? intval($total) : intval($post['page']);
  499. // $list = Db::name("user_role")
  500. // ->alias("a")
  501. // ->leftJoin("supplier_info b", "a.companyNo=b.code")
  502. // ->where($where)
  503. // ->field("a.uid,a.nickname,roleid,companyNo,b.name as companyName,a.status,a.company_type")
  504. // ->page($page, intval($post['size']))
  505. // ->select()
  506. // ->toArray();
  507. foreach ($rs['data']['list'] as &$value) {
  508. switch ($value['level']) {
  509. case 1:
  510. $role_arr_where = ["uid" => $value['account_id'], "is_del" => 0];
  511. break;
  512. case 2:
  513. $role_arr_where = ["uid" => $value['account_id'], "a.companyNo" => $value['companyCode'], "is_del" => 0];
  514. break;
  515. case 3:
  516. $role_arr_where = ["uid" => $value['account_id'], "a.companyNo" => [$value['companyCode'] . ''], "is_del" => 0];
  517. break;
  518. }
  519. $value['role_name'] = Db::name("user_role")
  520. ->alias("a")
  521. ->leftJoin("role b", "a.roleid=b.id")
  522. ->where($role_arr_where)
  523. ->order(['a.companyNo' => 'desc'])
  524. ->value('b.role_name', '');
  525. }
  526. return app_show(0, '获取成功', $rs['data']);
  527. }
  528. //密码设置
  529. public function setPwd(){
  530. $post=$this->request->only(["id"=>"","password"=>""],"post","trim");
  531. $validate=Validate::rule([
  532. 'id|账户ID' => 'require|number',
  533. 'password|密码' => 'require|min:6|max:200',
  534. ]);
  535. if($validate->check($post)==false) return error_show(1004,$validate->getError());
  536. $data = resetpwd($post);
  537. if(empty($data)||$data['code']!=0){
  538. return error_show($data['code'],$data['message']);
  539. }else{
  540. return app_show(0,"密码修改成功");
  541. }
  542. }
  543. public function usersave()
  544. {
  545. $post = $this->request->only(["id" => "", "nickname", "mobile", "email" => "", "relaComNo" => "", "companyArr" => [], "token" => "", 'level'], "post", "trim");
  546. $validate = Validate::rule([
  547. 'id|账户ID' => 'number',
  548. 'nickname|真实姓名' => 'require|min:2|max:200',
  549. 'mobile|手机号' => 'require|mobile',
  550. 'email|邮箱' => 'email',
  551. 'level|账号等级' => 'require|number|in:1,2,3',
  552. 'companyArr|关联公司' => 'requireIf:level,2|requireIf:level,3|array',
  553. ]);
  554. if ($validate->check($post) == false) return error_show(1004, $validate->getError());
  555. if (!empty($post['companyArr'])) {
  556. foreach ($post['companyArr'] as &$item) {
  557. $company = Db::name("supplier_info")->where(["code" => $item['companyCode']])->find();
  558. $item['company_type'] = $company["type"] == 3 ? 2 : 1;
  559. }
  560. }
  561. if ($post['id'] != "") {
  562. if ($post['level'] == 1) {
  563. $isSuper = Db::name("user_role")->alias("a")
  564. ->leftJoin("role b", "a.roleid=b.id")
  565. ->where(["a.uid" => $post['id'], "a.is_del" => 0, "b.level" => 1])
  566. ->findOrEmpty();
  567. if (!empty($isSuper) && !empty($post['companyArr'])) return error_show(1002, "超管账户不允许设置关联业务公司");
  568. }
  569. $saveinfo = [
  570. "id" => $post['id'],
  571. "nickname" => $post['nickname'],
  572. "mobile" => $post['mobile'],
  573. "token" => $post['token']
  574. ];
  575. $saev = resetinfo($saveinfo);
  576. if (!isset($saev['code']) || $saev['code'] != 0) {
  577. return error_show(1004, $saev['message']);
  578. }
  579. if (!empty($post['companyArr'])) {
  580. $companyinfo = ["id" => $post['id'], "company" => $post['companyArr'], "token" => $post['token']];
  581. $companyset = setUserCompany($companyinfo);
  582. if (!isset($companyset['code']) || $companyset['code'] != 0) {
  583. return error_show(1004, $companyset['message']);
  584. }
  585. }
  586. } else {
  587. if ($post['relaComNo'] != "") {
  588. $company = Db::name("supplier_info")->where(["code" => $post['relaComNo']])->find();
  589. if ($company) {
  590. $temp = [
  591. "companyCode" => $company["code"],
  592. "companyName" => $company["name"],
  593. "company_type" => $company["type"] == 3 ? 2 : 1,
  594. "is_main" => 1,
  595. ];
  596. $post['companyArr'][] = $temp;
  597. }
  598. } else {
  599. if (!empty($post['companyArr'])) {
  600. foreach ($post['companyArr'] as &$item) {
  601. $company = Db::name("supplier_info")->where(["code" => $item['companyCode']])->find();
  602. $item['company_type'] = $company["type"] == 3 ? 2 : 1;
  603. }
  604. } else {
  605. $post['companyArr'][] = [
  606. "companyCode" => '',
  607. "companyName" => '',
  608. "company_type" => 0,
  609. "is_main" => 1,
  610. ];
  611. }
  612. }
  613. $nam = addacount($post);
  614. if (!isset($nam['code']) || $nam['code'] != 0) {
  615. return error_show(1004, $nam['message']);
  616. }
  617. $roleid = 0;
  618. if ($post['level'] == 3) {
  619. //如果是供应商账号,在结算添加以后,要在采销也添加
  620. Db::connect('mysql_wsm3.0')
  621. ->table('wsm_user_role')
  622. ->insert([
  623. "uid" => $nam['data']["userid"],
  624. "nickname" => $nam['data']["nickname"],
  625. "roleid" => env('default_supplier_addcount_role_id', 90),//需要专门为供应商账号指定一个默认的角色id,采销和结算的id要保持一致
  626. "companyNo" => '',
  627. "company_type" => '',
  628. "status" => 1,
  629. "is_main" => 1,
  630. "is_del" => 0,
  631. "addtime" => date("Y-m-d H:i:s"),
  632. "updatetime" => date("Y-m-d H:i:s"),
  633. ]);
  634. $roleid = env('default_supplier_addcount_role_id', 90);//需要专门为供应商账号指定一个默认的角色id,要同步到采销;,
  635. } elseif ($post['level'] == 1) $roleid = 1;//超管
  636. $userrole=[];
  637. switch ($post['level']){
  638. case 1:
  639. //超管账号不和公司关联,给一个默认的角色记录
  640. $userrole = [[
  641. "uid" => $nam['data']["userid"],
  642. "nickname" => $nam['data']["nickname"],
  643. "roleid" => $roleid,
  644. "companyNo" => '',
  645. "company_type" => '',
  646. "status" => 0,
  647. "is_main" => 1,
  648. "is_del" => 0,
  649. "addtime" => date("Y-m-d H:i:s"),
  650. "updatetime" => date("Y-m-d H:i:s"),
  651. ]];
  652. break;
  653. default:
  654. //其他的都需要和公司关联
  655. foreach ($post['companyArr'] as $val) {
  656. $userrole[]=[
  657. "uid" => $nam['data']["userid"],
  658. "nickname" => $nam['data']["nickname"],
  659. "roleid" => $roleid,
  660. "companyNo" => $val['companyCode'],
  661. "company_type" => $val['company_type'],
  662. "status" =>1,
  663. "is_main" => $val['is_main'],
  664. "is_del" => 0,
  665. "addtime" => date("Y-m-d H:i:s"),
  666. "updatetime" => date("Y-m-d H:i:s"),
  667. ];
  668. }
  669. }
  670. if ($userrole) {
  671. $up = Db::name("user_role")->insertAll($userrole);
  672. if ($up == false) return error_show(1004, '账户角色分配失败');
  673. }
  674. // if (!empty($userrole)) {
  675. // $userrole['uid'] = $nam['data']["userid"];
  676. // $userrole['nickname'] = $nam['data']["nickname"];
  677. // }
  678. }
  679. return $post['id'] == "" ? app_show(0, "账户新建成功") : app_show(0, "账户编辑成功");
  680. }
  681. }