IndexForm.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\plugins\scanCodePay\models\client;
  8. use app\models\Coupon;
  9. use app\models\CouponAutoSend;
  10. use app\models\Goods;
  11. use app\models\Option;
  12. use app\models\UserCoupon;
  13. use app\plugins\scanCodePay\models\ScanCodePaySetting;
  14. use yii\base\Model;
  15. class IndexForm extends Model
  16. {
  17. public $store_id;
  18. public $user_id;
  19. public $price;
  20. public function rules()
  21. {
  22. return [
  23. [['store_id', 'user_id', 'price'], 'required'],
  24. [['store_id', 'user_id'], 'integer'],
  25. ];
  26. }
  27. public function search()
  28. {
  29. if (!$this->validate()) {
  30. return [
  31. 'code' => 1,
  32. 'msg' => $this->getErrorSummary(false)[0],
  33. ];
  34. }
  35. $couponList = $this->getCouponList();
  36. // $payTypeList = $this->getPayTypeList();
  37. // Todo 暂时去掉支付类型,由客户端根据平台自动判断支付宝和微信
  38. $payTypeList = [];
  39. $goods = $this->getGoods();
  40. return [
  41. 'code' => 0,
  42. 'msg' => 'success',
  43. 'data' => [
  44. 'coupon_list' => $couponList,
  45. 'payType_list' => $payTypeList,
  46. 'goods_id' => $goods->id,
  47. ],
  48. ];
  49. }
  50. /**
  51. * 获取当面付优惠券
  52. * @return array
  53. */
  54. public function getCouponList()
  55. {
  56. // TODO: 如果加入联盟,就取联盟的优惠券,否则取当前store_id下的优惠券
  57. $query = UserCoupon::find()->alias('uc')
  58. ->leftJoin(['c' => Coupon::tableName()], 'uc.coupon_id=c.id')
  59. ->leftJoin(['cas' => CouponAutoSend::tableName()], 'uc.coupon_auto_send_id=cas.id')
  60. ->where([
  61. 'AND',
  62. ['uc.store_id' => $this->store_id],
  63. ['uc.is_delete' => 0],
  64. ['uc.is_use' => 0],
  65. ['uc.is_expire' => 0],
  66. ['uc.user_id' => $this->user_id],
  67. ]);
  68. if ($this->price) {
  69. $query->andWhere(['<=', 'c.min_price', $this->price]);
  70. }
  71. $list = $query->select('uc.id user_coupon_id,uc.is_use,uc.is_expire,c.rule,c.sub_price,c.min_price,cas.event,uc.begin_time,uc.end_time,uc.type,c.appoint_type,c.cat_id_list,c.goods_id_list')
  72. ->orderBy('c.sub_price DESC')
  73. ->asArray()->all();
  74. $new_list = [];
  75. $events = [
  76. 0 => '平台发放',
  77. 1 => '分享红包',
  78. 2 => '购物返券',
  79. 3 => '领券中心',
  80. ];
  81. \Yii::error($list);
  82. foreach ($list as $i => $item) {
  83. if ($item['begin_time'] > (strtotime(date('Y-M-d')) + 86400) || $item['end_time'] < time()) {
  84. continue;
  85. }
  86. $list[$i]['status'] = 0;
  87. if ($item['is_use']) {
  88. $list[$i]['status'] = 1;
  89. }
  90. if ($item['is_expire']) {
  91. $list[$i]['status'] = 2;
  92. }
  93. $list[$i]['min_price_desc'] = $item['min_price'] == 0 ? '无门槛' : '满' . $item['min_price'] . '元可用';
  94. $list[$i]['begin_time'] = date('Y.m.d H:i', $item['begin_time']);
  95. $list[$i]['end_time'] = date('Y.m.d H:i', $item['end_time']);
  96. if (!$item['event']) {
  97. if ($item['type'] == 2) {
  98. $list[$i]['event'] = $item['event'] = 3;
  99. } else {
  100. $list[$i]['event'] = $item['event'] = 0;
  101. }
  102. }
  103. $list[$i]['event_desc'] = $events[$item['event']];
  104. $list[$i]['min_price'] = doubleval($item['min_price']);
  105. $list[$i]['sub_price'] = doubleval($item['sub_price']);
  106. if ($this->price < $list[$i]['min_price']) {
  107. unset($list[$i]);
  108. continue;
  109. }
  110. $new_list[] = $list[$i];
  111. }
  112. return $new_list;
  113. }
  114. /**
  115. * 获取支付类型
  116. * @return array
  117. */
  118. protected function getPayTypeList()
  119. {
  120. // TODO: store_id mch
  121. $setting = ScanCodePaySetting::findOne(['store_id' => $this->store_id, 'mch' => 0, 'is_delete' => 0]);
  122. $paymentType = json_decode($setting['payment_type'], true);
  123. $payType = [];
  124. $payType[] = [
  125. 'name' => '微信支付',
  126. 'payment' => 1,
  127. 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/recharge/icon-online.png'
  128. ];
  129. foreach ($paymentType as $key => $value) {
  130. if ($key == 'wechat' && $value == 0) {
  131. $payType = [];
  132. }
  133. if ($key == 'balance' && $value == 1) {
  134. $balance = Option::get('payment', $this->store_id, 'store');
  135. $balance = json_decode($balance['value'], true);
  136. if ($balance && $balance['balance']['value'] == 1) {
  137. $payType[] = [
  138. 'name' => '账户余额支付',
  139. 'payment' => 3,
  140. 'icon' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/recharge/icon-balance.png'
  141. ];
  142. }
  143. }
  144. }
  145. return $payType;
  146. }
  147. /**
  148. * 获取当面付商品
  149. */
  150. protected function getGoods()
  151. {
  152. $goods = Goods::find()->where([
  153. 'is_delete' => 0,
  154. 'store_id' => $this->store_id,
  155. 'type' => 6,
  156. ])->one();
  157. if (!$goods) {
  158. $goods = new Goods();
  159. $goods->store_id = $this->store_id;
  160. $goods->name = '当面付';
  161. $goods->detail = '<p>当面付</p>';
  162. $goods->created_at = time();
  163. $goods->attr = '[{"attr_list":[{"attr_id":1,"attr_name":"默认"}],"num":0,"price":0,"no":"","pic":"","share_commission_first":"","share_commission_second":"","share_commission_third":""}]';
  164. $goods->cover_pic = 'https://chidian.cyyvip.com/web/statics/images/scan-code-pay/goods_pic.png';
  165. $goods->full_cut = '{"pieces":"","forehead":""}';
  166. $goods->integral = '{"give":"0","forehead":"","more":""}';
  167. $goods->use_attr = 0;
  168. $goods->quick_purchase = 0;
  169. $goods->hot_cakes = 0;
  170. $goods->type = 6;
  171. $goods->save();
  172. }
  173. return $goods;
  174. }
  175. }