hh-doctor-sdk 0.9.0 → 0.9.1

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,6 +4,7 @@ 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')
@@ -20,6 +21,7 @@ Page({
20
21
  onLoad(options) {
21
22
  this._detailAlive = true
22
23
  wx.statLog.sendStatLog(options.orderId, '打开咨询单详情界面')
24
+ hhDoctor.absorbEntryOptions(options)
23
25
  const doctorId = options.doctorId
24
26
  doctorLoader.loadDoctor({
25
27
  doctorId: doctorId,
@@ -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,12 @@ Page({
19
20
  * 生命周期函数--监听页面加载
20
21
  */
21
22
  onLoad(options) {
22
-
23
+ // 短链直达列表页时,链接里的 doctorId / userToken 要落下来,否则 onShow 只读 storage 会取空
24
+ const doctorId = options && options.doctorId ? String(options.doctorId).trim() : ''
25
+ if (doctorId) {
26
+ wx.setStorageSync('doctorId', doctorId)
27
+ }
28
+ hhDoctor.absorbEntryOptions(options)
23
29
  },
24
30
 
25
31
  /**
@@ -51,10 +57,8 @@ Page({
51
57
  if (judgeLogin) {
52
58
  wx.statLog.sendStatLog(null, '打开咨询单列表界面')
53
59
  const doctorId = wx.getStorageSync('doctorId') || ''
54
- const userToken = wx.getStorageSync('userToken') || ''
55
60
  cache.cache({
56
- doctorId: doctorId,
57
- userToken: userToken
61
+ doctorId: doctorId
58
62
  })
59
63
  if (!doctorId) {
60
64
  this.setData({
@@ -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'
@@ -90,17 +90,43 @@ function setUserToken(token) {
90
90
  })
91
91
  }
92
92
 
93
- /** 外部预先写入的 userToken 优先;医生信息回来后仍可回退到 doctor.userToken */
93
+ /**
94
+ * userToken 由宿主通过 hhDoctor.setUserToken 传入(同时写内存和 storage)。
95
+ * 顺序:内存 > 医生信息 > storage > 本次进入的链接参数。
96
+ * 最后一级给「专家从微信消息短链直接进入」用:短链里的 userToken 落在 enter/launch options 的 query 上,
97
+ * 与 App 拼 H5 链接的传参方式一致(userToken / hhusertoken 两个 key 都认)。
98
+ */
94
99
  function userToken() {
95
100
  const preset = getValue('userToken')
96
101
  if (preset) {
97
102
  return preset
98
103
  }
99
104
  const doctorInfo = doctor()
100
- if (doctorInfo) {
105
+ if (doctorInfo && doctorInfo.userToken) {
101
106
  return doctorInfo.userToken
102
107
  }
103
- return null
108
+ return storedUserToken() || linkUserToken() || null
109
+ }
110
+
111
+ function storedUserToken() {
112
+ try {
113
+ return wx.getStorageSync('userToken') || ''
114
+ } catch (e) {
115
+ return ''
116
+ }
117
+ }
118
+
119
+ function linkUserToken() {
120
+ const pick = (options) => {
121
+ const query = (options && options.query) || {}
122
+ return query.userToken || query.hhusertoken || ''
123
+ }
124
+ try {
125
+ const enter = wx.getEnterOptionsSync && wx.getEnterOptionsSync()
126
+ return pick(enter) || pick(wx.getLaunchOptionsSync && wx.getLaunchOptionsSync())
127
+ } catch (e) {
128
+ return ''
129
+ }
104
130
  }
105
131
 
106
132
  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.1",
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
  }