123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457 |
- <?php
- namespace app\api\controller;
- use app\api\model\MarketGoods;
- use think\Db;
- /**
- * @title 集市商城
- * @controller MagicMarket
- * @group base
- */
- class MagicMarket extends Base
- {
- public function initialize()
- {
- parent::initialize();
- parent::check_login();
- }
- /**
- * @title 售卖魔玩柜
- * @desc 售卖魔玩柜
- * @author qc
- * @url /api/Magic_market/sellMagicCase
- * @method POST
- * @header name:Authorization require:1 desc:Token
- * @param name:json_str type:string : default:1 desc:售卖的json串[{'id':'';'num':'数量']
- * @param name:money type:float : default:1 desc:售卖价格
- */
- public function sellMagicCase()
- {
- $json_str = input('post.json_str');
- $money = input('post.money');
- $json_data = json_decode($json_str,true);
- $error = false;
- $pro_info = [];
- Db::startTrans();
- foreach ($json_data as $val) {
- $magic_case = Db::table('magic_case')
- ->where(['user_id'=>$this->uid,'id'=>$val['id']])
- ->find();
- if(empty($magic_case) || $magic_case['num'] < $val['num']){
- $error = true;break;
- }
- $pro_info[] = [
- 'magic_id' => $magic_case['id'],
- 'goods_id' => $magic_case['goods_id'],
- 'num' => $val['num'],
- 'case_name' => $magic_case['case_name'],
- 'case_cover'=> $magic_case['case_cover'],
- 'crystal' => $magic_case['crystal'],
- ];
- // 扣减魔玩柜的数量
- Db::table('magic_case')->where('id',$val['id'])->setDec('num',$val['num']);
- }
- if($error){
- Db::rollback();
- $this->error('售卖数据有误');
- }
- $market_goods=[
- 'user_id' => $this->uid,
- 'sell_price' => $money,
- 'create_at' => date('Y-m-d H:i:s'),
- 'crystal' => array_sum(array_column($pro_info,'crystal')),
- 'pro_info' => json_encode($pro_info),
- 'stock' =>array_sum(array_column($pro_info,'num')),
- 'name_str' => implode(',', array_column($pro_info,'case_name'))
- ];
- MarketGoods::create($market_goods);
- Db::commit();
- $this->success('售卖成功',[]);
- }
- /**
- * @title 商品下架
- * @desc 商品下架
- * @author qc
- * @url /api/Magic_market/cancelMarketGoods
- * @method POST
- * @header name:Authorization require:1 desc:Token
- * @param name:id type:int require:1 default:-- desc:商品id
- */
- public function cancelMarketGoods()
- {
- $goods_id = input('post.id');
- $goods_detail = MarketGoods::get(['id'=>$goods_id,'user_id'=>$this->uid]);
- if(!$goods_detail) $this->error('商品不存在!');
- if($goods_detail->status != 1) $this->error('商品已下架!');
- if($goods_detail->stock == 0) $this->error('商品已售罄!');
- Db::startTrans();
- try {
- //恢复库存
- $pro_info = json_decode($goods_detail['pro_info'],true);
- foreach ($pro_info as $pv) {
- Db::table('magic_case')->where(['id'=>$pv['magic_id']])->setInc('num',$pv['num']);
- }
- MarketGoods::where('id',$goods_id)->delete();
- Db::commit();
- }catch (\Exception $e){
- Db::rollback();
- }
- $this->success('商品下架成功');
- }
- /**
- * @title 订单确认
- * @desc 订单确认
- * @author qc
- * @url /api/Magic_market/orderConfirm
- * @method GET
- * @header name:Authorization require:1 desc:Token
- * @param name:goods_id type:int : default:-- desc:商品id
- * @return name:add_info type:array require:0 default:-- desc:默认地址
- * @return name:add_info.id type:int default:1 desc:收货地址记录id
- * @return name:add_info.pro_id type:int default:-- desc:省id
- * @return name:add_info.city_id type:int default:-- desc:市id
- * @return name:add_info.county_id type:int default:-- desc:县区id
- * @return name:add_info.pro_name type:string default:-- desc:省名
- * @return name:add_info.city_name type:string default:-- desc:市名
- * @return name:add_info.county_name type:string default:-- desc:县区名
- * @return name:add_info.detail type:string default:-- desc:详细地址
- * @return name:add_info.phone type:string default:-- desc:联系电话
- * @return name:add_info.name type:string default:-- desc:联系人
- * @return name:goods_data type:array require:0 default:-- desc:商品详情
- * @return name:goods_data.id type:int default:-- desc:商品id
- * @return name:goods_data.sell_price type:string default:-- desc:售价
- * @return name:goods_data.cover type:string default:-- desc:商品图
- * @return name:goods_data.status type:int default:-- desc:0已下架1正常售卖
- * @return name:goods_data.user_id type:int default:-- desc:售卖商品会员id
- * @return name:goods_data.stock type:int default:-- desc:数量
- * @return name:goods_data.sell_price type:int default:-- desc:售价(所有数量的售价)
- * @return name:goods_data.crystal type:float default:-- desc:明信片总价值
- * @return name:goods_data.order_status type:int default:-- desc:售卖状态0售卖中1已售卖/待发货2待收货3已完成
- * @return name:goods_data.pro_info type:array default:-- desc:详情
- * @return name:goods_data.pro_info.num type:int default:-- desc:数量
- * @return name:goods_data.pro_info.case_name type:int default:-- desc:名称
- * @return name:goods_data.pro_info.case_cover type:int default:-- desc:图片
- * @return name:goods_data.pro_info.crystal type:int default:-- desc:明信片(每个)
- * @return name:market_freight type:float default:-- desc:运费
- * @return name:coupon_list type:array default:-- desc:可用运费券
- * @return name:coupon_list.id type:int default:-- desc:券id
- * @return name:coupon_list.past_at type:int default:-- desc:过期时间
- */
- public function orderConfirm(){
- $goods_id = input('goods_id',0);
- $goods_info = MarketGoods::get($goods_id);
- if(!$goods_info) $this->error('商品已下架');
- $market_freight = bcadd(sysconf('market_freight'),0,2);
- //$freight_data = get_freight($goods_info->stock);// 计算运费
- // 默认收货地址
- $add_info = Db::table('delivery_address')->where(['user_id'=>$this->uid,'is_mr'=>1])->find();
- // 运费券
- $coupon_list = Db::table('user_coupon_list')
- ->field('id,past_at')
- ->where('user_id','=',$this->uid)
- ->where('status','=',1)
- ->where('past_at','> time',date('Y-m-d H:i:s'))
- ->order('id desc')
- ->select();
- $this->success('ok',['add_info'=> $add_info ,'goods_data'=>$goods_info,'market_freight'=>$market_freight,'coupon_list'=>$coupon_list]);
- }
- /**
- * @title 生成订单
- * @desc 生成订单
- * @author qc
- * @url /api/Magic_market/createOrder
- * @method POST
- * @header name:Authorization require:1 desc:Token
- * @param name:goods_id type:int : default:-- desc:商品id
- * @param name:add_id type:int require:1 default:0 desc:收货地址id
- * @param name:coupon_id type:int require:0 default:0 desc:运费券的id
- * @return name:order_id type:int require:0 default:0 desc:成功时返回订单id
- */
- public function createOrder()
- {
- $goods_id = input('post.goods_id',0);
- $add_id = input('post.add_id',0);
- if(!$add_id) $this->error('请选择收货地址');
- $send_info = Db::table('delivery_address')->field('pro_name,city_name,county_name,mer_name,detail,phone,name')->find($add_id);
- $goods_info = MarketGoods::get($goods_id);
- if(!$goods_info) $this->error('商品有误');
- if($goods_info->status != 1) $this->error('商品已下架');
- if($goods_info->stock == 0) $this->error('商品已售罄');
- if($this->uid == $goods_info->user_id ) $this->error('无法购买自己的商品');
- $num = $goods_info->stock;// 商品数量
- // 所选的券id
- $coupon_id = input('post.coupon_id',0);
- if($coupon_id) {
- $coupon_info = Db::table('user_coupon_list')->where('id','=',$coupon_id)
- ->where('past_at','> time',date('Y-m-d H:i:s'))
- ->find();
- if(!$coupon_info || $coupon_info['status'] != 1 ) $this->error('运费券信息有误');
- }
- $freight_money = $coupon_id ? 0 :bcadd(sysconf('market_freight'),0,2) ;
- $price_total = bcadd($goods_info->sell_price,$freight_money,2);// 订单金额
- $order_int =[
- 'uid' => $this->uid,
- 'vendor_id' => $goods_info->user_id,
- 'order_no' => get_order_sn(),
- 'price_total' => $price_total,
- 'goods_num' => $num,
- 'pro_info' => $goods_info->pro_info,
- 'create_at' => date('Y-m-d H:i:s'),
- 'freight' => $freight_money,
- 'cl_id' => $coupon_id,
- 'address_id' => $add_id,
- 'goods_id' => $goods_id,
- 'crystal' => $goods_info->crystal,
- 'send_info' =>json_encode($send_info),
- ];
- Db::startTrans();
- $msg= '';$com=true;
- try{
- //扣减库存
- Db::table('market_goods')->where('id',$goods_id)->setDec('stock',$num);
- $goods_info = MarketGoods::get($goods_id);
- if($goods_info->stock < 0) {
- Db::rollback();
- $com=false;
- $msg = '库存不足';
- }else{
- if($coupon_id) Db::table('user_coupon_list')->where('id',$coupon_id)->update(['status'=>2,'used_time'=>date('Y-m-d H:i:s')]);
- // 生成订单
- Db::table('market_order')->insert($order_int);
- $order_id = Db::getLastInsID();
- 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/Magic_market/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: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:pay_time type:string default:-- desc:支付时间
- * @return name:create_at 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: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:send_info type:string default:-- desc:收货详情
- * @return name:pro_info type:array default:0 desc:商品详情
- * @return name:pro_info.num type:int default:-- desc:数量
- * @return name:pro_info.case_name type:int default:-- desc:名称
- * @return name:pro_info.case_cover type:int default:-- desc:图片
- * @return name:pro_info.crystal type:int default:-- desc:明信片
- * @return name:send_info type:int default:-- desc:收货地址
- * @return name:send_info.pro_name type:string default:-- desc:省
- * @return name:send_info.city_name type:string default:-- desc:市
- * @return name:send_info.county_name type:string default:-- desc:区
- * @return name:send_info.mer_name type:string default:-- desc:省-市-区
- * @return name:send_info.detail type:string default:-- desc:详细地址
- * @return name:send_info.phone type:string default:-- desc:收货人电话
- * @return name:send_info.name type:string default:-- desc:收货人名称
- */
- public function getOrderDetail(){
- $order_id = input('id',0);
- $detail = Db::table('market_order')->find($order_id);
- $this->success('获取成功',$detail);
- }
- /**
- * @title 去支付
- * @desc 去支付
- * @author qc
- * @url /api/Magic_market/payOrder
- * @method POST
- * @header name:Authorization require:1 desc:Token
- * @param name:o_id type:int require:1 default:-- desc:订单id
- * @return name:config type:array default:0 desc:支付配置
- * @return name:config.appId type:string default:-- desc:公众账号ID
- * @return name:config.nonceStr type:string default:-- desc:随机字符串
- * @return name:config.signType type:string default:-- desc:签名类型
- * @return name:config.paySign type:string default:-- desc:签名
- * @return name:config.timestamp type:string default:-- desc:时间戳
- */
- public function payOrder()
- {
- $o_id = input('post.o_id');
- $user_info = Db::table('store_member')->where('id',$this->uid)->find();
- if(!$user_info['openid']) $this->error('请先完善用户信息');
- $order_info = Db::table('market_order')->where(['uid'=>$this->uid,'id'=>$o_id])->find();
- if(empty($order_info)) $this->error('订单不存在');
- if($order_info['cancel_state'] != 0) $this->error('订单已取消');
- if($order_info['status'] != 0) $this->error('订单已支付');
- $pay_no = $order_info['pay_no'] ? $order_info['pay_no'] : get_order_sn();
- $msg ='';
- $notify_url = $this->request->root(true) . '/api/Pay/marketOrderNotify';
- Db::startTrans();
- try{
- // 更新订单
- $price_total = $order_info['price_total'];
- Db::table('market_order')->where(['uid'=>$this->uid,'id'=>$o_id])->update(['pay_no'=>$pay_no]);
- $config = Pay::wxPay('订单支付',$pay_no,$price_total,$notify_url,'JSAPI',$user_info['openid']);
- if($config){
- Db::commit();
- }else{
- Db::rollback();
- }
- }catch (\Exception $e){
- Db::rollback();
- $msg = $e->getMessage();
- }
- if($config){
- $this->success('ok',['config'=>$config]);
- }else{
- $this->error($msg);
- }
- }
- /**
- * @title 获取订单列表
- * @desc 获取订单列表
- * @author qc
- * @url /api/Magic_market/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是全部)
- * @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:pay_time type:string default:-- desc:支付时间
- * @return name:create_at 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: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:pro_info type:array default:0 desc:商品详情
- * @return name:pro_info.num type:int default:-- desc:数量
- * @return name:pro_info.case_name type:int default:-- desc:名称
- * @return name:pro_info.case_cover type:int default:-- desc:图片
- * @return name:pro_info.crystal type:int default:-- desc:明信片
- */
- public function getMyOrderList()
- {
- $status = input('status',-1);
- $where=[];
- if($status > -1) $where['status'] = $status;
- $where['uid'] = $this->uid;
- $where['is_deleted'] = 0;
- $list = Db::table('market_order')
- ->where($where)
- ->order('id desc')
- ->limit($this->off_set,$this->page_num)
- ->select();
- $this->success('获取成功',['list'=>$list]);
- }
- /**
- * @title 取消订单
- * @desc 取消订单
- * @author qc
- * @url /api/Magic_market/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('market_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('market_goods')->where('id',$order_detail['goods_id'])->setInc('stock',$order_detail['goods_num']);
- if($order_detail['cl_id']) Db::table('user_coupon_list')->where('id',$order_detail['cl_id'])->update(['status'=>1,'used_time'=>'']);
- // 取消订单
- Db::table('market_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/Magic_market/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('market_order')
- ->where(['uid'=>$this->uid,'is_deleted'=>0,'id'=>$order_id])
- ->find();
- if(empty($order_info)) $this->error('订单不存在');
- Db::table('market_order')->where(['id'=>$order_id])->update(['is_deleted'=>1]);
- $this->success('删除成功');
- }
- /**
- * @title 确认收货
- * @desc 确认收货
- * @author qc
- * @url /api/Magic_market/confirmReceipt
- * @method POST
- * @header name:Authorization require:1 desc:Token
- * @param name:id type:int require:1 default:1 desc:订单id
- */
- public function confirmReceipt(){
- $order_info = Db::table('market_order')
- ->where(['uid'=>$this->uid,'id'=>input('post.id')])
- ->find();
- if(empty($order_info) || $order_info['status'] != 2) $this->error('商品未发货');
- Db::table('market_order')->where(['id'=>$order_info['id']])->update(['status'=>3,'receipt_at'=>date('Y-m-d H:i:s')]);
- MarketGoods::where('id',$order_info['goods_id'])->update(['order_status'=>3]);
- $this->success('收货成功');
- }
- }
|