utilsTools.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /**
  2. * @Time 2019-12-12
  3. * @Author Zhengjingyi
  4. * @Action 全局公共方法
  5. */
  6. import requestUrl from '@/services/config.env.js'
  7. const _Tools = {
  8. getComStorage:function(key){// 获取本地Storage
  9. return new Promise(function(resolve,reject) {
  10. uni.getStorage({
  11. key: key,
  12. success: function (res){
  13. resolve(res.data)
  14. },
  15. fail: function(res){
  16. reject(false)
  17. }
  18. })
  19. })
  20. },
  21. setStorage:function(key,data){// 存储本地Storage
  22. return new Promise(function(resolve,reject) {
  23. uni.setStorage({
  24. key: key,
  25. data:data,
  26. success: function (res){
  27. }
  28. })
  29. })
  30. },
  31. getStorage:function(){// 获取本地userInfo
  32. return new Promise(function(resolve,reject) {
  33. uni.getStorage({
  34. key: 'userInfo',
  35. success: function (res){
  36. resolve(res.data)
  37. },
  38. fail: function(res){
  39. reject(false)
  40. }
  41. })
  42. })
  43. },
  44. getStorageAddressKey:function(){// 获取本地地址信息
  45. return new Promise(function(resolve,reject) {
  46. uni.getStorage({
  47. key: 'address_key',
  48. success: function (res){
  49. resolve(res.data)
  50. }
  51. })
  52. })
  53. },
  54. navigateTo:function(url){
  55. //路由跳转:页面之间路由跳转
  56. uni.navigateTo({
  57. url:url
  58. })
  59. },
  60. redirectTo:function(url){
  61. //路由跳转:关闭当前页跳转到新页面
  62. uni.redirectTo({
  63. url:url
  64. })
  65. },
  66. switchTabTo:function(url){
  67. //路由跳转:底部 tab页
  68. uni.switchTab({
  69. url:url
  70. })
  71. },
  72. getWindowHeight:function(){
  73. // 获取窗口高度
  74. const {windowHeight, pixelRatio} = wx.getSystemInfoSync()
  75. return windowHeight
  76. },
  77. adaptRichTextImg:function(res){
  78. /**
  79. *@富文本实现图片自适应
  80. *@style再添加自适应样式
  81. */
  82. const html = res.replace(/<img[^>]*>/gi,function(match,capture){
  83. let match1 = match.replace(/<img*/gi, '<img style="width:100% !important;height:auto !important;float:left !important;"'),
  84. results = match1.replace(/style=/gi, 'style="width:100%;height:auto;float:left;"')
  85. return results
  86. })
  87. return html
  88. },
  89. formatDate:function(){
  90. //获取当前时间
  91. let date = new Date();
  92. let y = date.getFullYear();
  93. let MM = date.getMonth() + 1;
  94. MM = MM < 10 ? ('0' + MM) : MM;
  95. let d = date.getDate();
  96. d = d < 10 ? ('0' + d) : d;
  97. let h = date.getHours();
  98. h = h < 10 ? ('0' + h) : h;
  99. let m = date.getMinutes();
  100. m = m < 10 ? ('0' + m) : m;
  101. let s = date.getSeconds();
  102. s = s < 10 ? ('0' + s) : s;
  103. return y + '-' + MM + '-' + d + ' ' + h + ':' + m + ':' + s;
  104. },
  105. }
  106. /**
  107. *@导出
  108. */
  109. module.exports = {
  110. formatDate: _Tools.formatDate,
  111. navigateTo: _Tools.navigateTo,
  112. redirectTo: _Tools.redirectTo,
  113. switchTabTo: _Tools.switchTabTo,
  114. setStorage: _Tools.setStorage,
  115. getStorage: _Tools.getStorage,
  116. getComStorage: _Tools.getComStorage,
  117. getWindowHeight: _Tools.getWindowHeight,
  118. adaptRichTextImg: _Tools.adaptRichTextImg,
  119. getStorageAddressKey: _Tools.getStorageAddressKey,
  120. }