utilsTools.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /**
  2. * @Time 2019-12-12
  3. * @Author Zhengjingyi
  4. * @Action 全局公共方法
  5. */
  6. import requestUrl from '@/services/config.env.js'
  7. export function getComStorage(key) { // 获取本地Storage
  8. return new Promise(function(resolve, reject) {
  9. uni.getStorage({
  10. key: key,
  11. success: function(res) {
  12. resolve(res.data)
  13. },
  14. fail: function(res) {
  15. reject(false)
  16. }
  17. })
  18. })
  19. }
  20. export function setStorage(key, data) { // 存储本地Storage
  21. return new Promise(function(resolve, reject) {
  22. uni.setStorage({
  23. key: key,
  24. data: data,
  25. success: function(res) {}
  26. })
  27. })
  28. }
  29. export function getStorage() { // 获取本地userInfo
  30. return new Promise(function(resolve, reject) {
  31. uni.getStorage({
  32. key: 'userInfo',
  33. success: function(res) {
  34. resolve(res.data)
  35. },
  36. fail: function(res) {
  37. reject(false)
  38. }
  39. })
  40. })
  41. }
  42. export function getStorageAddressKey() { // 获取本地地址信息
  43. return new Promise(function(resolve, reject) {
  44. uni.getStorage({
  45. key: 'address_key',
  46. success: function(res) {
  47. resolve(res.data)
  48. }
  49. })
  50. })
  51. }
  52. export function navigateTo(url) {
  53. //路由跳转:页面之间路由跳转
  54. uni.navigateTo({
  55. url: url
  56. })
  57. }
  58. export function redirectTo(url) {
  59. //路由跳转:关闭当前页跳转到新页面
  60. uni.redirectTo({
  61. url: url
  62. })
  63. }
  64. export function switchTabTo(url) {
  65. //路由跳转:底部 tab页
  66. uni.switchTab({
  67. url: url
  68. })
  69. }
  70. export function getWindowHeight() {
  71. // 获取窗口高度
  72. const { windowHeight, pixelRatio } = wx.getSystemInfoSync()
  73. return windowHeight
  74. }
  75. export function adaptRichTextImg(res) {
  76. /**
  77. *@富文本实现图片自适应
  78. *@style再添加自适应样式
  79. */
  80. const html = res.replace(/<img[^>]*>/gi, function(match, capture) {
  81. let match1 = match.replace(/<img*/gi,
  82. '<img style="width:100% !important;height:auto !important;float:left !important;"'),
  83. results = match1.replace(/style=/gi, 'style="width:100%;height:auto;float:left;"')
  84. return results
  85. })
  86. return html
  87. }
  88. export function formatDate() {
  89. //获取当前时间
  90. let date = new Date()
  91. let y = date.getFullYear()
  92. let MM = date.getMonth() + 1
  93. MM = MM < 10 ? ('0' + MM) : MM
  94. let d = date.getDate()
  95. d = d < 10 ? ('0' + d) : d
  96. let h = date.getHours()
  97. h = h < 10 ? ('0' + h) : h
  98. let m = date.getMinutes()
  99. m = m < 10 ? ('0' + m) : m
  100. let s = date.getSeconds()
  101. s = s < 10 ? ('0' + s) : s
  102. return y + '-' + MM + '-' + d + ' ' + h + ':' + m + ':' + s
  103. }
  104. export function isNumber(value) { //验证是否为数字
  105. var patrn = /^(-)?\d+(\.\d+)?$/
  106. if (patrn.exec(value) == null || value == '') {
  107. return false
  108. } else {
  109. return true
  110. }
  111. }
  112. export function FormatMoney(num) {
  113. // 金额千分位
  114. return num.toString().replace(/\d+/, function(n) { // 先提取整数部分
  115. return n.replace(/(\d)(?=(\d{3})+$)/g, function($1) { // 对整数部分添加分隔符
  116. return $1 + ','
  117. })
  118. })
  119. }
  120. const install = Vue => {
  121. console.log('初始化挂载API工具方法 utilsTools.js')
  122. Vue.prototype.$api = {
  123. FormatMoney,
  124. formatDate,
  125. navigateTo,
  126. redirectTo,
  127. switchTabTo,
  128. isNumber,
  129. setStorage,
  130. getStorage,
  131. getComStorage,
  132. getWindowHeight,
  133. adaptRichTextImg,
  134. getStorageAddressKey,
  135. }
  136. }
  137. export default install