123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- <?php
- namespace app\admin\model\card;
- use app\admin\model\store\StoreProduct;
- use app\admin\model\system\SystemAdmin;
- use app\admin\model\user\UserCard;
- use app\models\article\ArticleContent;
- 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;
- class CardOrder extends BaseModel
- {
- use ModelTrait;
- protected $pk = 'id';
- protected $name = 'card_order';
- protected $autoWriteTimestamp = true;
- public static function list($where)
- {
- $model = self::alias('a')
- ->field('a.*,b.nickname,c.name')
- ->leftJoin('user b', 'a.uid = b.uid')
- ->leftJoin('card c', 'c.id = a.card_id')
- ->order('a.id DESC');
- if ($where['name'])$model->where('b.nickname|b.uid|a.order_id' , 'like', '%'.$where['name'].'%');
- if ($where['type']){
- if ($where['type'] == 1){
- $model->where('paid', 0);
- }else{
- $model->where('paid', 1);
- }
- }
- $data['count'] = $model->count();
- if ($where['page'] && $where['limit']){
- $model->page($where['page'], $where['limit']);
- }else{
- $model->page(20, 1);
- }
- $list = $model->select()->toArray();
- $data['data'] = $list;
- return $data;
- }
- public static function addCard($uid, $card_id, $pay_type)
- {
- $order_id = self::getNewOrderId();
- if (!$order_id) return self::setErrorInfo('订单生成失败!');
- $card = Card::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'],
- 'pay_type' => $pay_type,
- 'price' => $card['price'],
- 'create_time' => $add_time
- ]);
- }
-
- public static function getNewOrderId()
- {
- do {
- list($msec, $sec) = explode(' ', microtime());
- $msectime = number_format((floatval($msec) + floatval($sec)) * 1000, 0, '', '');
- $orderId = 'wx' . $msectime . mt_rand(10000, 99999);
- } while (self::be(['order_id' => $orderId]));
- return $orderId;
- }
-
- public static function jsPay($orderInfo)
- {
- return MiniProgramService::jsPay(WechatUser::uidToOpenid($orderInfo['uid']), $orderInfo['order_id'], $orderInfo['price'], 'user_card', '用户购买优惠卡');
- }
-
- public static function wxH5Pay($orderInfo)
- {
- return WechatService::paymentPrepare(null, $orderInfo['order_id'], $orderInfo['price'], 'user_card', '用户购买优惠卡', '', 'MWEB');
- }
-
- public static function wxPay($orderInfo)
- {
- return WechatService::jsPay(WechatUser::uidToOpenid($orderInfo['uid'], 'openid'), $orderInfo['order_id'], $orderInfo['price'], 'user_card', '用户购买优惠卡');
- }
-
- 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 = Card::where('id', $order['card_id'])->find();
- $mark = '购买优惠卡'.$card['name'];
- $res2 = UserBill::income('购买优惠卡', $order['uid'], 'now_money', 'discount', $order['price'], $order['id'], $user['now_money'], $mark);
- $res3 = UserCard::create([
- 'card_id' => $order['card_id'],
- 'uid' => $order['uid'],
- 'card' => self::getRandPass(12),
- 'pwd' => self::getRandPass(12),
- ]);
- $res = $res1 && $res2 && $res3;
- self::checkTrans($res);
- return $res;
- }
- public static function getRandPass($length = 6){
- $password = '';
- $chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
- $char_len = strlen($chars);
- for ($i=0;$i < $length;$i++){
- $loop = mt_rand(0, ($char_len - 1));
- $password .= $chars[$loop];
- }
- return $password;
- }
- }
|