123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455 |
- 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: [], // 档案信息
- baseStr: 'data:image/jpg;base64,',
- QRCodeBase64: '',
- qrcodeDialog: false,
- qrUserName: '',
- phoneAuthShow: false,
- userAuthShow: false,
- avatarUrl: '',
- nickName: '',
- showTopBar: false,
- topBarObj: null,
- homepageImg: '',
- pageHasRequested: false // 页面数据是否已经请求过
- },
- /**
- * 生命周期函数--监听页面显示
- */
- 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')
- 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 () {
- wx.showNavigationBarLoading()
- await this.awaitHomePage()
- await this.awaitAppointmentList()
- await this.awaitAppGetConfigImg()
- wx.hideNavigationBarLoading()
- },
- // 获取首页档案信息 剩余次数
- awaitHomePage () {
- var that = this
- 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
- })
- app.globalData.useNumber = hoemRes.data.useNumber
- reslove(hoemRes)
- }).catch(e => {
- // wx.hideLoading()
- wx.showModal({
- content: e,
- confirmColor: '#333',
- showCancel: false
- })
- })
- })
- },
- // 获取待预约次数
- awaitAppointmentList () {
- const that = this
- return new Promise((reslove,reject) => {
- appointmentList({
- status: 1,
- currentPage: 1
- }).then(res => {
- 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)
- })
- })
- },
- // 获取图片资源
- awaitAppGetConfigImg () {
- const that = this
- return new Promise((reslove,reject) => {
- appGetConfigImg({
- 'imgType': '5'
- }).then(configRes => {
- that.setData({
- homepageImg: configRes.data[0].imgUrl
- })
- reslove(configRes)
- }).catch(e => {
- reject(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:
- 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()
- }
- })
|