StoreTryProduct.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. /**
  3. * Created by PhpStorm
  4. * Author: 向往那片天空
  5. * Date: 2020/6/8
  6. * Time: 16:21
  7. * 微信/QQ: 250023777
  8. * 格言: 抓住中心,宁精勿杂,宁专勿多
  9. */
  10. namespace app\admin\controller\ump;
  11. use app\admin\controller\AuthController;
  12. use app\Request;
  13. use crmeb\services\FormBuilder as Form;
  14. use crmeb\services\JsonService;
  15. use crmeb\services\UtilService;
  16. use app\admin\model\ump\StoreActivity as StoreActivityModel;
  17. use think\facade\Route as Url;
  18. use app\admin\model\ump\StoreTryProduct as StoreTryProductModel;
  19. use app\admin\model\store\StoreCategory;
  20. use app\admin\model\ump\StoreTryRecord;
  21. class StoreTryProduct extends AuthController
  22. {
  23. public function index(Request $request)
  24. {
  25. $this->assign('countCombination', StoreTryProductModel::where('is_del=0')->count());
  26. $this->assign('combinationId', StoreTryProductModel::getCombinationIdAll());
  27. return $this->fetch();
  28. }
  29. /**
  30. * 异步获取活动数据
  31. */
  32. public function get_combination_list()
  33. {
  34. $where = UtilService::getMore([
  35. ['page', 1],
  36. ['limit', 20],
  37. ['export', 0],
  38. ['is_show', ''],
  39. ['store_name', ''],
  40. ['is_finish', '']
  41. ]);
  42. $combinationList = StoreTryProductModel::systemPage($where);
  43. if (is_object($combinationList['list'])) $combinationList['list'] = $combinationList['list']->toArray();
  44. $data = $combinationList['list']['data'];
  45. foreach ($data as &$value) {
  46. $value['start_time'] = date('Y-m-d H:i:s', $value['start_time']);
  47. $value['stop_time'] = date('Y/m/d H:i:s', $value['stop_time']);
  48. $value['add_time'] = date('Y-m-d H:i:s', $value['add_time']);
  49. $value['join_people'] = StoreTryRecord::where('tid', $value['id'])->where('is_del=0')->count();
  50. }
  51. $count = StoreTryProductModel::setWhere($where)->count();
  52. return JsonService::successlayui(['data' => $data, 'count' => $count]);
  53. }
  54. public function set_show(Request $request)
  55. {
  56. $param = UtilService::getMore([
  57. 'id',
  58. 'is_show'
  59. ], $request);
  60. if (!$param['id']) return JsonService::fail('参数错误');
  61. StoreTryProductModel::update([
  62. 'id' => $param['id'],
  63. 'is_show' => $param['is_show']
  64. ]);
  65. return JsonService::success("修改成功");
  66. }
  67. /**
  68. * 删除指定资源
  69. *
  70. * @param int $id
  71. * @return \think\Response
  72. */
  73. public function delete($id)
  74. {
  75. if (!$id) return $this->failed('数据不存在');
  76. $product = StoreTryProductModel::get($id);
  77. if (!$product) return JsonService::fail('数据不存在!');
  78. if ($product['is_del']) return JsonService::fail('已删除!');
  79. $data['is_del'] = 1;
  80. if (!StoreTryProductModel::edit($data, $id))
  81. return JsonService::fail(StoreTryProductModel::getErrorInfo('删除失败,请稍候再试!'));
  82. else
  83. return JsonService::successful('删除成功!');
  84. }
  85. /**
  86. * 显示创建资源表单页.
  87. *
  88. * @return \think\Response
  89. */
  90. public function create(Request $request)
  91. {
  92. $f = [];
  93. $f[] = Form::frameImageOne('product', '选择商品', Url::buildUrl('productList', array('fodder' => 'product')))->icon('plus')->width('100%')->height('500px');
  94. $f[] = Form::hidden('product_id', '');
  95. $f[] = Form::input('store_name', '商品名称');
  96. $f[] = Form::input('explain', '试用说明')->type('textarea');
  97. $f[] = Form::dateTimeRange('section_time', '开始结束时间');
  98. $f[] = Form::frameImageOne('image', '商品主图片(305*305px)', Url::buildUrl('admin/widget.images/index', array('fodder' => 'image')))->icon('image')->width('100%')->height('500px');
  99. $f[] = Form::number('quota', '试用人数', 2)->min(2)->col(12);
  100. $f[] = Form::number('sort', '排序', 1)->min(2)->col(12);
  101. $form = Form::make_post_form('添加用户通知', $f, Url::buildUrl('save'));
  102. $this->assign(compact('form'));
  103. return $this->fetch('public/form-builder');
  104. }
  105. /**
  106. * 获取商品列表
  107. * @return string
  108. * @throws \Exception
  109. */
  110. public function productList()
  111. {
  112. $cate = StoreCategory::getTierList(null, 1);
  113. $this->assign('cate', $cate);
  114. return $this->fetch();
  115. }
  116. /**
  117. * 保存新建的资源
  118. * @param int $id
  119. */
  120. public function save($id = 0)
  121. {
  122. $data = UtilService::postMore([
  123. 'product_id',
  124. 'store_name',
  125. ['image', ''],
  126. ['explain', ''],
  127. ['section_time', ''],
  128. ['quota', 0],
  129. ['sort', 1],
  130. ]);
  131. if (!$data['store_name']) return JsonService::fail('请输入商品名称');
  132. if (!$data['explain']) return JsonService::fail('请输入试用说明');
  133. if (!$data['image']) return JsonService::fail('请上传商品图片');
  134. if (count($data['section_time']) < 1) return JsonService::fail('请选择活动时间');
  135. $data['start_time'] = strtotime($data['section_time'][0]);
  136. $data['stop_time'] = strtotime($data['section_time'][1]);
  137. unset($data['section_time']);
  138. if ($id) {
  139. $product = StoreTryProductModel::get($id);
  140. if (!$product) return JsonService::fail('数据不存在!');
  141. $data['product_id'] = $product['product_id'];
  142. StoreTryProductModel::edit($data, $id);
  143. return JsonService::successful('编辑成功!');
  144. } else {
  145. $data['add_time'] = time();
  146. StoreTryProductModel::create($data);
  147. return JsonService::successful('添加活动成功!');
  148. }
  149. }
  150. /**
  151. * 显示编辑资源表单页.
  152. *
  153. * @param int $id
  154. * @return \think\Response
  155. */
  156. public function edit($id)
  157. {
  158. if (!$id) return $this->failed('数据不存在');
  159. $product = StoreTryProductModel::get($id);
  160. if (!$product) return JsonService::fail('数据不存在!');
  161. $f = [];
  162. $f[] = Form::input('product_id', '产品ID', $product->getData('product_id'))->disabled(true);
  163. $f[] = Form::input('store_name', '商品名称', $product->getData('store_name'));
  164. $f[] = Form::input('explain', '试用说明', $product->getData('explain'))->type('textarea');
  165. $f[] = Form::dateTimeRange('section_time', '活动时间', date("Y-m-d H:i:s", $product->getData('start_time')), date("Y-m-d H:i:s", $product->getData('stop_time')));
  166. $f[] = Form::frameImageOne('image', '商品主图片(305*305px)', Url::buildUrl('admin/widget.images/index', array('fodder' => 'image')), $product->getData('image'))->icon('image')->width('100%')->height('500px');
  167. $f[] = Form::number('quota', '试用人数', $product['quota'])->min(2)->col(12);
  168. $f[] = Form::number('sort', '排序', $product['sort'])->min(2)->col(12);
  169. $form = Form::make_post_form('添加用户通知', $f, Url::buildUrl('save', compact('id')));
  170. $this->assign(compact('form'));
  171. return $this->fetch('public/form-builder');
  172. }
  173. public function finish(Request $request)
  174. {
  175. $param = UtilService::getMore([
  176. 'id'
  177. ], $request);
  178. StoreTryProductModel::where("id={$param['id']}")->update([
  179. 'is_finish' => 1,
  180. ]);
  181. return JsonService::successful('结束成功!');
  182. }
  183. }