// pages/my/my.js import itt from '../../utils/util' import { ittLogin, bindMobile, bindBaseInfo } from '../../api/my' import { getAccountNmber } from '../../api/appointment' import { userUploadHeadImg } from '../../api/upload' const app = getApp() Page({ /** * 页面的初始数据 */ data: { overshow: false, pageLogin: false, isRelogin: false, // 是否为设置退出登录重新登录 showPhoneAuthBtn: true, showAvaNick: false, isNeedPhoneBtn: false, // 是否需要显示手机号授权 isNeedHeadImgBtn: false,// 是否需要显示用户头像 昵称 授权 isRegister: false, avatarUrl: '', confirmAvatarUrl: '', confirmNickname: '', nickname: '', phone: '', count: 0, gridList: [ { id: 0, title: '我的地址', url: '/pages/address/address?form=my&back=0', imgUrl: '../../imaes/dizhi@2x.png' }, { id: 1, title: '我的档案', url: '/pages/myFile/myFile?form=my', imgUrl: '../../imaes/dangan@2x.png' }, { id: 2, title: '我的预约', url: '/pages/myAppointment/myAppointment?form=my', imgUrl: '../../imaes/yuyue@2x.png' }, { id: 7, title: '实体卡兑换', url: '/pages/exchange/exchange?form=my', imgUrl: '../../imaes/chongzhi.png' }, { id: 3, title: '我的记录', url: '/pages/myRecord/myRecord?form=my', imgUrl: '../../imaes/jilu@2x.png' }, { id: 4, title: '我的报告', url: '/pages/report/report?form=my', imgUrl: '../../imaes/baogao2@2x.png' }, { id: 5, title: '开通区域', url: '/pages/openArea/openArea?form=my', imgUrl: '../../imaes/quyu@2x.png' }, { id: 6, title: '设置', url: '/pages/set/set?form=my', imgUrl: '../../imaes/shezhi@2x.png' }, /* { id: 7, title: '测试页面', url: '/pages/aatest/test', imgUrl: '../../imaes/shezhi@2x.png' } */ ] }, /** * 生命周期函数--监听页面加载 * 调用login接口,判断是否需要绑定手机号和头像昵称 */ onLoad(options) { app.globalData.selectedInex = 2 this.userLogin() }, // 获取用户头像和昵称权限 该api将于2022年10月25号之后失效 getUserinfo () { var that = this wx.getUserProfile({ desc: '用于获取用户权益' }).then(res => { const _avatarUrl = res.userInfo.avatarUrl const _nickName = res.userInfo.nickName that.setData({ avatarUrl: _avatarUrl, nickname: _nickName }) that.handleConfirmNickname() }).catch(e => { wx.showToast({ title: '获取授权失败', icon: 'error' }) }) }, // 退出登录后重新登录 handleReLogin () { this.userLogin() }, // 通过 code 获取 token userLogin () { var that = this wx.login({ success (loginCode) { if (loginCode.code) { var data = { code: loginCode.code } wx.showLoading({ title: '登录中...', mask: true }) ittLogin(data).then(res => { wx.hideLoading() app.globalData.accessToken = res.data.accessToken app.globalData.userInfo.headImg= res.data.headImg app.globalData.userInfo.userName = res.data.userName app.globalData.userInfo.phoneNumber = res.data.phoneNumber const resIsNeedPhone = res.data.isNeedPhone const resHeadImg = res.data.headImg that.getAccountNmberFn() if (!resIsNeedPhone && resHeadImg !== '') { app.globalData.userInfo.login = true that.setData({ isNeedHeadImgBtn: true, confirmAvatarUrl: app.globalData.userInfo.headImg, confirmNickname: app.globalData.userInfo.userName, }) } else { if (resIsNeedPhone) { // true 代表需要手机号授权 that.setData({ isNeedPhoneBtn: true }) } else { if (resHeadImg == '') { // 需要授权头像 that.setData({ overshow: true, phone: res.data, isNeedPhoneBtn: false, isNeedHeadImgBtn: true }) } else { that.setData({ isNeedHeadImgBtn: true, confirmAvatarUrl: app.globalData.userInfo.headImg, confirmNickname: app.globalData.userInfo.userName, }) } } } }).catch(e => { wx.hideLoading() }) } } }) }, // 获取用户剩余检测机会 getAccountNmberFn () { var that = this getAccountNmber({}).then(res => { that.setData({ count: res.data }) }) }, // 用户本地选择图片头像 onChooseAvatar(e) { const { avatarUrl } = e.detail this.uploadImg(avatarUrl) }, // 上传用户头像文件到后台 uploadImg (avatarUrl) { var that = this var data = { filePath: avatarUrl } wx.showLoading({ title: '上传中...', mask: true }) userUploadHeadImg(data).then(res => { wx.hideLoading() var response = JSON.parse(res) if (response.code != '000') { wx.showToast({ title: '头像上传失败', icon: 'error' }) } else { that.setData({ avatarUrl: response.data }) } }).catch(e => { wx.hideLoading() wx.showModal({ content: e, confirmColor: '#333', showCancel: false }) }) }, // 拉起手机号授权弹窗 getPhoneNumber (e) { var that = this var phonePermission = e.detail.errMsg.split(':')[1] if (phonePermission == 'ok') { var phoneCode = e.detail.code that.bindMobileFn(phoneCode) } else { wx.showToast({ title: '手机号授权失败', icon: 'error' }) } }, // 绑定手机号 bindMobileFn (phoneCode) { var that = this var data = { mobileCode: phoneCode } wx.showLoading({ title: '加载中...', mask: true }) bindMobile(data).then(res => { wx.hideLoading() app.globalData.userInfo.phoneNumber = res.data if (app.globalData.userInfo.headImg == '') { // 需要授权头像 that.setData({ overshow: true, phone: res.data, isNeedPhoneBtn: false, isNeedHeadImgBtn: true }) } else { that.setData({ isNeedHeadImgBtn: true, confirmAvatarUrl: app.globalData.userInfo.headImg, confirmNickname: app.globalData.userInfo.userName, }) app.globalData.userInfo.login = true } }).catch(e => { wx.hideLoading() wx.showModal({ content: e, confirmColor: '#333', showCancel: false }) }) }, // 提交用户头像和昵称到后台 handleConfirmNickname () { var that = this if (that.data.avatarUrl == '') { wx.showToast({ title: '请选择头像', icon: 'error' }) return } if (that.data.nickname == '') { wx.showToast({ title: '请输入昵称', icon: 'error' }) return } var data = { avatarUrl: that.data.avatarUrl, nickName: that.data.nickname } wx.showLoading({ title: '加载中...', mask: true }) bindBaseInfo(data).then(res => { wx.hideLoading() app.globalData.userInfo.login = true that.setData({ pageLogin: true, overshow: false, confirmAvatarUrl: that.data.avatarUrl, confirmNickname: that.data.nickname }) app.globalData.userInfo.headImg = that.data.confirmAvatarUrl app.globalData.userInfo.userName = that.data.confirmNickname }).catch(e => { wx.hideLoading() wx.showModal({ content: e, confirmColor: '#333', showCancel: false }) }) }, /** * 生命周期函数--监听页面显示 */ onShow() { this.setData({ pageLogin: app.globalData.userInfo.login }) }, // 路由跳转 handleNavTo (e) { var url = e.currentTarget.dataset.url this.authNavTo(url) }, handleAppoint () { this.authNavTo('/pages/appointment/appointment') }, /** * 路由拦截 * **/ authNavTo (url) { if (itt.loginAuth()) { if (url.indexOf('/report') !== -1) { app.globalData.selectedInex = 1 wx.switchTab({ url: url, }) } else { wx.navigateTo({ url: url, }) } } else { itt.errorToast('请先授权登录') } } })