HomeController.php 10.8 KB
<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/11/27
 * Time: 11:31
 */

namespace api\index\controller;


use api\index\model\HomeModel;
use cmf\controller\RestBaseController;
use think\Db;
use think\Validate;

/**
 * @title 家政服务
 * @description
 */
class HomeController extends RestBaseController
{
    /**
     * @title 家政服务首页轮播图
     * @description
     * @author GuoSheng
     * @url /index/Home/photo
     * @method GET
     *
     * @return id:ID
     * @return thumbnail:图片
     * @return url:链接地址
     *
     */
    public function photo(){
        $where['delete_time'] = ['eq',0];
        $data = Db::name('homepic')
            ->where($where)
            ->order('id desc')
            ->select();
        foreach ($data as &$v){
            $v['thumbnail'] =  cmf_get_image_url($v['thumbnail']);
        }
        $this->success('SUCCESS',$data);
    }

    /**
     * @title 家政公司列表
     * @description
     * @author GuoSheng
     * @url /index/Home/home
     * @method GET
     *
     * @return id:ID
     * @return home_name:名称
     * @return thumbnail:缩略图
     *
     */
    public function home(){
        $where['delete_time'] = ['eq',0];
        $data = Db::name('home')
            ->where($where)
            ->field('id,home_name,thumbnail')
            ->order('id desc')
            ->select();
        foreach ($data as &$v){
            $v['thumbnail'] =  cmf_get_image_url($v['thumbnail']);
        }
        $this->success('SUCCESS',$data);
    }

    /**
     * @title 家政服务列表
     * @description
     * @author GuoSheng
     * @url /index/Home/sever
     * @method GET
     *
     * @return id:ID
     * @return sever_name:服务名称
     * @return thumbnail:缩略图
     *
     */
    public function sever(){
        $where['delete_time'] = ['eq',0];
        $data = Db::name('service')
            ->where($where)
            ->field('id,sever_name,thumbnail')
            ->order('id desc')
            ->select();
        foreach ($data as &$v){
            $v['thumbnail'] =  cmf_get_image_url($v['thumbnail']);
        }
        $this->success('SUCCESS',$data);
    }


    /**
     * @title 家政公司详情
     * @description 家政公司详情
     * @author GuoSheng
     * @url /index/Home/homeDetail
     * @method GET
     *
     * @header name:XX-Token require:1 default: desc:token
     *
     * @param name:id type:int require:1 other: desc:家政服务公司ID
     *
     * @return id:ID
     * @return home_name:名称
     * @return home_address:地址
     * @return thumbnail:家政公司封面缩略图
     * @return home_phone:联系电话
     * @return images:轮播图
     * @return content:公司介绍
     * @return scver_name:服务名称
     *
     */
    public function homeDetail()
    {
        $user_id = $this->getUserId();
        $id = $this->request->param('id');
        if(empty($id)){
            $this->error(['code'=>40001,'msg'=>'缺少必要参数']);
        }
        $where['id'] = ['eq',$id];
        $homeModel = new HomeModel();
        $data = $homeModel->findData($where)->toArray();
        $data['scver_name'] = Db::name('service')->where('home_id',$id)->field('id as sever_id,thumbnail,sever_name')->select();
        $this->success('SUCCESS',$data);
    }

    /**
     * @title 家政服务服务评价列表
     * @description 家政服务服务评价列表
     * @author GuoSheng
     * @url /index/Home/comment
     * @method GET
     *
     * @param name:home_id type:int require:1 other: desc:家政公司id
     * @param name:page type:int require:0 other: desc:当前页(默认1)
     * @param name:pageNum type:int require:0 other: desc:每页显示数据个数(默认10)
     *
     * @return id:评价id
     * @return user_id:用户ID
     * @return user_nickname:用户名
     * @return num:评价星数
     * @return content:评价内容
     *
     */
    public function comment()
    {
        $home_id = $this->request->param('home_id',0,'intval');
        if(empty($home_id)){
            $this -> error(['code'=>40005,'msg'=>'缺少必要参数']);
        }
        $page = $this->request->param('page',1,'intval');
        $pageNum = $this->request->param('pageNum',10,'intval');
        $res = Db::name('homecomment')
            ->alias('a')
            ->join('user b','a.user_id = b.id')
            ->field('a.*,b.user_nickname')
            ->where('home_id',$home_id)
            ->page($page,$pageNum)
            ->select()
            ->toArray();
        foreach ($res as &$v){
            $v['num'] = ceil(($v['speed']+$v['service']+$v['recycle'])/3);
            $v['create_time'] = date('Y-m-d H:i:s',$v['create_time']);
        }
        $this->success('SUCCESS',$res);
    }

    /**
     * @title 预约家政服务地址列表
     * @description
     * @author GuoSheng
     * @url /index/Home/recycleList
     * @method GET
     *
     * @header name:XX-Token require:1 default: desc:token
     *
     * @return id:地址ID
     * @return name:姓名
     * @return phone:电话
     * @return address:详细地址
     *
     */
    public function recycleList()
    {
        $user_id = $this->getUserId();
        $where['user_id'] = ['eq',$user_id];
        $where['delete_time'] = ['eq',0];
        $data = Db::name('homeadress')
            ->field('id,name,phone,address')
            ->where($where)
            ->order('id desc')
            ->select();
        $this->success('SUCCESS',$data);
    }

    /**
     * @title 预约家政服务地址详情
     * @description
     * @author GuoSheng
     * @url /index/Shopgoods/recycleDetail
     * @method GET
     *
     * @header name:XX-Token require:1 default: desc:token
     *
     * @param name:id require:1 default: desc:回收地址ID
     *
     * @return name:姓名
     * @return phone:电话
     * @return address:详细地址
     *
     */
    public function recycleDetail()
    {
        $user_id = $this->getUserId();
        $id = $this->request->param('id',0,'intval');
        if(empty($id)){
            $this -> error(['code'=>40005,'msg'=>'缺少必要参数']);
        }
        $data = Db::name('homeadress')
            ->where('id',$id)
            ->field('id,name,phone,address')
            ->find();
        $this->success('SUCCESS',$data);
    }

    /**
     * @title 添加预约家政服务地址
     * @description
     * @author GuoSheng
     * @url /index/Home/addrecycle
     * @method GET
     *
     * @header name:XX-Token require:1 default: desc:token
     *
     * @param name:name require:1 default: desc:联系人姓名
     * @param name:phone require:1 default: desc:联系人电话
     * @param name:address require:1 default: desc:联系人详细地址
     *
     */
    public function addrecycle()
    {
        $user_id = $this->getUserId();
        $param = $this->request->param();
        $param['user_id'] = $user_id;
        $param['create_time'] = time();
        $validate = new Validate([
            'name' => 'require',
            'phone' => 'require',
            'address'=>'require',
        ]);
        if (!$validate->check($param)) {
            $this->error(['code'=>40005,'msg'=>$validate->getError()]);
        }
        $data = Db::name('homeadress')
            ->insert($param);
        if(empty($data)){
            $this->error(['code'=>40006,'msg'=>'sql执行失败']);
        }
        $this->success('SUCCESS');
    }

    /**
     * @title 修改预约家政服务地址
     * @description
     * @author GuoSheng
     * @url /index/Home/editrecycle
     * @method GET
     *
     * @header name:XX-Token require:1 default: desc:token
     *
     * @param name:id require:1 default: desc:回收地址ID
     *
     * @param name:name require:1 default: desc:联系人姓名
     * @param name:phone require:1 default: desc:联系人电话
     * @param name:address require:1 default: desc:联系人详细地址
     *
     */
    public function editrecycle()
    {
        $user_id = $this->getUserId();
        $id = $this->request->param('id',0,'intval');
        if(empty($id)){
            $this -> error(['code'=>40005,'msg'=>'缺少必要参数']);
        }
        $param = $this->request->param();
        $param['user_id'] = $user_id;
        $param['update_time'] = time();
        $validate = new Validate([
            'name' => 'require',
            'phone' => 'require',
            'address'=>'require',
        ]);
        if (!$validate->check($param)) {
            $this->error(['code'=>40005,'msg'=>$validate->getError()]);
        }
        $data = Db::name('homeadress')
            ->where('id',$id)
            ->update($param);
        if(empty($data)){
            $this->error(['code'=>40006,'msg'=>'sql执行失败']);
        }
        $this->success('SUCCESS');
    }

    /**
     * @title 删除预约家政服务地址
     * @description
     * @author GuoSheng
     * @url /index/Home/delrecycle
     * @method GET
     *
     * @header name:XX-Token require:1 default: desc:token
     *
     * @param name:id require:1 default: desc:回收地址ID
     *
     */
    public function delrecycle()
    {
        $user_id = $this->getUserId();
        $id = $this->request->param('id',0,'intval');
        if(empty($id)){
            $this -> error(['code'=>40005,'msg'=>'缺少必要参数']);
        }
        $data = Db::name('homeadress')
            ->where('id',$id)
            ->update(['delete_time'=>time()]);
        if(empty($data)){
            $this->error(['code'=>40006,'msg'=>'sql执行失败']);
        }
        $this->success('SUCCESS');
    }

    /**
     * @title 家政服务预约订单
     * @description
     * @author GuoSheng
     * @url /index/Home/subscribe
     * @method GET
     *
     * @header name:XX-Token require:1 default: desc:token
     *
     * @param name:address_id require:1 other: desc: 预约家政服务地址ID
     * @param name:home_id require:1 other: desc: 家政公司ID
     * @param name:service_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([
            'address_id' => 'require',
            'home_id' => 'require',
            'service_id'=>'require',
            'sub_time'=>'require',
            'content'=>'require'
        ]);
        if (!$validate->check($param)) {
            $this->error(['code'=>40005,'msg'=>$validate->getError()]);
        }
        $param['num'] = cmf_get_order_sn();
        $data = Db::name('subhome')
            ->insert($param);
        if(empty($data)){
            $this->error(['code'=>40006,'msg'=>'sql执行失败']);
        }
        $this->success('SUCCESS');
    }


}