StoreCoupon.php 12 KB

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