riku
2022-10-10 437144f41c74505d362a5214a18cec3d01b3ce4b
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
/**
 * 
 */
module.exports = Behavior({
  data: {
    msg: [],
    info: {}
  },
  lifetimes: {
    attached: function () {}
  },
  methods: {
    changeMsg(e) {
      let id = e.detail.params.id
      let value = e.detail.params.value
      let msg = this.data.msg
      let msgLength = msg.length
      for (let i = 0; i < msgLength; i++) {
        if (msg[i].id === id) {
          let path = "msg[" + i + "].value"
          let nPath = "msg[" + i + "].noValue"
          this.setData({
            [path]: value,
            [nPath]: false
          })
        }
      }
    },
 
    onSubmit: function () {
      console.log('onSubmit');
      if (!this.submitCheck()) return
 
      let msg = this.data.msg
      if (msg) {
        let info = {}
        msg.forEach(m => {
          info[m.id] = m.value
        });
        // console.log(info);
        this.setData({info})
        this.triggerEvent('onSubmit', info)
        // wx.showToast({
        //   title: 'onSubmit',
        //   duration: 1000,
        //   icon: 'none',
        //   success: (res) => {},
        //   fail: (res) => {},
        //   complete: (res) => {},
        // })
      }
    },
 
    /**
     * 信息完整度检查
     */
    submitCheck() {
      let msg = this.data.msg
      //确认信息完整度
      for (let i = 0; i < msg.length; i++) {
        const m = msg[i]
        //选填项直接略过
        if (m.required == false) continue
        //复选框和单选框(复选框的逻辑包含了单选框)
        if (m.type == 'checkbox' || m.type == 'radio') {
          // console.log('m.options', m.options);
          m.value = ''
          for (let t = 0; t < m.options.length; t++) {
            const o = m.options[t];
            if (o.checked) {
              if (m.value != '') {
                m.value += ';'
              }
              if (o.hasRemark) {
                // console.log('o.remark', o.remark);
                if (o.remark == '') {
                  this.setData({
                    [`msg[${i}].options[${t}].noValue`]: true
                  })
                  this.showErrorMsg(i, '备注信息不能为空')
                  return false
                } else {
                  m.value += o.remark
                }
              } else {
                m.value += o.name
              }
            }
          }
          if (m.value == '') {
            this.showErrorMsg(i)
            return false
          }
        }
        //下拉选择框
        else if (m.type == 'picker') {
          if (m.pickerMode == 'selector') {
            if (m.selectIndex == 0) {
              this.showErrorMsg(i, `${this.data.msg[i].name}未选择`)
              return false
            } else {
              m.value = m.options[m.selectIndex].name
            }
          } else if (m.pickerMode == 'region') {
            if (m.value.length == 0) {
              this.showErrorMsg(i, `${this.data.msg[i].name}未选择`)
              return false
            }
          }
        }
        //输入框
        else if (m.value === "") {
          this.showErrorMsg(i)
          return false
        }
      }
 
      // //密码一致性确认
      // if (msg[1].value != msg[2].value) {
      //   let error = "两次输入的密码不一致"
      //   this.setData({
      //     errorMsg: error,
      //   })
      //   return false
      // }
      // //密码复杂度确认
      // var pwError
      // var pw = msg[1].value
      // var regex1 = /[a-zA-Z]/
      // var regex2 = /[0-9]/
      // var r = regex1.test(pw) && regex2.test(pw)
      // console.log('regex:' + r);
      // if (pw.length < 6) {
      //   pwError = '密码位数最少6位'
      // } else if (!r) {
      //   pwError = '密码必须包含字母和数字'
      // }
      // if (pwError) {
      //   this.setData({
      //     errorMsg: pwError,
      //   })
      //   return false
      // }
 
      return true
    },
 
    showErrorMsg(index, err = `${this.data.msg[index].name}不可为空`) {
      this.setData({
        errorMsg: err,
        [`msg[${index}].noValue`]: true
      })
      const id = "#" + this.data.msg[index].id
      wx.createSelectorQuery().select('.page').boundingClientRect(res1 => {
        const top1 = res1.top
        wx.createSelectorQuery().in(this).select(id).boundingClientRect(res => {
          const h = res.top - top1 - 60
          // console.log(h);
          wx.pageScrollTo({
            // duration: 300,
            scrollTop: h,
            // selector: id,
            // offsetTop: -60,
          })
        }).exec();
      }).exec();
    }
  }
})