154 lines
3.2 KiB
TypeScript
154 lines
3.2 KiB
TypeScript
// pages/login/login.ts
|
|
const defaultAvatarUrl = '../../assets/avatar.png'
|
|
const api = require("../../api/index")
|
|
const app = getApp<IAppOption>()
|
|
Page({
|
|
|
|
/**
|
|
* 页面的初始数据
|
|
*/
|
|
data: {
|
|
avatarUrl:defaultAvatarUrl ,
|
|
nickName:''
|
|
},
|
|
|
|
/**
|
|
* 生命周期函数--监听页面加载
|
|
*/
|
|
onLoad() {
|
|
|
|
},
|
|
|
|
/**
|
|
* 生命周期函数--监听页面初次渲染完成
|
|
*/
|
|
onReady() {
|
|
|
|
},
|
|
|
|
/**
|
|
* 生命周期函数--监听页面显示
|
|
*/
|
|
onShow() {
|
|
|
|
},
|
|
|
|
/**
|
|
* 生命周期函数--监听页面隐藏
|
|
*/
|
|
onHide() {
|
|
|
|
},
|
|
|
|
/**
|
|
* 生命周期函数--监听页面卸载
|
|
*/
|
|
onUnload() {
|
|
|
|
},
|
|
|
|
/**
|
|
* 页面相关事件处理函数--监听用户下拉动作
|
|
*/
|
|
onPullDownRefresh() {
|
|
|
|
},
|
|
|
|
/**
|
|
* 页面上拉触底事件的处理函数
|
|
*/
|
|
onReachBottom() {
|
|
|
|
},
|
|
|
|
/**
|
|
* 用户点击右上角分享
|
|
*/
|
|
onShareAppMessage() {
|
|
|
|
},
|
|
onChooseAvatar(e:any) {
|
|
console.log(e);
|
|
const { avatarUrl } = e.detail
|
|
this.setData({
|
|
avatarUrl,
|
|
})
|
|
},
|
|
async toWXLogin() {
|
|
console.log(app.globalData.code)
|
|
wx.showLoading({
|
|
title: '加载中',
|
|
})
|
|
if(!this.data.nickName) {
|
|
wx.showToast({
|
|
title:'请填写昵称',
|
|
icon: 'error',
|
|
duration: 2000
|
|
})
|
|
return
|
|
}
|
|
const token_res = await api.getAccess_token({code:app.globalData.code,nickName:this.data.nickName,avatarUrl:this.data.avatarUrl});
|
|
if(token_res.code == 200){
|
|
// 设置缓存
|
|
wx.setStorageSync('token', token_res.data.access_token);//token
|
|
wx.setStorageSync('userId', token_res.data.userId);//userId
|
|
wx.setStorageSync('nickName', this.data.nickName);//nickName
|
|
wx.setStorageSync('avatarUrl', this.data.avatarUrl);//avatarUrl
|
|
//绑定微信
|
|
const login_res =await api.bindingWX({code:app.globalData.code});
|
|
if(login_res.code == 200){
|
|
wx.hideLoading()
|
|
wx.navigateBack({
|
|
delta: -1
|
|
})
|
|
}
|
|
}
|
|
else{
|
|
wx.hideLoading()
|
|
wx.showToast({
|
|
title:token_res.message,
|
|
icon: 'error',
|
|
duration: 2000
|
|
})
|
|
}
|
|
},
|
|
async getPhoneNumber (e:any) {//获取手机号
|
|
// var _this=this;
|
|
console.log(e)
|
|
wx.showLoading({
|
|
title: '加载中',
|
|
})
|
|
if(!this.data.nickName) {
|
|
wx.showToast({
|
|
title:'请填写昵称',
|
|
icon: 'error',
|
|
duration: 2000
|
|
})
|
|
return
|
|
}
|
|
const token_res = await api.getAccess_token({code:e.detail.code,nickName:this.data.nickName,avatarUrl:this.data.avatarUrl});
|
|
if(token_res.code == 200){
|
|
// 设置缓存
|
|
wx.setStorageSync('token', token_res.data.access_token);//token
|
|
wx.setStorageSync('userId', token_res.data.userId);//userId
|
|
wx.setStorageSync('nickName', this.data.nickName);//nickName
|
|
wx.setStorageSync('avatarUrl', this.data.avatarUrl);//avatarUrl
|
|
//绑定微信
|
|
const login_res =await api.toLogin();
|
|
if(login_res.code == 200){
|
|
wx.hideLoading()
|
|
wx.navigateBack({
|
|
delta: -1
|
|
})
|
|
}
|
|
}
|
|
else{
|
|
wx.hideLoading()
|
|
wx.showToast({
|
|
title:token_res.message,
|
|
icon: 'error',
|
|
duration: 2000
|
|
})
|
|
}
|
|
},
|
|
}) |