武田基因病B开发环境
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

572 lines
16 KiB

2 months ago
/* eslint-disable */
// app.ts
import { request } from './api/request'
import { parseScene } from './utils/util'
const licia = require('miniprogram-licia')
const dayjs = require('dayjs')
require('/utils/dayjs/day-zh-cn.js')
const relativeTime = require('/utils/dayjs/relativeTime.js')
dayjs.locale('zh-cn') // 全局使用
dayjs.extend(relativeTime)
// page
import page from '@/utils/page'
App<IAppOption>({
globalData: {
// dev
// appid:wxa4ece062e60e93a5
url: 'https://m.zd.hbraas.com',
upFileUrl: 'https://m.zd.hbraas.com/',
2 months ago
imageUrl: "https://m.wtx.hbraas.com/GeneB/",
2 months ago
// pro
// appid:wx96f45ca4f1fa36ec
// url: 'https://m.zd.hbsaas.com',
// upFileUrl: 'https://m.zd.hbsaas.com/',
// imageUrl: 'https://m.zd.hbsaas.com/zd/',
//
//login
registrationSource: 0,
registChannel: 0,
regBusinessId: '',
IsAliQiWei: 0,
scene: {},
listeners: [],
Timestamp: new Date().getTime(),
loginState: '',
isLogin: 0, // 0 未登录 1 已登录
isReg: '0',
WorkerId: '',
loginType: '', // 1:患者 2:医生
first: true,
// anyWhere: true,
anyWhere: false,
userInfo: {},
},
onLaunch() {
// if (options.path) {
// this.globalData.anyWhere = true;
// }
Page = page as WechatMiniprogram.Page.Constructor
wx.ajax = licia.curry(request)({ gUrl: this.globalData.url })
this.autoUpdate()
wx.setInnerAudioOption({
obeyMuteSwitch: false,
mixWithOther: false,
})
2 months ago
wx.login({
success: (res) => {
console.log('code',res)
},
})
2 months ago
},
onShow(options) {
if (options.query.scene) {
this.globalData.anyWhere = false
this.globalData.scene = parseScene(options.query.scene) as { workerId: string }
}
// this.startLogin()
},
startLogin() {
const that = this
wx.login({
success(res) {
wx.ajax({
method: 'GET',
url: '?r=zd/user/init-login',
data: {
code: res.code,
},
}).then((res) => {
that.globalData.loginState = res.loginState
that.globalData.isReg = res.isReg
that.globalData.isLogin = res.isLogin
that.globalData.loginType = res.loginType
if (!Object.keys(that.globalData.scene)?.length) {
that.globalData.anyWhere = res.anyWhere
}
if (wx.getLaunchOptionsSync()?.query?.activity === 'NRDL') {
that.clickPush()
}
that.getUserInfo()
})
},
})
},
waitLogin({ isReg = true, loginPage = false } = { isReg: true, loginPage: false }) {
let time: number
const regFun = (resolve) => {
if (this.verifySys()) {
if (this.globalData.loginType === 1) {
this.registrationVerification(() => resolve(), loginPage)
} else {
resolve()
}
}
}
const unRegFun = (resolve) => {
if (!this.verifySys(isReg)) return
resolve()
}
return new Promise((resolve: (value?) => void) => {
if (isReg) {
if (Object.keys(this.globalData.userInfo).length) {
regFun(resolve)
return
}
time = setInterval(() => {
if (Object.keys(this.globalData.userInfo).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)
})
},
registrationVerification(callback: () => void, loginPage = false) {
// 1-空白用户,2-注册用户,3-疾病患者,4-用药患者
const { PatientId, AuditStatus, isFollow, UserType } = this.globalData.userInfo
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: '/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: '/pages/enterInfo/index',
noCert: '/pages/enterInfo/index',
reject: '/pages/enterInfo/index',
pending: '/pages/enterInfo/index',
nopending: '/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()
},
verifySys() {
// 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: '/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) {
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
},
permissionVerification(grade = 1, registChannel = 0, backPage = null, regBusinessId = '') {
// 1-空白用户,2-注册用户,3-疾病患者,4-用药患者
const { PatientId, AuditStatus, isFollow, UserType } = this.globalData.userInfo
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: '/pages/login/index',
})
isReject = true
}
if (grade == 3 && Number(UserType) < 3) {
if (UserType == 1) {
wx.reLaunch({
url: '/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: '/pages/enterInfo/index',
noCert: '/pages/enterInfo/index',
reject: '/pages/enterInfo/index',
pending: '/pages/enterInfo/index',
nopending: '/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: '/pages/vipLogin/index',
})
isReject = true
}
if (isReject) {
that.globalData.backPage = backPage
reject(null)
return
}
resolve(null)
})
},
getUserInfo(self, update = false, callback = (_userInfo) => {}) {
if (this.globalData.userInfo?.UserId && !update) {
self?.setData({
userInfo: this.globalData.userInfo,
})
callback(this.globalData.userInfo)
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.userInfo = res
this.triggerListeners()
self?.setData({
userInfo: 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)
})
},
mpBehavior(data: { PageName: string; doctor?: boolean }) {
let url = '?r=zd/mp-behavior/add'
if (data.doctor) {
url = '?r=zd/doctor/mp-behavior/add'
}
delete data.doctor
wx.ajax({
method: 'POST',
url,
data: data,
loading: false,
showMsg: false,
}).then((res) => {
console.log('res: ', res)
})
},
// 更新用户信息
async updateUserInfo(encryptedData, iv) {
let loginState = this.globalData.loginState
const encodeEncryptedData = encodeURIComponent(encryptedData)
const encodeIv = encodeURIComponent(iv)
const {
data: { code, msg },
} = await wx.ajax({
method: 'POST',
url: '?r=eyecare/user/update-wx-user-info',
data: {
loginState,
encryptedData: encodeEncryptedData,
iv: encodeIv,
},
})
if (code !== 0) {
wx.showToast({
duration: 1500,
title: msg,
icon: 'none',
})
}
return code
},
getCode() {
return new Promise((resolve) => {
wx.login({
success: (res) => {
resolve(res.code)
},
fail: () => {
wx.showToast({
duration: 10000,
title: '发生错误请重试',
icon: 'none',
})
},
})
})
},
// 微信登录
doLogin() {
return new Promise((resolve) => {
wx.showLoading({
title: '加载中...',
mask: true,
})
this.getCode().then((code) => {
wx.ajax({
url: '?r=eyecare/user/init-login',
data: {
code,
},
}).then((res) => {
if (res.data.code === 0) {
resolve(res)
}
})
})
})
},
clickPush() {
wx.ajax({
method: 'POST',
url: '?r=zd/nrdl/add-push-click-record',
})
},
watch: function (key, method) {
var obj = this.globalData
//加个前缀生成隐藏变量,防止死循环发生
let ori = obj[key] //obj[key]这个不能放在Object.defineProperty里
if (ori) {
//处理已经声明的变量,绑定处理
method(ori)
}
Object.defineProperty(obj, key, {
configurable: true,
enumerable: true,
set: function (value) {
this['_' + key] = value
console.log('是否会被执行2')
method(value)
},
get: function () {
// 在其它界面调用key值的时候,这里就会执行。
if (typeof this['_' + key] == 'undefined') {
if (ori) {
//这里读取数据的时候隐藏变量和 globalData设置不一样,所以要做同步处理
this['_' + key] = ori
return ori
} else {
return undefined
}
} else {
return this['_' + key]
}
},
})
},
// 注册监听器函数的方法
registerListener: function (listener: () => {}) {
this.globalData.listeners.push(listener)
},
// 触发监听器函数的方法
triggerListeners: function () {
var listeners = this.globalData.listeners
for (var i = 0; i < listeners.length; i++) {
listeners[i]()
}
},
autoUpdate: function () {
var self = this
// 获取小程序更新机制兼容
if (wx.canIUse('getUpdateManager')) {
const updateManager = wx.getUpdateManager()
//1. 检查小程序是否有新版本发布
updateManager.onCheckForUpdate(function (res) {
// 请求完新版本信息的回调
if (res.hasUpdate) {
//检测到新版本,需要更新,给出提示
wx.showModal({
title: '更新提示',
content: '检测到新版本,是否下载新版本并重启小程序?',
success: function (res) {
if (res.confirm) {
//2. 用户确定下载更新小程序,小程序下载及更新静默进行
self.downLoadAndUpdate(updateManager)
} else if (res.cancel) {
//用户点击取消按钮的处理,如果需要强制更新,则给出二次弹窗,如果不需要,则这里的代码都可以删掉了
wx.showModal({
title: '温馨提示~',
content: '本次版本更新涉及到新的功能添加,旧版本无法正常访问的哦~',
showCancel: false, //隐藏取消按钮
confirmText: '确定更新', //只保留确定更新按钮
success: function (res) {
if (res.confirm) {
//下载新版本,并重新应用
self.downLoadAndUpdate(updateManager)
}
},
})
}
},
})
}
})
} else {
// 如果希望用户在最新版本的客户端上体验您的小程序,可以这样子提示
wx.showModal({
title: '提示',
content: '当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试。',
})
}
},
downLoadAndUpdate: function (updateManager) {
//静默下载更新小程序新版本
updateManager.onUpdateReady(function () {
wx.hideLoading()
//新的版本已经下载好,调用 applyUpdate 应用新版本并重启
updateManager.applyUpdate()
})
updateManager.onUpdateFailed(function () {
// 新的版本下载失败
wx.showModal({
title: '新版本更新失败',
content: '您可删除当前小程序,重新打开尝试',
})
})
},
getTheme() {
return new Promise((resolve) => {
const checkUserType = () => {
const userInfo = this.globalData.userInfo
if (userInfo.UserType) {
// resolve(userInfo.UserType >= 4 ? 'DRUG' : 'PATIENT')
resolve('PATIENT')
return true
}
return false
}
const tryResolve = () => {
if (!checkUserType()) {
setTimeout(tryResolve, 30)
}
}
tryResolve()
})
},
})