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 backPage = () => {
  123. const pages = getCurrentPages() //获取小程序页面栈
  124. // const beforePage = pages[pages.length - 2] //获取上个页面的实例对象
  125. // beforePage.research(keyword) //触发上个页面自定义的shuaxin方法
  126. if (pages.length > 1) {
  127. wx.navigateBack({
  128. delta: 1,
  129. })
  130. return
  131. } else {
  132. Navi.switchTab({ url: '/pages/index/index' })
  133. }
  134. }
  135. export const rpxToPx = (rpx: string) => {
  136. return (Number(rpx.toString().replace(/rpx$/gi, '')) * wx.getSystemInfoSync().windowWidth) / 750
  137. }
  138. export const getCurrentUrl = () => {
  139. const pages = getCurrentPages() //获取加载的页面
  140. const currentPage = pages[pages.length - 1] //获取当前页面的对象
  141. let query = Object.keys(currentPage.options)
  142. .map((o) => {
  143. const obj = currentPage.options[o]
  144. return `${o}=${obj}`
  145. })
  146. .join('&')
  147. query = query && query.length > 0 ? '?' + query : ''
  148. const url = currentPage.route + query //当前页面url
  149. return url
  150. }
  151. export const saveToAlbum = (url: string) => {
  152. return new Promise<Number>((resove) => {
  153. wx.authorize({
  154. scope: 'scope.writePhotosAlbum', // 请求保存到相册的授权
  155. success() {
  156. console.log('success')
  157. // 用户同意授权,继续下一步操作
  158. wx.downloadFile({
  159. url: url,
  160. success(res) {
  161. if (res.statusCode === 200) {
  162. // 下载成功,保存到相册
  163. wx.saveImageToPhotosAlbum({
  164. filePath: res.tempFilePath,
  165. success() {
  166. resove(0)
  167. wx.showToast({
  168. title: '已保存相册',
  169. icon: 'success',
  170. duration: 2000,
  171. })
  172. },
  173. fail(error) {
  174. resove(3)
  175. // 保存失败,可提示用户重新尝试
  176. console.error(error)
  177. },
  178. })
  179. } else {
  180. // 下载失败,可提示用户重新尝试
  181. console.error('下载失败', res.statusCode)
  182. }
  183. },
  184. fail(error) {
  185. resove(2)
  186. // 下载失败,可提示用户重新尝试
  187. console.error('下载失败', error)
  188. },
  189. })
  190. },
  191. fail() {
  192. resove(1)
  193. console.log('faile')
  194. // 用户拒绝授权,可以提示用户去设置页面打开授权
  195. },
  196. })
  197. })
  198. }