StoreCoupon.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. <?php
  2. namespace app\admin\controller\ump;
  3. use app\admin\controller\AuthController;
  4. use app\admin\model\store\StoreCategory as CategoryModel;
  5. use app\admin\model\system\SystemStore;
  6. use think\facade\Route as Url;
  7. use app\admin\model\wechat\WechatUser as UserModel;
  8. use app\admin\model\ump\{StoreCouponIssue, StoreCoupon as CouponModel};
  9. use crmeb\services\{FormBuilder as Form, UtilService as Util, JsonService as Json};
  10. /**
  11. * 优惠券控制器
  12. * Class StoreCategory
  13. * @package app\admin\controller\system
  14. */
  15. class StoreCoupon extends AuthController
  16. {
  17. /**
  18. * @return mixed
  19. */
  20. public function index()
  21. {
  22. $where = Util::getMore([
  23. ['status', ''],
  24. ['title', ''],
  25. ['type','']
  26. ], $this->request);
  27. $this->assign('where', $where);
  28. $this->assign(CouponModel::systemPage($where));
  29. return $this->fetch();
  30. }
  31. /**
  32. * @return mixed
  33. */
  34. public function create()
  35. {
  36. $data = Util::getMore(['type',]);//接收参数
  37. $tab_id = !empty(request()->param('tab_id')) ? request()->param('tab_id') : 1;
  38. //前面通用字段
  39. $f = [];
  40. $f[] = Form::input('title', '优惠券名称');
  41. //不同类型不同字段
  42. $formbuider = [];
  43. switch ($data['type']) {
  44. case 1://品类券
  45. $formbuider = CouponModel::createClassRule($tab_id);
  46. break;
  47. case 2://商品券
  48. $formbuider = CouponModel::createProductRule($tab_id);
  49. break;
  50. }
  51. //后面通用字段
  52. $formbuiderfoot = array();
  53. $formbuiderfoot[] = Form::number('coupon_price', '优惠券面值', 0)->min(0);
  54. $formbuiderfoot[] = Form::selectOne('store_id', '绑定商家', 0)
  55. ->options(SystemStore::field('id as value,name as label')->where('is_show', 1)->where('is_del', 0)->select()->toArray())->filterable(1)->col(10);
  56. // $formbuiderfoot[] = Form::number('use_min_price', '最低消费')->min(0);
  57. // $formbuiderfoot[] = Form::number('coupon_time', '有效期限')->min(0);
  58. $formbuiderfoot[] = Form::number('sort', '排序');
  59. $formbuiderfoot[] = Form::hidden('type', $data['type']);
  60. $formbuiderfoot[] = Form::dateTimeRange('range_date', '使用时间')->placeholder('不填为永久有效');
  61. $formbuiderfoot[] = Form::radio('status', '状态', 0)->options([['label' => '开启', 'value' => 1], ['label' => '关闭', 'value' => 0]])->value(1);
  62. $formbuiders = array_merge($f, $formbuider, $formbuiderfoot);
  63. $form = Form::make_post_form('添加优惠券', $formbuiders, Url::buildUrl('save'));
  64. $this->assign(compact('form'));
  65. $this->assign('get', request()->param());
  66. return $this->fetch();
  67. }
  68. /**
  69. * 选择商品
  70. * @param int $id
  71. */
  72. public function select()
  73. {
  74. return $this->fetch();
  75. }
  76. /**
  77. * 保存
  78. */
  79. public function save()
  80. {
  81. $data = Util::postMore([
  82. 'title',
  83. ['product_id', []],
  84. ['category_id', 0],
  85. 'coupon_price',
  86. [ 'use_min_price', 0],
  87. 'sort',
  88. ['status', 0],
  89. ['type', 0],
  90. ['range_date', ['', '']],
  91. ['store_id']
  92. ]);
  93. $rangeTime = $data['range_date'];
  94. if (count($rangeTime) != 2) return Json::fail('请选择正确的时间区间');
  95. list($startTime, $endTime) = $rangeTime;
  96. // echo $startTime;echo $endTime;var_dump($rangeTime);die;
  97. if (!$startTime) $startTime = 0;
  98. if (!$endTime) $endTime = 0;
  99. if (!$startTime && $endTime) return Json::fail('请选择正确的开始时间');
  100. if ($startTime && !$endTime) return Json::fail('请选择正确的结束时间');
  101. if (!in_array($data['type'],[0,1,2])) return Json::fail('优惠券类型有误');
  102. $data['start_time'] = $startTime != 0 ? strtotime($startTime) : 0;
  103. $data['end_time'] = $endTime!= 0 ? strtotime($endTime) : 0;;
  104. if (!$data['title']) return Json::fail('请输入优惠券名称');
  105. if (!$data['coupon_price']) return Json::fail('请输入优惠券面值');
  106. $data['product_id'] = implode(',', $data['product_id']);
  107. $data['add_time'] = time();
  108. CouponModel::create($data);
  109. return Json::successful('添加优惠券成功!');
  110. }
  111. /**
  112. * 显示编辑资源表单页.
  113. * @param $id
  114. * @return string|void
  115. * @throws \FormBuilder\exception\FormBuilderException
  116. */
  117. public function edit($id)
  118. {
  119. $details = \app\admin\model\ump\StoreCoupon::find($id);
  120. $data = Util::getMore(['type',]);//接收参数
  121. $tab_id = !empty(request()->param('tab_id')) ? request()->param('tab_id') : 1;
  122. //前面通用字段
  123. $f = [];
  124. $f[] = Form::input('title', '优惠券名称', $details->getData('title'));
  125. //不同类型不同字段
  126. $formbuider = [];
  127. //后面通用字段
  128. $formbuiderfoot = array();
  129. $formbuiderfoot[] = Form::number('coupon_price', '优惠券面值', $details->getData('coupon_price'))->min(0);
  130. $formbuiderfoot[] = Form::selectOne('store_id', '绑定商家', json_encode($details->getData('store_id')))
  131. ->options(SystemStore::field('id as value,name as label')->where('is_show', 1)->where('is_del', 0)->select()->toArray())->filterable(1)->col(10);
  132. // $formbuiderfoot[] = Form::number('use_min_price', '最低消费')->min(0);
  133. // $formbuiderfoot[] = Form::number('coupon_time', '有效期限')->min(0);
  134. $formbuiderfoot[] = Form::number('sort', '排序', $details->getData('sort'));
  135. $formbuiderfoot[] = Form::hidden('type', $data['type']);
  136. $formbuiderfoot[] = Form::hidden('id',$id);
  137. $formbuiderfoot[] = Form::dateTimeRange('range_date', '使用时间',date('Y-m-d H:i:s',$details->getData('start_time')), date('Y-m-d H:i:s',$details->getData('end_time')))->placeholder('不填为永久有效');
  138. $formbuiderfoot[] = Form::radio('status', '状态', $details->getData('status'))->options([['label' => '开启', 'value' => 1], ['label' => '关闭', 'value' => 0]])->value(1);
  139. $formbuiders = array_merge($f, $formbuider, $formbuiderfoot);
  140. $form = Form::make_post_form('添加优惠券', $formbuiders, Url::buildUrl('update'));
  141. $this->assign(compact('form'));
  142. return $this->fetch('public/form-builder');
  143. }
  144. /**
  145. * 保存更新的资源
  146. *
  147. * @param $id
  148. */
  149. public function update($id)
  150. {
  151. $data = Util::postMore([
  152. 'id',
  153. 'title',
  154. ['product_id', []],
  155. ['category_id', 0],
  156. 'coupon_price',
  157. [ 'use_min_price', 0],
  158. 'sort',
  159. ['status', 0],
  160. ['type', 0],
  161. ['range_date', ['', '']],
  162. ['store_id']
  163. ]);
  164. $rangeTime = $data['range_date'];
  165. if (count($rangeTime) != 2) return Json::fail('请选择正确的时间区间');
  166. list($startTime, $endTime) = $rangeTime;
  167. // echo $startTime;echo $endTime;var_dump($rangeTime);die;
  168. if (!$startTime) $startTime = 0;
  169. if (!$endTime) $endTime = 0;
  170. if (!$startTime && $endTime) return Json::fail('请选择正确的开始时间');
  171. if ($startTime && !$endTime) return Json::fail('请选择正确的结束时间');
  172. if (!in_array($data['type'],[0,1,2])) return Json::fail('优惠券类型有误');
  173. if (!$data['title']) return Json::fail('请输入优惠券名称');
  174. if (!$data['coupon_price']) return Json::fail('请输入优惠券面值');
  175. $details = CouponModel::find($data['id']);
  176. unset($data['id']);
  177. $details['start_time'] = $startTime != 0 ? strtotime($startTime) : 0;
  178. $details['end_time'] = $endTime != 0 ? strtotime($endTime) : 0;
  179. $details['title'] = $data['title'];
  180. $details['category_id'] = $data['category_id'];
  181. $details['coupon_price'] = $data['coupon_price'];
  182. $details['use_min_price'] = $data['use_min_price'];
  183. $details['sort'] = $data['sort'];
  184. $details['status'] = $data['status'];
  185. $details['type'] = $data['type'];
  186. $details['store_id'] = $data['store_id'];
  187. $details['product_id'] = implode(',', $data['product_id']);
  188. $details->save();
  189. return Json::success('修改优惠券成功!');
  190. }
  191. /**
  192. * 删除指定资源
  193. *
  194. * @param int $id
  195. * @return \think\Response
  196. */
  197. public function delete($id)
  198. {
  199. if (!$id) return Json::fail('数据不存在!');
  200. $data['is_del'] = 1;
  201. if (!CouponModel::edit($data, $id))
  202. return Json::fail(CouponModel::getErrorInfo('删除失败,请稍候再试!'));
  203. else
  204. return Json::successful('删除成功!');
  205. }
  206. /**
  207. * 修改优惠券状态
  208. * @param $id
  209. * @return \think\response\Json
  210. */
  211. public function status($id)
  212. {
  213. if (!$id) return Json::fail('数据不存在!');
  214. if (!CouponModel::editIsDel($id))
  215. return Json::fail(CouponModel::getErrorInfo('修改失败,请稍候再试!'));
  216. else
  217. return Json::successful('修改成功!');
  218. }
  219. /**
  220. * @return mixed
  221. */
  222. public function grant_subscribe()
  223. {
  224. $where = Util::getMore([
  225. ['status', ''],
  226. ['title', ''],
  227. ['is_del', 0],
  228. ], $this->request);
  229. $this->assign('where', $where);
  230. $this->assign(CouponModel::systemPageCoupon($where));
  231. return $this->fetch();
  232. }
  233. /**
  234. * @return mixed
  235. */
  236. public function grant_all()
  237. {
  238. $where = Util::getMore([
  239. ['status', ''],
  240. ['title', ''],
  241. ['is_del', 0],
  242. ], $this->request);
  243. $this->assign('where', $where);
  244. $this->assign(CouponModel::systemPageCoupon($where));
  245. return $this->fetch();
  246. }
  247. /**
  248. * @param $id
  249. */
  250. public function grant($id)
  251. {
  252. $where = Util::getMore([
  253. ['status', ''],
  254. ['title', ''],
  255. ['is_del', 0],
  256. ], $this->request);
  257. $nickname = UserModel::where('uid', 'IN', $id)->column('nickname', 'uid');
  258. $this->assign('where', $where);
  259. $this->assign('uid', $id);
  260. $this->assign('nickname', implode(',', $nickname));
  261. $this->assign(CouponModel::systemPageCoupon($where));
  262. return $this->fetch();
  263. }
  264. public function issue($id)
  265. {
  266. if (!CouponModel::be(['id' => $id, 'status' => 1, 'is_del' => 0]))
  267. return $this->failed('发布的优惠劵已失效或不存在!');
  268. $f = [];
  269. $f[] = Form::input('id', '优惠劵ID', $id)->disabled(1);
  270. $f[] = Form::dateTimeRange('range_date', '领取时间')->placeholder('不填为永久有效');
  271. $f[] = Form::radio('is_permanent', '是否不限量', 0)->options([['label' => '限量', 'value' => 0], ['label' => '不限量', 'value' => 1]]);
  272. $f[] = Form::number('count', '发布数量', 0)->min(0)->placeholder('不填或填0,为不限量');
  273. $f[] = Form::radio('is_full_give', '消费满赠', 0)->options([['label' => '开启', 'value' => 1], ['label' => '关闭', 'value' => 0]]);
  274. $f[] = Form::number('full_reduction', '满赠金额')->min(0)->placeholder('赠送优惠券的最低消费金额');
  275. $f[] = Form::radio('is_give_subscribe', '首次关注赠送', 0)->options([['label' => '开启', 'value' => 1], ['label' => '关闭', 'value' => 0]]);
  276. $f[] = Form::radio('status', '状态', 1)->options([['label' => '开启', 'value' => 1], ['label' => '关闭', 'value' => 0]]);
  277. $form = Form::make_post_form('添加优惠券', $f, Url::buildUrl('update_issue', array('id' => $id)));
  278. $this->assign(compact('form'));
  279. return $this->fetch('public/form-builder');
  280. // FormBuilder::text('id','优惠劵ID',$id)->disabled();
  281. // FormBuilder::dateTimeRange('range_date','领取时间')->placeholder('不填为永久有效');
  282. // FormBuilder::text('count','发布数量')->placeholder('不填或填0,为不限量');
  283. // FormBuilder::radio('status','是否开启',[
  284. // ['value'=>1,'label'=>'开启'],
  285. // ['value'=>0,'label'=>'关闭']
  286. // ],1);
  287. // $this->assign(['title'=>'发布优惠券','rules'=>FormBuilder::builder()->getContent(),'action'=>Url::buildUrl('update_issue',array('id'=>$id))]);
  288. // return $this->fetch('public/common_form');
  289. }
  290. public function update_issue($id)
  291. {
  292. list($_id, $rangeTime, $count, $status, $is_permanent, $full_reduction, $is_give_subscribe, $is_full_give) = Util::postMore([
  293. 'id',
  294. ['range_date', ['', '']],
  295. ['count', 0],
  296. ['status', 0],
  297. ['is_permanent', 0],
  298. ['full_reduction', 0],
  299. ['is_give_subscribe', 0],
  300. ['is_full_give', 0]
  301. ], null, true);
  302. if ($_id != $id) return Json::fail('操作失败,信息不对称');
  303. if (!$count) $count = 0;
  304. if (!CouponModel::be(['id' => $id, 'status' => 1, 'is_del' => 0])) return Json::fail('发布的优惠劵已失效或不存在!');
  305. if (count($rangeTime) != 2) return Json::fail('请选择正确的时间区间');
  306. list($startTime, $endTime) = $rangeTime;
  307. // echo $startTime;echo $endTime;var_dump($rangeTime);die;
  308. if (!$startTime) $startTime = 0;
  309. if (!$endTime) $endTime = 0;
  310. if (!$startTime && $endTime) return Json::fail('请选择正确的开始时间');
  311. if ($startTime && !$endTime) return Json::fail('请选择正确的结束时间');
  312. if (StoreCouponIssue::setIssue($id, $count, strtotime($startTime), strtotime($endTime), $count, $status, $is_permanent,$full_reduction, $is_give_subscribe, $is_full_give))
  313. return Json::successful('发布优惠劵成功!');
  314. else
  315. return Json::fail('发布优惠劵失败!');
  316. }
  317. /**
  318. * 给分组用户发放优惠券
  319. */
  320. public function grant_group()
  321. {
  322. $where = Util::getMore([
  323. ['status', ''],
  324. ['title', ''],
  325. ['is_del', 0],
  326. ], $this->request);
  327. $group = UserModel::getUserGroup();
  328. $this->assign('where', $where);
  329. $this->assign('group', json_encode($group));
  330. $this->assign(CouponModel::systemPageCoupon($where));
  331. return $this->fetch();
  332. }
  333. /**
  334. * 给标签用户发放优惠券
  335. */
  336. public function grant_tag()
  337. {
  338. $where = Util::getMore([
  339. ['status', ''],
  340. ['title', ''],
  341. ['is_del', 0],
  342. ], $this->request);
  343. $tag = UserModel::getUserTag();;//获取所有标签
  344. $this->assign('where', $where);
  345. $this->assign('tag', json_encode($tag));
  346. $this->assign(CouponModel::systemPageCoupon($where));
  347. return $this->fetch();
  348. }
  349. }