Browse Source

feat(小程序/首页): 新增短链登录

qq12rrr 3 months ago
parent
commit
d4ddbc9094
5 changed files with 80 additions and 5 deletions
  1. 10 1
      src/app.tsx
  2. 20 2
      src/pages/home/index.tsx
  3. 20 2
      src/pages/user/components/LoginBox.tsx
  4. 5 0
      src/utils/storageUtil.ts
  5. 25 0
      src/utils/toolsUtil.ts

+ 10 - 1
src/app.tsx

@@ -21,6 +21,7 @@ import { useRequest } from "ahooks";
 import { Provider as ReduxProvider } from "react-redux";
 import Taro, { useDidShow } from "@tarojs/taro";
 import { WebView } from "@tarojs/components";
+import StorageUtil, { EStorage } from "@/utils/storageUtil";
 import AppContext from "./context/appContext";
 import ReduxUtil from "./utils/reduxUtil";
 import RouteUtil from "./utils/routeUtil";
@@ -35,7 +36,7 @@ const App = (props: PropsWithChildren<IAppProps>) => {
     return UserHelper.cacheTabMenu();
   });
   useDidShow((options: any) => {
-    if (options.query.source === "app") {
+    if (options.query?.source === "app" && options.query?.platform) {
       let jumpUrls = "";
 
       switch (options.query?.platform) {
@@ -55,6 +56,14 @@ const App = (props: PropsWithChildren<IAppProps>) => {
         url: jumpUrls,
       });
     }
+
+    if (StorageUtil.get(EStorage.channelCode)) {
+      StorageUtil.remove(EStorage.channelCode);
+    }
+
+    if (options.query?.channelCode) {
+      StorageUtil.set(EStorage.channelCode, options.query?.channelCode);
+    }
   });
   return (
     // (appContext.systemInfo?.screenHeight || 0) - (appContext.systemInfo?.safeArea?.bottom || 0);

+ 20 - 2
src/pages/home/index.tsx

@@ -18,7 +18,12 @@ import {
   Text,
   OfficialAccount,
 } from "@tarojs/components";
-import { parseQueryParams, isOuterLink } from "@/utils/toolsUtil";
+import {
+  parseQueryParams,
+  isOuterLink,
+  replaceQueryParam,
+} from "@/utils/toolsUtil";
+import StorageUtil, { EStorage } from "@/utils/storageUtil";
 import { pxTransform } from "@tarojs/taro";
 import { useRequest } from "ahooks";
 import React, { useContext, useMemo, useState, useRef } from "react";
@@ -26,6 +31,8 @@ import LoginBox from "../user/components/LoginBox";
 import BottomBarContext from "@/context/bottomBarContext";
 
 const Home = () => {
+  const channelCode = StorageUtil.get(EStorage.channelCode) || "";
+
   const bottomBarContext = useContext(BottomBarContext);
 
   const childRef: any = useRef();
@@ -72,9 +79,20 @@ const Home = () => {
   const loginFlowHandler = async () => {
     await UserHelper.UpdateOpenId();
     if (isOuterLink(indexInfoResult.data.link)) {
+      // 如果是外部链接,不用管
       await postJumpUrlResult();
     } else {
-      await RouteUtil.toWebViewPage({ url: indexInfoResult.data.link });
+      const jumpUrl = channelCode
+        ? replaceQueryParam(
+            indexInfoResult.data.link,
+            "channelCode",
+            channelCode
+          )
+        : indexInfoResult.data.link;
+
+      RouteUtil.toWebViewPage({
+        url: jumpUrl,
+      });
     }
   };
 

+ 20 - 2
src/pages/user/components/LoginBox.tsx

@@ -4,9 +4,14 @@ import Button from "@/component/button";
 import APPConfig from "@/config";
 import UserHelper from "@/helper/userHelper";
 import RouteUtil from "@/utils/routeUtil";
+import StorageUtil, { EStorage } from "@/utils/storageUtil";
 import { Image, View, Text, Input } from "@tarojs/components";
 import { useRequest } from "ahooks";
-import { parseQueryParams, isOuterLink } from "../../../utils/toolsUtil";
+import {
+  parseQueryParams,
+  isOuterLink,
+  replaceQueryParam,
+} from "../../../utils/toolsUtil";
 import { useState, useEffect, useImperativeHandle, forwardRef } from "react";
 import { AtFloatLayout } from "taro-ui";
 import { showToast } from "@tarojs/taro";
@@ -14,6 +19,8 @@ import ctyptoUtil from "@/utils/crypto";
 
 // @ts-ignore
 const LoginBox = forwardRef((props, ref) => {
+  const channelCode = StorageUtil.get(EStorage.channelCode) || "";
+
   useImperativeHandle(ref, () => ({
     handleOpen() {
       handleOpen();
@@ -44,9 +51,20 @@ const LoginBox = forwardRef((props, ref) => {
   const loginFlowHandler = async () => {
     await UserHelper.UpdateOpenId();
     if (isOuterLink(indexInfoResult?.data.link)) {
+      // 如果是外部链接,不用管
       await postJumpUrlResult();
     } else {
-      await RouteUtil.toWebViewPage({ url: indexInfoResult?.data.link });
+      const jumpUrl = channelCode
+        ? replaceQueryParam(
+            indexInfoResult.data.link,
+            "channelCode",
+            channelCode
+          )
+        : indexInfoResult.data.link;
+
+      RouteUtil.toWebViewPage({
+        url: jumpUrl,
+      });
     }
   };
 

+ 5 - 0
src/utils/storageUtil.ts

@@ -3,6 +3,7 @@ import Taro from "@tarojs/taro";
 export enum EStorage {
   userInfo = "USER_INFO",
   tabMenu = "TAB_MENU",
+  channelCode = "CHANNEL_CODE",
 }
 
 export default class StorageUtil {
@@ -22,4 +23,8 @@ export default class StorageUtil {
     } catch (error) {}
     return value;
   }
+
+  public static remove(key: EStorage): any {
+    return Taro.removeStorageSync(key);
+  }
 }

+ 25 - 0
src/utils/toolsUtil.ts

@@ -42,3 +42,28 @@ export const parseQueryParams = (url: string) => {
 export const isOuterLink = (url: string) => {
   return !url.includes("/promote/home");
 };
+
+/**
+ * 替换url的里的参数
+ */
+export const replaceQueryParam = (
+  url: any,
+  paramName: string,
+  newValue: any
+) => {
+  const [baseUrl, queryString] = url.split("?");
+  if (!queryString) {
+    return url;
+  }
+
+  const params = queryString.split("&").map((param: any) => {
+    const [key] = param.split("=");
+    if (key === paramName) {
+      return `${key}=${newValue}`;
+    }
+
+    return param;
+  });
+
+  return `${baseUrl}?${params.join("&")}`;
+};