You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
173 lines
4.6 KiB
173 lines
4.6 KiB
// pages/weChatAuth/index.js
|
|
import { store } from '../../utils/store.js'
|
|
import { UserModel } from '../../models/user.js'
|
|
let userModel = new UserModel()
|
|
import { AuthModel } from '../../models/auth.js'
|
|
let authModel = new AuthModel()
|
|
import { config } from '../../config.js'
|
|
const app = getApp()
|
|
Page({
|
|
|
|
/**
|
|
* 页面的初始数据
|
|
*/
|
|
data: {
|
|
titlePic:config.api_url + "/image/head.jpg",
|
|
canIUse: wx.canIUse('button.open-type.getUserInfo'),
|
|
canIUseGetUserProfile: false,
|
|
nickName: '',
|
|
avatarUrl: '',
|
|
userInfo: {
|
|
type: Object,
|
|
value: {}
|
|
},
|
|
type:1,
|
|
tabType:""
|
|
},
|
|
|
|
/**
|
|
* 生命周期函数--监听页面加载
|
|
*/
|
|
onLoad: function (options) {
|
|
console.log(wx.getUserProfile)
|
|
if (wx.getUserProfile) {
|
|
this.setData({
|
|
canIUseGetUserProfile: true
|
|
})
|
|
}
|
|
this.setData({
|
|
type:options.type,
|
|
tabType:options.tabType
|
|
})
|
|
},
|
|
getUserProfile(e) {
|
|
// 推荐使用wx.getUserProfile获取用户信息,开发者每次通过该接口获取用户个人信息均需用户确认
|
|
// 开发者妥善保管用户快速填写的头像昵称,避免重复弹窗
|
|
wx.getUserProfile({
|
|
desc: '用于完善会员资料', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写
|
|
success: (res) => {
|
|
console.log('用户信息--',res)
|
|
let avatarUrl =res.userInfo.avatarUrl
|
|
let nickName = res.userInfo.nickName
|
|
store.saveUserInfo({
|
|
nickName: nickName,
|
|
avatarUrl: avatarUrl,
|
|
phone: ''
|
|
})
|
|
this.sendUserInfo(res.userInfo)
|
|
}
|
|
})
|
|
},
|
|
/**
|
|
* 生命周期函数--监听页面显示
|
|
*/
|
|
onShow: function () {
|
|
|
|
},
|
|
|
|
// 授权登录
|
|
bindGetUserInfo(e) {
|
|
//console.log(e.detail.userInfo)
|
|
if (e.detail.userInfo) {
|
|
let avatarUrl = e.detail.userInfo.avatarUrl
|
|
let nickName = e.detail.userInfo.nickName
|
|
store.saveUserInfo({
|
|
nickName: nickName,
|
|
avatarUrl: avatarUrl,
|
|
phone: ''
|
|
})
|
|
this.sendUserInfo(e.detail.userInfo)
|
|
}
|
|
},
|
|
|
|
// 上传用户信息
|
|
sendUserInfo(weChatInfo) {
|
|
userModel.sendUserInfo(weChatInfo, res => {
|
|
this.getUserInfo()
|
|
})
|
|
},
|
|
|
|
// 获取用户信息
|
|
getUserInfo() {
|
|
userModel.getUserInfo(res => {
|
|
console.log(res)
|
|
let nickName = res.result.nickName
|
|
let avatarUrl = res.result.avatarUrl
|
|
|
|
if (nickName && avatarUrl) {
|
|
//console.log('已授权')
|
|
this.setData({
|
|
userInfo: res.result,
|
|
avatarUrl: res.result.avatarUrl,
|
|
nickName: res.result.nickName,
|
|
id:res.result.id,
|
|
reAuth: true,
|
|
isAuth: false
|
|
}, () => {
|
|
if (store.hasPhone()) {
|
|
console.log('已经绑定手机号码')
|
|
let tabType=this.data.tabType
|
|
console.log('tabType--',tabType)
|
|
if(tabType=='funeng'){
|
|
app.globalData.currentTab = "1"
|
|
wx.switchTab({
|
|
url: '/pages/topics/index'
|
|
})
|
|
}
|
|
if(tabType=='login'){
|
|
wx.switchTab({
|
|
url: '/pages/user/index'
|
|
})
|
|
}
|
|
} else {
|
|
console.log('未绑定手机号码')
|
|
wx.showModal({
|
|
title: '温馨提示',
|
|
content: '是否前往验证手机号码?',
|
|
success(res) {
|
|
if (res.confirm) {
|
|
wx.redirectTo({
|
|
// url: '/pages/register/index',
|
|
url: '/pages/weChatAuth/index?type=2',
|
|
})
|
|
} else if (res.cancel) {
|
|
wx.switchTab({
|
|
url: '/pages/home/index',
|
|
})
|
|
}
|
|
}
|
|
})
|
|
}
|
|
})
|
|
store.saveUserInfo({
|
|
nickName: res.result.nickName,
|
|
avatarUrl: res.result.avatarUrl,
|
|
phone: res.result.phone || '',
|
|
id:res.result.id,
|
|
})
|
|
} else {
|
|
//console.log('未授权')
|
|
this.setData({
|
|
isAuth: true
|
|
})
|
|
}
|
|
})
|
|
},
|
|
bindGetPhoneNumber(e){
|
|
// var user = UserModel;
|
|
if (e.detail.encryptedData){
|
|
console.log(e);
|
|
let data = {}
|
|
// data.nickName = this.data.nickName;
|
|
// data.avatarUrl = this.data.avatarUrl;
|
|
data.encryptedData = e.detail.encryptedData;
|
|
data.iv = e.detail.iv;
|
|
// data.session = app.globalData.session;
|
|
userModel.wxRegister(data,res => {
|
|
wx.reLaunch({
|
|
url: '/pages/home/index',
|
|
})
|
|
});
|
|
}
|
|
}
|
|
})
|