uid)) $this->error('别处登录,请重新登录','',0,99); $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 ($this->uid!='100000'){ 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) $this->error('hash未铸造,无法购买'); //if (!$hashCount || $hashCount<$num) $this->error('hash未铸造,无法购买'); //redis加锁判断 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_order')->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_order')->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; case 'yue': Db::name('store_collection')->where('id',$id)->setDec('now_inventory',$num); $retrun_data['order_no'] = $order_no; $retrun_data['pay'] = ['order_no'=>$order_no]; Db::commit(); break; } }catch (\Exception $e){ $com=false; Db::rollback(); } if ($com){ DelRedisSetNx($id); //删除锁 $this->success('成功',$retrun_data); }else{ //加上库存 addCollectionInventory($id,$num); //加上抢购卡数量 addMembercard($this->uid); //减少用户购买数量 DecrByCount($this->uid,$id,$num); DelRedisSetNx($id); //删除锁 $this->error('服务器繁忙,请稍后重试'); } }else{ $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); $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(); //自动取消分钟数 $cancel_time = getCancelTime(); foreach ($list as &$v){ $v['pro_info'] = json_decode($v['pro_info'],true); if ($v['status']==0){ $v['cancel_time'] = date('Y-m-d H:i:s',strtotime($v['create_at'])+($cancel_time*60)); } } $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_id type:int : default:0 desc:订单ID * * @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_id = input('order_id'); if (!$order_id) $this->error('参数错误'); $order = Db::name('store_order') ->where('id',$order_id) ->where('mid',$this->uid) ->where('is_deleted',0) ->find(); if (!$order) $this->error('订单不存在'); //自动取消分钟数 $cancel_time = getCancelTime(); $order['pro_info'] = json_decode($order['pro_info'],true); 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 M * @url /api/Order/walletPayOrder * @method POST * @header name:Authorization require:1 desc:Token * @param name:order_no type:int require:1 default:-- desc:订单号 * @param name:pay_pass type:int require:1 default:-- desc:二级密码 */ public function walletPayOrder(){ $order_no = input('order_no'); //订单号 $pay_pass = input('pay_pass'); //二级密码 if (!$order_no) $this->error('参数错误'); if (!$pay_pass) $this->error('请输入二级密码'); $pay_way = Db::name('SystemPayWay')->where('code','yue')->field('status')->find(); if(empty($pay_way)) $this->error('余额支付不存在'); if($pay_way['status'] != 1) $this->error('余额支付已关闭'); $order = Db::name('store_order')->where('order_no',$order_no)->where('is_deleted',0)->find(); if (!$order) $this->error('订单不存在'); if ($order['status']!=0) $this->error('订单已支付或已取消'); if ($order['pay_type']!='yue') $this->error('该订单无法使用余额支付'); $member = Db::name('store_member') ->where('id', $order['mid']) ->where('is_deleted',0) ->field('second_password,money') ->find(); if(empty($member)) $this->error('用户信息不正确'); if (empty($member['second_password'])) $this->error('未设置支付密码'); if ($member['second_password']!=md5($pay_pass)) $this->error('密码错误'); $money = $member['money']; $total_fee = $order['pay_price']; if($money < $total_fee) $this->error('账户余额不足'); $result['out_trade_no'] = $order_no; $return = $this->dealData($result); if ($return){ $this->success('支付成功'); }else{ $this->error('支付失败'); } } /** * 处理数据库信息 * @param $result * @return bool */ function dealData($result){ Db::startTrans(); try { $order = Db::name('store_order')->where('order_no',$result['out_trade_no'])->find(); // if($order['status'] != 0){ // return true; // } 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)]); $this->reduceMoney($order['mid'],$order['pay_price']); $info = Db::name('store_order_info')->where('order_id',$order['id'])->count(); if ($info>=$order['num']){ Db::commit(); return 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 = '象寻数字科技(上海)有限公司'; // $hash = getCompanyHash($order['c_id']); // $company_hash = $hash['hash']; // $ddcid = Db::name('hash')->where('hash',$hash['hash'])->value('ddcid'); // $company_hash_time = $hash['create_at'] ? $hash['create_at'] : date('Y-m-d H:i:s'); // Db::name('hash')->where('hash',$hash['hash'])->update(['status'=>1]); $company_hash = Db::name('hash2')->where('goods_id',$order['c_id'])->where('success',1)->field('hash,ddcid')->find(); $company_hash_time = date('Y-m-d H:i:s'); $collectors_hash = ''; $date = [ 'order_id'=>$order['id'], 'order_no'=>get_order_sn(), 'tag'=>$tag, 'mid'=>$order['mid'], 'c_id'=>$order['c_id'], 'pro_info'=>$order['pro_info'], 'company'=>$company, 'company_hash'=>$company_hash['hash'], 'company_hash_time'=>$company_hash_time, 'ddcid'=>$company_hash['ddcid'], 'collectors_hash'=>$collectors_hash, 'collectors_hash_time'=>'', 'type'=>2 ]; $array[] = $date; } Db::name('store_order_info')->insertAll($array); //送积分 $by_collection_integral = getConfigValue('by_collection_integral'); if ($by_collection_integral){ $by_collection_integral = bcmul($by_collection_integral,$order['num']); memberMoneyChange($by_collection_integral,1,$order['mid'],'购买藏品',1,$order['id']); } Db::commit(); return true; } catch (\Exception $e){ Db::rollback(); return false; } } function reduceMoney($mem_id,$money){ if($money <= 0) return ['code'=>true,'message'=>'成功']; $old_money = Db::name('StoreMember') -> where('id',$mem_id)->field('id,money')->find(); Db::name('StoreMember') -> where('id',$mem_id)->setDec('money',$money); $sre_data['mem_id']=$mem_id; $sre_data['change']=$money; $sre_data['pm']=0; $sre_data['old_money']=$old_money['money']; $sre_data['reason']='购买藏品'; $sre_data['time']=time(); Db::name('SystemMoneyRecord')->insert($sre_data); } /** * @title 待支付订单支付 * @desc 待支付订单支付 * @author Gavin * @url /api/Order/payOrder * @method POST * @header name:Authorization require:1 desc:Token * @param name:order_id type:int require:1 default:-- desc:订单id * * @return name:order_no type:int require:0 default:0 desc:订单号 * @return name:pay type:string require:0 default:0 desc:支付信息 */ public function payOrder(){ $order_id = input('order_id'); //订单id if (!$order_id) $this->error('参数错误'); $order = Db::name('store_order')->where('id',$order_id)->where('is_deleted',0)->find(); if (!$order) $this->error('订单不存在'); if ($order['status']!=0) $this->error('订单已支付或已取消'); $order_no = $order['order_no']; $total_fee = $order['pay_price']; $pay_type = $order['pay_type']; $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_order')->where('order_no', $order_no)->update(['wx_order' => json_encode($order1, true)]); $retrun_data['order_no'] = $order_no; $retrun_data['pay'] = $order1; $this->success('成功',$retrun_data); }else{ $this->error('调起支付失败,请稍后重试'); } break; case 'zfb': $zfb = new AliPay(); $notify_url = $this->request->root(true).'/api/Pay/alipayOrderNotify';//回调地址 $order1 = $zfb->aliPay($body, $total_fee, $order_no, $notify_url);//调用支付宝支付的方法 $retrun_data['order_no'] = $order_no; $retrun_data['pay'] = $order1; $this->success('成功',$retrun_data); break; } } /** * @title 取消订单 * @desc 取消订单 * @author Gavin * @url /api/Order/cancelOrder * @method POST * @header name:Authorization require:1 desc:Token * @param name:order_id type:int require:1 default:-- desc:订单id */ public function cancelOrder(){ $order_id = input('order_id'); //订单id if (!$order_id) $this->error('参数错误'); $order = Db::name('store_order')->where('id',$order_id)->where('is_deleted',0)->find(); if (!$order) $this->error('订单不存在'); if ($order['status']!=0) $this->error('订单已支付或已取消'); $com = true; Db::startTrans(); try { //修改订单状态 Db::name('store_order')->where('id',$order_id)->update(['status'=>2,'cancel_at'=>date('Y-m-d H:i:s'),'cancel_state'=>2]); //增加数据库库存 Db::name('store_collection')->where('id',$order['c_id'])->setInc('now_inventory',$order['num']); //增加用户抢购卡数量 Db::name('store_member')->where('id',$order['mid'])->setInc('snap_card'); Db::commit(); }catch (\Exception $e){ $com=false; Db::rollback(); } if ($com){ //加上库存 addCollectionInventory($order['c_id'],$order['num']); //加上抢购卡数量 addMembercard($this->uid); //减少用户购买数量 DecrByCount($this->uid,$order['c_id'],$order['num']); $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; } } } }