CouponController.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. namespace app\admin\controller;
  3. use think\exception\ValidateException;
  4. use app\model\Coupon;
  5. use app\model\Category;
  6. use app\model\Goods;
  7. class CouponController extends Base
  8. {
  9. function index()
  10. {
  11. $weid = weid();
  12. $page = input('post.page', 1, 'intval');
  13. $keyword = input('post.keyword', '', 'serach_in');
  14. $status = input('post.status', '', 'serach_in');
  15. $query = Coupon::where(['weid' => $weid]);
  16. if (!empty($keyword)) {
  17. $query->where('name', 'like', '%' . $keyword . '%');
  18. }
  19. if (!empty($status) || $status === "0") {
  20. $query->where(['status' => $status]);
  21. }
  22. if (!empty($this->sid)) {
  23. $query->where('sid', $this->sid);
  24. }
  25. $res = $query->order('sort asc,id desc')
  26. ->paginate(getpage())
  27. ->toArray();
  28. foreach ($res['data'] as &$vo) {
  29. if ($vo['coupon_type'] == 10) {
  30. $vo['couponway'] = '满:' . $vo['min_price'] . '元,减:' . $vo['reduce_price'] . '元';
  31. } elseif ($vo['coupon_type'] == 20) {
  32. $vo['couponway'] = '满:' . $vo['min_price'] . '元,打:' . $vo['discount'] . '折';
  33. }
  34. if ($vo['expire_type'] == 10) {
  35. if ($vo['expire_day'] > 0) {
  36. $vo['expire_day'] = $vo['expire_day'] . "天";
  37. } else {
  38. $vo['expire_day'] = "长期";
  39. }
  40. $vo['expire_type'] = '领取后:' . $vo['expire_day'] . '有效';;
  41. } elseif ($vo['expire_type'] == 20) {
  42. $vo['expire_type'] = '有效期:' . time_format($vo['start_time']) . '到' . time_format($vo['end_time']);
  43. }
  44. $vo['ptype'] = getCouponPtype($vo['ptype']);
  45. $vo['coupon_type'] = getCouponType($vo['coupon_type']);
  46. $vo['color'] = getColor($vo['color']);
  47. }
  48. $data['data'] = $res;
  49. return $this->json($data);
  50. }
  51. function listUpdate()
  52. {
  53. $data = only('id,status,sort');
  54. if (!$data['id']) throw new ValidateException('参数错误');
  55. Coupon::update($data);
  56. return $this->json(['msg' => '操作成功']);
  57. }
  58. public function update()
  59. {
  60. $id = $this->request->post('id');
  61. $data = input('post.');
  62. unset($data['create_time']);
  63. $data['start_time'] = xm_strtotime($data['start_time']);
  64. $data['end_time'] = xm_strtotime($data['end_time']);
  65. if (empty($id)) {
  66. $data['weid'] = weid();
  67. if (!empty($this->sid)) {
  68. $data['sid'] = $this->sid;
  69. }
  70. try {
  71. $res = Coupon::create($data);
  72. if ($res->id && empty($data['sort'])) {
  73. Coupon::update(['sort' => $res->id, 'id' => $res->id]);
  74. }
  75. } catch (\Exception $e) {
  76. throw new ValidateException($e->getMessage());
  77. }
  78. return $this->json(['msg' => '添加成功', 'data' => $res->id]);
  79. } else {
  80. try {
  81. Coupon::update($data);
  82. } catch (\Exception $e) {
  83. throw new ValidateException($e->getMessage());
  84. }
  85. return $this->json(['msg' => '修改成功']);
  86. }
  87. }
  88. function getInfo()
  89. {
  90. $id = $this->request->post('id', '', 'serach_in');
  91. if (!$id) throw new ValidateException('参数错误');
  92. $data = Coupon::field('*')->find($id)->toArray();
  93. if ($data['use_goods']==2 && !empty($data['goods_ids'])) {
  94. $goods = Goods::find($data['goods_ids']);
  95. if (!empty($goods)) {
  96. $data['goods'] = $goods->toArray();
  97. }
  98. }else{
  99. $data['goods'] = [];
  100. }
  101. if (empty($data['start_time'])) {
  102. $data['start_time'] = "";
  103. } else {
  104. $data['start_time'] = time_ymd($data['start_time']);
  105. }
  106. if (empty($data['end_time'])) {
  107. $data['end_time'] = "";
  108. } else {
  109. $data['end_time'] = time_ymd($data['end_time']);
  110. }
  111. return $this->json(['data' => $data]);
  112. }
  113. function delete()
  114. {
  115. return $this->del(new Coupon());
  116. }
  117. function getField()
  118. {
  119. $data['colorarray'] = getColor();
  120. $data['ptypearray'] = getCouponPtype();
  121. $data['coupon_typearray'] = getCouponType();
  122. $data['expire_typearray'] = getExpireType();
  123. $data['cat_idsarray'] = _generateSelectTree(Category::getpcarray());
  124. return $this->json(['data' => $data]);
  125. }
  126. }