|
@@ -1,6 +1,6 @@
|
|
|
// pages/my/my.js
|
|
|
import itt from '../../utils/util'
|
|
|
-import { ittLogin } from '../../api/app'
|
|
|
+import { ittLogin, bindMobile, bindBaseInfo } from '../../api/my'
|
|
|
const app = getApp()
|
|
|
Page({
|
|
|
|
|
@@ -8,9 +8,17 @@ Page({
|
|
|
* 页面的初始数据
|
|
|
*/
|
|
|
data: {
|
|
|
+ overshow: false,
|
|
|
pageLogin: false,
|
|
|
- name: '点击授权登录',
|
|
|
+ showPhoneAuthBtn: true,
|
|
|
+ showAvaNick: false,
|
|
|
+ isNeedPhone: false,
|
|
|
+ isRegister: false,
|
|
|
avatarUrl: '',
|
|
|
+ confirmAvatarUrl: '',
|
|
|
+ confirmNickname: '点击获取头像昵称',
|
|
|
+ nickname: '',
|
|
|
+ phone: '',
|
|
|
count: 0,
|
|
|
gridList: [
|
|
|
{
|
|
@@ -60,49 +68,148 @@ Page({
|
|
|
|
|
|
/**
|
|
|
* 生命周期函数--监听页面加载
|
|
|
+ * 调用login接口,判断是否需要绑定手机号和头像昵称
|
|
|
*/
|
|
|
onLoad(options) {
|
|
|
-
|
|
|
+ this.userLogin()
|
|
|
},
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取用户信息
|
|
|
- */
|
|
|
- getUserProfile() {
|
|
|
- if (this.data.avatarUrl == '') {
|
|
|
- var that = this;
|
|
|
- wx.getUserProfile({
|
|
|
- desc:"授权信息",
|
|
|
- success:function(res){
|
|
|
- if(res.userInfo){
|
|
|
- that.loginFn(res.userInfo)
|
|
|
+ // 通过 code 获取 token
|
|
|
+ userLogin () {
|
|
|
+ var that = this
|
|
|
+ wx.login({
|
|
|
+ success (res) {
|
|
|
+ if (res.code) {
|
|
|
+ var data = {
|
|
|
+ code: res.code
|
|
|
}
|
|
|
- },
|
|
|
+ ittLogin(data).then(loginRes => {
|
|
|
+ console.log(loginRes, 'loginRes');
|
|
|
+ that.setData({
|
|
|
+ isNeedPhone: loginRes.data.isNeedPhone,
|
|
|
+ isRegister: loginRes.data.isRegister
|
|
|
+ })
|
|
|
+ if (loginRes.data.isNeedPhone) {
|
|
|
+ that.setData({
|
|
|
+ showPhoneAuthBtn: true,
|
|
|
+ showAvaNick: false
|
|
|
+ })
|
|
|
+ } else {
|
|
|
+ app.globalData.userInfo.phoneNumber = loginRes.data.phoneNumber
|
|
|
+ if (loginRes.data.isRegister) {
|
|
|
+ that.setData({
|
|
|
+ showPhoneAuthBtn: false,
|
|
|
+ showAvaNick: true
|
|
|
+ })
|
|
|
+ } else {
|
|
|
+ // 全部授权
|
|
|
+ that.setData({
|
|
|
+ showPhoneAuthBtn: false,
|
|
|
+ showAvaNick: true,
|
|
|
+ pageLogin: true,
|
|
|
+ confirmAvatarUrl: loginRes.data.headImg,
|
|
|
+ confirmNickname: loginRes.data.userName.length > 4 ? loginRes.data.userName.substring(0,4) : loginRes.data.userName
|
|
|
+ })
|
|
|
+ app.globalData.userInfo.login = true
|
|
|
+ app.globalData.userInfo.phoneNumber = loginRes.data.phoneNumber
|
|
|
+ app.globalData.userInfo.headImg = loginRes.data.headImg
|
|
|
+ app.globalData.userInfo.userName = loginRes.data.userName
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }).catch(e => {
|
|
|
+ console.log(e, 'ittlogin');
|
|
|
+ })
|
|
|
+ }
|
|
|
+ }
|
|
|
+ })
|
|
|
+ },
|
|
|
+ onChooseAvatar(e) {
|
|
|
+ const { avatarUrl } = e.detail
|
|
|
+ this.setData({
|
|
|
+ avatarUrl,
|
|
|
+ })
|
|
|
+ },
|
|
|
+ // 拉起用户头像和昵称
|
|
|
+ getNickName () {
|
|
|
+ if (!this.data.pageLogin) {
|
|
|
+ this.setData({
|
|
|
+ overshow: true
|
|
|
})
|
|
|
}
|
|
|
},
|
|
|
- loginFn (userInfo) {
|
|
|
+ // 拉起手机号授权弹窗
|
|
|
+ getPhoneNumber (e) {
|
|
|
var that = this
|
|
|
+ var phoneCode = e.detail.code
|
|
|
wx.login({
|
|
|
success (res) {
|
|
|
if (res.code) {
|
|
|
- console.log(res.code, 'code');
|
|
|
let data = {
|
|
|
code: res.code
|
|
|
}
|
|
|
- ittLogin(data).then(res => {
|
|
|
- console.log(res,'then login');
|
|
|
- app.globalData.userInfo.login = true
|
|
|
- that.setData({
|
|
|
- name: userInfo.nickName,
|
|
|
- avatarUrl: userInfo.avatarUrl,
|
|
|
- pageLogin: true
|
|
|
- })
|
|
|
+ ittLogin(data).then(ittLoginRes => {
|
|
|
+ that.bindMobileFn(phoneCode, ittLoginRes)
|
|
|
+ }).catch(e => {
|
|
|
+ console.log(e, 'ittlogin');
|
|
|
})
|
|
|
}
|
|
|
}
|
|
|
})
|
|
|
},
|
|
|
+ // 绑定手机号
|
|
|
+ // isRegister = true 代表第一次进入显示获取头像
|
|
|
+ bindMobileFn (phoneCode, ittLoginRes) {
|
|
|
+ var that = this
|
|
|
+ var data = {
|
|
|
+ mobileCode: phoneCode
|
|
|
+ }
|
|
|
+ bindMobile(data).then(res => {
|
|
|
+ app.globalData.userInfo.phoneNumber = res.data
|
|
|
+ if (that.data.isRegister) {
|
|
|
+ that.setData({
|
|
|
+ overshow: true,
|
|
|
+ phone: res.data,
|
|
|
+ showPhoneAuthBtn: false,
|
|
|
+ showAvaNick: true
|
|
|
+ })
|
|
|
+ } else {
|
|
|
+ that.setData({
|
|
|
+ phone: res.data,
|
|
|
+ showPhoneAuthBtn: false,
|
|
|
+ showAvaNick: true,
|
|
|
+ pageLogin: true,
|
|
|
+ avatarUrl: ittLoginRes.data.headImg,
|
|
|
+ nickname: ittLoginRes.data.userName.length > 4 ? ittLoginRes.data.userName.substring(0,4) : ittLoginRes.data.userName
|
|
|
+ })
|
|
|
+ app.globalData.userInfo.login = true
|
|
|
+ app.globalData.userInfo.headImg = ittLoginRes.data.headImg
|
|
|
+ app.globalData.userInfo.userName = ittLoginRes.data.userName
|
|
|
+ }
|
|
|
+ })
|
|
|
+ },
|
|
|
+ bindNickNameInput (e) {
|
|
|
+ this.setData({
|
|
|
+ nickname: e.detail.value
|
|
|
+ })
|
|
|
+ },
|
|
|
+ // 绑定用户头像和昵称
|
|
|
+ handleConfirmNickname () {
|
|
|
+ var that = this
|
|
|
+ var data = {
|
|
|
+ avatarUrl: that.data.avatarUrl,
|
|
|
+ nickName: that.data.nickname
|
|
|
+ }
|
|
|
+ bindBaseInfo(data).then(res => {
|
|
|
+ app.globalData.userInfo.login = true
|
|
|
+ that.setData({
|
|
|
+ pageLogin: true,
|
|
|
+ overshow: false,
|
|
|
+ confirmAvatarUrl: that.data.avatarUrl,
|
|
|
+ confirmNickname: that.data.nickname.length > 4 ? that.data.nickname.substring(0,4) : that.data.nickname
|
|
|
+ })
|
|
|
+ app.globalData.userInfo.headImg = that.data.confirmAvatarUrl
|
|
|
+ app.globalData.userInfo.userName = that.data.confirmNickname
|
|
|
+ })
|
|
|
+ },
|
|
|
/**
|
|
|
* 生命周期函数--监听页面显示
|
|
|
*/
|
|
@@ -114,10 +221,7 @@ Page({
|
|
|
// 路由跳转
|
|
|
handleNavTo (e) {
|
|
|
var url = e.currentTarget.dataset.url
|
|
|
- wx.navigateTo({
|
|
|
- url: url,
|
|
|
- })
|
|
|
- // this.authNavTo(url)
|
|
|
+ this.authNavTo(url)
|
|
|
},
|
|
|
/**
|
|
|
* 路由拦截
|