uniapp 檢測版本更新函數封裝(未完)

檢測版本更新

import $http from "./request.js";
// app更新
const Update = function(showToast = false){
	// #ifdef APP-PLUS  
	plus.runtime.getProperty(plus.runtime.appid, function(widgetInfo) {  
		/*
		
		{
			"msg": "ok",
			"data": {
				"id": 1,
				"url": "http://www.baidu.com",
				"version": "1.0.1",
				"status": 1,
				"create_time": null
			}
		}
		
		*/
		$http.post('/update',{
			ver:widgetInfo.version, 
		}).then((res) => {
			let [err,result] = res;
			// 錯誤處理
			if (!$http.errorCheck(err,result)) return;
			// 成功
			var data = result.data.data;  
			if (!data.url){
				// 無需更新
				if (showToast) {
					return uni.showToast({ title: '無需更新',icon:"none" })
				}
			}
			
			uni.showModal({
				title: '發現新的版本',
				content: '最新版本:'+data.version,
				cancelText: '放棄更新',
				confirmText: '立即更新',
				success: res => {
					if(res.confirm){
						uni.downloadFile({  
							url: data.url,  
							success: (downloadResult) => {  
								if (downloadResult.statusCode === 200) {  
									plus.runtime.install(downloadResult.tempFilePath, {  
										force: false  
									}, function() {  
										console.log('install success...');  
										plus.runtime.restart();  
									}, function(e) {  
										console.error('install fail...');  
									});  
								}  
							}  
						});  
					}
				}
			});
			
		});
		
	});  
	// #endif  
}

import config from "./config.js"
export default {
	Update
}

引入的兩個文件如下:

config.js

// 配置信息
export default {
	// api請求前綴
	webUrl:'https://api.xxxx.com/api/v1',
	// websocket地址
	websocketUrl:"wss://api.xxxx.com/wss"
}

request.js

// 引入配置文件
import config from "./config.js";
import User from "./user.js";
export default{
	config:{
		baseUrl:config.webUrl,
		header:{
			'Content-Type':'application/json;charset=UTF-8',
			'Content-Type':'application/x-www-form-urlencoded'
		},
		data: {},
		method: "GET",
		dataType: "json",
	},
	request(options = {}){
		options.header = options.header || this.config.header;
		options.method = options.method || this.config.method;
		options.dataType = options.dataType || this.config.dataType;
		options.url = this.config.baseUrl+options.url;
		// TODO:token增加等操作
		if (options.token) {
			// 驗證用戶是否登錄
			if (!this.checkToken(options.checkToken)) return;
			// 驗證用戶操作權限(驗證是否綁定手機號碼)
			if (!this.checkAuth(options.checkAuth)) return;
			options.header.token = User.token;
		}
		return uni.request(options);
	},
	get(url,data,options={}){
		options.url = url;
		options.data = data;
		options.method = 'GET';
		return this.request(options);
	},
	post(url,data,options={}){
		options.url = url;
		options.data = data;
		options.method = 'POST';
		return this.request(options);
	},
	// 上傳圖片
	upload(url,options = {}){
		options.url = this.config.baseUrl+url;
		options.header = options.header || this.config.header;
		options.fileType = options.fileType || "image";
		options.formData = options.formData || {};
		options.filePath = options.filePath;
		options.name = options.name;
		// TODO:token增加等操作
		if (options.token) {
			// 驗證是否登錄
			if (!this.checkToken(options.checkToken)) return;
			// 驗證權限
			if (!this.checkAuth(options.checkAuth)) return; 
			options.header.token = User.token;
		}
		
		return uni.uploadFile(options);
	},
	// 錯誤處理
	errorCheck(err,res,errfun = false,resfun = false){
		if (err) {
			typeof errfun === 'function' && errfun();
			uni.showToast({ title: '加載失敗',icon:"none" });
			return false;
		}
		if (res.data.errorCode) {
			typeof errfun === 'function' && resfun();
			uni.showToast({ title: res.data.msg,icon:"none" });
			return false;
		}
		return true;
	},
	// 驗證用戶是否登錄
	checkToken(checkToken){
		if (checkToken && !User.token) {
			uni.showToast({ title: '請先登錄', icon:"none" })
			uni.navigateTo({
				url: '/pages/login/login'
			});
			return false;
		}
		return true;
	},
	// 驗證用戶權限
	checkAuth(checkAuth){
		if (checkAuth && !User.userinfo.phone) {
			uni.showToast({ title: '請先綁定手機號碼', icon:"none" })
			uni.navigateTo({
				url: '/pages/user-bind-phone/user-bind-phone'
			});
			return false;
		}
		return true;
	}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章