123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238 |
- // pages/my/my.js
- import itt from '../../utils/util'
- import { ittLogin, bindMobile, bindBaseInfo } from '../../api/my'
- const app = getApp()
- Page({
- /**
- * 页面的初始数据
- */
- data: {
- overshow: false,
- pageLogin: false,
- showPhoneAuthBtn: true,
- showAvaNick: false,
- isNeedPhone: 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: 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'
- }
- ]
- },
- /**
- * 生命周期函数--监听页面加载
- * 调用login接口,判断是否需要绑定手机号和头像昵称
- */
- onLoad(options) {
- this.userLogin()
- },
- // 通过 code 获取 token
- userLogin () {
- var that = this
- wx.login({
- success (res) {
- if (res.code) {
- var data = {
- code: res.code
- }
- ittLogin(data).then(loginRes => {
- app.globalData.accessToken = loginRes.data.accessToken
- 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
- })
- }
- },
- // 拉起手机号授权弹窗
- getPhoneNumber (e) {
- var that = this
- var phoneCode = e.detail.code
- wx.login({
- success (res) {
- if (res.code) {
- let data = {
- code: res.code
- }
- 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
- })
- },
- /**
- * 生命周期函数--监听页面显示
- */
- onShow() {
- this.setData({
- pageLogin: app.globalData.userInfo.login
- })
- },
- // 路由跳转
- handleNavTo (e) {
- var url = e.currentTarget.dataset.url
- this.authNavTo(url)
- },
- /**
- * 路由拦截
- * **/
- authNavTo (url) {
- if (itt.loginAuth()) {
- wx.navigateTo({
- url: url,
- })
- } else {
- itt.errorToast('请先授权登录')
- }
- }
- })
|