123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361 |
- // 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('请先授权登录')
- }
- }
- })
|