2]); $redis_value = $redis->get('order_buy'.$this->uid); if ($redis_value){ $this->error('请求过快,请稍后重试'); }else{ $redis->set('order_buy'.$this->uid,1,1); } $id = input('id'); $num = input('num',1); $pay_type = input('pay_type','wx'); $from = input('from','wx'); if (!$id || !$num) $this->error('参数错误'); //极验数据 // $lot_number = input('lot_number'); // $captcha_output = input('captcha_output'); // $pass_token = input('pass_token'); // $gen_time = input('gen_time'); // if (!$lot_number || !$captcha_output || !$pass_token || !$gen_time) $this->error('参数错误'); // // $jy = new Jiyan(); // $result = $jy->jy($lot_number,$captcha_output,$pass_token,$gen_time,2); // if ($result['result']=='fail') $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 ($coll_info['state']!=1) $this->error('状态错误'); if ($inventory<$num) $this->error('库存不足'); if ($inventory<1) $this->error('还有未支付的订单,您有可能捡漏哦!'); if (!$coll_info['sold_out']) $this->error('已售罄'); $sell_time = strtotime($coll_info['sell_time']); if ($sell_time>time()) $this->error('商品还未开始售卖!请耐心等待'); //获取用户已经购买数量 $userByCount = getByCount($this->uid,$id); if ($coll_info['buy_count']<($userByCount+$num)) $this->error('每人限购'.$coll_info['buy_count'].'个'); //获取是否已经铸造hash //$hashCount = getLenCollection($id); $hash = Db::name('hash2')->where('goods_id',$id)->where('success',1)->count(); if (!$hash) $this->error('nft未上架,无法购买'); // if (!$hashCount || $hashCount<$num) $this->error('hash未铸造,无法购买'); if ($from=='wx'){ if (!$user['openid']) $this->error('微信未授权,无法购买'); } //先减掉库存 loseCollectionInventory($id,$num); //先增加购买数量 IncrByCount($this->uid,$id,$num); $order_no = get_order_sn(); $total_fee = bcmul($coll_info['price'],$num,2); $redis_data = [ 'mid'=>$this->uid, 'c_id'=>$id, 'order_no'=>$order_no, 'inventory'=>$coll_info['inventory'], 'num'=>$num, 'pro_info'=>json_encode($coll_info), 'status'=>0, 'create_at'=>date('Y-m-d H:i:s'), 'pay_price'=>$total_fee, 'pay_type'=>$pay_type ]; $com = true; $retrun_data['order_no'] = $order_no; $retrun_data['pay'] = ''; if ($com){ $redis = new Redis(); //收集下单用户id setCollectionBuyUser($this->uid); $redis->hMSet('order_not_pay_'.$this->uid,[$order_no=>json_encode($redis_data,true)]); //存入redis $this->success('成功',$retrun_data); }else{ //加上库存 addCollectionInventory($id,$num); //减少用户购买数量 DecrByCount($this->uid,$id,$num); $this->error('服务器繁忙,请稍后重试'); } } /** * @title 我的订单-待支付、已购买 * @desc 我的订单-待支付、已购买 * @author Gavin * @url /api/Order/getMyOrderList * @method POST * @header name:Authorization require:1 desc:Token * @param name:status type:int : default:0 desc:0:待支付1:已购买 * @param name:page type:int : default:1 desc:页数 * @param name:page_num type:int : default:20 desc:每页数 * * @return name:order_no type:string default:-- desc:订单号 * @return name:num type:int default:-- desc:数量 * @return name:pay_price type:string default:-- desc:订单金额 * @return name:status type:int default:-- desc:类型0:待支付1:已支付 * @return name:cancel_time type:string default:-- desc:待支付自动取消时间 * @return name:pay_type type:string default:-- desc:支付方式wx:微信zfb:支付宝 * @return name:create_at type:string default:-- desc:创建时间 * @return name:pro_info@name type:string default:-- desc:藏品名称 * @return name:pro_info@cover type:string default:-- desc:藏品图片 * @return name:pro_info@price type:string default:-- desc:藏品单价 * @return name:pro_info@auth_img type:string default:-- desc:藏品作者头像 * @return name:pro_info@auth_name type:string default:-- desc:藏品作者名称 */ public function getMyOrderList(){ $status = input('status',0); if ($status==0){ $redis = new Redis(); $key = 'order_not_pay_'.$this->uid; $count = $redis->hGetLen($key); $list = $redis->hGetvals($key); if ($list){ //自动取消分钟数 $cancel_time = getCancelTime(); foreach ($list as $k=>&$v){ $info = json_decode($v,true); $info['pro_info'] = json_decode($info['pro_info'],true); $info['cancel_time'] = date('Y-m-d H:i:s',strtotime($info['create_at'])+($cancel_time*60)); $list[$k] = $info; } } }else{ $where = [ 'is_deleted'=>0, 'status'=>$status, 'mid'=>$this->uid ]; $count = Db::name('store_order')->where($where)->count(); $list = Db::name('store_order') ->where($where) ->field('id,c_id,order_no,num,pro_info,status,create_at,pay_price,pay_type') ->order('id desc') ->limit($this->off_set,$this->page_num) ->select(); foreach ($list as &$v){ $v['pro_info'] = json_decode($v['pro_info'],true); } } $this->success('成功',compact('count','list')); } /** * @title 待支付、已购买订单详情 * @desc 待支付、已购买订单详情 * @author Gavin * @url /api/Order/MyOrderListDetail * @method POST * @header name:Authorization require:1 desc:Token * @param name:order_no type:string default:0 desc:订单号 * @param name:type type:int default:1 desc:1:待支付2:已支付 * * @return name:order_no type:string default:-- desc:订单号 * @return name:num type:int default:-- desc:数量 * @return name:pay_price type:string default:-- desc:订单金额 * @return name:status type:int default:-- desc:类型0:待支付1:已支付 * @return name:cancel_time type:string default:-- desc:待支付自动取消时间 * @return name:create_at type:string default:-- desc:创建时间 * @return name:pay_at type:string default:-- desc:支付时间 * @return name:pay_type type:string default:-- desc:支付方式wx:微信zfb:支付宝 * @return name:pro_info@name type:string default:-- desc:藏品名称 * @return name:pro_info@cover type:string default:-- desc:藏品图片 * @return name:pro_info@price type:string default:-- desc:藏品单价 * @return name:pro_info@auth_img type:string default:-- desc:藏品作者头像 * @return name:pro_info@auth_name type:string default:-- desc:藏品作者名称 */ public function MyOrderListDetail(){ $order_no = input('order_no'); $type = input('type',1); if (!$order_no) $this->error('参数错误'); if ($type==1){ $redis = new Redis(); $key = 'order_not_pay_'.$this->uid; $order = $redis->hGet($key,$order_no); $order = json_decode($order,true); }else{ $order = Db::name('store_order') ->where('order_no',$order_no) ->where('mid',$this->uid) ->where('is_deleted',0) ->find(); } if (!$order) $this->error('订单不存在'); $order['pro_info'] = json_decode($order['pro_info'],true); //自动取消分钟数 $cancel_time = getCancelTime(); if ($order['status']==0){ $order['cancel_time'] = date('Y-m-d H:i:s',strtotime($order['create_at'])+($cancel_time*60)); } $this->success('成功',$order); } /** * @title 待支付订单支付 * @desc 待支付订单支付 * @author Gavin * @url /api/Order/payOrder * @method POST * @header name:Authorization require:1 desc:Token * @param name:order_no type:string require:1 default:-- desc:订单号 * @param name:from type:string require:1 default:wx desc:wx:微信公众号h5:网页 * * @return name:order_no type:int require:0 default:0 desc:订单号 * @return name:pay type:string require:0 default:0 desc:支付信息 */ public function payOrder(){ $user = getMemberInfoHash($this->uid); //获取用户信息 $order_no = input('order_no'); //订单号 $from = input('from','wx'); if (!$order_no) $this->error('参数错误'); $redis = new Redis(); $key = 'order_not_pay_'.$this->uid; $order = $redis->hGet($key,$order_no); if (!$order) $this->error('订单不存在'); $order = json_decode($order,true); if ($order['status']!=0) $this->error('订单已支付或已取消'); $order_no = $order['order_no']; $total_fee = $order['pay_price']; $pay_type = $order['pay_type']; $body = 'top艺术购买藏品'; $com = true; 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); $post_data = [ 'body' => $body, 'out_trade_no' => $order_no, 'total_fee' => $total_fee, 'attach'=>$this->uid, //自定义传值 ]; //trade_type SAPI--JSAPI支付(或小程序支付)、NATIVE--Native支付、APP--app支付,MWEB--H5支付 if ($from=='wx'){ $post_data['openid'] = $user['openid']; $post_data['trade_type'] = 'JSAPI'; }elseif ($from=='h5'){ $post_data['trade_type'] = 'MWEB'; } $result = $app->order->unify($post_data); if ($result['return_msg']=='OK'){ if ($result['result_code']=='FAIL'){ $com = false; }else{ $order1 = $app->jssdk->bridgeConfig($result['prepay_id']);//执行二次签名返回参数 $redis_data['wx_order'] = $order1; $retrun_data['order_no'] = $order_no; $retrun_data['pay'] = json_decode($order1,true); } }else{ $com = false; } break; case 'zfb': $order_nos = get_order_sn(); $order['order_no'] = $order_nos; $redis->hMSet($key,[$order_nos=>json_encode($order,true)]); //存入redis //存入redis $redis->hdel($key,$order_no); $zfb = new AliPay3(); $notify_url = 'https://'.$_SERVER['SERVER_NAME'].'/index.php/api/Pay/alipayOrderNotify';//回调地址 $callback_url = 'https://'.$_SERVER['SERVER_NAME'].'/pages/mine/order-list?num=1'; $order = $zfb->aliPay($body,$total_fee,$order_nos,$notify_url,$this->uid,$callback_url); $retrun_data['order_no'] = $order_nos; $retrun_data['pay'] = $order['pay_url']; break; case 'ylh5': $order_nos = get_order_sn(); $order['order_no'] = $order_nos; $redis->hMSet($key,[$order_nos=>json_encode($order,true)]); //存入redis //存入redis $redis->hdel($key,$order_no); $callback_url = 'https://'.$_SERVER['SERVER_NAME'].'/pages/mine/order-list?num=1'; $huiju = new HuijuH5Pay(); $notify_url = 'https://'.$_SERVER['SERVER_NAME'].'/index.php/api/Pay/ylOrderNotify';//回调地址 $pro_info = json_decode($order['pro_info'],true); $result = $huiju->ylh5Pay($order_nos,$callback_url,$notify_url,$pro_info['name'],$total_fee,$this->uid,2); if ($result['ra_Code']=='100'){ $retrun_data['order_no'] = $order_nos; $retrun_data['pay'] = $result['rc_Result']; }else{ $com = false; } break; case 'zfbh5': $order_nos = get_order_sn(); $order['order_no'] = $order_nos; $redis->hMSet($key,[$order_nos=>json_encode($order,true)]); //存入redis //存入redis $redis->hdel($key,$order_no); $callback_url = 'https://'.$_SERVER['SERVER_NAME'].'/pages/mine/order-list?num=1'; $huiju = new HuijuH5Pay(); $notify_url = 'https://'.$_SERVER['SERVER_NAME'].'/index.php/api/Pay/ylOrderNotify';//回调地址 $pro_info = json_decode($order['pro_info'],true); $result = $huiju->ylh5Pay($order_nos,$callback_url,$notify_url,$pro_info['name'],$total_fee,$this->uid,1); if ($result['ra_Code']=='100'){ $retrun_data['order_no'] = $order_nos; $retrun_data['pay'] = $result['rc_Result']; }else{ $com = false; } break; } if ($com){ $this->success('成功',$retrun_data); }else{ $this->error('调起支付失败,请稍后重试'); } } /** * @title 取消订单 * @desc 取消订单 * @author Gavin * @url /api/Order/cancelOrder * @method POST * @header name:Authorization require:1 desc:Token * @param name:order_no type:string require:1 default:-- desc:订单号 */ public function cancelOrder(){ $order_no = input('order_no'); if (!$order_no) $this->error('参数错误'); $redis = new Redis(); $key = 'order_not_pay_'.$this->uid; $order = $redis->hGet($key,$order_no); if (!$order) $this->error('订单不存在'); $order = json_decode($order,true); if ($order['status']!=0) $this->error('订单已支付或已取消'); $com = true; Db::startTrans(); try { $order['status'] = 2; $order['cancel_at'] = date('Y-m-d H:i:s'); $order['cancel_state'] = 2; Db::name('store_order')->insert($order); Db::commit(); }catch (\Exception $e){ $com=false; Db::rollback(); } if ($com){ //加上库存 addCollectionInventory($order['c_id'],$order['num']); //减少用户购买数量 DecrByCount($this->uid,$order['c_id'],$order['num']); //删除数据 $redis->hdel($key,$order_no); $this->success('取消成功'); }else{ $this->error('取消失败,请稍后重试'); } } /** * @title 我的订单-转赠 * @desc 我的订单-转赠 * @author Gavin * @url /api/Order/getMySendList * @method POST * @header name:Authorization require:1 desc:Token * @param name:page type:int : default:1 desc:页数 * @param name:page_num type:int : default:20 desc:每页数 * * @return name:order_no type:string default:-- desc:订单号 * @return name:over_time type:string default:-- desc:转赠时间 * @return name:status type:int default:-- desc:类型2:赠出3:获赠 * @return name:send_name type:string default:-- desc:转赠者/受赠者 * @return name:collectors_hash type:string default:-- desc:hash * @return name:pro_info@name type:string default:-- desc:藏品名称 * @return name:pro_info@price type:string default:-- desc:藏品价格 * @return name:pro_info@price type:string default:-- desc:藏品单价 * @return name:pro_info@auth_img type:string default:-- desc:藏品作者头像 * @return name:pro_info@auth_name type:string default:-- desc:藏品作者名称 */ public function getMySendList(){ $where = [ 'mid'=>$this->uid, ]; $count = Db::name('store_order_info')->where($where)->whereIn('status','2,3')->count(); $list = Db::name('store_order_info') ->where($where) ->whereIn('status','2,3') ->field('id,order_no,pro_info,over_time,create_at,status,to_mid,collectors_hash') ->order('id desc') ->limit($this->off_set,$this->page_num) ->select(); foreach ($list as &$v){ $v['pro_info'] = json_decode($v['pro_info'],true); $v['send_name'] = Db::name('store_member')->where('id',$v['to_mid'])->value('name'); } $this->success('成功',compact('count','list')); } /** * @title 转赠订单详情 * @desc 转赠订单详情 * @author Gavin * @url /api/Order/getMySendListDetail * @method POST * @header name:Authorization require:1 desc:Token * @param name:order_id type:int : default:1 desc:订单ID * * @return name:order_no type:string default:-- desc:订单号 * @return name:over_time type:string default:-- desc:转赠时间 * @return name:status type:int default:-- desc:类型2:赠出3:获赠 * @return name:send_name type:string default:-- desc:转赠者/受赠者 * @return name:collectors_hash type:string default:-- desc:hash * @return name:pro_info@name type:string default:-- desc:藏品名称 * @return name:pro_info@price type:string default:-- desc:藏品价格 * @return name:pro_info@price type:string default:-- desc:藏品单价 * @return name:pro_info@auth_img type:string default:-- desc:藏品作者头像 * @return name:pro_info@auth_name type:string default:-- desc:藏品作者名称 */ public function getMySendListDetail(){ $order_id = input('order_id'); //订单id if (!$order_id) $this->error('参数错误'); $order = Db::name('store_order_info') ->where('id',$order_id) ->where('mid',$this->uid) ->find(); if (!$order) $this->error('订单不存在'); $order['pro_info'] = json_decode($order['pro_info'],true); $order['send_name'] = Db::name('store_member')->where('id',$order['to_mid'])->value('name'); $this->success('成功',$order); } function savetest($order_no){ $result['out_trade_no'] = $order_no; $order = Db::name('store_order')->where('order_no',$result['out_trade_no'])->find(); if ($order['status']==0){ Db::startTrans(); try { Db::name('store_order') ->where('order_no',$result['out_trade_no']) ->update(['status'=>1,'pay_at'=>date('Y-m-d H:i:s'),'return_success_info'=>json_encode($result,true)]); $array = []; for ($i=0;$i<$order['num'];$i++){ //获取排名 $rank = getRanking($order['c_id'])+1; $tag = getTag($order['c_id'],$rank,$order['inventory']); saveRanking($order['c_id']); $company = '象寻数字科技(上海)有限公司'; $company_hash = ''; $company_hash_time = ''; $collectors_hash = ''; $date = [ 'order_id'=>$order['id'], 'order_no'=>get_order_sn(), 'tag'=>$tag, 'mid'=>$order['mid'], 'pro_info'=>$order['pro_info'], 'company'=>$company, 'company_hash'=>$company_hash, 'company_hash_time'=>$company_hash_time, 'collectors_hash'=>$collectors_hash, 'collectors_hash_time'=>date('Y-m-d H:i:s') ]; $array[] = $date; } Db::name('store_order_info')->insertAll($array); Db::commit(); // return true; } catch (\Exception $e){ Db::rollback(); //return false; } } } }