123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689 |
- <?php
- namespace app\api\controller;
- use think\Db;
- use EasyWeChat\Factory;
- use think\Session;
- /**
- * @title 抽奖商品订单
- * @controller GoodsOrder
- * @group base
- */
- class GoodsOrder extends Base
- {
- public function initialize()
- {
- parent::initialize();
- parent::check_login();
- }
- /**
- * @title 获取订单详情
- * @desc 获取订单详情
- * @author qc
- * @url /api/Goods_order/getOrderDetail
- * @method GET
- * @header name:Authorization require:1 desc:Token
- * @param name:id type:int require:1 default:-- desc:订单id
- * @return name:order_no type:string default:-- desc:订单号
- * @return name:status type:int default:-- desc:支付状态:0待支付,1已支付(待发货),2已发货(待收货),3已收货(待评论)4已完成,9取消
- * @return name:price_total type:float default:-- desc:订单金额
- * @return name:pay_state type:int default:-- desc:支付状态(0未支付,1已支付)
- * @return name:pay_no type:string default:-- desc:支付单号
- * @return name:create_at type:string default:-- desc:订单创建时间
- * @return name:pay_type type:string default:-- desc:支付方式(0未支付,1明信片,2余额)
- * @return name:pay_time type:string default:-- desc:支付时间
- * @return name:cancel_state type:int default:-- desc:取消状态
- * @return name:goods_id type:int default:-- desc:商品id
- * @return name:goods_num type:int default:-- desc:商品数量
- * @return name:goods_cover type:int default:-- desc:商品封面
- */
- public function getOrderDetail()
- {
- $order_id = input('id',0);
- $detail = Db::table('goods_order')->find($order_id);
- $this->success('获取成功',$detail);
- }
- /**
- * @title 立即购买
- * @desc 立即购买
- * @author qc
- * @url /api/Goods_order/payOrderImmediately
- * @method POST
- * @header name:Authorization require:1 desc:Token
- * @param name:goods_id type:int require:1 default:-- desc:商品id
- * @param name:num type:int require:1 default:1 desc:商品数量
- * @param name:pay_type type:int require:1 default:1 desc:支付方式(1明信片,2余额)
- * @return name:order_id type:int require:0 default:0 desc:成功时返回订单id
- */
- public function payOrderImmediately()
- {
- $goods_id = input("post.goods_id");
- $num = input("post.num",1);
- $pay_type = input("post.pay_type",1);
- $goods_info = Db::table('store_goods')->where(['id'=>$goods_id,'status'=>1,'is_deleted'=>0])
- ->find();
- $price_total = bcmul($goods_info['goods_price'],$num,2);
- if(empty($goods_info)) $this->error('商品已下架');
- //if(!$goods_info['end_time'] || strtotime($goods_info['end_time']) < time()) $this->error('商品售卖时间已结束');
- if($goods_info['stock'] < $num) $this->error('商品库存不足');
- $user_info = Db::table('store_member')->find($this->uid);
- if($price_total <= 0) $this->error('金额有误');
- if(!in_array($pay_type,[1,2])) $this->error('支付方式有误!');
- if($pay_type == 1 && $user_info['crystal'] < $price_total) $this->error('明信片不足!');
- if($pay_type == 2 && $user_info['crystal_cash'] < $price_total) $this->error('余额不足!');
- $order_info =[
- 'uid'=>$this->uid,
- 'order_no' => get_order_sn(),
- 'price_total' => $price_total,
- 'goods_num' => $num,
- 'goods_id' => $goods_id,
- 'goods_cover' => $goods_info['cover'],
- 'goods_name' => $goods_info['name'],
- 'create_at' => date('Y-m-d H:i:s'),
- 'pay_type' => $pay_type,
- ];
- Db::startTrans();
- $msg= '';$com=true;
- try{
- //扣减库存(先扣库存在抽奖)
- Db::table('store_goods')->where('id',$goods_id)->setDec('stock',$num);
- // 生成订单
- Db::table('goods_order')->insert($order_info);
- $order_id = Db::getLastInsID();
- $order_info['id'] = $order_id;
- $goods_info = Db::table('store_goods')->where('id',$goods_id)->find();
- // 重新验证库存
- if($goods_info['stock'] < 0 ) {
- Db::rollback();
- $com = false;
- $msg = '库存不足';
- }else{
- // 抽奖设置
- $goods_spec = json_decode($goods_info['spec'],true);
- $is_clip = 0;
- $st_num = bcmul($goods_info['base_stock'],0.7);// 1000
- $end_num = bcmul($goods_info['base_stock'],0.2);
- if($goods_info['base_stock'] > 30) {
- if( $goods_info['stock'] < $st_num && $goods_info['stock'] > $end_num) {
- $is_clip = 1;
- }
- }
- // 抽奖
- if($goods_info['base_stock'] > 80 ) {
- $draw_data = draw_lottery($goods_spec,'store_num',$order_info['goods_num'],$is_clip);
- }else{
- $draw_data = point_lottery($goods_spec,'store_num',$order_info['goods_num'],$goods_info['base_stock'] - $goods_info['stock']);
- }
- $magic_data = [];
- $magic_day = intval(sysconf('magic_days'));// 获取设置天数
- // 扣减商品库存数组 && 魔玩柜数据
- foreach ($draw_data as $dk=>$dv) {
- $goods_spec[$dk]['store_num'] -= $dv;
- $magic_data[] = [
- 'order_id' => $order_id,
- 'goods_id' => $goods_info['id'],
- 'user_id' => $this->uid,
- 'create_at' => date('Y-m-d H:i:s'),
- 'case_name' => $goods_spec[$dk]['award_name'],
- 'case_cover'=> $goods_spec[$dk]['spec_img'],
- 'num' => $dv,
- 'base_num' => $dv,
- 'crystal' => $goods_spec[$dk]['crystal'],
- 'past_at' => date('Y-m-d H:i:s',strtotime("+$magic_day days")),
- 'past_int' => strtotime("+$magic_day days"),
- 'title' => $goods_spec[$dk]['title'],
- 'color' => $goods_spec[$dk]['color'],
- ];
- }
- $magic_draw = [];// 待抽将数据
- foreach ($magic_data as $mv)
- {
- for ($i=0;$i< $mv['base_num'];$i++ ) {
- $magic_draw[]=[
- 'order_id' => $order_info['id'],
- 'goods_id' => $goods_info['id'],
- 'user_id' => $order_info['uid'],
- 'create_at' => date('Y-m-d H:i:s'),
- 'status' => 0,
- 'award_status' => 1,
- 'award_name' => $mv['case_name'],
- 'award_cover' => $mv['case_cover'],
- 'title' => $mv['title'],
- 'color_num' =>$mv['color'],
- ];
- }
- }
- // 推荐线奖励 start
- if($user_info['pid'])
- {
- $parent_info = Db::table('store_member')->find($user_info['pid']);
- $first_rate = intval(sysconf('first_rebate'));
- if($first_rate > 0) {
- $first_money = bcmul($order_info['price_total'],$first_rate/100,2);
- Db::table('store_member')->where(['id'=>$user_info['pid']])
- ->update(['crystal'=>bcadd($parent_info['crystal'],$first_money,2)]);
- crystal_log($user_info['pid'],$first_money,'一级推荐线奖励',12,$order_id);
- }
- if($parent_info['pid'] > 0)
- {
- $grand = Db::table('store_member')->find($parent_info['pid']);
- $second_rate = intval(sysconf('second_rebate'));
- if($second_rate > 0) {
- $second_money =bcmul($order_info['price_total'],$second_rate/100,2);
- Db::table('store_member')->where(['id'=>$grand['pid']])
- ->update(['crystal'=>bcadd($grand['crystal'],$second_money,2)]);
- crystal_log($parent_info['pid'],$second_money,'二级推荐线奖励',13,$order_id);
- }
- }
- }
- // 推荐线奖励 end
- // 更新库存 && 规格详情
- Db::table('store_goods')->where('id',$goods_info['id'])->update(['spec'=>json_encode($goods_spec)]);
- // 魔玩柜
- Db::table('magic_case')->insertAll($magic_data);
- // 抽奖数据
- Db::table('magic_draw')->insertAll($magic_draw);
- // 扣除余额
- if($pay_type == 1) {
- Db::table('store_member')->where(['id'=>$this->uid])->update(['crystal'=>bcsub($user_info['crystal'],$order_info['price_total'],2)]);
- crystal_log($this->uid,$order_info['price_total'],'明信片支付',6);
- }else{
- Db::table('store_member')->where(['id'=>$this->uid])->update(['crystal_cash'=>bcsub($user_info['crystal_cash'],$order_info['price_total'],2)]);
- balance_log($this->uid,$order_info['price_total'],'余额支付',6);
- }
- // 更新订单状态
- $pay_no = get_order_sn();
- Db::table('goods_order')->where(['uid'=>$this->uid,'id'=>$order_id])
- ->update(['pay_time'=>date('Y-m-d H:i:s'),'pay_state'=>1,'status'=>1,'pay_no'=>$pay_no]);
- // 特殊奖励
- $special_award = json_decode($goods_info['special_award'],true);
- $special_point = $goods_info['special_point'];
- $front_court = $special_award[0];// 前场设置
- $back_court = $special_award[1]; // 后场设置
- $is_draw = 0;
- // 前场未抽奖
- if($front_court['is_over'] == 0) {
- $front_magic = Db::table('magic_draw')
- ->field('id,user_id,order_id')
- ->where(['goods_id'=>$goods_info['id'],'status'=>0,'is_special'=>0,'is_last'=>0])
- ->limit(0,$special_point)
- ->order('id asc')
- ->select();
- // 前场抽奖次数达到前场抽奖节点
- if(count($front_magic) == $special_point) {
- $is_draw = 1;
- $special_award[0]['is_over'] = 1;// 前场抽奖完成标识
- $rand_id = array_rand($front_magic,1);// 中奖记录ids
- $front_user = $front_magic[$rand_id]['user_id']; // 中奖会员id
- $last_id = $front_magic[count($front_magic)-1]['id'];
- // 设置了奖励
- if($front_court['goods']) {
- // 中奖记录插入魔玩柜
- $front_case = [
- 'order_id' => $front_magic[$rand_id]['order_id'],
- 'goods_id' => $goods_info['id'],
- 'user_id' => $front_user,
- 'create_at' => date('Y-m-d H:i:s'),
- 'num' => 1,
- 'base_num' => 1,
- 'source' => 1,
- 'crystal' => $front_court['crystal'],
- 'case_name' => $front_court['goods'],
- 'case_cover' =>$front_court['cover'],
- 'past_at' => date('Y-m-d H:i:s',strtotime("+$magic_day days")),
- 'past_int' => strtotime("+$magic_day days"),
- ];
- Db::table('magic_case')->insert($front_case);
- $special_award1 = [
- 'order_id' => $order_info['id'],
- 'goods_id' => $goods_info['id'],
- 'user_id' => $front_user,
- 'create_at' => date('Y-m-d H:i:s'),
- 'status' => 1,
- 'award_status' => 0,
- 'award_name' => $front_court['goods'],
- 'award_cover' =>$front_court['cover'],
- 'is_special' => 1,
- 'title' => $front_court['title'],
- 'color_num' => $front_court['color'],
- ];
- Db::table('magic_draw')->insert($special_award1);
- // 生成消息记录
- send_user_message($front_user,1,'恭喜您被大奖砸中!获得商品:'.$front_court['goods']);
- }
- // 更新抽奖记录
- Db::table('magic_draw')
- ->where('goods_id','=',$goods_info['id'])->where('id','<=', $last_id)
- ->where('id','<>',$front_magic[$rand_id]['id'])
- ->where('status','=',0)
- ->where('is_special','=',0)
- ->update(['status'=>1]);
- Db::table('magic_draw')
- ->where('id','=', $front_magic[$rand_id]['id'])
- ->where('status','=',0)
- ->where('is_special','=',0)
- ->update(['status'=>1,'award_status'=>2]);
- }
- }
- // 后场未抽奖 && 商品售罄
- if( $special_award[0]['is_over'] ==1 && $back_court['is_over'] == 0 && $goods_info['stock'] == 0){
- $back_num = $goods_info['base_stock'] - $special_point;
- $back_magic = Db::table('magic_draw')
- ->field('id,user_id,order_id')
- ->where(['goods_id'=>$goods_info['id'],'status'=>0,'is_special'=>0,'is_last'=>0])
- ->limit(0,$back_num)
- ->order('id asc')
- ->select();
- if($back_num == count($back_magic)) {
- $is_draw = 1;
- $special_award[1]['is_over'] = 1;// 后场抽奖完成标识
- $rand_id = array_rand($back_magic,1);// 中奖记录id
- $back_user = $back_magic[$rand_id]['user_id']; // 中奖会员id
- if( $back_court['goods']) {
- $back_case = [
- 'order_id' => $back_magic[$rand_id]['order_id'],
- 'goods_id' => $goods_info['id'],
- 'user_id' => $back_user,
- 'create_at' => date('Y-m-d H:i:s'),
- 'num' => 1,
- 'base_num' => 1,
- 'source' => 2,
- 'crystal' => $back_court['crystal'],
- 'case_name' => $back_court['goods'],
- 'case_cover' =>$back_court['cover'],
- 'past_at' => date('Y-m-d H:i:s',strtotime("+$magic_day days")),
- 'past_int' => strtotime("+$magic_day days"),
- ];
- Db::table('magic_case')->insert($back_case);
- $special_award2 = [
- 'order_id' => $order_info['id'],
- 'goods_id' => $goods_info['id'],
- 'user_id' => $back_user,
- 'create_at' => date('Y-m-d H:i:s'),
- 'status' => 1,
- 'award_status' => 0,
- 'award_name' => $back_court['goods'],
- 'award_cover' =>$back_court['cover'],
- 'is_special' => 1,
- 'title' => $back_court['title'],
- 'color_num' => $back_court['color'],
- ];
- Db::table('magic_draw')->insert($special_award2);
- // 生成消息记录
- send_user_message($back_user,1,'恭喜您被大奖砸中!!获得商品:'.$back_court['goods']);
- }
- // 更新抽奖记录
- Db::table('magic_draw')
- ->where('goods_id','=',$goods_info['id'])->where('status','=', 0)
- ->where('id','<>',$back_magic[$rand_id]['id'])
- ->where('is_special','=',0)
- ->update(['status'=>1]);
- Db::table('magic_draw')->where('id','=', $back_magic[$rand_id]['id'])
- ->where('is_special','=',0)
- ->update(['status'=>1,'award_status'=>3]);
- }
- // 场终奖励
- $last_award = json_decode($goods_info['last_award'],true);
- if(!empty($last_award))
- {
- // 全局奖励
- if($last_award[0]['goods']) {
- $last_award[0]['is_over'] = 1;
- $last_all = rand(0,$goods_info['base_stock']-1);
- $last_info = Db::table('magic_draw')
- ->where(['goods_id'=>$goods_info['id']])
- ->limit($last_all,1)
- ->find();
- $qj_user = $last_info['user_id'];
- $qj_case = [
- 'order_id' => $last_info['order_id'],
- 'goods_id' => $goods_info['id'],
- 'user_id' => $qj_user,
- 'create_at' => date('Y-m-d H:i:s'),
- 'num' => 1,
- 'base_num' => 1,
- 'source' => 3,
- 'crystal' => $last_award[0]['crystal'],
- 'case_name' => $last_award[0]['goods'],
- 'case_cover' =>$last_award[0]['cover'],
- 'past_at' => date('Y-m-d H:i:s',strtotime("+$magic_day days")),
- 'past_int' => strtotime("+$magic_day days"),
- ];
- Db::table('magic_case')->insert($qj_case);
- $last_award1 = [
- 'order_id' => $order_info['id'],
- 'goods_id' => $goods_info['id'],
- 'user_id' => $last_info['user_id'],
- 'create_at' => date('Y-m-d H:i:s'),
- 'status' => 1,
- 'award_status' => 0,
- 'award_name' => $last_award[0]['goods'],
- 'award_cover' =>$last_award[0]['cover'],
- 'is_last' => 1,
- 'title' => $last_award[0]['title'],
- 'color_num' => $last_award[0]['color'],
- ];
- Db::table('magic_draw')->insert($last_award1);
- }
- // W奖励
- if($last_award[1]['goods']) {
- $last_award[1]['is_over'] = 1;
- $w_case = [
- 'order_id' => $order_id,
- 'goods_id' => $goods_info['id'],
- 'user_id' => $this->uid,
- 'create_at' => date('Y-m-d H:i:s'),
- 'num' => 1,
- 'base_num' => 1,
- 'source' => 4,
- 'crystal' => $last_award[1]['crystal'],
- 'case_name' => $last_award[1]['goods'],
- 'case_cover' =>$last_award[1]['cover'],
- 'past_at' => date('Y-m-d H:i:s',strtotime("+$magic_day days")),
- 'past_int' => strtotime("+$magic_day days"),
- ];
- Db::table('magic_case')->insert($w_case);
- $last_award_w = [
- 'order_id' => $order_info['id'],
- 'goods_id' => $goods_info['id'],
- 'user_id' => $this->uid,
- 'create_at' => date('Y-m-d H:i:s'),
- 'status' => 1,
- 'award_status' => 0,
- 'award_name' => $last_award[1]['goods'],
- 'award_cover' =>$last_award[1]['cover'],
- 'is_last' => 1,
- 'title' => $last_award[1]['title'],
- 'color_num' => $last_award[1]['color'],
- ];
- Db::table('magic_draw')->insert($last_award_w);
- }
- }
- Db::table('store_goods')->where('id',$goods_info['id'])->update(['last_award'=>json_encode($last_award)]);
- }
- // 更新商品抽奖详情
- if($is_draw) Db::table('store_goods')->where('id',$goods_info['id'])->update(['special_award'=>json_encode($special_award)]);
- Db::commit();
- }
- }catch (\Exception $e){
- Db::rollback();
- $com=false;
- $msg = $e->getMessage();
- }
- if($com) {
- $this->success('ok',['order_id'=>$order_id]);
- }else{
- $this->error($msg);
- }
- }
- /**
- * @title 获取订单魔玩柜(抽奖详情)
- * @desc 获取订单列表
- * @author qc
- * @url /api/Goods_order/getMagicCaseByOrderId
- * @method GET
- * @header name:Authorization require:1 desc:Token
- * @param name:order_id type:int require:1 default:-- desc:订单id
- * @return name:num type:int default:0 desc:数据总条数
- * @return name:list type:array default:-- desc:数据列表
- * @return name:list.order_id type:int default:0 desc:关联的订单id
- * @return name:list.create_at type:string default:0 desc:抽中时间
- * @return name:list.case_name type:string default:0 desc:名称
- * @return name:list.case_cover type:string default:0 desc:封面
- * @return name:list.num type:string default:0 desc:数量
- * @return name:list.crystal type:float default:0.00 desc:每个明信片价值
- * @return name:list.title type:float default:-- desc:标题
- */
- public function getMagicCaseByOrderId()
- {
- $order_id = input('order_id');
- $list = Db::table('magic_case')
- ->field('id,order_id,create_at,case_name,case_cover,num,crystal,title')
- ->where(['order_id'=>$order_id,'user_id'=>$this->uid,'source'=>0])->select();
- $this->success('ok',['list'=>$list,'num'=>empty($list) ? 0: count($list)]);
- }
- /**
- * @title 取消订单
- * @desc 取消订单
- * @author qc
- * @url /api/Goods_order/cancelOrder
- * @method POST
- * @header name:Authorization require:1 desc:Token
- * @param name:id type:int require:1 default:-- desc:订单id
- */
- public function cancelOrder()
- {
- $order_id = input('post.id');
- $order_detail = Db::table('goods_order')->where(['id'=>$order_id,'uid'=>$this->uid])->find();
- if(empty($order_detail)) $this->error('订单不存在');
- if($order_detail['pay_state'] != 0) $this->error('订单已支付');
- if($order_detail['cancel_state'] != 0) $this->error('订单已取消,不能重复取消');
- Db::startTrans();
- try {
- //恢复库存
- Db::table('store_goods')->where('id',$order_detail['goods_id'])->setInc('stock',$order_detail['goods_num']);
- // 取消订单
- Db::table('goods_order')
- ->where(['id'=>$order_id])
- ->update(['cancel_state'=>1,'status'=>9]);
- Db::commit();
- }catch (\Exception $e){
- Db::rollback();
- }
- $this->success('取消成功');
- }
- /**
- * @title 获取订单列表
- * @desc 获取订单列表
- * @author qc
- * @url /api/Goods_order/getMyOrderList
- * @method GET
- * @header name:Authorization require:1 desc:Token
- * @param name:page type:int : default:1 desc:页数
- * @param name:page_num type:int : default:20 desc:每页数
- * @param name:status type:int : default:-1 desc:订单状态(-1全部,0待支付,1已支付(待发货),2已发货(待收货),3已收货(待评论)4已完成,9取消)
- * @return name:order_no type:string default:-- desc:订单号
- * @return name:status type:int default:-- desc:支付状态:0待支付,1已支付(待发货),2已发货(待收货),3已收货(待评论)4已完成,9取消
- * @return name:price_total type:float default:-- desc:订单金额
- * @return name:pay_state type:int default:-- desc:支付状态(0未支付,1已支付)
- * @return name:pay_no type:string default:-- desc:支付单号
- * @return name:create_at type:string default:-- desc:订单创建时间
- * @return name:pay_type type:string default:-- desc:支付方式(0未支付,1明信片,2余额)
- * @return name:pay_time type:string default:-- desc:支付时间
- * @return name:cancel_state type:int default:-- desc:取消状态
- * @return name:goods_id type:int default:-- desc:商品id
- * @return name:goods_num type:int default:-- desc:商品数量
- * @return name:goods_cover type:int default:-- desc:商品封面
- */
- public function getMyOrderList()
- {
- $status = input('status',-1);
- $where=[];
- if($status > -1) $where['o.status'] = $status;
- $where['o.uid'] = $this->uid;
- $where['o.is_deleted'] = 0;
- $list = Db::table('goods_order o')
- ->field('o.id,o.order_no,o.pay_state,o.create_at,pay_time,o.price_total,o.status,pay_type,goods_id,goods_num,goods_cover')
- ->where($where)
- ->order('o.id desc')
- ->limit($this->off_set,$this->page_num)
- ->select();
- $this->success('获取成功',$list);
- }
- /**
- * @title 获取订单列表
- * @desc 获取订单列表
- * @author qc
- * @url /api/Goods_order/getOrderList
- * @method GET
- * @header name:Authorization require:1 desc:Token
- * @param name:page type:int : default:1 desc:页数
- * @param name:page_num type:int : default:20 desc:每页数
- * @param name:status type:int : default:-1 desc:订单状态(-1全部,0待支付,1已支付(待发货),2已发货(待收货),3已收货(待评论)4已完成,9取消)
- * @return name:order_no type:string default:-- desc:订单号
- * @return name:status type:int default:-- desc:支付状态:0待支付,1已支付(待发货),2已发货(待收货),3已收货(待评论)4已完成,9取消
- * @return name:price_total type:float default:-- desc:订单金额
- * @return name:freight type:float default:-- desc:运费
- * @return name:pay_state type:int default:-- desc:支付状态(0未支付,1已支付)
- * @return name:pay_no type:string default:-- desc:支付单号
- * @return name:create_at type:string default:-- desc:订单创建时间
- * @return name:pay_type type:string default:-- desc:支付方式(0未支付,1明信片,2余额)
- * @return name:pay_time type:string default:-- desc:支付时间
- * @return name:cancel_state type:int default:-- desc:取消状态
- * @return name:goods_id type:int default:-- desc:商品id
- * @return name:goods_num type:int default:-- desc:商品数量
- * @return name:goods_cover type:int default:-- desc:商品封面
- * @return name:goods_num type:int default:-- desc:商品数量
- * @return name:express_company_title type:string default:-- desc:物流公司
- * @return name:express_send_no type:string default:-- desc:物流单号
- * @return name:express_send_at type:string default:-- desc:发货时间
- * @return name:express_company_id type:string default:-- desc:物流公司id
- * @return name:draw_time type:string default:-- desc:抽中时间
- * @return name:table_type type:int default:-- desc:1兑换订单2魔玩柜订单
- *
- */
- public function getOrderList()
- {
- $status = input('status',-1);
- $uid = $this->uid;
- $field = 'id,uid,order_no,price_total,create_at,pro_info,pay_time,pay_no,pay_state,status,table_type,freight,express_company_title,express_send_no,express_send_at,express_company_id';
- $where = $status == -1 ? 'uid = '.$uid.' AND is_deleted = 0':'status = '.$status.' AND uid = '.$uid.' AND is_deleted = 0';
- $sql = "select * from (select {$field} FROM magic_order WHERE {$where} union all select {$field} FROM exchange_order WHERE {$where}) as tab order by tab.create_at desc, tab.id desc "." LIMIT $this->off_set,$this->page_num";
- $res = Db::query($sql);
- if($res){
- foreach ($res as &$value){
- $value['pro_info'] = json_decode($value['pro_info'],true);
- if($value['table_type'] == 1){ //兑换订单
- $order_info = Db::name('exchange_order')->field('cl_id,goods_id,goods_name,goods_cover,freight,crystal')->where('id',$value['id'])->find();
- foreach ($order_info as $ok=>$ov) {
- $value[$ok]=$ov;
- }
- }else{
- // 魔玩柜订单
- $order_info = Db::name('magic_order')->field('pay_type,coupon_id')->where('id',$value['id'])->find();
- $value['pay_type'] = $order_info['pay_type'];
- $value['coupon_id'] = $order_info['coupon_id'];
- }
- }
- }
- $this->success('获取成功',$res);
- }
- /**
- * @title 删除订单
- * @desc 删除订单
- * @author qc
- * @url /api/Goods_order/delOrder
- * @method POST
- * @header name:Authorization require:1 desc:Token
- * @param name:id type:int require:1 default:1 desc:订单id
- */
- public function delOrder()
- {
- $order_id = input('post.id');
- $order_info = Db::table('goods_order')
- ->where(['uid'=>$this->uid,'is_deleted'=>0,'id'=>$order_id])
- ->find();
- if(empty($order_info)) $this->error('订单不存在');
- Db::table('goods_order')->where(['id'=>$order_id])->update(['is_deleted'=>1]);
- $this->success('删除成功');
- }
- /**
- * @title 收藏商品
- * @desc 收藏商品
- * @author qc
- * @url /api/Goods_order/goodsCollect`
- * @method POST
- * @header name:Authorization require:1 desc:Token
- * @param name:goods_id type:int require:1 default:0 desc:商品id
- */
- public function goodsCollect()
- {
- $goods_id = input('post.goods_id');
- if(!$goods_id) $this->error('请选择商品');
- $check_collect = Db::table('goods_collect')
- ->where(['goods_id'=>$goods_id,'user_id'=>$this->uid])
- ->value('id');
- if($check_collect) $this->error('商品已收藏');
- Db::table('goods_collect')->insert(['goods_id'=>$goods_id,'user_id'=>$this->uid,'create_at'=>date('Y-m-d H:i:s')]);
- $this->success('商品已收藏');
- }
- /**
- * @title 取消收藏
- * @desc 取消收藏
- * @author qc
- * @url /api/Goods_order/cancelCollect
- * @method POST
- * @header name:Authorization require:1 desc:Token
- * @param name:id type:int default:0 desc:收藏记录id(两个参数不能同时为空)
- * @param name:goods_id type:int default:0 desc:商品id(两个参数不能同时为空)
- */
- public function cancelCollect()
- {
- $id = input('post.id');
- $goods_id = input('post.goods_id');
- if(!$id && !$goods_id) $this->error('参数错误');
- $del_where = [];
- $del_where['user_id'] = $this->uid;
- if($id) $del_where['id'] = $id;
- if($goods_id) $del_where['goods_id'] = $goods_id;
- Db::table('goods_collect')->where($del_where)->delete();
- $this->success('取消成功');
- }
- /**
- * @title 获取我的收藏列表
- * @desc 获取我的收藏列表
- * @author qc
- * @url /api/Goods_order/getMyGoodsCollect
- * @method GET
- * @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:id type:int default:-- desc:收藏记录id
- * @return name:goods_id type:int default:-- desc:商品id
- * @return name:name type:string default:-- desc:商品名称
- * @return name:cover type:string default:-- desc:商品封面图
- * @return name:desc type:string default:-- desc:商品简述
- * @return name:goods_price type:float default:-- desc:售价
- * @return name:stock type:int default:-- desc:剩余库存
- * @return name:base_stock type:int default:-- desc:初始库存
- */
- public function getMyGoodsCollect()
- {
- $list = Db::table('goods_collect')->alias('c')
- ->field('c.id,c.goods_id,c.create_at,g.name,g.cover,g.desc,g.goods_price,g.stock,g.base_stock')
- ->join('store_goods g','c.goods_id = g.id','INNER')
- ->where(['c.user_id'=>$this->uid])
- ->order('c.id desc')
- ->limit($this->off_set,$this->page_num)
- ->select();
- $this->success('获取成功',['list'=>$list]);
- }
- }
|