Template.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. namespace app\badminapi\controller;
  3. use app\models\system\StoreTemplate;
  4. use crmeb\basic\BaseModel;
  5. use crmeb\services\UtilService;
  6. use think\db\exception\DataNotFoundException;
  7. use think\db\exception\DbException;
  8. use think\db\exception\ModelNotFoundException;
  9. use think\Exception;
  10. use think\Response;
  11. /**
  12. * 店铺模板类
  13. * Class Template
  14. * @package app\adminapi\controller
  15. */
  16. class Template extends AuthController
  17. {
  18. public function initialize()
  19. {
  20. parent::initialize(); // TODO: Change the autogenerated stub
  21. }
  22. /**
  23. * 获取模板列表
  24. * @return mixed
  25. * @throws DbException
  26. * @throws DataNotFoundException
  27. * @throws ModelNotFoundException
  28. */
  29. public function get_list()
  30. {
  31. $search = UtilService::getMore([
  32. ['name', ''],
  33. ['page', 1],
  34. ['limit', 10],
  35. ['use_version', ''],
  36. ['is_ban', ''],
  37. ], $this->request, false);
  38. $list = StoreTemplate::getList($search);
  39. return $this->success($list);
  40. }
  41. /**
  42. * 修改状态
  43. * @param string $id
  44. * @param string $is_ban
  45. * @return Response
  46. */
  47. public function set_ban($id = '')
  48. {
  49. $is_ban = $this->request->post('is_ban', 1);
  50. if (!in_array($is_ban, [1, -1])) {
  51. $this->fail('参数错误');
  52. }
  53. ($is_ban == '' || $id == '') && $this->fail('缺少参数');
  54. if (StoreTemplate::setTemplateBan($id, (int)$is_ban)) {
  55. return $this->success($is_ban == 1 ? '启用成功' : '禁用成功');
  56. } else {
  57. return $this->fail(StoreTemplate::getErrorInfo($is_ban == 1 ? '启用失败' : '禁用失败'));
  58. }
  59. }
  60. /**
  61. * 快速编辑
  62. * @param string $id
  63. * @return Response
  64. * @throws \Exception
  65. */
  66. public function set_template($id)
  67. {
  68. $data = UtilService::postMore([
  69. ['field', 'name', '', '', 'empty_check', '缺少参数'],
  70. ['value', '', '', '', 'empty_check', '缺少参数']
  71. ]);
  72. if (!$id) return $this->fail('缺少参数');
  73. if (StoreTemplate::where('id', $id)->update([$data['field'] => $data['value']]))
  74. return $this->success('保存成功');
  75. else
  76. return $this->fail('保存失败');
  77. }
  78. /**
  79. * 新增/修改模板
  80. * @param int $id
  81. * @return mixed
  82. * @throws DbException
  83. */
  84. public function add_template($id = 0)
  85. {
  86. $data = UtilService::postMore([
  87. ['name', '', '', '', 'empty_check', '请输入模板名'],
  88. ['code', '', '', '', 'empty_check', '请上传模板'],
  89. ['use_version', 1],
  90. ['is_ban', 1],
  91. ['sort', 0],
  92. ]);
  93. if ($id) {
  94. if (!StoreTemplate::vaildWhere()->where('id', $id)->find()) {
  95. return $this->fail('所选模板不存在');
  96. }
  97. }
  98. BaseModel::beginTrans();
  99. try {
  100. if ($id) {
  101. $res = StoreTemplate::edit($data, $id);
  102. } else {
  103. $data['add_time'] = time();
  104. $res = StoreTemplate::create($data);
  105. }
  106. if ($res) {
  107. BaseModel::commitTrans();
  108. return $this->success('操作成功');
  109. } else {
  110. BaseModel::rollbackTrans();
  111. return $this->fail('操作失败');
  112. }
  113. } catch (Exception $e) {
  114. BaseModel::rollbackTrans();
  115. return $this->fail($e->getMessage());
  116. } catch (DbException $e) {
  117. BaseModel::rollbackTrans();
  118. return $this->fail($e->getMessage());
  119. }
  120. }
  121. /**
  122. * 获取指定资源
  123. *
  124. * @param int $id
  125. * @return Response
  126. */
  127. public function get_template($id)
  128. {
  129. return $this->success('ok', StoreTemplate::get($id) ? StoreTemplate::get($id)->toArray() : []);
  130. }
  131. /**
  132. * 删除指定资源
  133. *
  134. * @param int $id
  135. * @return Response
  136. */
  137. public function delete($id)
  138. {
  139. if (!StoreTemplate::delTemplate($id))
  140. return $this->fail(StoreTemplate::getErrorInfo('删除失败,请稍候再试!'));
  141. else
  142. return $this->success('删除成功!');
  143. }
  144. }