import wafer from '../lib/wafer2-client-sdk'
import tip from './tip'
// 时间格式化
const formatTime = time => {
time = (time.length == 10) ? parseInt(time) * 1000 : parseInt(time)
var date = new Date(time)
const year = date.getFullYear()
const month = date.getMonth() + 1
const day = date.getDate()
const hour = date.getHours()
const minute = date.getMinutes()
const second = date.getSeconds()
return [year, month, day].map(formatNumber).join('-')
}
// Money格式化
const formatFen2Jiao = fen => {
if (fen) {
return parseFloat(fen / 100, 2)
} else {
return '0.00'
}
}
const formatNumber = n => {
n = n.toString()
return n[1] ? n : '0' + n
}
const deepClone = obj => {
const _this = this
var temp = null
if (obj && obj instanceof Array) {
temp = []
temp = obj.map(function (item) {
return deepClone(item)
})
} else if (obj && typeof obj === 'object') { // typeof null 的值是 object
temp = {}
for (let item in obj) {
temp[item] = deepClone(obj[item])
}
} else {
temp = obj
}
return temp
}
// 复制粘贴
const clip = (obj) => {
wx.setClipboardData(obj)
}
async function request(options, page, tryout, successCB, failCB) {
const session = wafer.Session.get()
if (tryout === 0) {
return
}
let tryTmp = 0
let result = 0
let failTmp = 0
while (tryTmp++ < tryout && !result) {
if (tryTmp > 2) await sleep(3)
failTmp = await tryRequest(options)
if (failTmp.data && (failTmp.data.code || failTmp.data.code == 0)) {
result = failTmp
}
}
if (result) {
successCB.call(page, result)
} else {
failCB.call(page, failTmp)
}
if (options.tip) {
tip.loaded()
}
}
async function tryRequest(options) {
return new Promise((resolve, reject) => {
if (options.tip) {
tip.loading()
}
wafer.request({
method: options.method || 'GET',
data: options.data || {},
url: options.url,
success: function (res) {
resolve(res)
},
fail: function (err) {
console.log('try err:', err)
if (!err.code) {
resolve(0)
} else {
resolve(err)
console.log('请求失败url', options.url)
console.log('请求失败', err)
}
},
complete: function (res) {
// console.log(res)
}
})
})
}
async function sleep(sec) {
return new Promise(resolve => {
setTimeout(() => {
let tmp = ''
console.log('sleep ' + sec + ' seconds')
resolve(true)
}, sec * 1000)
})
}
function getuuid(u) {
let mydate = new Date()
let uuid = u + mydate.getDay() + mydate.getHours() + mydate.getMinutes() +
mydate.getSeconds() + mydate.getMilliseconds() + Math.round(Math.random() * 10000)
return uuid
}
module.exports = {
formatTime,
deepClone,
formatFen2Jiao,
clip,
request,
getuuid
}

小程序util工具的更多相关文章

  1. 微信小程序 - Util工具类

    /utils/utils.js   已经扩展到App对象中,Page方法中直接使用  app.util.method(...) 调用. 1. 扩展String.replaceAll JS默认值提供re ...

  2. 微信小程序开发工具测评

    1月9日微信小程序正式上线.很多企业都希望能在这个.但是在技术开发的问题上,却不知道该如何下手.经过一些程序员不辞辛苦连夜测试,终于从十余款工具呕心沥血筛选出四款比较靠谱实用的微信小程序开发工具.接下 ...

  3. HotApp小程序统计,第一个专业的微信第三方小程序统计工具

    1.什么是HotApp小程序统计 HotApp小程序统计是第一个微信第三方小程序统计工具,就像做android 和 ios开发的人知道友盟统计一样,小程序也需要有个统计工具. 通过这个工具,可以知道小 ...

  4. 微信小程序开发工具的数据,配置,日志等目录在哪儿? 怎么找?

    原文地址:http://www.wxapp-union.com/portal.php?mod=view&aid=359 本文由本站halfyawn原创:感谢原创者:如有疑问,请在评论内回复   ...

  5. 微信小程序开发工具使用与设计规范(二)

    [未经作者本人同意,请勿以任何形式转载] 上一篇文章主要分析了微信小程序应用场景和优劣势.本篇你可以学习到: 如何使用小程序开发工具写一个Hello World 微信小程序设计规范 微信小程序项目结构 ...

  6. mac版微信web开发者工具(小程序开发工具)无法显示二维码 解决方案

    微信小程序概念的提出,绝对可以算得上中国IT界惊天动地的一件大事,这可能意味着一场新的开发热潮即将到来, 我也怀着激动的心情准备全身心投入其中,不过截止目前,在官方网站上下载的最新版本都无法使用,打开 ...

  7. 微信小程序开发工具中快捷键

    微信小程序开发工具表面上是没有更多的样式类的工具,例如缩进.隐藏代码什么的. 现在总结一下小程序开发工具常用的一些快捷键: 格式调整 Ctrl+S:保存文件Ctrl+[, Ctrl+]:代码行缩进Ct ...

  8. ubuntu系统安装微信小程序开发工具

    在ubuntu系统中安装微信小程序开发工具之前,先要安装wine与git 一.安装wine 1.如果您的系统是64位,启用32位架构(如果您还没有) sudo dpkg --add-architect ...

  9. 微信小程序开发工具

    微信小程序 1● 工具下载 https://mp.weixin.qq.com/debug/wxadoc/dev/devtools/download.html     2● webchart_devto ...

随机推荐

  1. django 消息框架 message

    在网页应用中,我们经常需要在处理完表单或其它类型的用户输入后,显示一个通知信息给用户. 对于这个需求,Django提供了基于Cookie或者会话的消息框架messages,无论是匿名用户还是认证的用户 ...

  2. cocos2dx spine之二 :spine变色

    cocos2dx版本为3.10 1.具体原理和代码可以参考博文<利用shader改变图片色相Hue>,下面的代码根据该博文进行整理优化. 基本原理就是将RGB值转换为HSL值后加上输入的H ...

  3. vue-循环标记列表元素

    <el-col :lg="4" class="list" v-for="(item,index) in picList"> &l ...

  4. 利用unittest+ddt进行接口测试(二):使用yaml文件管理测试数据

    知道ddt的基本使用方法之后,练习把之前用excel文件来维护的接口测试用例改用unittest+ddt来实现. 这里我选用yaml文件来管理接口参数,开始本来想用json,但是json无法添加注释, ...

  5. layui 3种导航栏

    <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html ...

  6. Program Option Modifiers

    Some option are 'boolean' and control behavior that can be turned on or off. --column-names option d ...

  7. 协方差分析 | ANCOVA (Analysis of Covariance)

    If you are worried about leaving out covariates you could regress out them first and analyse the res ...

  8. 决策论 | 信息论 | decision theory | information theory

    参考: 模式识别与机器学习(一):概率论.决策论.信息论 Decision Theory - Principles and Approaches 英文图书 What are the best begi ...

  9. idea ----> 使用idea工具整合mybaiti时出现的问题总结

    使用idea测试mabtis实例时出现  java.lang.IllegalArgumentException: Mapped Statements collection does not conta ...

  10. 串的模式匹配算法 ------ KMP算法

    //KMP串的模式匹配算法 #include <stdio.h> #include <stdlib.h> #include <string.h> int* get_ ...