StoreCoupon.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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')->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. $coupon = CouponModel::get($id);
  120. if (!$coupon) return Json::fail('数据不存在!');
  121. $f = [];
  122. $f[] = Form::input('title', '优惠券名称', $coupon->getData('title'));
  123. $f[] = Form::number('coupon_price', '优惠券面值', $coupon->getData('coupon_price'))->min(0);
  124. $f[] = Form::number('use_min_price', '优惠券最低消费', $coupon->getData('use_min_price'))->min(0);
  125. $f[] = Form::number('coupon_time', '优惠券有效期限', $coupon->getData('coupon_time'))->min(0);
  126. $f[] = Form::number('sort', '排序', $coupon->getData('sort'));
  127. $f[] = Form::radio('status', '状态', $coupon->getData('status'))->options([['label' => '开启', 'value' => 1], ['label' => '关闭', 'value' => 0]]);
  128. $form = Form::make_post_form('添加优惠券', $f, Url::buildUrl('update', array('id' => $id)));
  129. $this->assign(compact('form'));
  130. return $this->fetch('public/form-builder');
  131. }
  132. /**
  133. * 保存更新的资源
  134. *
  135. * @param $id
  136. */
  137. public function update($id)
  138. {
  139. $data = Util::postMore([
  140. 'title',
  141. 'coupon_price',
  142. 'use_min_price',
  143. 'coupon_time',
  144. 'sort',
  145. ['status', 0]
  146. ]);
  147. if (!$data['title']) return Json::fail('请输入优惠券名称');
  148. if (!$data['coupon_price']) return Json::fail('请输入优惠券面值');
  149. if (!$data['coupon_time']) return Json::fail('请输入优惠券有效期限');
  150. CouponModel::edit($data, $id);
  151. return Json::successful('修改成功!');
  152. }
  153. /**
  154. * 删除指定资源
  155. *
  156. * @param int $id
  157. * @return \think\Response
  158. */
  159. public function delete($id)
  160. {
  161. if (!$id) return Json::fail('数据不存在!');
  162. $data['is_del'] = 1;
  163. if (!CouponModel::edit($data, $id))
  164. return Json::fail(CouponModel::getErrorInfo('删除失败,请稍候再试!'));
  165. else
  166. return Json::successful('删除成功!');
  167. }
  168. /**
  169. * 修改优惠券状态
  170. * @param $id
  171. * @return \think\response\Json
  172. */
  173. public function status($id)
  174. {
  175. if (!$id) return Json::fail('数据不存在!');
  176. if (!CouponModel::editIsDel($id))
  177. return Json::fail(CouponModel::getErrorInfo('修改失败,请稍候再试!'));
  178. else
  179. return Json::successful('修改成功!');
  180. }
  181. /**
  182. * @return mixed
  183. */
  184. public function grant_subscribe()
  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. * @return mixed
  197. */
  198. public function grant_all()
  199. {
  200. $where = Util::getMore([
  201. ['status', ''],
  202. ['title', ''],
  203. ['is_del', 0],
  204. ], $this->request);
  205. $this->assign('where', $where);
  206. $this->assign(CouponModel::systemPageCoupon($where));
  207. return $this->fetch();
  208. }
  209. /**
  210. * @param $id
  211. */
  212. public function grant($id)
  213. {
  214. $where = Util::getMore([
  215. ['status', ''],
  216. ['title', ''],
  217. ['is_del', 0],
  218. ], $this->request);
  219. $nickname = UserModel::where('uid', 'IN', $id)->column('nickname', 'uid');
  220. $this->assign('where', $where);
  221. $this->assign('uid', $id);
  222. $this->assign('nickname', implode(',', $nickname));
  223. $this->assign(CouponModel::systemPageCoupon($where));
  224. return $this->fetch();
  225. }
  226. public function issue($id)
  227. {
  228. if (!CouponModel::be(['id' => $id, 'status' => 1, 'is_del' => 0]))
  229. return $this->failed('发布的优惠劵已失效或不存在!');
  230. $f = [];
  231. $f[] = Form::input('id', '优惠劵ID', $id)->disabled(1);
  232. $f[] = Form::dateTimeRange('range_date', '领取时间')->placeholder('不填为永久有效');
  233. $f[] = Form::radio('is_permanent', '是否不限量', 0)->options([['label' => '限量', 'value' => 0], ['label' => '不限量', 'value' => 1]]);
  234. $f[] = Form::number('count', '发布数量', 0)->min(0)->placeholder('不填或填0,为不限量');
  235. $f[] = Form::radio('is_full_give', '消费满赠', 0)->options([['label' => '开启', 'value' => 1], ['label' => '关闭', 'value' => 0]]);
  236. $f[] = Form::number('full_reduction', '满赠金额')->min(0)->placeholder('赠送优惠券的最低消费金额');
  237. $f[] = Form::radio('is_give_subscribe', '首次关注赠送', 0)->options([['label' => '开启', 'value' => 1], ['label' => '关闭', 'value' => 0]]);
  238. $f[] = Form::radio('status', '状态', 1)->options([['label' => '开启', 'value' => 1], ['label' => '关闭', 'value' => 0]]);
  239. $form = Form::make_post_form('添加优惠券', $f, Url::buildUrl('update_issue', array('id' => $id)));
  240. $this->assign(compact('form'));
  241. return $this->fetch('public/form-builder');
  242. // FormBuilder::text('id','优惠劵ID',$id)->disabled();
  243. // FormBuilder::dateTimeRange('range_date','领取时间')->placeholder('不填为永久有效');
  244. // FormBuilder::text('count','发布数量')->placeholder('不填或填0,为不限量');
  245. // FormBuilder::radio('status','是否开启',[
  246. // ['value'=>1,'label'=>'开启'],
  247. // ['value'=>0,'label'=>'关闭']
  248. // ],1);
  249. // $this->assign(['title'=>'发布优惠券','rules'=>FormBuilder::builder()->getContent(),'action'=>Url::buildUrl('update_issue',array('id'=>$id))]);
  250. // return $this->fetch('public/common_form');
  251. }
  252. public function update_issue($id)
  253. {
  254. list($_id, $rangeTime, $count, $status, $is_permanent, $full_reduction, $is_give_subscribe, $is_full_give) = Util::postMore([
  255. 'id',
  256. ['range_date', ['', '']],
  257. ['count', 0],
  258. ['status', 0],
  259. ['is_permanent', 0],
  260. ['full_reduction', 0],
  261. ['is_give_subscribe', 0],
  262. ['is_full_give', 0]
  263. ], null, true);
  264. if ($_id != $id) return Json::fail('操作失败,信息不对称');
  265. if (!$count) $count = 0;
  266. if (!CouponModel::be(['id' => $id, 'status' => 1, 'is_del' => 0])) return Json::fail('发布的优惠劵已失效或不存在!');
  267. if (count($rangeTime) != 2) return Json::fail('请选择正确的时间区间');
  268. list($startTime, $endTime) = $rangeTime;
  269. // echo $startTime;echo $endTime;var_dump($rangeTime);die;
  270. if (!$startTime) $startTime = 0;
  271. if (!$endTime) $endTime = 0;
  272. if (!$startTime && $endTime) return Json::fail('请选择正确的开始时间');
  273. if ($startTime && !$endTime) return Json::fail('请选择正确的结束时间');
  274. if (StoreCouponIssue::setIssue($id, $count, strtotime($startTime), strtotime($endTime), $count, $status, $is_permanent,$full_reduction, $is_give_subscribe, $is_full_give))
  275. return Json::successful('发布优惠劵成功!');
  276. else
  277. return Json::fail('发布优惠劵失败!');
  278. }
  279. /**
  280. * 给分组用户发放优惠券
  281. */
  282. public function grant_group()
  283. {
  284. $where = Util::getMore([
  285. ['status', ''],
  286. ['title', ''],
  287. ['is_del', 0],
  288. ], $this->request);
  289. $group = UserModel::getUserGroup();
  290. $this->assign('where', $where);
  291. $this->assign('group', json_encode($group));
  292. $this->assign(CouponModel::systemPageCoupon($where));
  293. return $this->fetch();
  294. }
  295. /**
  296. * 给标签用户发放优惠券
  297. */
  298. public function grant_tag()
  299. {
  300. $where = Util::getMore([
  301. ['status', ''],
  302. ['title', ''],
  303. ['is_del', 0],
  304. ], $this->request);
  305. $tag = UserModel::getUserTag();;//获取所有标签
  306. $this->assign('where', $where);
  307. $this->assign('tag', json_encode($tag));
  308. $this->assign(CouponModel::systemPageCoupon($where));
  309. return $this->fetch();
  310. }
  311. }