Vip.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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\VipCard;
  11. use app\allwin\model\AllwinUser;
  12. use app\allwin\model\Vip as AllwinVip;
  13. use app\allwin\model\AllwinConfig;
  14. use app\allwin\model\MchId;
  15. use app\allwin\model\CouponUser;
  16. use app\allwin\model\Bank;
  17. use app\allwin\model\Levels;
  18. use app\allwin\model\Coupon;
  19. use app\common\facade\WechatPay;
  20. use app\allwin\widget\Vip as VipIncome;
  21. class Vip extends Base{
  22. /**
  23. * 初始化当前应用管理员是不是联盟城市账户
  24. * @return void
  25. */
  26. public function initialize() {
  27. parent::initialize();
  28. $this->isUserAuth();
  29. }
  30. /**
  31. * 判断是否VIP和是否允许升级
  32. */
  33. public function isVip(){
  34. $param['signkey'] = $this->request->param('signkey');
  35. $param['sign'] = $this->request->param('sign');
  36. $rel = $this->apiSign($param);
  37. if($rel['code'] != 200){
  38. return enjson($rel['code'],'签名验证失败');
  39. }
  40. $data = ['isvip' => 0,'is_up' => 1,'icard' => [],'card' => []];
  41. $vipuser = AllwinVip::with(['vipcard'=> function($query) {
  42. $query->field('id,coupon_num,is_up,price,name,tips,rule');
  43. }])->where(['user_id' => $this->user->id,'state' => 1,'is_lock' => 0])->field('is_up,vipcard_id')->find();
  44. $condition = [];
  45. $condition[] = ['member_miniapp_id','=',$this->miniapp_id];
  46. if($vipuser){
  47. $bank = Bank::where(['user_id' => $this->user->id])->field('income_money')->find(); //收益
  48. $store = AllwinUser::where(['uid' => $this->user->id])->find(); //所属店铺
  49. $data['isvip'] = 1;
  50. $data['is_up'] = $vipuser->vipcard->is_up;
  51. $data['icard'] = $vipuser->vipcard;
  52. $data['icard']['title'] = empty($store->store) ? $this->miniapp->appname.'会员' : $store->store->name.'会员';
  53. $data['icard']['store_id'] = empty($store->store)?0:$store->store_id;
  54. $data['icard']['income_money'] = empty($bank) ? 0 : round($bank->income_money/100);
  55. $data['icard']['rule'] = str_replace('<img', '<img class="img" style="max-width:100%;height:auto"',dehtml($vipuser->vipcard->rule));
  56. $condition[] = ['id','<>',$vipuser->vipcard_id];
  57. }
  58. $data['card'] = VipCard::where($condition)->field('id,coupon_num,is_up,price,name,tips,rule')->order('sort desc,id desc')->find();
  59. if(empty($data['card'])){
  60. $data['card'] = [];
  61. }else{
  62. $data['card']['rule'] = str_replace('<img', '<img class="img" style="max-width:100%;height:auto"',dehtml($data['card']['rule']));
  63. $data['card']['price'] = round($data['card']['price']);
  64. }
  65. if($data['isvip'] == 1){
  66. $data['num'] = Levels::where(['parent_id' => $this->user->id,'level' => [1,2]])->count();
  67. }else{
  68. $data['num'] = AllwinVip::where(['state' => 1,'is_lock' => 0,'member_miniapp_id'=>$this->miniapp_id])->count();
  69. }
  70. if(empty($data['icard']) && empty($data['card'])){
  71. return enjson(204,'没有开通会员服务');
  72. }
  73. return enjson(200,'成功',$data);
  74. }
  75. /**
  76. * 开通会员中心
  77. * @return boolean
  78. */
  79. public function payment(){
  80. if (request()->isPost()) {
  81. $param = [
  82. 'vipcard' => $this->request->param('vipcard/d'),
  83. 'ucode' => $this->request->param('ucode/s',''),
  84. 'signkey' => $this->request->param('signkey'),
  85. 'sign' => $this->request->param('sign'),
  86. ];
  87. $rel = $this->apiSign($param);
  88. if($rel['code'] != 200){
  89. return enjson(403,'签名验证失败');
  90. }
  91. $card_id = $param['vipcard'];
  92. $vipcard = VipCard::where(['id' => $card_id])->field('id,price,is_up,name,coupon_ids')->find();
  93. if(empty($vipcard)){
  94. return enjson(403,'未找到要开通的会员类型');
  95. }
  96. $is_vip = AllwinVip::where(['id' => $vipcard->id,'state' => 1])->count();
  97. if($is_vip){
  98. return enjson(403,'你已经是会员');
  99. }
  100. //判断并读取邀请用户的所属店铺ID
  101. $store_id = 0;
  102. $ucode = de_code(strtoupper($param['ucode']));
  103. if($ucode){
  104. $vipuser = AllwinUser::where(['uid' => $ucode])->find();
  105. if($vipuser){
  106. if($this->miniapp_id != $vipuser->user->member_miniapp_id){
  107. return enjson(403,'邀请用户填写不正确');
  108. }
  109. $store_id = $vipuser['store_id'];
  110. }
  111. }
  112. //数据
  113. $coupon_ids = explode(',',$vipcard->coupon_ids);
  114. $order_no = 'VIP'.order_no();
  115. $vipdata = [];
  116. $vipdata['state'] = $vipcard->price <= 0 ? 1 : 0;
  117. $vipdata['is_lock'] = 0;
  118. $vipdata['coupon_ids'] = empty($coupon_ids)? '[]' : json_encode($coupon_ids);
  119. $vipdata['order_no'] = $order_no;
  120. $vipdata['amount'] = $vipcard->price;
  121. $vipdata['is_up'] = $vipcard->is_up;
  122. $vipdata['vipcard_id'] = $vipcard->id;
  123. $vipdata['store_id'] = $store_id; //邀请参数的店铺ID
  124. $vipdata['from_ucode'] = $param['ucode'];//邀请人的邀请码
  125. $vipdata['user_id'] = $this->user->id;
  126. $vipdata['member_miniapp_id'] = $this->miniapp_id;
  127. $vipdata['update_time'] = time();
  128. //判断帐号
  129. $rel = AllwinVip::where(['member_miniapp_id'=>$this->miniapp_id,'user_id' => $this->user->id,'state' => 0])->find();
  130. if($rel){
  131. AllwinVip::where(['id' => $rel->id])->update($vipdata);
  132. $vip_id = $rel->id;
  133. }else{
  134. $allwinVip = AllwinVip::create($vipdata);
  135. $vip_id = $allwinVip->id;
  136. }
  137. //如果免费
  138. if($vipcard->price <= 0){
  139. //增加邀请关系
  140. if(!empty($ucode)){
  141. VipIncome::addLevel($this->user->id,$ucode,$store_id);
  142. }
  143. //更新VIP状态
  144. AllwinVip::where([['user_id','=',$this->user->id],['state','=','1'],['id','<>',$vip_id]])->update(['is_lock' => 1]);
  145. //赠送优惠券
  146. CouponUser::addUserCoupon($vipdata['coupon_ids'],$this->user->id);
  147. //统计优惠综合
  148. $coupon_aumont = Coupon::where(['id' => $coupon_ids])->sum('size');
  149. return enjson(200,'成功加入会员',['isvip' => 1,'coupon_aumont'=> $coupon_aumont]);
  150. }else{
  151. $payparm = [
  152. 'openid' => $this->user->miniapp_uid,
  153. 'miniapp_id' => $this->miniapp_id,
  154. 'order_no' => $order_no,
  155. 'total_fee' => $vipcard->price*100,
  156. 'name' => '开通'.$vipcard->name,
  157. 'notify_url' => api(4,'allwin/vipnotify/index',$this->miniapp_id),
  158. ];
  159. //读取配置
  160. $setting = AllwinConfig::getConfig($this->miniapp_id);
  161. if($setting->is_psp == 1){
  162. $default_mchid = MchId::getMch(0,$this->miniapp_id); //默认收款账户
  163. if (empty($default_mchid)) {
  164. return enjson(403,'未找到收款账号');
  165. }
  166. $payparm['mchid'] = $default_mchid->mchid;
  167. }
  168. $ispay = WechatPay::orderPay($payparm);
  169. if($ispay['code'] == 0){
  170. return json(['code'=>403,'msg'=>$ispay['msg']]);
  171. }
  172. return enjson(200,'开始付款',['isvip' => 0,'pay' => $ispay['data']]);
  173. }
  174. }
  175. }
  176. /**
  177. * 读取会员专属优惠券
  178. */
  179. public function coupon(){
  180. $param['vipid'] = $this->request->param('vipid/d');
  181. $param['page'] = $this->request->param('page/d',0);
  182. $param['sign'] = $this->request->param('sign');
  183. $rel = $this->apiSign($param);
  184. if($rel['code'] != 200){
  185. return enjson(403,'签名失败');
  186. }
  187. $vipuser = VipCard::where(['id' => $param['vipid']])->field('coupon_ids')->find();
  188. if (empty($vipuser)) {
  189. return enjson(204,'没有优惠券');
  190. }
  191. $condition[] = ['id','in',$vipuser->coupon_ids];
  192. $condition[] = ['is_lock','=',0];
  193. $condition[] = ['is_end','=',0];
  194. $condition[] = ['num','>',0];
  195. $condition[] = ['endtime','>',time()];
  196. $coupon = Coupon::where($condition)->field('id,name,size,tips,discount,price,types')->order('is_top desc,sort desc,size desc')->paginate(6);
  197. if ($coupon->isEmpty()) {
  198. return enjson(204,'没有优惠券');
  199. }
  200. $data = [];
  201. foreach ($coupon as $key => $value) {
  202. $data[$key]['id'] = $value['id'];
  203. $data[$key]['name'] = $value['name'];
  204. $data[$key]['amount'] = round($value['size']);
  205. $data[$key]['tips'] = $value['tips'];
  206. $data[$key]['types'] = $value['types'];
  207. $data[$key]['price'] = $value['types'] ? $value['discount'].'折':$value['price'];
  208. }
  209. return enjson(200,'成功',$data);
  210. }
  211. }