GoodsController.php 3.4 KB
<?php
namespace api\portal\controller;
use cmf\controller\RestBaseController;
use Think\Db;

/**
 * @title 积分商城
 * @description 积分商城相关接口
 * @header name:key require:1 default: desc:秘钥(区别设置)
 * @param name:public type:int require:1 default:1 other: desc:公共参数(区别设置)
 */
class GoodsController extends RestBaseController
{
    /**
     * @title 积分商城
     * @description 积分商城首页渲染
     * @author panhaowen
     * @url /portal/Goods/index
     * @method POST
     */
    public function index()
    {
        $data=Db::name('goods')->where('status',1)->field('id,goods_name,before_price,now_price,vip_price,content,image')->select()->each(function ($item) {
            $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();
        $data=Db::name('goods')->where('id',$param['id'])->field('id,goods_name,before_price,now_price,vip_price,content,image')->find();
        $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'];
        }
        $data['image']=cmf_get_image_preview_url($data['image']);
        $data['address']=Db::name('address')->where(['user_id'=>$this->getUserId(),'status'=>1])->field('id,address')->find();
        $this->success('订单详情信息',$data);
    }

    /**
     * @title 立即兑换
     * @description 立即兑换接口
     * @author panhaowen
     * @url /portal/Goods/orderPost
     * @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:商品数量
     * @param name:price type:int require:1 other: desc:商品数量
     */
    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);
        }
        $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']);
            Db::name('user_score_log')->insert(['user_id'=>$this->getUserId(),'score'=>$param['score'],'action'=>'消费','now_score'=>$user_score-$param]);
            $this->success('兑换成功',1);
        }else{
            $this->error('兑换失败',3);
        }
    }

}