Storepay.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. /**
  3. * 买单付款
  4. */
  5. namespace app\ais\controller\api\v1;
  6. use app\ais\controller\api\Base;
  7. use app\ais\model\AisOrder;
  8. use app\ais\model\AisStore;
  9. use app\ais\model\AisCoupon;
  10. use app\ais\model\AisCouponUser;
  11. use app\ais\model\AisConfig;
  12. use app\common\facade\WechatPay;
  13. use app\common\model\SystemUserLevel;
  14. use think\Db;
  15. class Storepay extends Base{
  16. protected $config;
  17. /**
  18. * 初始化当前应用管理员是不是联盟城市账户
  19. * @return void
  20. */
  21. public function initialize() {
  22. parent::initialize();
  23. $this->isUserAuth();
  24. $this->config = AisConfig::getConfig($this->member_miniapp_id);
  25. }
  26. /**
  27. * 读取买单商家信息和优惠券
  28. * @return void
  29. */
  30. public function index(){
  31. $param['store_id'] = $this->request->param('store_id/d');
  32. $param['chain_id'] = $this->request->param('chain_id/d');
  33. $this->apiSign($param);
  34. if(empty($param['store_id'])){
  35. return enjson(404,'为找到付款好店');
  36. }
  37. $store = AisStore::where(['is_lock' => 0,'id' => $param['store_id']])->field('id,name')->find();
  38. if(empty($store)){
  39. return enjson(303,'未找到付款好店',['url' =>'/pages/index']);
  40. }
  41. if(!empty($param['chain_id'])){
  42. $store = $store->chain()->where('id',$param['chain_id'])->field('id,title,store_id')->find();
  43. if(empty($store)){
  44. return enjson(303,'未找到付款分店',['url' =>'/pages/index']);
  45. }
  46. $store->name = $store->title;
  47. $store->chain_id = $store->id;
  48. $store->id = $store->store_id;
  49. }
  50. $is_get_coupon = 0;
  51. if(empty($this->config->is_pay_open_coupon)){
  52. $condition[] = ['member_miniapp_id','=',$this->miniapp_id];
  53. $condition[] = ['uid','=',$this->user->id];
  54. $condition[] = ['is_end','=',0];
  55. $condition[] = ['money','=',0];
  56. $condition[] = ['store_id','=',$param['store_id']];
  57. $coupon = AisCouponUser::where($condition)->field('id,name,tips,howmuch,store_id,price,types,coupon_id,discount,create_time')->order('create_time desc')->select();
  58. foreach ($coupon as $key => $value){
  59. $coupon[$key]->is_get_coupon = 0;
  60. $coupon[$key]->end_time = $this->config->coupon_longtime * 60 * 60;
  61. }
  62. }else{
  63. $coupon = AisCoupon::where(['member_miniapp_id' => $this->miniapp_id,'store_id' => $param['store_id'],'is_lock' => 0,'is_end' => 0])->order('is_top desc,sort desc,create_time desc')->select();
  64. foreach ($coupon as $key => $info){
  65. $coupon[$key]->end_time = $info->create_time + $this->config->coupon_longtime * 60 * 60;
  66. $coupon[$key]->is_get_coupon = 0;
  67. }
  68. $is_get_coupon = 1;
  69. }
  70. return enjson(200,['coupon' => $coupon ,'store' => $store,'is_get_coupon' => $is_get_coupon]);
  71. }
  72. /**
  73. * 根据定位推荐附近同圈子的优惠券
  74. * @return void
  75. */
  76. public function couponTop(){
  77. $param['store_id'] = $this->request->param('store_id/d',0); //来自哪个商家
  78. $this->apiSign($param);
  79. $store = AisStore::where(['member_miniapp_id' => $this->miniapp_id,'id' => $param['store_id'],'is_lock' => 0])->find();
  80. if(empty($store)){
  81. return enjson(204);
  82. }
  83. if(empty($this->lbs->lat) || empty($this->lbs->lng)){
  84. return enjson(204);
  85. }
  86. $prefix = config('database.prefix');
  87. $latitude = $this->lbs->lat;
  88. $longitude = $this->lbs->lng;
  89. $sql = 'select id,img,name from '.$prefix.'Ais_store where latitude > '.$latitude .'-1 and latitude < '.$latitude .'+1 and longitude > '.$longitude.'-1 and longitude < '.
  90. $longitude.'+1 %s and id != '.$param['store_id'].' order by ACOS(SIN(('.$latitude .' * 3.1415) / 180 ) *SIN((latitude * 3.1415) / 180 ) +COS(('.$latitude .
  91. ' * 3.1415) / 180 ) * COS((latitude * 3.1415) / 180 ) *COS(('.$longitude.'* 3.1415) / 180 - (longitude * 3.1415) / 180 ) ) * 6380 asc limit 20';
  92. $ex = 'and id in(select store_id from '.$prefix.'Ais_store_union where group_id in (select group_id from '.$prefix.'Ais_store_union where member_miniapp_id = '.$this->miniapp_id.' and store_id = '.$param['store_id'].
  93. ' and type = 0 ) and type = 0 )';
  94. $unionList = Db::query(sprintf($sql,$ex)); //附近商圏店铺查询
  95. if(count($unionList) == 0){
  96. $ex = 'and cate_id = '. $store->cate_id;
  97. $unionList = Db::query(sprintf($sql,$ex)); //附近同行业店铺查询
  98. }
  99. if(count($unionList) == 0){
  100. $unionList = Db::query(sprintf($sql,'')); //附近店铺查询
  101. }
  102. $store = [];
  103. foreach($unionList as $key => $val){
  104. $store[$val['id']] = $val;
  105. }
  106. $store_ids = implode(',',array_column($unionList,'id'));
  107. $list = Db::query('SELECT * FROM '.$prefix.'Ais_coupon WHERE id >= (SELECT floor( RAND() * ((SELECT MAX(id) FROM '.$prefix.'Ais_coupon)-(SELECT MIN(id) FROM '.$prefix.'Ais_coupon)) + (SELECT MIN(id) FROM '.$prefix.'Ais_coupon))) and member_miniapp_id = '.
  108. $this->miniapp_id.' and is_lock = 0 and is_end = 0 and store_id in ('.$store_ids.') ORDER BY id LIMIT 15;');
  109. if (count($list)) {
  110. return enjson(200,'success',['coupon' => $list,'store' => $store]);
  111. }
  112. return enjson(204);
  113. }
  114. /**
  115. * 微信给商家支付
  116. * @param string $no
  117. * @return void
  118. */
  119. public function wechat(){
  120. if (request()->isPost()) {
  121. $rule = [
  122. 'member_miniapp_id' => $this->miniapp_id,
  123. 'store_id' => $this->request->param('store_id/d'),
  124. 'chain_id' => $this->request->param('chain_id/d',0),
  125. 'user_couponr_id' => $this->request->param('user_couponr_id/d',0),
  126. 'money' => $this->request->param('money/f'),
  127. 'amount' => $this->request->param('amount/f'),
  128. 'uid' => $this->user->id,
  129. ];
  130. $validate = $this->validate($rule,'Dopay.gopay');
  131. if (true !== $validate) {
  132. return enjson(204,$validate);
  133. }
  134. //判断好店
  135. $store = AisStore::where(['member_miniapp_id' => $this->miniapp_id,'id' => $rule['store_id'],'is_lock' => 0])->find();
  136. if(empty($store)){
  137. return enjson(204,'未找到商户');
  138. }
  139. $store->chain_id = 0;
  140. if(!empty($rule['chain_id'])){
  141. $chain = $store->chain()->where('id',$rule['chain_id'])->field('id,title,store_id')->find();
  142. if(empty($chain)){
  143. return enjson(204,'未找到付款分店');
  144. }
  145. $store->title = $chain->title;
  146. $store->chain_id = $chain->id;
  147. }
  148. if(empty($store->manage_uid)){
  149. return enjson(204,'未设置老板');
  150. }
  151. if(empty($store->mch_id)){
  152. return enjson(204,'未找到商家微信商户号');
  153. }
  154. //计算付款金额
  155. $amount = AisOrder::countPrice($rule);
  156. if(!$amount){
  157. return enjson(204,'优惠券不满足使用条件');
  158. }
  159. //创建订单
  160. $order_no = order_no($this->user->invite_code);
  161. //唤醒微信支付参数
  162. $payparm = [
  163. 'openid' => $this->user->miniapp_uid,
  164. 'miniapp_id' => $this->miniapp_id,
  165. 'name' => $store->name,
  166. 'order_no' => $order_no,
  167. 'attach' => 'Y',
  168. 'total_fee' => $amount['price'] * 100,
  169. 'notify_url' => api(1,'ais/notify/storePay', $this->miniapp_id),
  170. ];
  171. $payparm['mchid'] = $store->mch_id;
  172. if(!empty($amount['coupon'])){
  173. if($amount['coupon']->parent_store_id > 0){
  174. $rule['parent_store_id'] = $amount['coupon']->parent_store_id; //引荐的店铺
  175. $payparm['profit_sharing'] = 'Y';
  176. $payparm['attach'] = 'Y';
  177. }
  178. $rule['coupon_cache'] = $amount['coupon']; //优惠券
  179. }
  180. $level = SystemUserLevel::where(['user_id' => $this->user->id,'level' => 1])->find();
  181. if($level && $level->parent_id != $store->manage_uid){
  182. $payparm['profit_sharing'] = 'Y';
  183. $payparm['attach'] = 'Y';
  184. }
  185. $ispay = WechatPay::orderPay($payparm);
  186. if($ispay['code'] == 0){
  187. return enjson(204,$ispay['msg']);
  188. }
  189. //添加订单记录
  190. $rule['order_no'] = $order_no;
  191. $rule['price'] = $amount['price']; //实际金额
  192. $rule['store_chain_id'] = $store->chain_id; //分店ID
  193. $rel = AisOrder::addOrder($rule);
  194. if(!$rel){
  195. return enjson(204,'订单创建失败');
  196. }
  197. return enjson(200,'成功',$ispay['data']);
  198. }
  199. }
  200. }