shish 2 months ago
parent
commit
f7da094727

+ 2 - 1
hdApp/src/admin/home/components/purchase-ghs-panel.vue

@@ -242,7 +242,7 @@ import { currentShop } from "@/api/shop";
 import { hasNotice } from "@/api/admin";
 import permision from "@/utils/wa-permission_1.1/permission.js";
 import { iconSrc } from "@/utils/iconSrc";
-import { refreshPendingInviteState, getGhsProductQueryFromPending, buildGhsInviteSharePayload, hasPendingInvite, restorePendingInviteFromLegacy } from "@/utils/pendingGhsInvite";
+import { refreshPendingInviteState, getGhsProductQueryFromPending, buildGhsInviteSharePayload, hasPendingInvite, restorePendingInviteFromLegacy, expireInviteIfNeeded } from "@/utils/pendingGhsInvite";
 
 export default {
   name: "PurchaseGhsPanel",
@@ -319,6 +319,7 @@ export default {
     handlePageUnload() {},
     handlePageShow() {
       this.supplierSearchTime = uni.getStorageSync('supplier_search_time') || 0;
+      expireInviteIfNeeded()
       this.refreshPendingInviteUI()
       this.init();
     },

+ 16 - 7
hdApp/src/pagesClient/official/apply.vue

@@ -133,7 +133,7 @@ import { checkShop } from '@/api/shop'
 import { ghsInfo } from "@/api/ghs/index";
 import { getMobile } from "@/api/auth";
 import {getHasMap} from "@/api/express"
-import { savePendingInviteFromOption, tryBindPendingInvite, optionHasInviteContext, clearPendingInvite, restorePendingInviteFromLegacy, getPendingInvite } from "@/utils/pendingGhsInvite";
+import { persistInviteContext, tryBindPendingInvite, optionHasInviteContext, clearPendingInvite, restorePendingInviteFromLegacy, getRegisterInviteParams, expireInviteIfNeeded } from "@/utils/pendingGhsInvite";
 export default {
   name: "apply-data",
   components: {
@@ -185,8 +185,9 @@ export default {
 		...mapGetters(["getLoginInfo"])
 	},
   onLoad () {
+    expireInviteIfNeeded()
     if (optionHasInviteContext(this.option)) {
-      savePendingInviteFromOption(this.option)
+      persistInviteContext(this.option)
     } else {
       const legacyOption = uni.getStorageSync('ghsCgOption') || {}
       const hadLegacy = !!(uni.getStorageSync('ghsCgScene') || legacyOption.shopId)
@@ -394,9 +395,7 @@ export default {
     },
     confirmFn () {
       restorePendingInviteFromLegacy()
-      const pendingInvite = getPendingInvite()
-      const ghsShopAdminId = this.option.ghsShopAdminId || (pendingInvite && pendingInvite.ghsShopAdminId) || 0
-      const hdShopAdminId = this.option.hdShopAdminId || 0
+      const inviteParams = getRegisterInviteParams(this.option)
       uni.showLoading({mask:true})
       let currentMiniOpenId = uni.getStorageSync('currentMiniOpenId')
       let style = this.option.style || 0
@@ -408,7 +407,17 @@ export default {
       // #ifdef APP-PLUS
       appRegister = 1
       // #endif
-      applyRegister({hdShopAdminId:hdShopAdminId,ghsShopAdminId:ghsShopAdminId,...this.form,style:style,from:from,miniOpenId:currentMiniOpenId,appRegister:appRegister,fillMobile:this.fillMobile}).then(subRes => {
+      applyRegister({
+        hdShopAdminId: inviteParams.hdShopAdminId,
+        ghsShopAdminId: inviteParams.ghsShopAdminId,
+        inviteGhsShopId: inviteParams.inviteGhsShopId,
+        ...this.form,
+        style: style,
+        from: from,
+        miniOpenId: currentMiniOpenId,
+        appRegister: appRegister,
+        fillMobile: this.fillMobile
+      }).then(subRes => {
         uni.hideLoading()
         let token = subRes.data.token || ''
         uni.hideLoading()
@@ -423,7 +432,7 @@ export default {
             return false
           }
           uni.setStorageSync('token', subRes.data.token)
-          uni.setStorageSync('shopId', subRes.data.admin.currentShopId)
+          uni.setStorageSync('shopId', subRes.data.admin.currentShopId || subRes.data.shopId)
           getDictionaries({ token: subRes.data.token }).then(res => {
             if (that.$util.isEmpty(res)) {
               return false

+ 2 - 1
hdApp/src/pagesClient/official/result.vue

@@ -12,7 +12,7 @@
 </template>
 <script>
 import appResult from '@/components/app-result'
-import { getGhsProductQueryFromPending, getPendingInvite } from '@/utils/pendingGhsInvite'
+import { getGhsProductQueryFromPending, getPendingInvite, restorePendingInviteFromLegacy } from '@/utils/pendingGhsInvite'
 export default {
 	name: "ApplyResult",
 	components:{
@@ -27,6 +27,7 @@ export default {
     onUnload() {
     },
     onLoad () {
+      restorePendingInviteFromLegacy()
       const pending = getPendingInvite()
       if (pending) {
         this.fromCg = pending.fromCg || 0

+ 102 - 14
hdApp/src/utils/pendingGhsInvite.js

@@ -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
 		}
 	}