mirror of
				https://gitcode.com/gh_mirrors/re/react-native-pushy.git
				synced 2025-10-31 21:33:12 +08:00 
			
		
		
		
	Add report
This commit is contained in:
		| @@ -152,7 +152,7 @@ public class UpdateContext { | |||||||
|         } |         } | ||||||
|         editor.putBoolean("firstTime", true); |         editor.putBoolean("firstTime", true); | ||||||
|         editor.putBoolean("firstTimeOk", false); |         editor.putBoolean("firstTimeOk", false); | ||||||
|         editor.putBoolean("rolledBack", false); |         editor.putString("rolledBackVersion", null); | ||||||
|         editor.apply(); |         editor.apply(); | ||||||
|     } |     } | ||||||
|  |  | ||||||
| @@ -181,8 +181,8 @@ public class UpdateContext { | |||||||
|         return sp.getBoolean("firstTime", false); |         return sp.getBoolean("firstTime", false); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     public boolean isRolledBack() { |     public String rolledBackVersion() { | ||||||
|         return sp.getBoolean("rolledBack", false); |         return sp.getString("rolledBackVersion", null); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     public void markSuccess() { |     public void markSuccess() { | ||||||
| @@ -209,7 +209,7 @@ public class UpdateContext { | |||||||
|  |  | ||||||
|     public void clearRollbackMark() { |     public void clearRollbackMark() { | ||||||
|         SharedPreferences.Editor editor = sp.edit(); |         SharedPreferences.Editor editor = sp.edit(); | ||||||
|         editor.putBoolean("rolledBack", false); |         editor.putString("rolledBackVersion", null); | ||||||
|         editor.apply(); |         editor.apply(); | ||||||
|  |  | ||||||
|         this.cleanUp(); |         this.cleanUp(); | ||||||
| @@ -265,6 +265,7 @@ public class UpdateContext { | |||||||
|  |  | ||||||
|     private String rollBack() { |     private String rollBack() { | ||||||
|         String lastVersion = sp.getString("lastVersion", null); |         String lastVersion = sp.getString("lastVersion", null); | ||||||
|  |         String currentVersion = sp.getString("currentVersion", null); | ||||||
|         SharedPreferences.Editor editor = sp.edit(); |         SharedPreferences.Editor editor = sp.edit(); | ||||||
|         if (lastVersion == null) { |         if (lastVersion == null) { | ||||||
|             editor.remove("currentVersion"); |             editor.remove("currentVersion"); | ||||||
| @@ -273,7 +274,7 @@ public class UpdateContext { | |||||||
|         } |         } | ||||||
|         editor.putBoolean("firstTimeOk", true); |         editor.putBoolean("firstTimeOk", true); | ||||||
|         editor.putBoolean("firstTime", false); |         editor.putBoolean("firstTime", false); | ||||||
|         editor.putBoolean("rolledBack", true); |         editor.putString("rolledBackVersion", currentVersion); | ||||||
|         editor.apply(); |         editor.apply(); | ||||||
|         return lastVersion; |         return lastVersion; | ||||||
|     } |     } | ||||||
|   | |||||||
| @@ -54,9 +54,9 @@ public class UpdateModule extends ReactContextBaseJavaModule { | |||||||
|         if (isFirstTime) { |         if (isFirstTime) { | ||||||
|             updateContext.clearFirstTime(); |             updateContext.clearFirstTime(); | ||||||
|         } |         } | ||||||
|         boolean isRolledBack = updateContext.isRolledBack(); |         String rolledBackVersion = updateContext.rolledBackVersion(); | ||||||
|         constants.put("isRolledBack", isRolledBack); |         constants.put("rolledBackVersion", rolledBackVersion); | ||||||
|         if (isRolledBack) { |         if (rolledBackVersion != null) { | ||||||
|             updateContext.clearRollbackMark(); |             updateContext.clearRollbackMark(); | ||||||
|         } |         } | ||||||
|         constants.put("blockUpdate", updateContext.getBlockUpdate()); |         constants.put("blockUpdate", updateContext.getBlockUpdate()); | ||||||
| @@ -134,9 +134,7 @@ public class UpdateModule extends ReactContextBaseJavaModule { | |||||||
|     private void downloadPatchFromPackage(ReadableMap options, final Promise promise) { |     private void downloadPatchFromPackage(ReadableMap options, final Promise promise) { | ||||||
|         String url = options.getString("updateUrl"); |         String url = options.getString("updateUrl"); | ||||||
|         String hash = options.getString("hash"); |         String hash = options.getString("hash"); | ||||||
|         if (hash == null) { |          | ||||||
|             hash = options.getString("hashName"); |  | ||||||
|         } |  | ||||||
|         updateContext.downloadPatchFromApk(url, hash, new UpdateContext.DownloadFileListener() { |         updateContext.downloadPatchFromApk(url, hash, new UpdateContext.DownloadFileListener() { | ||||||
|             @Override |             @Override | ||||||
|             public void onDownloadCompleted(DownloadTaskParams params) { |             public void onDownloadCompleted(DownloadTaskParams params) { | ||||||
| @@ -154,13 +152,9 @@ public class UpdateModule extends ReactContextBaseJavaModule { | |||||||
|     private void downloadPatchFromPpk(ReadableMap options, final Promise promise) { |     private void downloadPatchFromPpk(ReadableMap options, final Promise promise) { | ||||||
|         String url = options.getString("updateUrl"); |         String url = options.getString("updateUrl"); | ||||||
|         String hash = options.getString("hash"); |         String hash = options.getString("hash"); | ||||||
|         if (hash == null) { |          | ||||||
|             hash = options.getString("hashName"); |  | ||||||
|         } |  | ||||||
|         String originHash = options.getString("originHash"); |         String originHash = options.getString("originHash"); | ||||||
|         if (originHash == null) { |          | ||||||
|             originHash = options.getString(("originHashName")); |  | ||||||
|         } |  | ||||||
|         updateContext.downloadPatchFromPpk(url, hash, originHash, new UpdateContext.DownloadFileListener() { |         updateContext.downloadPatchFromPpk(url, hash, originHash, new UpdateContext.DownloadFileListener() { | ||||||
|             @Override |             @Override | ||||||
|             public void onDownloadCompleted(DownloadTaskParams params) { |             public void onDownloadCompleted(DownloadTaskParams params) { | ||||||
| @@ -176,8 +170,7 @@ public class UpdateModule extends ReactContextBaseJavaModule { | |||||||
|  |  | ||||||
|     @ReactMethod |     @ReactMethod | ||||||
|     public void reloadUpdate(ReadableMap options) { |     public void reloadUpdate(ReadableMap options) { | ||||||
|         final String hash = options.getString("hash") == null ? |         final String hash = options.getString("hash"); | ||||||
|                 options.getString("hashName") : options.getString("hash"); |  | ||||||
|  |  | ||||||
|         UiThreadUtil.runOnUiThread(new Runnable() { |         UiThreadUtil.runOnUiThread(new Runnable() { | ||||||
|             @Override |             @Override | ||||||
| @@ -218,8 +211,7 @@ public class UpdateModule extends ReactContextBaseJavaModule { | |||||||
|  |  | ||||||
|     @ReactMethod |     @ReactMethod | ||||||
|     public void setNeedUpdate(ReadableMap options) { |     public void setNeedUpdate(ReadableMap options) { | ||||||
|         final String hash = options.getString("hash") == null ? |         final String hash = options.getString("hash"); | ||||||
|                 options.getString("hashName") : options.getString("hash"); |  | ||||||
|  |  | ||||||
|         UiThreadUtil.runOnUiThread(new Runnable() { |         UiThreadUtil.runOnUiThread(new Runnable() { | ||||||
|             @Override |             @Override | ||||||
|   | |||||||
| @@ -120,6 +120,7 @@ RCT_EXPORT_MODULE(RCTPushy); | |||||||
|      |      | ||||||
|     NSDictionary *pushyInfo = [defaults dictionaryForKey:keyPushyInfo]; |     NSDictionary *pushyInfo = [defaults dictionaryForKey:keyPushyInfo]; | ||||||
|     NSString *lastVersion = pushyInfo[paramLastVersion]; |     NSString *lastVersion = pushyInfo[paramLastVersion]; | ||||||
|  |     NSString *curVersion = pushyInfo[paramCurrentVersion];  | ||||||
|     NSString *curPackageVersion = [RCTPushy packageVersion]; |     NSString *curPackageVersion = [RCTPushy packageVersion]; | ||||||
|     if (lastVersion.length) { |     if (lastVersion.length) { | ||||||
|         // roll back to last version |         // roll back to last version | ||||||
| @@ -133,7 +134,7 @@ RCT_EXPORT_MODULE(RCTPushy); | |||||||
|         // roll back to bundle |         // roll back to bundle | ||||||
|         [defaults setObject:nil forKey:keyPushyInfo]; |         [defaults setObject:nil forKey:keyPushyInfo]; | ||||||
|     } |     } | ||||||
|     [defaults setObject:@(YES) forKey:keyRolledBackMarked]; |     [defaults setObject:curVersion forKey:keyRolledBackMarked]; | ||||||
|     [defaults synchronize]; |     [defaults synchronize]; | ||||||
|     return lastVersion; |     return lastVersion; | ||||||
| } | } | ||||||
| @@ -151,7 +152,7 @@ RCT_EXPORT_MODULE(RCTPushy); | |||||||
|     ret[@"downloadRootDir"] = [RCTPushy downloadDir]; |     ret[@"downloadRootDir"] = [RCTPushy downloadDir]; | ||||||
|     ret[@"packageVersion"] = [RCTPushy packageVersion]; |     ret[@"packageVersion"] = [RCTPushy packageVersion]; | ||||||
|     ret[@"buildTime"] = [RCTPushy buildTime]; |     ret[@"buildTime"] = [RCTPushy buildTime]; | ||||||
|     ret[@"isRolledBack"] = [defaults objectForKey:keyRolledBackMarked]; |     ret[@"rolledBackVersion"] = [defaults objectForKey:keyRolledBackMarked]; | ||||||
|     ret[@"isFirstTime"] = [defaults objectForKey:keyFirstLoadMarked]; |     ret[@"isFirstTime"] = [defaults objectForKey:keyFirstLoadMarked]; | ||||||
|     ret[@"blockUpdate"] = [defaults objectForKey:keyBlockUpdate]; |     ret[@"blockUpdate"] = [defaults objectForKey:keyBlockUpdate]; | ||||||
|     ret[@"uuid"] = [defaults objectForKey:keyUuid]; |     ret[@"uuid"] = [defaults objectForKey:keyUuid]; | ||||||
| @@ -159,12 +160,12 @@ RCT_EXPORT_MODULE(RCTPushy); | |||||||
|     ret[@"currentVersion"] = [pushyInfo objectForKey:paramCurrentVersion]; |     ret[@"currentVersion"] = [pushyInfo objectForKey:paramCurrentVersion]; | ||||||
|      |      | ||||||
|     // clear isFirstTimemarked |     // clear isFirstTimemarked | ||||||
|     if ([[defaults objectForKey:keyFirstLoadMarked] boolValue]) { |     if (ret[@"isFirstTime"]) { | ||||||
|         [defaults setObject:nil forKey:keyFirstLoadMarked]; |         [defaults setObject:nil forKey:keyFirstLoadMarked]; | ||||||
|     } |     } | ||||||
|      |      | ||||||
|     // clear rolledbackmark |     // clear rolledbackmark | ||||||
|     if ([[defaults objectForKey:keyRolledBackMarked] boolValue]) { |     if (ret[@"rolledBackVersion"] != nil) { | ||||||
|         [defaults setObject:nil forKey:keyRolledBackMarked]; |         [defaults setObject:nil forKey:keyRolledBackMarked]; | ||||||
|         [self clearInvalidFiles]; |         [self clearInvalidFiles]; | ||||||
|     } |     } | ||||||
| @@ -268,9 +269,7 @@ RCT_EXPORT_METHOD(downloadPatchFromPpk:(NSDictionary *)options | |||||||
| RCT_EXPORT_METHOD(setNeedUpdate:(NSDictionary *)options) | RCT_EXPORT_METHOD(setNeedUpdate:(NSDictionary *)options) | ||||||
| { | { | ||||||
|     NSString *hash = options[@"hash"]; |     NSString *hash = options[@"hash"]; | ||||||
|     if (hash.length <= 0) { |      | ||||||
|         hash = options[@"hashName"]; |  | ||||||
|     } |  | ||||||
|     if (hash.length) { |     if (hash.length) { | ||||||
|         NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; |         NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; | ||||||
|         NSString *lastVersion = nil; |         NSString *lastVersion = nil; | ||||||
| @@ -294,9 +293,7 @@ RCT_EXPORT_METHOD(setNeedUpdate:(NSDictionary *)options) | |||||||
| RCT_EXPORT_METHOD(reloadUpdate:(NSDictionary *)options) | RCT_EXPORT_METHOD(reloadUpdate:(NSDictionary *)options) | ||||||
| { | { | ||||||
|     NSString *hash = options[@"hash"]; |     NSString *hash = options[@"hash"]; | ||||||
|     if (hash.length <= 0) { |  | ||||||
|         hash = options[@"hashName"]; |  | ||||||
|     } |  | ||||||
|     if (hash.length) { |     if (hash.length) { | ||||||
|         [self setNeedUpdate:options]; |         [self setNeedUpdate:options]; | ||||||
|          |          | ||||||
| @@ -359,9 +356,7 @@ RCT_EXPORT_METHOD(markSuccess) | |||||||
| { | { | ||||||
|     NSString *updateUrl = [RCTConvert NSString:options[@"updateUrl"]]; |     NSString *updateUrl = [RCTConvert NSString:options[@"updateUrl"]]; | ||||||
|     NSString *hash = [RCTConvert NSString:options[@"hash"]]; |     NSString *hash = [RCTConvert NSString:options[@"hash"]]; | ||||||
|     if (hash.length <= 0) { |  | ||||||
|         hash = [RCTConvert NSString:options[@"hashName"]];; |  | ||||||
|     } |  | ||||||
|     if (updateUrl.length <= 0 || hash.length <= 0) { |     if (updateUrl.length <= 0 || hash.length <= 0) { | ||||||
|         callback([self errorWithMessage:ERROR_OPTIONS]); |         callback([self errorWithMessage:ERROR_OPTIONS]); | ||||||
|         return; |         return; | ||||||
|   | |||||||
| @@ -76,6 +76,10 @@ export function getCheckUrl(APPKEY, endpoint = currentEndpoint) { | |||||||
|   return `${endpoint}/checkUpdate/${APPKEY}`; |   return `${endpoint}/checkUpdate/${APPKEY}`; | ||||||
| } | } | ||||||
|  |  | ||||||
|  | export function getReportUrl(endpoint = currentEndpoint) { | ||||||
|  |   return `${endpoint}/report`; | ||||||
|  | } | ||||||
|  |  | ||||||
| export function setCustomEndpoints({ main, backups, backupQueryUrl }) { | export function setCustomEndpoints({ main, backups, backupQueryUrl }) { | ||||||
|   currentEndpoint = main; |   currentEndpoint = main; | ||||||
|   backupEndpointsQueryUrl = null; |   backupEndpointsQueryUrl = null; | ||||||
|   | |||||||
							
								
								
									
										54
									
								
								lib/main.js
									
									
									
									
									
								
							
							
						
						
									
										54
									
								
								lib/main.js
									
									
									
									
									
								
							| @@ -2,6 +2,7 @@ import { | |||||||
|   tryBackupEndpoints, |   tryBackupEndpoints, | ||||||
|   getCheckUrl, |   getCheckUrl, | ||||||
|   setCustomEndpoints, |   setCustomEndpoints, | ||||||
|  |   getReportUrl, | ||||||
| } from './endpoint'; | } from './endpoint'; | ||||||
| import { | import { | ||||||
|   NativeEventEmitter, |   NativeEventEmitter, | ||||||
| @@ -25,7 +26,9 @@ export const downloadRootDir = Pushy.downloadRootDir; | |||||||
| export const packageVersion = Pushy.packageVersion; | export const packageVersion = Pushy.packageVersion; | ||||||
| export const currentVersion = Pushy.currentVersion; | export const currentVersion = Pushy.currentVersion; | ||||||
| export const isFirstTime = Pushy.isFirstTime; | export const isFirstTime = Pushy.isFirstTime; | ||||||
| export const isRolledBack = Pushy.isRolledBack; | const rolledBackVersion = Pushy.rolledBackVersion; | ||||||
|  | export const isRolledBack = typeof rolledBackVersion === 'string'; | ||||||
|  |  | ||||||
| export const buildTime = Pushy.buildTime; | export const buildTime = Pushy.buildTime; | ||||||
| let blockUpdate = Pushy.blockUpdate; | let blockUpdate = Pushy.blockUpdate; | ||||||
| let uuid = Pushy.uuid; | let uuid = Pushy.uuid; | ||||||
| @@ -45,7 +48,7 @@ async function getLocalHashInfo(hash) { | |||||||
| } | } | ||||||
|  |  | ||||||
| export async function getCurrentVersionInfo() { | export async function getCurrentVersionInfo() { | ||||||
|   return currentVersion ? await getLocalHashInfo(currentVersion) || {} : {}; |   return currentVersion ? (await getLocalHashInfo(currentVersion)) || {} : {}; | ||||||
| } | } | ||||||
|  |  | ||||||
| const eventEmitter = new NativeEventEmitter(Pushy); | const eventEmitter = new NativeEventEmitter(Pushy); | ||||||
| @@ -59,30 +62,29 @@ function logger(text) { | |||||||
|   console.log(`Pushy: ${text}`); |   console.log(`Pushy: ${text}`); | ||||||
| } | } | ||||||
|  |  | ||||||
|  | function report(hash, type) { | ||||||
|  |   logger(type); | ||||||
|  |   fetch(getReportUrl(), { | ||||||
|  |     method: 'POST', | ||||||
|  |     headers: { | ||||||
|  |       Accept: 'application/json', | ||||||
|  |       'Content-Type': 'application/json', | ||||||
|  |     }, | ||||||
|  |     body: JSON.stringify({ | ||||||
|  |       hash, | ||||||
|  |       type, | ||||||
|  |       cInfo, | ||||||
|  |       packageVersion, | ||||||
|  |       buildTime, | ||||||
|  |     }), | ||||||
|  |   }).catch((_e) => {}); | ||||||
|  | } | ||||||
|  |  | ||||||
| logger('uuid: ' + uuid); | logger('uuid: ' + uuid); | ||||||
|  |  | ||||||
| /* | if (isRolledBack) { | ||||||
| Return json: |   report(rolledBackVersion, 'rollback'); | ||||||
| Package expired: |  | ||||||
| { |  | ||||||
|   expired: true, |  | ||||||
|   downloadUrl: 'http://appstore/downloadUrl', |  | ||||||
| } | } | ||||||
| Package is up to date: |  | ||||||
| { |  | ||||||
|   upToDate: true, |  | ||||||
| } |  | ||||||
| There is available update: |  | ||||||
| { |  | ||||||
|   update: true, |  | ||||||
|   name: '1.0.3-rc', |  | ||||||
|   hash: 'hash', |  | ||||||
|   description: '添加聊天功能\n修复商城页面BUG', |  | ||||||
|   metaInfo: '{"silent":true}', |  | ||||||
|   pdiffUrl: 'http://update-packages.reactnative.cn/hash', |  | ||||||
|   diffUrl: 'http://update-packages.reactnative.cn/hash', |  | ||||||
| } |  | ||||||
|  */ |  | ||||||
|  |  | ||||||
| export const cInfo = { | export const cInfo = { | ||||||
|   pushy: require('../package.json').version, |   pushy: require('../package.json').version, | ||||||
| @@ -173,6 +175,10 @@ export async function downloadUpdate(options, eventListeners) { | |||||||
|   if (!options.update) { |   if (!options.update) { | ||||||
|     return; |     return; | ||||||
|   } |   } | ||||||
|  |   if (rolledbackVersion === options.hash) { | ||||||
|  |     logger(`rolledback hash ${rolledbackVersion}, ignored`); | ||||||
|  |     return; | ||||||
|  |   } | ||||||
|   if (downloadedHash === options.hash) { |   if (downloadedHash === options.hash) { | ||||||
|     logger(`duplicated downloaded hash ${downloadedHash}, ignored`); |     logger(`duplicated downloaded hash ${downloadedHash}, ignored`); | ||||||
|     return; |     return; | ||||||
| @@ -275,8 +281,8 @@ export function markSuccess() { | |||||||
|     return; |     return; | ||||||
|   } |   } | ||||||
|   marked = true; |   marked = true; | ||||||
|   logger('markSuccess'); |  | ||||||
|   Pushy.markSuccess(); |   Pushy.markSuccess(); | ||||||
|  |   report(currentVersion, 'success'); | ||||||
| } | } | ||||||
|  |  | ||||||
| export async function downloadAndInstallApk({ url, onDownloadProgress }) { | export async function downloadAndInstallApk({ url, onDownloadProgress }) { | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 sunnylqm
					sunnylqm