util.js
1 import SiteInfo from "../siteInfo.js"; 2 const App = getApp(); 3 const formatTime = date => { 4 const year = date.getFullYear() 5 const month = date.getMonth() + 1 6 const day = date.getDate() 7 const hour = date.getHours() 8 const minute = date.getMinutes() 9 const second = date.getSeconds() 10 return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':') 11 } 12 const formatMD_long = date => { 13 const year = date.getFullYear() 14 const month = date.getMonth() + 1 15 const day = date.getDate() 16 return [year, month, day].join('-') 17 } 18 const formatMD = date => { 19 const month = date.getMonth() + 1 20 const day = date.getDate() 21 return [month, day].join('.') 22 } 23 const formatHM = date => { 24 const hour = date.getHours() 25 const minute = date.getMinutes() 26 return [hour, minute].map(formatNumber).join(':') 27 } 28 29 const formatNumber = n => { 30 n = n.toString() 31 return n[1] ? n : '0' + n 32 } 33 const loadImageError = (e, vm) => { 34 let _errImg = e.target.dataset.errImg; 35 let _objImg = "'" + _errImg + "'"; 36 let _errObj = {}; 37 let _image = e.target.dataset.defaultImage; 38 _errObj[_errImg] = _image; 39 console.error(e.detail.errMsg + "----" + _errObj[_errImg] + "----" + _objImg); 40 vm.setData(_errObj); 41 } 42 const request = opt => { 43 //Location 44 let Location = wx.getStorageSync('location'); 45 delete Location.name; 46 delete Location.address; 47 //Token 48 const Token = wx.getStorageSync('access_token'); 49 //Url 50 const Url = SiteInfo["Siteroot"] + opt.url; 51 wx.request({ 52 url: Url, 53 data: opt.data ? opt.data : {}, 54 header: { 55 Version: SiteInfo["Version"], 56 Authorization: Token, 57 Location: JSON.stringify(Location) 58 }, 59 method: opt.method ? opt.method : "POST", 60 dataType: opt.dataType ? opt.dataType : "json", 61 responseType: opt.responseType ? opt.responseType : "text", 62 success: function (res) { 63 if (res.statusCode == 200) { 64 typeof opt.success == 'function' ? opt.success(res.data) : null;; 65 } else { 66 console.error(`请求发送成功,获取数据失败(${opt.url})statusCode ${res.data.status_code} message:${res.data.message}`); 67 if (!App.globalData.userInfoBool) {//!App.globalData.locationBool || 68 console.error(`用户未授权`); 69 wx.navigateTo({ 70 url: '/pages/openauth/index', //跳转到授权页面 71 }) 72 } 73 } 74 }, 75 fail: function (res) { 76 wx.hideLoading(); 77 typeof opt.fail == 'function' ? opt.fail(res) : console.error(`请求发送失败(${opt.url})`); 78 }, 79 complete: function (res) { 80 setTimeout(function () { 81 wx.hideLoading(); 82 }, 500) 83 typeof opt.complete == 'function' ? opt.complete(res) : null; 84 } 85 }) 86 } 87 let util = { 88 FormatMD_long: formatMD_long, //时间格式:2019-5-1 89 FormatMD: formatMD, //时间格式:5.1 90 FormatHM: formatHM, //时间格式:16:00 91 FormatTime: formatTime, // 时间格式 :2019-02-01 10:22:11 92 LoadImageError: loadImageError,/**图片加载错误 */ 93 Request: request/**请求封装 */ 94 } 95 export default util
封装了 request 、时间格式化、图片加载错误
作者:胡倩倩0903
本文版权归作者和博客园共有,欢迎转载,但必须给出原文链接,并保留此段声明,否则保留追究法律责任的权利。
posted on 2019-06-27 10:39 kitty20180903suzhou 阅读(325) 评论(0) 编辑 收藏 举报
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY