function zero_fill_hex(num, digits) {
let s = num.toString(16);
while (s.length < digits)
s = "0" + s;
return s;
} /**
* rgba 转 16进制
* @param rgb
* @returns {*}
*/
function rgb2hex(rgb) {
if (!rgb) {
return rgb;
}
if (rgb.charAt(0) === '#')
return rgb;
const ds = rgb.split(/\D+/);
const decimal = Number(ds[1]) * 65536 + Number(ds[2]) * 256 + Number(ds[3]);
return "#" + zero_fill_hex(decimal, 6);
} /**
*
* @returns {string}
*/
function guid() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
const r = Math.random() * 16 | 0, v = c === 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
} /**
* 图标转换
* @param icon
* @returns {*} 图标原字符串
*/
function convertIcon(icon) {
if (!icon)
return icon;
return icon.slice(5);
} /**
* url
* @param url
* @param name
* @param value
* @returns {*}
*/
function urlUpdateParams(url, name, value) {
let r = url;
if (r != null && r !== 'undefined' && r !== "") {
value = encodeURIComponent(value);
const reg = new RegExp("(^|)" + name + "=([^&]*)(|$)");
const tmp = name + "=" + value;
if (url.match(reg) != null) {
r = url.replace(eval(reg), tmp);
} else {
if (url.match("[\?]")) {
r = url + "&" + tmp;
} else {
r = url + "?" + tmp;
}
}
}
return r;
} /**
* 格式化时间
* @param timestamp
* @returns {string}
*/
function timestampToTime(timestamp) {
const date = new Date(timestamp * 1000);//时间戳为10位需*1000,时间戳为13位的话不需乘1000
const Y = date.getFullYear() + '-';
const M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
const D = date.getDate() + ' ';
const h = date.getHours() + ':';
const m = date.getMinutes() + ':';
const s = date.getSeconds();
return Y + M + D + h + m + s;
} /**
* 跳转到新页面 并生产缓存key
* @param data
*/
function navigateToCacheData(data) {
const that = data.that;
let url = data.url;
const cacheKey = "_cacheKey_" + (data.type || "");
const key = "temp_cache_" + guid();
that.setState({
[cacheKey]: key
});
url = urlUpdateParams(url, "key", key);
Taro.navigateTo({
url: url
});
} /**
* 返回上一个页面并且传回数据
* @param that
* @param data
*/
function navigateBackCacheData(that, data) {
const key = that.$router.params.key;
if (key) {
Taro.setStorage({
key: key,
data: data
}).then(() => {
Taro.navigateBack({});
});
} else {
Taro.navigateBack({});
}
} /**
* 获取关闭的页面传回的数据
* @param data
* @param fn
*/
function getPageBackCacheData(data, fn) {
const that = data.that;
const cacheKey = "_cacheKey_" + (data.type || "");
const _cacheKey_ = that.state[cacheKey];
if (_cacheKey_) {
Taro.getStorage({
key: _cacheKey_
}).then((res) => {
fn && fn(res.data);
}).catch(() => {
});
Taro.removeStorage({
key: _cacheKey_
})
}
} // 弹窗
function showModal(data, fn) {
Taro.showModal({
title: data.title,
content: data.content || "",
confirmText: data.confirmText || "确认",
cancelText: data.cancelText || "取消",
confirmColor: "#2297e5",
success: function (t) {
if (!t.confirm) {
return
}
fn && fn();
}
});
} // 装换带小数的钱
function convertMoney(couponVal) {
if (parseInt(couponVal) === parseFloat(couponVal)) {
return parseFloat(couponVal);
}
return parseFloat(couponVal);
} // 打开新页面
function navigateTo(data, parName, json) {
if (!data) {
console.error(data);
return;
}
if (parName && json) {
// 添加参数
json = JSON.stringify(json);
json = encodeURIComponent(json);
data.url = urlUpdateParams(data.url, parName, json);
}
Taro.navigateTo(data).catch((res) => {
if (res.errMsg.indexOf("limit") !== -1) {
// 超出限制关闭所有重新打开
reLaunch(data)
} else {
Taro.showToast({
title: "打开失败",
icon: "none"
});
}
})
} // 延迟加载
function reLaunch(data) {
Taro.reLaunch(data).catch(() => {
// 跳转失败
Taro.showLoading({
title: '加载中',
});
setTimeout(function () {
Taro.hideLoading();
Taro.reLaunch(data)
}, 2000);
}).then(() => {
Taro.hideLoading();
});
}

微信小程序常用的方法(留着用)的更多相关文章

  1. 微信小程序数据请求方法wx.request小测试

    微信小程序数据请求方法 wx.request wxml文件: <view> <textarea value="{{textdata}}"/> </vi ...

  2. 微信小程序开发系列教程三:微信小程序的调试方法

    微信小程序开发系列教程 微信小程序开发系列一:微信小程序的申请和开发环境的搭建 微信小程序开发系列二:微信小程序的视图设计 这个教程的前两篇文章,介绍了如何用下图所示的微信开发者工具自动生成一个Hel ...

  3. 微信小程序常用样式汇总

    本文系转载: 原文作者:chenzheng8975 原文地址:https://www.cnblogs.com/chenzheng8975/p/9605186.html 微信小程序常用样式汇总 小程序特 ...

  4. 微信小程序最新授权方法,getUserInfo

    20180511微信小程序正式关闭原先getUserInfo的逻辑 不再允许自动弹出授权框. 方法一: index.wxml(准备一个用于给用户授权的页面,我这里直接用了一个全屏按钮) <vie ...

  5. 微信小程序——wxParse使用方法

    wxParse是一个微信小程序富文本解析组件.现在小程序里面自带了一个<rich-text>组件也能解析富文本,但是表现不尽人意.所以我还是采用的wxParse来解析富文本的. wxPar ...

  6. 微信小程序节点查询方法:wx.createSelectorQuery()的使用场景与注意事项

    小程序由于内置于微信,这使得它有了得天独厚的宣传和使用优势,本着学习的心态,我在官网上看了一遍开发文档,大致得出小程序框架的设计模式与使用注意事项(重点来了,其实开发文档某些方面叙述的并不仔细,甚至存 ...

  7. 微信小程序特殊字符转义方法——&转义&amp;等等

    在我编写公司小程序的过程中,有一次在网页端添加了一张图片,结果在小程序端访问失败了,究其原因,竟然是因为该图片名称中有一个“&”符号,网页端添加后,自动转义成了“&”存储到了数据库.当 ...

  8. 微信小程序--数据共享与方法共享

    目录 全局数据共享 Mobox npm安装及其注意事项 小程序对 npm 的支持与限制 npm 依赖包的安装与使用 Mobox 1. 全局数据共享 2. 小程序中的全局数据共享方案 3. 使用mobx ...

  9. 微信小程序~生命周期方法详解

    生命周期是指一个小程序从创建到销毁的一系列过程 在小程序中 ,通过App()来注册一个小程序 ,通过Page()来注册一个页面 先来看一张小程序项目结构 从上图可以看出,根目录下面有包含了app.js ...

随机推荐

  1. 网络工程师和Linux运维工程师有什么区别?学哪个比较好?

    网络工程师和Linux运维工程师有什么区别?学哪个比较好? 机缘巧合下,我进入了一家从事vpn与系统集成的公司,很感谢公司能留下我这个非网络工程专业的毕业生,从对网络一窍不通,慢慢可以自己独立完成工作 ...

  2. c++作业22题

    一.单选题(共22题,100.0分) 1 已知int i=5,下列do-while循环语句的循环次数是 do{ cout<<i - -<<endl; i - -; }while ...

  3. async,await怎么用

    async声明一个函数是异步的,await用于等待异步完成,并且await只能在async中使用. 使用async,await并行处理请求,速度减半: 将多个promise直接发起请求,先执行asyn ...

  4. SyntaxError: unexpected character after line continuation character

    SyntaxError: unexpected character after line continuation character 待解决问题:在运行.py文件时报错SyntaxError: un ...

  5. 033.Kubernetes集群安全-API Server认证及授权

    一 Kubernetes集群安全 1.1 安全机制 Kubernetes通过一系列机制来实现集群的安全控制,其中包括API Server的认证授权.准入控制机制及保护敏感信息的Secret机制等.集群 ...

  6. 1.JVM中的五大内存区域划分详解及快速扫盲

    本博客参考<深入理解Java虚拟机>这本书 视频及电子书详见:https://shimo.im/docs/HP6qqHx38xCJwcv9/ 一.快速扫盲 1. JVM是什么   JVM是 ...

  7. 关于WPF System.windows.Media.FontFamily 的类型初始值设定项引发异常问题解决方法

    造成原因:此问题的根本原因是.NET Framework January 2018 Rollup(KB4055002)与已安装的.NET Framework 4.7.1产品版本之间的MSI安装交互.R ...

  8. foobox,基于foobar2000汉化版的CUI配置整合版

    名 称:foobox 作 者:dreamawake 发布博客:https://www.cnblogs.com/foobox/ GitHub: https://github.com/dream7180/ ...

  9. How to do error checking in CUDA(如何在CUDA里做错误检查)

    https://codeyarns.com/2011/03/02/how-to-do-error-checking-in-cuda/ Error checks in CUDA code can hel ...

  10. 聊聊 WebSocket,还有 HTTP

    还记得曾经风靡一时的 QQ 秀聊天室吗?那时,还在上初.高中的我们,QQ 是最常用的聊天交友工具:而 QQ 秀聊天室的出现打破了只能按条件查找好友的局限性,大家可以随意进入聊天室房间,进行在线聊天.怀 ...