StoreProductEnsure.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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\store\product\ensure;
  12. use app\controller\store\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\store\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'] = $this->storeId;
  48. $where['type'] = 1;
  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(1, (int)$this->storeId));
  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'] = 1;
  91. $data['relation_id'] = $this->storeId;
  92. $data['add_time'] = time();
  93. $this->services->save($data);
  94. return $this->success('添加保障服务成功!');
  95. }
  96. /**
  97. * 生成更新表单
  98. * @param $id
  99. * @return mixed
  100. * @throws \FormBuilder\Exception\FormBuilderException
  101. */
  102. public function edit($id)
  103. {
  104. return $this->success($this->services->editForm((int)$id));
  105. }
  106. /**
  107. * 更新保障服务
  108. * @param $id
  109. * @return mixed
  110. */
  111. public function update($id)
  112. {
  113. $data = $this->request->postMore([
  114. ['name', ''],
  115. ['image', ''],
  116. ['desc', ''],
  117. ['sort', 0],
  118. ]);
  119. if (!$data['name']) {
  120. return $this->fail('请输入保障服务条款');
  121. }
  122. if (!$data['image']) return $this->fail('请上传图标');
  123. $cate = $this->services->getOne(['name' => $data['name']]);
  124. if ($cate && $cate['id'] != $id) {
  125. return $this->fail('保障服务条款已存在');
  126. }
  127. $this->services->update($id, $data);
  128. return $this->success('修改成功!');
  129. }
  130. /**
  131. * 设置保障服务是否显示
  132. * @param $id
  133. * @param $is_show
  134. * @return mixed
  135. */
  136. public function set_show($id, $is_show)
  137. {
  138. ($is_show == '' || $id == '') && $this->fail('缺少参数');
  139. $res = $this->services->update((int)$id, ['status' => (int)$is_show]);
  140. if ($res) {
  141. return $this->success('设置成功');
  142. } else {
  143. return $this->fail('设置失败');
  144. }
  145. }
  146. /**
  147. * 删除保障服务
  148. * @param $id
  149. * @return mixed
  150. */
  151. public function delete($id)
  152. {
  153. if (!$id || !$this->services->count(['id' => $id])) {
  154. return $this->fail('删除的数据不存在');
  155. }
  156. $this->services->delete((int)$id);
  157. return $this->success('删除成功!');
  158. }
  159. }