redirect('@admin/login'); } public function tiaokuan(){ $privacy= Db::name('system_config')->where('name','privacy')->value('value'); $this->assign('info',$privacy); $this->fetch('index'); } /** * @title 生成订单(立即购买 ) * @desc 生成订单(立即购买 ) * @author Gavin * @url /api/Order/createOrder * @method POST * @header name:Authorization require:1 desc:Token * @param name:id type:int require:1 default:-- desc:藏品id * @param name:num type:int require:1 default:1 desc:数量 * @param name:pay_type type:string require:1 default:wx desc:wx:微信zfb:支付宝 * * @return name:order_no type:int require:0 default:0 desc:订单号 * @return name:pay type:string require:0 default:0 desc:支付信息 */ public function createOrder() { // $redis = new \think\cache\driver\Redis(); // $info = Db::name('store_collection')->where('id',30)->find(); // $info['is_deleted'] = 0; // $info['sell_time'] = '2022-04-21 15:53:04'; // $info['buy_count'] = '10000'; // $array = ['collection30'=>json_encode($info)]; // $redis->hMSet('collectionInfo',$array); // $redis->set('collection_count_30',5000); // dump($info);die; // die; // $redis->set('is_buy_30',1); // die; $this->uid = '100000'; $id = input('id'); $num = input('num',1); $pay_type = input('pay_type','wx'); if (!$id || !$num) $this->error('参数错误'); $user = getMemberInfoHash($this->uid); //获取用户信息 if ($user['is_auth']==0) $this->error('请实名认证后购买!'); $coll_info = getCollectionInfoHash($id); if ($coll_info['is_deleted']==1 || $coll_info['status']==0) $this->error('藏品已下架'); $inventory = getCollectionInventory($id); if ($inventory<1) $this->error('已售罄'); if ($user['vip']==2){ $advance_minutes = getAdvanceMinutes(); //获取vip提前抢购分钟 $sell_time = strtotime($coll_info['sell_time'])-($advance_minutes*60); }else{ $sell_time = strtotime($coll_info['sell_time']); } if ($sell_time>time()) $this->error('商品还未开始售卖!请耐心等待'); $snap_card = getMembercard($this->uid); //获取用户抢购卡数量 if ($snap_card<1) $this->error('抢购卡不足,无法抢购'); //获取用户已经购买数量 $userByCount = getByCount($this->uid,$id); if ($coll_info['buy_count']<($userByCount+$num)) $this->error('每人限购'.$coll_info['buy_count'].'个'); //获取是否已经铸造hash $hashCount = getLenCollection($id); // if (!$hashCount || $hashCount<$num) $this->error('hash未铸造,无法购买'); // $redis2 = new \think\cache\driver\Redis(); // $max = $redis2->get('max_30'); // if ($max>100){ // $this->error('排队中...'); // } // $redis2->Incr('max_30'); if (redisSetNx($id)){ //先减掉库存 loseCollectionInventory($id,$num); //先减掉抢购卡数量 loseMembercard($this->uid); //先增加购买数量 IncrByCount($this->uid,$id,$num); $com = true; Db::startTrans(); try { $order_no = get_order_sn(); $total_fee = bcmul($coll_info['price'],$num,2); $order_int =[ 'mid' => $this->uid, 'c_id' => $id, 'inventory' => $coll_info['inventory'], 'order_no' => $order_no, 'num' => $num, 'pro_info' => json_encode($coll_info), 'pay_type' => $pay_type, 'pay_price' => $total_fee ]; Db::name('store_order2')->insert($order_int); $body = '象寻购买藏品'; switch ($pay_type){ case 'wx': $config = retrunWxConfig(); $total_fee = $total_fee * 100; $config['notify_url'] = $this->request->root(true).'/api/Pay/WxOrderNotify'; $app = Factory::payment($config); $result = $app->order->unify([ 'body' => $body, 'out_trade_no' => $order_no, 'total_fee' => $total_fee, 'trade_type' => 'APP', // 请对应换成你的支付方式对应的值类型 ]); if ($result['return_code']=='SUCCESS'){ $jssdk = $app->jssdk; $order1 = $jssdk->appConfig($result['prepay_id']); Db::name('store_order2')->where('order_no',$order_no)->update(['wx_order'=>json_encode($order1,true)]); $retrun_data['order_no'] = $order_no; $retrun_data['pay'] = $order1; //减少数据库库存 Db::name('store_collection')->where('id',$id)->setDec('now_inventory',$num); //减少用户抢购卡数量 Db::name('store_member')->where('id',$this->uid)->setDec('snap_card'); Db::commit(); }else{ $com=false; Db::rollback(); } break; case 'zfb': $zfb = new AliPay(); $notify_url = $this->request->root(true).'/api/Pay/alipayOrderNotify';//回调地址 $order = $zfb->aliPay($body, $total_fee, $order_no, $notify_url);//调用支付宝支付的方法 $retrun_data['order_no'] = $order_no; $retrun_data['pay'] = $order; //减少数据库库存 Db::name('store_collection')->where('id',$id)->setDec('now_inventory',$num); //减少用户抢购卡数量 Db::name('store_member')->where('id',$this->uid)->setDec('snap_card'); Db::commit(); break; } }catch (\Exception $e){ $com=false; Db::rollback(); } if ($com){ DelRedisSetNx($id); //$redis2->Decr('max_30'); // $this->savetest($order_no); $this->success('成功',$retrun_data); }else{ // $redis2->Decr('max_30'); //加上库存 addCollectionInventory($id,$num); //加上抢购卡数量 addMembercard($this->uid); //减少用户购买数量 DecrByCount($this->uid,$id,$num); $this->error('抢购失败,请稍后重试'); } }else{ $this->error('排队中...'); } } }