Files
ruo-shan-cloud-pharmacy/miniprogram/package-live/volc-mini-sdk/utils/socket.js
T
陈誉午 994b267f5c feat: 电子药箱小程序 - 4Tab药品管理
- 电子药箱: 药品库存管理 + 手动录入 + 今日用药打卡
- 药品百科: 搜索 + 分类筛选 + 20种药品静态数据
- 惠教中心: 4条药品使用指南课程
- 我的: 家庭成员信息管理
- 自定义TabBar + navigation-bar组件
- SVG药品分类插图
2026-07-29 11:20:52 +08:00

1 line
18 KiB
JavaScript

"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.default=exports.WsMethod=void 0;var _fws=require("../lib/fws");function _regeneratorRuntime(){_regeneratorRuntime=function(){return e};var e={},t=Object.prototype,r=t.hasOwnProperty,n=Object.defineProperty||function(e,t,r){e[t]=r.value},o="function"==typeof Symbol?Symbol:{},i=o.iterator||"@@iterator",a=o.asyncIterator||"@@asyncIterator",s=o.toStringTag||"@@toStringTag";function c(e,t,r){return Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}),e[t]}try{c({},"")}catch(e){c=function(e,t,r){return e[t]=r}}function u(e,t,r,o){var i=t&&t.prototype instanceof f?t:f,a=Object.create(i.prototype),s=new T(o||[]);return n(a,"_invoke",{value:S(e,r,s)}),a}function l(e,t,r){try{return{type:"normal",arg:e.call(t,r)}}catch(e){return{type:"throw",arg:e}}}e.wrap=u;var d={};function f(){}function h(){}function y(){}var p={};c(p,i,function(){return this});var v=Object.getPrototypeOf,g=v&&v(v(P([])));g&&g!==t&&r.call(g,i)&&(p=g);var m=y.prototype=f.prototype=Object.create(p);function _(e){["next","throw","return"].forEach(function(t){c(e,t,function(e){return this._invoke(t,e)})})}function b(e,t){function o(n,i,a,s){var c=l(e[n],e,i);if("throw"!==c.type){var u=c.arg,d=u.value;return d&&"object"==_typeof(d)&&r.call(d,"__await")?t.resolve(d.__await).then(function(e){o("next",e,a,s)},function(e){o("throw",e,a,s)}):t.resolve(d).then(function(e){u.value=e,a(u)},function(e){return o("throw",e,a,s)})}s(c.arg)}var i;n(this,"_invoke",{value:function(e,r){function n(){return new t(function(t,n){o(e,r,t,n)})}return i=i?i.then(n,n):n()}})}function S(e,t,r){var n="suspendedStart";return function(o,i){if("executing"===n)throw new Error("Generator is already running");if("completed"===n){if("throw"===o)throw i;return{value:void 0,done:!0}}for(r.method=o,r.arg=i;;){var a=r.delegate;if(a){var s=w(a,r);if(s){if(s===d)continue;return s}}if("next"===r.method)r.sent=r._sent=r.arg;else if("throw"===r.method){if("suspendedStart"===n)throw n="completed",r.arg;r.dispatchException(r.arg)}else"return"===r.method&&r.abrupt("return",r.arg);n="executing";var c=l(e,t,r);if("normal"===c.type){if(n=r.done?"completed":"suspendedYield",c.arg===d)continue;return{value:c.arg,done:r.done}}"throw"===c.type&&(n="completed",r.method="throw",r.arg=c.arg)}}}function w(e,t){var r=t.method,n=e.iterator[r];if(void 0===n)return t.delegate=null,"throw"===r&&e.iterator.return&&(t.method="return",t.arg=void 0,w(e,t),"throw"===t.method)||"return"!==r&&(t.method="throw",t.arg=new TypeError("The iterator does not provide a '"+r+"' method")),d;var o=l(n,e.iterator,t.arg);if("throw"===o.type)return t.method="throw",t.arg=o.arg,t.delegate=null,d;var i=o.arg;return i?i.done?(t[e.resultName]=i.value,t.next=e.nextLoc,"return"!==t.method&&(t.method="next",t.arg=void 0),t.delegate=null,d):i:(t.method="throw",t.arg=new TypeError("iterator result is not an object"),t.delegate=null,d)}function k(e){var t={tryLoc:e[0]};1 in e&&(t.catchLoc=e[1]),2 in e&&(t.finallyLoc=e[2],t.afterLoc=e[3]),this.tryEntries.push(t)}function E(e){var t=e.completion||{};t.type="normal",delete t.arg,e.completion=t}function T(e){this.tryEntries=[{tryLoc:"root"}],e.forEach(k,this),this.reset(!0)}function P(e){if(e){var t=e[i];if(t)return t.call(e);if("function"==typeof e.next)return e;if(!isNaN(e.length)){var n=-1,o=function t(){for(;++n<e.length;)if(r.call(e,n))return t.value=e[n],t.done=!1,t;return t.value=void 0,t.done=!0,t};return o.next=o}}return{next:O}}function O(){return{value:void 0,done:!0}}return h.prototype=y,n(m,"constructor",{value:y,configurable:!0}),n(y,"constructor",{value:h,configurable:!0}),h.displayName=c(y,s,"GeneratorFunction"),e.isGeneratorFunction=function(e){var t="function"==typeof e&&e.constructor;return!!t&&(t===h||"GeneratorFunction"===(t.displayName||t.name))},e.mark=function(e){return Object.setPrototypeOf?Object.setPrototypeOf(e,y):(e.__proto__=y,c(e,s,"GeneratorFunction")),e.prototype=Object.create(m),e},e.awrap=function(e){return{__await:e}},_(b.prototype),c(b.prototype,a,function(){return this}),e.AsyncIterator=b,e.async=function(t,r,n,o,i){void 0===i&&(i=Promise);var a=new b(u(t,r,n,o),i);return e.isGeneratorFunction(r)?a:a.next().then(function(e){return e.done?e.value:a.next()})},_(m),c(m,s,"Generator"),c(m,i,function(){return this}),c(m,"toString",function(){return"[object Generator]"}),e.keys=function(e){var t=Object(e),r=[];for(var n in t)r.push(n);return r.reverse(),function e(){for(;r.length;){var n=r.pop();if(n in t)return e.value=n,e.done=!1,e}return e.done=!0,e}},e.values=P,T.prototype={constructor:T,reset:function(e){if(this.prev=0,this.next=0,this.sent=this._sent=void 0,this.done=!1,this.delegate=null,this.method="next",this.arg=void 0,this.tryEntries.forEach(E),!e)for(var t in this)"t"===t.charAt(0)&&r.call(this,t)&&!isNaN(+t.slice(1))&&(this[t]=void 0)},stop:function(){this.done=!0;var e=this.tryEntries[0].completion;if("throw"===e.type)throw e.arg;return this.rval},dispatchException:function(e){if(this.done)throw e;var t=this;function n(r,n){return a.type="throw",a.arg=e,t.next=r,n&&(t.method="next",t.arg=void 0),!!n}for(var o=this.tryEntries.length-1;o>=0;--o){var i=this.tryEntries[o],a=i.completion;if("root"===i.tryLoc)return n("end");if(i.tryLoc<=this.prev){var s=r.call(i,"catchLoc"),c=r.call(i,"finallyLoc");if(s&&c){if(this.prev<i.catchLoc)return n(i.catchLoc,!0);if(this.prev<i.finallyLoc)return n(i.finallyLoc)}else if(s){if(this.prev<i.catchLoc)return n(i.catchLoc,!0)}else{if(!c)throw new Error("try statement without catch or finally");if(this.prev<i.finallyLoc)return n(i.finallyLoc)}}}},abrupt:function(e,t){for(var n=this.tryEntries.length-1;n>=0;--n){var o=this.tryEntries[n];if(o.tryLoc<=this.prev&&r.call(o,"finallyLoc")&&this.prev<o.finallyLoc){var i=o;break}}i&&("break"===e||"continue"===e)&&i.tryLoc<=t&&t<=i.finallyLoc&&(i=null);var a=i?i.completion:{};return a.type=e,a.arg=t,i?(this.method="next",this.next=i.finallyLoc,d):this.complete(a)},complete:function(e,t){if("throw"===e.type)throw e.arg;return"break"===e.type||"continue"===e.type?this.next=e.arg:"return"===e.type?(this.rval=this.arg=e.arg,this.method="return",this.next="end"):"normal"===e.type&&t&&(this.next=t),d},finish:function(e){for(var t=this.tryEntries.length-1;t>=0;--t){var r=this.tryEntries[t];if(r.finallyLoc===e)return this.complete(r.completion,r.afterLoc),E(r),d}},catch:function(e){for(var t=this.tryEntries.length-1;t>=0;--t){var r=this.tryEntries[t];if(r.tryLoc===e){var n=r.completion;if("throw"===n.type){var o=n.arg;E(r)}return o}}throw new Error("illegal catch attempt")},delegateYield:function(e,t,r){return this.delegate={iterator:P(e),resultName:t,nextLoc:r},"next"===this.method&&(this.arg=void 0),d}},e}function asyncGeneratorStep(e,t,r,n,o,i,a){try{var s=e[i](a),c=s.value}catch(e){return void r(e)}s.done?t(c):Promise.resolve(c).then(n,o)}function _asyncToGenerator(e){return function(){var t=this,r=arguments;return new Promise(function(n,o){var i=e.apply(t,r);function a(e){asyncGeneratorStep(i,n,o,a,s,"next",e)}function s(e){asyncGeneratorStep(i,n,o,a,s,"throw",e)}a(void 0)})}}function ownKeys(e,t){var r=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);t&&(n=n.filter(function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable})),r.push.apply(r,n)}return r}function _objectSpread(e){for(var t=1;t<arguments.length;t++){var r=null!=arguments[t]?arguments[t]:{};t%2?ownKeys(Object(r),!0).forEach(function(t){_defineProperty(e,t,r[t])}):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(r)):ownKeys(Object(r)).forEach(function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(r,t))})}return e}function _defineProperty(e,t,r){return(t=_toPropertyKey(t))in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function _classCallCheck(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function _defineProperties(e,t){for(var r=0;r<t.length;r++){var n=t[r];n.enumerable=n.enumerable||!1,n.configurable=!0,"value"in n&&(n.writable=!0),Object.defineProperty(e,_toPropertyKey(n.key),n)}}function _createClass(e,t,r){return t&&_defineProperties(e.prototype,t),r&&_defineProperties(e,r),Object.defineProperty(e,"prototype",{writable:!1}),e}function _toPropertyKey(e){var t=_toPrimitive(e,"string");return"symbol"===_typeof(t)?t:String(t)}function _toPrimitive(e,t){if("object"!==_typeof(e)||null===e)return e;var r=e[Symbol.toPrimitive];if(void 0!==r){var n=r.call(e,t||"default");if("object"!==_typeof(n))return n;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===t?String:Number)(e)}function _typeof(e){return _typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e},_typeof(e)}var md5=require("../lib/md5"),pakoLib=require("../lib/pako_inflate.js"),getEnv=function(){var e=wx.getStorageSync("env");return e?JSON.parse(e):{envName:"prod"}},hostMap={prod:"wss://frontier.snssdk.com/ws/v2",staging:"wss://frontier.snssdk.com/ws/v2",boe:"wss://frontier-boe.bytedance.net/ws/v2"},getReqHost=function(){var e=getEnv();return hostMap[e.envName]},WsMethod={miniConnect:4,PLAYER_UPDATE_LINE:32,linkUserMediaStatus:51,linkLayoutUpdate:52,closeAudienceLink:55,linkUserEnterRoom:58,applyUserMedia:59,linkMute:60,linkUserNameUpdate:61,imageTextUpdate:25,imageTextRedPocketUpdate:40,imageTextQuizUpdate:46,imageTextLotteryOrCheckInUpdate:27,NOTIFICATION_MESSAGE:34,POLLING_MESSAGE:110,COUPON_STATUS_CHANGE:120,ORDER_STATUS_CHANGE:121,CARD_HOT_SELL_MSG_CHANGE:122,ACTIVITY_BONUS_TASK_FINISHED:124,ACTIVITY_BONUS_TASK_AUTO_RECEIVE:125,KICK_OUT_MESSAGE:130,MENU_CONFIG_UPDATE:131,KICK_OUT_V2_MESSAGE:132,EMOJI_UPDATE:85,AUDIENCE_LEVEL_INFO:136};exports.WsMethod=WsMethod;var DEBUG_WS_METHOD_SET=new Set([String(WsMethod.KICK_OUT_MESSAGE),String(WsMethod.MENU_CONFIG_UPDATE),String(WsMethod.KICK_OUT_V2_MESSAGE)]),normalizeMethodKey=function(e){return String(null!=e?e:"").trim()||""},isArrayBufferView=function(e){return!(!e||"object"!==_typeof(e))&&("function"==typeof ArrayBuffer.isView?ArrayBuffer.isView(e):e.buffer instanceof ArrayBuffer&&"number"==typeof e.byteLength)},isPlainObjectPayload=function(e){return!(!e||"object"!==_typeof(e)||e instanceof ArrayBuffer||isArrayBufferView(e)||"[object Object]"!==Object.prototype.toString.call(e))},looksLikeFrontierMenuPayload=function(e){return!!isPlainObjectPayload(e)&&("ActivityId"in e||"RefreshByPull"in e||"Base"in e||"base"in e||"Body"in e||"body"in e||"Patch"in e||"patch"in e||"Menus"in e||"CommentConfig"in e||"InteractiveSession"in e||"ImageTextConfig"in e)},safeParsePayloadText=function(e){if("string"!=typeof e)return e;try{return JSON.parse(e)}catch(t){return e}},isGzJsonPayload=function(e){return[e.payloadType,e.payloadEncoding,e.type].some(function(e){return"gz_json"===String(e||"").toLowerCase()})},normalizeDecodedGzJsonPayload=function(e){return looksLikeFrontierMenuPayload(e)||isPlainObjectPayload(e)?e:"string"==typeof e?safeParsePayloadText(e):void 0},WS=function(){function e(t,r){_classCallCheck(this,e),this.config=_objectSpread({},t),this.isDestroyed=!1,this.isOpen=!1,this.pingTimeoutId=null,this.pongTimeoutId=null,this.pingTimeoutDelay=2e3,this.pongTimeoutDelay=2e3,this.PINGPONGMESSAGE="PINGPONGMESSAGE",this.timeoutLimit=3,this.timeoutCount=0,this.connectStartMs=0,this.methodHandler=new Map,this.report=r,this.initConfig(),this.createSocket()}var t;return _createClass(e,[{key:"initConfig",value:function(){var e,t;this.device_id=Number(Date.now()+(1e3*Math.random()).toFixed(0)),this.app_key="434ce15c19b9978a184b8da842caadfb",this.salt="f8a69f1719916z",this.baseConfig={fpid:218,aid:2098,device_id:this.device_id,webControl:2,access_key:md5.hexMD5(String(218)+String(this.app_key)+String(this.device_id)+String(this.salt)),userId:null===(e=this.config)||void 0===e?void 0:e.userID,browser_device_id:null===(t=this.config)||void 0===t?void 0:t.userUniqueId},this.host=getReqHost(),this.report("mini_sdk_ws_init",{device_id:this.device_id})}},{key:"getActivityConfig",value:function(){var e=this.config,t=e.userID;return{activityId:e.activityId,activityStatus:e.activityStatus,UserID:t}}},{key:"getSocketUrl",value:function(){return urlAddQueryDeal(this.host,_objectSpread(_objectSpread({},this.baseConfig),this.getActivityConfig()))}},{key:"createSocket",value:(t=_asyncToGenerator(_regeneratorRuntime().mark(function e(){var t,r=this;return _regeneratorRuntime().wrap(function(e){for(;;)switch(e.prev=e.next){case 0:if(!this.isDestroyed){e.next=2;break}return e.abrupt("return");case 2:return this.socketTask&&(this.socketTask.close(),this.socketTask=null),t=this.getSocketUrl(),this.connectStartMs=Date.now(),this.report("mini_sdk_ws_connect",{device_id:this.device_id,url:t}),e.next=8,wx.connectSocket({url:t,complete:function(e){r.report("mini_sdk_ws_connect_complete",{device_id:r.device_id,url:t,errMsg:null==e?void 0:e.errMsg,isDestroyed:r.isDestroyed})}});case 8:this.socketTask=e.sent,this.bindSocketEvents();case 10:case"end":return e.stop()}},e,this)})),function(){return t.apply(this,arguments)})},{key:"bindSocketEvents",value:function(){var e=this;this.socketTask.onOpen(function(t){var r,n,o=e.connectStartMs?Date.now()-e.connectStartMs:0;e.connectStartMs=0,e.report("mini_sdk_ws_open",{device_id:e.device_id,openResult:t,connectCostMs:o,isDestroyed:e.isDestroyed}),e.isDestroyed||(e.timeoutCount=0,e.isOpen=!0,e.startHeartCheck(),null===(r=e.config)||void 0===r||null===(n=r.onOpen)||void 0===n||n.call(r,e,{connectCostMs:o}))}),this.socketTask.onClose(function(t){var r,n;e.report("mini_sdk_ws_close",{device_id:e.device_id,detail:t,isDestroyed:e.isDestroyed}),e.isDestroyed||(e.isOpen=!1,e.heartStop(),null===(r=e.config)||void 0===r||null===(n=r.onClose)||void 0===n||n.call(r,e,t))}),this.socketTask.onError(function(t){var r,n;e.report("mini_sdk_ws_error",{device_id:e.device_id,detail:t,isDestroyed:e.isDestroyed}),e.isDestroyed||(e.isOpen=!1,e.heartStop(),null===(r=e.config)||void 0===r||null===(n=r.onError)||void 0===n||n.call(r,e,t))}),this.socketTask.onMessage(function(t){if(!e.isDestroyed&&(e.startHeartCheck(),"object"===_typeof(t.data)))try{var r=readArrayBuffer(t.data),n=r.method,o=r.eventPayload,i=normalizeMethodKey(n);DEBUG_WS_METHOD_SET.has(i)&&console.log("[mini-sdk] 你有新的ws消息,method=".concat(n),{method:n,payload:o,_module_:"basic"}),e.report("mini_sdk_ws_receive",{origin:t.data,data:r}),e.methodHandler.has(i)?e.methodHandler.get(i).forEach(function(e){e(o)}):DEBUG_WS_METHOD_SET.has(i)&&e.report("mini_sdk_ws_method_unmatched",{method:n,normalizedMethod:i,registeredMethods:Array.from(e.methodHandler.keys())})}catch(r){e.report("mini_sdk_ws_parsing_fail",{error:r,data:t.data}),console.log("解析参数报错",r,"ws data type=",_typeof(t.data))}})}},{key:"on",value:function(e,t){var r,n=normalizeMethodKey(e);if(n){var o=null!==(r=this.methodHandler.get(n))&&void 0!==r?r:[];o.push(t),this.methodHandler.set(n,o)}}},{key:"off",value:function(e){var t,r=arguments.length>1&&void 0!==arguments[1]?arguments[1]:void 0,n=normalizeMethodKey(e);if(n&&this.methodHandler.has(n))if(r){var o=null!==(t=this.methodHandler.get(n))&&void 0!==t?t:[],i=o.indexOf(r);i>-1&&o.splice(i,1),this.methodHandler.set(n,o)}else this.methodHandler.delete(n);else console.log("不存在callback")}},{key:"startHeartCheck",value:function(){this.heartStop(),this.heartStart()}},{key:"heartStart",value:function(){var e=this;clearTimeout(this.pingTimeoutId),this.pingTimeoutId=setTimeout(function(){e.socketTask.send({data:e.PINGPONGMESSAGE}),clearTimeout(e.pongTimeoutId),e.pongTimeoutId=setTimeout(function(){e.heartTimeout()},e.pongTimeoutDelay)},this.pingTimeoutDelay)}},{key:"heartStop",value:function(){clearTimeout(this.pingTimeoutId),this.pingTimeoutId=null,clearTimeout(this.pongTimeoutId),this.pongTimeoutId=null}},{key:"heartTimeout",value:function(){if(console.warn("心跳超时"),++this.timeoutCount>=this.timeoutLimit)return console.warn("try 心跳超时,超过限制次数"),void this.createSocket();this.startHeartCheck()}},{key:"send",value:function(e,t){var r=this;if(this.socketTask&&!this.isDestroyed){var n=getEnv(),o={SeqID:1,LogID:Date.now(),service:"staging"===n.envName?20282:1,method:null!=e?e:0,payload_encoding:"text",payload_type:"tts",payload:JSON.stringify(t)},i=(0,_fws.encodeFrame)(o),a=i.buffer.slice(i.byteOffset,i.byteLength+i.byteOffset);this.socketTask.send({data:a,fail:function(e){r.report("mini_sdk_ws_send_fail",{error:e}),console.log("WS send Fail",e)},success:function(){r.report("mini_sdk_ws_send_success",{content:o}),console.log("WS send Success")}})}}},{key:"destroy",value:function(){var e,t,r=arguments.length>0&&void 0!==arguments[0]?arguments[0]:"manual";this.report("mini_sdk_ws_destroy",{reason:r}),this.isDestroyed=!0,this.heartStop();try{var n,o;null===(n=this.socketTask)||void 0===n||null===(o=n.close)||void 0===o||o.call(n)}catch(e){}null===(e=this.methodHandler)||void 0===e||null===(t=e.clear)||void 0===t||t.call(e),this.socketTask=null}}]),e}(),readArrayBuffer=function(e){var t=(0,_fws.decodeFrame)(new Uint8Array(e));try{var r;if(isGzJsonPayload(t)){var n=normalizeDecodedGzJsonPayload(t.payload);if(void 0!==n)return t.textPayload=n,t.payloadValue=n,t.eventPayload=n,t;if(null==pakoLib||!pakoLib.ungzip)return t.payloadParseError="missing gzip decoder",t.eventPayload={payloadType:t.payloadType,payloadEncoding:t.payloadEncoding,payload:t.payload},t;var o=t.payload instanceof Uint8Array?t.payload:new Uint8Array(t.payload),i=JSON.parse(pakoLib.ungzip(o,{to:"string"}));return t.textPayload=i,t.payloadValue=i,t.eventPayload=i,t}var a=(0,_fws.readU8a)(t.payload),s=safeParsePayloadText(a);return t.textPayload=s,t.eventPayload=s,Array.isArray(s)&&null!==(r=s[0])&&void 0!==r&&r.SendMsg?t.payloadValue=safeParsePayloadText(s[0].SendMsg):t.payloadValue=s,t}catch(e){return t.payloadParseError=e,t.eventPayload=void 0!==t.textPayload?t.textPayload:{payloadType:t.payloadType,payloadEncoding:t.payloadEncoding,payload:t.payload},t}},urlAddQueryDeal=function(e,t){var r="",n=e;return"object"===_typeof(t)&&Object.keys(t).length&&(Object.keys(t).forEach(function(e){r+="&".concat(e,"=").concat(t[e])}),n=e.includes("?")?"".concat(e).concat(r):"".concat(e,"?").concat(r.substring(1))),n},_default=WS;exports.default=_default;