examtest_mp/pages/login/login.js

102 lines
2.5 KiB
JavaScript

//index.js
//获取应用实例
const app = getApp()
const api = require("../../utils/request.js");
Page({
data: {
userInfo: {},
if_checked:false,
hasUserInfo: false,
canIUse: wx.canIUse('button.open-type.getUserInfo'),
countDown:40,
form:{
phone:0,
code:0,
nickname:0,
avatar:0
}
},
onLoad: function () {
wx.hideHomeButton()
if (app.globalData.userInfo) {
this.setData({
userInfo: app.globalData.userInfo,
hasUserInfo: true
})
} else if (this.data.canIUse) {
// 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
// 所以此处加入 callback 以防止这种情况
app.userInfoReadyCallback = res => {
this.setData({
userInfo: res.userInfo,
hasUserInfo: true
})
}
} else {
// 在没有 open-type=getUserInfo 版本的兼容处理
wx.getUserInfo({
success: res => {
app.globalData.userInfo = res.userInfo
this.setData({
userInfo: res.userInfo,
hasUserInfo: true
})
}
})
}
},
goMain: function (){
wx.reLaunch({
url: '/pages/main/main',
})
},
denglu: function (data) {
api.request('/crm/consumer/register/', 'POST', data).then(res => {
getApp().onLaunch()
wx.switchTab({
url: '/pages/main/main',
})
})
},
checkedChange(e){
let if_checked = !this.data.if_checked;
this.setData({
if_checked:if_checked
})
},
getPhoneNumber: function(e){
if(e.detail.encryptedData){
let form = {
encryptedData: e.detail.encryptedData,
iv: e.detail.iv,
sessionKey:getApp().globalData.session_key
}
api.request('/crm/consumer/wxphoneregister/', 'POST', form).then(res => {
getApp().onLaunch()
wx.login({
success: res => {
api.request('/crm/consumer/mplogin/','POST', {code:res.code}).then(res=>{
if(res.code==200){
wx.setStorageSync('token', res.data.token)
wx.setStorageSync('session_key', res.data.session_key)
wx.setStorageSync('userinfo', res.data.userinfo)
wx.switchTab({
url: '/pages/main/main',
})
}
})
}
})
})
}
},
clickdenglu(){
wx.showToast({
icon: 'none',
title: '请先阅读并同意协议',
})
},
})