questionnaire.js
5.07 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// packageMine/pages/questionnaire/questionnaire.js
Page({
/**
* 页面的初始数据
*/
data: {
finish: false
},
answerList: {},
blur(e) {
const {
value
} = e.detail, {
num
} = e.currentTarget.dataset
this.change({
detail: {
num,
value
}
})
},
/**
* 生命周期函数--监听页面加载
*/
onLoad(options) {
this.setData(options)
},
async getQuestionnaireDetail(options) {
const res = await wx.p.router.intercept()
if (!res) {
const {
userInfo
} = this.data
if (!userInfo.nickName) return this.getNickNameAndHeadUrl()
try {
const data = await wx.p.api.questionnaireApi.getQuestionnaireDetail(options)
if (data.status == -1) {
return wx.showModal({
title: '温馨提示',
content: '问卷活动已结束。',
showCancel: false,
confirmText: '返回首页',
confirmColor: '#3772EC',
complete: (res) => {
if (res.confirm) wx.p.router.goHome()
}
})
}
if (data) {
data.questions.map(e => {
if (e.type != 3) {
let option = e.option.split('\n')
if (e.type == 5) {
const [, two, three] = option[0].split('/')
option.splice(0, 1)
e.data = this.transformData(option, three ? 3 : two ? 2 : 1)
} else e.data = option
}
e.answer = ''
e.userFormId = e.formId
})
this.setData({
questionnaireObj: data
})
wx.setNavigationBarTitle({
title: data.title,
})
this.shareTitle = data.cardTitle || data.title
this.shareQuery = `id=${data.id}`
this.shareImage = data.cardCover || ''
this.addTrackInfo()
} else throw '获取问卷失败'
} catch (error) {
console.log(error)
if (error) wx.p.wxApi.showToast(error)
}
}
},
transformData(data, childrenDepth) {
const result = []
data.forEach(item => {
const levels = item.split('/')
let currentLevel = result
for (let i = 0; i < childrenDepth; i++) {
const text = levels[i],
value = text,
existingOption = currentLevel && currentLevel.find((option) => option.text === text)
if (existingOption) {
if (i === childrenDepth - 1) {
continue
}
if (existingOption.children) currentLevel = existingOption.children
} else {
if (text && value) {
const newOption = {
text,
value,
}
if (levels[i + 1]) newOption.children = []
currentLevel.push(newOption)
if (i === childrenDepth - 1) delete newOption.children
currentLevel = newOption.children
}
}
}
})
return result
},
change(e) {
const {
num,
value
} = e.detail
this.answerList[num] = value
},
async submit() {
const {
questionnaireObj
} = this.data, {
merchantId,
} = wx.p.storage.get('userInfo'), {
answerList
} = this
try {
let questions = []
questionnaireObj.questions.map(e => {
if (e.required && answerList[e.num] == null) throw `请填写第${e.num}题!`
questions.push({
userFormId: e.userFormId,
num: e.num,
answer: answerList[e.num] || ''
})
})
let params = {
questions,
merchantId,
formId: questionnaireObj.id,
}
console.log(params)
const res = await wx.p.api.questionnaireApi.submitQuestionnaireForm(params)
if (res) {
this.setData({
finish: true
})
} else throw '提交失败'
console.log(res)
} catch (error) {
console.log(error)
if (error) wx.p.wxApi.showToast(error)
}
},
getNickNameAndHeadUrl() {
const redirectUrl = `https://scrm.glsx.com.cn/wy-wx-oauth`,
appid = 'wx1cf40dc91c28a7fd',
wxUrl = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri=${encodeURIComponent(redirectUrl)}&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect`
wx.p.router.jumpH5(wxUrl)
},
/**
* 生命周期函数--监听页面显示
*/
onShow() {
const {
options
} = this
if (options.id) this.getQuestionnaireDetail(options)
},
/**
* 用户点击右上角分享
*/
onShareAppMessage() {
},
// 添加埋点信息
addTrackInfo(){
const {userInfo} = this.data
let item = {
add_way: '104',
customer_phone: userInfo.phone
}
if (userInfo.nickName) {
item.customer_qw_nickname = userInfo.nickName
}
if (userInfo.headImgUrl) {
item.avatar = userInfo.headImgUrl
}
if (userInfo.unionId) {
item.union_id = userInfo.unionId
}
let json = JSON.stringify(item)
// 数据上报
wx.p.api.common.trackHttp({ json }).then(response => { })
}
})