import { homePage, appGetConfigImg } from '../../api/index' import { createQRcode } from "../../api/document" import { userLogin, bindMobile, bindBaseInfo } from '../../api/my' import { appointmentList } from '../../api/appointment' const app = getApp() Page({ /** * 页面的初始数据 */ data: { useNumber: 0, // 剩余预约次数 documentVos: [], // 档案信息 cardNo: '', cardSecret: '', scanTimer: null, baseStr: 'data:image/jpg;base64,', QRCodeBase64: '', qrcodeDialog: false, qrUserName: '', phoneAuthShow: false, userAuthShow: false, avatarUrl: '', nickName: '', isRefreshPage: false, showTopBar: false, topBarObj: null, homepageImg: '' }, /** * 生命周期函数--监听页面显示 */ onShow() { app.globalData.selectedInex = 0 wx.setStorageSync('selectedInex', 0) this.handleToken() }, handleToken () { const accessToken = wx.getStorageSync('accessToken') if (accessToken) { this.storageTokenLogin() } else { this.handleWXCodeLogin() } }, storageTokenLogin () { this.handleUserLoginRes() }, handleWXCodeLogin () { var that = this wx.login({ success (loginCode) { if (loginCode.code) { that.userLoginFn(loginCode.code) } } }) }, userLoginFn (code) { var that = this var data = { code: code } wx.showLoading({ title: '登录中...', mask: true }) userLogin(data).then(res => { wx.hideLoading() that.setStorage(res.data) }).catch(e => { wx.hideLoading() }) }, handleUserLoginRes () { const that = this const _isNeedPhone = wx.getStorageSync('isNeedPhone') const _headImg = wx.getStorageSync('headImg') console.log(_isNeedPhone, _headImg, '79'); if (!_isNeedPhone && _headImg !== '') { app.globalData.userInfo.login = true wx.setStorageSync('login', true) this.initIndexData() } else { if (_isNeedPhone) { that.setData({ phoneAuthShow: true }) } else { if (_headImg == '') { that.setData({ userAuthShow: true }) } else { app.globalData.userInfo.login = true wx.setStorageSync('login', true) } } } }, // 拉起手机号授权弹窗 getPhoneNumber (e) { var phonePermission = e.detail.errMsg.split(':')[1] if (phonePermission == 'ok') { var phoneCode = e.detail.code this.bindMobileFn(phoneCode) } else { wx.setStorageSync('isNeedPhone', true) wx.showToast({ title: '授权失败', icon: 'error' }) } }, // 绑定手机号 bindMobileFn (phoneCode) { var that = this var data = { mobileCode: phoneCode } const _headImg = wx.getStorageSync('headImg') wx.showLoading({ title: '加载中...', mask: true }) bindMobile(data).then(res => { wx.hideLoading() const resPhoneNumber = res.data app.globalData.userInfo.phoneNumber = resPhoneNumber app.globalData.isNeedPhone = false wx.setStorageSync('isNeedPhone', false) wx.setStorageSync('phoneNumber', resPhoneNumber) that.setData({ phoneAuthShow: false }) if (!_headImg) { // 需要授权头像 that.setData({ userAuthShow: true }) } else { app.globalData.userInfo.login = true wx.setStorageSync('login', true) } }).catch(e => { wx.hideLoading() wx.showModal({ content: e, confirmColor: '#333', showCancel: false }) }) }, // 获取用户头像和昵称权限 该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' }) }) }, // 提交用户头像和昵称到后台 handleConfirmNickname () { var that = this var data = { avatarUrl: that.data.avatarUrl, nickName: that.data.nickname } wx.showLoading({ title: '加载中...', mask: true }) bindBaseInfo(data).then(res => { const _headImg = that.data.avatarUrl const _userName = that.data.nickname app.globalData.userInfo.login = true app.globalData.userInfo.headImg = _headImg app.globalData.userInfo.userName = _userName app.globalData.isNeedHeadImg = false wx.setStorageSync('login', true) wx.setStorageSync('headImg', _headImg) wx.setStorageSync('userName', _userName) wx.setStorageSync('isNeedHeadImg', false) that.setData({ userAuthShow: false }) that.initIndexData() wx.hideLoading() }).catch(e => { wx.hideLoading() wx.showModal({ content: e, confirmColor: '#333', showCancel: false }) }) }, async initIndexData () { await this.awaitHomePage() await this.awaitAppointmentList() await this.awaitAppGetConfigImg() }, // 获取首页档案信息 剩余次数 awaitHomePage () { var that = this wx.showLoading({ title: '加载中...', mask: true }) return new Promise((reslove,reject) => { homePage({}).then(hoemRes => { wx.hideLoading() var response = hoemRes.data.documentVos || [] response.map(item => { item.birthDay = item.birthday.split(' ')[0] }) that.setData({ useNumber: hoemRes.data.useNumber, documentVos: response, isRefreshPage: true }) app.globalData.useNumber = hoemRes.data.useNumber reslove(hoemRes) }).catch(e => { wx.hideLoading() wx.showModal({ content: e, confirmColor: '#333', showCancel: false }) }) }) }, // 获取待预约次数 awaitAppointmentList () { const that = this wx.showNavigationBarLoading() return new Promise((reslove,reject) => { appointmentList({ status: 1, currentPage: 1 }).then(res => { wx.hideNavigationBarLoading() if (res.data.vos.length !== 0) { const response = res.data.vos || [] response.map(item => { item.timeStr = item.appointmentTime.split(' ')[0] }) that.setData({ showTopBar: true, topBarObj: response[0] }) app.globalData.hasAppointment = true } else { that.setData({ showTopBar: false, topBarObj: null }) } reslove(res) }).catch(e => { reject(e) wx.hideNavigationBarLoading() }) }) }, // 获取图片资源 awaitAppGetConfigImg () { const that = this wx.showNavigationBarLoading() return new Promise((reslove,reject) => { appGetConfigImg({ 'imgType': '5' }).then(configRes => { wx.hideNavigationBarLoading() that.setData({ homepageImg: configRes.data[0].imgUrl }) reslove(configRes) }).catch(e => { reject(e) wx.hideNavigationBarLoading() }) }) }, /** * 处理套餐 购买 or 预约 * useNumber: 0-购买 1-预约 * **/ handlePackage () { var type = this.data.useNumber > 0 ? 1 : 0 switch (type) { case 0: wx.navigateTo({ url: '/pages/buy/buy?from=index', }) break; case 1: if (this.data.showTopBar) { this.stopAppointment() } else { this.appointmentAuth() } break; } }, // 预约前判断是否存在一位检测人 appointmentAuth () { if (this.data.documentVos.length == 0) { wx.showModal({ content: '预约用户前需要先添加检测人员信息', cancelColor: '#666', confirmColor: '#333', success (res) { if (res.confirm) { wx.navigateTo({ url: '/pages/createFile/createFile?from=index' }) } } }) } else { wx.navigateTo({ url: '/pages/appointment/appointment?from=index', }) } }, // 点击全部档案 handleAllFile () { wx.navigateTo({ url: '/pages/myFile/myFile', }) }, // 点击查看报告 handleRepoetDetail (e) { var reportid = e.currentTarget.dataset.reportid wx.navigateTo({ url: '/pages/reportDetail/reportDetail?reportid=' + reportid }) }, // 添加检测人 handleAddCheck () { wx.navigateTo({ url: '/pages/createFile/createFile?form=index', }) }, /* 车子的图片 做跳转交互,跳转做判断,0次则跳出去充值弹框,充值弹窗有两个选择 :若激活卡,则跳转至实体卡兑换页面;若充值,则跳转至体验卡购买页面 */ handlActions () { if (this.data.showTopBar) { this.stopAppointment() } else { if (this.data.useNumber == 0) { wx.showModal({ title: '去充值', cancelColor: '#666', cancelText: '激活卡', confirmText: '去充值', confirmColor: '#333', success (res) { if (res.confirm) { wx.navigateTo({ url: '/pages/buy/buy?from=index', }) } else if (res.cancel) { wx.navigateTo({ url: '/pages/exchange/exchange', }) } } }) } else { this.appointmentAuth() } } }, stopAppointment () { wx.showModal({ content: '您预约的健康筛查还未体验,请先体验', cancelColor: '#666', cancelText: '取消', confirmText: '我的预约', confirmColor: '#333', success (res) { if (res.confirm) { wx.navigateTo({ url: '/pages/myAppointment/myAppointment', }) } } }) }, handleShowQRCode (e) { var that = this var id = e.currentTarget.dataset.id var realname = e.currentTarget.dataset.realname this.setData({ qrUserName: realname }) wx.showLoading({ title: '生成中...' }) createQRcode({ documentId: id }).then(res => { wx.hideLoading() that.setData({ QRCodeBase64: that.data.baseStr + res.data, qrcodeDialog: true }) }).catch(e => { wx.hideLoading() wx.showModal({ content: e, confirmColor: '#333', showCancel: false }) }) }, handleCloseQRCode (e) { this.setData({ qrcodeDialog: false, QRCodeBase64: '' }) }, onShareAppMessage () { return { title: '3分钟500+项健康指标', path: '/pages/index/index', imageUrl: '../../imaes/share.jpg' } }, setStorage (response) { const accessToken = response.accessToken const isNeedPhone = response.isNeedPhone const isNeedHeadImg = response.headImg ? false : true const headImg = response.headImg const userName = response.userName const phoneNumber = response.phoneNumber app.globalData.accessToken = accessToken app.globalData.isNeedPhone = isNeedPhone app.globalData.isNeedHeadImg = isNeedHeadImg app.globalData.userInfo.headImg= headImg app.globalData.userInfo.userName = userName app.globalData.userInfo.phoneNumber = phoneNumber wx.setStorageSync('accessToken', accessToken) wx.setStorageSync('isNeedPhone', isNeedPhone) wx.setStorageSync('isNeedHeadImg', isNeedHeadImg) wx.setStorageSync('headImg', headImg) wx.setStorageSync('userName', userName) wx.setStorageSync('phoneNumber', phoneNumber) this.handleUserLoginRes() } })