Gmcard.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. /**
  3. * 商家储值管理
  4. */
  5. namespace app\ais\controller\api\v1;
  6. use app\ais\controller\api\Base;
  7. use app\ais\model\AisCoupon;
  8. use app\ais\model\AisStore;
  9. use app\ais\model\AisCard;
  10. use app\ais\model\AisCouponUser;
  11. use app\ais\model\AisStoreUnion;
  12. use filter\Filter;
  13. use think\Db;
  14. class Gmcard extends Base{
  15. protected $store;
  16. public function initialize() {
  17. parent::initialize();
  18. $this->isUserAuth();
  19. $this->store = AisStore::manageStore($this->user->id);
  20. if(empty($this->store)){
  21. exit(json_encode(['code' => 403,'msg'=>'无法找到该商家']));
  22. }
  23. }
  24. /**
  25. * 用户已领优惠券
  26. * @return void
  27. */
  28. public function index(){
  29. $this->apiSign();
  30. $condition[] = ['member_miniapp_id','=',$this->member_miniapp_id];
  31. $condition[] = ['store_id','=',$this->store->id];
  32. $condition[] = ['is_end','=',0];
  33. $card = AisCard::with(['coupon' => function($query) {
  34. $query->field('id,amount,num,howmuch');
  35. }])->withCount('carduser')->withSum('cardorder','amount')->field('id,locktime,name,price,store_id,create_time,coupon_id')->where($condition)->select();
  36. if ($card->isEmpty()) {
  37. return enjson(204);
  38. }
  39. return enjson(200,$card);
  40. }
  41. /**
  42. * 读取活动优惠券
  43. */
  44. public function storeCardInfo(){
  45. $param['id'] = $this->request->param('id/d');
  46. $this->apiSign($param);
  47. $card = AisCard::where(['id' => $param['id']])->find();
  48. if (empty($card)) {
  49. return enjson(204);
  50. }
  51. $data = [
  52. 'is_lock' => $card->is_lock,
  53. 'name' => $card->name,
  54. 'price' => intval($card->price),
  55. 'num' => $card->coupon->num,
  56. 'locktime' => date('Y/m/d',$card->locktime),
  57. 'content' => htmlspecialchars_decode($card->tips),
  58. 'coupon' =>[
  59. 'howmuch' => $card->coupon->howmuch,
  60. 'amount' => intval($card->coupon->amount),
  61. 'name' => $card->coupon->name,
  62. 'tips' => $card->coupon->tips,
  63. ]
  64. ];
  65. return enjson(200,$data);
  66. }
  67. /**
  68. * 以下商家管理
  69. * ##########################################
  70. * @return void
  71. * 添加和修改优惠券
  72. */
  73. public function edit(){
  74. if(request()->isPost()){
  75. $param['id'] = $this->request->param('id');
  76. $param['name'] = $this->request->param('name');
  77. $param['price'] = $this->request->param('price/f',0);
  78. $param['amount'] = $this->request->param('amount/d',0);
  79. $param['howmuch'] = $this->request->param('howmuch/f',0);
  80. $param['num'] = $this->request->param('num/d');
  81. $param['locktime'] = $this->request->param('locktime/s');
  82. $param['tips'] = $this->request->param('tips/s');
  83. $param['content'] = $this->request->param('content/s');
  84. $this->apiSign($param);
  85. $validate = $this->validate($param,'Card.api');
  86. if(true !== $validate){
  87. return enjson(403,$validate);
  88. }
  89. //优惠券数据
  90. $coupon['types'] = 3;
  91. $coupon['is_lock'] = 1;
  92. $coupon['name'] = $param['name'];
  93. $coupon['amount'] = $param['amount'];
  94. $coupon['howmuch'] = $param['howmuch'];
  95. $coupon['tips'] = $param['tips'];
  96. $coupon['num'] = $param['num'];
  97. $coupon['member_miniapp_id'] = $this->member_miniapp_id;
  98. $coupon['store_id'] = $this->store->id;
  99. $coupon['citycode'] = $this->store->citycode;
  100. $coupon['update_time'] = time();
  101. $coupon['create_time'] = time();
  102. //判断是添加还是修改
  103. $card_rel = [];
  104. if(!empty($param['id'])){
  105. $card_rel = AisCard::where(['id' => $param['id']])->find();
  106. }
  107. if(empty($card_rel)){
  108. $coupon_rel = AisCoupon::create($coupon);
  109. if(!$coupon_rel){
  110. return enjson(403,'储值活动创建失败');
  111. }
  112. $coupon_id = $coupon_rel->id;
  113. }else{
  114. $coupon_id = $card_rel->coupon_id;
  115. $coupon_rel = AisCoupon::where(['id' => $card_rel->coupon_id])->update($coupon);
  116. if(!$coupon_rel){
  117. return enjson(403,'未找到储值活动');
  118. }
  119. }
  120. //判断是修改储值还是增加
  121. $card['store_id'] = $this->store->id;
  122. $card['member_miniapp_id'] = $this->member_miniapp_id;
  123. $card['name'] = $param['name'];
  124. $card['price'] = $param['price'];
  125. $card['tips'] = htmlspecialchars(base64_decode($param['content']));
  126. $card['coupon_id'] = $coupon_id;
  127. $card['locktime'] = strtotime($param['locktime']);
  128. $card['update_time'] = time();
  129. $card['create_time'] = time();
  130. if(empty($card_rel)){
  131. $rel = AisCard::create($card);
  132. }else{
  133. $rel = AisCard::where(['id' => $card_rel->id])->update($card);
  134. }
  135. if($rel){
  136. return enjson(200,'储值活动创建成功');
  137. }
  138. return enjson(403,'未找到储值活动');
  139. }
  140. }
  141. //删除储值
  142. public function storeCardDel(){
  143. $param['id'] = $this->request->param('id/d',0);
  144. $this->apiSign($param);
  145. $card = AisCard::where(['member_miniapp_id' => $this->miniapp_id,'store_id' => $this->store->id,'id' => $param['id']])->find();
  146. if (empty($card)) {
  147. return enjson(404,'未找到储值活动');
  148. }
  149. $card->is_end = 1;
  150. $card->save();
  151. AisCoupon::where(['id' => $card->coupon_id])->update(['is_end' => 1]);
  152. return enjson(200,'删除成功');
  153. }
  154. }