hh-doctor-sdk 0.8.8 → 0.9.0

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.
@@ -51,9 +51,10 @@ Page({
51
51
  if (judgeLogin) {
52
52
  wx.statLog.sendStatLog(null, '打开咨询单列表界面')
53
53
  const doctorId = wx.getStorageSync('doctorId') || ''
54
- // console.error(`doctorId => ${doctorId}`)
54
+ const userToken = wx.getStorageSync('userToken') || ''
55
55
  cache.cache({
56
- doctorId: doctorId
56
+ doctorId: doctorId,
57
+ userToken: userToken
57
58
  })
58
59
  if (!doctorId) {
59
60
  this.setData({
@@ -1,5 +1,5 @@
1
1
  const api = require('./apis/order-app-api')
2
- const { doctor } = require('./public/cache-data')
2
+ const cache = require('./public/cache-data')
3
3
 
4
4
  function initEventBus() {
5
5
  if (wx.eventBus) {
@@ -37,6 +37,17 @@ function setLoginCheck(judgeLogin, directLogin) {
37
37
  wx.loginConfig.directLogin = directLogin
38
38
  }
39
39
 
40
+ /** 宿主在拉医生信息前写入 userToken,order 请求和换 session 都读这份 */
41
+ function setUserToken(userToken) {
42
+ const token = (userToken || '').trim()
43
+ cache.setUserToken(token)
44
+ if (token) {
45
+ wx.setStorageSync('userToken', token)
46
+ } else {
47
+ wx.removeStorageSync('userToken')
48
+ }
49
+ }
50
+
40
51
  /**
41
52
  * 检测医生信息是否认证完成
42
53
  * @param {*} doctorId 医生Id
@@ -86,6 +97,7 @@ module.exports = {
86
97
  initTim,
87
98
  initStatLog,
88
99
  setLoginCheck,
100
+ setUserToken,
89
101
  enterForground,
90
102
  enterBackground,
91
103
  isDoctorAuth
@@ -5,8 +5,6 @@ const appVersion = '7.1.6'
5
5
  const appID = '10423'
6
6
  const appKey = ''
7
7
  const appSecret = 'N0e7293NSF8nBZjO'
8
- const sessionId = '1776843236242882600'
9
- const sessionIdProd = '1792831747042312418'
10
8
 
11
9
  module.exports = {
12
10
  sdkVersion,
@@ -14,6 +12,4 @@ module.exports = {
14
12
  appID,
15
13
  appKey,
16
14
  appSecret,
17
- sessionId,
18
- sessionIdProd,
19
15
  }
@@ -84,7 +84,18 @@ function uuid() {
84
84
  return null
85
85
  }
86
86
 
87
+ function setUserToken(token) {
88
+ cache({
89
+ userToken: token || ''
90
+ })
91
+ }
92
+
93
+ /** 外部预先写入的 userToken 优先;医生信息回来后仍可回退到 doctor.userToken */
87
94
  function userToken() {
95
+ const preset = getValue('userToken')
96
+ if (preset) {
97
+ return preset
98
+ }
88
99
  const doctorInfo = doctor()
89
100
  if (doctorInfo) {
90
101
  return doctorInfo.userToken
@@ -134,5 +145,6 @@ module.exports = {
134
145
  getLogId,
135
146
  clearLogId,
136
147
  isRequest,
148
+ setUserToken,
137
149
  userToken,
138
150
  }
@@ -7,6 +7,12 @@ const cache = require('./cache-data')
7
7
  */
8
8
  function loadDoctor(options, callback) {
9
9
  const doctorId = options.doctorId
10
+ if (!cache.userToken()) {
11
+ const storedToken = wx.getStorageSync('userToken') || ''
12
+ if (storedToken) {
13
+ cache.setUserToken(storedToken)
14
+ }
15
+ }
10
16
  if (!doctorId) {
11
17
  wx.statLog.sendStatLog(null, `场景:${options.scence || '未知'} 未传必要的参数`)
12
18
  wx.showToast({
@@ -2,6 +2,7 @@ const sha1 = require('./checksumUtils')
2
2
  const cache = require('../public/cache-data')
3
3
  const appConfig = require('../public/app-config')
4
4
  const hostDefine = require('../apis/api-host-define')
5
+ const orderSession = require('./orderSession')
5
6
 
6
7
  var cookie = ''
7
8
 
@@ -26,6 +27,33 @@ function httpGet(request, params, data) {
26
27
  }
27
28
 
28
29
  function _request(request, method, params, data) {
30
+ if (request.hostType !== hostDefine.hostTypes.order) {
31
+ return _send(request, method, params, data, '')
32
+ }
33
+ return _sendWithSession(request, method, params, data, false)
34
+ }
35
+
36
+ // 会话被拒绝时清掉并换一次重试;换不到不阻断,交给服务端按 402 返回
37
+ function _sendWithSession(request, method, params, data, retried) {
38
+ return orderSession.ensure()
39
+ .catch((err) => {
40
+ console.warn('orderapp session 换取失败,按无会话继续', {
41
+ doctorId: cache.doctorId(),
42
+ err: err && err.message
43
+ })
44
+ return ''
45
+ })
46
+ .then((sessionId) => _send(request, method, params, data, sessionId))
47
+ .catch((err) => {
48
+ if (!retried && orderSession.rejected(err)) {
49
+ orderSession.clear('status=' + err.status)
50
+ return _sendWithSession(request, method, params, data, true)
51
+ }
52
+ return Promise.reject(err)
53
+ })
54
+ }
55
+
56
+ function _send(request, method, params, data, sessionId) {
29
57
  return new Promise((resolve, reject) => {
30
58
  let host = hostDefine.host(request.hostType)
31
59
  let url = `${host}${request.path}`
@@ -39,6 +67,9 @@ function _request(request, method, params, data) {
39
67
  fullParams.app_version = appConfig.appVersion
40
68
  fullParams.actionSource = 'wxdoctor'
41
69
  fullParams.device_token = ''
70
+ if (sessionId) {
71
+ fullParams.session_id = sessionId
72
+ }
42
73
  if (doctorId) {
43
74
  fullParams.doctor_id = doctorId
44
75
  fullParams.user_id = doctorId
@@ -49,17 +80,6 @@ function _request(request, method, params, data) {
49
80
  if (userToken) {
50
81
  fullParams.userToken = userToken
51
82
  }
52
- // 安全获取环境配置
53
- let profileName = 'prod'
54
- try {
55
- const app = getApp()
56
- if (app && app.globalData && app.globalData.profileName) {
57
- profileName = app.globalData.profileName
58
- }
59
- } catch (error) {
60
- console.warn('获取环境配置失败,使用默认生产环境:', error)
61
- }
62
- fullParams.session_id = (profileName === 'test') ? appConfig.sessionId : appConfig.sessionIdProd
63
83
  fullParams.random = new Date().getTime()
64
84
 
65
85
  for (let [key, value] of Object.entries(fullParams)) {
@@ -143,4 +163,4 @@ function setCookie(value) {
143
163
  module.exports = {
144
164
  post: httpPost,
145
165
  get: httpGet,
146
- }
166
+ }
@@ -0,0 +1,113 @@
1
+ const cache = require('../public/cache-data')
2
+ const hostDefine = require('../apis/api-host-define')
3
+
4
+ // 服务端 90 分钟滑动过期;客户端提前换,避免踩着过期点发请求
5
+ const CACHE_KEY = 'hh_doctor_order_session'
6
+ const TTL_MILLIS = 80 * 60 * 1000
7
+ const LOGIN_PATH = '/login/loginByUUIDWithWX'
8
+
9
+ let pending = null
10
+
11
+ function doctorId() {
12
+ return cache.doctorId() || ''
13
+ }
14
+
15
+ function read() {
16
+ try {
17
+ const cached = wx.getStorageSync(CACHE_KEY)
18
+ if (cached && cached.expireAt > Date.now() && cached.userId === doctorId()) {
19
+ return cached.sessionId
20
+ }
21
+ } catch (e) {
22
+ console.warn('orderapp session 读取失败', { doctorId: doctorId(), err: e && e.message })
23
+ }
24
+ return ''
25
+ }
26
+
27
+ function clear(reason) {
28
+ try {
29
+ wx.removeStorageSync(CACHE_KEY)
30
+ } catch (e) {
31
+ console.warn('orderapp session 清理失败', { doctorId: doctorId(), err: e && e.message })
32
+ }
33
+ if (reason) {
34
+ console.warn('orderapp session 已失效', { reason: reason, doctorId: doctorId() })
35
+ }
36
+ }
37
+
38
+ function ensure() {
39
+ const cached = read()
40
+ return cached ? Promise.resolve(cached) : exchange()
41
+ }
42
+
43
+ // 同一医生的并发请求共用一次换取;换到的 session 只记在发起时的医生名下
44
+ function exchange() {
45
+ const currentDoctorId = doctorId()
46
+ const userToken = cache.userToken()
47
+ if (!userToken) {
48
+ console.warn('orderapp session 未换取: 缺少 userToken', { doctorId: currentDoctorId })
49
+ return Promise.reject(new Error('缺少 userToken'))
50
+ }
51
+ if (pending && pending.doctorId === currentDoctorId) {
52
+ return pending.promise
53
+ }
54
+
55
+ const started = Date.now()
56
+ const url = `${hostDefine.host(hostDefine.hostTypes.order)}${LOGIN_PATH}?userToken=${encodeURIComponent(userToken)}`
57
+ const promise = new Promise((resolve, reject) => {
58
+ wx.request({
59
+ url: url,
60
+ method: 'POST',
61
+ data: {},
62
+ header: { 'content-type': 'application/json', 'APPTYPE': 'pc' },
63
+ success: function (res) {
64
+ const body = res.data || {}
65
+ // sessionId 是 19 位 Long,JS 解析会丢精度,必须取字符串字段 sSessionId
66
+ const sessionId = body.data && body.data.sSessionId
67
+ if (body.status == 200 && sessionId) {
68
+ wx.setStorageSync(CACHE_KEY, {
69
+ sessionId: sessionId,
70
+ userId: currentDoctorId,
71
+ expireAt: Date.now() + TTL_MILLIS
72
+ })
73
+ console.info('orderapp session 已换取', {
74
+ doctorId: currentDoctorId,
75
+ elapsed_ms: Date.now() - started
76
+ })
77
+ resolve(sessionId)
78
+ return
79
+ }
80
+ reject(new Error(body.error_msg || '换取 orderapp session 失败'))
81
+ },
82
+ fail: function (err) {
83
+ console.warn('orderapp session 换取失败', {
84
+ doctorId: currentDoctorId,
85
+ err: err && err.errMsg,
86
+ elapsed_ms: Date.now() - started
87
+ })
88
+ reject(err)
89
+ },
90
+ })
91
+ })
92
+ promise.then(function () {
93
+ if (pending && pending.promise === promise) {
94
+ pending = null
95
+ }
96
+ }, function () {
97
+ if (pending && pending.promise === promise) {
98
+ pending = null
99
+ }
100
+ })
101
+ pending = { doctorId: currentDoctorId, promise: promise }
102
+ return promise
103
+ }
104
+
105
+ function rejected(body) {
106
+ return !!(body && (body.status == 402 || body.status == 600))
107
+ }
108
+
109
+ module.exports = {
110
+ ensure,
111
+ clear,
112
+ rejected,
113
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hh-doctor-sdk",
3
- "version": "0.8.8",
3
+ "version": "0.9.0",
4
4
  "description": "和缓名医小程序SDK",
5
5
  "scripts": {
6
6
  "test": "echo \"Error: no test specified\" && exit 1"
@@ -1,129 +0,0 @@
1
- # 宿主小程序 · 腾讯云即时通信 IM 服务器域名配置
2
-
3
- > 发给运维/小程序管理员,在 **宿主小程序**(接入 `hh-doctor-sdk` 的小程序)公众平台配置。
4
- > **不是**和缓业务 API 域名(如 `sec.hh-medic.com`),本文仅覆盖 **腾讯 IM**。
5
-
6
- ---
7
-
8
- ## 1. 背景与依据
9
-
10
- | 项 | 说明 |
11
- |----|------|
12
- | SDK 形态 | 内置 `tim-wx.js`(等同 npm **`tim-wx-sdk@2.27.6`**) |
13
- | IM SDKAppID | **`1400073238`**(与 `hh-doctor-sdk` 内 `im-manager.js` 一致) |
14
- | 配置入口 | [微信公众平台](https://mp.weixin.qq.com) → **开发 → 开发管理 → 开发设置 → 服务器域名** |
15
- | 官方域名表 | [即时通信 IM - 小程序域名配置](https://cloud.tencent.com/document/product/269/117335)(与 2.x 小程序 TIM 通用;V4 文档表最全) |
16
- | 典型现象 | 体验版 **关闭调试** 后 IM 推送不生效、医助回复不刷新;**开启调试** 正常 → 多为 **socket 合法域名未配** |
17
-
18
- 说明:体验版/正式版 **不会** 像开发者工具那样「不校验合法域名」,必须配齐下列白名单。
19
-
20
- ---
21
-
22
- ## 2. 配置优先级
23
-
24
- | 优先级 | 类型 | 用途 |
25
- |--------|------|------|
26
- | **P0 必配** | **socket 合法域名** | IM 长连接 WebSocket,收自定义消息(如 `doctorReply`) |
27
- | **P0 必配** | **request 合法域名** | IM 登录、拉配置、上报等 HTTPS |
28
- | P1 按需 | uploadFile 合法域名 | IM 内发送图片/文件/语音等 |
29
- | P1 按需 | downloadFile 合法域名 | IM 内下载媒体资源 |
30
-
31
- 当前名医 SDK 场景:**医助回复刷新** 至少完成 **P0(socket + request)**。
32
-
33
- ---
34
-
35
- ## 3. socket 合法域名(P0,共 5 个)
36
-
37
- 在「**socket 合法域名**」中新增(保留原有业务域名,勿删):
38
-
39
- ```
40
- wss://wss.im.tencent.cn
41
- wss://wssv6.im.qcloud.com
42
- wss://wss.tim.qq.com
43
- wss://wss.im.qcloud.com
44
- wss://1400073238w4c.my-imcloud.com
45
- ```
46
-
47
- > 最后一条为 SDKAppID **1400073238** 的 IM 独立域名(文档:`wss://${SDKAppID}w4c.my-imcloud.com`)。
48
-
49
- ---
50
-
51
- ## 4. request 合法域名(P0,共 12 个)
52
-
53
- 在「**request 合法域名**」中新增:
54
-
55
- ```
56
- https://web.sdk.qcloud.com
57
- https://boce-cdn.my-imcloud.com
58
- https://api.im.qcloud.com
59
- https://events.im.qcloud.com
60
- https://webim.tim.qq.com
61
- https://wss.im.qcloud.com
62
- https://wssv6.im.qcloud.com
63
- https://wss.tim.qq.com
64
- https://web.sdk.cloud.tencent.cn
65
- https://web.sdk.tencent.cn
66
- https://wss.im.tencent.cn
67
- https://1400073238w4c.my-imcloud.com
68
- ```
69
-
70
- > 最后一条为 SDKAppID **1400073238** 的 IM 独立 HTTPS 域名。
71
-
72
- ---
73
-
74
- ## 5. uploadFile 合法域名(P1,IM 发图/文件时需要)
75
-
76
- ```
77
- https://cnv6.rich.my-imcloud.com
78
- https://1400073238-cn.rich.my-imcloud.com
79
- https://cn.rich.my-imcloud.com
80
- https://cn.imrich.qcloud.com
81
- https://cos.ap-shanghai.myqcloud.com
82
- https://cos.ap-shanghai.tencentcos.cn
83
- https://cos.ap-guangzhou.myqcloud.com
84
- ```
85
-
86
- ---
87
-
88
- ## 6. downloadFile 合法域名(P1,IM 下载媒体时需要)
89
-
90
- ```
91
- https://cnv6.rich.my-imcloud.com
92
- https://1400073238-cn.rich.my-imcloud.com
93
- https://cn.rich.my-imcloud.com
94
- https://cn.imrich.qcloud.com
95
- https://cos.ap-shanghai.myqcloud.com
96
- https://cos.ap-shanghai.tencentcos.cn
97
- https://cos.ap-guangzhou.myqcloud.com
98
- ```
99
-
100
- ---
101
-
102
- ## 7. 操作注意
103
-
104
- 1. **配置对象**:必须是 **宿主小程序** AppID,不是和缓名医 SDK 演示工程。
105
- 2. **保存后**:等待约 **5~10 分钟** 再测;体验版请 **关闭「打开调试」** 验证。
106
- 3. **第三方代发布**:若小程序由第三方平台发版,域名可能以 **第三方平台账号下登记的域名** 为准,需在第三方侧同步添加上述域名。
107
- 4. **额度**:服务器域名每月修改次数有限(微信规则),建议一次性按上表合并提交。
108
- 5. **格式**:按腾讯文档填写 **带协议** 的完整域名(`https://` / `wss://`);`wss` 域名 **无需** 配置端口。
109
- 6. **与业务域名关系**:和缓订单、图片等 **业务 request 域名** 仍需单独保留,本文列表 **追加** 即可。
110
-
111
- ---
112
-
113
- ## 8. 验收建议
114
-
115
- 1. 体验版 **关调试**,进入咨询详情页,等待 IM 登录(可配合实时日志/statLog:`登录腾讯IM完成`、`IM SDK Ready`)。
116
- 2. 医助回复后,详情页应自动刷新信息流(自定义消息 `doctorReply`)。
117
- 3. 若仍失败:在 vConsole/实时日志中查看是否有 **socket / 域名不在合法列表** 类报错。
118
-
119
- ---
120
-
121
- ## 9. 参考链接
122
-
123
- - [腾讯云 IM - 小程序 & uni-app(域名配置章节)](https://cloud.tencent.com/document/product/269/117335)
124
- - [微信 - 小程序网络与服务器域名](https://developers.weixin.qq.com/miniprogram/dev/framework/ability/network.html)
125
- - [tim-wx-sdk 2.27.6(npm)](https://www.npmjs.com/package/tim-wx-sdk/v/2.27.6)
126
-
127
- ---
128
-
129
- 文档整理:和缓名医小程序 SDK 项目组 · 对应 `hh-doctor-sdk` 内置 TIM **2.27.6**