123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216 |
- import dayjs from 'dayjs'
- import Navi from './navi'
- /**
- * 手机号码*加密函数
- * @param {string} phone 电话号
- * @returns
- */
- export const phoneEncryption = (phone: string) => {
- return phone.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2')
- }
- // 内置手机号正则字符串
- export const innerPhoneReg = '^1(?:3\\d|4[4-9]|5[0-35-9]|6[67]|7[0-8]|8\\d|9\\d)\\d{8}$'
- export const formatTime = (date: Date) => {
- const year = date.getFullYear()
- const month = date.getMonth() + 1
- const day = date.getDate()
- const hour = date.getHours()
- const minute = date.getMinutes()
- const second = date.getSeconds()
- return (
- [year, month, day].map(formatNumber).join('/') +
- ' ' +
- [hour, minute, second].map(formatNumber).join(':')
- )
- }
- export const formatNumber = (n: number) => {
- const s = n.toString()
- return s[1] ? s : '0' + s
- }
- export const delay = (millionseconds: number) => {
- return new Promise<void>((resolve) => setTimeout(() => resolve(), millionseconds))
- }
- export const obj2Params = (obj: Record<string, string>, encode = false) => {
- const result: string[] = []
- if (obj) {
- Object.keys(obj).forEach((key) =>
- result.push(`${key}=${encode ? encodeURIComponent(obj[key]) : obj[key]}`),
- )
- }
- return result.join('&')
- }
- export const clearUser = () => {
- wx.removeStorageSync('__uvx')
- }
- export const getUser = () => {
- const info = wx.getStorageSync('__uvx')
- if (info && info.length > 0) {
- const user = JSON.parse(info) as OpenUserInfo
- const now = dayjs()
- const exp = dayjs(user.expiration)
- if (now.isAfter(exp)) {
- clearUser()
- } else {
- return {
- avatarUrl:
- user.avatar && user.avatar.length > 0 ? user.avatar : '/assets/navbar/avatar.png',
- nickName: user.name,
- phoneNumber: user.phone,
- }
- }
- }
- return null
- }
- export const updateAvatar = (obj: string) => {
- const info = wx.getStorageSync('__uvx')
- if (info && info.length > 0) {
- const user = JSON.parse(info) as OpenUserInfo
- const now = dayjs()
- const exp = dayjs(user.expiration)
- if (now.isAfter(exp)) {
- clearUser()
- } else {
- user.avatar = obj
- wx.setStorageSync('__uvx', JSON.stringify(user))
- }
- }
- return null
- }
- export const updateName = (obj: string) => {
- const info = wx.getStorageSync('__uvx')
- if (info && info.length > 0) {
- const user = JSON.parse(info) as OpenUserInfo
- const now = dayjs()
- const exp = dayjs(user.expiration)
- if (now.isAfter(exp)) {
- clearUser()
- } else {
- user.name = obj
- wx.setStorageSync('__uvx', JSON.stringify(user))
- }
- }
- return null
- }
- export const getSign = () => {
- const info = wx.getStorageSync('__uvx')
- if (info && info.length > 0) {
- const user = JSON.parse(info) as OpenUserInfo
- const now = dayjs()
- const exp = dayjs(user.expiration)
- if (now.isAfter(exp)) {
- clearUser()
- } else {
- return user.signature
- }
- }
- return null
- }
- export const isLogin = () => {
- const info = wx.getStorageSync('__uvx')
- if (info && info.length > 0) {
- const user = JSON.parse(info) as OpenUserInfo
- const now = dayjs()
- const exp = dayjs(user.expiration)
- if (now.isAfter(exp)) {
- clearUser()
- } else {
- return true
- }
- }
- return false
- }
- export const rpxToPx = (rpx: string) => {
- return (Number(rpx.toString().replace(/rpx$/gi, '')) * wx.getSystemInfoSync().windowWidth) / 750
- }
- export const getCurrentUrl = () => {
- const pages = getCurrentPages() //获取加载的页面
- const currentPage = pages[pages.length - 1] //获取当前页面的对象
- let query = Object.keys(currentPage.options)
- .map((o) => {
- const obj = currentPage.options[o]
- return `${o}=${obj}`
- })
- .join('&')
- query = query && query.length > 0 ? '?' + query : ''
- const url = currentPage.route + query //当前页面url
- return url
- }
- export const saveToAlbum = (url: string) => {
- return new Promise<Number>((resove) => {
- wx.authorize({
- scope: 'scope.writePhotosAlbum', // 请求保存到相册的授权
- success() {
- console.log('success')
- // 用户同意授权,继续下一步操作
- wx.downloadFile({
- url: url,
- success(res) {
- if (res.statusCode === 200) {
- // 下载成功,保存到相册
- wx.saveImageToPhotosAlbum({
- filePath: res.tempFilePath,
- success() {
- resove(0)
- wx.showToast({
- title: '已保存相册',
- icon: 'success',
- duration: 2000,
- })
- },
- fail(error) {
- resove(3)
- // 保存失败,可提示用户重新尝试
- console.error(error)
- },
- })
- } else {
- // 下载失败,可提示用户重新尝试
- console.error('下载失败', res.statusCode)
- }
- },
- fail(error) {
- resove(2)
- // 下载失败,可提示用户重新尝试
- console.error('下载失败', error)
- },
- })
- },
- fail() {
- resove(1)
- console.log('faile')
- // 用户拒绝授权,可以提示用户去设置页面打开授权
- },
- })
- })
- }
- export const backPage = () => {
- const pages = getCurrentPages() //获取小程序页面栈
- // const beforePage = pages[pages.length - 2] //获取上个页面的实例对象
- // beforePage.research(keyword) //触发上个页面自定义的shuaxin方法
- if (pages.length > 2) {
- wx.navigateBack({
- delta: 2,
- })
- return
- } else {
- Navi.switchTab({ url: '/pages/index/index' })
- }
- }
|