You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
chatai/dist/dev/mp-weixin/utils/util.js

123 lines
3.4 KiB

"use strict";
const common_vendor = require("../common/vendor.js");
const stores_navigationBarTitle = require("../stores/navigationBarTitle.js");
const getRect = (selector, all = false, context) => {
return new Promise((resolve, reject) => {
let qurey = common_vendor.index.createSelectorQuery();
if (context) {
qurey = common_vendor.index.createSelectorQuery().in(context);
}
qurey[all ? "selectAll" : "select"](selector).boundingClientRect(function(rect) {
if (all && Array.isArray(rect) && rect.length) {
return resolve(rect);
}
if (!all && rect) {
return resolve(rect);
}
reject("找不到元素");
}).exec();
});
};
function navigateTo(link, takeName = false, navigateType = "navigateTo") {
const url = link.query ? `${link.path}?${objectToQuery(link.query)}` : link.path;
const navigationBarTitleStore = stores_navigationBarTitle.useNavigationBarTitleStore();
navigationBarTitleStore.add({
path: link.path,
title: link.name
});
navigateType == "navigateTo" && common_vendor.index.navigateTo({
url
});
navigateType == "reLaunch" && common_vendor.index.reLaunch({
url
});
navigateType == "switchTab" && common_vendor.index.switchTab({
url
});
}
const isEmpty = (value) => {
return value == null || typeof value == "undefined";
};
function objectToQuery(params) {
let query = "";
for (const props of Object.keys(params)) {
const value = params[props];
if (!isEmpty(value)) {
query += props + "=" + value + "&";
}
}
return query.slice(0, -1);
}
const addUnit = (value, unit = "rpx") => {
return !Object.is(Number(value), NaN) ? `${value}${unit}` : value;
};
function formatPrice({ price, take = "all", prec = void 0 }) {
let [integer, decimals = ""] = (price + "").split(".");
if (prec !== void 0) {
const LEN = decimals.length;
for (let i = prec - LEN; i > 0; --i)
decimals += "0";
decimals = decimals.substr(0, prec);
}
switch (take) {
case "int":
return integer;
case "dec":
return decimals;
case "all":
return integer + "." + decimals;
}
}
function series(...task) {
return function(_arg) {
return new Promise((resolve, reject) => {
const iteratorTask = task.values();
const next = (res) => {
const nextTask = iteratorTask.next();
if (nextTask.done) {
resolve(res);
} else {
Promise.resolve(nextTask.value(res)).then(next).catch(reject);
}
};
next(_arg);
});
};
}
function strToParams(str) {
if (str === "" || str === "?")
return {};
const newparams = {};
for (const item of str.split("&")) {
newparams[item.split("=")[0]] = item.split("=")[1];
}
return newparams;
}
function paramsToStr(params) {
let p = "";
if (common_vendor.isObject(params)) {
p = "?";
for (const props in params) {
p += `${props}=${params[props]}&`;
}
p = p.slice(0, -1);
}
return p;
}
const splitDomain = (domain) => {
if (domain.includes("https://")) {
return domain.replace(/^https?:\/\//, "");
}
if (domain.includes("http://")) {
return domain.replace(/^http?:\/\//, "");
}
return domain;
};
exports.addUnit = addUnit;
exports.formatPrice = formatPrice;
exports.getRect = getRect;
exports.navigateTo = navigateTo;
exports.paramsToStr = paramsToStr;
exports.series = series;
exports.splitDomain = splitDomain;
exports.strToParams = strToParams;