审查视图

api/portal/controller/GoodsController.php 4.5 KB
1  
潘浩文 authored
1 2 3 4
<?php
namespace api\portal\controller;
use cmf\controller\RestBaseController;
use Think\Db;
潘浩文 authored
5
1  
潘浩文 authored
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/**
 * @title 积分商城
 * @description 积分商城相关接口
 */
class GoodsController extends RestBaseController
{
    /**
     * @title 积分商城
     * @description 积分商城首页渲染
     * @author panhaowen
     * @url /portal/Goods/index
     * @method POST
     */
    public function index()
    {
潘浩文 authored
21
        $data=Db::name('goods')->where('status',1)->field('id,goods_name,before_price,now_price,vip_price,content,image')->select()->each(function ($item) {
1  
潘浩文 authored
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
            $item['image'] =cmf_get_image_preview_url($item['image']);
            return $item;
        })->toArray();
        $this->success('商城首页信息',$data);
    }


    /**
     * @title 订单详情
     * @description 订单详情页面
     * @author panhaowen
     * @url /portal/Goods/goodsOrder
     * @method POST
     * @header name:XX-Token require:1 default: desc:token
     * @param name:id type:int require:1 other: desc:商品id
     */
    public function goodsOrder()
    {
        $param=$this->request->param();
潘浩文 authored
41
        $data=Db::name('goods')->where('id',$param['id'])->field('id,goods_name,before_price,now_price,vip_price,content,image')->find();
1  
潘浩文 authored
42 43 44 45 46 47
        $status=Db::name('user')->where('id',$this->getUserId())->find()['status'];
        if ($status==1){
            $data['price']=$data['now_price'];
        }else if ($status==2){
            $data['price']=$data['vip_price'];
        }
潘浩文 authored
48
        $data['image']=cmf_get_image_preview_url($data['image']);
1  
潘浩文 authored
49 50 51 52 53 54 55 56
        $data['address']=Db::name('address')->where(['user_id'=>$this->getUserId(),'status'=>1])->field('id,address')->find();
        $this->success('订单详情信息',$data);
    }

    /**
     * @title 立即兑换
     * @description 立即兑换接口
     * @author panhaowen
潘浩文 authored
57
     * @url /portal/Goods/orderPost
1  
潘浩文 authored
58 59 60 61 62 63
     * @method POST
     * @header name:XX-Token require:1 default: desc:token
     * @param name:goods_id type:int require:1 other: desc:商品id
     * @param name:address_id type:int require:1 other: desc:收货地址id
     * @param name:remark type:text require:0 other: desc:备注
     * @param name:count type:int require:1 other: desc:商品数量
潘浩文 authored
64
     * @param name:price type:int require:1 other: desc:实付金额
1  
潘浩文 authored
65 66 67 68 69 70 71 72
     */
    public function orderPost()
    {
        $param=$this->request->param();
        $user_score=Db::name('user')->where('id',$this->getUserId())->find()['score'];
        if ($user_score<$param['price']){
            $this->error('用户积分不足',2);
        }
潘浩文 authored
73 74 75
        if (Db::name('goods_order')->where('id',$param['id'])->find()['stock']<$param['count']){
            $this->error('商品库存不足',3);
        }
1  
潘浩文 authored
76 77 78 79 80 81 82 83 84 85 86 87
        $re=Db::name('goods_order')->insert([
            'user_id'=>$this->getUserId(),
            'goods_id'=>$param['goods_id'],
            'real_price'=>$param['price'],
            'count'=>$param['count'],
            'remark'=>$param['remark'],
            'order_sn'=>cmf_get_order_sn(),
            'create_time'=>time(),
            'address_id'=>$param['address_id']
        ]);
        if ($re) {
            Db::name('user')->where('id',$this->getUserId())->setDec('score',$param['score']);
潘浩文 authored
88 89
            Db::name('user_score_log')->insert(['user_id'=>$this->getUserId(),'score'=>-$param['score'],'action'=>'消费','now_score'=>$user_score-$param['score']]);
            Db::name('goods')->where('id',$param['goods_id'])->setDec('stock',$param['count']);
1  
潘浩文 authored
90 91
            $this->success('兑换成功',1);
        }else{
潘浩文 authored
92
            $this->error('兑换失败',4);
1  
潘浩文 authored
93 94 95
        }
    }
潘浩文 authored
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127


    /**
     * @title 添加收货地址提交
     * @description 添加收货地址提交接口
     * @author panhaowen
     * @url /portal/Goods/addressPost
     * @method POST
     * @header name:XX-Token require:1 default: desc:token
     */
    public function addressPost()
    {
        $param=$this->request->param();
        $param['user_id']=$this->getUserId();
        $param['create_time']=time();
        Db::name('address')->insert($param);
        $this->success('收货地址添加成功');
    }

    /**
     * @title 收货地址列标
     * @description 收货地址列表接口
     * @author panhaowen
     * @url /portal/Goods/address
     * @method POST
     * @header name:XX-Token require:1 default: desc:token
     */
    public function address()
    {
        $data=Db::name('address')->where('user_id',$this->getUserId())->select()->toArray();
        $this->success('用户全部地址',$data);
    }
1  
潘浩文 authored
128
}