Browse Source

feat: 增加外部页面跳转流程

await 7 months ago
parent
commit
5d2b03d831
4 changed files with 119 additions and 19 deletions
  1. 19 0
      src/apis/userApi.ts
  2. 34 10
      src/pages/home/index.tsx
  3. 22 9
      src/pages/user/mine/index.tsx
  4. 44 0
      src/utils/toolsUtil.ts

+ 19 - 0
src/apis/userApi.ts

@@ -162,6 +162,7 @@ export  class HomeFetchChannelBody {
     ///
     refer: string
 }
+
 export  class HomeTabUsingPostResponse {
         
                 /// undefined
@@ -171,6 +172,19 @@ export  class HomeTabUsingPostResponse {
                   articleCategories!: HomeTabUsingPostResponseArticleCategories[] ;
                 
 }
+
+export type GetJumpUrlPostBody = {
+    appId: number | string
+
+    channelCode: string
+
+    productId: number
+
+}
+
+export type GetJumpUrlPostResponse = {
+    adJumpLink: string
+}
     
         
         export default class UserApi {
@@ -204,5 +218,10 @@ export  class HomeTabUsingPostResponse {
         static homeFetchChannel(options: AxiosRequestConfigFunc<HomeFetchChannelBody, any>):RequestUtil<any> {
             return new RequestUtil<any>('/h5/query/channel', 'POST', options)
         }
+
+        
+        static getJumpUrlPost(options: AxiosRequestConfigFunc<GetJumpUrlPostBody, any>): RequestUtil<GetJumpUrlPostResponse> {
+            return new RequestUtil<any>('/h5/get/product/jump/url', 'POST' ,options)
+        }
         }
         

+ 34 - 10
src/pages/home/index.tsx

@@ -10,7 +10,9 @@ import APPConfig from "@/config";
 import AppContext from "@/context/appContext";
 import UserHelper from "@/helper/userHelper";
 import ImageUtil from "@/utils/imgUtils";
+import RequestUtil from "@/utils/request";
 import RouteUtil from "@/utils/routeUtil";
+import { parseQueryParams, isOuterLink } from "@/utils/toolsUtil";
 import { Image, ScrollView, View, Text } from "@tarojs/components";
 import Taro, { pxTransform } from "@tarojs/taro";
 import { useRequest } from "ahooks";
@@ -30,12 +32,31 @@ const Home = () => {
   
   const channelResult = useRequest(() => {
     return UserApi.homeFetchChannel({
-      data: { channelCode: 'txxcx01', refer: '' },
+      data: { channelCode: 'statistic', refer: '' },
     }).toData().then(res => {
       setBottomRemark(res.bottom_remark)
     })
   });
 
+  const postJumpUrlResult = () => {
+    const params = parseQueryParams(indexInfoResult.data.link)
+    return UserApi.getJumpUrlPost({
+      loading: true,
+      data: { appId: APPConfig.APPID, channelCode: params.channelCode, productId: params.productId }
+    }).toData().then(res => {
+      console.log("获取跳转链接结果", res)
+      RouteUtil.toWebViewPage({ url: res?.adJumpLink })
+    })
+  }
+
+  const loginFlowHandler = () => {
+    if (isOuterLink(indexInfoResult.data.link)) {
+      postJumpUrlResult()
+    } else {
+      RouteUtil.toWebViewPage({ url: indexInfoResult.data.link })
+    }
+  }
+
 
   const imgBackStyle = useMemo(() => {
     return {
@@ -72,7 +93,6 @@ const Home = () => {
   const BottomRemark = () => bottomRemark.split(';').map(text => <View style={{ margin: '4px 0' }}>{text}</View>)
 
   const ApplyButton = ({ isLogin }) => {
-    console.log("isLogin", isLogin)
     return (
       <>
         {
@@ -80,20 +100,24 @@ const Home = () => {
             UserHelper.afterLogin({
               pop: false,
               callback: async () => {
-                indexInfoResult.data &&
-                  RouteUtil.toWebViewPage({
-                    url: indexInfoResult.data.link,
-                  });
+                indexInfoResult.data && loginFlowHandler()
+                // indexInfoResult.data && postJumpUrlResult()
+                  // RouteUtil.toWebViewPage({
+                  //   url: indexInfoResult.data.link,
+                  // });
               },
             });
           }}
           >立即申请额度</Button>) : (<Button openType='getPhoneNumber' 
             onGetPhoneNumber={async (e) => {
           if (e.detail.code && await UserHelper.userWxLogin(e.detail.code) === true) {
-            indexInfoResult.data &&
-            RouteUtil.toWebViewPage({
-              url: indexInfoResult.data.link,
-            });
+            // indexInfoResult.data &&
+            // RouteUtil.toWebViewPage({
+            //   url: indexInfoResult.data.link,
+            // });
+
+            indexInfoResult.data && loginFlowHandler()
+
               // if (params.callbackBeforeRoute?.toString() === "1") {
               //     console.log('进来了', params)
               //     if (params.page) {

+ 22 - 9
src/pages/user/mine/index.tsx

@@ -13,6 +13,7 @@ import useStore from "@/hooks/useRedux";
 import RouteUtil from "@/utils/routeUtil";
 import UserHelper from "@/helper/userHelper";
 import Button from "@/component/button";
+import { parseQueryParams, isOuterLink } from "@/utils/toolsUtil";
 import "./index.scss";
 
 interface IMineProps {}
@@ -30,10 +31,28 @@ const Mine = (props: PropsWithChildren<IMineProps>) => {
     }).toData();
   });
 
+  const postJumpUrlResult = () => {
+    const params = parseQueryParams(indexInfoResult.data.link)
+    return UserApi.getJumpUrlPost({
+      loading: true,
+      data: { appId: APPConfig.APPID, channelCode: params.channelCode, productId: params.productId }
+    }).toData().then(res => {
+      console.log("获取跳转链接结果", res)
+      RouteUtil.toWebViewPage({ url: res?.adJumpLink })
+    })
+  }
+
+  const loginFlowHandler = () => {
+    if (isOuterLink(indexInfoResult.data.link)) {
+      postJumpUrlResult()
+    } else {
+      RouteUtil.toWebViewPage({ url: indexInfoResult.data.link })
+    }
+  }
+
 
 
   const ApplyButton = ({ isLogin }) => {
-    console.log("isLogin", isLogin)
     return (
       <>
         {
@@ -41,20 +60,14 @@ const Mine = (props: PropsWithChildren<IMineProps>) => {
             UserHelper.afterLogin({
               pop: false,
               callback: async () => {
-                indexInfoResult.data &&
-                  RouteUtil.toWebViewPage({
-                    url: indexInfoResult.data.link,
-                  });
+                indexInfoResult.data && loginFlowHandler()
               },
             });
           }}
           >立即申请</Button>) : (<Button openType='getPhoneNumber' 
             onGetPhoneNumber={async (e) => {
           if (e.detail.code && await UserHelper.userWxLogin(e.detail.code) === true) {
-            indexInfoResult.data &&
-            RouteUtil.toWebViewPage({
-              url: indexInfoResult.data.link,
-            });
+            indexInfoResult.data && loginFlowHandler()
               if (UserHelper.callback) {
                   await UserHelper.callback();
               }

+ 44 - 0
src/utils/toolsUtil.ts

@@ -0,0 +1,44 @@
+// 工具函数
+
+/**
+ * 提取url查询参数
+ * @example
+ * ```js
+ * const url = 'https://baidu.com?s=hello&k=world'
+ * 
+ * parseQueryParams(url)  // { s:'hello', k:'world' }
+ * 
+ * const url2 = 'https://www.baidu.com'
+ * 
+ * parseQueryParams(url2) // {}
+ * 
+ * ```
+ */
+export const parseQueryParams = (url: string) => {
+  const param = {}
+  if (!url) {
+    throw new ReferenceError('param url can not be undefined')
+  }
+  // if the url not includes ? then return result straight
+  if (!url.includes('?')) {
+    return param
+  }
+
+  const [,queryParamsString] = url.split('?')
+
+  const queryParamsArray = queryParamsString.split('&')
+
+  queryParamsArray.forEach((queryItem) => {
+    const [key, value] = queryItem.split('=')
+    param[key] = value
+  })
+
+  return param
+}
+
+/**
+ * 判断是直接跳转内部H5还是外部获取url进行跳转
+ */
+export const isOuterLink = (url: string) => {
+  return !url.includes('/promote/home')
+}