// pages/login/index.js import * as API_Passport from '../../api/passport.js' import Toast from '../../miniprogram_npm/vant-weapp/toast/toast.js' import * as RegExp from '../../utils/RegExp.js' import * as API_Shop from '../../api/shop.js' import * as Reg from '../../utils/RegExp.js' import * as Common from '../../utils/common.js' import { $wuxToptips, $wuxToast, $wuxLoading, $wuxDialog } from '../../wux/index.js' const app = getApp() Page({ /** * 页面的初始数据 */ data: { username: '', password: '', /** 激活的标签页 */ tabActive: 0, /**用户信息 */ userInfo: {}, regmobile_label: '手机号', regpassword_label: '密码', regpassword2_label: '确认密码', username_label: '用户名', password_label: '密码', /** app已存储用户信息 */ hasUserInfo: false, /** 获取用户信息接口可用? */ canIUse: wx.canIUse('button.open-type.getUserInfo'), buttonClicked:false }, /** * 生命周期函数--监听页面加载 */ onLoad: function(options) { //注册之前删除自动登录记录,注册成功之后需要进行自动登录,因为注册时绑定了微信unionId,可以自动登录成功.如果不删除自动登录记录,自动登录不会进行 wx.removeStorageSync('login_result') wx.getSetting({ complete: (res) => { console.log('getsetting',res) if (!res.authSetting['scope.userInfo']) { wx.removeStorageSync('welcome') wx.removeStorageSync('hasAuthorize') wx.redirectTo({ url: '/pages/welcome/index', }) } else { wx.getUserInfo({ complete: (res) => { console.log(res) this.setData({ 'userInfo': res.userInfo }) }, }) } }, }) }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady: function() { }, /** * 生命周期函数--监听页面显示 */ onShow: function() { }, /** * 生命周期函数--监听页面隐藏 */ onHide: function() { }, /** * 生命周期函数--监听页面卸载 */ onUnload: function() { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function() { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function() { }, /** * 用户点击右上角分享 */ onShareAppMessage: function() { }, onChange(event) { // event.detail 为当前输入的值 let dataKey = event.currentTarget.dataset.name this.setData({ [dataKey]: event.detail }) }, login() { let that = this wx.login({ success: function(res) { let param = { username: null, password: null } let ps = { ...param, ...that.data } Object.keys(param).forEach( n => { if (!ps[n]) { that.setData({ [n + '_error']: true, [n + '_error_message']: that.data[n + '_label'] + '必填' }) } } ) let flag = true Object.keys(that.data).forEach(n => { if (n.indexOf('_error') > -1 && that.data[n]) { flag = false } }) if (!flag) { return false } let params = { username: that.data.username, password: that.data.password, uuid: app.globalData.uuid, code: res.code, mini_program_type: app.globalData.miniProgramType } API_Passport.bindUser(params).then( response => { if (response.result === 'bind_success') { Common.autoLogin().then(res => { $wuxToptips().success({ hidden: false, text: '绑定成功!', duration: 2000, success: () => { wx.switchTab({ url: '/pages/store/index', }) } }) }) } } ).catch(error => { $wuxToptips().error({ icon: 'cancel', hidden: false, text: error.message, duration: 2000 }) }) } }) }, /** 输入框失去焦点 */ onBlur(e) { const that = this let dataKey = e.currentTarget.dataset.name const { rules, label } = e.currentTarget.dataset const { value } = e.detail if (rules) { that.setData({ [dataKey + '_error']: false, [dataKey + '_error_message']: '' }) try { rules.split(',').forEach(n => { if (n === 'required' && !value) { that.setData({ [dataKey + '_error']: true, [dataKey + '_error_message']: label + '必填' }) throw new Error() } if (n !== 'required' && !Reg[n].test(value)) { that.setData({ [dataKey + '_error']: true, [dataKey + '_error_message']: label + '格式不正确' }) } }) } catch (e) { } } }, /** tab标签切换 */ tabChange(event) { }, /** 获取用户信息 */ getUserInfo(e) { const { errMsg } = e.detail if (errMsg === 'getUserInfo:ok') { app.globalData.userInfo = e.detail.userInfo wx.setStorageSync('userInfo', e.detail.userInfo) this.setData({ userInfo: e.detail.userInfo, hasUserInfo: true }) } }, /** 注册新用户 */ register(e) { const that=this let param = { regmobile: null, regpassword: null, regpassword2: null } let ps = { ...param, ...this.data } Object.keys(param).forEach( n => { if (!ps[n]) { this.setData({ [n + '_error']: true, [n + '_error_message']: this.data[n + '_label'] + '必填' }) } } ) let flag = true Object.keys(this.data).forEach(n => { if (n.indexOf('_error') > -1 && this.data[n]) { flag = false } }) if (this.data.regpassword2 !== this.data.regpassword) { flag = false this.setData({ regpassword2_error: true, regpassword2_error_message: '两次密码不一致' }) } if (!flag) { return false } that.setData({ buttonClicked:true }) const para = { nick_name: this.data.userInfo.nickName, face: this.data.userInfo.avatarUrl, sex: this.data.userInfo.gender, mobile: this.data.regmobile, password: this.data.regpassword } API_Passport.registerBind(app.globalData.uuid, para).then(response => { if (response.result === 'bind_success') { let now = Math.round(new Date() / 1000) if (response.access_token) { //保存登录信息 const {refresh_token_timeout,access_token_timeout}=response app.globalData.authorInfo={...response,'refresh_token_timeout':now+refresh_token_timeout-1000,'access_token_timeout':now+access_token_timeout-1000} //登录信息写入本地存储 wx.setStorageSync('authorInfo', app.globalData.authorInfo) } $wuxToptips().success({ hidden: false, text: '注册成功!', duration: 2000, success: () => { that.setData({ buttonClicked:false }) //注册成功自动登录,在该函数中统一实现跳转逻辑 Common.toAutoLogin() } }) } }).catch(error=>{ $wuxToptips().error({ hidden: false, text: error.message, duration: 2000, success:()=>{ that.setData({ buttonClicked:false }) } }) }) } })