H5在線預覽php
Githubhtml
Gitee前端
uniapp-admin是基於uni-app開發的管理系統模板。該模板集成了以下的功能:nginx
第三方插件git
解決方案(重要)github
若是有欠缺的地方,或者有更好的想法,你們能夠多多交流,文章最後能夠加我。正則表達式
跨域的解決方法之一就是採用 代理
"h5" : { "devServer" : { "port" : 9090, "disableHostCheck" : true, "proxy" : { "/ua/ua-service" : { "target" : "http://localhost:8080", "changeOrigin" : true, "secure" : false, "pathRewrite" : { "^/ua" : "" } }, "/ua-service" : { "target" : "http://localhost:8080", "changeOrigin" : true, "secure" : false } } }, "title" : "uniapp-admin", "router" : { "mode" : "hash", "base" : "/ua" } }
原理:h5端配置api前綴,而後proxy代理(在manifest.json的proxy);
APP端不存在跨域問題,直接配置全域名或ip地址便可
/** * ip地址或域名 */ const ipAddress = 'http://localhost:8080' // 文件訪問地址 const fileAddr = 'http://localhost:8082/fileUpload/' /** * api前綴 */ const apiPrefix = '/ua-service' /** * 針對不一樣平臺的baseUrl */ const getBaseUrl = () => { // #ifdef H5 return apiPrefix // #endif // #ifndef H5 return ipAddress + apiPrefix // #endif } export default { /** * 針對不一樣平臺的baseUrl */ baseUrl: getBaseUrl(), fileAddr }
// 設置默認配置 minRequest.setConfig((config) => { config.baseURL = globalConfig.baseUrl return config })
生產環境的跨域,典型的方案就是採用Nginx代理
配置以下:json
# 前端訪問代理 location /ua { alias /root/ua; index index.html index.htm; try_files $uri $uri/ /index.html?/$request_uri; } # 服務端代理,對應開發環境proxy location ~/ua/ua-service/* { rewrite ^/ua/(.*)$ /$1 break; proxy_pass http://service; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_connect_timeout 360000s; proxy_read_timeout 360000s; proxy_send_timeout 360000s; } # 服務端代理,對應開發環境proxy location ~/ua-service/* { proxy_pass http://service; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_connect_timeout 360000s; proxy_read_timeout 360000s; proxy_send_timeout 360000s; }
其中proxy_pass http://service的service是配置在nginx的http{}下的後端服務後端
upstream service { server 127.0.0.1:8080 max_fails=1 fail_timeout=10s; }
rewrite ^/ua/(.*)$ \/\$1 break;是路徑替換,第一個參數是正則表達式,$1是正則表達式中第一個括號內容。
參考文檔,uni-app 整包升級/更新方案 ,另外也有,uni-app 資源在線升級/熱更新
本項目是採用整包升級更新方案,以下:
/** * app整包更新檢測 */ appUpgrade() { //#ifndef APP-PLUS uni.showToast({ title: '目前只支持Android版本軟件更新', icon: 'none' }) //#endif //#ifdef APP-PLUS uni.getSystemInfo({ success: sysInfo => { let platform = sysInfo.platform plus.runtime.getProperty(plus.runtime.appid, (wgtinfo) => { // app版本字符串如:1.1.0 // this.appInfo.version = wgtinfo.version // app名稱 // this.appInfo.name = wgtinfo.name let params = { appid: plus.runtime.appid, // app整數版本號,如110,必定要用versionCode作判斷 version: wgtinfo.versionCode, platform: platform } this.$minApi.findUpgradeApp(params).then(appRes => { if (appRes.appid) { uni.showModal({ title: "下載更新提示", content: appRes.note, showCancel: false, confirmText: '肯定', success: sucRes => { if (sucRes.confirm) { plus.runtime.openURL(appRes.url) // uni.downloadFile({ // url: appRes.url, // success: res => {} // }) } } }) } else { uni.showToast({ title: '已是最新版本了。', icon: 'none' }) } }) }) } }) //#endif }
<span style="color:red">注意:必定要使用plus.runtime.getProperty(plus.runtime.appid, (wgtinfo) =>()),回調函數返回的wgtinfo對象的versionCode作判斷,wgtinfo.version是版本字符串,wgtinfo.name是app應用名稱</span>
核心是調用uni.request,參考代碼
request(options = {}) { options.baseURL = options.baseURL || this[config].baseURL options.dataType = options.dataType || this[config].dataType options.url = MinRequest[isCompleteURL](options.url) ? options.url : (options.baseURL + options.url) options.data = options.data options.header = { ...options.header, ...this[config].header } options.method = options.method || this[config].method options = { ...options, ...MinRequest[requestBefore](options) } return new Promise((resolve, reject) => { options.success = function(res) { resolve(MinRequest[requestAfter](res)) } options.fail = function(err) { reject(MinRequest[requestAfter](err)) } uni.request(options) }) }
必定要使用plus.runtime.getProperty(plus.runtime.appid, (wgtinfo) =>()),回調函數返回的wgtinfo對象的versionCode作判斷,wgtinfo.version是版本字符串,wgtinfo.name是app應用名稱
解決辦法:提早將數據格式化好,而後套上模板。
你們也能夠提出想要的模板或功能
贊助做者