Commit 151be183 authored by jyx's avatar jyx

代码优化

parent 2b43b53b
const {
printError
} = require("./printUtil");
/**
* 解析请求路径参数对象
* @param {string} url 请求路径
*/
function getObjectFromUrl(url) {
let tempObj = {};
if (/\?/.test(url)) {
let paramStr = url.substring(url.indexOf("?") + 1);
let paramArray = paramStr.split("&");
paramArray.forEach(item => {
let resultArray = item.split("=");
tempObj[resultArray[0]] = resultArray[1];
});
}
return tempObj;
}
/**
* 获取路径 参数字串
* @param {object} params 参数对象
*/
function appendParam(params, appendMark = true) {
if (typeof params == 'object') {
let keys = Object.keys(params);
if (keys && keys.length > 0) {
let list = keys.map(item => {
return `${item}=${params[item]}`;
})
return `${appendMark?'?':''}${list.join("&")}`
}
return "";
const {
printError
} = require("./printUtil");
/**
* 解析请求路径参数对象
* @param {string} url 请求路径
*/
function getObjectFromUrl(url) {
let tempObj = {};
if (/\?/.test(url)) {
let paramStr = url.substring(url.indexOf("?") + 1);
let paramArray = paramStr.split("&");
paramArray.forEach(item => {
let resultArray = item.split("=");
tempObj[resultArray[0]] = resultArray[1];
});
}
return tempObj;
}
/**
* 获取路径 参数字串
* @param {object} params 参数对象
*/
function appendParam(params, appendMark = true) {
if (typeof params == 'object') {
let keys = Object.keys(params);
if (keys && keys.length > 0) {
let list = keys.map(item => {
return `${item}=${params[item]}`;
})
return `${appendMark?'?':''}${list.join("&")}`
}
return "";
} else {
throw new Error("Params 类型错误")
}
}
/** 过滤空值属性 */
function emptyPrototypeFilter(obj) {
let result = {};
Object.keys(obj).forEach(item => {
if (obj[item] != null && obj[item] != undefined) {
result[item] = obj[item]
}
})
return result;
}
/**
* 拨打电话
* @param {string} phoneNumber 电话号码
*/
function callPhoneNumber(phoneNumber) {
if (!phoneNumber) return;
uni.makePhoneCall({
phoneNumber,
}).catch((error) => {
printError(error.errMsg);
})
}
/** 检查用户授权 */
function checkAuthSetting(settingName, callback) {
uni.getSetting({
success: (getSettingRes) => {
if (getSettingRes.authSetting[`scope.${settingName}`] == undefined) { // 未接受或拒绝过此权限
uni.authorize({
scope: `scope.${settingName}`,
success: (authorizeRes) => { // 同意授权
if (typeof callback == 'function') callback(true);
},
fail: (authorizeError) => {
if (typeof callback == 'function') callback(false,
`未授权: ${settingName}`)
}
})
} else if (getSettingRes.authSetting[`scope.${settingName}`] == false) { // 拒绝权限
openSettingView(settingName, callback);
} else { // 已授权
if (typeof callback == 'function') callback(true);
}
}
})
}
/** 打开授权页面 */
function openSettingView(settingName, callback) {
uni.openSetting({
success: (res) => {
if (res.authSetting[`scope.${settingName}`]) { // 同意授权
if (typeof callback == 'function') callback(true)
} else {
if (typeof callback == 'function') callback(false, `未授权:${settingName}`)
}
},
fail: (error) => {
if (typeof callback == 'function') callback(false, 'openSetting 失败')
}
})
}
/**
* 电话隐私处理
*/
function privatePhone(phone, start = 3, end = 7, replace = '****') {
if (!phone || phone.length <= end) return phone;
return phone.replace(phone.substring(start, end), replace)
}
/**
* 姓名隐私处理
* @param {Object} name
*/
function privateName(name) {
if (!name) return name;
name = name.replace(" ", "");
let result = "";
name = name.split("");
name.forEach((item) => {
if (!result) {
result = item;
} else {
result = `${result}*`
}
})
return result;
}
/**
* px rpx 转换
*/
const pxrpxChangeMap = {};
function rpx2px(rpx) {
let key = `rpx${rpx}`;
if (pxrpxChangeMap[key]) return pxrpxChangeMap[key];
let result = 0;
let deviceWidth = uni.getSystemInfoSync().windowWidth; //获取设备屏幕宽度
let px = (deviceWidth / 750) * Number(rpx)
result = Math.floor(px);
return result;
}
function px2rpx(px) {
let key = `px{px}`;
if (pxrpxChangeMap[key]) return pxrpxChangeMap[key];
let result = 0;
let deviceWidth = uni.getSystemInfoSync().windowWidth; //获取设备屏幕宽度
let rpx = (750 / deviceWidth) * Number(px)
result = Math.floor(rpx);
return result;
}
/**
* 是否是array
* @param {Object} list
* @returns true == array | false != array
*/
function isArray(list) {
return Object.prototype.toString.call(list) == '[object Array]';
}
/**
* 是否是空
* @param {Object} data 要检查的数据
* @param {Boolean} onlyNull 是否只检查是否为 null 或者 undefined
* @returns true == empty | false != empty
*/
function isEmpty(data, onlyNull = false) {
if (onlyNull) {
return data === null || data === undefined;
} else {
throw new Error("Params 类型错误")
}
}
/** 过滤空值属性 */
function emptyPrototypeFilter(obj) {
let result = {};
Object.keys(obj).forEach(item => {
if (obj[item] != null && obj[item] != undefined) {
result[item] = obj[item]
}
})
return result;
}
/**
* 拨打电话
* @param {string} phoneNumber 电话号码
*/
function callPhoneNumber(phoneNumber) {
if (!phoneNumber) return;
uni.makePhoneCall({
phoneNumber,
}).catch((error) => {
printError(error.errMsg);
})
}
/** 检查用户授权 */
function checkAuthSetting(settingName, callback) {
uni.getSetting({
success: (getSettingRes) => {
if (getSettingRes.authSetting[`scope.${settingName}`] == undefined) { // 未接受或拒绝过此权限
uni.authorize({
scope: `scope.${settingName}`,
success: (authorizeRes) => { // 同意授权
if (typeof callback == 'function') callback(true);
},
fail: (authorizeError) => {
if (typeof callback == 'function') callback(false,
`未授权: ${settingName}`)
}
})
} else if (getSettingRes.authSetting[`scope.${settingName}`] == false) { // 拒绝权限
openSettingView(settingName, callback);
} else { // 已授权
if (typeof callback == 'function') callback(true);
}
}
})
}
/** 打开授权页面 */
function openSettingView(settingName, callback) {
uni.openSetting({
success: (res) => {
if (res.authSetting[`scope.${settingName}`]) { // 同意授权
if (typeof callback == 'function') callback(true)
} else {
if (typeof callback == 'function') callback(false, `未授权:${settingName}`)
}
},
fail: (error) => {
if (typeof callback == 'function') callback(false, 'openSetting 失败')
}
})
}
/**
* 电话隐私处理
*/
function privatePhone(phone, start = 3, end = 7, replace = '****') {
if (!phone || phone.length <= end) return phone;
return phone.replace(phone.substring(start, end), replace)
}
/**
* 姓名隐私处理
* @param {Object} name
*/
function privateName(name) {
if (!name) return name;
name = name.replace(" ", "");
let result = "";
name = name.split("");
name.forEach((item) => {
if (!result) {
result = item;
} else {
result = `${result}*`
}
})
return result;
}
/**
* px rpx 转换
*/
const pxrpxChangeMap = {};
function rpx2px(rpx) {
let key = `rpx${rpx}`;
if (pxrpxChangeMap[key]) return pxrpxChangeMap[key];
let result = 0;
let deviceWidth = uni.getSystemInfoSync().windowWidth; //获取设备屏幕宽度
let px = (deviceWidth / 750) * Number(rpx)
result = Math.floor(px);
return result;
}
function px2rpx(px) {
let key = `px{px}`;
if (pxrpxChangeMap[key]) return pxrpxChangeMap[key];
let result = 0;
let deviceWidth = uni.getSystemInfoSync().windowWidth; //获取设备屏幕宽度
let rpx = (750 / deviceWidth) * Number(px)
result = Math.floor(rpx);
return result;
}
/**
* 是否是array
* @param {Object} list
* @returns true == array | false != array
*/
function isArray(list) {
return Object.prototype.toString.call(list) == '[object Array]';
}
/**
* 是否是空
* @param {Object} data 要检查的数据
* @param {Boolean} onlyNull 是否只检查是否为 null 或者 undefined
* @returns true == empty | false != empty
*/
function isEmpty(data, onlyNull = false) {
if (onlyNull) {
return data === null || data === undefined;
} else {
if (typeof data == 'object') {
if (data) {
let result = JSON.stringify(data);
return result == "{}" || result == "[]"
} else {
return true;
}
} else {
return data ? false : true;
}
}
}
/**
* 是否不为空
*/
function isNotEmpty(data, onlyNull = false) {
return !isEmpty(data, onlyNull);
}
/**
* 获取随机数
* @param {Object} min
* @param {Object} max
* @param {Boolean} include
*/
function getRandomInt(min = 0, max = 1, include = true) {
min = Math.ceil(min);
max = Math.floor(max);
return include ? Math.floor(Math.random() * (max - min + 1)) + min : Math.floor(Math.random() * (max - min)) + min;
}
/**
* 版本比对
* @param {Object} versionA
* @param {Object} versionB
* @returns 1 versionA 大于 versionB || -1 versionA 小于 versionB || 0 两个version相等
*/
function compareVersion(versionA, versionB) {
const versionArr_A = versionA.split(".");
const versionArr_B = versionB.split(".");
const maxLength = Math.max(versionArr_A.length, versionArr_B.length);
for (let i = 0; i < maxLength; i++) {
const a = versionArr_A[i] || 0;
const b = versionArr_B[i] || 0;
if (Number(a) > Number(b)) {
return 1;
} else if (Number(a) < Number(b)) {
return -1;
}
if (i === maxLength - 1) {
return 0;
}
}
}
/**
* 遍历枚举
*/
function ergodicEnum(enumObj, value, compareKey = 'value') {
let result = null;
if (value == null || value == undefined) return result;
if (typeof enumObj == 'object') {
if (enumObj instanceof Array) {
enumObj.map((item, index) => {
return {
...item,
index
}
}).forEach((item) => {
if (value == item[compareKey]) {
result = item;
}
})
} else {
Object.keys(enumObj).forEach((keyItem, keyIndex) => {
let item = {
...enumObj[keyItem],
index: keyIndex
};
if (value == item[compareKey]) {
result = item;
}
})
}
}
return result;
}
/** 通过 Value 获取 Name */
function getEnumValueName(enumObj, value) {
let result = ergodicEnum(enumObj, value);
if (result) {
return result.name;
} else {
return "";
}
}
/** 通过 Value 获取 Index */
function getEnumValueIndex(enumObj, value) {
let result = ergodicEnum(enumObj, value);
if (result) {
return result.index;
} else {
return -1;
}
}
/** 通过 Name 获取 Value */
function getEnumNameValue(enumObj, name) {
let result = ergodicEnum(enumObj, name, 'name');
if (result) {
return result.value;
} else {
return '';
}
}
/** 通过 Name 获取 Index */
function getEnumNameIndex(enumObj, name) {
let result = ergodicEnum(enumObj, name, 'name');
if (result) {
return result.index;
} else {
return -1;
}
}
/** 通过 Index 获取 Value */
function getEnumIndexValue(enumObj, index) {
let result = ergodicEnum(enumObj, index, 'index');
if (result) {
return result.value
} else {
return ""
}
}
/** 通过 Index 获取 Name */
function getEnumIndexName(enumObj, index) {
let result = ergodicEnum(enumObj, index, 'index');
if (result) {
return result.name
} else {
return ""
}
}
//生成从minNum到maxNum的随机数
function randomNum(minNum, maxNum) {
switch (arguments.length) {
case 1:
return parseInt(Math.random() * minNum + 1, 10);
case 2:
return parseInt(Math.random() * (maxNum - minNum + 1) + minNum, 10);
default:
return 0;
}
}
module.exports = {
getObjectFromUrl,
appendParam,
emptyPrototypeFilter,
callPhoneNumber,
checkAuthSetting,
privatePhone,
privateName,
rpx2px,
px2rpx,
isArray,
isEmpty,
isNotEmpty,
getRandomInt,
compareVersion,
ergodicEnum,
getEnumValueName,
getEnumValueIndex,
getEnumNameValue,
getEnumNameIndex,
getEnumIndexValue,
getEnumIndexName,
randomNum
if (typeof data == 'object') {
if (data) {
let result = JSON.stringify(data);
return result == "{}" || result == "[]"
} else {
return true;
}
} else {
return data ? false : true;
}
}
}
/**
* 是否不为空
*/
function isNotEmpty(data, onlyNull = false) {
return !isEmpty(data, onlyNull);
}
/**
* 获取随机数
* @param {Object} min
* @param {Object} max
* @param {Boolean} include
*/
function getRandomInt(min = 0, max = 1, include = true) {
min = Math.ceil(min);
max = Math.floor(max);
return include ? Math.floor(Math.random() * (max - min + 1)) + min : Math.floor(Math.random() * (max - min)) + min;
}
/**
* 版本比对
* @param {Object} versionA
* @param {Object} versionB
* @returns 1 versionA 大于 versionB || -1 versionA 小于 versionB || 0 两个version相等
*/
function compareVersion(versionA, versionB) {
const versionArr_A = versionA.split(".");
const versionArr_B = versionB.split(".");
const maxLength = Math.max(versionArr_A.length, versionArr_B.length);
for (let i = 0; i < maxLength; i++) {
const a = versionArr_A[i] || 0;
const b = versionArr_B[i] || 0;
if (Number(a) > Number(b)) {
return 1;
} else if (Number(a) < Number(b)) {
return -1;
}
if (i === maxLength - 1) {
return 0;
}
}
}
/**
* 遍历枚举
*/
function ergodicEnum(enumObj, value, compareKey = 'value') {
let result = null;
if (value == null || value == undefined) return result;
if (typeof enumObj == 'object') {
if (enumObj instanceof Array) {
enumObj.map((item, index) => {
return {
...item,
index
}
}).forEach((item) => {
if (value == item[compareKey]) {
result = item;
}
})
} else {
Object.keys(enumObj).forEach((keyItem, keyIndex) => {
let item = {
...enumObj[keyItem],
index: keyIndex
};
if (value == item[compareKey]) {
result = item;
}
})
}
}
return result;
}
/** 通过 Value 获取 Name */
function getEnumValueName(enumObj, value) {
let result = ergodicEnum(enumObj, value);
if (result) {
return result.name;
} else {
return "";
}
}
/** 通过 Value 获取 Index */
function getEnumValueIndex(enumObj, value) {
let result = ergodicEnum(enumObj, value);
if (result) {
return result.index;
} else {
return -1;
}
}
/** 通过 Name 获取 Value */
function getEnumNameValue(enumObj, name) {
let result = ergodicEnum(enumObj, name, 'name');
if (result) {
return result.value;
} else {
return '';
}
}
/** 通过 Name 获取 Index */
function getEnumNameIndex(enumObj, name) {
let result = ergodicEnum(enumObj, name, 'name');
if (result) {
return result.index;
} else {
return -1;
}
}
/** 通过 Index 获取 Value */
function getEnumIndexValue(enumObj, index) {
let result = ergodicEnum(enumObj, index, 'index');
if (result) {
return result.value
} else {
return ""
}
}
/** 通过 Index 获取 Name */
function getEnumIndexName(enumObj, index) {
let result = ergodicEnum(enumObj, index, 'index');
if (result) {
return result.name
} else {
return ""
}
}
//生成从minNum到maxNum的随机数
function randomNum(minNum, maxNum) {
switch (arguments.length) {
case 1:
return parseInt(Math.random() * minNum + 1, 10);
case 2:
return parseInt(Math.random() * (maxNum - minNum + 1) + minNum, 10);
default:
return 0;
}
}
module.exports = {
getObjectFromUrl,
appendParam,
emptyPrototypeFilter,
callPhoneNumber,
checkAuthSetting,
privatePhone,
privateName,
rpx2px,
px2rpx,
isArray,
isEmpty,
isNotEmpty,
getRandomInt,
compareVersion,
ergodicEnum,
getEnumValueName,
getEnumValueIndex,
getEnumNameValue,
getEnumNameIndex,
getEnumIndexValue,
getEnumIndexName,
randomNum
}
\ No newline at end of file
......@@ -126,7 +126,17 @@
},
computed: {
showEmpty: function() {
return isEmpty(this.dataList);
console.log(this.dataList)
if (isEmpty(this.dataList)) {
return true
} else {
if (typeof this.dataList == 'object') {
if (this.dataList.list != undefined) {
return isEmpty(this.dataList.list)
}
}
}
return false
},
scrollStyle: function() {
return {
......@@ -290,8 +300,8 @@
...param,
...this.param
}
}
}
apiRequest({
url: this.url,
method: this.method || "GET",
......
......@@ -19,10 +19,11 @@
};
},
methods: {
goWareHouse() {
uni.switchTab({
url: '/pages/warehouse/warehouse'
});
goWareHouse() {
uni.$emit('goWareHouse')
// uni.switchTab({
// url: '/pages/warehouse/warehouse'
// });
}
},
}
......
......@@ -86,6 +86,11 @@
mounted() {
// 渲染完成 初始化首页数据
this.loadComponentData();
let that = this
uni.$on('goWareHouse', function(data) {
that.tabChange(1)
})
},
methods: {
tabChange(index) {
......@@ -93,14 +98,14 @@
this.currentPage = index;
this.loadComponentData();
},
loadComponentData() {
loadComponentData() {
if (this.currentPage == 0) {
this.$refs.index.show();
this.$refs.index.show();
} else if (this.currentPage == 1) {
this.$refs.recommend.show();
} else if (this.currentPage == 2) {
this.$refs.user.show();
}
}
}
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment