import { sceneTypes1 } from '../../../data/sceneTypes'; const userservice = require('../../../service/userservice'); const app = getApp(); Page({ /** * 页面的初始数据 */ data: { sceneTypes: sceneTypes1, sceneTypeIndex: 0, imgFiles: [], msg: [ { name: '用户名称', id: 'username', input: true, value: '', noValue: false, }, { name: '新密码', id: 'password', input: true, type: 'password', maxLength: 20, value: '', noValue: false, }, { name: '确认密码', id: 'number', input: true, type: 'password', placeholder: '请再次确认密码', maxLength: 20, value: '', noValue: false, }, { name: '企业名称', id: 'department', input: true, value: '', noValue: false, }, { name: '企业地址', id: 'address', input: true, value: '', noValue: false, }, { name: '联系方式', id: 'contract', input: true, value: '', noValue: false, required: false, }, { name: '信用代码', id: 'code', input: true, value: '', noValue: false, required: false, }, ], }, /** * 生命周期函数--监听页面加载 */ onLoad(options) {}, changeSceneType(e) { let i = e.detail.value; this.setData({ sceneTypeIndex: i, }); }, 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, }); } } }, uploadFile(file) { let data = file.detail.newFiles; data.forEach(element => { element.loading = false; }); this.setData({ imgFiles: data, }); }, onSubmit: function () { if (!this.submitCheck()) return; let msg = this.data.msg; let info = { sceneType: this.data.sceneTypes[this.data.sceneTypeIndex].value, userName: msg[0].value, password: msg[1].value, department: msg[3].value, address: msg[4].value, telephone: msg[5].value, orgCode: msg[6].value, }; wx.showLoading({ title: '注册中', mask: true, }); setTimeout(() => { wx.hideLoading(); }, 10000); var that = this; userservice.register(info, { success(res) { if (res.success) { app.globalData.accessToken = { userId: res.userId, suserId: res.suserId, }; that.getUserInfo(); } else { wx.hideLoading({ success: res => { wx.showToast({ title: '用户名称重复', duration: 2000, icon: 'error', mask: true, }); }, fail: res => {}, complete: res => {}, }); } }, }); }, /** * 信息完整度检查 */ submitCheck() { let msg = this.data.msg; let msgLength = msg.length; //确认信息完整度 for (let i = 0; i < msgLength; i++) { if (msg[i].required != false && msg[i].value === '') { let error = msg[i].name + '不可为空'; let path = 'msg[' + i + '].noValue'; this.setData({ errorMsg: error, [path]: true, }); wx.pageScrollTo({ duration: 300, scrollTop: 0, // selector:"#" + msg[i].id, }); 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; }, getUserInfo() { userservice.getUserInfo(app.globalData.accessToken.userId, { success(data) { app.globalData.accessToken.userName = data.acountname; app.globalData.accessToken.password = data.acountname; wx.setStorage({ key: 'accessToken', data: app.globalData.accessToken, }); app.globalData.userInfo = data; wx.setStorage({ key: 'userInfo', data: data, success: result => { wx.switchTab({ url: '/pages/home/home', }); }, }); }, }); }, });