diff --git a/android/src/main/java/cn/reactnative/modules/update/UpdateContext.java b/android/src/main/java/cn/reactnative/modules/update/UpdateContext.java index e533ee7..6d9fa74 100644 --- a/android/src/main/java/cn/reactnative/modules/update/UpdateContext.java +++ b/android/src/main/java/cn/reactnative/modules/update/UpdateContext.java @@ -7,6 +7,8 @@ import android.content.pm.PackageManager; import android.util.Log; import com.facebook.react.ReactInstanceManager; +import java.util.HashMap; +import java.util.Map; import java.util.concurrent.Executor; import java.util.concurrent.Executors; @@ -68,6 +70,17 @@ public class UpdateContext { return context.getString(R.string.pushy_build_time); } + public String getUuid() { + return sp.getString("uuid", null); + } + + public Map getBlockUpdate() { + return new HashMap() {{ + put("until", sp.getInt("blockUntil", 0)); + put("reason", sp.getString("blockReason", null)); + }}; + } + public boolean getIsUsingBundleUrl() { return isUsingBundleUrl; } @@ -130,6 +143,19 @@ public class UpdateContext { editor.apply(); } + public void setUuid(String uuid) { + SharedPreferences.Editor editor = sp.edit(); + editor.putString("uuid", uuid); + editor.apply(); + } + + public void setBlockUpdate(int until, String reason) { + SharedPreferences.Editor editor = sp.edit(); + editor.putInt("blockUntil", until); + editor.putString("blockReason", reason); + editor.apply(); + } + public String getCurrentVersion() { return sp.getString("currentVersion", null); } diff --git a/android/src/main/java/cn/reactnative/modules/update/UpdateModule.java b/android/src/main/java/cn/reactnative/modules/update/UpdateModule.java index 24ac18d..911943a 100644 --- a/android/src/main/java/cn/reactnative/modules/update/UpdateModule.java +++ b/android/src/main/java/cn/reactnative/modules/update/UpdateModule.java @@ -57,6 +57,8 @@ public class UpdateModule extends ReactContextBaseJavaModule{ if (isRolledBack) { updateContext.clearRollbackMark(); } + constants.put("blockUpdate", updateContext.getBlockUpdate()); + constants.put("uuid", updateContext.getUuid()); return constants; } @@ -184,6 +186,28 @@ public class UpdateModule extends ReactContextBaseJavaModule{ }); } + @ReactMethod + public void setBlockUpdate(ReadableMap options) { + final int until = options.getInt("until"); + final String reason = options.getString("reason"); + UiThreadUtil.runOnUiThread(new Runnable() { + @Override + public void run() { + updateContext.setBlockUpdate(until, reason); + } + }); + } + + @ReactMethod + public void setUuid(final String uuid) { + UiThreadUtil.runOnUiThread(new Runnable() { + @Override + public void run() { + updateContext.setUuid(uuid); + } + }); + } + /* 发送事件*/ public static void sendEvent(String eventName, WritableMap params) { ((ReactContext) mContext).getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class).emit(eventName, diff --git a/lib/index.js b/lib/index.js index 7b33f50..ca3ce3f 100644 --- a/lib/index.js +++ b/lib/index.js @@ -9,7 +9,7 @@ const { version: v, } = require('react-native/Libraries/Core/ReactNativeVersion'); const RNVersion = `${v.major}.${v.minor}.${v.patch}`; -import { v4 as uuidv4 } from 'uuid'; +const uuidv4 = require('uuid/v4'); let Pushy = NativeModules.Pushy; @@ -70,16 +70,26 @@ function assertRelease() { export async function checkUpdate(APPKEY, isRetry) { assertRelease(); - if (blockUpdate && blockUpdate.until > Date.now()) { - console.warn( - `Pushy update is blocked until ${new Date( - blockUpdate.until, - ).toLocaleString()}. Reason: ${blockUpdate.reason}`, + if (blockUpdate && blockUpdate.until > Date.now() / 1000) { + throw new Error( + `热更新已暂停,原因:${blockUpdate.reason}。请在"${new Date( + blockUpdate.until * 1000, + ).toLocaleString()}"之后重试。`, ); - return; } let resp; try { + console.warn({ + packageVersion, + hash: currentVersion, + buildTime, + cInfo: { + pushy: require('../package.json').version, + rn: RNVersion, + os: Platform.OS + ' ' + Platform.Version, + uuid, + }, + }); resp = await fetch(getCheckUrl(APPKEY), { method: 'POST', headers: { @@ -105,25 +115,25 @@ export async function checkUpdate(APPKEY, isRetry) { await tryBackupEndpoints(APPKEY); return checkUpdate(APPKEY, true); } - - checkOperation(resp); + const result = await resp.json(); + checkOperation(result.op); if (resp.status !== 200) { - throw new Error((await resp.json()).message); + throw new Error(result.message); } - return resp.json(); + return result; } -function checkOperation(resp) { - if (!Array.isArray(resp.op)) { +function checkOperation(op) { + if (!Array.isArray(op)) { return; } - resp.op.forEach((action) => { + op.forEach((action) => { if (action.type === 'block') { blockUpdate = { reason: action.reason, - until: Date.now() + action.duration, + until: (Date.now() + action.duration) / 1000, }; Pushy.setBlockUpdate(blockUpdate); } diff --git a/package.json b/package.json index 3f02c2d..a64a71a 100644 --- a/package.json +++ b/package.json @@ -27,6 +27,6 @@ }, "homepage": "https://github.com/reactnativecn/react-native-pushy#readme", "dependencies": { - "uuid": "^8.3.0" + "uuid": "3" } } diff --git a/yarn.lock b/yarn.lock index cd35e2b..3aeeef0 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2,7 +2,7 @@ # yarn lockfile v1 -uuid@^8.3.0: - version "8.3.0" - resolved "https://registry.yarnpkg.com/uuid/-/uuid-8.3.0.tgz#ab738085ca22dc9a8c92725e459b1d507df5d6ea" - integrity sha512-fX6Z5o4m6XsXBdli9g7DtWgAx+osMsRRZFKma1mIUsLCz6vRvv+pz5VNbyu9UEDzpMWulZfvpgb/cmDXVulYFQ== +uuid@3: + version "3.4.0" + resolved "https://registry.yarnpkg.com/uuid/-/uuid-3.4.0.tgz#b23e4358afa8a202fe7a100af1f5f883f02007ee" + integrity sha512-HjSDRw6gZE5JMggctHBcjVak08+KEVhSIiDzFnT9S9aegmp85S/bReBVTb4QTFaRNptJ9kuYaNhnbNEOkbKb/A==