Coupon.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 https://www.sapixx.com All rights reserved.
  4. * @license Licensed (http://www.apache.org/licenses/LICENSE-2.0).
  5. * @author pillar<ltmn@qq.com>
  6. * 优惠券接口
  7. */
  8. namespace app\allwin\controller\api\v4;
  9. use app\allwin\controller\api\Base;
  10. use app\allwin\model\Coupon as ModelCoupon;
  11. use app\allwin\model\CouponUser;
  12. use app\allwin\model\CouponOrder;
  13. use app\allwin\model\AllwinConfig;
  14. use app\common\model\SystemMemberForm;
  15. use app\common\facade\WechatPay;
  16. class Coupon extends Base{
  17. /**
  18. * 读取单个优惠券信息
  19. **/
  20. public function getCoupon(){
  21. $param['id'] = $this->request->param('id/d');
  22. $param['signkey'] = $this->request->param('signkey');
  23. $param['sign'] = $this->request->param('sign');
  24. $rel = $this->apiSign($param);
  25. if($rel['code'] != 200){
  26. return enjson($rel['code'],'签名验证失败');
  27. }
  28. $condition['member_miniapp_id'] = $this->miniapp_id;
  29. $condition['id'] = $param['id'];
  30. $coupon = ModelCoupon::where($condition)->find();
  31. if (empty($coupon)) {
  32. return enjson(204);
  33. }
  34. //判断用户已领取优惠券
  35. $user_coupon_id = [];
  36. if ($this->user) {
  37. $ids = CouponUser::userCouponIds($this->user->id);
  38. if (!empty($ids)) {
  39. $user_coupon_id = $ids;
  40. }
  41. }
  42. $data['id'] = $coupon['id'];
  43. $data['store_id'] = $coupon['store_id'];
  44. $data['types'] = $coupon['types'];
  45. $data['name'] = $coupon['name'];
  46. $data['size'] = $coupon['size'];
  47. $data['sizes'] = round($coupon['size']);
  48. $data['price'] = $coupon['price'];
  49. $data['amount'] = $coupon['types'] ? $coupon['discount'].'折':round($coupon['price']).'元';
  50. $data['discount'] = $coupon['discount'];
  51. $data['howmuch'] = $coupon['howmuch'];
  52. $data['img'] = $coupon['img'];
  53. $data['weekday'] = implode(',',weekdays(json_decode($coupon['weekday'])));
  54. $data['starttime'] = date('Y-m-d',$coupon['starttime']);
  55. $data['endtime'] = date('Y-m-d',$coupon['endtime']);
  56. $data['tips'] = $coupon['tips'];
  57. $data['state'] = in_array($coupon['id'],$user_coupon_id) ? 1 : 0;
  58. $data['rate'] = rate($coupon['num'],$coupon['num_of']);
  59. $data['store']['id'] = $coupon->store['id'];
  60. $data['store']['name'] = $coupon->store['name'];
  61. $data['store']['img'] = $coupon->store['img']."?x-oss-process=style/w100";
  62. $data['store']['address'] = $coupon->store['address'];
  63. $data['store']['telphone'] = $coupon->store['telphone'];
  64. $data['store']['state_text'] = $coupon->store['state_text'];
  65. $data['store']['is_top'] = $coupon->store['is_top'];
  66. $data['store']['tips'] = $coupon->store['tips'];
  67. $data['store']['tags'] = explode(',',$coupon->store['tags']);
  68. return enjson(200,'成功',$data);
  69. }
  70. /**
  71. *优惠券列表
  72. * @return void
  73. */
  74. public function couponList(){
  75. $param['cate_id'] = $this->request->param('cate_id/d',0);
  76. $param['types'] = $this->request->param('types/d',0);
  77. $param['page'] = $this->request->param('page/d',0);
  78. $param['keyword'] = $this->request->param('keyword','');
  79. $param['sign'] = $this->request->param('sign');
  80. $rel = $this->apiSign($param);
  81. if($rel['code'] != 200){
  82. return enjson(403,'签名失败');
  83. }
  84. //搜索条件
  85. $condition = [];
  86. $condition[] = ['is_platform','=',0];
  87. if($param['cate_id']){
  88. $condition[] = ['cate_id','=',$param['cate_id']];
  89. }
  90. switch ($param['types']) {
  91. case 1:
  92. $condition[] = ['is_check','=',1];
  93. break;
  94. case 2:
  95. $condition[] = ['is_vip','=',1];
  96. break;
  97. default:
  98. $condition[] = ['is_shop','=',1];
  99. break;
  100. }
  101. //搜索关键字
  102. if(!empty($param['keyword'])){
  103. $condition[] = ["name","like","%{$param['keyword']}%"];
  104. }
  105. $condition[] = ['member_miniapp_id','=',$this->miniapp_id];
  106. $condition[] = ['is_lock','=',0];
  107. $condition[] = ['is_end','=',0];
  108. $condition[] = ['num','>',0];
  109. $condition[] = ['endtime','>',time()];
  110. $coupon = ModelCoupon::where($condition)->order('is_top desc,sort desc,size desc')->paginate(6);
  111. if ($coupon->isEmpty()) {
  112. return enjson(204,'没有优惠券');
  113. }
  114. //判断用户已领取优惠券
  115. $user_coupon_id = [];
  116. if ($this->user) {
  117. $ids = CouponUser::userCouponIds($this->user->id);
  118. if (!empty($ids)) {
  119. $user_coupon_id = $ids;
  120. }
  121. }
  122. $data = [];
  123. foreach ($coupon as $key => $value) {
  124. $data[$key]['id'] = $value['id'];
  125. $data[$key]['store_id'] = $value->store->id;
  126. $data[$key]['store'] = $value->store->name;
  127. $data[$key]['address'] = $value->store->address;
  128. $data[$key]['img'] = $value['img'].'?x-oss-process=style/w100';
  129. $data[$key]['name'] = $value['name'];
  130. $data[$key]['size'] = $value['size'];
  131. $data[$key]['amount'] = round($value['size']);
  132. $data[$key]['tips'] = $value['tips'];
  133. $data[$key]['types'] = $value['types'];
  134. $data[$key]['price'] = $value['types'] ? $value['discount'].'折':$value['price'];
  135. $data[$key]['pay_price'] = $value['pay_price'];
  136. $data[$key]['theme'] = $value['theme'];
  137. $data[$key]['starttime'] = date('Y-m-d',$value['starttime']);
  138. $data[$key]['endtime'] = date('Y-m-d',$value['endtime']);
  139. $data[$key]['state'] = in_array($value['id'],$user_coupon_id) ? 1 : 0;
  140. $data[$key]['rate'] = rate($value['num'],$value['num_of']);
  141. }
  142. return enjson(200,'成功',$data);
  143. }
  144. /**
  145. * 用户发起领取优惠券
  146. */
  147. public function buyCoupon(){
  148. $this->isUserAuth();
  149. if (request()->isPost()) {
  150. $param['coupon_id'] = $this->request->param('coupon_id/d');
  151. $param['signkey'] = $this->request->param('signkey');
  152. $param['sign'] = $this->request->param('sign');
  153. $rel = $this->apiSign($param);
  154. if($rel['code'] != 200){
  155. return enjson($rel['code'],'签名验证失败');
  156. }
  157. $param['member_miniapp_id'] = $this->miniapp_id;
  158. $validate = $this->validate($param,'Dopay.coupon');
  159. if (true !== $validate) {
  160. return enjson(403,$validate);
  161. }
  162. //禁止重复领取
  163. $coupon_user = CouponUser::userCouponCount($this->user->id,$param['coupon_id']);
  164. if($coupon_user){
  165. return enjson(403,'禁止重复领取');
  166. }
  167. //读取优惠券
  168. $condition['member_miniapp_id'] = $this->miniapp_id;
  169. $condition['is_lock'] = 0;
  170. $condition['id'] = $param['coupon_id'];
  171. $coupon = ModelCoupon::where($condition)->find();
  172. if (empty($coupon)) {
  173. return enjson(403,'未找到优惠券');
  174. }
  175. if ($coupon->is_platform == 1) {
  176. return enjson(403,'这个优惠券不允许单独领取哦');
  177. }
  178. //免费或付费领取
  179. $state = 0;
  180. if($coupon->shop_price > 0){
  181. $order_no = 'BUYCP'.order_no();
  182. $payparm = [
  183. 'name' => '付费购买优惠券['.$coupon->name.']',
  184. 'openid' => $this->user->miniapp_uid,
  185. 'miniapp_id' => $this->miniapp_id,
  186. 'order_no' => $order_no,
  187. 'total_fee' => $coupon->shop_price*100,
  188. 'notify_url' => api(4,'allwin/couponnotify/index',$this->miniapp_id),
  189. ];
  190. //读取配置
  191. $setting = AllwinConfig::getConfig($this->miniapp_id);
  192. if($setting->is_psp == 1){
  193. $default_mchid = model('MchId')->getMch(0, $this->miniapp_id); //默认收款账户
  194. if (empty($default_mchid)) {
  195. return enjson(403,'未找到商户号');
  196. }
  197. $payparm['mchid'] = $default_mchid->mchid;
  198. }
  199. $ispay = WechatPay::orderPay($payparm);
  200. if ($ispay['code'] == 0) {
  201. return enjson(403,$ispay['msg']);
  202. }
  203. $paydata = $ispay['data'];
  204. }else{
  205. $order_no = 'FREECP'.order_no();
  206. $state = 1;
  207. $paydata = [];
  208. $coupon_ids = json_encode([$param['coupon_id']]);
  209. CouponUser::addUserCoupon($coupon_ids,$this->user->id); //领取优惠券入库
  210. }
  211. //创建记录
  212. $vipdata = [];
  213. $vipdata['state'] = $state;
  214. $vipdata['coupon_ids'] = json_encode([$param['coupon_id']]);
  215. $vipdata['order_no'] = $order_no;
  216. $vipdata['amount'] = $coupon->shop_price;
  217. $vipdata['store_id'] = $coupon->store_id;
  218. $vipdata['user_id'] = $this->user->id;
  219. $vipdata['member_miniapp_id'] = $this->miniapp_id;
  220. $vipdata['update_time'] = time();
  221. $rel = CouponOrder::insert($vipdata);
  222. if($rel){
  223. if($coupon->shop_price > 0){
  224. SystemMemberForm::addForm($this->miniapp_id,$this->user->id,substr($ispay['data']['package'],10));
  225. }
  226. return enjson(200,'成功',['pay' => $paydata,'isbuy' => $state]);
  227. }else{
  228. return enjson(403,'领取优惠券失败');
  229. }
  230. }
  231. }
  232. }