123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- <?php
- /**
- * @author: xaboy<365615158@qq.com>
- * @day: 2017/11/11
- */
- namespace app\admin\model\water;
- use app\models\user\User;
- use app\models\user\UserBill;
- use app\models\user\WechatUser;
- use crmeb\services\MiniProgramService;
- use crmeb\services\WechatService;
- use crmeb\traits\ModelTrait;
- use crmeb\basic\BaseModel;
- use think\model\concern\SoftDelete;
- /**
- * Class StoreCategory
- * @package app\admin\model\store
- */
- class WaterCardOrder extends BaseModel
- {
- /**
- * 数据表主键
- * @var string
- */
- protected $pk = 'id';
- /**
- * 模型名称
- * @var string
- */
- protected $name = 'water_card_order';
- use ModelTrait;
- protected $autoWriteTimestamp = true;
- public static function list($where)
- {
- $model = self::alias('a')->field('a.*,b.nickname,c.title')->order('a.id DESC')
- ->leftJoin('user b', 'a.uid = b.uid')
- ->leftJoin('water_membershio_card c', 'a.card_id = c.id');
- if ($where['name'])$model->where('b.nickname|b.uid' , 'like', '%'.$where['name'],'%');
- if ($where['card'])$model->where('c.title' , 'like', '%'.$where['card'],'%');
- if ($where['order_id'])$model->where('a.order_id' , 'like', '%'.$where['order_id'],'%');
- $data['count'] = $model->count();
- if ($where['page'] && $where['limit']){
- $model->page($where['page'], $where['limit']);
- }else{
- $model->page(20, 1);
- }
- $list = $model->select()->toArray();
- foreach ($list as &$item){
- if ($item['pay_type'] == 'routine'){
- $item['pay_type'] = '小程序';
- }elseif ($item['pay_type'] == 'weixinh5'){
- $item['pay_type'] = '公众号';
- }else{
- $item['pay_type'] = '其他支付';
- }
- }
- $data['data'] = $list;
- return $data;
- }
- public static function addCard($uid, $card_id, $pay_type)
- {
- $order_id = self::getNewOrderId($uid);
- if (!$order_id) return self::setErrorInfo('订单生成失败!');
- $card = WaterMembershioCard::find($card_id);
- if (!$card) return self::setErrorInfo('没有该会员卡');
- $add_time = time();
- return self::create([
- 'order_id' => $order_id,
- 'uid' => $uid,
- 'card_id' => $card_id,
- 'time' => $card['time'],
- 'price' => $card['price'],
- 'pay_type' => $pay_type,
- 'create_time' => $add_time
- ]);
- }
- /**
- * 生成充值订单号
- * @param int $uid
- * @return bool|string
- */
- public static function getNewOrderId($uid = 0)
- {
- if (!$uid) return false;
- $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();
- return 'wx' . date('YmdHis', time()) . (10000 + $count + $uid);
- }
- /**
- * 充值js支付
- * @param $orderInfo
- * @return array|string
- * @throws \Exception
- */
- public static function jsPay($orderInfo)
- {
- return MiniProgramService::jsPay(WechatUser::uidToOpenid($orderInfo['uid']), $orderInfo['order_id'], $orderInfo['price'], 'user_card', '用户购买会员卡');
- }
- /**
- * 微信H5支付
- * @param $orderInfo
- * @return mixed
- */
- public static function wxH5Pay($orderInfo)
- {
- return WechatService::paymentPrepare(null, $orderInfo['order_id'], $orderInfo['price'], 'user_card', '用户充值', '', 'MWEB');
- }
- /**
- * 公众号支付
- * @param $orderInfo
- * @return array|string
- * @throws \Exception
- */
- public static function wxPay($orderInfo)
- {
- return WechatService::jsPay(WechatUser::uidToOpenid($orderInfo['uid'], 'openid'), $orderInfo['order_id'], $orderInfo['price'], 'user_card', '用户充值');
- }
- /**
- * 会员卡购买成功
- * @param $orderId
- * @return bool
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public static function rechargeSuccess($orderId)
- {
- $order = self::where('order_id', $orderId)->where('paid', 0)->find();
- if (!$order) return false;
- $user = User::getUserInfo($order['uid']);
- self::beginTrans();
- $res1 = self::where('order_id', $order['order_id'])->update(['paid' => 1, 'pay_time' => time()]);
- $card = WaterMembershioCard::where('id', $order['card_id'])->find();
- $mark = '成功开通会员'.$card['title'];
- $res2 = UserBill::income('会员开通', $order['uid'], 'now_money', 'recharge', $order['price'], $order['id'], $user['now_money'], $mark);
- if ($user['member_time'] < time()){
- $member_time = time() + ($order['time'] * 86400);
- }else{
- $member_time = $user['member_time'] + ($order['time'] * 86400);
- }
- $res3 = User::edit(['member' => 1, 'member_time' => $member_time], $order['uid'], 'uid');
- $res = $res1 && $res2 && $res3;
- self::checkTrans($res);
- // event('RechargeSuccess', [$order]);
- return $res;
- }
- /**
- * 会员到期
- * @return void
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public static function expire()
- {
- $user = User::where('member', 1)->where('member_time', '<', time())->select();
- if (count($user) > 0){
- foreach ($user as $item){
- User::where('uid', $item['uid'])->update(['member' => 0]);
- }
- }
- }
- }
|