VueModule.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use app\utils\AppResult;
  5. use app\utils\DataUtil;
  6. use app\utils\VueAdmin\AdminData;
  7. use think\Request;
  8. /**
  9. * Admin 模块父级
  10. * Module 接管 CURD 公共操作
  11. * Class ModuleController
  12. * @package App\Http\Controllers\Framework
  13. */
  14. class VueModule extends Backend
  15. {
  16. protected $model; // 在子类中赋值
  17. protected array $params = []; // 请求参数
  18. protected array $unset_fields = ['status']; // 初始化隐藏字段
  19. protected int $statusValue = 1; // 状态默认值
  20. protected bool $isPost; // 请求类型是否为POST
  21. public function __construct(Request $request = null)
  22. {
  23. parent::__construct($request);
  24. $this->isPost = $this->request->isPost();
  25. $this->params = $this->request->param();
  26. // 前置操作
  27. $this->callAction($this->request->action(), $this->request->param());
  28. }
  29. /**
  30. * Execute an action on the controller.
  31. *
  32. * @param $method
  33. * @param $parameters
  34. * @return mixed
  35. */
  36. public function callAction($method, $parameters = [])
  37. {
  38. return $this;
  39. }
  40. /**
  41. * 列表页
  42. * @return string|\think\response\Json
  43. * @throws \think\Exception
  44. */
  45. public function index()
  46. {
  47. if ($this->isPost) {
  48. $params = $this->params;
  49. $query = $this->model;
  50. // 自动排序
  51. if ($orderBy = AdminData::orderBy()) {
  52. foreach ($orderBy as $order) {
  53. $query->order($order['sort'], $order['orderBy']);
  54. }
  55. }
  56. if (!isset($params['page'])) {
  57. $list = $query->select();
  58. } else {
  59. $list = $query->paginate($params['size'] ?? 15);
  60. }
  61. return AppResult::response200('success', $list);
  62. } else {
  63. return $this->view->fetch();
  64. }
  65. }
  66. /**
  67. * 添加
  68. * @return string
  69. */
  70. public function insert()
  71. {
  72. $params = $this->params;
  73. // 初始化空数据
  74. $form = DataUtil::field($params, $this->unset_fields);
  75. $form['status'] = $this->statusValue;
  76. $form['create_time'] = time();
  77. if (!$this->model->insert($form)) {
  78. return AppResult::response201('创建失败');
  79. }
  80. return AppResult::response200('创建成功');
  81. }
  82. /**
  83. * 修改
  84. * @return string
  85. */
  86. public function update()
  87. {
  88. $params = $this->params;
  89. if (empty($params['id'])) {
  90. return AppResult::response203('操作超时!');
  91. }
  92. // 初始化空数据
  93. $form = DataUtil::field($params, array_merge(['is_system'], $this->unset_fields));
  94. $form['update_time'] = time();
  95. if (!$this->model->where('id', $params['id'])->update($form)) {
  96. return AppResult::response201('操作失败');
  97. }
  98. return AppResult::response200('操作成功');
  99. }
  100. /**
  101. * 状态
  102. * @return string
  103. */
  104. public function status()
  105. {
  106. $params = $this->params;
  107. if (empty($params['id']) || !isset($params['status'])) {
  108. return AppResult::response203('操作超时!');
  109. }
  110. $form['status'] = $params['status'] == 1 ? 2 : 1;
  111. $form['update_time'] = time();
  112. if (!$this->model->where('id', $params['id'])->update($form)) {
  113. return AppResult::response201('操作失败');
  114. }
  115. return AppResult::response200('操作成功');
  116. }
  117. /**
  118. * 删除
  119. * @return string
  120. */
  121. public function remove()
  122. {
  123. $params = $this->params;
  124. if (empty($params['id'])) {
  125. return AppResult::response203('操作超时!');
  126. }
  127. // $form['delete_time'] = time();
  128. // $form['update_time'] = time();
  129. // if (!$this->model->where('id', $params['id'])->update($form)) {
  130. // return AppResult::response201('操作失败');
  131. // }
  132. if (!$this->model->where('id', $params['id'])->delete()) {
  133. return AppResult::response201('操作失败');
  134. }
  135. return AppResult::response200('操作成功');
  136. }
  137. /**
  138. * 排序
  139. * @return string
  140. */
  141. public function rank()
  142. {
  143. $params = $this->params;
  144. if (!isset($params['id']) || !isset($params['weigh'])) {
  145. return AppResult::response203('操作超时!');
  146. }
  147. $form['weigh'] = $params['weigh'];
  148. $form['update_time'] = time();
  149. if (!$this->model->where('id', $params['id'])->update($form)) {
  150. return AppResult::response201('操作失败');
  151. }
  152. return AppResult::response200('操作成功');
  153. }
  154. }