QuestionController.php 4.0 KB
<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/26
 * Time: 13:20
 */

namespace app\index\controller;


use app\index\model\QuestionModel;
use app\index\model\UserModel;
use cmf\controller\WeChatBaseController;

use think\Db;
use think\Validate;

class QuestionController extends WeChatBaseController
{
    //授权
    function _initialize()
    {
        //判断用户是否微信浏览器打开
        //$this->isWechat();
        //判断是否手机端
        //$this->isMobile();
        //微信授权
        parent::_initialize();
        $this->checkWeChatUserLogin();
        //阻止拉黑用户
        $this->ban();
    }
    public function index(){
        $this->assign(
            array(
                'title'=>"问卷调查"
            )
        );
        return $this->fetch();
    }

    public function create_question(){
        $user_id = cmf_get_current_user_id();
        //判断是否提交过问卷
        $questionModel = new QuestionModel();
        $question = $questionModel->findData(array('user_id'=>$user_id));
        if(!empty($question)){
            $arr['code'] = 40005;
            $arr['msg'] = '您已提交过一份了!';
            return json_encode($arr);
        }
        $param = $this->request->param();
        $validate = new Validate([
            'name' => 'require',
            'sex' => 'require',
            'birthday' => 'require',
            'area' => 'require',
            'family_num' => 'require',
            'occupation' => 'require',
            'smoke_drink' => 'require',
            'going' => 'require',
            'social' => 'require',
            'year_income' => 'require',
            'personage_year_income' => 'require',
            'liabilities' => 'require',
            'not_return' => 'require',
            'family_disease' => 'require',
            'safeguard' => 'require',
            'phone' => 'require',
        ]);
        $validate->message([
            'name' => '姓名不能为空!',
            'sex' => '请选择性别!',
            'birthday' => '请选择出生日期!',
            'area' => '所在地区不能为空!',
            'family_num' => '家庭结构不能为空!',
            'occupation' => '职业不能为空!',
            'smoke_drink' => '有无吸烟酗酒史!',
            'going' => '出行方式不能为空!',
            'social' => '有无社保!',
            'year_income' => '家庭年收入!',
            'personage_year_income' => '个人年收入!',
            'liabilities' => '有无负债!',
            'not_return' => '大约负债!',
            'family_disease' => '有无家族病!',
            'safeguard' => '保障多选!',
            'phone' => '电话不能为空!',
        ]);
        if (!$validate->check($param)) {
            $this->error($validate->getError());
        }
        $safeguard = implode(',',$param['safeguard']);
        $data = [
            'name'=>$param['name'],
            'sex'=>$param['sex'],
            'birthday'=>$param['birthday'],
            'area'=>$param['area'],
            'family_num'=>$param['family_num'],
            'occupation'=>$param['occupation'],
            'smoke_drink'=>$param['smoke_drink'],
            'going'=>$param['going'],
            'social'=>$param['social'],
            'year_income'=>$param['year_income'],
            'personage_year_income'=>$param['personage_year_income'],
            'liabilities'=>$param['liabilities'],
            'not_return'=>$param['not_return'],
            'family_disease'=>$param['family_disease'],
            'safeguard'=>$safeguard,
            'phone'=>$param['phone'],
            'create_time'=>time(),
            'user_id'=>$user_id,
        ];
        $result = Db::name('question')->insert($data);
        if(empty($result)){
            $arr['code'] = 40000;
            $arr['msg'] = "保存失败!";
        }else{
            $arr['code'] = 20000;
            $arr['msg'] = "保存成功!";
        }
        return json_encode($arr);

    }
}