ManagerController.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. <?php
  2. namespace Modules\ManageApi\Controllers;
  3. use App\Constants\CommonConstant;
  4. use App\Constants\RedisKeyConstant;
  5. use App\Models\AdminModel;
  6. use App\Models\ConfigModel;
  7. use App\Models\MerchantModel;
  8. use Illuminate\Support\Facades\Cache;
  9. class ManagerController extends BaseController
  10. {
  11. public function lists()
  12. {
  13. $inputData = $this->getJsonData();
  14. $rules = [
  15. 'page' => 'required|integer',
  16. 'page_size' => 'required|integer',
  17. ];
  18. $validator = app('validator')->make($inputData, $rules);
  19. if ($validator->fails()) {
  20. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  21. return json_fail($validator->errors()->first());
  22. }
  23. $pageSize = $inputData['page_size'] ?? CommonConstant::PAGE_SIZE;
  24. if ($pageSize > 500) {
  25. $pageSize = CommonConstant::PAGE_SIZE;
  26. }
  27. $page = format_page($inputData['page'] ?? 0);
  28. $res = AdminModel::query()->orderByDesc('created_at')->paginate($pageSize,
  29. ['id', 'account', 'created_at', 'updated_at', 'login_time', 'status'], 'page', $page);
  30. return json_success($res);
  31. }
  32. /**
  33. * 添加管理员
  34. *
  35. * @return \Illuminate\Http\JsonResponse|mixed
  36. * @throws \Illuminate\Contracts\Container\BindingResolutionException
  37. */
  38. public function add()
  39. {
  40. $inputData = $this->getJsonData();
  41. $rules = [
  42. 'account' => 'required|max:120',
  43. 'pass' => 'required|max:64',
  44. ];
  45. $validator = app('validator')->make($inputData, $rules);
  46. if ($validator->fails()) {
  47. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  48. return json_fail($validator->errors()->first());
  49. }
  50. $admin = AdminModel::query()->where('account', $inputData['account'])->first();
  51. if ($admin) {
  52. return json_fail('用户名重复,请重新创建');
  53. }
  54. $salt = random(6);
  55. $data = [
  56. 'salt' => $salt,
  57. 'account' => $inputData['account'],
  58. 'password' => generatePassword($inputData['pass'], $salt),
  59. ];
  60. $res = AdminModel::query()->create($data);
  61. if ($res) {
  62. return json_success([], '创建成功');
  63. } else {
  64. return json_fail('创建失败');
  65. }
  66. }
  67. /**
  68. * 修改管理员
  69. *
  70. * @return \Illuminate\Http\JsonResponse|mixed
  71. * @throws \Illuminate\Contracts\Container\BindingResolutionException
  72. */
  73. public function edit()
  74. {
  75. $inputData = $this->getJsonData();
  76. $rules = [
  77. 'id' => 'required|integer',
  78. 'pass' => 'nullable|string',
  79. 'status' => 'required|in:1,2',
  80. ];
  81. $validator = app('validator')->make($inputData, $rules);
  82. if ($validator->fails()) {
  83. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  84. return json_fail($validator->errors()->first());
  85. }
  86. $admin = AdminModel::query()->where('id', $inputData['id'])->first();
  87. if (!$admin) {
  88. return json_fail('该用户不存在');
  89. }
  90. if (!empty($inputData['pass'])) {
  91. $admin->password = generatePassword($inputData['pass'], $admin->salt);
  92. }
  93. if (!empty($inputData['status'])) {
  94. $admin->status = $inputData['status'];
  95. }
  96. $res = $admin->save();
  97. if ($res) {
  98. return json_success([], '修改成功');
  99. } else {
  100. return json_fail('修改失败');
  101. }
  102. }
  103. /**
  104. * 设置列表
  105. *
  106. * @return mixed
  107. */
  108. public function configLists()
  109. {
  110. $res = ConfigModel::query()->get(['name', 'key', 'value', 'id']);
  111. return json_success($res);
  112. }
  113. /**
  114. * 添加设置
  115. *
  116. * @return \Illuminate\Http\JsonResponse|mixed
  117. * @throws \Illuminate\Contracts\Container\BindingResolutionException
  118. */
  119. public function configAdd()
  120. {
  121. $inputData = $this->getJsonData();
  122. $rules = [
  123. 'name' => 'required|string',
  124. 'key' => 'required|string',
  125. 'value' => 'required|string',
  126. ];
  127. $validator = app('validator')->make($inputData, $rules);
  128. if ($validator->fails()) {
  129. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  130. return json_fail($validator->errors()->first());
  131. }
  132. $data = [
  133. 'key' => $inputData['key'],
  134. 'value' => $inputData['value'],
  135. 'name' => $inputData['name']
  136. ];
  137. $res = ConfigModel::query()->create($data);
  138. if ($res) {
  139. Cache::forget(RedisKeyConstant::CONFIG);
  140. return json_success([], '添加成功');
  141. } else {
  142. return json_fail('添加失败');
  143. }
  144. }
  145. /**
  146. * 修改设置
  147. *
  148. * @return \Illuminate\Http\JsonResponse|mixed
  149. * @throws \Illuminate\Contracts\Container\BindingResolutionException
  150. */
  151. public function configEdit()
  152. {
  153. $inputData = $this->getJsonData();
  154. $rules = [
  155. 'id' => 'required|integer',
  156. 'name' => 'nullable|string',
  157. 'key' => 'nullable|string',
  158. 'value' => 'nullable|string',
  159. ];
  160. $validator = app('validator')->make($inputData, $rules);
  161. if ($validator->fails()) {
  162. // 如果有错误(可能是多个信息),这里只取第一个错误信息返回.
  163. return json_fail($validator->errors()->first());
  164. }
  165. $info = ConfigModel::query()->where('id', $inputData['id'])->first();
  166. if (!$info) {
  167. return json_fail('修改的设置不存在');
  168. }
  169. if (!empty($inputData['key'])) {
  170. $info->key = $inputData['key'];
  171. }
  172. if (!empty($inputData['value'])) {
  173. $info->value = $inputData['value'];
  174. }
  175. if (!empty($inputData['name'])) {
  176. $info->name = $inputData['name'];
  177. }
  178. if ($info->save()) {
  179. Cache::forget(RedisKeyConstant::CONFIG);
  180. return json_success([], '修改成功');
  181. } else {
  182. return json_fail('修改失败');
  183. }
  184. }
  185. }