SubscribeController.php 5.1 KB
<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/11/15
 * Time: 14:35
 */

namespace api\index\controller;


use cmf\controller\RestBaseController;
use think\Db;
use think\Validate;

/**
 * @title 回收商品预约
 * @description
 */
class SubscribeController extends RestBaseController
{
    public function _initialize()
    {
        $user_id = $this->getUserId();
        $data = Db::name('integral')
            ->where('user_id',$user_id)
            ->find();
        if(empty($data)){
            $this->error(['code'=>40005,'msg'=>'请先注册']);
        }
    }

    /**
     * @title 回收商品类型
     * @description
     * @author GuoSheng
     * @url /index/Subscribe/index
     * @method GET
     *
     * @header name:XX-Token require:1 default: desc:token
     *
     * @return id:类型ID
     * @return recycle_type_name:类型名称
     *
     */
    public function index()
    {
        $user_id = $this->getUserId();
        $data = Db::name('rtype')
            ->where('delete_time',0)
            ->field('id,recycle_type_name')
            ->select();
        $this->success('SUCCESS',$data);
    }

    /**
     * @title 回收商品列表以及属性
     * @description
     * @author GuoSheng
     * @url /index/Subscribe/goods
     * @method GET
     *
     * @header name:XX-Token require:1 default: desc:token
     *
     * @param name:rtype_id require:1 other: desc: 类型ID
     *
     * @return id:商品ID
     * @return rtype_id:所属类型ID
     * @return rgoods_name:商品名称
     * @return attr_id:属性ID
     * @return attr_name:属性名称
     * @return rgoods_id:所属商品ID
     * @return attr_price:价格
     */
    public function goods()
    {
//        $user_id = $this->getUserId();
        $rtype_id = $this->request->param('rtype_id',0,'intval');
        if(empty($rtype_id)){
            $this -> error(['code'=>40005,'msg'=>'缺少必要参数']);
        }
        $data = Db::name('rgoods')
            ->where('delete_time',0)
            ->where('rtype_id',$rtype_id)
            ->field('id,rtype_id,rgoods_name')
            ->select()
            ->toArray();
        $res = Db::name('attr')
            ->field('id as attr_id,attr_name,rgoods_id,attr_price')
            ->select()
            ->toArray();
        foreach ($data as &$v){
            $v['child'] = [];
            foreach ($res as $key =>$val){
                if($val['rgoods_id'] == $v['id']){
                    array_push($v['child'],$val);
                }
            }
        }
        $this->success('SUCCESS',$data);


    }


    /**
     * @title 通过属性获取商品信息
     * @description
     * @author GuoSheng
     * @url /index/Subscribe/attr
     * @method GET
     *
     * @header name:XX-Token require:1 default: desc:token
     *
     * @param name:attr_id require:1 other: desc: 属性ID
     *
     * @return id:属性ID
     * @return attr_name:属性名称
     * @return rgoods_name:商品名称
     * @return rgoods_id:所属商品ID
     * @return attr_price:价格
     */
    public function attr(){
        $user_id = $this->getUserId();
        $attr_id = $this->request->param('attr_id');
        if(empty($attr_id)){
            $this->error(['code'=>40005,'msg'=>'缺少必要参数']);
        }
        $data = Db::name('attr')
            ->alias('a')
            ->join('rgoods r','a.rgoods_id = r.id')
            ->field('a.*,r.rgoods_name')
            ->where('a.id',$attr_id)
            ->find();
        $this->success('SUCCESS',$data);
    }


    /**
     * @title 回收预约订单
     * @description
     * @author GuoSheng
     * @url /index/Subscribe/subscribe
     * @method GET
     *
     * @header name:XX-Token require:1 default: desc:token
     *
     * @param name:recycle_id require:1 other: desc: 上门回收地址ID
     * @param name:image require:1 other: desc: 回收物品图片
     * @param name:attr_id require:1 other desc: 商品属性ID
     * @param name:sub_time require:1 other desc: 预约时间
     * @param name:content require:1 other desc: 留言备注
     *
     */
    public function subscribe()
    {
        $user_id = $this->getUserId();
        $param = $this->request->param();
        $param['user_id'] = $user_id;
        $param['create_time'] = time();
        $validate = new Validate([
            'recycle_id' => 'require',
            'image' => 'require',
            'attr_id'=>'require',
            'sub_time'=>'require',
            'content'=>'require'
        ]);
        $validate->message([
            'recycle_id'=>'上门回收地址不能为空',
            'image'=>'图片不能为空',
            'attr_id'=>'回收商品不能为空',
            'sub_time'=>'预约时间不能为空',
            'content'=>'留言备注不能为空'
        ]);
        if (!$validate->check($param)) {
            $this->error(['code'=>40005,'msg'=>$validate->getError()]);
        }
        $param['num'] = cmf_get_order_sn();
        $data = Db::name('subscribe')
            ->insert($param);
        if(empty($data)){
            $this->error(['code'=>40006,'msg'=>'sql执行失败']);
        }
        $this->success('SUCCESS');
    }
}