QuestionController.php
4.0 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?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);
}
}