forked from nsjcy/frontEnd/nsjcy

liuwh
2020-03-31 a0ec2242677dced1e6e098c114460da3d56be157
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
// pages/createSub/createSub.js
const app = getApp();
const $v = app.globalData.createInfo;
Page({
 
  /**
   * 页面的初始数据
   */
  data: {
    question: {},
    confirm: true,
    value1: '',
    answer: [],
    id: ''
  },
  // 去单选页面
  toRadio(e) {
    var types = e.currentTarget.dataset.type;
    wx.navigateTo({
      url: '/pages/radio/radio?type=' + types,
    })
  },
  //去编辑页面
  edit(e) {
    var index = e.currentTarget.dataset.index;
    wx.navigateTo({
      url: '/pages/radio/radio?index=' + index,
    })
  },
  // 提交信息
  submit() {
    console.log(this.data.question)
    var list = this.data.question.psqList.map(({
      questionId,
      questionItemId,
      objectId,
      questionnaireId
    }) => ({
      questionId,
      questionItemId,
      objectId,
      questionnaireId
    }))
    console.log(list)
 
    var that = this;
    wx.request({
      url: app.globalData.url + '/api/question/submitResult',
      data: list,
      method: 'POST',
      header: {
        "Content-Type": "application/json"
      },
      success: function(res) {
        if (res.data.code == 0) {
          wx.showToast({
            title: '提交成功!',
            icon: 'success',
            duration: 2000
          })
          setTimeout(function() {
            wx.reLaunch({
              url: '../yshjqyfw/yshjqyfw',
            })
          }, 2000)
        } else {
          wx.showModal({
            title: '提示',
            content: "提交失败!"
          })
        }
      }
    })
    // if ($v.psqList.length == 0) {
    //   wx.showToast({
    //     title: '你还没有创建题目',
    //     icon: "none",
    //     duration: 800
    //   });
    //   return;
    // }
    // wx.showToast({
    //   title: '提交成功!',
    //   icon: 'success',
    //   duration: 2000
    // }, wx.reLaunch({
    //   url: '../yshjqyfw/yshjqyfw',
    // }))
 
  },
  // 发布信息
  publish() {
    app.res({
      url: "psq/save",
      method: "POST",
      data: $v,
      callback: res => {
        $v.paper = {};
        $v.psqList = [];
        wx.showModal({
          title: '提示',
          content: '发布成功,跳转新建问卷',
          showCancel: false,
          success: res => {
            if (res.confirm) {
              wx.switchTab({
                url: '/pages/creat/creat',
                success: function(e) {
                  var page = getCurrentPages().pop();
                  if (page == undefined || page == null) return;
                  page.onLoad();
                }
              })
            }
          }
        })
      }
    })
  },
  returnEdit() {
    this.setData({
      confirm: true
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var _m = "question";
    var id = options.id
    console.log(options)
    var that = this;
    wx.request({
      url: app.globalData.url + '/api/question/find/' + id,
      success: function(res) {
        console.log(res)
        if (res.data.code == 0) {
          console.log(res)
          that.setData({
            id: res.data.data.id,
            [_m]: {
              "paper": {
                "title": res.data.data.title,
                "des": res.data.data.content
              },
              "psqList": res.data.data.questionDtos
              // "psqList": [{
              //   "isMust": 1,
              //   "type": 1,
              //   "question": "111",
              //   "answerslist": [{
              //     "answer": "11"
              //   }, {
              //     "answer": "111"
              //   }, {
              //     "answer": "11"
              //   }, {
              //     "answer": "111"
              //   }],
              //   'value': []
              // }, {
              //   "isMust": 1,
              //   "type": 1,
              //   "question": "11111",
              //   "answerslist": [{
              //     "answer": "111"
              //   }, {
              //     "answer": "111"
              //   }],
              //   value: []
              // }, {
              //   "isMust": 1,
              //   "type": 0,
              //   "question": "11111"
              // }, {
              //   "isMust": 0,
              //   "type": 1,
              //   "question": "wqwqw",
              //   "answerslist": [{
              //     "answer": "qw"
              //   }, {
              //     "answer": "wqw"
              //   }, {
              //     "answer": "wqw"
              //   }]
              // }]
            }
 
          });
        } else {
          wx.showModal({
            title: '提示',
            content: "请求失败!"
          })
        }
      }
    })
 
  },
 
  onChange1(e) {
    var index = e.currentTarget.dataset['index'];
    var userinfo = wx.getStorageSync("user");
    var questionId = e.currentTarget.dataset['questionId'];
    const {
      value
    } = e.detail;
 
    console.log('checkbox', e, '索引', index);
    console.log('value', value);
    console.log('questionId', questionId);
    console.log('this.data.question', this.data.question);
    const data = this.data.question.psqList[index].value || [];
    console.log(data)
    this.data.question.psqList[index].value = value
    this.data.question.psqList[index].questionItemId = this.data.question.psqList[index].optionsId[this.data.question.psqList[index].options.indexOf(value)]
    this.data.question.psqList[index].objectId = userinfo.id
    this.data.question.psqList[index].questionnaireId = this.data.id
 
    console.log('this.data.question', this.data.question)
    this.setData({
      question: this.data.question
    })
  },
 
  onChange: function(e) {
    var index = e.currentTarget.dataset['index'];
    var userinfo = wx.getStorageSync("user");
    var questionId = e.currentTarget.dataset['questionId'];
    const {
      value
    } = e.detail;
    console.log('checkbox', e, '索引', index);
    const data = this.data.question.psqList[index].value || [];
    const idx = data.indexOf(value)
    const current = idx === -1 ? [...data, value] : data.filter((n) => n !== value)
    console.log('current', current);
    this.data.question.psqList[index].value = current
    this.data.question.psqList[index].questionItemId = this.data.question.psqList[index].optionsId[this.data.question.psqList[index].options.indexOf(value)]
    this.data.question.psqList[index].objectId = userinfo.id
    this.data.question.psqList[index].questionnaireId = this.data.id
    this.setData({
      question: this.data.question
    })
 
  }
 
})