2024-02-21 17:43:11 +08:00
|
|
|
let app = getApp();
|
2024-02-26 15:34:59 +08:00
|
|
|
import {
|
|
|
|
loginRq,
|
|
|
|
registerPhone
|
|
|
|
} from "../../api/login/login.js"
|
|
|
|
|
2024-02-21 17:43:11 +08:00
|
|
|
Page({
|
2024-02-26 15:34:59 +08:00
|
|
|
/**
|
|
|
|
* 页面的初始数据
|
|
|
|
*/
|
|
|
|
data: {
|
|
|
|
IMG_NAME: app.IMG_NAME,
|
|
|
|
getUserDataType: 'getPhoneNumber',
|
|
|
|
openid: null,
|
|
|
|
},
|
2024-02-21 17:43:11 +08:00
|
|
|
|
2024-02-26 15:34:59 +08:00
|
|
|
/**
|
|
|
|
* 生命周期函数--监听页面加载
|
|
|
|
*/
|
|
|
|
onLoad: function (e) {
|
2024-02-26 15:48:35 +08:00
|
|
|
// 清空所有缓存
|
|
|
|
wx.clearStorageSync()
|
|
|
|
// 自动登录获取openid
|
2024-02-26 15:34:59 +08:00
|
|
|
this.autoLogin();
|
|
|
|
},
|
2024-02-21 17:43:11 +08:00
|
|
|
|
2024-02-26 15:34:59 +08:00
|
|
|
// 自动登录获取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) {
|
|
|
|
// 跳转首页
|
2024-03-04 17:10:32 +08:00
|
|
|
_this.jumpIndex(res.openid, res.user, res.token.token)
|
2024-02-26 15:34:59 +08:00
|
|
|
}
|
|
|
|
})
|
2024-02-21 17:43:11 +08:00
|
|
|
}
|
|
|
|
})
|
2024-02-26 15:34:59 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
// 获取手机号
|
|
|
|
getPhoneNumber(e) {
|
|
|
|
let _this = this;
|
|
|
|
console.log('getPhoneNumber', e)
|
|
|
|
registerPhone({
|
|
|
|
"code": e.detail.code,
|
|
|
|
"openid": _this.data.openid
|
|
|
|
}).then(res => {
|
|
|
|
console.log('registerPhone', res);
|
|
|
|
// 跳转首页
|
2024-03-04 17:10:32 +08:00
|
|
|
_this.jumpIndex(res.openid, res.user, res.token.token)
|
2024-02-26 15:34:59 +08:00
|
|
|
})
|
|
|
|
},
|
|
|
|
// 跳转首页
|
2024-03-04 17:10:32 +08:00
|
|
|
jumpIndex(openid, user, token) {
|
|
|
|
// 清空所有缓存
|
|
|
|
wx.clearStorageSync()
|
2024-02-26 15:34:59 +08:00
|
|
|
// 存储用户信息
|
|
|
|
wx.setStorageSync('openid', openid)
|
2024-03-04 17:10:32 +08:00
|
|
|
wx.setStorageSync('user', user)
|
|
|
|
wx.setStorageSync('userId', user.id)
|
2024-02-26 15:34:59 +08:00
|
|
|
wx.setStorageSync('token', token)
|
2024-04-02 17:51:41 +08:00
|
|
|
wx.setStorageSync('parkId', user.parkId)
|
|
|
|
wx.setStorageSync('parkName', user.parkName)
|
2024-02-26 15:34:59 +08:00
|
|
|
// 跳转首页
|
|
|
|
wx.reLaunch({
|
|
|
|
url: '/pages/index/index',
|
|
|
|
})
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 授权登录
|
|
|
|
*/
|
|
|
|
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) {}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
2024-02-21 17:43:11 +08:00
|
|
|
|
2024-02-26 15:34:59 +08:00
|
|
|
/**
|
|
|
|
* 授权成功 跳转回原页面
|
|
|
|
*/
|
|
|
|
navigateBack: function () {
|
|
|
|
wx.navigateBack();
|
|
|
|
// let currentPage = wx.getStorageSync('currentPage');
|
|
|
|
// wx.redirectTo({
|
|
|
|
// url: '/' + currentPage.route + '?' + App.urlEncode(currentPage.options)
|
|
|
|
// });
|
|
|
|
},
|
2024-02-21 17:43:11 +08:00
|
|
|
|
|
|
|
})
|