diff --git a/common/http.js b/common/http.js index c75ad23..69800b2 100644 --- a/common/http.js +++ b/common/http.js @@ -24,7 +24,7 @@ function getsign(params) { return params; } -function $http(url, data = {}){ +function $http(url, data = {}, options = {}){ return new Promise((resolve, reject) => { // 绑定this let that = this; @@ -74,6 +74,12 @@ function $http(url, data = {}){ // 生成sign getsign(data); console.log(url, data); + + // 动态赋值是否显示loading加载框 TODO验证一下是否有问题 + if(that.$shared.isValueType(options.showLoading) != 'undefined'){ + that.$u.http.setConfig({showLoading: options.showLoading}); + } + // 发起请求 that.$u.post(url, data).then(res => { resolve(res); diff --git a/common/shared.js b/common/shared.js new file mode 100644 index 0000000..c7a8b9c --- /dev/null +++ b/common/shared.js @@ -0,0 +1,43 @@ +/* + 全局共享实用方法 shared.js + */ + +// 设置角标, 必须传入下标值,设置值可传可不传,传参时显示,不传时移除角标 +function setBadge(index = 0, value){ + if(isRight(value)){ + uni.setTabBarBadge({ + index: Number(index), + text: value > 99 ? '99+' : String(value) + }) + }else{ + uni.removeTabBarBadge({index: Number(index)}); + } +} + +// 判断对错/是否显示,万能校验 +function isRight(obj) { + if (isValueType(obj) === 'string') { + obj = obj.trim(); + if (obj === 'null' || obj === 'undefined') { + return false; + } + } else if (isValueType(obj) === 'number' && (isValueType(obj) === "number" && !isNaN(obj)) && obj !== 0) { + return true; + } + for (var key in obj) { + return true; + } + return false; +} + +// 判断一个值所属的类型,返回一个字符串 +function isValueType(value) { + let str = Object.prototype.toString.call(value); + return str.match(/\[object (.*?)\]/)[1].toLowerCase(); +} + +module.exports = { + setBadge, + isRight, + isValueType +} \ No newline at end of file diff --git a/main.js b/main.js index 1a69262..a972626 100644 --- a/main.js +++ b/main.js @@ -2,6 +2,7 @@ import Vue from 'vue' import App from './App' import mixin from '@/common/mixin.js'; import * as API from '@/common/api.js'; +import $shared from '@/common/shared.js'; Vue.config.productionTip = false @@ -10,6 +11,8 @@ Vue.mixin(mixin); // 将API注入全局 Vue.prototype.API = API; +// 全局共享方法 +Vue.prototype.$shared = $shared; // 引入全局uView import uView from 'uview-ui'