@ray-js/api 1.2.4 → 1.2.6
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.
- package/lib/index.d.ts +1 -0
- package/lib/index.js +2 -1
- package/lib/panel/devInfo/index.d.ts +59 -0
- package/lib/panel/devInfo/index.js +177 -0
- package/lib/panel/i18n/index.d.ts +39 -0
- package/lib/panel/i18n/index.js +293 -0
- package/lib/panel/i18n/index.wechat.d.ts +39 -0
- package/lib/panel/i18n/index.wechat.js +302 -0
- package/lib/panel/index.d.ts +4 -0
- package/lib/panel/index.js +4 -0
- package/lib/panel/normalizeNetwork.d.ts +15 -0
- package/lib/panel/normalizeNetwork.js +25 -0
- package/lib/panel/publishDps.d.ts +27 -0
- package/lib/panel/publishDps.js +70 -0
- package/lib/panel/types/app.d.ts +14 -0
- package/lib/panel/types/app.js +1 -0
- package/lib/panel/types/devInfo.d.ts +106 -0
- package/lib/panel/types/devInfo.js +1 -0
- package/lib/panel/types/index.d.ts +3 -0
- package/lib/panel/types/index.js +3 -0
- package/lib/panel/types/utilities.d.ts +9 -0
- package/lib/panel/types/utilities.js +1 -0
- package/lib/panel/utils.d.ts +15 -0
- package/lib/panel/utils.js +17 -0
- package/package.json +5 -5
package/lib/index.d.ts
CHANGED
package/lib/index.js
CHANGED
@@ -0,0 +1,59 @@
|
|
1
|
+
import { DevInfo } from '../types/devInfo';
|
2
|
+
export interface InitPanelEnvironmentOptions {
|
3
|
+
/**
|
4
|
+
* @description 是否需要使用默认离线弹窗
|
5
|
+
* @default true
|
6
|
+
*/
|
7
|
+
useDefaultOffline?: boolean;
|
8
|
+
/**
|
9
|
+
* @description 蓝牙提示是否需要阻止交互
|
10
|
+
* @default false
|
11
|
+
*/
|
12
|
+
bleCover?: boolean;
|
13
|
+
/**
|
14
|
+
* @description 蓝牙及 toast 提示自定义顶部高度(自定义导航时使用)如: 100px,10vh等
|
15
|
+
* @default 0
|
16
|
+
*/
|
17
|
+
customTop?: string;
|
18
|
+
/**
|
19
|
+
*@description 蓝牙连接方式,默认0
|
20
|
+
* 0: 网关和app都需要,默认值,本地和网关两个途径任何一个可用均可生效
|
21
|
+
* 1: 仅 app, 只会判定本地是否在线, 以及本地连接是否成功
|
22
|
+
* 2: 仅网关连接, 只会判定网关是否在线,以及网关连接是否成功
|
23
|
+
* @default 0
|
24
|
+
*/
|
25
|
+
bleConnectType?: number;
|
26
|
+
/**
|
27
|
+
* @description 是否显示蓝牙连接状态提示
|
28
|
+
*
|
29
|
+
* @default true
|
30
|
+
* @version 2.10.4
|
31
|
+
*/
|
32
|
+
showBLEToast?: boolean;
|
33
|
+
/**
|
34
|
+
* @description 当前设备 id,默认为 undefined,表示自动从小程序的 query参数中获取
|
35
|
+
* @default undefined
|
36
|
+
*/
|
37
|
+
deviceId?: string;
|
38
|
+
/**
|
39
|
+
* @description 当前群组 id,默认为 undefined,表示自动从小程序的 query参数中获取
|
40
|
+
* @default undefined
|
41
|
+
*/
|
42
|
+
groupId?: string;
|
43
|
+
}
|
44
|
+
export declare function parseDevice(dev: ty.device.DeviceInfo | DevInfo): DevInfo;
|
45
|
+
/**
|
46
|
+
* 初始化面板环境下使用到的设备信息
|
47
|
+
* @returns Promise<DevInfo>
|
48
|
+
*/
|
49
|
+
export declare const initDevInfo: (option?: {
|
50
|
+
deviceId?: string;
|
51
|
+
groupId?: string;
|
52
|
+
}) => Promise<DevInfo>;
|
53
|
+
export declare const getDevInfo: () => DevInfo;
|
54
|
+
export declare const updateDevInfo: (devInfo: DevInfo) => void;
|
55
|
+
/**
|
56
|
+
* 绑定当前设备或群组基础事件
|
57
|
+
* @param config: InitPanelEnvironmentOptions 配置项
|
58
|
+
*/
|
59
|
+
export declare function initPanelEnvironment(options?: InitPanelEnvironmentOptions): Promise<void>;
|
@@ -0,0 +1,177 @@
|
|
1
|
+
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
|
2
|
+
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
|
3
|
+
import _regeneratorRuntime from "@babel/runtime/regenerator";
|
4
|
+
import "core-js/modules/es.object.to-string.js";
|
5
|
+
import "core-js/modules/web.dom-collections.for-each.js";
|
6
|
+
import "core-js/modules/es.promise.js";
|
7
|
+
import { getDeviceInfo, getLaunchOptionsSync, getGroupInfo, getApp, registerDeviceListListener, registerGroupChange, exitMiniProgram, subscribeDeviceRemoved, onDeviceRemoved } from '@ray-js/api';
|
8
|
+
// 本地化缓存 deviceInfo 方便随时调用
|
9
|
+
var __deviceInfo = null;
|
10
|
+
var defaultPanelEnvironmentOptions = {
|
11
|
+
useDefaultOffline: true,
|
12
|
+
bleCover: false,
|
13
|
+
customTop: null,
|
14
|
+
bleConnectType: 0,
|
15
|
+
showBLEToast: true
|
16
|
+
};
|
17
|
+
export function parseDevice(dev) {
|
18
|
+
if (!dev) return null; // @ts-ignore
|
19
|
+
|
20
|
+
var deviceInfo = _objectSpread({}, dev);
|
21
|
+
|
22
|
+
var schema = dev.schema;
|
23
|
+
var idCodes = {};
|
24
|
+
var codeIds = {};
|
25
|
+
schema.forEach(function (item) {
|
26
|
+
// @ts-ignore
|
27
|
+
var id = item.id,
|
28
|
+
code = item.code;
|
29
|
+
idCodes[id] = code;
|
30
|
+
codeIds[code] = id;
|
31
|
+
});
|
32
|
+
deviceInfo.idCodes = idCodes;
|
33
|
+
deviceInfo.codeIds = codeIds;
|
34
|
+
return deviceInfo;
|
35
|
+
}
|
36
|
+
/**
|
37
|
+
* 初始化面板环境下使用到的设备信息
|
38
|
+
* @returns Promise<DevInfo>
|
39
|
+
*/
|
40
|
+
|
41
|
+
export var initDevInfo = function (option) {
|
42
|
+
return new Promise(function (resolve, reject) {
|
43
|
+
if (__deviceInfo) {
|
44
|
+
resolve(__deviceInfo);
|
45
|
+
}
|
46
|
+
|
47
|
+
var data = getLaunchOptionsSync();
|
48
|
+
|
49
|
+
var _ref = option || data.query,
|
50
|
+
deviceId = _ref.deviceId,
|
51
|
+
groupId = _ref.groupId;
|
52
|
+
|
53
|
+
if (groupId) {
|
54
|
+
getGroupInfo({
|
55
|
+
groupId: groupId,
|
56
|
+
success: function success(groupInfo) {
|
57
|
+
__deviceInfo = groupInfo;
|
58
|
+
resolve(__deviceInfo);
|
59
|
+
}
|
60
|
+
});
|
61
|
+
} else {
|
62
|
+
getDeviceInfo({
|
63
|
+
deviceId: deviceId,
|
64
|
+
success: function success(deviceInfo) {
|
65
|
+
__deviceInfo = parseDevice(deviceInfo);
|
66
|
+
resolve(__deviceInfo);
|
67
|
+
},
|
68
|
+
fail: reject
|
69
|
+
});
|
70
|
+
}
|
71
|
+
});
|
72
|
+
};
|
73
|
+
export var getDevInfo = function () {
|
74
|
+
return __deviceInfo;
|
75
|
+
};
|
76
|
+
export var updateDevInfo = function (devInfo) {
|
77
|
+
__deviceInfo = devInfo;
|
78
|
+
};
|
79
|
+
/**
|
80
|
+
* 监听设备移除
|
81
|
+
*/
|
82
|
+
|
83
|
+
var handleDeviceRemoved = function () {
|
84
|
+
exitMiniProgram();
|
85
|
+
};
|
86
|
+
/**
|
87
|
+
* 绑定当前设备或群组基础事件
|
88
|
+
* @param config: InitPanelEnvironmentOptions 配置项
|
89
|
+
*/
|
90
|
+
|
91
|
+
|
92
|
+
export function initPanelEnvironment() {
|
93
|
+
return _initPanelEnvironment.apply(this, arguments);
|
94
|
+
}
|
95
|
+
|
96
|
+
function _initPanelEnvironment() {
|
97
|
+
_initPanelEnvironment = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
|
98
|
+
var options,
|
99
|
+
App,
|
100
|
+
_args = arguments;
|
101
|
+
return _regeneratorRuntime.wrap(function (_context) {
|
102
|
+
while (1) {
|
103
|
+
switch (_context.prev = _context.next) {
|
104
|
+
case 0:
|
105
|
+
options = _args.length > 0 && _args[0] !== undefined ? _args[0] : defaultPanelEnvironmentOptions;
|
106
|
+
|
107
|
+
if (getApp()) {
|
108
|
+
_context.next = 5;
|
109
|
+
break;
|
110
|
+
}
|
111
|
+
|
112
|
+
_context.next = 4;
|
113
|
+
return new Promise(function (resolve) {
|
114
|
+
setTimeout(function () {
|
115
|
+
resolve(initPanelEnvironment(options));
|
116
|
+
}, 0);
|
117
|
+
});
|
118
|
+
|
119
|
+
case 4:
|
120
|
+
return _context.abrupt("return");
|
121
|
+
|
122
|
+
case 5:
|
123
|
+
App = getApp(); // 基础库版本需要大于 2.6.0
|
124
|
+
// @ts-ignore
|
125
|
+
|
126
|
+
App.onReady = function (event) {
|
127
|
+
var _options$deviceId, _options$groupId;
|
128
|
+
|
129
|
+
var deviceId = (_options$deviceId = options.deviceId) !== null && _options$deviceId !== void 0 ? _options$deviceId : event.query.deviceId;
|
130
|
+
var groupId = (_options$groupId = options.groupId) !== null && _options$groupId !== void 0 ? _options$groupId : event.query.groupId;
|
131
|
+
var useDefaultOffline = options.useDefaultOffline; // @ts-ignore
|
132
|
+
|
133
|
+
if (ty.panel && ty.panel.initPanelKit && useDefaultOffline) {
|
134
|
+
// @ts-ignore
|
135
|
+
ty.panel.initPanelKit(_objectSpread({
|
136
|
+
deviceId: deviceId,
|
137
|
+
groupId: groupId
|
138
|
+
}, options));
|
139
|
+
} else {
|
140
|
+
// 否则使用 registerDeviceListListener 注册,删除上述条件方法
|
141
|
+
// 若需要能用离线逻辑,则需要在 global.config.ts 中添加 pageWrapper: ['@ray-js/ray-panel-wrapper/lib/page'],
|
142
|
+
if (deviceId) {
|
143
|
+
registerDeviceListListener({
|
144
|
+
deviceIdList: [deviceId]
|
145
|
+
});
|
146
|
+
}
|
147
|
+
|
148
|
+
if (groupId) {
|
149
|
+
registerGroupChange({
|
150
|
+
groupIdList: [groupId]
|
151
|
+
});
|
152
|
+
}
|
153
|
+
}
|
154
|
+
|
155
|
+
if (deviceId) {
|
156
|
+
subscribeDeviceRemoved({
|
157
|
+
deviceId: deviceId,
|
158
|
+
success: function success() {
|
159
|
+
onDeviceRemoved(handleDeviceRemoved);
|
160
|
+
}
|
161
|
+
});
|
162
|
+
}
|
163
|
+
}; // 使用 Promise 确保业务可以通过 initPanelEnvironment 的正常调用明确设备信息初始化完成
|
164
|
+
|
165
|
+
|
166
|
+
_context.next = 9;
|
167
|
+
return initDevInfo(options.deviceId || options.groupId ? options : undefined);
|
168
|
+
|
169
|
+
case 9:
|
170
|
+
case "end":
|
171
|
+
return _context.stop();
|
172
|
+
}
|
173
|
+
}
|
174
|
+
}, _callee);
|
175
|
+
}));
|
176
|
+
return _initPanelEnvironment.apply(this, arguments);
|
177
|
+
}
|
@@ -0,0 +1,39 @@
|
|
1
|
+
declare type LanguageMap = {
|
2
|
+
[key: string]: Record<string, string>;
|
3
|
+
};
|
4
|
+
export default class I18N {
|
5
|
+
[x: string]: any;
|
6
|
+
strings: LanguageMap;
|
7
|
+
defaultLang: string;
|
8
|
+
__language: string;
|
9
|
+
constructor(props: LanguageMap);
|
10
|
+
forceUpdateNetworkLang(productId: string): void;
|
11
|
+
mergeLanguage(L1: LanguageMap, L2: LanguageMap): LanguageMap;
|
12
|
+
isZh(language: string): boolean;
|
13
|
+
setLanguage(language: string): void;
|
14
|
+
buildLanguage(language: string): void;
|
15
|
+
_getBestMatchingLanguage(language: string, props: LanguageMap): any;
|
16
|
+
formatString(str: string, ...values: any[]): string;
|
17
|
+
formatValue(key: string, ...values: any[]): any;
|
18
|
+
_replaceAll(find: string, replace: string, str: string): string;
|
19
|
+
getDpLang(code: string | number, value?: undefined | boolean | string): any;
|
20
|
+
getDpName(code: string, defaultName: string): any;
|
21
|
+
getDpsLang(key: {
|
22
|
+
[key: string]: string;
|
23
|
+
}): {};
|
24
|
+
getLang(key: string, defaultString?: string): any;
|
25
|
+
/**
|
26
|
+
* 获取picker标题
|
27
|
+
* @param {*} dpCode
|
28
|
+
*/
|
29
|
+
getRangeStrings(dpCode: string, schema: Record<string, any>): {};
|
30
|
+
/**
|
31
|
+
* 开关倒计时转换为文案 time => 设备将在xxx后 关闭/开启
|
32
|
+
* 精确到分钟
|
33
|
+
* @param {*} t 倒计时剩余(秒)
|
34
|
+
* @param {*} power 设备当前的开关状态 (如果当前设备为开启状态, 则倒计时显示为关闭)
|
35
|
+
*/
|
36
|
+
parseCountdown(t: number, power: boolean): string;
|
37
|
+
getFaultStrings(schema: Record<string, any>, faultCode: number | string, faultValue: number, onlyPrior?: boolean): string;
|
38
|
+
}
|
39
|
+
export {};
|
@@ -0,0 +1,293 @@
|
|
1
|
+
import _typeof from "@babel/runtime/helpers/esm/typeof";
|
2
|
+
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
|
3
|
+
import _createClass from "@babel/runtime/helpers/esm/createClass";
|
4
|
+
import "core-js/modules/es.object.keys.js";
|
5
|
+
import "core-js/modules/es.object.assign.js";
|
6
|
+
import "core-js/modules/es.array.map.js";
|
7
|
+
import "core-js/modules/es.regexp.exec.js";
|
8
|
+
import "core-js/modules/es.regexp.test.js";
|
9
|
+
import "core-js/modules/es.array.last-index-of.js";
|
10
|
+
import "core-js/modules/es.string.replace.js";
|
11
|
+
import "core-js/modules/es.regexp.constructor.js";
|
12
|
+
import "core-js/modules/es.regexp.dot-all.js";
|
13
|
+
import "core-js/modules/es.regexp.sticky.js";
|
14
|
+
import "core-js/modules/es.regexp.to-string.js";
|
15
|
+
import "core-js/modules/es.array.concat.js";
|
16
|
+
import "core-js/modules/es.parse-float.js";
|
17
|
+
import { requestCloud, getSystemInfoSync, THING } from '../..';
|
18
|
+
import { getBitValue } from '../utils';
|
19
|
+
/**
|
20
|
+
* I18n 智能小程序中用来获取多语言的对象
|
21
|
+
* 文档: https://developer.tuya.com/cn/miniapp/framework/app/i18n
|
22
|
+
*/
|
23
|
+
|
24
|
+
var I18N = /*#__PURE__*/function () {
|
25
|
+
function I18N(props) {
|
26
|
+
_classCallCheck(this, I18N);
|
27
|
+
|
28
|
+
this.strings = this.mergeLanguage(props, {});
|
29
|
+
this.defaultLang = this.strings.en ? 'en' : Object.keys(this.strings)[0];
|
30
|
+
this.setLanguage(this.defaultLang);
|
31
|
+
var langKey = getSystemInfoSync().language;
|
32
|
+
this.setLanguage(langKey);
|
33
|
+
}
|
34
|
+
|
35
|
+
_createClass(I18N, [{
|
36
|
+
key: "forceUpdateNetworkLang",
|
37
|
+
value: function forceUpdateNetworkLang(productId) {
|
38
|
+
var _this = this;
|
39
|
+
|
40
|
+
requestCloud({
|
41
|
+
api: "".concat(THING, ".m.i18n.get"),
|
42
|
+
version: '1.0',
|
43
|
+
data: {
|
44
|
+
productId: productId,
|
45
|
+
moduleName: 'h5',
|
46
|
+
endId: 2,
|
47
|
+
osId: 0
|
48
|
+
},
|
49
|
+
// @ts-ignore
|
50
|
+
success: function success(data) {
|
51
|
+
if (data) {
|
52
|
+
_this.strings = _this.mergeLanguage(_this.strings, data);
|
53
|
+
|
54
|
+
_this.buildLanguage(_this.__language);
|
55
|
+
}
|
56
|
+
}
|
57
|
+
});
|
58
|
+
}
|
59
|
+
}, {
|
60
|
+
key: "mergeLanguage",
|
61
|
+
value: function mergeLanguage(L1, L2) {
|
62
|
+
if (typeof L1 === 'undefined' && typeof L2 === 'undefined') return {};
|
63
|
+
if (typeof L1 === 'undefined') return L2;
|
64
|
+
if (typeof L2 === 'undefined') return L1;
|
65
|
+
var L0 = Object.assign({}, L1);
|
66
|
+
Object.keys(L2).map(function (k) {
|
67
|
+
if (typeof L0[k] !== 'undefined') {
|
68
|
+
Object.assign(L0[k], L2[k]);
|
69
|
+
} else {
|
70
|
+
L0[k] = Object.assign({}, L2[k]);
|
71
|
+
}
|
72
|
+
|
73
|
+
return null;
|
74
|
+
});
|
75
|
+
return L0;
|
76
|
+
}
|
77
|
+
}, {
|
78
|
+
key: "isZh",
|
79
|
+
value: function isZh(language) {
|
80
|
+
/**
|
81
|
+
* ios 的中文简体固定为 zh-Hans,
|
82
|
+
* 但安卓的中文简体可能有一大堆排列组件 = =,如 zh_CN、zh_cn、zh_Hans_CH、zh_hans_cn 等等;
|
83
|
+
*/
|
84
|
+
|
85
|
+
/**
|
86
|
+
* 如果匹配到位中文简体地区,
|
87
|
+
* 则将中文 zh 相关的 values 都写入到 this.strings 下,保证兜底本地 zh 相关的能取到,
|
88
|
+
* 再将当前地区 key 相关的 values 都写入到 this.strings 下,优先级最高;
|
89
|
+
*/
|
90
|
+
if (typeof language === 'string' && /^zh-hans$|^zh_hans$|^zh_cn$|^zh-cn$|^zh_hans_\w+|^zh-hans-\w+/.test(language.toLowerCase())) {
|
91
|
+
return true;
|
92
|
+
}
|
93
|
+
|
94
|
+
return false;
|
95
|
+
}
|
96
|
+
}, {
|
97
|
+
key: "setLanguage",
|
98
|
+
value: function setLanguage(language) {
|
99
|
+
this.__language = language;
|
100
|
+
|
101
|
+
if (this.isZh(language)) {
|
102
|
+
this.buildLanguage('zh');
|
103
|
+
this.buildLanguage(language);
|
104
|
+
} else {
|
105
|
+
this.buildLanguage(this.__language);
|
106
|
+
}
|
107
|
+
}
|
108
|
+
}, {
|
109
|
+
key: "buildLanguage",
|
110
|
+
value: function buildLanguage(language) {
|
111
|
+
var _this2 = this;
|
112
|
+
|
113
|
+
if (this.strings[language]) {
|
114
|
+
var localizedStrings = this.strings[language];
|
115
|
+
Object.keys(localizedStrings).map(function (key) {
|
116
|
+
// eslint-disable-next-line no-prototype-builtins
|
117
|
+
if (localizedStrings.hasOwnProperty(key)) {
|
118
|
+
_this2[key] = localizedStrings[key];
|
119
|
+
}
|
120
|
+
|
121
|
+
return null;
|
122
|
+
});
|
123
|
+
}
|
124
|
+
}
|
125
|
+
}, {
|
126
|
+
key: "_getBestMatchingLanguage",
|
127
|
+
value: function _getBestMatchingLanguage(language, props) {
|
128
|
+
if (props[language]) return language;
|
129
|
+
var idx = language.lastIndexOf('-');
|
130
|
+
|
131
|
+
if (idx >= 0) {
|
132
|
+
var lang = language.substring(0, idx);
|
133
|
+
return this._getBestMatchingLanguage(lang, props);
|
134
|
+
}
|
135
|
+
|
136
|
+
var underlineIdx = language.lastIndexOf('_');
|
137
|
+
|
138
|
+
if (underlineIdx >= 0) {
|
139
|
+
var _lang = language.substring(0, underlineIdx);
|
140
|
+
|
141
|
+
return this._getBestMatchingLanguage(_lang, props);
|
142
|
+
}
|
143
|
+
|
144
|
+
return this.defaultLang;
|
145
|
+
} // Format the passed string replacing the numbered placeholders
|
146
|
+
// i.e. I'd like some {0} and {1}, or just {0}
|
147
|
+
// Use example:
|
148
|
+
// strings.formatString(strings.question, strings.bread, strings.butter)
|
149
|
+
|
150
|
+
}, {
|
151
|
+
key: "formatString",
|
152
|
+
value: function formatString(str) {
|
153
|
+
var res = str;
|
154
|
+
|
155
|
+
for (var i = 0; i < (arguments.length <= 1 ? 0 : arguments.length - 1); i++) {
|
156
|
+
res = this._replaceAll("{".concat(i, "}"), i + 1 < 1 || arguments.length <= i + 1 ? undefined : arguments[i + 1], res);
|
157
|
+
}
|
158
|
+
|
159
|
+
return res;
|
160
|
+
}
|
161
|
+
}, {
|
162
|
+
key: "formatValue",
|
163
|
+
value: function formatValue(key) {
|
164
|
+
var res = typeof this[key] !== 'undefined' ? this[key] : I18n.t(key);
|
165
|
+
|
166
|
+
for (var i = 0; i < (arguments.length <= 1 ? 0 : arguments.length - 1); i++) {
|
167
|
+
res = this._replaceAll("{".concat(i, "}"), i + 1 < 1 || arguments.length <= i + 1 ? undefined : arguments[i + 1], res);
|
168
|
+
}
|
169
|
+
|
170
|
+
return res;
|
171
|
+
}
|
172
|
+
}, {
|
173
|
+
key: "_replaceAll",
|
174
|
+
value: function _replaceAll(find, replace, str) {
|
175
|
+
// eslint-disable-next-line no-useless-escape, no-param-reassign
|
176
|
+
find = find.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, '\\$1');
|
177
|
+
if (!str) return '';
|
178
|
+
return str.replace(new RegExp(find, 'g'), replace);
|
179
|
+
}
|
180
|
+
}, {
|
181
|
+
key: "getDpLang",
|
182
|
+
value: function getDpLang(code, value) {
|
183
|
+
var key;
|
184
|
+
|
185
|
+
if (typeof value === 'undefined') {
|
186
|
+
key = "dp_".concat(code).toLowerCase();
|
187
|
+
} else if (typeof value === 'boolean') {
|
188
|
+
var valStr = value ? 'on' : 'off';
|
189
|
+
key = "dp_".concat(code, "_").concat(valStr).toLowerCase();
|
190
|
+
} else {
|
191
|
+
key = "dp_".concat(code, "_").concat(value).toLowerCase();
|
192
|
+
}
|
193
|
+
|
194
|
+
return I18n.t(key) !== key ? I18n.t(key) : typeof this[key] !== 'undefined' ? this[key] : I18n.t(key);
|
195
|
+
}
|
196
|
+
}, {
|
197
|
+
key: "getDpName",
|
198
|
+
value: function getDpName(code, defaultName) {
|
199
|
+
var key = "dp_".concat(code).toLowerCase();
|
200
|
+
return I18n.t(key) !== key ? I18n.t(key) : typeof this[key] !== 'undefined' ? this[key] : defaultName;
|
201
|
+
}
|
202
|
+
}, {
|
203
|
+
key: "getDpsLang",
|
204
|
+
value: function getDpsLang(key) {
|
205
|
+
var _this3 = this;
|
206
|
+
|
207
|
+
var strs = {};
|
208
|
+
|
209
|
+
if (_typeof(key) === 'object') {
|
210
|
+
if (typeof key.strKey === 'string') {
|
211
|
+
strs = I18n.t(key.strKey) !== key.strKey ? I18n.t(key.strKey) : typeof this[key.strKey] !== 'undefined' ? this[key.strKey] : I18n.t(key.strKey);
|
212
|
+
} else {
|
213
|
+
Object.keys(key).map(function (i) {
|
214
|
+
strs[key[i]] = I18n.t(key[i]) !== key[i] ? I18n.t(key[i]) : typeof _this3[key[i]] !== 'undefined' ? _this3[key[i]] : I18n.t(key[i]);
|
215
|
+
return null;
|
216
|
+
});
|
217
|
+
}
|
218
|
+
} else {
|
219
|
+
strs = I18n.t(key) !== key ? I18n.t(key) : typeof this[key] !== 'undefined' ? this[key] : I18n.t(key);
|
220
|
+
}
|
221
|
+
|
222
|
+
return strs;
|
223
|
+
}
|
224
|
+
}, {
|
225
|
+
key: "getLang",
|
226
|
+
value: function getLang(key, defaultString) {
|
227
|
+
return I18n.t(key) !== key ? I18n.t(key) : typeof this[key] !== 'undefined' ? this[key] : typeof defaultString !== 'undefined' ? defaultString : I18n.t(key);
|
228
|
+
}
|
229
|
+
/**
|
230
|
+
* 获取picker标题
|
231
|
+
* @param {*} dpCode
|
232
|
+
*/
|
233
|
+
|
234
|
+
}, {
|
235
|
+
key: "getRangeStrings",
|
236
|
+
value: function getRangeStrings(dpCode, schema) {
|
237
|
+
var _this4 = this;
|
238
|
+
|
239
|
+
var result = {};
|
240
|
+
if (typeof schema === 'undefined') return result;
|
241
|
+
var lists = schema.range;
|
242
|
+
lists.map(function (v) {
|
243
|
+
var key = "dp_".concat(dpCode, "_").concat(v).toLowerCase();
|
244
|
+
result[v] = I18n.t(key) !== key ? I18n.t(key) : typeof _this4[key] !== 'undefined' ? _this4[key] : I18n.t(key);
|
245
|
+
return null;
|
246
|
+
});
|
247
|
+
return result;
|
248
|
+
}
|
249
|
+
/**
|
250
|
+
* 开关倒计时转换为文案 time => 设备将在xxx后 关闭/开启
|
251
|
+
* 精确到分钟
|
252
|
+
* @param {*} t 倒计时剩余(秒)
|
253
|
+
* @param {*} power 设备当前的开关状态 (如果当前设备为开启状态, 则倒计时显示为关闭)
|
254
|
+
*/
|
255
|
+
|
256
|
+
}, {
|
257
|
+
key: "parseCountdown",
|
258
|
+
value: function parseCountdown(t, power) {
|
259
|
+
var h = parseFloat("".concat(t / 3600));
|
260
|
+
var m = parseFloat("".concat(t / 60 - h * 60));
|
261
|
+
var tHour = I18n.t('t_hour') !== 't_hour' ? I18n.t('t_hour') : typeof this.t_hour !== 'undefined' ? this.t_hour : I18n.t('t_hour');
|
262
|
+
var tMinute = I18n.t('t_minute') !== 't_minute' ? I18n.t('t_minute') : typeof this.t_minute !== 'undefined' ? this.t_minute : I18n.t('t_minute');
|
263
|
+
var time = h >= 1.0 ? "".concat(Math.round(h)).concat(tHour) : "".concat(Math.round(m)).concat(tMinute);
|
264
|
+
var key = "countdown_".concat(power ? 'on' : 'off');
|
265
|
+
var str = I18n.t(key) !== key ? I18n.t(key) : typeof this[key] !== 'undefined' ? this[key] : I18n.t(key);
|
266
|
+
return this.formatString(str, time);
|
267
|
+
}
|
268
|
+
}, {
|
269
|
+
key: "getFaultStrings",
|
270
|
+
value: function getFaultStrings(schema, faultCode, faultValue) {
|
271
|
+
var onlyPrior = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : true;
|
272
|
+
if (!faultValue) return '';
|
273
|
+
var label = schema.label;
|
274
|
+
var labels = [];
|
275
|
+
|
276
|
+
for (var i = 0; i < label.length; i++) {
|
277
|
+
var value = label[i];
|
278
|
+
var isExist = getBitValue(faultValue, i);
|
279
|
+
|
280
|
+
if (isExist) {
|
281
|
+
labels.push(this.getDpLang(faultCode, value));
|
282
|
+
if (onlyPrior) break;
|
283
|
+
}
|
284
|
+
}
|
285
|
+
|
286
|
+
return onlyPrior ? labels[0] : labels.join(', ');
|
287
|
+
}
|
288
|
+
}]);
|
289
|
+
|
290
|
+
return I18N;
|
291
|
+
}();
|
292
|
+
|
293
|
+
export { I18N as default };
|
@@ -0,0 +1,39 @@
|
|
1
|
+
declare type LanguageMap = {
|
2
|
+
[key: string]: Record<string, string>;
|
3
|
+
};
|
4
|
+
export default class I18N {
|
5
|
+
[x: string]: any;
|
6
|
+
strings: LanguageMap;
|
7
|
+
defaultLang: string;
|
8
|
+
__language: string;
|
9
|
+
constructor(props: LanguageMap);
|
10
|
+
forceUpdateNetworkLang(productId: string): void;
|
11
|
+
mergeLanguage(L1: LanguageMap, L2: LanguageMap): LanguageMap;
|
12
|
+
isZh(language: string): boolean;
|
13
|
+
setLanguage(language: string): void;
|
14
|
+
buildLanguage(language: string): void;
|
15
|
+
_getBestMatchingLanguage(language: string, props: LanguageMap): any;
|
16
|
+
formatString(str: string, ...values: any[]): string;
|
17
|
+
formatValue(key: string, ...values: any[]): any;
|
18
|
+
_replaceAll(find: string, replace: string, str: string): string;
|
19
|
+
getDpLang(code: string | number, value?: undefined | boolean | string): any;
|
20
|
+
getDpName(code: string, defaultName: string): any;
|
21
|
+
getDpsLang(key: {
|
22
|
+
[key: string]: string;
|
23
|
+
}): {};
|
24
|
+
getLang(key: string, defaultString?: string): any;
|
25
|
+
/**
|
26
|
+
* 获取picker标题
|
27
|
+
* @param {*} dpCode
|
28
|
+
*/
|
29
|
+
getRangeStrings(dpCode: string, schema: Record<string, any>): {};
|
30
|
+
/**
|
31
|
+
* 开关倒计时转换为文案 time => 设备将在xxx后 关闭/开启
|
32
|
+
* 精确到分钟
|
33
|
+
* @param {*} t 倒计时剩余(秒)
|
34
|
+
* @param {*} power 设备当前的开关状态 (如果当前设备为开启状态, 则倒计时显示为关闭)
|
35
|
+
*/
|
36
|
+
parseCountdown(t: number, power: boolean): string;
|
37
|
+
getFaultStrings(schema: Record<string, any>, faultCode: number | string, faultValue: number, onlyPrior?: boolean): string;
|
38
|
+
}
|
39
|
+
export {};
|
@@ -0,0 +1,302 @@
|
|
1
|
+
import _typeof from "@babel/runtime/helpers/esm/typeof";
|
2
|
+
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
|
3
|
+
import _createClass from "@babel/runtime/helpers/esm/createClass";
|
4
|
+
import "core-js/modules/es.object.keys.js";
|
5
|
+
import "core-js/modules/es.object.assign.js";
|
6
|
+
import "core-js/modules/es.array.map.js";
|
7
|
+
import "core-js/modules/es.regexp.exec.js";
|
8
|
+
import "core-js/modules/es.regexp.test.js";
|
9
|
+
import "core-js/modules/es.array.last-index-of.js";
|
10
|
+
import "core-js/modules/es.string.replace.js";
|
11
|
+
import "core-js/modules/es.regexp.constructor.js";
|
12
|
+
import "core-js/modules/es.regexp.dot-all.js";
|
13
|
+
import "core-js/modules/es.regexp.sticky.js";
|
14
|
+
import "core-js/modules/es.regexp.to-string.js";
|
15
|
+
import "core-js/modules/es.array.concat.js";
|
16
|
+
import "core-js/modules/es.parse-float.js";
|
17
|
+
import { requestCloud, getSystemInfoSync, THING } from '../..';
|
18
|
+
import { getBitValue } from '../utils'; // /**
|
19
|
+
// * I18n 智能小程序中用来获取多语言的对象
|
20
|
+
// * 文档: https://developer.tuya.com/cn/miniapp/framework/app/i18n
|
21
|
+
// */
|
22
|
+
// declare class I18n {
|
23
|
+
// static t: (key: string) => string;
|
24
|
+
// }
|
25
|
+
// 微信小程序暂时只支持本地多语言
|
26
|
+
|
27
|
+
var I18n = {
|
28
|
+
t: function t(key) {
|
29
|
+
return key;
|
30
|
+
}
|
31
|
+
};
|
32
|
+
|
33
|
+
var I18N = /*#__PURE__*/function () {
|
34
|
+
function I18N(props) {
|
35
|
+
_classCallCheck(this, I18N);
|
36
|
+
|
37
|
+
this.strings = this.mergeLanguage(props, {});
|
38
|
+
this.defaultLang = this.strings.en ? 'en' : Object.keys(this.strings)[0];
|
39
|
+
this.setLanguage(this.defaultLang);
|
40
|
+
var langKey = getSystemInfoSync().language;
|
41
|
+
this.setLanguage(langKey);
|
42
|
+
}
|
43
|
+
|
44
|
+
_createClass(I18N, [{
|
45
|
+
key: "forceUpdateNetworkLang",
|
46
|
+
value: function forceUpdateNetworkLang(productId) {
|
47
|
+
var _this = this;
|
48
|
+
|
49
|
+
requestCloud({
|
50
|
+
api: "".concat(THING, ".m.i18n.get"),
|
51
|
+
version: '1.0',
|
52
|
+
data: {
|
53
|
+
productId: productId,
|
54
|
+
moduleName: 'h5',
|
55
|
+
endId: 2,
|
56
|
+
osId: 0
|
57
|
+
},
|
58
|
+
// @ts-ignore
|
59
|
+
success: function success(data) {
|
60
|
+
if (data) {
|
61
|
+
_this.strings = _this.mergeLanguage(_this.strings, data);
|
62
|
+
|
63
|
+
_this.buildLanguage(_this.__language);
|
64
|
+
}
|
65
|
+
}
|
66
|
+
});
|
67
|
+
}
|
68
|
+
}, {
|
69
|
+
key: "mergeLanguage",
|
70
|
+
value: function mergeLanguage(L1, L2) {
|
71
|
+
if (typeof L1 === 'undefined' && typeof L2 === 'undefined') return {};
|
72
|
+
if (typeof L1 === 'undefined') return L2;
|
73
|
+
if (typeof L2 === 'undefined') return L1;
|
74
|
+
var L0 = Object.assign({}, L1);
|
75
|
+
Object.keys(L2).map(function (k) {
|
76
|
+
if (typeof L0[k] !== 'undefined') {
|
77
|
+
Object.assign(L0[k], L2[k]);
|
78
|
+
} else {
|
79
|
+
L0[k] = Object.assign({}, L2[k]);
|
80
|
+
}
|
81
|
+
|
82
|
+
return null;
|
83
|
+
});
|
84
|
+
return L0;
|
85
|
+
}
|
86
|
+
}, {
|
87
|
+
key: "isZh",
|
88
|
+
value: function isZh(language) {
|
89
|
+
/**
|
90
|
+
* ios 的中文简体固定为 zh-Hans,
|
91
|
+
* 但安卓的中文简体可能有一大堆排列组件 = =,如 zh_CN、zh_cn、zh_Hans_CH、zh_hans_cn 等等;
|
92
|
+
*/
|
93
|
+
|
94
|
+
/**
|
95
|
+
* 如果匹配到位中文简体地区,
|
96
|
+
* 则将中文 zh 相关的 values 都写入到 this.strings 下,保证兜底本地 zh 相关的能取到,
|
97
|
+
* 再将当前地区 key 相关的 values 都写入到 this.strings 下,优先级最高;
|
98
|
+
*/
|
99
|
+
if (typeof language === 'string' && /^zh-hans$|^zh_hans$|^zh_cn$|^zh-cn$|^zh_hans_\w+|^zh-hans-\w+/.test(language.toLowerCase())) {
|
100
|
+
return true;
|
101
|
+
}
|
102
|
+
|
103
|
+
return false;
|
104
|
+
}
|
105
|
+
}, {
|
106
|
+
key: "setLanguage",
|
107
|
+
value: function setLanguage(language) {
|
108
|
+
this.__language = language;
|
109
|
+
|
110
|
+
if (this.isZh(language)) {
|
111
|
+
this.buildLanguage('zh');
|
112
|
+
this.buildLanguage(language);
|
113
|
+
} else {
|
114
|
+
this.buildLanguage(this.__language);
|
115
|
+
}
|
116
|
+
}
|
117
|
+
}, {
|
118
|
+
key: "buildLanguage",
|
119
|
+
value: function buildLanguage(language) {
|
120
|
+
var _this2 = this;
|
121
|
+
|
122
|
+
if (this.strings[language]) {
|
123
|
+
var localizedStrings = this.strings[language];
|
124
|
+
Object.keys(localizedStrings).map(function (key) {
|
125
|
+
// eslint-disable-next-line no-prototype-builtins
|
126
|
+
if (localizedStrings.hasOwnProperty(key)) {
|
127
|
+
_this2[key] = localizedStrings[key];
|
128
|
+
}
|
129
|
+
|
130
|
+
return null;
|
131
|
+
});
|
132
|
+
}
|
133
|
+
}
|
134
|
+
}, {
|
135
|
+
key: "_getBestMatchingLanguage",
|
136
|
+
value: function _getBestMatchingLanguage(language, props) {
|
137
|
+
if (props[language]) return language;
|
138
|
+
var idx = language.lastIndexOf('-');
|
139
|
+
|
140
|
+
if (idx >= 0) {
|
141
|
+
var lang = language.substring(0, idx);
|
142
|
+
return this._getBestMatchingLanguage(lang, props);
|
143
|
+
}
|
144
|
+
|
145
|
+
var underlineIdx = language.lastIndexOf('_');
|
146
|
+
|
147
|
+
if (underlineIdx >= 0) {
|
148
|
+
var _lang = language.substring(0, underlineIdx);
|
149
|
+
|
150
|
+
return this._getBestMatchingLanguage(_lang, props);
|
151
|
+
}
|
152
|
+
|
153
|
+
return this.defaultLang;
|
154
|
+
} // Format the passed string replacing the numbered placeholders
|
155
|
+
// i.e. I'd like some {0} and {1}, or just {0}
|
156
|
+
// Use example:
|
157
|
+
// strings.formatString(strings.question, strings.bread, strings.butter)
|
158
|
+
|
159
|
+
}, {
|
160
|
+
key: "formatString",
|
161
|
+
value: function formatString(str) {
|
162
|
+
var res = str;
|
163
|
+
|
164
|
+
for (var i = 0; i < (arguments.length <= 1 ? 0 : arguments.length - 1); i++) {
|
165
|
+
res = this._replaceAll("{".concat(i, "}"), i + 1 < 1 || arguments.length <= i + 1 ? undefined : arguments[i + 1], res);
|
166
|
+
}
|
167
|
+
|
168
|
+
return res;
|
169
|
+
}
|
170
|
+
}, {
|
171
|
+
key: "formatValue",
|
172
|
+
value: function formatValue(key) {
|
173
|
+
var res = typeof this[key] !== 'undefined' ? this[key] : I18n.t(key);
|
174
|
+
|
175
|
+
for (var i = 0; i < (arguments.length <= 1 ? 0 : arguments.length - 1); i++) {
|
176
|
+
res = this._replaceAll("{".concat(i, "}"), i + 1 < 1 || arguments.length <= i + 1 ? undefined : arguments[i + 1], res);
|
177
|
+
}
|
178
|
+
|
179
|
+
return res;
|
180
|
+
}
|
181
|
+
}, {
|
182
|
+
key: "_replaceAll",
|
183
|
+
value: function _replaceAll(find, replace, str) {
|
184
|
+
// eslint-disable-next-line no-useless-escape, no-param-reassign
|
185
|
+
find = find.replace(/([.*+?^=!:${}()|\[\]\/\\])/g, '\\$1');
|
186
|
+
if (!str) return '';
|
187
|
+
return str.replace(new RegExp(find, 'g'), replace);
|
188
|
+
}
|
189
|
+
}, {
|
190
|
+
key: "getDpLang",
|
191
|
+
value: function getDpLang(code, value) {
|
192
|
+
var key;
|
193
|
+
|
194
|
+
if (typeof value === 'undefined') {
|
195
|
+
key = "dp_".concat(code).toLowerCase();
|
196
|
+
} else if (typeof value === 'boolean') {
|
197
|
+
var valStr = value ? 'on' : 'off';
|
198
|
+
key = "dp_".concat(code, "_").concat(valStr).toLowerCase();
|
199
|
+
} else {
|
200
|
+
key = "dp_".concat(code, "_").concat(value).toLowerCase();
|
201
|
+
}
|
202
|
+
|
203
|
+
return I18n.t(key) !== key ? I18n.t(key) : typeof this[key] !== 'undefined' ? this[key] : I18n.t(key);
|
204
|
+
}
|
205
|
+
}, {
|
206
|
+
key: "getDpName",
|
207
|
+
value: function getDpName(code, defaultName) {
|
208
|
+
var key = "dp_".concat(code).toLowerCase();
|
209
|
+
return I18n.t(key) !== key ? I18n.t(key) : typeof this[key] !== 'undefined' ? this[key] : defaultName;
|
210
|
+
}
|
211
|
+
}, {
|
212
|
+
key: "getDpsLang",
|
213
|
+
value: function getDpsLang(key) {
|
214
|
+
var _this3 = this;
|
215
|
+
|
216
|
+
var strs = {};
|
217
|
+
|
218
|
+
if (_typeof(key) === 'object') {
|
219
|
+
if (typeof key.strKey === 'string') {
|
220
|
+
strs = I18n.t(key.strKey) !== key.strKey ? I18n.t(key.strKey) : typeof this[key.strKey] !== 'undefined' ? this[key.strKey] : I18n.t(key.strKey);
|
221
|
+
} else {
|
222
|
+
Object.keys(key).map(function (i) {
|
223
|
+
strs[key[i]] = I18n.t(key[i]) !== key[i] ? I18n.t(key[i]) : typeof _this3[key[i]] !== 'undefined' ? _this3[key[i]] : I18n.t(key[i]);
|
224
|
+
return null;
|
225
|
+
});
|
226
|
+
}
|
227
|
+
} else {
|
228
|
+
strs = I18n.t(key) !== key ? I18n.t(key) : typeof this[key] !== 'undefined' ? this[key] : I18n.t(key);
|
229
|
+
}
|
230
|
+
|
231
|
+
return strs;
|
232
|
+
}
|
233
|
+
}, {
|
234
|
+
key: "getLang",
|
235
|
+
value: function getLang(key, defaultString) {
|
236
|
+
return I18n.t(key) !== key ? I18n.t(key) : typeof this[key] !== 'undefined' ? this[key] : typeof defaultString !== 'undefined' ? defaultString : I18n.t(key);
|
237
|
+
}
|
238
|
+
/**
|
239
|
+
* 获取picker标题
|
240
|
+
* @param {*} dpCode
|
241
|
+
*/
|
242
|
+
|
243
|
+
}, {
|
244
|
+
key: "getRangeStrings",
|
245
|
+
value: function getRangeStrings(dpCode, schema) {
|
246
|
+
var _this4 = this;
|
247
|
+
|
248
|
+
var result = {};
|
249
|
+
if (typeof schema === 'undefined') return result;
|
250
|
+
var lists = schema.range;
|
251
|
+
lists.map(function (v) {
|
252
|
+
var key = "dp_".concat(dpCode, "_").concat(v).toLowerCase();
|
253
|
+
result[v] = I18n.t(key) !== key ? I18n.t(key) : typeof _this4[key] !== 'undefined' ? _this4[key] : I18n.t(key);
|
254
|
+
return null;
|
255
|
+
});
|
256
|
+
return result;
|
257
|
+
}
|
258
|
+
/**
|
259
|
+
* 开关倒计时转换为文案 time => 设备将在xxx后 关闭/开启
|
260
|
+
* 精确到分钟
|
261
|
+
* @param {*} t 倒计时剩余(秒)
|
262
|
+
* @param {*} power 设备当前的开关状态 (如果当前设备为开启状态, 则倒计时显示为关闭)
|
263
|
+
*/
|
264
|
+
|
265
|
+
}, {
|
266
|
+
key: "parseCountdown",
|
267
|
+
value: function parseCountdown(t, power) {
|
268
|
+
var h = parseFloat("".concat(t / 3600));
|
269
|
+
var m = parseFloat("".concat(t / 60 - h * 60));
|
270
|
+
var tHour = I18n.t('t_hour') !== 't_hour' ? I18n.t('t_hour') : typeof this.t_hour !== 'undefined' ? this.t_hour : I18n.t('t_hour');
|
271
|
+
var tMinute = I18n.t('t_minute') !== 't_minute' ? I18n.t('t_minute') : typeof this.t_minute !== 'undefined' ? this.t_minute : I18n.t('t_minute');
|
272
|
+
var time = h >= 1.0 ? "".concat(Math.round(h)).concat(tHour) : "".concat(Math.round(m)).concat(tMinute);
|
273
|
+
var key = "countdown_".concat(power ? 'on' : 'off');
|
274
|
+
var str = I18n.t(key) !== key ? I18n.t(key) : typeof this[key] !== 'undefined' ? this[key] : I18n.t(key);
|
275
|
+
return this.formatString(str, time);
|
276
|
+
}
|
277
|
+
}, {
|
278
|
+
key: "getFaultStrings",
|
279
|
+
value: function getFaultStrings(schema, faultCode, faultValue) {
|
280
|
+
var onlyPrior = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : true;
|
281
|
+
if (!faultValue) return '';
|
282
|
+
var label = schema.label;
|
283
|
+
var labels = [];
|
284
|
+
|
285
|
+
for (var i = 0; i < label.length; i++) {
|
286
|
+
var value = label[i];
|
287
|
+
var isExist = getBitValue(faultValue, i);
|
288
|
+
|
289
|
+
if (isExist) {
|
290
|
+
labels.push(this.getDpLang(faultCode, value));
|
291
|
+
if (onlyPrior) break;
|
292
|
+
}
|
293
|
+
}
|
294
|
+
|
295
|
+
return onlyPrior ? labels[0] : labels.join(', ');
|
296
|
+
}
|
297
|
+
}]);
|
298
|
+
|
299
|
+
return I18N;
|
300
|
+
}();
|
301
|
+
|
302
|
+
export { I18N as default };
|
@@ -0,0 +1,15 @@
|
|
1
|
+
import { getNetworkType, onNetworkStatusChange } from '../..';
|
2
|
+
import { GetTTTSuccessData, GetTTTEventListenerParams, NetworkState } from './types';
|
3
|
+
declare type GetNetworkResult = GetTTTSuccessData<typeof getNetworkType>;
|
4
|
+
declare type NetworkStatusChangeParam = GetTTTEventListenerParams<typeof onNetworkStatusChange>;
|
5
|
+
/**
|
6
|
+
* 规范统一处理 network 返回值
|
7
|
+
*
|
8
|
+
* 1. 比如 getNetworkType 里没有返回 isConnected 字段是正常的,为了和 wx 保持统一
|
9
|
+
* 2. 事件回调数据里没有返回 signalStrength 字段是正常的,为了和 wx 保持统一
|
10
|
+
*
|
11
|
+
* @param network - 当前的 network 数据
|
12
|
+
* @param data - 从 getNetworkType 或 onNetworkStatusChange 获取到的数据
|
13
|
+
*/
|
14
|
+
export declare function normalizeNetwork(network: NetworkState, data: GetNetworkResult | NetworkStatusChangeParam): NetworkState;
|
15
|
+
export {};
|
@@ -0,0 +1,25 @@
|
|
1
|
+
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
|
2
|
+
|
3
|
+
/**
|
4
|
+
* 规范统一处理 network 返回值
|
5
|
+
*
|
6
|
+
* 1. 比如 getNetworkType 里没有返回 isConnected 字段是正常的,为了和 wx 保持统一
|
7
|
+
* 2. 事件回调数据里没有返回 signalStrength 字段是正常的,为了和 wx 保持统一
|
8
|
+
*
|
9
|
+
* @param network - 当前的 network 数据
|
10
|
+
* @param data - 从 getNetworkType 或 onNetworkStatusChange 获取到的数据
|
11
|
+
*/
|
12
|
+
export function normalizeNetwork(network, data) {
|
13
|
+
var _result$networkType;
|
14
|
+
|
15
|
+
var result = _objectSpread(_objectSpread({}, network), data);
|
16
|
+
|
17
|
+
var networkType = result === null || result === void 0 ? void 0 : (_result$networkType = result.networkType) === null || _result$networkType === void 0 ? void 0 : _result$networkType.toUpperCase();
|
18
|
+
result.networkType = networkType;
|
19
|
+
|
20
|
+
if (typeof result.isConnected === 'undefined') {
|
21
|
+
result.isConnected = networkType !== 'NONE';
|
22
|
+
}
|
23
|
+
|
24
|
+
return result;
|
25
|
+
}
|
@@ -0,0 +1,27 @@
|
|
1
|
+
import { publishDpsBase } from '../..';
|
2
|
+
import { GetTTTAllParams, DpState } from './types';
|
3
|
+
declare type PublishDpsOptions = Omit<GetTTTAllParams<typeof publishDpsBase>, 'deviceId' | 'dps'>;
|
4
|
+
/**
|
5
|
+
* 下发 DP 点控制设备
|
6
|
+
*
|
7
|
+
* @param data - 当前要下发的 DP 数据,同时支持 key 为 dp code 或 dp id
|
8
|
+
* @param options - publishDps 的高阶配置
|
9
|
+
|
10
|
+
* @example
|
11
|
+
*
|
12
|
+
* publishDps({ switch: false });
|
13
|
+
* publishDps({ '1': false })
|
14
|
+
*/
|
15
|
+
export declare function sendDps(data: DpState, options?: PublishDpsOptions): Promise<boolean>;
|
16
|
+
/**
|
17
|
+
* 兼容新旧 publishDps,可以使用原有小程序提供的调用方式,也可以使用 SDK 提供的调用方式。
|
18
|
+
* @param data ty.device.publishDps的参数 或 当前要下发的 DP 数据,同时支持 key 为 dp code 或 dp id
|
19
|
+
* @param options publishDps 的高阶配置,默认值为 { mode: 2, options: {}, pipelines: [] } * @example
|
20
|
+
*
|
21
|
+
* @example
|
22
|
+
* publishDps({ deviceId: string, dps: {}, mode: number, pipelines: number[], options: {}, success(){}, fail(){},complete(){} })
|
23
|
+
* publishDps({ switch: false });
|
24
|
+
* publishDps({ '1': false })
|
25
|
+
*/
|
26
|
+
export declare function publishDps<T extends GetTTTAllParams<typeof publishDpsBase> | DpState>(data: T, options?: PublishDpsOptions): T extends GetTTTAllParams<typeof publishDpsBase> ? void : Promise<boolean>;
|
27
|
+
export {};
|
@@ -0,0 +1,70 @@
|
|
1
|
+
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
|
2
|
+
import "core-js/modules/es.object.to-string.js";
|
3
|
+
import "core-js/modules/web.dom-collections.for-each.js";
|
4
|
+
import "core-js/modules/es.object.keys.js";
|
5
|
+
import "core-js/modules/es.promise.js";
|
6
|
+
import { publishDpsBase } from '../..';
|
7
|
+
import { getDevInfo } from './devInfo';
|
8
|
+
var DEFAULT_OPTIONS = {
|
9
|
+
mode: 2,
|
10
|
+
options: {},
|
11
|
+
pipelines: []
|
12
|
+
};
|
13
|
+
/**
|
14
|
+
* 下发 DP 点控制设备
|
15
|
+
*
|
16
|
+
* @param data - 当前要下发的 DP 数据,同时支持 key 为 dp code 或 dp id
|
17
|
+
* @param options - publishDps 的高阶配置
|
18
|
+
|
19
|
+
* @example
|
20
|
+
*
|
21
|
+
* publishDps({ switch: false });
|
22
|
+
* publishDps({ '1': false })
|
23
|
+
*/
|
24
|
+
|
25
|
+
export function sendDps(data) {
|
26
|
+
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : DEFAULT_OPTIONS;
|
27
|
+
var devInfo = getDevInfo();
|
28
|
+
var dps = {};
|
29
|
+
var codeIds = (devInfo === null || devInfo === void 0 ? void 0 : devInfo.codeIds) || {};
|
30
|
+
Object.keys(data).forEach(function (code) {
|
31
|
+
if (codeIds[code] !== undefined) {
|
32
|
+
dps["".concat(codeIds[code])] = data[code];
|
33
|
+
} else {
|
34
|
+
dps[code] = data[code];
|
35
|
+
}
|
36
|
+
});
|
37
|
+
return new Promise(function (resolve, reject) {
|
38
|
+
publishDpsBase(_objectSpread({
|
39
|
+
deviceId: devInfo.devId,
|
40
|
+
dps: dps,
|
41
|
+
success: function success(ret) {
|
42
|
+
resolve(ret);
|
43
|
+
},
|
44
|
+
fail: function fail(err) {
|
45
|
+
reject(err);
|
46
|
+
}
|
47
|
+
}, options));
|
48
|
+
});
|
49
|
+
}
|
50
|
+
/**
|
51
|
+
* 兼容新旧 publishDps,可以使用原有小程序提供的调用方式,也可以使用 SDK 提供的调用方式。
|
52
|
+
* @param data ty.device.publishDps的参数 或 当前要下发的 DP 数据,同时支持 key 为 dp code 或 dp id
|
53
|
+
* @param options publishDps 的高阶配置,默认值为 { mode: 2, options: {}, pipelines: [] } * @example
|
54
|
+
*
|
55
|
+
* @example
|
56
|
+
* publishDps({ deviceId: string, dps: {}, mode: number, pipelines: number[], options: {}, success(){}, fail(){},complete(){} })
|
57
|
+
* publishDps({ switch: false });
|
58
|
+
* publishDps({ '1': false })
|
59
|
+
*/
|
60
|
+
|
61
|
+
export function publishDps(data) {
|
62
|
+
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : DEFAULT_OPTIONS;
|
63
|
+
|
64
|
+
if (data.deviceId && data.dps) {
|
65
|
+
publishDpsBase(_objectSpread(_objectSpread({}, data), options));
|
66
|
+
} else {
|
67
|
+
// @ts-ignore
|
68
|
+
return sendDps(data, options);
|
69
|
+
}
|
70
|
+
}
|
@@ -0,0 +1 @@
|
|
1
|
+
export {};
|
@@ -0,0 +1,106 @@
|
|
1
|
+
export declare type DpValue = boolean | number | string;
|
2
|
+
export declare type DpState = Record<string, DpValue>;
|
3
|
+
export interface DpSchema {
|
4
|
+
attr: number;
|
5
|
+
canTrigger: boolean;
|
6
|
+
/**
|
7
|
+
* 功能点标识码,如 switch
|
8
|
+
*/
|
9
|
+
code: string;
|
10
|
+
defaultRecommend: boolean;
|
11
|
+
editPermission: boolean;
|
12
|
+
executable: boolean;
|
13
|
+
extContent: string;
|
14
|
+
iconname: string;
|
15
|
+
/**
|
16
|
+
* 功能点 ID
|
17
|
+
*/
|
18
|
+
id: number;
|
19
|
+
/**
|
20
|
+
* 功能点模式类型
|
21
|
+
* rw: 可下发可上报(可读可写)
|
22
|
+
* ro: 只可上报(仅可读)
|
23
|
+
* wr: 只可下发(仅可写)
|
24
|
+
*/
|
25
|
+
mode: 'rw' | 'ro' | 'wr';
|
26
|
+
/**
|
27
|
+
* 功能点名称,一般用于语音等场景
|
28
|
+
*/
|
29
|
+
name: string;
|
30
|
+
/**
|
31
|
+
* 功能点属性
|
32
|
+
*/
|
33
|
+
property?: {
|
34
|
+
/**
|
35
|
+
* 功能点类型
|
36
|
+
*/
|
37
|
+
type: 'bool' | 'value' | 'enum' | 'bitmap' | 'string';
|
38
|
+
/**
|
39
|
+
* 枚举值范围,type = enum 时才存在
|
40
|
+
*/
|
41
|
+
range?: string[] | readonly string[];
|
42
|
+
/**
|
43
|
+
* 故障型标签列表,type = bitmap 时才存在
|
44
|
+
*/
|
45
|
+
label?: string[] | readonly string[];
|
46
|
+
/**
|
47
|
+
* 故障型最大长度,type = bitmap 时才存在
|
48
|
+
*/
|
49
|
+
maxlen?: number;
|
50
|
+
/**
|
51
|
+
* 数值型单位,type = value 时才存在
|
52
|
+
*/
|
53
|
+
unit?: string;
|
54
|
+
/**
|
55
|
+
* 数值型最小值,type = value 时才存在
|
56
|
+
*/
|
57
|
+
min?: number;
|
58
|
+
/**
|
59
|
+
* 数值型最大值,type = value 时才存在
|
60
|
+
*/
|
61
|
+
max?: number;
|
62
|
+
/**
|
63
|
+
* 数值型精度,type = value 时才存在
|
64
|
+
* @example
|
65
|
+
* scale = 0; value = 10; 则业务上显示值为 10
|
66
|
+
* scale = 1; value = 10; 则业务上显示值为 1
|
67
|
+
* scale = 2; value = 10; 则业务上显示值为 0.1
|
68
|
+
*/
|
69
|
+
scale?: number;
|
70
|
+
/**
|
71
|
+
* 数值型步长,type = value 时才存在
|
72
|
+
*/
|
73
|
+
step?: number;
|
74
|
+
};
|
75
|
+
type: 'raw' | 'obj';
|
76
|
+
}
|
77
|
+
interface PanelConfig {
|
78
|
+
bic: Array<{
|
79
|
+
code: string;
|
80
|
+
selected: boolean;
|
81
|
+
value?: string | undefined;
|
82
|
+
}>;
|
83
|
+
fun?: Record<string, any> | undefined;
|
84
|
+
}
|
85
|
+
/**
|
86
|
+
* 设备信息
|
87
|
+
*/
|
88
|
+
export declare type DevInfo = Omit<ty.device.DeviceInfo, 'schema' | 'panelConfig'> & {
|
89
|
+
/**
|
90
|
+
* dp id 与 dp code 的映射
|
91
|
+
*/
|
92
|
+
idCodes: Record<string, string>;
|
93
|
+
/**
|
94
|
+
* dp code 与 dp id 的映射
|
95
|
+
*/
|
96
|
+
codeIds: Record<string, string>;
|
97
|
+
/**
|
98
|
+
* 产品信息,schema,功能定义都在里面
|
99
|
+
*/
|
100
|
+
schema: Array<DpSchema>;
|
101
|
+
/**
|
102
|
+
* 面板云配置
|
103
|
+
*/
|
104
|
+
panelConfig: PanelConfig;
|
105
|
+
};
|
106
|
+
export {};
|
@@ -0,0 +1 @@
|
|
1
|
+
export {};
|
@@ -0,0 +1,9 @@
|
|
1
|
+
declare type AnyFn = (...args: any) => any;
|
2
|
+
export declare type GetTTTAllParams<Fn extends AnyFn> = Parameters<Fn>['0'];
|
3
|
+
export declare type GetTTTParams<Fn extends AnyFn> = Omit<GetTTTAllParams<Fn>, 'complete' | 'success' | 'fail'>;
|
4
|
+
export declare type GetTTTSuccessData<Fn extends AnyFn, TTTParams extends AnyFn = GetTTTAllParams<Fn>['success']> = Parameters<TTTParams>['0'];
|
5
|
+
export declare type GetTTTFailData<Fn extends AnyFn, TTTParams extends AnyFn = GetTTTAllParams<Fn>['failed']> = Parameters<TTTParams>['0'];
|
6
|
+
export declare type GetTTTEventListener<Fn extends AnyFn> = GetTTTAllParams<Fn>;
|
7
|
+
export declare type GetTTTEventListenerParams<Fn extends AnyFn, TTTEventListener extends AnyFn = GetTTTEventListener<Fn>> = Parameters<TTTEventListener>['0'];
|
8
|
+
export declare type PromisifyTTT<Fn extends AnyFn> = (params: GetTTTParams<Fn>) => Promise<GetTTTSuccessData<Fn>>;
|
9
|
+
export {};
|
@@ -0,0 +1 @@
|
|
1
|
+
export {};
|
@@ -0,0 +1,15 @@
|
|
1
|
+
/**
|
2
|
+
* @example
|
3
|
+
* // 17 = 10001(2)
|
4
|
+
* getBitValue(17, 0)
|
5
|
+
* // 1
|
6
|
+
* @example
|
7
|
+
* getBitValue(17, 1)
|
8
|
+
* // 0
|
9
|
+
* @example
|
10
|
+
* getBitValue(17, 4)
|
11
|
+
* // 1
|
12
|
+
* @param {Number} idx, idx is reverse, and it begins 0
|
13
|
+
* @returns {Number} a num
|
14
|
+
*/
|
15
|
+
export declare const getBitValue: (num: number, idx: number) => number;
|
@@ -0,0 +1,17 @@
|
|
1
|
+
/**
|
2
|
+
* @example
|
3
|
+
* // 17 = 10001(2)
|
4
|
+
* getBitValue(17, 0)
|
5
|
+
* // 1
|
6
|
+
* @example
|
7
|
+
* getBitValue(17, 1)
|
8
|
+
* // 0
|
9
|
+
* @example
|
10
|
+
* getBitValue(17, 4)
|
11
|
+
* // 1
|
12
|
+
* @param {Number} idx, idx is reverse, and it begins 0
|
13
|
+
* @returns {Number} a num
|
14
|
+
*/
|
15
|
+
export var getBitValue = function (num, idx) {
|
16
|
+
return (num & 1 << idx) >> idx;
|
17
|
+
};
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@ray-js/api",
|
3
|
-
"version": "1.2.
|
3
|
+
"version": "1.2.6",
|
4
4
|
"description": "Ray universal api",
|
5
5
|
"keywords": [
|
6
6
|
"ray"
|
@@ -26,13 +26,13 @@
|
|
26
26
|
"build:kit:api": "node scripts/api-creator.mjs"
|
27
27
|
},
|
28
28
|
"dependencies": {
|
29
|
-
"@ray-js/framework": "^1.2.
|
30
|
-
"@ray-js/router": "^1.2.
|
29
|
+
"@ray-js/framework": "^1.2.6",
|
30
|
+
"@ray-js/router": "^1.2.6",
|
31
31
|
"@ray-js/wechat": "^0.0.16",
|
32
32
|
"base64-browser": "^1.0.1"
|
33
33
|
},
|
34
34
|
"devDependencies": {
|
35
|
-
"@ray-js/cli": "^1.2.
|
35
|
+
"@ray-js/cli": "^1.2.6",
|
36
36
|
"art-template": "^4.13.2",
|
37
37
|
"fs-extra": "^10.1.0",
|
38
38
|
"miniprogram-api-typings": "^3.4.3",
|
@@ -44,6 +44,6 @@
|
|
44
44
|
"email": "tuyafe@tuya.com"
|
45
45
|
}
|
46
46
|
],
|
47
|
-
"gitHead": "
|
47
|
+
"gitHead": "942db710384451acafa9aa16f6c0df5fab34d29b",
|
48
48
|
"repository": {}
|
49
49
|
}
|