shcp-api-lib 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1027,6 +1027,183 @@ class CommonDictApi {
1027
1027
  }
1028
1028
  }
1029
1029
 
1030
+ // 七牛云区域上传URL映射常量
1031
+ const QINIU_UPLOAD_URLS = {
1032
+ ECN: 'https://up.qiniup.com',
1033
+ NCN: 'https://up-z1.qiniup.com',
1034
+ SCN: 'https://up-z2.qiniup.com',
1035
+ NA: 'https://up-na0.qiniup.com',
1036
+ ASG: 'https://up-as0.qiniup.com',
1037
+ };
1038
+ // 根据区域代码获取上传URL
1039
+ function getUploadURL(code) {
1040
+ return (code && QINIU_UPLOAD_URLS[code]) || null;
1041
+ }
1042
+ // 七牛云上传 API 类
1043
+ class _QiniuUploaderApi {
1044
+ constructor() {
1045
+ // 请参考demo的index.js中的initQiniu()方法,若在使用处对options进行了赋值,则此处config不需要赋默认值。init(options) 即updateConfigWithOptions(options),会对config进行赋值
1046
+ this.config = {
1047
+ // bucket 所在区域。ECN, SCN, NCN, NA, ASG,分别对应七牛云的:华东,华南,华北,北美,新加坡 5 个区域
1048
+ qiniuRegion: '',
1049
+ // 七牛云bucket 外链前缀,外链在下载资源时用到
1050
+ qiniuBucketURLPrefix: '',
1051
+ // 获取uptoken方法三选一即可,执行优先级为:uptoken > uptokenURL > uptokenFunc。三选一,剩下两个置空。推荐使用uptokenURL,详情请见 README.md
1052
+ // 由其他程序生成七牛云uptoken,然后直接写入uptoken
1053
+ qiniuUploadToken: '',
1054
+ // 从指定 url 通过 HTTP GET 获取 uptoken,返回的格式必须是 json 且包含 uptoken 字段,例如: {"uptoken": "0MLvWPnyy..."}
1055
+ qiniuUploadTokenURL: '',
1056
+ // qiniuShouldUseQiniuFileName 如果是 true,则文件的 key 由 qiniu 服务器分配(全局去重)。如果是 false,则文件的 key 使用微信自动生成的 filename。出于初代sdk用户升级后兼容问题的考虑,默认是 false。
1057
+ // 微信自动生成的 filename较长,导致fileURL较长。推荐使用{qiniuShouldUseQiniuFileName: true} + "通过fileURL下载文件时,自定义下载名" 的组合方式。
1058
+ // 自定义上传key 需要两个条件:1. 此处 shouldUseQiniuFileName 值为false。 2. 通过修改qiniuUploader.upload方法传入的options参数,可以进行自定义key。(请不要直接在sdk中修改options参数,修改方法请见demo的index.js)
1059
+ // 通过fileURL下载文件时,自定义下载名,请参考:七牛云"对象存储 > 产品手册 > 下载资源 > 下载设置 > 自定义资源下载名"(https://developer.qiniu.com/kodo/manual/1659/download-setting)。本sdk在README.md的"常见问题"板块中,有"通过fileURL下载文件时,自定义下载名"使用样例。
1060
+ qiniuShouldUseQiniuFileName: false,
1061
+ shouldUseQiniuFileName: false,
1062
+ fileType: '',
1063
+ };
1064
+ }
1065
+ // init(options) 将七牛云相关配置初始化进本sdk
1066
+ // 在整个程序生命周期中,只需要 init(options); 一次即可
1067
+ // 如果需要变更七牛云配置,再次调用 init(options); 即可
1068
+ init(options) {
1069
+ this.updateConfigWithOptions(options);
1070
+ }
1071
+ // 正式上传的前置方法,做预处理,应用七牛云配置
1072
+ async upload(filePath, suffix) {
1073
+ if (filePath == null) {
1074
+ throw new Error('qiniu uploader need filePath to upload');
1075
+ }
1076
+ const res = await this.getQiniuPolicy({
1077
+ tenantId: '000000',
1078
+ fileNames: (filePath.split('/').pop() || '') + '.' + suffix,
1079
+ });
1080
+ const result = res.data;
1081
+ if (result) {
1082
+ this.updateConfigWithOptions(result);
1083
+ // 自定义上传key(即自定义上传文件名)。通过修改qiniuUploader.upload方法传入的options参数,可以进行自定义文件名称。如果options非空,则使用options中的key作为fileName
1084
+ if (result.key) {
1085
+ filePath = result.key;
1086
+ }
1087
+ }
1088
+ if (this.config.qiniuUploadToken) {
1089
+ return await this.doUpload(filePath);
1090
+ }
1091
+ else if (this.config.qiniuUploadTokenURL) {
1092
+ const token = await this.getQiniuToken();
1093
+ if (token) {
1094
+ return await this.doUpload(filePath);
1095
+ }
1096
+ }
1097
+ throw new Error('qiniu uploader need one of [uptoken, uptokenURL]');
1098
+ }
1099
+ // 更新七牛云配置
1100
+ updateConfigWithOptions(options) {
1101
+ if (options.region) {
1102
+ this.config.qiniuRegion = options.region;
1103
+ }
1104
+ else {
1105
+ console.error('qiniu uploader need your bucket region');
1106
+ }
1107
+ if (options.uptoken) {
1108
+ this.config.qiniuUploadToken = options.uptoken;
1109
+ }
1110
+ else if (options.uptokenURL) {
1111
+ this.config.qiniuUploadTokenURL = options.uptokenURL;
1112
+ }
1113
+ if (options.domain) {
1114
+ this.config.qiniuBucketURLPrefix = options.domain;
1115
+ }
1116
+ if (options.shouldUseQiniuFileName !== undefined) {
1117
+ this.config.qiniuShouldUseQiniuFileName = options.shouldUseQiniuFileName;
1118
+ }
1119
+ if (options.fileType) {
1120
+ this.config.fileType = options.fileType;
1121
+ }
1122
+ if (options.uploadFile) {
1123
+ this.config.uploadFile = options.uploadFile;
1124
+ }
1125
+ }
1126
+ // 正式上传
1127
+ async doUpload(filePath) {
1128
+ if (this.config.qiniuUploadToken == null || this.config.qiniuUploadToken.length === 0) {
1129
+ throw new Error('qiniu UploadToken is null, please check the init config or networking');
1130
+ }
1131
+ const url = getUploadURL(this.config.qiniuRegion);
1132
+ if (!url) {
1133
+ throw new Error('Invalid qiniu region code');
1134
+ }
1135
+ const fileName = filePath.split('//')[1];
1136
+ const formData = {
1137
+ token: this.config.qiniuUploadToken,
1138
+ };
1139
+ // qiniuShouldUseQiniuFileName 如果是 true,则文件的 key 由 qiniu 服务器分配(全局去重)。如果是 false,则文件的 key 使用微信自动生成的 filename。出于初代sdk用户升级后兼容问题的考虑,默认是 false。
1140
+ if (!this.config.qiniuShouldUseQiniuFileName) {
1141
+ formData.key = fileName;
1142
+ }
1143
+ const config = this.config;
1144
+ const res = await this.config.uploadFile?.({
1145
+ url,
1146
+ filePath,
1147
+ name: 'file',
1148
+ formData,
1149
+ });
1150
+ const dataString = res.data;
1151
+ // // this if case is a compatibility with wechat server returned a charcode, but was fixed
1152
+ // if(res.data.hasOwnProperty('type') && res.data.type === 'Buffer'){
1153
+ // dataString = String.fromCharCode.apply(null, res.data.data)
1154
+ // }
1155
+ const dataObject = JSON.parse(dataString);
1156
+ // 拼接fileURL
1157
+ const fileURL = config.qiniuBucketURLPrefix + '/' + dataObject.key;
1158
+ const result = {
1159
+ ...dataObject,
1160
+ name: filePath,
1161
+ fileURL,
1162
+ // imageURL字段和fileURL字段重复,但本sdk不做删除,因为在最初版本使用的是imageURL。直接删除可能导致原有用户升级至新版sdk后出现异常。
1163
+ imageURL: fileURL,
1164
+ fileType: config.fileType,
1165
+ };
1166
+ return result;
1167
+ }
1168
+ // 获取七牛云uptoken, url为后端服务器获取七牛云uptoken接口
1169
+ async getQiniuToken() {
1170
+ const res = await ShcpApiSdk.request({
1171
+ url: this.config.qiniuUploadTokenURL,
1172
+ method: 'GET',
1173
+ });
1174
+ const data = res.data;
1175
+ const token = data.uptoken;
1176
+ if (token && token.length > 0) {
1177
+ this.config.qiniuUploadToken = token;
1178
+ }
1179
+ return token || '';
1180
+ }
1181
+ async getQiniuPolicy(data) {
1182
+ return ShcpApiSdk.request({
1183
+ url: `/api/blade-resource/oss/endpoint/uptoken?tenantId=${data.tenantId}&fileNames=${data.fileNames}`,
1184
+ method: 'POST',
1185
+ });
1186
+ }
1187
+ }
1188
+ const QiniuUploaderApi = new _QiniuUploaderApi();
1189
+
1190
+ class FileApi {
1191
+ static async uploadFileQiniu(file) {
1192
+ if (Array.isArray(file)) {
1193
+ return await Promise.all(file.map(async (item) => await FileApi.upload_file(item)));
1194
+ }
1195
+ else {
1196
+ return await FileApi.upload_file(file);
1197
+ }
1198
+ }
1199
+ static async upload_file(file) {
1200
+ console.log(" upload_file file=", file);
1201
+ file.url.split("/");
1202
+ const suffix = (file.name || file.path || file.cloudPath).split(".")[1];
1203
+ return await QiniuUploaderApi.upload(file.url, suffix);
1204
+ }
1205
+ }
1206
+
1030
1207
  class CommonRightApi {
1031
1208
  // 获取权益列表
1032
1209
  // URL: /api/shcp-healthcare/${ShcpApiSdk.getApiSuffix()}/right/list
@@ -2971,6 +3148,7 @@ exports.DeviceStatusOptions = DeviceStatusOptions;
2971
3148
  exports.DeviceTypeOptions = DeviceTypeOptions;
2972
3149
  exports.DeviceUserApi = DeviceUserApi;
2973
3150
  exports.DiseaseCareServiceFlowStatusOptions = DiseaseCareServiceFlowStatusOptions;
3151
+ exports.FileApi = FileApi;
2974
3152
  exports.FileTypeOptions = FileTypeOptions;
2975
3153
  exports.GenderOptions = GenderOptions;
2976
3154
  exports.GroupJoinTypeOptions = GroupJoinTypeOptions;