StoreProductEnsure.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\controller\admin\v1\product\ensure;
  12. use app\controller\admin\AuthController;
  13. use app\Request;
  14. use app\services\product\ensure\StoreProductEnsureServices;
  15. use think\facade\App;
  16. /**
  17. * 商品保障服务
  18. * Class StoreProductEnsure
  19. * @package app\controller\admin\v1\product\ensure
  20. */
  21. class StoreProductEnsure extends AuthController
  22. {
  23. /**
  24. * @var StoreProductEnsureServices
  25. */
  26. protected $services;
  27. /**
  28. * StoreProductEnsure constructor.
  29. * @param App $app
  30. * @param StoreProductEnsureServices $services
  31. */
  32. public function __construct(App $app, StoreProductEnsureServices $services)
  33. {
  34. parent::__construct($app);
  35. $this->services = $services;
  36. }
  37. /**
  38. * 获取保障服务列表
  39. * @param Request $request
  40. * @return mixed
  41. */
  42. public function index(Request $request)
  43. {
  44. $where = $request->postMore([
  45. ['name', '']
  46. ]);
  47. $where['relation_id'] = 0;
  48. $where['type'] = 0;
  49. return $this->success($this->services->getEnsureList($where));
  50. }
  51. /**
  52. * 获取保障服务列表
  53. * @return mixed
  54. * @throws \think\db\exception\DataNotFoundException
  55. * @throws \think\db\exception\DbException
  56. * @throws \think\db\exception\ModelNotFoundException
  57. */
  58. public function allEnsure()
  59. {
  60. return $this->success($this->services->getAllEnsure());
  61. }
  62. /**
  63. * 生成新增表单
  64. * @return mixed
  65. * @throws \FormBuilder\Exception\FormBuilderException
  66. */
  67. public function create()
  68. {
  69. return $this->success($this->services->createForm());
  70. }
  71. /**
  72. * 保存新增保障服务
  73. * @return mixed
  74. */
  75. public function save()
  76. {
  77. $data = $this->request->postMore([
  78. ['name', ''],
  79. ['image', ''],
  80. ['desc', ''],
  81. ['sort', 0],
  82. ]);
  83. if (!$data['name']) {
  84. return $this->fail('请输入保障服务条款');
  85. }
  86. if (!$data['image']) return $this->fail('请上传图标');
  87. if ($this->services->getOne(['name' => $data['name']])) {
  88. return $this->fail('保障服务条款已存在');
  89. }
  90. $data['type'] = 0;
  91. $data['relation_id'] = 0;
  92. $data['add_time'] = time();
  93. $this->services->save($data);
  94. $this->services->cacheUpdate($data);
  95. return $this->success('添加保障服务成功!');
  96. }
  97. /**
  98. * 生成更新表单
  99. * @param $id
  100. * @return mixed
  101. * @throws \FormBuilder\Exception\FormBuilderException
  102. */
  103. public function edit($id)
  104. {
  105. return $this->success($this->services->editForm((int)$id));
  106. }
  107. /**
  108. * 更新保障服务
  109. * @param $id
  110. * @return mixed
  111. */
  112. public function update($id)
  113. {
  114. $data = $this->request->postMore([
  115. ['name', ''],
  116. ['image', ''],
  117. ['desc', ''],
  118. ['sort', 0],
  119. ]);
  120. if (!$data['name']) {
  121. return $this->fail('请输入保障服务条款');
  122. }
  123. if (!$data['image']) return $this->fail('请上传图标');
  124. $cate = $this->services->getOne(['name' => $data['name']]);
  125. if ($cate && $cate['id'] != $id) {
  126. return $this->fail('保障服务条款已存在');
  127. }
  128. $this->services->update($id, $data);
  129. $this->services->cacheUpdate($data);
  130. return $this->success('修改成功!');
  131. }
  132. /**
  133. * 设置保障服务是否显示
  134. * @param $id
  135. * @param $is_show
  136. * @return mixed
  137. */
  138. public function set_show($id, $is_show)
  139. {
  140. ($is_show == '' || $id == '') && $this->fail('缺少参数');
  141. $res = $this->services->update((int)$id, ['status' => (int)$is_show]);
  142. if ($res) {
  143. //更新缓存
  144. if ($is_show) {
  145. $this->services->cacheSaveValue((int)$id, 'status', $is_show);
  146. } else {
  147. $this->services->cacheDelById($id);
  148. }
  149. return $this->success('设置成功');
  150. } else {
  151. return $this->fail('设置失败');
  152. }
  153. }
  154. /**
  155. * 删除保障服务
  156. * @param $id
  157. * @return mixed
  158. */
  159. public function delete($id)
  160. {
  161. if (!$id || !$this->services->count(['id' => $id])) {
  162. return $this->fail('删除的数据不存在');
  163. }
  164. $this->services->delete((int)$id);
  165. $this->services->cacheDelById($id);
  166. return $this->success('删除成功!');
  167. }
  168. }