|
|
@@ -1,7 +1,8 @@
|
|
|
import { ghsInfo } from '@/api/ghs/index'
|
|
|
|
|
|
const PENDING_KEY = 'pendingGhsInvite'
|
|
|
-const TTL_MS = 7 * 24 * 60 * 60 * 1000
|
|
|
+const LEGACY_TS_KEY = 'ghsCgInviteTs'
|
|
|
+const TTL_MS = 3 * 24 * 60 * 60 * 1000
|
|
|
|
|
|
function parseSceneToInvite(scene) {
|
|
|
if (!scene) {
|
|
|
@@ -27,7 +28,56 @@ function parseSceneToInvite(scene) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+function getStoredInviteTs() {
|
|
|
+ const legacyTs = uni.getStorageSync(LEGACY_TS_KEY)
|
|
|
+ if (legacyTs) {
|
|
|
+ return Number(legacyTs)
|
|
|
+ }
|
|
|
+ const pending = uni.getStorageSync(PENDING_KEY)
|
|
|
+ if (pending && pending.ts) {
|
|
|
+ return Number(pending.ts)
|
|
|
+ }
|
|
|
+ return 0
|
|
|
+}
|
|
|
+
|
|
|
+function touchInviteTs() {
|
|
|
+ if (!getStoredInviteTs()) {
|
|
|
+ uni.setStorageSync(LEGACY_TS_KEY, Date.now())
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+function getInviteTsForSave() {
|
|
|
+ return getStoredInviteTs() || Date.now()
|
|
|
+}
|
|
|
+
|
|
|
+function isInviteExpired() {
|
|
|
+ const ts = getStoredInviteTs()
|
|
|
+ if (!ts) {
|
|
|
+ return false
|
|
|
+ }
|
|
|
+ return Date.now() - ts > TTL_MS
|
|
|
+}
|
|
|
+
|
|
|
+export function expireInviteIfNeeded() {
|
|
|
+ if (!isInviteExpired()) {
|
|
|
+ return false
|
|
|
+ }
|
|
|
+ clearInviteContext()
|
|
|
+ return true
|
|
|
+}
|
|
|
+
|
|
|
+export function clearInviteContext() {
|
|
|
+ uni.removeStorageSync(PENDING_KEY)
|
|
|
+ uni.removeStorageSync('ghsCgScene')
|
|
|
+ uni.removeStorageSync('ghsCgOption')
|
|
|
+ uni.removeStorageSync(LEGACY_TS_KEY)
|
|
|
+}
|
|
|
+
|
|
|
function migrateFromLegacy() {
|
|
|
+ if (expireInviteIfNeeded()) {
|
|
|
+ return null
|
|
|
+ }
|
|
|
+ const inviteTs = getInviteTsForSave()
|
|
|
const ghsCgOption = uni.getStorageSync('ghsCgOption')
|
|
|
if (ghsCgOption && ghsCgOption.shopId) {
|
|
|
const invite = {
|
|
|
@@ -35,7 +85,7 @@ function migrateFromLegacy() {
|
|
|
ghsShopAdminId: ghsCgOption.ghsShopAdminId || 0,
|
|
|
fromCg: ghsCgOption.fromCg || 0,
|
|
|
source: 'ghsCgOption',
|
|
|
- ts: Date.now()
|
|
|
+ ts: inviteTs
|
|
|
}
|
|
|
uni.setStorageSync(PENDING_KEY, invite)
|
|
|
return invite
|
|
|
@@ -44,7 +94,7 @@ function migrateFromLegacy() {
|
|
|
if (scene) {
|
|
|
const invite = parseSceneToInvite(scene)
|
|
|
if (invite) {
|
|
|
- invite.ts = Date.now()
|
|
|
+ invite.ts = inviteTs
|
|
|
uni.setStorageSync(PENDING_KEY, invite)
|
|
|
return invite
|
|
|
}
|
|
|
@@ -75,9 +125,11 @@ export function optionHasInviteContext(option) {
|
|
|
}
|
|
|
|
|
|
export function persistInviteContext(option) {
|
|
|
- if (!option) {
|
|
|
+ if (!option || !optionHasInviteContext(option)) {
|
|
|
return false
|
|
|
}
|
|
|
+ expireInviteIfNeeded()
|
|
|
+ touchInviteTs()
|
|
|
const saved = savePendingInviteFromOption(option)
|
|
|
if (option.scene) {
|
|
|
uni.setStorageSync('ghsCgScene', option.scene)
|
|
|
@@ -114,18 +166,21 @@ export function savePendingInviteFromOption(option) {
|
|
|
if (!invite || !invite.ghsShopId) {
|
|
|
return false
|
|
|
}
|
|
|
- invite.ts = Date.now()
|
|
|
+ invite.ts = getInviteTsForSave()
|
|
|
uni.setStorageSync(PENDING_KEY, invite)
|
|
|
return true
|
|
|
}
|
|
|
|
|
|
export function getPendingInvite() {
|
|
|
+ if (expireInviteIfNeeded()) {
|
|
|
+ return null
|
|
|
+ }
|
|
|
let data = uni.getStorageSync(PENDING_KEY)
|
|
|
if (!data || !data.ghsShopId) {
|
|
|
return null
|
|
|
}
|
|
|
if (data.ts && Date.now() - data.ts > TTL_MS) {
|
|
|
- clearPendingInvite()
|
|
|
+ clearInviteContext()
|
|
|
return null
|
|
|
}
|
|
|
return data
|
|
|
@@ -146,6 +201,18 @@ export function clearPendingInvite() {
|
|
|
uni.removeStorageSync(PENDING_KEY)
|
|
|
}
|
|
|
|
|
|
+export function getRegisterInviteParams(option) {
|
|
|
+ restorePendingInviteFromLegacy()
|
|
|
+ const pending = getPendingInvite()
|
|
|
+ const ghsShopAdminId = (option && option.ghsShopAdminId) || (pending && pending.ghsShopAdminId) || 0
|
|
|
+ const inviteGhsShopId = (pending && pending.ghsShopId) || 0
|
|
|
+ return {
|
|
|
+ ghsShopAdminId: ghsShopAdminId,
|
|
|
+ hdShopAdminId: (option && option.hdShopAdminId) || 0,
|
|
|
+ inviteGhsShopId: inviteGhsShopId
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
export function getGhsProductQueryFromPending(pending) {
|
|
|
if (!pending) {
|
|
|
pending = getPendingInvite()
|
|
|
@@ -179,32 +246,53 @@ export function buildGhsInviteSharePayload(ghsInfo) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-export function tryBindPendingInvite() {
|
|
|
- const token = uni.getStorageSync('token')
|
|
|
- if (!token) {
|
|
|
- return Promise.resolve(false)
|
|
|
- }
|
|
|
- restorePendingInviteFromLegacy()
|
|
|
+function bindPendingInviteOnce() {
|
|
|
const pending = getPendingInvite()
|
|
|
if (!pending || !pending.ghsShopId) {
|
|
|
return Promise.resolve(false)
|
|
|
}
|
|
|
return ghsInfo({ shopId: pending.ghsShopId }).then(res => {
|
|
|
if (res.code == 1 && res.data && res.data.id > 0) {
|
|
|
- clearPendingInvite()
|
|
|
+ clearInviteContext()
|
|
|
return true
|
|
|
}
|
|
|
return false
|
|
|
}).catch(() => false)
|
|
|
}
|
|
|
|
|
|
+export function tryBindPendingInvite(retryCount) {
|
|
|
+ const token = uni.getStorageSync('token')
|
|
|
+ if (!token) {
|
|
|
+ return Promise.resolve(false)
|
|
|
+ }
|
|
|
+ restorePendingInviteFromLegacy()
|
|
|
+ const pending = getPendingInvite()
|
|
|
+ if (!pending || !pending.ghsShopId) {
|
|
|
+ return Promise.resolve(false)
|
|
|
+ }
|
|
|
+ const maxRetry = typeof retryCount === 'number' ? retryCount : 2
|
|
|
+ let attempt = 0
|
|
|
+ const run = () => {
|
|
|
+ return bindPendingInviteOnce().then(bound => {
|
|
|
+ if (bound || attempt >= maxRetry) {
|
|
|
+ return bound
|
|
|
+ }
|
|
|
+ attempt += 1
|
|
|
+ return new Promise(resolve => {
|
|
|
+ setTimeout(resolve, 400)
|
|
|
+ }).then(run)
|
|
|
+ })
|
|
|
+ }
|
|
|
+ return run()
|
|
|
+}
|
|
|
+
|
|
|
export function refreshPendingInviteState(supplierList) {
|
|
|
restorePendingInviteFromLegacy()
|
|
|
const pending = getPendingInvite()
|
|
|
if (pending && pending.ghsShopId && supplierList && supplierList.length) {
|
|
|
const bound = supplierList.some(item => Number(item.shopId) === Number(pending.ghsShopId))
|
|
|
if (bound) {
|
|
|
- clearPendingInvite()
|
|
|
+ clearInviteContext()
|
|
|
return false
|
|
|
}
|
|
|
}
|