提交 7fbc160a authored 作者: vipcxj's avatar vipcxj

增加资源后台上传支持

上级 4f7fdfd6
......@@ -161,6 +161,7 @@ android {
}
dependencies {
compile project(':react-native-background-upload')
compile project(':react-native-code-push')
compile project(':react-native-device-info')
compile fileTree(dir: "libs", include: ["*.jar"])
......
......@@ -7,6 +7,7 @@ import android.support.multidex.MultiDex;
import com.bolan.android.modules.IDCardReactPackage;
import com.bolan.android.modules.UpdaterReactPackage;
import com.facebook.react.ReactApplication;
import com.vydia.RNUploader.UploaderReactPackage;
import com.microsoft.codepush.react.CodePush;
import com.learnium.RNDeviceInfo.RNDeviceInfo;
import com.facebook.react.ReactNativeHost;
......@@ -35,6 +36,7 @@ public class MainApplication extends Application implements ReactApplication {
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
new MainReactPackage(),
new UploaderReactPackage(),
new CodePush(BuildConfig.CODEPUSH_KEY, getApplicationContext(), BuildConfig.DEBUG, "http://192.168.1.2:3000"),
new RNDeviceInfo(),
new IDCardReactPackage(),
......
rootProject.name = 'bolanManagerApp'
include ':react-native-background-upload'
project(':react-native-background-upload').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-background-upload/android')
include ':react-native-code-push'
project(':react-native-code-push').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-code-push/android/app')
include ':react-native-device-info'
......
......@@ -40,6 +40,7 @@
53E9F8ADCC714D9693F0C068 /* libRNDeviceInfo-tvOS.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 83554850969D48C684F45F7F /* libRNDeviceInfo-tvOS.a */; };
1A1EABAFBB254C51AA95A884 /* libCodePush.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 5049A267195B4A6D95EE87C7 /* libCodePush.a */; };
BDC17299171149F2874B1AB8 /* libz.tbd in Frameworks */ = {isa = PBXBuildFile; fileRef = 83AC580FC9064DB19C36CE97 /* libz.tbd */; };
63F9C160CE05449786E0C256 /* libVydiaRNFileUploader.a in Frameworks */ = {isa = PBXBuildFile; fileRef = D60A2B4F96C34035A331240F /* libVydiaRNFileUploader.a */; };
/* End PBXBuildFile section */
/* Begin PBXContainerItemProxy section */
......@@ -273,6 +274,8 @@
F140EEF54B894D10B664588B /* CodePush.xcodeproj */ = {isa = PBXFileReference; name = "CodePush.xcodeproj"; path = "../node_modules/react-native-code-push/ios/CodePush.xcodeproj"; sourceTree = "<group>"; fileEncoding = undefined; lastKnownFileType = wrapper.pb-project; explicitFileType = undefined; includeInIndex = 0; };
5049A267195B4A6D95EE87C7 /* libCodePush.a */ = {isa = PBXFileReference; name = "libCodePush.a"; path = "libCodePush.a"; sourceTree = "<group>"; fileEncoding = undefined; lastKnownFileType = archive.ar; explicitFileType = undefined; includeInIndex = 0; };
83AC580FC9064DB19C36CE97 /* libz.tbd */ = {isa = PBXFileReference; name = "libz.tbd"; path = "usr/lib/libz.tbd"; sourceTree = SDKROOT; fileEncoding = undefined; lastKnownFileType = sourcecode.text-based-dylib-definition; explicitFileType = undefined; includeInIndex = 0; };
C6C24E880A174F99B0C84663 /* VydiaRNFileUploader.xcodeproj */ = {isa = PBXFileReference; name = "VydiaRNFileUploader.xcodeproj"; path = "../node_modules/react-native-background-upload/ios/VydiaRNFileUploader.xcodeproj"; sourceTree = "<group>"; fileEncoding = undefined; lastKnownFileType = wrapper.pb-project; explicitFileType = undefined; includeInIndex = 0; };
D60A2B4F96C34035A331240F /* libVydiaRNFileUploader.a */ = {isa = PBXFileReference; name = "libVydiaRNFileUploader.a"; path = "libVydiaRNFileUploader.a"; sourceTree = "<group>"; fileEncoding = undefined; lastKnownFileType = archive.ar; explicitFileType = undefined; includeInIndex = 0; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
......@@ -305,6 +308,7 @@
53E9F8ADCC714D9693F0C068 /* libRNDeviceInfo-tvOS.a in Frameworks */,
1A1EABAFBB254C51AA95A884 /* libCodePush.a in Frameworks */,
BDC17299171149F2874B1AB8 /* libz.tbd in Frameworks */,
63F9C160CE05449786E0C256 /* libVydiaRNFileUploader.a in Frameworks */,
);
runOnlyForDeploymentPostprocessing = 0;
};
......@@ -475,6 +479,7 @@
139FDEE61B06529A00C62182 /* RCTWebSocket.xcodeproj */,
20C53C1323F44CCBB35F0247 /* RNDeviceInfo.xcodeproj */,
F140EEF54B894D10B664588B /* CodePush.xcodeproj */,
C6C24E880A174F99B0C84663 /* VydiaRNFileUploader.xcodeproj */,
);
name = Libraries;
sourceTree = "<group>";
......@@ -1035,6 +1040,7 @@
"\"$(SRCROOT)/$(TARGET_NAME)\"",
"\"$(SRCROOT)/$(TARGET_NAME)\"",
"\"$(SRCROOT)/$(TARGET_NAME)\"",
"\"$(SRCROOT)/$(TARGET_NAME)\"",
);
HEADER_SEARCH_PATHS = (
"$(inherited)",
......@@ -1063,6 +1069,7 @@
"\"$(SRCROOT)/$(TARGET_NAME)\"",
"\"$(SRCROOT)/$(TARGET_NAME)\"",
"\"$(SRCROOT)/$(TARGET_NAME)\"",
"\"$(SRCROOT)/$(TARGET_NAME)\"",
);
HEADER_SEARCH_PATHS = (
"$(inherited)",
......@@ -1145,6 +1152,7 @@
"\"$(SRCROOT)/$(TARGET_NAME)\"",
"\"$(SRCROOT)/$(TARGET_NAME)\"",
"\"$(SRCROOT)/$(TARGET_NAME)\"",
"\"$(SRCROOT)/$(TARGET_NAME)\"",
);
HEADER_SEARCH_PATHS = (
"$(inherited)",
......@@ -1182,6 +1190,7 @@
"\"$(SRCROOT)/$(TARGET_NAME)\"",
"\"$(SRCROOT)/$(TARGET_NAME)\"",
"\"$(SRCROOT)/$(TARGET_NAME)\"",
"\"$(SRCROOT)/$(TARGET_NAME)\"",
);
HEADER_SEARCH_PATHS = (
"$(inherited)",
......@@ -1214,6 +1223,7 @@
"\"$(SRCROOT)/$(TARGET_NAME)\"",
"\"$(SRCROOT)/$(TARGET_NAME)\"",
"\"$(SRCROOT)/$(TARGET_NAME)\"",
"\"$(SRCROOT)/$(TARGET_NAME)\"",
);
};
name = Debug;
......@@ -1241,6 +1251,7 @@
"\"$(SRCROOT)/$(TARGET_NAME)\"",
"\"$(SRCROOT)/$(TARGET_NAME)\"",
"\"$(SRCROOT)/$(TARGET_NAME)\"",
"\"$(SRCROOT)/$(TARGET_NAME)\"",
);
};
name = Release;
......
......@@ -41,6 +41,7 @@
"rc-form": "^1.4.8",
"react": "^16.0.0-beta.5",
"react-native": "^0.49.3",
"react-native-background-upload": "^4.1.0",
"react-native-code-push": "^5.1.3-beta",
"react-native-device-info": "^0.12.1",
"react-native-fetch-polyfill": "^1.1.2",
......
import Upload from 'react-native-background-upload';
import { castArray } from 'lodash';
import post from '../utils/post';
import config from '../utils/config';
import request from '../utils/request';
import { getToken } from '../utils/auth';
import config from '../utils/config';
export class Operations {
constructor() {
......@@ -77,10 +79,6 @@ export async function queryResourceByUsage(usage, pst = -1, psz = -1) {
return request(`${config.apiContextPath}/api/resource/user/query`, { usage, pst, psz });
}
export async function queryResourceByUsageStartWith(usage, pst = -1, psz = -1) {
return request(`${config.apiContextPath}/api/resource/user/query`, { usage, startWith: true, pst, psz });
}
export async function queryResourceByAllTags(tags, pst = -1, psz = -1) {
return request(`${config.apiContextPath}/api/resource/user/query`, { tags: castArray(tags), pst, psz });
}
......@@ -97,10 +95,108 @@ export async function queryResourceByUsageAndAnyTags(usage, tags, pst = -1, psz
return request(`${config.apiContextPath}/api/resource/user/query`, { usage, tags: castArray(tags), andOr: 'or', pst, psz });
}
export async function queryResourceByUsageStartWithAndAllTags(usage, tags, pst = -1, psz = -1) {
return request(`${config.apiContextPath}/api/resource/user/query`, { usage, startWith: true, tags: castArray(tags), pst, psz });
}
/**
* @callback UploadErrorCallback
* @param {string} uploadId 上传id,用于标识上传任务
* @param {Error} error 错误异常
*/
/**
* @callback UploadCompleteCallback
* @param {string} uploadId 上传id,用于标识上传任务
* @param {string} uri 上传完成后,服务端返回的内部uri,这个uri可以用于后续定位上传的资源
*/
export async function queryResourceByUsageStartWithAndAnyTags(usage, tags, pst = -1, psz = -1) {
return request(`${config.apiContextPath}/api/resource/user/query`, { usage, startWith: true, tags: castArray(tags), andOr: 'or', pst, psz });
/**
* @callback UploadProgressCallback
* @param {string} uploadId 上传id,用于标识上传任务
* @param {number} progress 上传进度,0-100的整数
*/
/**
* 上传文件,支持后台上传
* @param path 文件绝对路径
* @param {UploadErrorCallback} [error] 错误回调,上传失败会调用此回调,并传入错误异常
* @param {UploadCompleteCallback} [complete] 成功回调,上传成功会调用此回调,并传入服务端资源内部uri
* @param {UploadProgressCallback} [progress] 上传进度回调,可以获得进度
* @param {string} [uri] 服务端资源内部uri,可用于覆盖已有资源
* @return {Promise<string>} uploadId 上传id,用于引用上传任务
*/
export async function uploadResource(path, error = () => {}, complete = () => {}, progress = () => {}, uri) {
const { exists, mimeType } = await Upload.getFileInfo(path);
if (exists) {
const token = await getToken();
let url;
if (uri) {
url = `${config.apiContextPath}/resource/${token}/${encodeURIComponent(uri)}`;
} else {
url = `${config.apiContextPath}/resource/${token}`;
}
const uploadId = await Upload.startUpload({
url,
path,
method: 'POST',
field: 'file',
type: 'multipart',
headers: {
'content-type': mimeType,
},
});
let completeHandle;
const progressHandle = Upload.addListener('progress', uploadId, (data) => {
progress(uploadId, data.progress);
});
const errorHandle = Upload.addListener('error', uploadId, (data) => {
try {
error(uploadId, data.error);
} finally {
progressHandle.remove();
completeHandle.remove();
errorHandle.remove();
}
});
completeHandle = Upload.addListener('completed', uploadId, (event) => {
try {
const { responseCode, responseBody } = event;
if (responseCode >= 200 && responseCode < 300) {
try {
const response = JSON.parse(responseBody);
if (response.errorCode != null && response.errorCode === 0) {
complete(uploadId, response.data[0]);
} else if (response.errorCode != null) {
const err = new Error(response.message);
err.data = response;
error(uploadId, err);
} else {
error(uploadId, new Error(`Invalid response: ${responseBody}.`));
}
} catch (err) {
error(uploadId, new Error(`Invalid response: ${responseBody}.`));
}
} else {
try {
const response = JSON.parse(responseBody);
if (response.message) {
const err = new Error(response.message);
err.data = response;
error(uploadId, err);
} else if (response.data) {
const err = new Error(response.data);
err.data = response;
error(uploadId, err);
} else {
error(uploadId, new Error(responseBody));
}
} catch (err) {
error(uploadId, new Error(responseBody));
}
}
} finally {
progressHandle.remove();
completeHandle.remove();
errorHandle.remove();
}
});
return uploadId;
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论