UserOrderRepository.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\common\repositories\user;
  12. use app\common\dao\user\LabelRuleDao;
  13. use app\common\dao\user\UserOrderDao;
  14. use app\common\repositories\BaseRepository;
  15. use app\common\repositories\system\groupData\GroupDataRepository;
  16. use crmeb\jobs\SendSmsJob;
  17. use crmeb\services\PayService;
  18. use FormBuilder\Factory\Elm;
  19. use think\facade\Db;
  20. use think\facade\Log;
  21. use think\facade\Queue;
  22. /**
  23. * Class LabelRuleRepository
  24. * @package app\common\repositories\user
  25. * @author xaboy
  26. * @day 2020/10/20
  27. * @mixin LabelRuleDao
  28. */
  29. class UserOrderRepository extends BaseRepository
  30. {
  31. //付费会员
  32. const TYPE_SVIP = 'S-';
  33. /**
  34. * LabelRuleRepository constructor.
  35. * @param LabelRuleDao $dao
  36. */
  37. public function __construct(UserOrderDao $dao)
  38. {
  39. $this->dao = $dao;
  40. }
  41. public function getList(array $where, $page, $limit)
  42. {
  43. $query = $this->dao->search($where);
  44. $count = $query->count();
  45. $list = $query->with([
  46. 'user' => function($query){
  47. $query->field('uid,nickname,avatar,phone,is_svip,svip_endtime');
  48. }
  49. ])->order('create_time DESC')->page($page, $limit)->select()->toArray();
  50. return compact('count', 'list');
  51. }
  52. /**
  53. * TODO 获取订单号
  54. * @return string
  55. * @author Qinii
  56. * @day 2022/11/12
  57. */
  58. public function setOrderSn()
  59. {
  60. list($msec, $sec) = explode(' ', microtime());
  61. $msectime = number_format((floatval($msec) + floatval($sec)) * 1000, 0, '', '');
  62. $orderId = 'wxs' . $msectime . mt_rand(10000, max(intval($msec * 10000) + 10000, 98369));
  63. return $orderId;
  64. }
  65. /**
  66. * @param $data
  67. * @return mixed
  68. * @author xaboy
  69. * @day 2020/10/21
  70. */
  71. public function add($res, $user, $params)
  72. {
  73. $order_sn = $this->setOrderSn();
  74. $data = [
  75. 'title' => $res['value']['svip_name'],
  76. 'link_id' => $res->group_data_id,
  77. 'order_sn' => $order_sn,
  78. 'pay_price' => $res['value']['price'],
  79. 'order_info' => json_encode($res['value'],JSON_UNESCAPED_UNICODE),
  80. 'uid' => $user->uid,
  81. 'order_type' => self::TYPE_SVIP.$res['value']['svip_type'],
  82. 'pay_type' => $res['value']['price'] == 0 ? 'free' : $params['pay_type'],
  83. 'status' => 1,
  84. 'other' => $user->is_svip == -1 ? 'first' : '',
  85. ];
  86. $body = [
  87. 'order_sn' => $order_sn,
  88. 'pay_price' => $data['pay_price'],
  89. 'attach' => 'user_order',
  90. 'body' =>'付费会员'
  91. ];
  92. $type = $params['pay_type'];
  93. if (in_array($type, ['weixin', 'alipay'], true) && $params['is_app']) {
  94. $type .= 'App';
  95. }
  96. if ($params['return_url'] && $type === 'alipay') $body['return_url'] = $params['return_url'];
  97. $info = $this->dao->create($data);
  98. if ($data['pay_price']){
  99. try {
  100. $service = new PayService($type,$body);
  101. $config = $service->pay($user);
  102. return app('json')->status($type, $config + ['order_id' => $info->order_id]);
  103. } catch (\Exception $e) {
  104. return app('json')->status('error', $e->getMessage(), ['order_id' => $info->order_id]);
  105. }
  106. } else {
  107. $res = $this->paySuccess($data);
  108. return app('json')->status('success', ['order_id' => $info->order_id]);
  109. }
  110. }
  111. public function paySuccess($data)
  112. {
  113. /*
  114. array (
  115. 'order_sn' => 'wxs167090166498470921',
  116. 'data' =>
  117. EasyWeChat\Support\Collection::__set_state(array(
  118. 'items' =>
  119. array (
  120. 'appid' => 'wx4409eaedbd62b213',
  121. 'attach' => 'user_order',
  122. 'bank_type' => 'OTHERS',
  123. 'cash_fee' => '1',
  124. 'fee_type' => 'CNY',
  125. 'is_subscribe' => 'N',
  126. 'mch_id' => '1288093001',
  127. 'nonce_str' => '6397efa100165',
  128. 'openid' => 'oOdvCvjvCG0FnCwcMdDD_xIODRO0',
  129. 'out_trade_no' => 'wxs167090166498470921',
  130. 'result_code' => 'SUCCESS',
  131. 'return_code' => 'SUCCESS',
  132. 'sign' => '125C56DE030A461E45D421E44C88BC30',
  133. 'time_end' => '20221213112118',
  134. 'total_fee' => '1',
  135. 'trade_type' => 'JSAPI',
  136. 'transaction_id' => '4200001656202212131458556229',
  137. ),
  138. )),
  139. */
  140. $res = $this->dao->getWhere(['order_sn' => $data['order_sn']]);
  141. $type = explode('-',$res['order_type'])[0].'-';
  142. // 付费会员充值
  143. if ($type == self::TYPE_SVIP) {
  144. return Db::transaction(function () use($data, $res) {
  145. $res->paid = 1;
  146. $res->pay_time = date('y_m-d H:i:s', time());
  147. $res->save();
  148. return $this->payAfter($res, $res);
  149. });
  150. }
  151. }
  152. public function payAfter($data, $ret)
  153. {
  154. $info = json_decode($data['order_info']);
  155. $user = app()->make(UserRepository::class)->get($ret['uid']);
  156. $day = $info->svip_type == 3 ? 0 : $info->svip_number;
  157. $endtime = ($user['svip_endtime'] && $user['is_svip'] != 0) ? $user['svip_endtime'] : date('Y-m-d H:i:s',time());
  158. $svip_endtime = date('Y-m-d H:i:s',strtotime("$endtime +$day day" ));
  159. $user->is_svip = $info->svip_type;
  160. $user->svip_endtime = $svip_endtime;
  161. $user->save();
  162. $ret->status = 1;
  163. $ret->pay_time = date('Y-m-d H:i:s',time());
  164. $ret->end_time = $svip_endtime;
  165. $ret->save();
  166. $date = $info->svip_type == 3 ? '终身会员' : $svip_endtime;
  167. Queue::push(SendSmsJob::class,['tempId' => 'SVIP_PAY_SUCCESS','id' => ['phone' => $user->phone, 'date' => $date]]);
  168. return ;
  169. }
  170. }