hh-doctor-sdk 0.9.0 → 0.9.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,10 +4,12 @@ var currentPlayIndex = -1
4
4
  var audioAnimationInterval
5
5
 
6
6
  const hhNavigation = require('../../hh-navigation')
7
+ const hhDoctor = require('../../hh-doctor')
7
8
  const doctorLoader = require('../../public/doctor-loader')
8
9
  const permission = require('../../utils/permission-utils')
9
10
  const imManager = require('../../public/im-manager')
10
11
  const cache = require('../../public/cache-data')
12
+ const loginGuard = require('../../public/login-guard')
11
13
  const { urlEndsWith } = require('../../utils/common-utils')
12
14
 
13
15
  Page({
@@ -20,6 +22,7 @@ Page({
20
22
  onLoad(options) {
21
23
  this._detailAlive = true
22
24
  wx.statLog.sendStatLog(options.orderId, '打开咨询单详情界面')
25
+ hhDoctor.absorbEntryOptions(options)
23
26
  const doctorId = options.doctorId
24
27
  doctorLoader.loadDoctor({
25
28
  doctorId: doctorId,
@@ -64,10 +67,14 @@ Page({
64
67
  this.setData({
65
68
  showPreview: false,
66
69
  })
67
- } else {
68
- // this._refreshOrder()
69
- wx.startPullDownRefresh()
70
+ return
71
+ }
72
+ // 未登录时 onLoad 已经去登录页,这里再刷新会失败并 navigateBack,把登录页弹掉
73
+ if (!loginGuard.isLoggedIn()) {
74
+ wx.stopPullDownRefresh()
75
+ return
70
76
  }
77
+ wx.startPullDownRefresh()
71
78
  },
72
79
 
73
80
  /**
@@ -108,6 +115,10 @@ Page({
108
115
  },
109
116
 
110
117
  _refreshOrder() {
118
+ if (!loginGuard.isLoggedIn()) {
119
+ wx.stopPullDownRefresh()
120
+ return
121
+ }
111
122
  if (this.data.orderId === null || this.data.orderId === '') {
112
123
  return
113
124
  }
@@ -1,5 +1,6 @@
1
1
  const doctorLoader = require('../../public/doctor-loader')
2
2
  const cache = require('../../public/cache-data')
3
+ const hhDoctor = require('../../hh-doctor')
3
4
 
4
5
  Page({
5
6
 
@@ -19,7 +20,13 @@ Page({
19
20
  * 生命周期函数--监听页面加载
20
21
  */
21
22
  onLoad(options) {
22
-
23
+ // 短链直达列表页时,链接里的 doctorId / userToken 要落下来,否则 onShow 只读 storage 会取空。
24
+ // doctorId 先校验格式再写,避免转发来的链接把本地医生身份覆盖成脏值
25
+ const doctorId = options && options.doctorId ? String(options.doctorId).trim() : ''
26
+ if (/^[DE]\d{19}$/.test(doctorId)) {
27
+ wx.setStorageSync('doctorId', doctorId)
28
+ }
29
+ hhDoctor.absorbEntryOptions(options)
23
30
  },
24
31
 
25
32
  /**
@@ -51,10 +58,8 @@ Page({
51
58
  if (judgeLogin) {
52
59
  wx.statLog.sendStatLog(null, '打开咨询单列表界面')
53
60
  const doctorId = wx.getStorageSync('doctorId') || ''
54
- const userToken = wx.getStorageSync('userToken') || ''
55
61
  cache.cache({
56
- doctorId: doctorId,
57
- userToken: userToken
62
+ doctorId: doctorId
58
63
  })
59
64
  if (!doctorId) {
60
65
  this.setData({
@@ -88,6 +93,11 @@ Page({
88
93
  init: true
89
94
  })
90
95
  }
96
+ } else {
97
+ // 未登录被引导去登录、或拉医生信息失败:复位 loading,避免停在加载态
98
+ this.setData({
99
+ isLoading: false
100
+ })
91
101
  }
92
102
  })
93
103
  }
@@ -48,6 +48,17 @@ function setUserToken(userToken) {
48
48
  }
49
49
  }
50
50
 
51
+ /** 短链直达页面时,把 onLoad(options) 里的 userToken 落地,后续页面和请求直接复用 */
52
+ function absorbEntryOptions(options) {
53
+ if (!options) {
54
+ return
55
+ }
56
+ const token = (options.userToken || options.hhusertoken || '').trim()
57
+ if (token) {
58
+ setUserToken(token)
59
+ }
60
+ }
61
+
51
62
  /**
52
63
  * 检测医生信息是否认证完成
53
64
  * @param {*} doctorId 医生Id
@@ -98,6 +109,7 @@ module.exports = {
98
109
  initStatLog,
99
110
  setLoginCheck,
100
111
  setUserToken,
112
+ absorbEntryOptions,
101
113
  enterForground,
102
114
  enterBackground,
103
115
  isDoctorAuth
@@ -1,5 +1,5 @@
1
1
  /** 发版 npm 时与 package.json version 保持一致 */
2
- const sdkVersion = '0.8.8'
2
+ const sdkVersion = '0.9.1'
3
3
 
4
4
  const appVersion = '7.1.6'
5
5
  const appID = '10423'
@@ -1,5 +1,6 @@
1
1
  const api = require('../../apis/order-app-api')
2
2
  const cache = require('../../public/cache-data')
3
+ const loginGuard = require('../../public/login-guard')
3
4
 
4
5
  module.exports = Behavior({
5
6
 
@@ -20,10 +21,12 @@ module.exports = Behavior({
20
21
  * @param options keepPage 为 true 时失败只留日志,不提示也不退出页面
21
22
  */
22
23
  loadOrder(callback, options) {
23
- // wx.showLoading({
24
- // title: '加载订单数据...',
25
- // mask: true,
26
- // })
24
+ // 未登录时详情页 onShow 仍会进来。请求必定失败,随后 navigateBack 会把刚打开的登录页退掉。
25
+ if (!loginGuard.isLoggedIn()) {
26
+ wx.hideNavigationBarLoading()
27
+ wx.stopPullDownRefresh()
28
+ return
29
+ }
27
30
  const requestToken = (this._orderLoadToken || 0) + 1
28
31
  this._orderLoadToken = requestToken
29
32
  const orderId = this.data.orderId
@@ -61,7 +64,10 @@ module.exports = Behavior({
61
64
  }
62
65
  wx.statLog.sendStatLog(orderId, `获取订单数据出错${JSON.stringify(e)}`)
63
66
  wx.hideLoading()
64
- if (options && options.keepPage) {
67
+ wx.hideNavigationBarLoading()
68
+ wx.stopPullDownRefresh()
69
+ // 请求发出后登录态没了:同样不能退栈,否则登录页会被弹掉
70
+ if ((options && options.keepPage) || !loginGuard.isLoggedIn()) {
65
71
  return
66
72
  }
67
73
  wx.showToast({
@@ -84,23 +84,67 @@ function uuid() {
84
84
  return null
85
85
  }
86
86
 
87
+ /** 链接参数里的 token 只采纳一次,避免退出登录后又被取回 */
88
+ let linkTokenConsumed = false
89
+
87
90
  function setUserToken(token) {
91
+ const value = token || ''
92
+ if (value) {
93
+ // 任何来源写过非空 token 后,本次启动内不再回退到链接参数
94
+ linkTokenConsumed = true
95
+ }
88
96
  cache({
89
- userToken: token || ''
97
+ userToken: value
90
98
  })
91
99
  }
92
100
 
93
- /** 外部预先写入的 userToken 优先;医生信息回来后仍可回退到 doctor.userToken */
101
+ /**
102
+ * userToken 由宿主通过 hhDoctor.setUserToken 传入(同时写内存和 storage)。
103
+ * 顺序:内存 > 医生信息 > storage > 本次进入的链接参数。
104
+ * 最后一级给「专家从微信消息短链直接进入」用:短链里的 userToken 落在 enter/launch options 的 query 上,
105
+ * 与 App 拼 H5 链接的传参方式一致(userToken / hhusertoken 两个 key 都认)。
106
+ */
94
107
  function userToken() {
95
108
  const preset = getValue('userToken')
96
109
  if (preset) {
97
110
  return preset
98
111
  }
99
112
  const doctorInfo = doctor()
100
- if (doctorInfo) {
113
+ if (doctorInfo && doctorInfo.userToken) {
101
114
  return doctorInfo.userToken
102
115
  }
103
- return null
116
+ return storedUserToken() || linkUserToken() || null
117
+ }
118
+
119
+ function storedUserToken() {
120
+ try {
121
+ return wx.getStorageSync('userToken') || ''
122
+ } catch (e) {
123
+ return ''
124
+ }
125
+ }
126
+
127
+ function linkUserToken() {
128
+ if (linkTokenConsumed) {
129
+ return ''
130
+ }
131
+ const pick = (options) => {
132
+ const query = (options && options.query) || {}
133
+ return query.userToken || query.hhusertoken || ''
134
+ }
135
+ let token = ''
136
+ try {
137
+ const enter = wx.getEnterOptionsSync && wx.getEnterOptionsSync()
138
+ token = pick(enter) || pick(wx.getLaunchOptionsSync && wx.getLaunchOptionsSync())
139
+ } catch (e) {
140
+ token = ''
141
+ }
142
+ if (!token) {
143
+ return ''
144
+ }
145
+ // 采纳一次即落库,之后都走内存/storage;退出登录清空后不会再从链接取回
146
+ setUserToken(token)
147
+ return token
104
148
  }
105
149
 
106
150
  function videoToken() {
@@ -1,5 +1,6 @@
1
1
  const api = require('../apis/order-app-api')
2
2
  const cache = require('./cache-data')
3
+ const loginGuard = require('./login-guard')
3
4
 
4
5
  /**
5
6
  * 获取登录医生的数据
@@ -7,12 +8,6 @@ const cache = require('./cache-data')
7
8
  */
8
9
  function loadDoctor(options, callback) {
9
10
  const doctorId = options.doctorId
10
- if (!cache.userToken()) {
11
- const storedToken = wx.getStorageSync('userToken') || ''
12
- if (storedToken) {
13
- cache.setUserToken(storedToken)
14
- }
15
- }
16
11
  if (!doctorId) {
17
12
  wx.statLog.sendStatLog(null, `场景:${options.scence || '未知'} 未传必要的参数`)
18
13
  wx.showToast({
@@ -20,6 +15,20 @@ function loadDoctor(options, callback) {
20
15
  })
21
16
  return
22
17
  }
18
+ // 没有登录态时后面每个请求都会被鉴权挡掉,直接转登录,别再发注定 402 的请求
19
+ if (!loginGuard.isLoggedIn()) {
20
+ wx.statLog.sendStatLog(null, `场景:${options.scence || '未知'} 未登录,转登录页`)
21
+ if (!loginGuard.redirectToLogin()) {
22
+ wx.showToast({
23
+ title: '请先登录',
24
+ icon: 'none',
25
+ })
26
+ }
27
+ if (callback) {
28
+ callback(false)
29
+ }
30
+ return
31
+ }
23
32
  // 由于后续所有联网请求都需要doctorId所以在此缓存doctorId在联网公共参数会用到
24
33
  if (doctorId === cache.doctorId() && cache.doctor() !== null) {
25
34
  // 如果传入的doctorId和以前已经加载过的doctorId一致并且doctor数据不为空可以不再次加载doctor数据
@@ -0,0 +1,36 @@
1
+ const cache = require('./cache-data')
2
+
3
+ let redirectingAt = 0
4
+
5
+ /** 是否已登录:宿主注册的 judgeLogin 优先,没注册就按有没有 userToken 判断 */
6
+ function isLoggedIn() {
7
+ const config = wx.loginConfig
8
+ if (config && typeof config.judgeLogin === 'function') {
9
+ return !!config.judgeLogin()
10
+ }
11
+ return !!cache.userToken()
12
+ }
13
+
14
+ /**
15
+ * 跳宿主登录页。
16
+ * 返回 false 表示宿主没注册 directLogin,调用方自己决定怎么提示。
17
+ * 3 秒内只跳一次,避免返回后 onShow 再触发、页面栈叠一摞登录页。
18
+ */
19
+ function redirectToLogin() {
20
+ const config = wx.loginConfig || {}
21
+ if (typeof config.directLogin !== 'function') {
22
+ return false
23
+ }
24
+ const now = Date.now()
25
+ if (now - redirectingAt < 3000) {
26
+ return true
27
+ }
28
+ redirectingAt = now
29
+ config.directLogin(true)
30
+ return true
31
+ }
32
+
33
+ module.exports = {
34
+ isLoggedIn,
35
+ redirectToLogin,
36
+ }
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "hh-doctor-sdk",
3
- "version": "0.9.0",
3
+ "version": "0.9.2",
4
4
  "description": "和缓名医小程序SDK",
5
5
  "scripts": {
6
- "test": "echo \"Error: no test specified\" && exit 1"
6
+ "test": "node test/sdk-check.js"
7
7
  },
8
8
  "keywords": [
9
9
  "和缓名医小程序SDK"
@@ -11,6 +11,9 @@
11
11
  "author": "liumingming",
12
12
  "license": "ISC",
13
13
  "miniprogram": "components",
14
+ "files": [
15
+ "components"
16
+ ],
14
17
  "dependencies": {
15
18
  }
16
19
  }