238 lines
7.0 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import type { SelectorQuery } from '@tarojs/taro'
import Taro, { useDidShow, useLaunch, useLoad, useRouter, useShareAppMessage } from '@tarojs/taro'
import { formatImgUrl } from './fotmat'
import { analysisShortCodeApi, bindInvitationUser, getUserInfo } from './shortCode/index'
import { InvitationWay, invitationWay } from './enum'
import { isEmptyObject } from './common'
import { getCDNSource } from './constant'
import { GetAdminUserInfoApi } from '@/api/user'
/**
* 防抖
* @param {*} fn
* @param {*} delay
* @returns
*/
export const debounce = (fn, delay) => {
let timer: any = null
return (...param) => {
if (timer) { clearTimeout(timer) }
timer = setTimeout(() => {
fn(...param)
}, delay)
}
}
/**
* 节流
* @param {*} fn
* @param {*} delay
* @returns
*/
export const throttle = (fn, delay) => {
let pre = 0
return (...params) => {
const now = new Date().getTime()
if (now - pre > delay) {
fn(...params)
pre = now
}
}
}
/**
* 批量过滤对象值为空的属性
* @param {Object} val 需要过滤的对象
* @param {Array} arr 排除过滤的属性
* @returns
*/
export const getFilterData = (val = {}, arr: string[] = []) => {
const res = {}
for (const key in val) {
if (val[key] !== undefined && val[key] !== null && val[key] !== '' && !arr.includes(key)) {
if (typeof val[key] == 'number') {
if (!Number.isNaN(val[key])) {
res[key] = val[key]
}
}
else {
res[key] = val[key]
}
}
}
return res
}
/**
* 对象深拷贝
* @param {*} object
* @returns
*/
export const copyObject = (object) => {
if (object.constructor == Object) {
const keys = Object.keys(object)
const newObject = {}
keys.forEach((key) => {
newObject[key] = copyObject(object[key])
})
return newObject
}
else if (object.constructor == Array) {
return object.map((item) => {
return copyObject(item)
})
}
else {
return object
}
}
/**
*
* @param {*} suffix
* !w80
!w100
!w160
!w200
!w400
!w800
!wh400
!w600
*/
export const screenshot = (url, suffix = '!w200') => {
return url + suffix
}
// 获取数据加载状态 //0:数据从无到有加载数据1没有任何数据 2下拉加载3下拉没有数据
export const dataLoadingStatus = ({ list = [], total = 0, status = false }: { list: any[]; total: number; status: true | false }) => {
if (list.length == 0 && status) {
return 0
}
else if (list.length == 0 && !status) {
return 1
}
else if (list.length < total) {
return 2
}
else {
return 3
}
}
// 全局分享监听
export const useShareShop = () => {
const page = Taro.getCurrentInstance().page
const adminUserInfo = JSON.parse(Taro.getStorageSync('adminUserInfo') || '{}')
useDidShow(() => {
setTimeout(async() => {
const enterOptions = Taro.getEnterOptionsSync()
console.log('enterOptions', enterOptions)
// 当有分享参数时,绑定上下级
// if (enterOptions.query?.share) {
// analysisShortCodeApi(enterOptions.query?.share)
// }
// 扫小程序码进入
if (enterOptions.query?.scene) {
const scene = decodeURIComponent(enterOptions.query?.scene)
const query = analysisScene(scene)
Taro.setStorageSync('invitationInfo', JSON.stringify(query))
const { user_id, invitation_way } = query
// 获取最新的用户信息
const newUserInfo = await getUserInfo()
// 校验是否被绑定过了 自己不能邀请自己
if (!newUserInfo.is_passive_invite && user_id !== newUserInfo.user_id) {
bindInvitationUser(Number(invitation_way) as InvitationWay, Number(user_id))
}
}
if (enterOptions.query.user_id && enterOptions.query.invitation_way) {
Taro.setStorageSync('invitationInfo', JSON.stringify(enterOptions.query))
const { user_id, invitation_way } = enterOptions.query
// 获取最新的用户信息
const newUserInfo = await getUserInfo()
// 校验是否被绑定过了 自己不能邀请自己
if (!newUserInfo.is_passive_invite && user_id !== newUserInfo.user_id) {
bindInvitationUser(Number(invitation_way) as InvitationWay, Number(user_id))
}
}
}, 500)
})
// }
if (page && page.onShareAppMessage) {
console.log('adminUserInfo 真假', adminUserInfo)
page.onShareAppMessage = () => {
const sortCode = Taro.getStorageSync('sort_code') ? JSON.parse(Taro.getStorageSync('sort_code')) : ''
let path = `/pages/index/index?share=${sortCode.shareShortPage.code}`
let title = sortCode.shareShortPage.title
let imageUrl = formatImgUrl('/mall/share_img_01.png', '!w400')
const pageInfo: any = page
const promise = new Promise((resolve, reject) => {
getUserInfo().then((newUserInfo) => {
// 商品详情分享
if (pageInfo.route === 'pages/details/index') {
// path = `/pages/details/index?user_id=${newUserInfo?.user_id}&invitation_way=${InvitationWay.INVITE_LINK}`
path = `/pages/details/index?id=${sortCode.shareShortDetail.id}&user_id=${newUserInfo?.user_id}&invitation_way=${InvitationWay.INVITE_LINK}`
title = sortCode.shareShortDetail.title
imageUrl = sortCode.shareShortDetail.img
}
else {
// 分享链接方式 绑定邀请人
path = `/pages/index/index?user_id=${newUserInfo?.user_id}&invitation_way=${InvitationWay.INVITE_LINK}`
title = '打造面料爆品 专注客户服务'
imageUrl = getCDNSource('/mall/share_img_01.png')
}
console.log('inside promise imageUrl:::', path)
resolve({
path,
title,
imageUrl,
})
})
})
console.log('outside promise imageUrl:::', path)
return {
title,
path,
imageUrl,
promise,
}
}
}
}
function delay(delayTime = 25): Promise<null> {
return new Promise((resolve) => {
setTimeout(() => {
// @ts-expect-error no error
resolve()
}, delayTime)
})
}
export function delayQuerySelector(selectorStr: string, delayTime = 500): Promise<any[]> {
return new Promise((resolve) => {
const selector: SelectorQuery = Taro.createSelectorQuery()
delay(delayTime).then(() => {
selector
.select(selectorStr)
.boundingClientRect()
.exec((res: any[]) => {
resolve(res)
})
})
})
}
function analysisScene(scene: string, split = ';') {
const sceneArr = scene.split(split)
const sceneObj: Record<string, any> = {}
sceneArr.forEach((item) => {
const itemArr = item.split('=')
sceneObj[itemArr[0]] = itemArr[1]
})
return sceneObj
}
function handleBindInvitationUser() {
}