x1ongzhu 2 år sedan
förälder
incheckning
da75516a5d
5 ändrade filer med 53 tillägg och 4 borttagningar
  1. 2 1
      .eslintrc.cjs
  2. 1 0
      index.html
  3. 21 0
      src/api/index.ts
  4. 24 0
      src/main.ts
  5. 5 3
      src/typings/global.d.ts

+ 2 - 1
.eslintrc.cjs

@@ -23,6 +23,7 @@ module.exports = {
     },
     globals: {
         Chat: 'readonly',
-        NoCaptcha: 'readonly'
+        NoCaptcha: 'readonly',
+        wx: 'readonly'
     }
 }

+ 1 - 0
index.html

@@ -9,6 +9,7 @@
 		content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, viewport-fit=cover" />
 	<title>CHILLGPT</title>
     <script type="text/javascript" charset="utf-8" src="//g.alicdn.com/sd/nch5/index.js?t=2015052012"></script>
+    <script type="text/javascript" charset="utf-8" src="http://res.wx.qq.com/open/js/jweixin-1.6.0.js"></script>
 </head>
 
 <body class="dark:bg-black">

+ 21 - 0
src/api/index.ts

@@ -83,3 +83,24 @@ export function fetchSendVerify<T>(data: { phone: string | number; token: string
         data
     })
 }
+
+export function fetchOpenid<T>(code: string) {
+    return get<T>({
+        url: '/weixin/code2openid',
+        data: { code }
+    })
+}
+
+export function fetchJsapiSign<T>(url: string) {
+    return get<T>({
+        url: '/weixin/jsapiSign',
+        data: { url }
+    })
+}
+
+export function fetchPay<T>(openid: string) {
+    return get<T>({
+        url: '/weixin/pay',
+        data: { openid }
+    })
+}

+ 24 - 0
src/main.ts

@@ -5,6 +5,7 @@ import { setupAssets, setupScrollbarStyle } from './plugins'
 import { setupStore } from './store'
 import { setupRouter } from './router'
 import queryString from 'query-string'
+import { fetchOpenid, fetchJsapiSign, fetchPay } from '@/api'
 
 const query = queryString.parse(location.search)
 
@@ -14,6 +15,29 @@ if (query.invitor) {
 }
 if (query.code) {
     alert(query.code + ',' + query.state)
+    let openid: any = ''
+    fetchOpenid(query.code as string)
+        .then(res => {
+            openid = res
+            return fetchJsapiSign(location.href.split('#')[0])
+        })
+        .then((res: any) => {
+            wx.config({
+                ...res,
+                debug: true,
+                jsApiList: ['chooseWXPay', 'updateAppMessageShareData', 'updateTimelineShareData']
+            })
+            wx.ready(function () {
+                fetchPay(openid).then((params: any) => {
+                    wx.chooseWXPay({
+                        ...params,
+                        success: function (res: any) {
+                            console.log(res)
+                        }
+                    })
+                })
+            })
+        })
 }
 
 async function bootstrap() {

+ 5 - 3
src/typings/global.d.ts

@@ -2,8 +2,10 @@ interface Window {
     $loadingBar?: import('naive-ui').LoadingBarProviderInst
     $dialog?: import('naive-ui').DialogProviderInst
     $message?: import('naive-ui').MessageProviderInst
-    $notification?: import('naive-ui').NotificationProviderInst,
-    NoCaptcha?: any,
+    $notification?: import('naive-ui').NotificationProviderInst
+    NoCaptcha?: any
     nc?: any
 }
-declare module 'resolve-url';
+declare module 'resolve-url'
+
+declare let wx: any