WaterCardOrder.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. /**
  3. * @author: xaboy<365615158@qq.com>
  4. * @day: 2017/11/11
  5. */
  6. namespace app\admin\model\water;
  7. use app\models\user\User;
  8. use app\models\user\UserBill;
  9. use app\models\user\WechatUser;
  10. use crmeb\services\MiniProgramService;
  11. use crmeb\services\WechatService;
  12. use crmeb\traits\ModelTrait;
  13. use crmeb\basic\BaseModel;
  14. use think\model\concern\SoftDelete;
  15. /**
  16. * Class StoreCategory
  17. * @package app\admin\model\store
  18. */
  19. class WaterCardOrder extends BaseModel
  20. {
  21. /**
  22. * 数据表主键
  23. * @var string
  24. */
  25. protected $pk = 'id';
  26. /**
  27. * 模型名称
  28. * @var string
  29. */
  30. protected $name = 'water_card_order';
  31. use ModelTrait;
  32. protected $autoWriteTimestamp = true;
  33. public static function list($where)
  34. {
  35. $model = self::alias('a')->field('a.*,b.nickname,c.title')->order('a.id DESC')
  36. ->leftJoin('user b', 'a.uid = b.uid')
  37. ->leftJoin('water_membershio_card c', 'a.card_id = c.id');
  38. if ($where['name'])$model->where('b.nickname|b.uid' , 'like', '%'.$where['name'],'%');
  39. if ($where['card'])$model->where('c.title' , 'like', '%'.$where['card'],'%');
  40. if ($where['order_id'])$model->where('a.order_id' , 'like', '%'.$where['order_id'],'%');
  41. $data['count'] = $model->count();
  42. if ($where['page'] && $where['limit']){
  43. $model->page($where['page'], $where['limit']);
  44. }else{
  45. $model->page(20, 1);
  46. }
  47. $list = $model->select()->toArray();
  48. foreach ($list as &$item){
  49. if ($item['pay_type'] == 'routine'){
  50. $item['pay_type'] = '小程序';
  51. }elseif ($item['pay_type'] == 'weixinh5'){
  52. $item['pay_type'] = '公众号';
  53. }else{
  54. $item['pay_type'] = '其他支付';
  55. }
  56. }
  57. $data['data'] = $list;
  58. return $data;
  59. }
  60. public static function addCard($uid, $card_id, $pay_type)
  61. {
  62. $order_id = self::getNewOrderId($uid);
  63. if (!$order_id) return self::setErrorInfo('订单生成失败!');
  64. $card = WaterMembershioCard::find($card_id);
  65. if (!$card) return self::setErrorInfo('没有该会员卡');
  66. $add_time = time();
  67. return self::create([
  68. 'order_id' => $order_id,
  69. 'uid' => $uid,
  70. 'card_id' => $card_id,
  71. 'time' => $card['time'],
  72. 'price' => $card['price'],
  73. 'pay_type' => $pay_type,
  74. 'create_time' => $add_time
  75. ]);
  76. }
  77. /**
  78. * 生成充值订单号
  79. * @param int $uid
  80. * @return bool|string
  81. */
  82. public static function getNewOrderId($uid = 0)
  83. {
  84. if (!$uid) return false;
  85. $count = (int)self::where('uid', $uid)->where('create_time', '>=', strtotime(date("Y-m-d")))->where('create_time', '<', strtotime(date("Y-m-d", strtotime('+1 day'))))->count();
  86. return 'wx' . date('YmdHis', time()) . (10000 + $count + $uid);
  87. }
  88. /**
  89. * 充值js支付
  90. * @param $orderInfo
  91. * @return array|string
  92. * @throws \Exception
  93. */
  94. public static function jsPay($orderInfo)
  95. {
  96. return MiniProgramService::jsPay(WechatUser::uidToOpenid($orderInfo['uid']), $orderInfo['order_id'], $orderInfo['price'], 'user_card', '用户购买会员卡');
  97. }
  98. /**
  99. * 微信H5支付
  100. * @param $orderInfo
  101. * @return mixed
  102. */
  103. public static function wxH5Pay($orderInfo)
  104. {
  105. return WechatService::paymentPrepare(null, $orderInfo['order_id'], $orderInfo['price'], 'user_card', '用户充值', '', 'MWEB');
  106. }
  107. /**
  108. * 公众号支付
  109. * @param $orderInfo
  110. * @return array|string
  111. * @throws \Exception
  112. */
  113. public static function wxPay($orderInfo)
  114. {
  115. return WechatService::jsPay(WechatUser::uidToOpenid($orderInfo['uid'], 'openid'), $orderInfo['order_id'], $orderInfo['price'], 'user_card', '用户充值');
  116. }
  117. /**
  118. * 会员卡购买成功
  119. * @param $orderId
  120. * @return bool
  121. * @throws \think\db\exception\DataNotFoundException
  122. * @throws \think\db\exception\DbException
  123. * @throws \think\db\exception\ModelNotFoundException
  124. */
  125. public static function rechargeSuccess($orderId)
  126. {
  127. $order = self::where('order_id', $orderId)->where('paid', 0)->find();
  128. if (!$order) return false;
  129. $user = User::getUserInfo($order['uid']);
  130. self::beginTrans();
  131. $res1 = self::where('order_id', $order['order_id'])->update(['paid' => 1, 'pay_time' => time()]);
  132. $card = WaterMembershioCard::where('id', $order['card_id'])->find();
  133. $mark = '成功开通会员'.$card['title'];
  134. $res2 = UserBill::income('会员开通', $order['uid'], 'now_money', 'recharge', $order['price'], $order['id'], $user['now_money'], $mark);
  135. if ($user['member_time'] < time()){
  136. $member_time = time() + ($order['time'] * 86400);
  137. }else{
  138. $member_time = $user['member_time'] + ($order['time'] * 86400);
  139. }
  140. $res3 = User::edit(['member' => 1, 'member_time' => $member_time], $order['uid'], 'uid');
  141. $res = $res1 && $res2 && $res3;
  142. self::checkTrans($res);
  143. // event('RechargeSuccess', [$order]);
  144. return $res;
  145. }
  146. /**
  147. * 会员到期
  148. * @return void
  149. * @throws \think\db\exception\DataNotFoundException
  150. * @throws \think\db\exception\DbException
  151. * @throws \think\db\exception\ModelNotFoundException
  152. */
  153. public static function expire()
  154. {
  155. $user = User::where('member', 1)->where('member_time', '<', time())->select();
  156. if (count($user) > 0){
  157. foreach ($user as $item){
  158. User::where('uid', $item['uid'])->update(['member' => 0]);
  159. }
  160. }
  161. }
  162. }