util.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. import dayjs from 'dayjs'
  2. import Navi from './navi'
  3. /**
  4. * 手机号码*加密函数
  5. * @param {string} phone 电话号
  6. * @returns
  7. */
  8. export const phoneEncryption = (phone: string) => {
  9. return phone.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2')
  10. }
  11. // 内置手机号正则字符串
  12. export const innerPhoneReg = '^1(?:3\\d|4[4-9]|5[0-35-9]|6[67]|7[0-8]|8\\d|9\\d)\\d{8}$'
  13. export const formatTime = (date: Date) => {
  14. const year = date.getFullYear()
  15. const month = date.getMonth() + 1
  16. const day = date.getDate()
  17. const hour = date.getHours()
  18. const minute = date.getMinutes()
  19. const second = date.getSeconds()
  20. return (
  21. [year, month, day].map(formatNumber).join('/') +
  22. ' ' +
  23. [hour, minute, second].map(formatNumber).join(':')
  24. )
  25. }
  26. export const formatNumber = (n: number) => {
  27. const s = n.toString()
  28. return s[1] ? s : '0' + s
  29. }
  30. export const delay = (millionseconds: number) => {
  31. return new Promise<void>((resolve) => setTimeout(() => resolve(), millionseconds))
  32. }
  33. export const obj2Params = (obj: Record<string, string>, encode = false) => {
  34. const result: string[] = []
  35. if (obj) {
  36. Object.keys(obj).forEach((key) =>
  37. result.push(`${key}=${encode ? encodeURIComponent(obj[key]) : obj[key]}`),
  38. )
  39. }
  40. return result.join('&')
  41. }
  42. export const clearUser = () => {
  43. wx.removeStorageSync('__uvx')
  44. }
  45. export const getUser = () => {
  46. const info = wx.getStorageSync('__uvx')
  47. if (info && info.length > 0) {
  48. const user = JSON.parse(info) as OpenUserInfo
  49. const now = dayjs()
  50. const exp = dayjs(user.expiration)
  51. if (now.isAfter(exp)) {
  52. clearUser()
  53. } else {
  54. return {
  55. avatarUrl:
  56. user.avatar && user.avatar.length > 0 ? user.avatar : '/assets/navbar/avatar.png',
  57. nickName: user.name,
  58. phoneNumber: user.phone,
  59. }
  60. }
  61. }
  62. return null
  63. }
  64. export const updateAvatar = (obj: string) => {
  65. const info = wx.getStorageSync('__uvx')
  66. if (info && info.length > 0) {
  67. const user = JSON.parse(info) as OpenUserInfo
  68. const now = dayjs()
  69. const exp = dayjs(user.expiration)
  70. if (now.isAfter(exp)) {
  71. clearUser()
  72. } else {
  73. user.avatar = obj
  74. wx.setStorageSync('__uvx', JSON.stringify(user))
  75. }
  76. }
  77. return null
  78. }
  79. export const updateName = (obj: string) => {
  80. const info = wx.getStorageSync('__uvx')
  81. if (info && info.length > 0) {
  82. const user = JSON.parse(info) as OpenUserInfo
  83. const now = dayjs()
  84. const exp = dayjs(user.expiration)
  85. if (now.isAfter(exp)) {
  86. clearUser()
  87. } else {
  88. user.name = obj
  89. wx.setStorageSync('__uvx', JSON.stringify(user))
  90. }
  91. }
  92. return null
  93. }
  94. export const getSign = () => {
  95. const info = wx.getStorageSync('__uvx')
  96. if (info && info.length > 0) {
  97. const user = JSON.parse(info) as OpenUserInfo
  98. const now = dayjs()
  99. const exp = dayjs(user.expiration)
  100. if (now.isAfter(exp)) {
  101. clearUser()
  102. } else {
  103. return user.signature
  104. }
  105. }
  106. return null
  107. }
  108. export const isLogin = () => {
  109. const info = wx.getStorageSync('__uvx')
  110. if (info && info.length > 0) {
  111. const user = JSON.parse(info) as OpenUserInfo
  112. const now = dayjs()
  113. const exp = dayjs(user.expiration)
  114. if (now.isAfter(exp)) {
  115. clearUser()
  116. } else {
  117. return true
  118. }
  119. }
  120. return false
  121. }
  122. export const rpxToPx = (rpx: string) => {
  123. return (Number(rpx.toString().replace(/rpx$/gi, '')) * wx.getSystemInfoSync().windowWidth) / 750
  124. }
  125. export const getCurrentUrl = () => {
  126. const pages = getCurrentPages() //获取加载的页面
  127. const currentPage = pages[pages.length - 1] //获取当前页面的对象
  128. let query = Object.keys(currentPage.options)
  129. .map((o) => {
  130. const obj = currentPage.options[o]
  131. return `${o}=${obj}`
  132. })
  133. .join('&')
  134. query = query && query.length > 0 ? '?' + query : ''
  135. const url = currentPage.route + query //当前页面url
  136. return url
  137. }
  138. export const saveToAlbum = (url: string) => {
  139. return new Promise<Number>((resove) => {
  140. wx.authorize({
  141. scope: 'scope.writePhotosAlbum', // 请求保存到相册的授权
  142. success() {
  143. console.log('success')
  144. // 用户同意授权,继续下一步操作
  145. wx.downloadFile({
  146. url: url,
  147. success(res) {
  148. if (res.statusCode === 200) {
  149. // 下载成功,保存到相册
  150. wx.saveImageToPhotosAlbum({
  151. filePath: res.tempFilePath,
  152. success() {
  153. resove(0)
  154. wx.showToast({
  155. title: '已保存相册',
  156. icon: 'success',
  157. duration: 2000,
  158. })
  159. },
  160. fail(error) {
  161. resove(3)
  162. // 保存失败,可提示用户重新尝试
  163. console.error(error)
  164. },
  165. })
  166. } else {
  167. // 下载失败,可提示用户重新尝试
  168. console.error('下载失败', res.statusCode)
  169. }
  170. },
  171. fail(error) {
  172. resove(2)
  173. // 下载失败,可提示用户重新尝试
  174. console.error('下载失败', error)
  175. },
  176. })
  177. },
  178. fail() {
  179. resove(1)
  180. console.log('faile')
  181. // 用户拒绝授权,可以提示用户去设置页面打开授权
  182. },
  183. })
  184. })
  185. }
  186. export const backPage = () => {
  187. const pages = getCurrentPages() //获取小程序页面栈
  188. // const beforePage = pages[pages.length - 2] //获取上个页面的实例对象
  189. // beforePage.research(keyword) //触发上个页面自定义的shuaxin方法
  190. if (pages.length > 2) {
  191. wx.navigateBack({
  192. delta: 2,
  193. })
  194. return
  195. } else {
  196. Navi.switchTab({ url: '/pages/index/index' })
  197. }
  198. }