市北人才赋能平台 --小程序端
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.

154 lines
3.9 KiB

6 years ago
// pages/user/index.js
import dayjs from '../../utils/dayjs/index.js'
import relativeTime from '../../utils/dayjs/relativeTime.js'
dayjs.extend(relativeTime);
6 years ago
import { store } from '../../utils/store.js'
import { UserModel } from '../../models/user.js'
let userModel = new UserModel()
6 years ago
import { AuthModel } from '../../models/auth.js'
let authModel = new AuthModel()
6 years ago
Page({
/**
* 页面的初始数据
*/
data: {
6 years ago
messageTotal: Number,
canIUse: wx.canIUse('button.open-type.getUserInfo'),
6 years ago
authType:0,
reAuth:false,
isAuth:false,
nickName:'',
avatarUrl:'',
6 years ago
userInfo:{
type:Object,
value:{}
6 years ago
}
6 years ago
},
6 years ago
6 years ago
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
6 years ago
},
onShow: function () {
6 years ago
this.getUserInfo()
},
isAuthUserInfo () {
6 years ago
let that = this
wx.showLoading()
console.log('未授权')
return new Promise(resolve => {
6 years ago
wx.getSetting({
success (res) {
6 years ago
if (res.authSetting['scope.userInfo']) {
// 已经授权,可以直接调用 getUserInfo 获取头像昵称
wx.getUserInfo({
success: function (res) {
let nickName = res.userInfo.nickName
let avatarUrl = res.userInfo.avatarUrl
6 years ago
store.saveUserInfo({
nickName: nickName,
avatarUrl: avatarUrl,
phone: ''
6 years ago
})
if (nickName && avatarUrl) {
6 years ago
that.setData({
isAuth: false
}, () => {
6 years ago
wx.hideLoading()
})
}
6 years ago
that.getUserInfo()
6 years ago
}
})
}
}
})
})
},
bindGetUserInfo (e) {
6 years ago
console.log(e.detail.userInfo)
if (e.detail.userInfo){
let avatarUrl = e.detail.userInfo.avatarUrl
let nickName = e.detail.userInfo.nickName
this.updateUserInfo(avatarUrl,nickName)
}
6 years ago
},
hasBindUserInfo () {
6 years ago
return store.hasBindUserInfo();
},
// 将用户信息发送服务器
updateUserInfo (avatarUrl, nickName) {
userModel.updateUserInfo(avatarUrl, nickName, res => {
if(res.code === 200){
this.isAuthUserInfo()
}
6 years ago
})
},
getUserInfo () {
userModel.getUserInfo(res => {
6 years ago
let nickName = res.result.nickName
let avatarUrl = res.result.avatarUrl
6 years ago
6 years ago
if (nickName && avatarUrl){
6 years ago
console.log('已授权')
6 years ago
this.setData({
6 years ago
userInfo: res.result,
6 years ago
avatarUrl: res.result.avatarUrl,
nickName: res.result.nickName,
reAuth: true
},()=>{
if (store.hasPhone()) {
console.log('已经绑定手机号码')
this.getMsgStatus()
} else {
console.log('未绑定手机号码')
wx.redirectTo({
url: '/pages/register/index',
})
}
6 years ago
})
store.saveUserInfo({
nickName: res.result.nickName,
avatarUrl: res.result.avatarUrl,
phone: res.result.phone || ''
6 years ago
})
} else {
console.log('未授权')
this.setData({
isAuth: true
6 years ago
})
}
6 years ago
})
},
getMsgStatus () {
userModel.getMyMessageTotal(res => {
console.log(res.result.total)
this.setData({
messageTotal: Number(res.result.total)
})
})
6 years ago
},
onTapItem (e) {
const { type } = e.currentTarget.dataset
console.log(type)
6 years ago
if (type === 'myInfo'){
// wx.navigateTo({
// url: `/pages/user/${type}/index?userIcon=${this.data.userInfo.avatarUrl}&userName=${this.data.userInfo.nickName}&userPhone=${this.data.userInfo.phone || ''}&company=${this.data.userInfo.company || ''}&position=${this.data.userInfo.position || ''}`,
// })
} else {
wx.navigateTo({
url: `/pages/user/${type}/index`,
})
6 years ago
}
6 years ago
}
})