NeedController.php 5.2 KB
<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------
namespace app\portal\controller;
use cmf\controller\AdminBaseController;
use app\portal\model\NeedAnswerModel;
use app\portal\model\MemberModel;
use app\portal\model\UserModel;
use app\portal\service\PostService;
use think\Db;
use think\Request;
use think\Loader;
/**
 * @title 保证金内容管理
 * @description 接口说明
 * @group 接口分组
 */

class NeedController extends AdminBaseController
{
//    列表
    public function index(Request $request)
    {
//        $where_coo['status'] = 1;
//        $where_coo['type'] = 2;
        $where_coo = array();
        if($request->post()){
            $search = $request->post();
            if($search['start_time']){
                $start = strtotime($search['start_time']);
                if($search['end_time']){
                    $end = strtotime($search['end_time']);
                }else{
                    $end = time();
                }
                $where_coo['n.update_time'] = array('between',"$start,$end");
                $this->assign('start_time',date('Y-m-d H:i',$start));
                $this->assign('end_time',date('Y-m-d H:i',$end));
            }
            if($search['keyword']){
                $where_coo['n.title'] = array('like',"%".$search['keyword']."%");
                $this->assign('keyword',$search['keyword']);
            }
        }
        $coo_list = Db::name('UserNeed')->alias('n')
            ->join("Member m",'m.id = n.user_id')
            ->where($where_coo)
            ->field("m.name as user_name,m.tel as user_tel,n.*")
            ->order('create_time desc')
            ->select()->toArray();


        $this->assign('list',$coo_list);
        return $this->fetch('index');
    }


//    详情
    public function detail(){
        $data = $this->request->param();
        $where_need['n.id'] = $data['id'];
        $where_need['n.status'] = array('neq',9);
        $need = Db::name('UserNeed')->alias('n')
            ->join("Member m",'m.id = n.user_id')
            ->where($where_need)
            ->field('m.name as user_name,n.*')
            ->find();
        if(!$need){
            $this->error('需求错误');
        }
        $this->assign('list',$need);
        return $this->fetch();
    }

//    修改信息
    public function change(Request $request)
    {
        if($request->post()){
//            添加数据
            $data = $request->post();
            if($request->Post('id')){
//                修改
                $add['content'] = $data['content'];
                $add['title'] = $data['title'];
                $add['score'] = $data['score'];
                $add['type'] = 2;
                $validate = Loader::validate('NeedAnswer');
                if(!$validate->scene('edit')->check($add)){
                    $mes = $validate->getError();
                    $this->error("$mes");
                }
                $add['id'] = $request->param('id');
                $model = new NeedAnswerModel();
                $final = $model->isUpdate(true)->allowField(true)->save($add);
            }else{
//                添加
                $add['content'] = $data['content'];
                $add['title'] = $data['title'];
                $add['score'] = $data['score'];
                $add['type'] = 2;
                $validate = Loader::validate('NeedAnswer');
                if(!$validate->scene('add')->check($add)){
                    $mes = $validate->getError();
                    $this->error("$mes");
                }

                $model = new NeedAnswerModel();
                $final = $model->save($add);
            }
            if($final){
                $this->success('成功',url('index'));
            }else{
                $this->error('失败');
            }
        }else{
            $where_find['type'] = $request->param('id');
            $list = Db::name('NeedAnswer')->where($where_find)->find();
            if($list){
                $list['content'] = htmlspecialchars_decode($list['content']);
            }
            $this->assign('list',$list);
            return $this->fetch('add');
        }
    }


    //  删除
    public function del(Request $request){
        $ids = $this->request->post();
        $id = $request->param('id');
        if($ids){
            $add_del['id'] = array('in',$ids['ids']);
        }else if($id){
            $add_del['id'] = $request->param('id');
        }else{
            $this->error('删除失败');
        }
        $add_del['status'] = 9;
        $model = new NeedAnswerModel();
        $del = $model->isUpdate(true)->allowField(true)->save($add_del);
        if($del){
            $this->success('删除成功',url('index'));
        }else{
            $this->error('删除失败');
        }
    }

}