123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470 |
- <?php
- namespace app\api\controller;
- use think\Db;
- use EasyWeChat\Factory;
- /**
- * @title 魔玩柜
- * @controller MagicCase
- * @group base
- */
- class MagicCase extends Base
- {
- public function initialize()
- {
- parent::initialize();
- parent::check_login();
- resolve_magic($this->uid);
- }
- /**
- * @title 获取魔玩柜列表
- * @desc 获取魔玩柜列表
- * @author qc
- * @url /api/Magic_case/getMagicCaseList
- * @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: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.past_at type:string default:0.00 desc:过期时间
- * @return name:will_past type:int default:-- desc:10分钟内将要到期的记录
- */
- public function getMagicCaseList()
- {
- $will_past = Db::table('magic_case')
- ->where('user_id','=',$this->uid)
- ->where('num','>',0)
- ->where('past_int','<',time()+600)
- ->count();
- $list = Db::table('magic_case')
- ->field('id,order_id,create_at,case_name,case_cover,num,crystal,past_at')
- ->where('user_id','=',$this->uid)
- ->where('num','>',0)
- ->order('id desc')
- ->limit($this->off_set,$this->page_num)
- ->select();
- $this->success('ok',['list'=>$list,'will_past'=>$will_past]);
- }
- /**
- * @title 分解魔玩柜
- * @desc 分解魔玩柜
- * @author qc
- * @url /api/Magic_case/resolveMagicCase
- * @method POST
- * @header name:Authorization require:1 desc:Token
- * @param name:json_str type:string : default:1 desc:分解的json串[{'id':'';'num':'数量'}] [{"id":11,"num":1}]
- * @return name:total_crystal type:float default:0.00 desc:分级魔玩柜获得的水晶
- */
- public function resolveMagicCase()
- {
- $json_str =input('post.json_str');
- $json_data = json_decode($json_str,true);
- $log_data =[];
- $total_crystal = 0;
- $error = false;
- Db::startTrans();
- foreach ($json_data as $val) {
- $magic_case = Db::table('magic_case')
- ->field('id,num,crystal')
- ->where(['user_id'=>$this->uid,'id'=>$val['id']])
- ->find();
- if(empty($magic_case) || $magic_case['num'] < $val['num']){
- $error = true;break;
- }
- // 扣减魔玩柜的数量
- Db::table('magic_case')->where('id',$val['id'])->setDec('num',$val['num']);
- // 扣减魔玩柜变更记录
- $crystal = bcmul($val['num'],$magic_case['crystal'],2);
- $log_data[]= [
- 'user_id' => $this->uid,
- 'create_at' => date('Y-m-d H:i:s'),
- 'num' => $val['num'],
- 'case_id' => $val['id'],
- 'crystal' =>$crystal ,
- ];
- $total_crystal = bcadd($total_crystal,$crystal,2);
- }
- if($error){
- Db::rollback();
- $this->error('分解数据有误');
- }
- //更新水晶余额
- $member_info = Db::table('store_member')->field('id,crystal_cash')->find($this->uid);
- Db::table('store_member')->where('id',$this->uid)->update(['crystal_cash'=>bcadd($member_info['crystal_cash'],$total_crystal,2)]);
- Db::table('magic_resolve')->insertAll($log_data);// 分解日志
- // 余额日志
- crystal_log($this->uid,$total_crystal,'魔玩柜分解',3);
- Db::commit();
- $this->success('ok',['total_crystal'=>$total_crystal]);
- }
- /**
- * @title 订单确认
- * @desc 订单确认
- * @author qc
- * @url /api/Magic_case/orderConfirm
- * @method GET
- * @header name:Authorization require:1 desc:Token
- * @param name:json_str type:string : default:1 desc:选中的json串[{'id':'';'num':'数量'}]
- * @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:array require:0 default:-- desc:魔玩柜记录id
- * @return name:goods_data.num type:string require:0 default:-- desc:剩余数量
- * @return name:goods_data.create_at type:string require:0 default:-- desc:时间
- * @return name:goods_data.buy_num type:string require:0 default:-- desc:剩余购买
- * @return name:goods_data.case_name type:string require:0 default:-- desc:名称
- * @return name:goods_data.case_cover type:string require:0 default:-- desc:图片
- * @return name:freight_data type:array default:-- desc:运费详情
- * @return name:freight_data.all_freight type:int default:-- desc:总运费
- * @return name:freight_data.first_freight type:int default:-- desc:首件运费
- * @return name:freight_data.later_freight type:int 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(){
- $json_str =input('get.json_str');
- $json_data = json_decode($json_str,true);
- $all_ids = array_column($json_data,'id'); // 所要购买的记录id
- $total_num = array_sum(array_column($json_data,'num'));//购买数量
- $freight_data = get_freight($total_num);// 计算运费
- $cl_data = array_column($json_data,null,'id');
- // 验证魔玩柜数量
- $magic_case = Db::table('magic_case')->field('id,num,case_name,case_cover,create_at')->where('id','in',$all_ids)->select();
- $is_ck = true;
- foreach ($magic_case as &$mv) {
- if($mv['num'] < $cl_data[$mv['id']]['num'])$is_ck=false;// 购买数量大于剩与数量
- $mv['buy_num'] = $cl_data[$mv['id']]['num'];
- }
- if(!$is_ck) $this->error('魔玩柜数量不足');
- // 默认收货地址
- $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'=>$magic_case,'freight_data'=>$freight_data,'coupon_list'=>$coupon_list]);
- }
- /**
- * @title 生成订单
- * @desc 生成订单
- * @author qc
- * @url /api/Magic_case/createOrder
- * @method POST
- * @header name:Authorization require:1 desc:Token
- * @param name:json_str type:string : default:1 desc:选中的json串[{'id':'';'num':'数量'}]
- * @param name:price_total type:float require:1 default:-- desc:支付金额
- * @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()
- {
- $json_str = input('post.json_str','');
- $add_id = input('post.add_id',0);
- $price_total = input("post.price_total",0);
- $coupon_id = input('post.coupon_id',0);
- if(!$add_id) $this->error('请选择收货地址');
- $json_data = json_decode($json_str,true);
- $cl_data = array_column($json_data,null,'id');
- $all_ids = array_column($json_data,'id'); // 所要购买的记录id
- $pro_info = []; // 商品详情
- // 验证魔玩柜数量
- $total_num = 0;
- $magic_case = Db::table('magic_case')->field('id,goods_id,num,crystal,case_name,case_cover,create_at')->where('id','in',$all_ids)->select();
- $is_ck = true;
- foreach ($magic_case as &$mv) {
- if($mv['num'] < $cl_data[$mv['id']]['num'])$is_ck=false;// 购买数量大于剩与数量
- $pro_info[] = [
- 'magic_id' => $mv['id'],
- 'goods_id' => $mv['goods_id'],
- 'num' => $cl_data[$mv['id']]['num'],
- 'case_name' => $mv['case_name'],
- 'case_cover'=> $mv['case_cover'],
- 'crystal' => $mv['crystal'],
- 'draw_time' => $mv['create_at'],
- ];
- $total_num += $cl_data[$mv['id']]['num'];
- }
- if(!$is_ck) $this->error('魔玩柜数量不足');
- $freight = get_freight($total_num); // 运费
- $price_total = $coupon_id ? $freight['all_freight'] - $freight['first_freight']:$freight['all_freight'] ;
- $order_int =[
- 'uid'=>$this->uid,
- 'order_no' => get_order_sn(),
- 'price_total' => $price_total,
- 'goods_num' => $total_num,
- 'create_at' => date('Y-m-d H:i:s'),
- 'pro_info' => json_encode($pro_info),
- 'coupon_id' => $coupon_id,
- 'address_id' => $add_id,
- 'freight' =>$freight['all_freight'],
- ];
- Db::startTrans();
- $msg= '';$com=true;
- try{
- // 扣减魔玩柜库存
- foreach ($pro_info as $pv) {
- Db::table('magic_case')->where(['id'=>$pv['magic_id']])->setDec('num',$pv['num']);
- }
- 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('magic_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_case/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('magic_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 {
- //恢复库存
- $pro_info = json_decode($order_detail['pro_info'],true);
- foreach ($pro_info as $pv) {
- Db::table('magic_case')->where(['id'=>$pv['magic_id']])->setInc('num',$pv['num']);
- }
- if($order_detail['coupon_id']) Db::table('user_coupon_list')->where('id',$order_detail['coupon_id'])->update(['status'=>1,'used_time'=>'']);
- // 取消订单
- Db::table('magic_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_case/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_time type:string default:-- desc:支付时间
- * @return name:cancel_state type:int default:-- desc:取消状态
- * @return name:pro_info type:array default:-- 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.draw_time 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:address_area type:string default:0 desc:收货地区(精确到县区)
- * @return name:address_detail type:string default:0 desc:详细地址
- * @return name:address_user type:string default:0 desc:收货人姓名
- * @return name:address_tel type:string default:0 desc:收货人电话
- *
- */
- public function getOrderDetail()
- {
- $order_id = input('id',0);
- $detail = Db::table('magic_order')->find($order_id);
- $add_info = Db::table('delivery_address')->find($detail['address_id']);
- $detail['address_area'] = $add_info['mer_name'];
- $detail['address_detail'] = $add_info['detail'];
- $detail['address_user'] = $add_info['name'];
- $detail['address_tel'] = $add_info['phone'];
- $this->success('获取成功',$detail);
- }
- /**
- * @title 去支付
- * @desc 去支付
- * @author qc
- * @url /api/Magic_case/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('magic_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('订单已支付');
- if($order_info['price_total'] < 0) $this->error('订单金额错误');
- $config = false;$msg= '';
- $pay_no = $order_info['pay_no'] ? $order_info['pay_no'] : get_order_sn();
- if($order_info['price_total'] == 0 ) {
- Db::table('magic_order')->where(['uid'=>$this->uid,'id'=>$o_id])
- ->update(['pay_time'=>date('Y-m-d H:i:s'),'pay_state'=>1,'pay_type'=>2,'status'=>1,'pay_no'=>$pay_no]);
- $this->success('支付成功',['pay_type'=>2,'config' => '']);
- }else{
- $notify_url = $this->request->root(true) . '/api/Pay/magicOrderNotify';
- Db::startTrans();
- try{
- // 更新订单
- $price_total = $order_info['price_total'];
- Db::table('magic_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',['pay_type'=>1,'config'=>$config]);
- }else{
- $this->error($msg);
- }
- }
- }
- /**
- * @title 获取订单列表
- * @desc 获取订单列表
- * @author qc
- * @url /api/Magic_case/getMyOrderList
- * @method GET
- * @header name:Authorization require:1 desc:Token
- * @return name:id type:string 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:pro_info type:array default:-- 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:图片
- */
- 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;
- $where['o.cancel_state'] = 0;
- $where['o.is_deleted'] = 0;
- $list = Db::table('magic_order o')
- ->field('o.id,o.order_no,o.pay_state,pay_type,pay_time,o.create_at,price_total,status,pro_info')
- ->where($where)
- ->order('o.id desc')
- ->limit($this->off_set,$this->page_num)
- ->select();
- $this->success('获取成功',['list'=>$list]);
- }
- /**
- * @title 删除订单
- * @desc 删除订单
- * @author qc
- * @url /api/Magic_case/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('magic_order')
- ->where(['uid'=>$this->uid,'is_deleted'=>0,'id'=>$order_id])
- ->find();
- if(empty($order_info)) $this->error('订单不存在');
- Db::table('magic_order')->where(['id'=>$order_id])->update(['is_deleted'=>1]);
- $this->success('删除成功');
- }
- /**
- * @title 确认收货
- * @desc 确认收货
- * @author qc
- * @url /api/Magic_case/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('magic_order')
- ->where(['uid'=>$this->uid,'id'=>input('post.id')])
- ->find();
- if(empty($order_info) || $order_info['status'] != 2) $this->error('商品未发货');
- Db::table('magic_order')->where(['id'=>$order_info['id']])->update(['status'=>3,'receipt_at'=>date('Y-m-d H:i:s')]);
- $this->success('收货成功');
- }
- }
|