const api = require('../../../../utils/api') import { $wuxDialog } from '../../../../dist/index' const app = getApp() Page({ /** * 页面的初始数据 全部为必填项 */ data: { realName: '',//真实姓名 mobile: '',//手机号 identityNo: '',//身份证号码 road: '',//所在道路 villageName: '',//小区名 dwellingPlace: '',//住处(楼栋-单元-房间) gridId: Number,//居住网格id gridName:'', wxCode: '',//用户unionId为空时 传入 encryptedData: '',//用户unionId为空时 传入 iv: '',//用户unionId为空时 传入 introduce: '',//自我介绍 unionIdStatus: '0', wait: 60, smsCode: '', btntxt: '获取验证码', dialogTitle: '', dialogContent: '', dialogConfirmText: '', dialogCancelText: '', getMobileType: 'wx', smsCodeText: '获取验证码' }, onLoad: function (options) { this.getPrepareComplete().then(res => { this.getGridList() }) this.checkWxUnionId() this.getWxCode() }, bindRealNameInput (e) { this.setData({ realName: e.detail.value }) }, bindMobileInput (e) { this.setData({ mobile: e.detail.value }) }, bindSmsCodeInput (e) { this.setData({ smsCode: e.detail.value }) }, bindIdentityNoInput (e) { this.setData({ identityNo: e.detail.value }) }, adInputStreet (e) { this.setData({ road: e.detail.value }) }, adInputCommunity (e) { this.setData({ villageName: e.detail.value }) }, adInputBuilding (e) { this.setData({ dwellingPlace: e.detail.value }) }, adInputIntroduce (e) { this.setData({ introduce: e.detail.value }) }, // 查看用户是否完善个人信息 checkWxUnionId() { api.checkWxUnionId().then(res => { console.log('查看用户是否已保存unionId', res) this.setData({ unionIdStatus: res.data }) }).catch(err => { this.setData({ unionIdStatus: '0' }) console.log(err) }) }, // 获取wxCode getWxCode() { const that = this wx.login({ success(res) { that.setData({ wxCode: res.code }) } }) }, // 获取用户信息 getUserInfo(e) { this.setData({ encryptedData: e.detail.encryptedData, iv: e.detail.iv }) if (e.detail.iv){ this.submission() }else{ wx.showToast({ title: '您拒绝了获取信息,无法完成认证', icon: 'none', duration: 3000 }) } }, // 拉取 用户信息 getPrepareComplete() { return new Promise((resolve, reject) => { api.prepareComplete().then(res => { console.log('拉取用户信息', res) this.setData({ identityNo: res.data.identityNo, realName: res.data.realName, mobile: res.data.mobile, road: res.data.road, villageName: res.data.villageName, dwellingPlace: res.data.dwellingPlace, state: res.data.state, gridId: res.data.gridId, gridName: res.data.gridName }) resolve(true) }).catch(err => { reject(false) }) }) }, // 获取所有网格 getGridList() { api.getGridList().then(res => { console.log('获取所有网格', res) const buttonList = [] res.data.forEach(item => { if (item.gridId !== this.data.gridId) { buttonList.push({ id: item.gridId, text: item.grid }) } }) this.setData({ gridList: res.data, changeGridList: buttonList }) }).catch(err => { this.setData({ gridList: [] }) console.log(err) }) }, // 志愿者认证提交 submission() { if (!this.data.realName) { this.showToast('请输入姓名') return false } if (this.data.realName.length > 20) { this.showToast('姓名不能超过20个字') return false } if (!this.data.identityNo) { this.showToast('请输入身份证号') return false } if(this.data.getMobileType === 'wx') { if (!this.data.mobile) { this.showToast('请获取手机号') return false } } else if (this.data.getMobileType === 'self') { if (!this.data.mobile) { this.showToast('请输入手机号') return false } if (!this.data.smsCode) { this.showToast('请输入验证码') return false } } if (!this.data.road) { this.showToast('请输入所在道路') return false } if (this.data.road.length > 100) { this.showToast('所在小区不能超过100个字') return false } if (!this.data.introduce) { this.showToast('请输入自我介绍') return false } if (!this.data.introduce.length > 200) { this.showToast('自我介绍输入内容不超过200字') return false } const params = { smsCode: this.data.smsCode, realName: this.data.realName, mobile: this.data.mobile, identityNo: this.data.identityNo, road: this.data.road, villageName: this.data.villageName, dwellingPlace: this.data.dwellingPlace, gridId: this.data.gridId, introduce: this.data.introduce, wxCode: this.data.unionIdStatus === '1' ? '' : this.data.wxCode, encryptedData: this.data.unionIdStatus === '1' ? '' : this.data.encryptedData, iv: this.data.unionIdStatus === '1' ? '' : this.data.iv } wx.showLoading({ title: '正在提交', }) api.authenticate(params).then( res => { wx.hideLoading() let state = res.data.userState app.globalData.infoCompleted = state wx.removeStorageSync('token') wx.setStorageSync('token', res.data.token) this.setData({ dialogVisible: !this.data.dialogVisible, dialogTitle: '志愿者认证', dialogContent: ['您已认证成功,快去报名吧'], dialogConfirmText: '是', dialogCancelText: '否' }) }) }, confirmDialog () { wx.navigateBack({ delta: 1 }) }, changeGetMobileType () { let type = 'wx' if (this.data.getMobileType === 'wx') { type = 'self' } else if (this.data.getMobileType === 'self') { type = 'wx' } this.setData({ getMobileType: type }) }, // 获取验证码 getSmsCode () { if (!this.data.mobile) { this.showToast('请先输入手机号') return false } this.setData({ smsCodeText: '获取中...' }) const mobile = this.data.mobile api.sendSms(mobile).then(res => { this.showToast('验证码发送成功') let num = 60 this.setData({ smsCodeText: "60s后重新获取" }) const timer = setInterval(() => { if (num >= 1) { this.setData({ smsCodeText: `${num}s后重新获取` }) --num } else { clearInterval(timer) this.setData({ smsCodeText: '获取验证码' }) } }, 1000) }).catch(err =>{ console.log(err) this.setData({ smsCodeText: '获取验证码' }) }) }, // 获取手机号 getPhoneNumber (e) { if (e.detail.errMsg === 'getPhoneNumber:ok') { const para = { wxCode: '', encryptedData: e.detail.encryptedData, iv: e.detail.iv } const that = this wx.login({ success (res) { para.wxCode = res.code api.getWxPhone(para).then(data => { console.log('获取微信手机号', data) that.setData({ mobile: data.data }) }).catch(err => { console.log(err) }) } }) } }, // 代码简化,弹窗统一封装 showToast (title) { wx.showToast({ title: title, icon: 'none', duration: 2000 }) }, })