WaterCardOrder.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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'] == 'yue'){
  50. $item['pay_type'] = '余额';
  51. }elseif ($item['pay_type'] == 'weixin'){
  52. $item['pay_type'] = '微信';
  53. }elseif ($item['pay_type'] == 'alipay'){
  54. $item['pay_type'] = '支付宝';
  55. }else{
  56. $item['pay_type'] = '其他支付';
  57. }
  58. }
  59. $data['data'] = $list;
  60. return $data;
  61. }
  62. public static function addCard($uid, $card_id, $pay_type)
  63. {
  64. $order_id = self::getNewOrderId($uid);
  65. if (!$order_id) return self::setErrorInfo('订单生成失败!');
  66. $card = WaterMembershioCard::find($card_id);
  67. if (!$card) return self::setErrorInfo('没有该会员卡');
  68. $add_time = time();
  69. return self::create([
  70. 'order_id' => $order_id,
  71. 'uid' => $uid,
  72. 'card_id' => $card_id,
  73. 'time' => $card['time'],
  74. 'price' => $card['price'],
  75. 'pay_type' => $pay_type,
  76. 'create_time' => $add_time
  77. ]);
  78. }
  79. /**
  80. * 生成充值订单号
  81. * @param int $uid
  82. * @return bool|string
  83. */
  84. public static function getNewOrderId($uid = 0)
  85. {
  86. if (!$uid) return false;
  87. $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();
  88. return 'wx' . date('YmdHis', time()) . (10000 + $count + $uid);
  89. }
  90. /**
  91. * 充值js支付
  92. * @param $orderInfo
  93. * @return array|string
  94. * @throws \Exception
  95. */
  96. public static function jsPay($orderInfo)
  97. {
  98. return MiniProgramService::jsPay(WechatUser::uidToOpenid($orderInfo['uid']), $orderInfo['order_id'], $orderInfo['price'], 'user_card', '用户购买会员卡');
  99. }
  100. /**
  101. * 微信H5支付
  102. * @param $orderInfo
  103. * @return mixed
  104. */
  105. public static function wxH5Pay($orderInfo)
  106. {
  107. return WechatService::paymentPrepare(null, $orderInfo['order_id'], $orderInfo['price'], 'user_card', '用户充值', '', 'MWEB');
  108. }
  109. /**
  110. * 公众号支付
  111. * @param $orderInfo
  112. * @return array|string
  113. * @throws \Exception
  114. */
  115. public static function wxPay($orderInfo)
  116. {
  117. return WechatService::jsPay(WechatUser::uidToOpenid($orderInfo['uid'], 'openid'), $orderInfo['order_id'], $orderInfo['price'], 'user_card', '用户充值');
  118. }
  119. /**
  120. * 会员卡购买成功
  121. * @param $orderId
  122. * @return bool
  123. * @throws \think\db\exception\DataNotFoundException
  124. * @throws \think\db\exception\DbException
  125. * @throws \think\db\exception\ModelNotFoundException
  126. */
  127. public static function rechargeSuccess($orderId)
  128. {
  129. $order = self::where('order_id', $orderId)->where('paid', 0)->find();
  130. if (!$order) return false;
  131. $user = User::getUserInfo($order['uid']);
  132. self::beginTrans();
  133. $res1 = self::where('order_id', $order['order_id'])->update(['paid' => 1, 'pay_time' => time()]);
  134. $card = WaterMembershioCard::where('id', $order['card_id'])->find();
  135. $mark = '成功开通会员'.$card['title'];
  136. $res2 = UserBill::income('会员开通', $order['uid'], 'now_money', 'recharge', $order['price'], $order['id'], $user['now_money'], $mark);
  137. if ($user['member_time'] < time()){
  138. $member_time = time() + ($order['time'] * 86400);
  139. }else{
  140. $member_time = $user['member_time'] + ($order['time'] * 86400);
  141. }
  142. $res3 = User::edit(['member' => 1, 'member_time' => $member_time], $order['uid'], 'uid');
  143. $res = $res1 && $res2 && $res3;
  144. self::checkTrans($res);
  145. // event('RechargeSuccess', [$order]);
  146. return $res;
  147. }
  148. /**
  149. * 会员到期
  150. * @return void
  151. * @throws \think\db\exception\DataNotFoundException
  152. * @throws \think\db\exception\DbException
  153. * @throws \think\db\exception\ModelNotFoundException
  154. */
  155. public static function expire()
  156. {
  157. $user = User::where('member', 1)->where('member_time', '<', time())->select();
  158. if (count($user) > 0){
  159. foreach ($user as $item){
  160. User::where('uid', $item['uid'])->update(['member' => 0]);
  161. }
  162. }
  163. }
  164. }