User.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. <?php
  2. namespace app\admin\controller;
  3. /**
  4. * 管理用户
  5. */
  6. class User extends Admin{
  7. protected $User = null;
  8. protected function _initialize(){
  9. parent::_initialize();
  10. $this->User = model('User');
  11. }
  12. public function index(){
  13. $this->assign("meta_title","用户列表");
  14. return $this->fetch();
  15. }
  16. public function load(){
  17. $where = [];
  18. $rpid = input('param.rpid');
  19. if (!empty($rpid)) {
  20. $where['r.pid'] = $rpid;
  21. }else{
  22. $where['r.name'] = ['eq','user'];
  23. }
  24. $user = input('param.user');
  25. if (!empty($user)) {
  26. $where['u.id|u.name'] = $user;
  27. }
  28. $page = input('get.page');
  29. $limit = input('get.limit');
  30. $list = $this->User->field('u.*')->alias('u')
  31. ->join('Role r','r.id = u.role_id')
  32. ->where($where)->order('u.id desc')->paginate($limit,false,['page'=>$page]);
  33. $data = [];
  34. foreach ($list as $key => $value) {
  35. $data[$key]['id'] = $value['id'];
  36. $data[$key]['name'] = $value['name'];
  37. $data[$key]['state'] = $value['state'];
  38. }
  39. $this->output(0,'加载成功',$data,$list->total());
  40. }
  41. public function add(){
  42. if($this->request->isPost()){
  43. $name = input('post.name');
  44. $user = $this->User->where(['name'=>$name])->find();
  45. if ($user) {
  46. return json(['data'=>null,'code'=>1,'msg'=>'用户名已经存在']);
  47. }
  48. $role_id = input('post.role_id');
  49. $where['id'] = $role_id;
  50. $Role = model('Role');
  51. $role = $Role->where($where)->find();
  52. if (!$role) {
  53. return json(['data'=>null,'code'=>1,'msg'=>'请选择角色']);
  54. }
  55. $password = input('post.password');
  56. if (empty($password)) {
  57. return json(['data'=>null,'code'=>1,'msg'=>'密码不能为空']);
  58. }
  59. $this->User->name= $name;
  60. $salt = getNonceStr(4);
  61. $this->User->password = md5(md5($password).$salt);
  62. $this->User->salt = $salt;
  63. $this->User->role_id = $role_id;
  64. $result = $this->User->save();
  65. if (!$result) {
  66. output(1,'添加失败');
  67. }
  68. output(0,'添加成功');
  69. }else{
  70. $rpid = input('get.rpid');
  71. if (empty($rpid)) {
  72. $where['name'] = 'user';
  73. }else{
  74. $where['pid'] = $rpid;
  75. }
  76. $Role = model('Role');
  77. $roles = $Role->where($where)->select();
  78. $this->assign('roles',$roles);
  79. $this->assign('meta_title','添加用户');
  80. return $this->fetch();
  81. }
  82. }
  83. public function edit(){
  84. if ($this->request->isPost()) {
  85. $id = input('post.id');
  86. $name = input('post.name');
  87. if (!empty($name)) {
  88. $user = $this->User->where(['name'=>$name])->find();
  89. if ($user && $user['id'] != $id) {
  90. return json(['data'=>null,'code'=>1,'msg'=>'用户名已经存在']);
  91. }
  92. }
  93. $user = $this->User->where(['id'=>$id])->find();
  94. if (!$user) {
  95. return json(['data'=>null,'code'=>1,'msg'=>'参数错误']);
  96. }
  97. $role_id = input('post.role_id');
  98. $where['id'] = $role_id;
  99. $Role = model('Role');
  100. $role = $Role->where($where)->find();
  101. if (!$role) {
  102. return json(['data'=>null,'code'=>1,'msg'=>'请选择角色']);
  103. }
  104. $password = input('post.password');
  105. if (!empty($password)) {
  106. $salt = $user['salt'];
  107. $user->password = md5(md5($password).$salt);
  108. }
  109. $oppwd = input('post.oppwd');
  110. if (!empty($oppwd)) {
  111. $user->oppwd = $oppwd;
  112. }
  113. $user->name= $name;
  114. $user->role_id = $role_id;
  115. $result = $user->save();
  116. if ($result) {
  117. return json(['data'=>null,'code'=>0,'msg'=>'编辑成功']);
  118. }
  119. return json(['data'=>null,'code'=>1,'msg'=>'编辑成功']);
  120. }else{
  121. $id = input('get.id');
  122. $user = $this->User->where(['id'=>$id])->find();
  123. $this->assign('user',$user);
  124. $role = $user['role'];
  125. if ($role['name'] == 'user') {
  126. $where['name'] = ['eq','user'];
  127. }else{
  128. $where['name'] = ['not in',['user','admin']];
  129. }
  130. $Role = model('Role');
  131. $roles = $Role->where($where)->select();
  132. $this->assign('roles',$roles);
  133. $id = input('get.id');
  134. $user = $this->User->where(['id'=>$id])->find();
  135. $this->assign('user',$user);
  136. $this->assign('meta_title','编辑用户');
  137. return $this->fetch();
  138. }
  139. }
  140. public function center(){
  141. if ($this->request->isPost()) {
  142. $id = input('post.id');
  143. $user = $this->User->where(['id'=>$id])->find();
  144. if (!$user) {
  145. $this->output(1,'参数错误');
  146. }
  147. $center = input('post.center');
  148. $user->center = $center == 'true'?1:0;
  149. $result = $user->save();
  150. if ($result) {
  151. $this->output(0,'编辑成功');
  152. }
  153. $this->output(1,'编辑失败');
  154. }
  155. }
  156. public function active(){
  157. if ($this->request->isPost()) {
  158. $id = input('post.id');
  159. $user = $this->User->where(['id'=>$id])->find();
  160. if (!$user) {
  161. $this->output(1,'参数错误');
  162. }
  163. $active = input('post.active');
  164. $user->active = $active == 'true'?1:0;
  165. $result = $user->save();
  166. if ($result) {
  167. $this->output(0,'编辑成功');
  168. }
  169. $this->output(1,'编辑失败');
  170. }
  171. }
  172. public function state(){
  173. if ($this->request->isPost()) {
  174. $id = input('post.id');
  175. $user = $this->User->where(['id'=>$id])->find();
  176. if (!$user) {
  177. $this->output(1,'参数错误');
  178. }
  179. $state = input('post.state');
  180. $user->state = $state == 'true'?1:0;
  181. $result = $user->save();
  182. if ($result) {
  183. $this->output(0,'编辑成功');
  184. }
  185. $this->output(1,'编辑失败');
  186. }
  187. }
  188. public function level(){
  189. if ($this->request->isPost()) {
  190. $id = input('post.id');
  191. $user = $this->User->where(['id'=>$id])->find();
  192. if (!$user) {
  193. output(1,'参数错误');
  194. }
  195. $UserLevel = model('user.Level');
  196. $level = input('post.level');
  197. $data = [];
  198. for ($i=0; $i <= $level; $i++) {
  199. $userlevel = $UserLevel->where(['user_id'=>$user['id'],'level'=>$i])->find();
  200. if (!$userlevel) {
  201. $data[$i]['user_id'] = $user['id'];
  202. $data[$i]['level'] = $i;
  203. }
  204. }
  205. $UserLevel->where(['user_id'=>$user['id'],'level'=>['gt',$level]])->delete();
  206. if (count($data) > 0) {
  207. $UserLevel->saveAll($data);
  208. }
  209. $user->level = $level;
  210. $result = $user->save();
  211. if ($result) {
  212. output(0,'编辑成功',['level_text'=>$user['level_text']]);
  213. }
  214. output(1,'编辑失败');
  215. }
  216. }
  217. public function balance(){
  218. if ($this->request->isPost()) {
  219. $id = input('post.id');
  220. $user = $this->User->where(['id'=>$id])->find();
  221. if (!$user) {
  222. output(1,'参数错误');
  223. }
  224. $balance = input('post.balance');
  225. $balance = floatval($balance);
  226. $this->User->startTrans();
  227. $user['balance'] += $balance;
  228. $result = $user->save();
  229. if (!$result) {
  230. $this->User->rollback();
  231. $this->output(1,'编辑失败');
  232. }
  233. $billdata = [];
  234. $i = 0;
  235. $billdata[$i]['user_id'] = $user['id'];
  236. $billdata[$i]['amount'] = $user['balance'];
  237. $billdata[$i]['value'] = $balance;
  238. $billdata[$i]['fee'] = 0;
  239. $billdata[$i]['type'] = 1;
  240. $billdata[$i]['account'] = 'balance';
  241. $Bill = model('Bill');
  242. $result = $Bill->saveAll($billdata);
  243. if (!$result) {
  244. $this->User->rollback();
  245. $this->output(1,'账单保存失败');
  246. }
  247. $this->User->commit();
  248. $this->output(0,'编辑成功',['balance'=>$user['balance']]);
  249. }
  250. }
  251. public function coin(){
  252. if ($this->request->isPost()) {
  253. $id = input('post.id');
  254. $user = $this->User->where(['id'=>$id])->find();
  255. if (!$user) {
  256. output(1,'参数错误');
  257. }
  258. $coin = input('post.coin');
  259. $coin = floatval($coin);
  260. $user['coin'] += $coin;
  261. $result = $user->save();
  262. if ($result) {
  263. output(0,'编辑成功',['coin'=>$user['coin']]);
  264. }
  265. output(1,'编辑失败');
  266. }
  267. }
  268. public function points(){
  269. if ($this->request->isPost()) {
  270. $id = input('post.id');
  271. $user = $this->User->where(['id'=>$id])->find();
  272. if (!$user) {
  273. $this->output(1,'参数错误');
  274. }
  275. $points = input('post.points');
  276. $points = floatval($points);
  277. $user['points'] += $points;
  278. $result = $user->save();
  279. if (!$result) {
  280. $this->User->rollback();
  281. $this->output(1,'编辑失败');
  282. }
  283. $billdata = [];
  284. $i = 0;
  285. $billdata[$i]['user_id'] = $user['id'];
  286. $billdata[$i]['amount'] = $user['points'];
  287. $billdata[$i]['value'] = $points;
  288. $billdata[$i]['fee'] = 0;
  289. $billdata[$i]['type'] = 1;
  290. $billdata[$i]['account'] = 'points';
  291. $Bill = model('Bill');
  292. $result = $Bill->saveAll($billdata);
  293. if (!$result) {
  294. $this->User->rollback();
  295. $this->output(1,'账单保存失败');
  296. }
  297. $this->User->commit();
  298. $this->output(0,'编辑成功',['points'=>$user['points']]);
  299. output(1,'编辑失败');
  300. }
  301. }
  302. public function remark(){
  303. if ($this->request->isPost()) {
  304. $id = input('post.id');
  305. $user = $this->User->where(['id'=>$id])->find();
  306. if (!$user) {
  307. output(1,'参数错误');
  308. }
  309. $value = input('post.value');
  310. $user->remark = $value;
  311. $result = $user->save();
  312. if ($result) {
  313. output(0,'编辑成功');
  314. }
  315. output(1,'编辑失败');
  316. }
  317. }
  318. public function forbid(){
  319. if ($this->request->isPost()) {
  320. $id = input('post.id');
  321. $user = $this->User->where(['id'=>$id])->find();
  322. if (!$user) {
  323. $this->output(1,'参数错误');
  324. }
  325. $options = input('options/a','');
  326. $user->forbid = $options;
  327. $result = $user->save();
  328. if (!$result) {
  329. $this->output(1,'保存失败');
  330. }
  331. $this->output(0,'保存成功');
  332. }else{
  333. $id = input('get.id');
  334. $user = $this->User->where(['id'=>$id])->find();
  335. if (!$user) {
  336. $this->error('参数错误');
  337. }
  338. $this->assign('user',$user);
  339. $this->assign('meta_title','禁止用户');
  340. return $this->fetch();
  341. }
  342. }
  343. public function delete(){
  344. if ($this->request->isPost()) {
  345. $id = input('post.id');
  346. $user = $this->User->where(['id'=>$id])->find();
  347. if (!$user) {
  348. $this->output(1,'参数错误');
  349. }
  350. $Tree = model('Tree');
  351. $tree = $Tree->where(['user_id'=>$user['id'],'layer'=>1])->find();
  352. if ($tree) {
  353. $Tree->where(['node_id'=>$user['id']])->update(['node_id'=>$tree['node_id']]);
  354. }
  355. $result = $user->delete();
  356. if (!$result) {
  357. $this->output(1,'删除失败');
  358. }
  359. $this->output(0,'删除成功');
  360. }
  361. }
  362. public function team(){
  363. $id = input('get.id');
  364. $user = $this->User->where(['id'=>$id])->find();
  365. if (!$user) {
  366. $this->error('参数错误');
  367. }
  368. $this->assign('user',$user);
  369. $this->assign('meta_title','团队');
  370. return $this->fetch();
  371. }
  372. public function loadusers(){;
  373. $pid = input('get.pid',0);
  374. $level = input('get.level',9999);
  375. $list = $this->User->tree($pid,$level);
  376. $this->output(0,'加载成功',$list);
  377. }
  378. public function fans(){
  379. $id = input('get.id');
  380. $user = $this->User->where(['id'=>$id])->find();
  381. if (!$user) {
  382. $this->error('参数错误');
  383. }
  384. $this->assign('user',$user);
  385. $this->assign('meta_title','粉丝团');
  386. return $this->fetch();
  387. }
  388. public function loadfans(){;
  389. $pid = input('get.pid',0);
  390. $level = input('get.level',9999);
  391. $list = $this->User->fans($pid,$level);
  392. $this->output(0,'加载成功',$list);
  393. }
  394. public function getinfobyname(){
  395. $name = input('post.name');
  396. $user = $this->User->where(['name'=>$name,'state'=>1])->find();
  397. if ($user) {
  398. return json(['data'=>$user,'code'=>0,'msg'=>'获取成功']);
  399. }
  400. return json(['data'=>null,'code'=>1,'msg'=>'获取失败']);
  401. }
  402. public function getinfobyno(){
  403. $no = input('post.no');
  404. $user = $this->User->where(['no'=>$no,'state'=>1])->find();
  405. if ($user) {
  406. $data['id'] = $user['id'];
  407. $data['no'] = $user['no'];
  408. $role = $user['role'];
  409. $data['role_name'] = $role['name'];
  410. $company = $user['company'];
  411. $data['company_cname'] = $company['cname'];
  412. return json(['data'=>$data,'code'=>0,'msg'=>'获取成功']);
  413. }
  414. return json(['data'=>null,'code'=>1,'msg'=>'获取失败']);
  415. }
  416. public function getinfobyid(){
  417. $id = input('post.id');
  418. $user = $this->User->where(['id'=>$id,'state'=>1])->find();
  419. if ($user) {
  420. return json(['data'=>$user,'code'=>0,'msg'=>'获取成功']);
  421. }
  422. return json(['data'=>null,'code'=>1,'msg'=>'获取失败']);
  423. }
  424. }