该方法有自己的模板
主页面的几个方法
与微信交互
1 <?php
2
3 namespace AddonsSurveyController; //命名空间
4
5 use HomeControllerAddonsController; //调用插件控制器类
6
7 class SurveyController extends AddonsController { //定义调研类名
8 function survey_question() { //调研问题管理方法
9 $param ['survey_id'] = I ( 'id', 0, 'intval' ); //问题ID
10 $url = addons_url ( 'Survey://Question/lists', $param ); //问题列表页面url
11 // dump($url);
12 redirect ( $url ); //跳转页面
13 }
14 function survey_answer() { //数据管理
15 $param ['survey_id'] = I ( 'id', 0, 'intval' ); //问题id
16 $url = addons_url ( 'Survey://Answer/lists', $param ); //数据列表页面
17 // dump($url);
18 redirect ( $url ); //跳转到数据管理页面
19 }
20 function preview() { //调研预览页面
21 $param ['survey_id'] = I ( 'id', 0, 'intval' ); //问题ID
22 $url = addons_url ( 'Survey://Survey/show', $param ); //调研页面URL
23 // dump($url);
24 redirect ( $url ); //跳转到调研页面
25 }
26 function show() { //调研页面
27 $map ['id'] = I ( 'survey_id', 0, 'intval' ); //设置数组中id值
28 $map ['token'] = get_token (); //设置数组中token
29 $info = M ( 'survey' )->where ( $map )->find (); //survey表中查询问题的信息
30 $this->assign ( 'info', $info ); //分配给前台显示
31
32 $this->display (); //调用模板显示
33 }
34 function profile() { //填写资料
35 $map ['id'] = $this->mid; //设置数组id
36 $info = M ( 'follow' )->where ( $map )->find (); //查询follow表获取相应信息
37 $this->assign ( 'info', $info ); //穿信息到前端
38
39 if (IS_POST) { //若页面有传参过来
40 if (! empty ( $_POST ['nickname'] ) && $_POST ['nickname'] != $info ['nickname']) {
41 $data ['nickname'] = I ( 'post.nickname' ); //判断是否提交姓名并组装数组
42 }
43 if (! empty ( $_POST ['mobile'] ) && $_POST ['mobile'] != $info ['mobile']) {
44 $data ['mobile'] = I ( 'post.mobile' ); //判断是否提交手机
45 }
46
47 if (! empty ( $data )) { //判断并查询follow表信息
48 $res = M ( 'follow' )->where ( $map )->save ( $data );
49 }
50
51 redirect ( U ( 'survey', 'survey_id=' . $_REQUEST ['survey_id'] ) ); //重定向到答题页面
52 exit ();
53 }
54
55 $this->display (); //显示页面
56 }
57 function survey() { //调用打开页面
58 $map ['id'] = intval ( $_REQUEST ['survey_id'] ); //接收传参并转为整型
59 $map ['token'] = get_token (); //获取token
60 $survey = M ( 'survey' )->where ( $map )->find (); //查表获得数据
61
62 $map ['survey_id'] = $map ['id']; //重新数组$map
63 unset ( $map ['id'] ); //销毁$map['id']
64 $list = M ( 'survey_question' )->where ( $map )->order ( 'sort asc, id asc' )->select (); //查表获得数据
65
66 if (IS_POST) { //如果用户答题并提交
67 $map ['uid'] = $this->mid; //设置uid
68 $map ['question_id'] = I ( 'post.question_id', 0, 'intval' ); //用数组接收问题id
69 $answer = M ( 'survey_answer' )->where ( $map )->find (); //从数据库中查答案选项
70
71 $data ['cTime'] = time (); //获取提交时间
72 $data ['answer'] = serialize ( $_POST ['answer'] ); //序列化答案
73 if ($answer) {
74 M ( 'survey_answer' )->where ( $map )->save ( $data ); //保存答案
75 } else { //否则组织数据
76 $data ['survey_id'] = $map ['survey_id']; //调研id
77 $data ['token'] = $map ['token']; //token值
78 $data ['question_id'] = $map ['question_id']; //问题id
79 $data ['uid'] = $map ['uid']; //uid
80 $data ['openid'] = get_openid (); //用户openid
81 M ( 'survey_answer' )->add ( $data ); //添加到数据库
82 }
83 }
84
85 $question_id = I ( 'post.next_id', 0, 'intval' ); //设置question_id
86 if ($question_id == '-1') { //若question_id 值等于-1,
87 redirect ( U ( 'finish', 'survey_id=' . $map ['survey_id'] ) ); 跳转到调研结束页面
88 }
89
90 if (empty ( $question_id )) { //若问题question_id为空
91 $question = $list [0];
92 $next_id = isset ( $list [1] ['id'] ) ? $list [1] ['id'] : '-1'; //检查调研问题是否有下一个
93 } else {
94 foreach ( $list as $k => $vo ) { //遍历数组
95 if ($vo ['id'] == $question_id) {
96 $question = $vo;
97 $next_id = isset ( $list [$k + 1] ['id'] ) ? $list [$k + 1] ['id'] : '-1';
98 }
99 }
100 }
101
102 $extra = parse_config_attr ( $question ['extra'] ); //枚举类型配置
103
104 $this->assign ( 'survey', $survey ); //传值
105 $this->assign ( 'question', $question );
106 $this->assign ( 'next_id', $next_id );
107 $this->assign ( 'extra', $extra );
108
109 $this->display (); //显示
110 }
111 function finish() { //调研结束
112 $map ['id'] = I ( 'survey_id', 0, 'intval' ); //取值
113 $map ['token'] = get_token (); //取值
114 $info = M ( 'survey' )->where ( $map )->find (); //查表
115 $this->assign ( 'info', $info ); //分配信息
116
117 // 增加积分
118 add_credit ( 'survey' );
119
120 $this->display ();
121 }
122 }