let app = getApp(); import { loginRq, registerPhone } from "../../api/login/login.js" Page({ /** * 页面的初始数据 */ data: { IMG_NAME: app.IMG_NAME, getUserDataType: 'getPhoneNumber', openid: null, }, /** * 生命周期函数--监听页面加载 */ onLoad: function (e) { // 清空所有缓存 wx.clearStorageSync() // 自动登录获取openid this.autoLogin(); }, // 自动登录获取openid autoLogin() { let _this = this; wx.login({ success(wxRes) { loginRq({ "jsCode": wxRes.code }).then(res => { console.log('loginRq', res); _this.setData({ openid: res.openid }) // 判断用户是否注册 if (res.token) { // 跳转首页 _this.jumpIndex(res.openid, res.user, res.token.token) } }) } }) }, // 获取手机号 getPhoneNumber(e) { let _this = this; console.log('getPhoneNumber', e) registerPhone({ "code": e.detail.code, "openid": _this.data.openid }).then(res => { console.log('registerPhone', res); // 跳转首页 _this.jumpIndex(res.openid, res.user, res.token.token) }) }, // 跳转首页 jumpIndex(openid, user, token) { // 清空所有缓存 wx.clearStorageSync() // 存储用户信息 wx.setStorageSync('openid', openid) wx.setStorageSync('user', user) wx.setStorageSync('userId', user.id) wx.setStorageSync('token', token) wx.setStorageSync('parkId', user.parkId) wx.setStorageSync('parkName', user.parkName) // 跳转首页 wx.navigateBack() }, /** * 授权登录 */ authorLogin: function (e) { console.log('getuserinfo', e.detail.rawData); let _this = this; if (e.detail.errMsg !== 'getUserInfo:ok') { return false; } wx.showLoading({ title: "正在登录", mask: true }); // 执行微信登录 wx.login({ success: function (res) { //发送请求 wx.request({ header: { 'content-type': 'application/json' }, method: 'POST', url: app.DOMAIN_NAME + '/social_user_login/login', data: { code: res.code, rawData: e.detail.rawData, encryptedData: e.detail.encryptedData, ivStr: e.detail.iv, signature: e.detail.signature }, success(res) { console.log(res); if (res.data.code != 0) { wx.showModal({ confirmText: '好的', content: res.data.errmsg || '服务器开小差去了,请重试', showCancel: false }); } else if (res.data.code == 0) { wx.setStorageSync('userId', res.data.userId) wx.setStorageSync('token', res.data.token) wx.hideLoading() wx.reLaunch({ url: '/pages/index/index', }) } }, fail(ret) {} }) } }); }, /** * 授权成功 跳转回原页面 */ navigateBack: function () { wx.navigateBack(); // let currentPage = wx.getStorageSync('currentPage'); // wx.redirectTo({ // url: '/' + currentPage.route + '?' + App.urlEncode(currentPage.options) // }); }, })