微信小程序> 微信小程序常用公共方法

微信小程序常用公共方法

浏览量:645 时间: 来源:景宇
//获取当前时间的时间戳const getNowTime = function () {    return new Date().getTime();}// 设置同步缓存const set = function (name, value) {    return wx.setStorageSync(name, value);}//获取缓存const get = function (name) {    return wx.getStorageSync(name);}//移除缓存const remove = function (name) {    return wx.removeStorageSync(name);}//时间戳转日期const timestampToTime = function (timestamp) {    let Timestamp;    //时间戳为10位需*1000,时间戳为13位的话不需乘1000    if (timestamp.length == 10) {        Timestamp = timestamp * 1000;    } else {        Timestamp = timestamp;    }    let date = new Date(Timestamp);    let Y = date.getFullYear();    let M = date.getMonth() + 1  10 ? "0" + (date.getMonth() + 1) : (date.getMonth() + 1);    let D = date.getDate()  10 ? "0" + date.getDate() : date.getDate();    let h = date.getHours()  10 ? "0" + date.getHours() : date.getHours();    let m = date.getMinutes()  10 ? "0" + date.getMinutes() : date.getMinutes();    let s = date.getSeconds()  10 ? "0" + date.getSeconds() : date.getSeconds();    return Y + "-" + M + "-" + D + " " + h + ":" + m + ":" + s;}//验证手机号码const regMobile = function (mobile) {    let myreg = /^0?(13[0-9]|14[5-9]|15[012356789]|166|17[0-8]|18[0-9]|19[8-9])[0-9]{8}$/;    return myreg.test(mobile);}//验证身份证号码 数字和结尾字母xconst regIdcard = function (idcard) {    let myreg = /(^d{17}(d|X|x)$)/;    return myreg.test(idcard);}//验证邮箱地址const regEmail = function (email) {    let myreg = /^[a-zA-Z0-9]+([._-]*[a-zA-Z0-9]*)*@[a-zA-Z0-9]+.[a-zA-Z0-9{2,5}$]/;    return myreg.test(email);}//字符串转数组 (character:分隔符号const stringToArray = function (string, character) {    return string.split(character);}//删除数组元素const deletArray = function (array, index) {    let temp = [];    array.forEach((element, idx) = {        if (idx != index) {            temp.push(element);        }    });    return temp;}//截取字符串const formatString = function (string, num) {    if (string) {        if (string.length  num) {            return string.slice(0, num) + "...";        } else {            return string;        }    } else {        return " ";    }}//http地址转httpsconst https = function (url) {    var http = url.indexOf("http:");    if (http  -1) {        var newurl = url.replace("http", "https");        return newurl;    } else {        return url;    }}//自定义错误提示信息const alert = (msg, time) = {    if (time) {        wx.showToast({            title: msg,            duration: time,            mask: true,            icon: "none"        })    } else {        wx.showToast({            title: msg,            duration: 2000,            mask: true,            icon: "none"        })    }}// 隐私数字 身份证号码,手机号码const encryption = function (string, minIdx, maxIndex) {    return string.substr(0, minIdx) + '****' + string.substr(maxIndex);}//处理数字加减乘除const formateNumber = function (number_1, number_2, character) {    if (character == "+") {        return (number_1 * 10e10 + number_2 * 10e10) / 10e10;    } else if (character == "-") {        return (number_1 * 10e10 - number_2 * 10e10) / 10e10;    } else if (character == "*") {        return ((number_1 * 10e10) * (number_2 * 10e10)) / (10e10 * 10e10);    } else if (character == "/") {        return (number_1 * 10e10) / (number_2 * 10e10);    }};// 数组去重const reArray = (array) = {    return Array.from(new Set(array));}export default {    getNowTime: getNowTime,    set: set,    get: get,    remove: remove,    timestampToTime: timestampToTime,    regMobile: regMobile,    regIdcard: regIdcard,    stringToArray: stringToArray,    formatString: formatString,    https: https,    alert: alert,    regEmail: regEmail,    deletArray: deletArray,    encryption: encryption,    formateNumber: formateNumber,    reArray: reArray}

 

版权声明

即速应用倡导尊重与保护知识产权。如发现本站文章存在版权问题,烦请提供版权疑问、身份证明、版权证明、联系方式等发邮件至197452366@qq.com ,我们将及时处理。本站文章仅作分享交流用途,作者观点不等同于即速应用观点。用户与作者的任何交易与本站无关,请知悉。

产品经理

手机 : 13312967497

擅长 : 小程序流量变现

扫码领取礼包

最新资讯

热门模板

  • 头条
  • 搜狐
  • 微博
  • 百家
  • 一点资讯
  • 知乎