// pages/index/index.js import { homePage } from '../../api/index' import { createQRcode } from "../../api/document" import { ittLogin, bindMobile, bindBaseInfo } from '../../api/my' import { appointmentList } from '../../api/appointment' const app = getApp() Page({ /** * 页面的初始数据 */ data: { locationStr: '定位地址…', 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 }, onShow () { app.globalData.selectedInex = 0 if (this.data.isRefreshPage) { this.initIndexData() } }, onLoad () { this.init() }, // 初始化函数,判读需不需要获取wx.login => code init () { console.log(app.globalData.accessToken, 'index onlaod') const accessToken = app.globalData.accessToken if (accessToken) { var isNeedPhone = wx.getStorageSync('isNeedPhone') var isNeedHeadImg = wx.getStorageSync('isNeedHeadImg') var headImg = wx.getStorageSync('headImg') var userName = wx.getStorageSync('userName') var phoneNumber = wx.getStorageSync('phoneNumber') app.globalData.isNeedPhone = isNeedPhone app.globalData.isNeedHeadImg = isNeedHeadImg app.globalData.userInfo.headImg= headImg app.globalData.userInfo.userName = userName app.globalData.userInfo.phoneNumber = phoneNumber app.globalData.userInfo.login = true this.handleAuth() } else { this.appCodeLoginFn() } }, // 是否需要权限判断 手机号 头像 昵称 handleAuth () { // 不需要手机号授权 有头像和昵称 const resIsNeedPhone = app.globalData.isNeedPhone const resHeadImg = app.globalData.userInfo.headImg const that = this if (!resIsNeedPhone && resHeadImg !== '') { app.globalData.userInfo.login = true this.initIndexData() } else { if (resIsNeedPhone) { // true 代表需要手机号授权 that.setData({ phoneAuthShow: true }) } else { if (resHeadImg == '') { // 需要授权头像 that.setData({ userAuthShow: true }) } else { app.globalData.userInfo.login = true } } } }, // 获取本地存储你的头像 昵称 手机号 以及 accessToken 信息 appLoginFn () { console.log('使用已存在的accessToekn登录') this.initIndexData() }, // 使用wx.login => code => accessToken appCodeLoginFn () { var that = this wx.login({ success (loginCode) { if (loginCode.code) { that.ittLoginFn(loginCode.code) } } }) }, // 登录函数 ittLoginFn (code) { var that = this var data = { code: code } wx.showLoading({ title: '登录中...', mask: true }) ittLogin(data).then(res => { const response = res.data const accessToken = response.accessToken const isNeedPhone = response.isNeedPhone const isNeedHeadImg = response.headImg ? true : false 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) wx.hideLoading() that.handleAuth() }).catch(e => { wx.hideLoading() }) }, // 拉起手机号授权弹窗 getPhoneNumber (e) { var phonePermission = e.detail.errMsg.split(':')[1] if (phonePermission == 'ok') { var phoneCode = e.detail.code this.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() const resPhoneNumber = res.data app.globalData.userInfo.phoneNumber = resPhoneNumber wx.setStorageSync('phoneNumber', resPhoneNumber) that.setData({ phoneAuthShow: false }) if (app.globalData.userInfo.headImg == '') { // 需要授权头像 that.setData({ userAuthShow: true }) } else { app.globalData.userInfo.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.confirmAvatarUrl const _userName = that.data.confirmNickname app.globalData.userInfo.login = true app.globalData.userInfo.headImg = _headImg app.globalData.userInfo.userName = _userName wx.setStorageSync('headImg', _headImg) wx.setStorageSync('userName', _userName) that.setData({ userAuthShow: false }) that.initIndexData() wx.hideLoading() }).catch(e => { wx.hideLoading() wx.showModal({ content: e, confirmColor: '#333', showCancel: false }) }) }, /* onPullDownRefresh () { wx.vibrateShort({ type: 'medium' }) this.initIndexData(true) }, */ onShareAppMessage () { return { title: '3分钟500+项健康指标', path: '/pages/index/index', imageUrl: '../../imaes/share.jpg' } }, // 获取首页信息 档案 剩余次数, /* 定位地址这边换成 1. 如果没有预约信息,就这条直接隐藏掉 2. 如果有预约信息,则显示,您预约的2022-10-25 8:00-12:00的健康筛查 待体验 */ initIndexData (pullDownRefresh = false) { var that = this wx.showLoading({ title: '加载中...', mask: true }) homePage({}).then(hoemRes => { wx.hideLoading() if (pullDownRefresh) { wx.stopPullDownRefresh() } var response = hoemRes.data.documentVos || [] response.map(item => { item.birthDay = item.birthday.split(' ')[0] }) that.setData({ useNumber: hoemRes.data.useNumber, documentVos: response, isRefreshPage: true }) }).catch(e => { wx.hideLoading() wx.showModal({ content: e, confirmColor: '#333', showCancel: false }) }) appointmentList({ status: 1, currentPage: 1 }).then(res => { console.log(res, 'appointmentList'); if (res.data.vos.length !== 0) { that.setData({ showTopBar: true, topBarObj: res.data.vos[0] }) } else { that.setData({ showTopBar: false, topBarObj: null }) } }).catch(e => { }) }, /** * 处理套餐 购买 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: 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.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() } }, 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: '' }) } })