import component from '@/utils/component'; import relativeTime from '@/utils/dayjs/relativeTime.js'; import page from '@/utils/page'; import { request } from '@/utils/request'; import { parseScene } from './utils/util'; const dayjs = require('dayjs'); const licia = require('miniprogram-licia'); require('/utils/dayjs/day-zh-cn.js'); dayjs.locale('zh-cn'); // 全局使用 dayjs.extend(relativeTime); App({ globalData: { // 测试号 wx2b0bb13edf717c1d // dev // appid:wxf9ce8010f1ad24aa // url: 'https://m.xd.hbraas.com', // upFileUrl: 'https://m.xd.hbraas.com/', // imageUrl: 'https://m.xd.hbraas.com/xd/', // pro // appid:wx71ac9c27c3c3e3f4 url: 'https://m.xd.hbsaas.com', upFileUrl: 'https://m.xd.hbsaas.com/', imageUrl: 'https://m.xd.hbsaas.com/api/xd/', loginState: '', isLogin: 0, loginType: 0, scene: null, backPath: '', zdUserInfo: {}, }, onLaunch() { Page = page as WechatMiniprogram.Page.Constructor; Component = component as WechatMiniprogram.Component.Constructor; wx.ajax = licia.curry(request)({ gUrl: this.globalData.url }); wx.login({ success: (res) => { wx.ajax({ method: 'GET', url: '?r=xd/user/init-login', data: { code: res.code, }, }).then((res: any) => { this.globalData.loginState = res.loginState; this.globalData.isLogin = res.isLogin; this.globalData.loginType = res.loginType; }); }, }); wx.setInnerAudioOption({ obeyMuteSwitch: false, mixWithOther: false, }); }, onShow(options) { if (options.query.scene) { this.globalData.scene = parseScene(options.query.scene); } }, waitLogin({ type = 0 } = { type: 'any' }) { return new Promise((resolve) => { const checkLogin = () => { if (this.globalData.loginState) { if (this.checkLoginType(type)) { resolve(); } return; } setTimeout(() => { checkLogin(); }, 500); }; checkLogin(); }); }, checkLoginType(type: 0 | 1 | 2 | 'any') { const { loginType, isLogin } = this.globalData; if (type === 'any') { return true; } if (isLogin !== 1) { if (type === 0) { return true; } wx.reLaunch({ url: '/pages/login/index', }); return false; } if (loginType !== 2) { wx.reLaunch({ url: '/pages/login/index', }); return false; } return true; }, getUserInfo(type: 0 | 1 | 2): Promise { const url = { 2: '?r=xd/doctor/index/get-doctor', }[type]; return wx.ajax({ method: 'GET', url, data: {}, }) as Promise; }, mpBehavior(data) { wx.ajax({ method: 'POST', url: '?r=xd/doctor/index/operate-action-submit', data, loading: false, showMsg: false, }); }, getMenuInfo(self) { const menuButtonInfo = wx.getMenuButtonBoundingClientRect(); self.setData({ menuButtonInfo, }); }, // zd相关函数 zdMpBehavior(data: { PageName: string; doctor?: boolean }) { let url = '?r=xd/mp-behavior/add'; if (data.doctor) { url = '?r=zd/doctor/mp-behavior/add'; } delete data.doctor; wx.ajax({ method: 'POST', url, data, loading: false, showMsg: false, }).then((res) => { console.log('res: ', res); }); }, zdGetTheme() { return new Promise((resolve) => { const checkUserType = () => { const zdUserInfo = this.globalData.zdUserInfo; if (zdUserInfo.UserType) { // resolve(zdUserInfo.UserType >= 4 ? 'DRUG' : 'PATIENT') resolve('PATIENT'); return true; } return false; }; const tryResolve = () => { if (!checkUserType()) { setTimeout(tryResolve, 30); } }; tryResolve(); }); }, zdWaitLogin({ isReg = true, loginPage = false, pub = false } = { isReg: true, loginPage: false, pub: false }) { let time: number; const regFun = (resolve) => { if (this.zdVerifySys(pub)) { if (this.globalData.loginType === 1) { this.zdRegistrationVerification(() => resolve(), loginPage); } else { resolve(); } } }; const unRegFun = (resolve) => { if (!this.zdVerifySys(pub)) return; resolve(); }; return new Promise((resolve: (value?) => void) => { if (isReg) { if (Object.keys(this.globalData.zdUserInfo).length) { regFun(resolve); return; } time = setInterval(() => { if (Object.keys(this.globalData.zdUserInfo).length) { clearInterval(time); regFun(resolve); } }, 500); return; } this.globalData.anyWhere = true; if (this.globalData.loginState) { unRegFun(resolve); return; } time = setInterval(() => { if (this.globalData.loginState) { clearInterval(time); unRegFun(resolve); } }, 500); }); }, zdRegistrationVerification(callback: () => void, loginPage = false) { // 1-空白用户,2-注册用户,3-疾病患者,4-用药患者 const { PatientId, AuditStatus, isFollow, UserType } = this.globalData.zdUserInfo; const { anyWhere, first } = this.globalData; if (loginPage || (anyWhere && first)) { callback(); return; } if (UserType == 4) { this.globalData.first = false; callback(); return; } if (UserType == 1) { this.globalData.registChannel = 0; this.globalData.first = false; wx.reLaunch({ url: '/patient/pages/login/index', }); return; } let urlKey = ''; if (!PatientId) { urlKey = 'enterInfo'; } else if (AuditStatus == 0) { urlKey = 'noCert'; } else if (AuditStatus == 1) { urlKey = isFollow ? 'nopending' : 'pending'; } else if (AuditStatus == 2) { urlKey = 'reject'; } let navUrl = { enterInfo: '/patient/pages/enterInfo/index', noCert: '/patient/pages/enterInfo/index', reject: '/patient/pages/enterInfo/index', pending: '/patient/pages/enterInfo/index', nopending: '/patient/pages/enterInfo/index', }[urlKey]; if (navUrl && first) { this.globalData.registChannel = 0; this.globalData.first = false; wx.reLaunch({ url: navUrl, }); this.globalData.first = false; return; } this.globalData.first = false; callback(); }, zdVerifySys(pub = false) { // 1:患者 2:医生 const { loginType, isLogin, anyWhere } = this.globalData; const pages = getCurrentPages(); const currentPage = pages[pages.length - 1]; const url = currentPage.route; const options = currentPage.options; const ignorePath = ['pages/start/index']; // 未登录用户 if (!isLogin) { // 医生端页面重定向到医生端登录页 if (url.includes('doctor/pages')) { wx.reLaunch({ url: '/doctor/pages/d_login/index', }); return false; } // 患者端随便看看 if (anyWhere) { return true; } wx.reLaunch({ url: '/patient/pages/login/index', }); return false; } if (ignorePath.includes(url)) return true; if (loginType === 1) { if (url.includes('doctor/pages')) { wx.reLaunch({ url: '/pages/index/index', }); return false; } else { return true; } } if (loginType === 2 && !pub) { if (url.includes('doctor/pages')) { return true; } else { const params = Object.entries(options) .map(([key, value]) => `${key}=${value}`) .join('&'); wx.reLaunch({ url: '/doctor/pages/d_trans/index?path=' + encodeURIComponent(`/${url}?${params}`), }); return false; } } return true; }, zdPermissionVerification(grade = 1, registChannel = 0, backPage = null, regBusinessId = '') { // 1-空白用户,2-注册用户,3-疾病患者,4-用药患者 const { PatientId, AuditStatus, isFollow, UserType } = this.globalData.zdUserInfo; const that = this; this.globalData.registChannel = registChannel; this.globalData.regBusinessId = regBusinessId; let isReject = false; return new Promise((resolve, reject) => { if (grade == 2 && Number(UserType) < 2) { wx.reLaunch({ url: '/patient/pages/login/index', }); isReject = true; } if (grade == 3 && Number(UserType) < 3) { if (UserType == 1) { wx.reLaunch({ url: '/patient/pages/login/index', }); isReject = true; } if (UserType == 2) { let urlKey = ''; if (!PatientId) { urlKey = 'enterInfo'; } else if (AuditStatus == 0) { urlKey = 'noCert'; } else if (AuditStatus == 1) { if (backPage && (backPage as string).includes('liveResult')) { urlKey = 'liveResult'; } else { urlKey = isFollow ? 'nopending' : 'pending'; } } else if (AuditStatus == 2) { urlKey = 'reject'; } let navUrl = { enterInfo: '/patient/pages/enterInfo/index', noCert: '/patient/pages/enterInfo/index', reject: '/patient/pages/enterInfo/index', pending: '/patient/pages/enterInfo/index', nopending: '/patient/pages/enterInfo/index', liveResult: backPage, }[urlKey]; if (urlKey == 'liveResult') { wx.navigateTo({ url: navUrl as string, }); } else { wx.reLaunch({ url: navUrl as string, }); } isReject = true; } } if (grade == 4 && Number(UserType) < 4) { wx.reLaunch({ url: '/patient/pages/vipLogin/index', }); isReject = true; } if (isReject) { that.globalData.backPage = backPage; reject(null); return; } resolve(null); }); }, getZdUserInfo(self, update = false, callback = (_zdUserInfo) => {}) { if (this.globalData.zdUserInfo?.UserId && !update) { self?.setData({ zdUserInfo: this.globalData.zdUserInfo, }); callback(this.globalData.zdUserInfo); return; } wx.ajax({ method: 'GET', url: '?r=zd/account/info', showMsg: false, }).then((res) => { res.UserType = res.UserType || 1; if (res.UserType > 2) { this.globalData.anyWhere = false; } this.globalData.zdUserInfo = res; self?.setData({ zdUserInfo: res, }); const { doctorId, inviteChan } = this.globalData.scene; if (doctorId && res.InviteDoctorId != doctorId && this.globalData.isLogin && res.PatientId) { wx.ajax({ method: 'POST', url: '?r=zd/account/update-doctor', data: { inviteDoctorId: doctorId, inviteChannel: inviteChan, }, }); } callback(res); }); }, });