|
|
@@ -0,0 +1,197 @@
|
|
|
+import { ghsInfo } from '@/api/ghs/index'
|
|
|
+
|
|
|
+const PENDING_KEY = 'pendingGhsInvite'
|
|
|
+const TTL_MS = 7 * 24 * 60 * 60 * 1000
|
|
|
+
|
|
|
+function parseSceneToInvite(scene) {
|
|
|
+ if (!scene) {
|
|
|
+ return null
|
|
|
+ }
|
|
|
+ const sceneStr = decodeURIComponent(scene)
|
|
|
+ const sceneArray = {}
|
|
|
+ sceneStr.split('&').forEach(item => {
|
|
|
+ const arr = item.split('=')
|
|
|
+ if (arr[0]) {
|
|
|
+ sceneArray[arr[0]] = arr[1] || ''
|
|
|
+ }
|
|
|
+ })
|
|
|
+ const ghsShopId = sceneArray.sId || sceneArray.shopId || 0
|
|
|
+ if (!ghsShopId) {
|
|
|
+ return null
|
|
|
+ }
|
|
|
+ return {
|
|
|
+ ghsShopId: ghsShopId,
|
|
|
+ ghsShopAdminId: sceneArray.gId || sceneArray.ghsShopAdminId || 0,
|
|
|
+ fromCg: 0,
|
|
|
+ source: 'mall_sun_code'
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+function migrateFromLegacy() {
|
|
|
+ const ghsCgOption = uni.getStorageSync('ghsCgOption')
|
|
|
+ if (ghsCgOption && ghsCgOption.shopId) {
|
|
|
+ const invite = {
|
|
|
+ ghsShopId: ghsCgOption.shopId,
|
|
|
+ ghsShopAdminId: ghsCgOption.ghsShopAdminId || 0,
|
|
|
+ fromCg: ghsCgOption.fromCg || 0,
|
|
|
+ source: 'ghsCgOption',
|
|
|
+ ts: Date.now()
|
|
|
+ }
|
|
|
+ uni.setStorageSync(PENDING_KEY, invite)
|
|
|
+ return invite
|
|
|
+ }
|
|
|
+ const scene = uni.getStorageSync('ghsCgScene')
|
|
|
+ if (scene) {
|
|
|
+ const invite = parseSceneToInvite(scene)
|
|
|
+ if (invite) {
|
|
|
+ invite.ts = Date.now()
|
|
|
+ uni.setStorageSync(PENDING_KEY, invite)
|
|
|
+ return invite
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return null
|
|
|
+}
|
|
|
+
|
|
|
+export function optionHasInviteContext(option) {
|
|
|
+ if (!option) {
|
|
|
+ return false
|
|
|
+ }
|
|
|
+ if (option.scene) {
|
|
|
+ return true
|
|
|
+ }
|
|
|
+ if (option.shopId || option.sId) {
|
|
|
+ return true
|
|
|
+ }
|
|
|
+ if (option.fromCg == 1) {
|
|
|
+ return true
|
|
|
+ }
|
|
|
+ if (option.ghsShopAdminId && Number(option.ghsShopAdminId) > 0) {
|
|
|
+ return true
|
|
|
+ }
|
|
|
+ if (option.fromGhsId && Number(option.fromGhsId) > 0) {
|
|
|
+ return true
|
|
|
+ }
|
|
|
+ return false
|
|
|
+}
|
|
|
+
|
|
|
+export function savePendingInviteFromOption(option) {
|
|
|
+ if (!option) {
|
|
|
+ option = {}
|
|
|
+ }
|
|
|
+ let invite = null
|
|
|
+ if (option.scene) {
|
|
|
+ invite = parseSceneToInvite(option.scene)
|
|
|
+ if (invite) {
|
|
|
+ invite.fromCg = option.fromCg || 0
|
|
|
+ }
|
|
|
+ } else if (option.shopId || option.sId) {
|
|
|
+ invite = {
|
|
|
+ ghsShopId: option.shopId || option.sId,
|
|
|
+ ghsShopAdminId: option.ghsShopAdminId || option.gId || 0,
|
|
|
+ fromCg: option.fromCg || 0,
|
|
|
+ source: option.fromCg == 1 ? 'from_cg' : 'query'
|
|
|
+ }
|
|
|
+ } else if (option.ghsShopAdminId) {
|
|
|
+ const scene = uni.getStorageSync('ghsCgScene')
|
|
|
+ if (scene) {
|
|
|
+ invite = parseSceneToInvite(scene)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (!invite || !invite.ghsShopId) {
|
|
|
+ return false
|
|
|
+ }
|
|
|
+ invite.ts = Date.now()
|
|
|
+ uni.setStorageSync(PENDING_KEY, invite)
|
|
|
+ return true
|
|
|
+}
|
|
|
+
|
|
|
+export function getPendingInvite() {
|
|
|
+ let data = uni.getStorageSync(PENDING_KEY)
|
|
|
+ if (!data || !data.ghsShopId) {
|
|
|
+ return null
|
|
|
+ }
|
|
|
+ if (data.ts && Date.now() - data.ts > TTL_MS) {
|
|
|
+ clearPendingInvite()
|
|
|
+ return null
|
|
|
+ }
|
|
|
+ return data
|
|
|
+}
|
|
|
+
|
|
|
+export function restorePendingInviteFromLegacy() {
|
|
|
+ if (getPendingInvite()) {
|
|
|
+ return getPendingInvite()
|
|
|
+ }
|
|
|
+ return migrateFromLegacy()
|
|
|
+}
|
|
|
+
|
|
|
+export function hasPendingInvite() {
|
|
|
+ return !!getPendingInvite()
|
|
|
+}
|
|
|
+
|
|
|
+export function clearPendingInvite() {
|
|
|
+ uni.removeStorageSync(PENDING_KEY)
|
|
|
+}
|
|
|
+
|
|
|
+export function getGhsProductQueryFromPending(pending) {
|
|
|
+ if (!pending) {
|
|
|
+ pending = getPendingInvite()
|
|
|
+ }
|
|
|
+ if (!pending || !pending.ghsShopId) {
|
|
|
+ return null
|
|
|
+ }
|
|
|
+ return {
|
|
|
+ shopId: pending.ghsShopId,
|
|
|
+ id: 0,
|
|
|
+ ghsShopAdminId: pending.ghsShopAdminId || 0,
|
|
|
+ fromCg: pending.fromCg || 0
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+export function tryBindPendingInvite() {
|
|
|
+ const token = uni.getStorageSync('token')
|
|
|
+ if (!token) {
|
|
|
+ return Promise.resolve(false)
|
|
|
+ }
|
|
|
+ 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()
|
|
|
+ return true
|
|
|
+ }
|
|
|
+ return false
|
|
|
+ }).catch(() => false)
|
|
|
+}
|
|
|
+
|
|
|
+export function navigateAfterLogin(vm, option) {
|
|
|
+ const pending = getPendingInvite()
|
|
|
+ if (!pending || !pending.ghsShopId) {
|
|
|
+ return Promise.resolve(false)
|
|
|
+ }
|
|
|
+ if (option && option.fromType && option.fromType == 2) {
|
|
|
+ return Promise.resolve(false)
|
|
|
+ }
|
|
|
+ if (option && option.itemId && Number(option.itemId) > 0 && option.ghsShopId && Number(option.ghsShopId) > 0) {
|
|
|
+ return Promise.resolve(false)
|
|
|
+ }
|
|
|
+ const query = getGhsProductQueryFromPending(pending)
|
|
|
+ return tryBindPendingInvite().then(() => {
|
|
|
+ if (vm && vm.$util && query) {
|
|
|
+ vm.$util.pageTo({ url: '/pagesPurchase/ghsProduct', query: query, type: 2 })
|
|
|
+ }
|
|
|
+ return true
|
|
|
+ })
|
|
|
+}
|
|
|
+
|
|
|
+export function defaultNavigateBack() {
|
|
|
+ const pages = getCurrentPages()
|
|
|
+ const prevPage = pages[pages.length - 2]
|
|
|
+ if (prevPage && prevPage.$vm) {
|
|
|
+ prevPage.$vm.backAction = 'login'
|
|
|
+ uni.navigateBack()
|
|
|
+ } else {
|
|
|
+ uni.reLaunch({ url: '/admin/home/workbench' })
|
|
|
+ }
|
|
|
+}
|