Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

qcloud-iotexplorer-fileresource-sdk

Package Overview
Dependencies
Maintainers
2
Versions
47
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

qcloud-iotexplorer-fileresource-sdk - npm Package Compare versions

Comparing version 1.3.4 to 1.4.2

19

lib/FileSdk.d.ts

@@ -0,11 +1,20 @@

export interface ResourceUploadResponse {
RequestId?: string;
ResourceName?: string;
UploadUrl?: string;
}
export interface ResourceCreateResponse {
RequestId?: string;
ResourceName?: string;
}
declare abstract class FileSdk {
protected sdk: any;
protected request: any;
protected request: (action: any, params: any) => Promise<any>;
constructor(sdk: any);
getResourceUploadURL(file: any, hash: any, productId: any): Promise<any>;
createResource(file: any, hash: unknown, productId: any, type: any): Promise<void>;
getDeviceResource(ResourceName: any, deviceId: any): Promise<unknown>;
controlDeviceResource(ResourceName: any, deviceId: any): Promise<void>;
getResourceUploadURL(file: any, hash: unknown, productId: string): Promise<ResourceUploadResponse>;
createResource(file: any, hash: unknown, productId: string): Promise<ResourceCreateResponse>;
getDeviceResource(ResourceName: any, deviceId: string): Promise<string>;
controlDeviceResource(ResourceName: string, deviceId: string): Promise<void>;
abstract handleUpload(UploadUrl: string, file: any, hash: string): void;
}
export default FileSdk;

@@ -1,175 +0,2 @@

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var FileSdk = /** @class */ (function () {
function FileSdk(sdk) {
var _this = this;
this.sdk = sdk;
this.request = function (action, params) {
if (_this.sdk.requestTokenApi) {
return _this.sdk.requestTokenApi(action, params);
}
else {
return _this.sdk.requestApi(action, params);
}
};
}
FileSdk.prototype.getResourceUploadURL = function (file, hash, productId) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var e_1;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 2, , 3]);
return [4 /*yield*/, this.request('AppGetResourceUploadURL', {
ProductId: productId,
UserResourceName: hash,
FileSize: file.size,
ResourceVer: '0.0.1',
ReadProtect: 1,
})];
case 1: return [2 /*return*/, _a.sent()];
case 2:
e_1 = _a.sent();
console.log('AppGetResourceUploadURL错误');
return [3 /*break*/, 3];
case 3: return [2 /*return*/];
}
});
});
};
FileSdk.prototype.createResource = function (file, hash, productId, type) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var ResourceName, e_2;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
_a.trys.push([0, 2, , 3]);
return [4 /*yield*/, this.request('AppCreateProductResource', {
ProductId: productId,
UserResourceName: hash,
FileSize: file.size,
ResourceVer: '0.0.1',
FileHash: hash,
ResourceType: 'FILE',
ReadProtect: 1,
})];
case 1:
ResourceName = (_a.sent()).ResourceName;
if (ResourceName) {
if (type === 'h5') {
this.sdk.tips.showSuccess('文件上传成功');
}
else {
wx.showToast({
title: '文件上传成功',
icon: 'success',
duration: 5000,
});
}
}
return [3 /*break*/, 3];
case 2:
e_2 = _a.sent();
if (type === 'h5') {
this.sdk.tips.showError("\u4E0A\u4F20\u5931\u8D25," + e_2.msg);
}
else {
wx.showToast({
title: "" + e_2.msg,
icon: 'error',
duration: 5000,
});
}
return [3 /*break*/, 3];
case 3: return [2 /*return*/];
}
});
});
};
FileSdk.prototype.getDeviceResource = function (ResourceName, deviceId) {
var _this = this;
var startTime = Date.now() / 1000;
// eslint-disable-next-line @typescript-eslint/naming-convention
var Resource = '';
return new Promise(function (resolve) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
var endTime, params, e_3, id_1;
var _this = this;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
endTime = Date.now() / 1000;
params = {
DeviceId: deviceId,
ResourceName: ResourceName,
};
_a.label = 1;
case 1:
_a.trys.push([1, 3, , 4]);
return [4 /*yield*/, this.request('AppDescribeDeviceResource', params)];
case 2:
(Resource = (_a.sent()).Resource);
return [3 /*break*/, 4];
case 3:
e_3 = _a.sent();
return [3 /*break*/, 4];
case 4:
if (!Resource) {
id_1 = setInterval(function () { return tslib_1.__awaiter(_this, void 0, void 0, function () {
var e_4;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
endTime = Date.now() / 1000;
_a.label = 1;
case 1:
_a.trys.push([1, 3, , 4]);
return [4 /*yield*/, this.request('AppDescribeDeviceResource', params)];
case 2:
(Resource = (_a.sent()).Resource);
return [3 /*break*/, 4];
case 3:
e_4 = _a.sent();
return [3 /*break*/, 4];
case 4:
if (Resource || (endTime - startTime) > 36) {
clearInterval(id_1);
resolve(Resource);
}
return [2 /*return*/];
}
});
}); }, 5000);
}
else {
resolve(Resource);
}
return [2 /*return*/];
}
});
}); });
};
FileSdk.prototype.controlDeviceResource = function (ResourceName, deviceId) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var params;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
params = {
DeviceId: deviceId,
ResourceName: ResourceName,
ResourceVer: '0.0.1',
Method: 'update',
};
return [4 /*yield*/, this.request('AppControlDeviceResource', params)];
case 1:
_a.sent();
return [2 /*return*/];
}
});
});
};
return FileSdk;
}());
exports.default = FileSdk;
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var tslib_1=require("tslib"),FileSdk=function(){function e(e){var t=this;this.sdk=e,this.request=function(e,r){return t.sdk.requestTokenApi?t.sdk.requestTokenApi(e,r):t.sdk.requestApi(e,r)}}return e.prototype.getResourceUploadURL=function(e,t,r){return tslib_1.__awaiter(this,void 0,void 0,(function(){return tslib_1.__generator(this,(function(s){try{return[2,this.request("AppGetResourceUploadURL",{ProductId:r,UserResourceName:t,FileSize:e.size,ResourceVer:"0.0.1",ReadProtect:1})]}catch(e){console.log("AppGetResourceUploadURL错误")}return[2]}))}))},e.prototype.createResource=function(e,t,r){return tslib_1.__awaiter(this,void 0,void 0,(function(){return tslib_1.__generator(this,(function(s){return[2,this.request("AppCreateProductResource",{ProductId:r,UserResourceName:t,FileSize:e.size,ResourceVer:"0.0.1",FileHash:t,ResourceType:"FILE",ReadProtect:1})]}))}))},e.prototype.getDeviceResource=function(e,t){var r=this,s=Date.now()/1e3,i="";return new Promise((function(o){return tslib_1.__awaiter(r,void 0,void 0,(function(){var r,n,u,c=this;return tslib_1.__generator(this,(function(a){switch(a.label){case 0:r=Date.now()/1e3,n={DeviceId:t,ResourceName:e},a.label=1;case 1:return a.trys.push([1,3,,4]),[4,this.request("AppDescribeDeviceResource",n)];case 2:return i=a.sent().Resource,[3,4];case 3:return a.sent(),[3,4];case 4:return i?o(i):u=setInterval((function(){return tslib_1.__awaiter(c,void 0,void 0,(function(){return tslib_1.__generator(this,(function(e){switch(e.label){case 0:r=Date.now()/1e3,e.label=1;case 1:return e.trys.push([1,3,,4]),[4,this.request("AppDescribeDeviceResource",n)];case 2:return i=e.sent().Resource,[3,4];case 3:return e.sent(),[3,4];case 4:return(i||r-s>36)&&(clearInterval(u),o(i)),[2]}}))}))}),5e3),[2]}}))}))}))},e.prototype.controlDeviceResource=function(e,t){return tslib_1.__awaiter(this,void 0,void 0,(function(){var r;return tslib_1.__generator(this,(function(s){switch(s.label){case 0:return r={DeviceId:t,ResourceName:e,ResourceVer:"0.0.1",Method:"update"},[4,this.request("AppControlDeviceResource",r)];case 1:return s.sent(),[2]}}))}))},e}();exports.default=FileSdk;
//# sourceMappingURL=FileSdk.js.map

@@ -1,9 +0,7 @@

import FileSdk from './FileSdk';
import FileSdk, { ResourceUploadResponse } from './FileSdk';
export declare class FileSdkForH5 extends FileSdk {
appGetResourceUploadURL(file: any): Promise<{
ResourceName: string;
}>;
handleUpload(UploadUrl: any, file: any, hash: any): Promise<void>;
getfileHash(file: Blob): Promise<unknown>;
handelDownToDevice(ResourceName: any): Promise<unknown>;
appGetResourceUploadURL(file: any): Promise<ResourceUploadResponse>;
handleUpload(UploadUrl: string, file: any, hash: unknown): void;
getfileHash(file: Blob): Promise<any>;
handelDownToDevice(ResourceName: string): Promise<string>;
}

@@ -1,114 +0,2 @@

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.FileSdkForH5 = void 0;
var tslib_1 = require("tslib");
var FileSdk_1 = tslib_1.__importDefault(require("./FileSdk"));
var spark_md5_1 = tslib_1.__importDefault(require("spark-md5"));
var FileSdkForH5 = /** @class */ (function (_super) {
tslib_1.__extends(FileSdkForH5, _super);
function FileSdkForH5() {
return _super !== null && _super.apply(this, arguments) || this;
}
FileSdkForH5.prototype.appGetResourceUploadURL = function (file) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var fileHash, resourceName;
var _this = this;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!file) {
this.sdk.tips.showError('请先选择文件');
return [2 /*return*/];
}
fileHash = this.getfileHash(file);
resourceName = '';
return [4 /*yield*/, fileHash.then(function (hash) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
var res;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, _super.prototype.getResourceUploadURL.call(this, file, hash, this.sdk.productId)];
case 1:
res = _a.sent();
this.handleUpload(res === null || res === void 0 ? void 0 : res.UploadUrl, file, hash);
resourceName = res === null || res === void 0 ? void 0 : res.ResourceName;
return [2 /*return*/];
}
});
}); })];
case 1:
_a.sent();
return [2 /*return*/, { ResourceName: resourceName }];
}
});
});
};
// 上传文件 XHR方式
FileSdkForH5.prototype.handleUpload = function (UploadUrl, file, hash) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var xhr;
var _this = this;
return tslib_1.__generator(this, function (_a) {
xhr = new XMLHttpRequest();
xhr.open('PUT', UploadUrl, true);
xhr.onreadystatechange = function () { return tslib_1.__awaiter(_this, void 0, void 0, function () {
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0:
if (!(xhr.readyState === 4 && xhr.status === 200)) return [3 /*break*/, 2];
return [4 /*yield*/, _super.prototype.createResource.call(this, file, hash, this.sdk.productId, 'h5')];
case 1:
_a.sent(); // 创建资源
_a.label = 2;
case 2: return [2 /*return*/];
}
});
}); };
xhr.send(file);
return [2 /*return*/];
});
});
};
// 小文件获取hash值
FileSdkForH5.prototype.getfileHash = function (file) {
var _this = this;
return new Promise(function (resolve) {
var spark = new spark_md5_1.default.ArrayBuffer();
var fileReader = new FileReader();
fileReader.onload = function (e) {
if (e === null || e === void 0 ? void 0 : e.target) {
var result = e.target.result;
spark.append(result);
resolve(spark.end());
}
};
fileReader.onerror = function () {
_this.sdk.tips.showError("\u4E0A\u4F20\u6587\u4EF6\u9519\u8BEF" + fileReader.error);
};
fileReader.readAsArrayBuffer(file);
});
};
FileSdkForH5.prototype.handelDownToDevice = function (ResourceName) {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var result;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, _super.prototype.controlDeviceResource.call(this, ResourceName, this.sdk.deviceId)];
case 1:
_a.sent();
this.sdk.tips.showLoading('文件下发中', {
type: 'loading',
canBeReplace: true,
duration: 0,
delayDuration: 200,
canClickClose: false,
});
result = _super.prototype.getDeviceResource.call(this, ResourceName, this.sdk.deviceId);
return [2 /*return*/, result];
}
});
});
};
return FileSdkForH5;
}(FileSdk_1.default));
exports.FileSdkForH5 = FileSdkForH5;
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.FileSdkForH5=void 0;var tslib_1=require("tslib"),FileSdk_1=tslib_1.__importDefault(require("./FileSdk")),spark_md5_1=tslib_1.__importDefault(require("spark-md5")),FileSdkForH5=function(e){function t(){return null!==e&&e.apply(this,arguments)||this}return tslib_1.__extends(t,e),t.prototype.appGetResourceUploadURL=function(t){return tslib_1.__awaiter(this,void 0,void 0,(function(){var r,i,s=this;return tslib_1.__generator(this,(function(o){switch(o.label){case 0:return t?(r=this.getfileHash(t),i="",[4,r.then((function(r){return tslib_1.__awaiter(s,void 0,void 0,(function(){var s;return tslib_1.__generator(this,(function(o){switch(o.label){case 0:return[4,e.prototype.getResourceUploadURL.call(this,t,r,this.sdk.productId)];case 1:return s=o.sent(),this.handleUpload(null==s?void 0:s.UploadUrl,t,r),i=null==s?void 0:s.ResourceName,[2]}}))}))}))]):(this.sdk.tips.showError("请先选择文件"),[2]);case 1:return o.sent(),[2,{ResourceName:i}]}}))}))},t.prototype.handleUpload=function(t,r,i){var s=this,o=new XMLHttpRequest;o.open("PUT",t,!0),o.onreadystatechange=function(){return tslib_1.__awaiter(s,void 0,void 0,(function(){var t;return tslib_1.__generator(this,(function(s){switch(s.label){case 0:if(4!==o.readyState||200!==o.status)return[3,4];s.label=1;case 1:return s.trys.push([1,3,,4]),[4,e.prototype.createResource.call(this,r,i,this.sdk.productId)];case 2:return s.sent().ResourceName&&this.sdk.tips.showSuccess("文件上传成功"),[3,4];case 3:return t=s.sent(),this.sdk.tips.showError("上传失败,"+t.msg),[3,4];case 4:return[2]}}))}))},o.send(r)},t.prototype.getfileHash=function(e){var t=this;return new Promise((function(r){var i=new spark_md5_1.default.ArrayBuffer,s=new FileReader;s.onload=function(e){if(null==e?void 0:e.target){var t=e.target.result;i.append(t),r(i.end())}},s.onerror=function(){t.sdk.tips.showError("上传文件错误"+s.error)},s.readAsArrayBuffer(e)}))},t.prototype.handelDownToDevice=function(t){return tslib_1.__awaiter(this,void 0,void 0,(function(){return tslib_1.__generator(this,(function(r){switch(r.label){case 0:return[4,e.prototype.controlDeviceResource.call(this,t,this.sdk.deviceId)];case 1:return r.sent(),this.sdk.tips.showLoading("文件下发中",{type:"loading",canBeReplace:!0,duration:0,delayDuration:200,canClickClose:!1}),[2,e.prototype.getDeviceResource.call(this,t,this.sdk.deviceId)]}}))}))},t}(FileSdk_1.default);exports.FileSdkForH5=FileSdkForH5;
//# sourceMappingURL=FileSdkForH5.js.map
import FileSdk from './FileSdk';
export declare class FileSdkForMiniProgram extends FileSdk {
handleUpload(file: any, productId: any): Promise<unknown>;
handleUpload(file: any, productId: any): Promise<string>;
}

@@ -1,56 +0,2 @@

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.FileSdkForMiniProgram = void 0;
var tslib_1 = require("tslib");
var FileSdk_1 = tslib_1.__importDefault(require("./FileSdk"));
var spark_md5_1 = tslib_1.__importDefault(require("spark-md5"));
var FileSdkForMiniProgram = /** @class */ (function (_super) {
tslib_1.__extends(FileSdkForMiniProgram, _super);
function FileSdkForMiniProgram() {
return _super !== null && _super.apply(this, arguments) || this;
}
FileSdkForMiniProgram.prototype.handleUpload = function (file, productId) {
var _this = this;
return new Promise(function (resolve) {
var fs = wx.getFileSystemManager();
fs.readFile({
filePath: file.path,
success: function (res) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
var spark, hash, _a, UploadUrl, ResourceName;
var _this = this;
return tslib_1.__generator(this, function (_b) {
switch (_b.label) {
case 0:
spark = new spark_md5_1.default.ArrayBuffer();
spark.append(res.data);
hash = spark.end(false);
return [4 /*yield*/, _super.prototype.getResourceUploadURL.call(this, file, hash, productId)];
case 1:
_a = _b.sent(), UploadUrl = _a.UploadUrl, ResourceName = _a.ResourceName;
wx.request({
url: UploadUrl,
method: 'PUT',
data: fs.readFileSync(file.path),
success: function () { return tslib_1.__awaiter(_this, void 0, void 0, function () {
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, _super.prototype.createResource.call(this, file, hash, productId, 'mini')];
case 1:
_a.sent();
return [2 /*return*/];
}
});
}); },
});
resolve(ResourceName);
return [2 /*return*/];
}
});
}); },
});
});
};
return FileSdkForMiniProgram;
}(FileSdk_1.default));
exports.FileSdkForMiniProgram = FileSdkForMiniProgram;
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.FileSdkForMiniProgram=void 0;var tslib_1=require("tslib"),FileSdk_1=tslib_1.__importDefault(require("./FileSdk")),spark_md5_1=tslib_1.__importDefault(require("spark-md5")),FileSdkForMiniProgram=function(e){function r(){return null!==e&&e.apply(this,arguments)||this}return tslib_1.__extends(r,e),r.prototype.handleUpload=function(r,t){var i=this;return new Promise((function(s,a){var n=wx.getFileSystemManager();n.readFile({filePath:r.path,success:function(o){return tslib_1.__awaiter(i,void 0,void 0,(function(){var i,u,l,c=this;return tslib_1.__generator(this,(function(d){switch(d.label){case 0:return(i=new spark_md5_1.default.ArrayBuffer).append(o.data),u=i.end(!1),[4,e.prototype.getResourceUploadURL.call(this,r,u,t)];case 1:return l=d.sent().UploadUrl,wx.request({url:l,method:"PUT",data:n.readFileSync(r.path),success:function(){return tslib_1.__awaiter(c,void 0,void 0,(function(){var i,n;return tslib_1.__generator(this,(function(o){switch(o.label){case 0:return o.trys.push([0,2,,3]),[4,e.prototype.createResource.call(this,r,u,t)];case 1:return(i=o.sent().ResourceName)&&(wx.showToast({title:"文件上传成功",icon:"success",duration:5e3}),s(i)),[3,3];case 2:return n=o.sent(),a(n.msg),wx.showToast({title:""+n.msg,icon:"error",duration:5e3}),[3,3];case 3:return[2]}}))}))}}),[2]}}))}))}})}))},r}(FileSdk_1.default);exports.FileSdkForMiniProgram=FileSdkForMiniProgram;
//# sourceMappingURL=FileSdkForMiniprogram.js.map

@@ -1,9 +0,2 @@

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.FileSdkForH5 = exports.FileSdkForMiniProgram = void 0;
var FileSdkForH5_1 = require("./FileSdkForH5");
Object.defineProperty(exports, "FileSdkForH5", { enumerable: true, get: function () { return FileSdkForH5_1.FileSdkForH5; } });
var FileSdkForMiniprogram_1 = require("./FileSdkForMiniprogram");
Object.defineProperty(exports, "FileSdkForMiniProgram", { enumerable: true, get: function () { return FileSdkForMiniprogram_1.FileSdkForMiniProgram; } });
// lerna test
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.FileSdkForH5=exports.FileSdkForMiniProgram=void 0;var FileSdkForH5_1=require("./FileSdkForH5");Object.defineProperty(exports,"FileSdkForH5",{enumerable:!0,get:function(){return FileSdkForH5_1.FileSdkForH5}});var FileSdkForMiniprogram_1=require("./FileSdkForMiniprogram");Object.defineProperty(exports,"FileSdkForMiniProgram",{enumerable:!0,get:function(){return FileSdkForMiniprogram_1.FileSdkForMiniProgram}});
//# sourceMappingURL=index.js.map
{
"name": "qcloud-iotexplorer-fileresource-sdk",
"version": "1.3.4",
"version": "1.4.2",
"main": "lib/index.js",

@@ -9,3 +9,3 @@ "dependencies": {

"scripts": {
"build:lib": "rm -rf lib && npx tsc -p tsconfig.json -t es5 --outDir lib && node ../../bin/minify.js -p insight-reporter -t lib",
"build:lib": "rm -rf lib && npx tsc -p tsconfig.json -t es5 --outDir lib && node ../../bin/minify.js -p qcloud-iotexplorer-fileresource-sdk -t lib",
"build": "npm run build:lib",

@@ -20,3 +20,3 @@ "dev": "rm -rf lib && npx tsc -p tsconfig.json -t es5 --outDir lib --watch"

},
"gitHead": "1c09d413c86f6e2089017267260533452fc8cf2e"
"gitHead": "902a3ca640cb46524c3f7a1e9f66897db59be629"
}

@@ -0,5 +1,14 @@

export interface ResourceUploadResponse {
RequestId?: string;
ResourceName?: string;
UploadUrl?: string;
}
export interface ResourceCreateResponse {
RequestId?: string;
ResourceName?: string;
}
abstract class FileSdk {
protected sdk: any;
protected request: any;
protected request: (action, params) => Promise<any>;
constructor(sdk: any) {

@@ -16,5 +25,5 @@ this.sdk = sdk;

async getResourceUploadURL(file, hash, productId) {
async getResourceUploadURL(file: any, hash: unknown, productId: string): Promise<ResourceUploadResponse> {
try {
return await this.request('AppGetResourceUploadURL', {
return this.request('AppGetResourceUploadURL', {
ProductId: productId,

@@ -30,38 +39,14 @@ UserResourceName: hash, // 不能传中文且每个文件必须独一无二, 所以传hash值代替

}
async createResource(file: any, hash: unknown, productId, type) {
try {
// eslint-disable-next-line @typescript-eslint/naming-convention
const { ResourceName } = await this.request('AppCreateProductResource', {
ProductId: productId,
UserResourceName: hash,
FileSize: file.size,
ResourceVer: '0.0.1',
FileHash: hash,
ResourceType: 'FILE',
ReadProtect: 1,
});
if (ResourceName) {
if (type === 'h5') {
this.sdk.tips.showSuccess('文件上传成功');
} else {
wx.showToast({
title: '文件上传成功',
icon: 'success',
duration: 5000,
});
}
}
} catch (e) {
if (type === 'h5') {
this.sdk.tips.showError(`上传失败,${e.msg}`);
} else {
wx.showToast({
title: `${e.msg}`,
icon: 'error',
duration: 5000,
});
}
}
async createResource(file: any, hash: unknown, productId: string): Promise<ResourceCreateResponse> {
return this.request('AppCreateProductResource', {
ProductId: productId,
UserResourceName: hash,
FileSize: file.size,
ResourceVer: '0.0.1',
FileHash: hash,
ResourceType: 'FILE',
ReadProtect: 1,
});
}
getDeviceResource(ResourceName: any, deviceId) {
getDeviceResource(ResourceName: any, deviceId: string): Promise<string> {
const startTime = Date.now() / 1000;

@@ -97,3 +82,3 @@ // eslint-disable-next-line @typescript-eslint/naming-convention

}
async controlDeviceResource(ResourceName, deviceId) {
async controlDeviceResource(ResourceName: string, deviceId: string): Promise<void> {
const params = {

@@ -100,0 +85,0 @@ DeviceId: deviceId,

@@ -1,6 +0,6 @@

import FileSdk from './FileSdk';
import FileSdk, { ResourceUploadResponse } from './FileSdk';
import SparkMD5 from 'spark-md5';
export class FileSdkForH5 extends FileSdk {
async appGetResourceUploadURL(file: any) {
async appGetResourceUploadURL(file: any): Promise<ResourceUploadResponse> {
if (!file) {

@@ -21,3 +21,3 @@ this.sdk.tips.showError('请先选择文件');

// 上传文件 XHR方式
async handleUpload(UploadUrl, file, hash) {
handleUpload(UploadUrl: string, file: any, hash: unknown): void {
const xhr = new XMLHttpRequest();

@@ -27,3 +27,11 @@ xhr.open('PUT', UploadUrl, true);

if (xhr.readyState === 4 && xhr.status === 200) {
await super.createResource(file, hash, this.sdk.productId, 'h5'); // 创建资源
try {
// eslint-disable-next-line @typescript-eslint/naming-convention
const { ResourceName } = await super.createResource(file, hash, this.sdk.productId);
if (ResourceName) {
this.sdk.tips.showSuccess('文件上传成功');
}
} catch (e) {
this.sdk.tips.showError(`上传失败,${e.msg}`);
}
}

@@ -35,3 +43,3 @@ };

// 小文件获取hash值
getfileHash(file: Blob) {
getfileHash(file: Blob): Promise<any> {
return new Promise((resolve) => {

@@ -55,3 +63,3 @@ const spark = new SparkMD5.ArrayBuffer();

async handelDownToDevice(ResourceName: any) {
async handelDownToDevice(ResourceName: string): Promise<string> {
await super.controlDeviceResource(ResourceName, this.sdk.deviceId);

@@ -58,0 +66,0 @@ this.sdk.tips.showLoading('文件下发中', {

@@ -6,4 +6,4 @@ import FileSdk from './FileSdk';

export class FileSdkForMiniProgram extends FileSdk {
handleUpload(file, productId) {
return new Promise((resolve) => {
handleUpload(file, productId): Promise<string> {
return new Promise((resolve, reject) => {
const fs = wx.getFileSystemManager();

@@ -17,3 +17,3 @@ fs.readFile({

// eslint-disable-next-line @typescript-eslint/naming-convention
const { UploadUrl, ResourceName } = await super.getResourceUploadURL(file, hash, productId);
const { UploadUrl } = await super.getResourceUploadURL(file, hash, productId);
wx.request({

@@ -24,6 +24,23 @@ url: UploadUrl,

success: async () => {
await super.createResource(file, hash, productId, 'mini');
try {
// eslint-disable-next-line @typescript-eslint/naming-convention
const { ResourceName } = await super.createResource(file, hash, productId);
if (ResourceName) {
wx.showToast({
title: '文件上传成功',
icon: 'success',
duration: 5000,
});
resolve(ResourceName);
}
} catch (e) {
reject(e.msg);
wx.showToast({
title: `${e.msg}`,
icon: 'error',
duration: 5000,
});
}
},
});
resolve(ResourceName);
},

@@ -30,0 +47,0 @@ });

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc