123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- /**
- * 显示消息提示框
- * @param content 提示的标题
- */
- export function toast(content) {
- uni.showToast({
- icon: 'none',
- title: content
- })
- }
- /**
- * 显示模态弹窗
- * @param content 提示的标题
- */
- export function showConfirm(content) {
- return new Promise((resolve, reject) => {
- uni.showModal({
- title: '提示',
- content: content,
- cancelText: '取消',
- confirmText: '确定',
- success: function(res) {
- resolve(res)
- }
- })
- })
- }
- /**
- * 参数处理
- * @param params 参数
- */
- export function tansParams(params) {
- let result = ''
- for (const propName of Object.keys(params)) {
- const value = params[propName]
- var part = encodeURIComponent(propName) + "="
- if (value !== null && value !== "" && typeof(value) !== "undefined") {
- if (typeof value === 'object') {
- for (const key of Object.keys(value)) {
- if (value[key] !== null && value[key] !== "" && typeof(value[key]) !== 'undefined') {
- let params = propName + '[' + key + ']'
- var subPart = encodeURIComponent(params) + "="
- result += subPart + encodeURIComponent(value[key]) + "&"
- }
- }
- } else {
- result += part + encodeURIComponent(value) + "&"
- }
- }
- }
- return result
- }
- /**
- * 随机数
- * @param {Object} min
- * @param {Object} max
- */
- export function getRandomNum(min, max) {
- var range = max - min;
- var rand = Math.random()
- return (min + Math.round(range * rand))
- }
- export function getSameData(arr1, arr2, key) {
- const idMapping = arr1.reduce((acc, el, i) => {
- acc[el[key]] = i;
- return acc;
- }, {});
- arr2.forEach(i => {
- if (idMapping[i[key]]) { //重复值
- i.checked = true
- }
- })
- }
- /**
- * @param {Function} func
- * @param {number} wait
- * @param {boolean} immediate
- * @return {*}
- */
- export function debounce(func, wait, immediate) {
- let timeout, args, context, timestamp, result
- const later = function() {
- // 据上一次触发时间间隔
- const last = +new Date() - timestamp
- // 上次被包装函数被调用时间间隔 last 小于设定时间间隔 wait
- if (last < wait && last > 0) {
- timeout = setTimeout(later, wait - last)
- } else {
- timeout = null
- // 如果设定为immediate===true,因为开始边界已经调用过了此处无需调用
- if (!immediate) {
- result = func.apply(context, args)
- if (!timeout) context = args = null
- }
- }
- }
- return function(...args) {
- context = this
- timestamp = +new Date()
- const callNow = immediate && !timeout
- // 如果延时不存在,重新设定延时
- if (!timeout) timeout = setTimeout(later, wait)
- if (callNow) {
- result = func.apply(context, args)
- context = args = null
- }
- return result
- }
- }
- //搜索框关键词存储
- export function saveKeyWord(type, val) {
- let arr = []
- if (uni.getStorageSync(type)) {
- let arr = JSON.parse(uni.getStorageSync(type))
- if (arr.indexOf(val) != -1) {
- console.log('arr.indexOf(val)', arr.indexOf(val))
- arr.splice(arr.indexOf(val), 1)
- }
- arr.unshift(val)
- if (arr.length > 6) {
- arr.pop()
- }
- uni.setStorageSync(type, JSON.stringify(arr))
- return arr
- } else {
- arr.unshift(val)
- uni.setStorageSync(type, JSON.stringify(arr))
- return arr
- }
- }
- //价格保留两位小数点正则
- export function testPrice(val) {
- let reg = /^\d+(\.([0-9]|\d[0-9]))?$/
- return reg.test(val)
- }
|