UserneedController.php 15.0 KB
<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: wuwu <15093565100@163.com>
// +----------------------------------------------------------------------
namespace api\portal\controller;

use api\portal\model\UserNeedModel;
use api\portal\model\PortalPostModel;
use cmf\controller\RestBaseController;
use think\Db;
use think\Request;
use think\Loader;
use think\Config;
use think\captcha\Captcha;
use think\Url;
use SmsDemo;
//use think\Route;
/**
 * @title 用户需求接口
 * @description 接口说明
 * @group 接口分组
 */

class UserneedController extends CommonController
{
    protected $postModel;
    public function __construct(PortalPostModel $postModel)
    {
        parent::__construct();
        $this->postModel = $postModel;
    }

    /**
     * @title  用户发布需求时保证金金额(填完地区,小区调用;每次修改重新调用)(sure)
     * @description 接口说明
     * @author 开发者
     * @url /api/portal/UserNeed/payMoney
     * @method POST
     * @param name:city type:int require:1 default: other: desc:地区
     * @param name:area type:int require:1 default: other: desc:面积
     */
    public function payMoney(Request $request){
        $data = $request->param();
        if(!$data['city'] || !$data['area'] ){
            $this->apiResponse('0','城市、面积不能为空');
        }
        $city = $data['city'];
        $area = $data['area'];
//        查询地区
        $where_area['area_name'] = array('like',"%$city%");
//        $where_area['parent_id'] = 0;
        $city_id = Db::name('Areas')->where($where_area)->field('area_id')->find();
        $where_money['city'] = array('like',"%".$city_id['area_id'].'%');
        $where_money['status'] = 1;
        $where_money['low_area'] = array('lt',$area);
        $where_money['height_area'] = array('egt',$area);
        $money = Db::name('MoneyRule')->where($where_money)->field('money')->find();
        if(!$money){
            $where_elsemoney['city'] = 0;
            $where_elsemoney['status'] = 1;
            $where_elsemoney['low_area'] = array('lt',$area);
            $where_elsemoney['height_area'] = array('egt',$area);
            $elsemoney = Db::name('MoneyRule')->where($where_elsemoney)->field('money')->find();
            if($elsemoney){
                $this->apiResponse('1','成功',$elsemoney['money']);
            }else{
                $this->apiResponse('0','保证金获取失败');
            }
        }else{
            $this->apiResponse('1','成功',$money['money']);
        }
    }


    /**
     * @title  用户发布需求(sure)
     * @description 接口说明
     * @author 开发者
     * @url /api/portal/UserNeed/send
     * @method POST
     * @param name:city type:varchar require:1 default: other: desc:地区(省-市-区)
     * @param name:quarters type:varchar require:1 default: other: desc:小区
     * @param name:room type:varchar require:1 default: other: desc:具体房号(1-1-1)
     * @param name:floor type:int require:1 default: other: desc:楼层
     * @param name:house_type type:varchar require:1 default: other: desc:户型
     * @param name:renovation type:varchar require:1 default: other: desc:装修
     * @param name:area type:varchar require:1 default: other: desc:建筑面积
     * @param name:is_first type:tinyint require:1 default: other: desc:是否首套房(1->首套房,2->两套及以上)
     * @param name:loan type:tinyint require:1 default: other: desc:贷款(1->贷款,2->全款)
     * @param name:contract type:tinyint require:1 default: other: desc:是否是包干价(1->是,2->否)
     * @param name:quotation_time type:int require:1 default: other: desc:报价时间
     * @param name:name type:varchar require:1 default: other: desc:联系人姓名
     * @param name:tel type:varchar require:1 default: other: desc:联系人电话
     * @param name:code type:int require:1 default: other: desc:验证码
     * @param name:else type:int require:1 default: other: desc:备注
     * @param name:token type:varchar require:1 default: other: desc:token
     * @return data:''@
     * @data type:'1->包干价,2->问答'
     * @data need_id:'需求id'
     */
    public function send(Request $request)
    {
        Db::startTrans();
        if($request->Post('token') && $request->Post('name')){
//                $data = $request->param();
//            判空,处理
                $validate = Loader::validate('UserNeed');
                if(!$validate->check($_POST)){
                    return json(array('code'=>0,'msg'=>$validate->getError()));
                }
//            加数据库
                $data = $request->Post();
//            保证金
                $city = $data['city'];
                $area = $data['area'];
//        查询地区
                $where_area['area_name'] = array('like',"%$city%");
                $city_id = Db::name('Areas')->where($where_area)->field('area_id')->find();
                $where_money['city'] = array('like',"%".$city_id['area_id'].'%');
                $where_money['status'] = 1;
                $where_money['low_area'] = array('lt',$area);
                $where_money['height_area'] = array('egt',$area);
                $money = Db::name('MoneyRule')->where($where_money)->field('money')->find();
                if(!$money){
                    $where_elsemoney['city'] = 0;
                    $where_elsemoney['status'] = 1;
                    $where_elsemoney['low_area'] = array('lt',$area);
                    $where_elsemoney['height_area'] = array('egt',$area);
                    $elsemoney = Db::name('MoneyRule')->where($where_elsemoney)->field('money')->find();
                    if($elsemoney){
                        $data['pay_money'] = $elsemoney['money'];
                    }else{
                        Db::rollback();
                        $this->apiResponse('0','保证金获取失败');
                    }
                }else{
                    $data['pay_money'] = $money['money'];
                }
                $where_mem['token'] = $data['token'];
                $mem_id = Db::name('Member')->where($where_mem)->field('id')->find();
                $data['user_id'] = $mem_id['id'];
                $model = new UserNeedModel();
//                    添加
                    unset($data['code']);
                    unset($data['connect']);
                    unset($data['token']);
                    $save = $model->save($data);

                if($save){
                    $final['userNeed_id'] = $model->getLastInsID();
                    Db::commit();
                    $this->apiResponse('1','添加成功',$final);
                }else{
                    Db::rollback();
                    $this->apiResponse('0','添加失败');
                }
        }else{

//            问答内容
            $where_pro['status'] = 1;
            $list = Db::name('NeedAnswer')->where($where_pro)->order("update_time desc")->field("title,content,id,type")->select()->toArray();
            foreach ($list as $lk=>$lv){
                $list[$lk]['content'] = htmlspecialchars_decode($lv['content']);
            }
            if($list){
                $final['answer'] = $list;
                $this->apiResponse('1','成功',$final);
            }else{
                $this->apiResponse('0','暂无内容');
            }


        }
    }
    /**
     * @title  用户修改需求(sure)
     * @description 接口说明
     * @author 开发者
     * @url /api/portal/UserNeed/changesend
     * @method POST
     * @param name:city type:varchar require:1 default: other: desc:地区(省-市-区)
     * @param name:quarters type:varchar require:1 default: other: desc:小区
     * @param name:room type:varchar require:1 default: other: desc:具体房号(1-1-1)
     * @param name:floor type:int require:1 default: other: desc:楼层
     * @param name:house_type type:varchar require:1 default: other: desc:户型
     * @param name:renovation type:varchar require:1 default: other: desc:装修
     * @param name:area type:varchar require:1 default: other: desc:建筑面积
     * @param name:is_first type:tinyint require:1 default: other: desc:是否首套房(1->首套房,2->两套及以上)
     * @param name:loan type:tinyint require:1 default: other: desc:贷款(1->贷款,2->全款)
     * @param name:contract type:tinyint require:1 default: other: desc:是否是包干价(1->是,2->否)
     * @param name:quotation_time type:int require:1 default: other: desc:报价时间
     * @param name:name type:varchar require:1 default: other: desc:联系人姓名
     * @param name:tel type:varchar require:1 default: other: desc:联系人电话
     * @param name:code type:int require:1 default: other: desc:验证码
     * @param name:else type:int require:1 default: other: desc:备注
     * @param name:need_id type:int require:1 default: other:修改需求时传 desc:需求id
     * @param name:token type:varchar require:1 default: other: desc:token
     * @return data:''@
     * @data type:'1->包干价,2->问答'
     * @data need_id:'需求id'
     */
    public function changeSend(Request $request)
    {
        if($request->Post('token') && $request->Post('name')){
//                $data = $request->param();
//            判空,处理
                $validate = Loader::validate('UserNeed');
                if(!$validate->check($_POST)){
                    return json(array('code'=>0,'msg'=>$validate->getError()));
                }
//            加数据库
                $data = $request->Post();
//            保证金
                $city = $data['city'];
                $area = $data['area'];
//            查询地区
                $where_area['area_name'] = array('like',"%$city%");
                $city_id = Db::name('Areas')->where($where_area)->field('area_id')->find();
                $where_money['city'] = array('like',"%".$city_id['area_id'].'%');
                $where_money['status'] = 1;
                $where_money['low_area'] = array('lt',$area);
                $where_money['height_area'] = array('egt',$area);
                $money = Db::name('MoneyRule')->where($where_money)->field('money')->find();
                if(!$money){
                    $where_elsemoney['city'] = 0;
                    $where_elsemoney['status'] = 1;
                    $where_elsemoney['low_area'] = array('lt',$area);
                    $where_elsemoney['height_area'] = array('egt',$area);
                    $elsemoney = Db::name('MoneyRule')->where($where_elsemoney)->field('money')->find();
                    if($elsemoney){
                        $data['pay_money'] = $elsemoney['money'];
                    }else{
                        $this->apiResponse('0','保证金获取失败');
                    }
                }else{
                    $data['pay_money'] = $money['money'];
                }
                $where_mem['token'] = $data['token'];
                $mem_id = Db::name('Member')->where($where_mem)->field('id')->find();
                $data['user_id'] = $mem_id['id'];
                $model = new UserNeedModel();

//                    修改
                    $where_need['id'] = $request->param('need_id');
                    $where_need['status'] = array('in','1,2');
                    $need = Db::name('UserNeed')->where($where_need)->find();
                    if(!$need){
                        $this->apiResponse('0','该需求不可修改');
                    }else{
                        unset($data['code']);
                        unset($data['connect']);
                        unset($data['token']);
                        $data['id'] = $data['need_id'];
                        if($need['status'] == 2){
                            unset($data['pay_money']);
                        }
                        $save = $model->isUpdate(true)->allowField(true)->save($data);
                    }
                if($save){
                    $final['need_id'] = $model->id;
                    $this->apiResponse('1','修改成功',$final);
                }else{
                    $this->apiResponse('0','修改失败');
                }


        }else{
            $data = $request->param();
            if(empty($data['need_id'])){
                $this->apiResponse('0','需求id不可為空');
            }
            $where_need['id'] = $request->param('need_id');
            $where_need['status'] = array('in','1,2');;
            $where_mem['token'] = $data['token'];
            $mem_id = Db::name('Member')->where($where_mem)->field('id')->find();
            $where_need['user_id'] = $mem_id['id'];
            $list['need'] = Db::name('UserNeed')->where($where_need)->find();
            if(!$list['need']){
                $this->apiResponse('0','该需求不可修改');
            }
//            问答内容
            $where_pro['status'] = 1;
            $answer = Db::name('NeedAnswer')->where($where_pro)->order("update_time desc")->field("title,content,id,type")->select()->toArray();
            foreach ($answer as $lk=>$lv){
                $answer[$lk]['content'] = htmlspecialchars_decode($lv['content']);
            }
            $list['answer'] = $answer;
            if($list){

                $this->apiResponse('1','成功',$list);
            }else{
                $this->apiResponse('0','暂无内容');
            }
        }
    }






//    发布需求支付
    public function needPay(Request $request){
//        添加订单
        Db::startTrans();
        $data = $request->param();
        if(!$data['need_id']){
            $this->apiResponse('0','需求不能为空');
        }
//        查询金额
        $where_need['id'] = $data['need_id'];
        $where_need['status'] = 1;
        $should_pay = Db::name('UserNeed')->where($where_need)->field('pay_money')->find();
        if(!$should_pay){
            $this->apiResponse('0','需求错误');
        }else{
            if(!$should_pay['pay_money']){
                $this->apiResponse('0','诚意金金额有误');
            }
        }
//        拼字度
        $add['need_id'] = $data['need_id'];
        $add['money'] = $should_pay['pay_money'];
        $add['name'] = '诚意金缴纳';
        $add['type'] = 1;
        $add['is_get'] = 2;
        $add['status'] = 1;
//        加入数据库
        $model = new OrderModel();
        $save = $model->save($add);
        if($save){
            Db::commit();
        }else{
            Db::rollback();
        }
//        支付(未完)
        echo "<pre/>";
        print_r('3333');
        die;
//        更改订单状态
//        成功
        $update['pay_time'] = time();
        $update['status'] = time();
        $update = $model->isUpdate(true)->allowField(true)->save($update);
        if($update){
            $this->apiResponse('1','支付成功');
        }

    }



}