User.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. namespace app\admin\controller;
  3. use app\BaseController;
  4. use app\common\model\StoreModel;
  5. use app\common\model\UserModel;
  6. use app\Request;
  7. use think\App;
  8. use think\facade\View;
  9. class User extends BaseController
  10. {
  11. private $userModel;
  12. private $storeModel;
  13. public function __construct(App $app)
  14. {
  15. $this->userModel = new UserModel();
  16. $this->storeModel = new StoreModel();
  17. parent::__construct($app);
  18. }
  19. public function index(Request $request) {
  20. $params = $request->param();
  21. $format_params = [
  22. 'store_id' => format_string($params['store_id'] ?? null),
  23. ];
  24. View::assign([
  25. 'list' => $this->userModel->findByPaginate($format_params),
  26. 'params' => $format_params,
  27. 'stores' => $this->storeModel->findAllStore(),
  28. ]);
  29. return view();
  30. }
  31. public function add() {
  32. $params = $this->request->param();
  33. $format_params = [
  34. 'store_id' => format_string($params['store_id'] ?? null),
  35. ];
  36. if ($this->request->isAjax()) {
  37. $exist = $this->userModel->doesItExist($params['mobile']);
  38. if ($exist)
  39. return $this->fail(lang("Data duplication"));
  40. $store = $this->storeModel->findById($params['store_id']);
  41. if (!$store)
  42. return $this->fail(lang('Store does not exist'));
  43. $res = $this->userModel->save([
  44. 'username' => $params['username'],
  45. 'mobile' => $params['mobile'],
  46. 'password' => md5($params['password']),
  47. 'store_id' => $store->id,
  48. 'store_abbr'=> $store->abbr,
  49. 'rule' => $params['rule'],
  50. ]);
  51. return $res ? $this->ok(true) : $this->fail(false);
  52. }
  53. View::assign([
  54. 'params' => $format_params,
  55. 'stores' => $this->storeModel->findAllStore(),
  56. ]);
  57. return view();
  58. }
  59. public function edit() {
  60. $params = $this->request->param();
  61. $user = $this->userModel->findById($params['id']);
  62. $format_params = [
  63. 'store_id' => $user->store_id,
  64. ];
  65. if (!$user)
  66. return $this->fail(lang('Data not exist'));
  67. if ($this->request->isAjax()) {
  68. $exist = $this->userModel->doesItExist($params['mobile']);
  69. if ($exist && $exist->id != $user->id)
  70. return $this->fail(lang("Data duplication"));
  71. $store = $this->storeModel->findById($params['store_id']);
  72. if (!$store)
  73. return $this->fail(lang('Store does not exist'));
  74. $res = $this->userModel->where('id', $params['id'])->update([
  75. 'username' => $params['username'],
  76. 'mobile' => $params['mobile'],
  77. 'password' => $params['password'] == $user->passwrd ? $user->password : md5($params['password']),
  78. 'store_id' => $store->id,
  79. 'store_abbr'=> $store->abbr,
  80. 'rule' => $params['rule'],
  81. 'update_time'=> time()
  82. ]);
  83. return $res ? $this->ok(true) : $this->fail(false);
  84. }
  85. View::assign([
  86. 'params' => $format_params,
  87. 'stores' => $this->storeModel->findAllStore(),
  88. 'user' => $user
  89. ]);
  90. return view();
  91. }
  92. public function delete(\think\Request $request) {
  93. $params = $request->param();
  94. if(!isset($params['ids']))
  95. return $this->fail(lang("Please select the data you want to delete"));
  96. $this->userModel->deleteByIds(explode(',', $params['ids']));
  97. return $this->ok(true);
  98. }
  99. public function total_data() {
  100. return "<p>Hello</p>";
  101. }
  102. }