From 495f86e7fcc908d8432973fd59bb711a7e46d342 Mon Sep 17 00:00:00 2001 From: RockYang Date: Sat, 5 Oct 2024 10:23:00 +0800 Subject: [PATCH] fixed alipay mobile payment --- api/handler/payment_handler.go | 25 ++++++++++++++++++------- api/service/payment/alipay_service.go | 8 +++----- web/.env.development | 4 ++-- web/src/App.vue | 22 +++++++++++++--------- web/src/store/sharedata.js | 3 +++ web/src/views/ChatPlus.vue | 4 ++++ web/src/views/Index.vue | 5 +++++ 7 files changed, 48 insertions(+), 23 deletions(-) diff --git a/api/handler/payment_handler.go b/api/handler/payment_handler.go index 70c2a8a6..39eb0887 100644 --- a/api/handler/payment_handler.go +++ b/api/handler/payment_handler.go @@ -120,13 +120,24 @@ func (h *PaymentHandler) Pay(c *gin.Context) { returnURL = fmt.Sprintf("%s/payReturn", data.Host) } money := fmt.Sprintf("%.2f", amount) - payURL, err = h.alipayService.PayPC(payment.AlipayParams{ - OutTradeNo: orderNo, - Subject: product.Name, - TotalFee: money, - ReturnURL: returnURL, - NotifyURL: notifyURL, - }) + if data.Device == "wechat" { + payURL, err = h.alipayService.PayMobile(payment.AlipayParams{ + OutTradeNo: orderNo, + Subject: product.Name, + TotalFee: money, + ReturnURL: returnURL, + NotifyURL: notifyURL, + }) + } else { + payURL, err = h.alipayService.PayPC(payment.AlipayParams{ + OutTradeNo: orderNo, + Subject: product.Name, + TotalFee: money, + ReturnURL: returnURL, + NotifyURL: notifyURL, + }) + } + if err != nil { resp.ERROR(c, "error with generate pay url: "+err.Error()) return diff --git a/api/service/payment/alipay_service.go b/api/service/payment/alipay_service.go index d6c8a139..09290df8 100644 --- a/api/service/payment/alipay_service.go +++ b/api/service/payment/alipay_service.go @@ -43,8 +43,8 @@ func NewAlipayService(appConfig *types.AppConfig) (*AlipayService, error) { //client.DebugSwitch = gopay.DebugOn // 开启调试模式 client.SetLocation(alipay.LocationShanghai). // 设置时区,不设置或出错均为默认服务器时间 - SetCharset(alipay.UTF8). // 设置字符编码,不设置默认 utf-8 - SetSignType(alipay.RSA2) // 设置签名类型,不设置默认 RSA2 + SetCharset(alipay.UTF8). // 设置字符编码,不设置默认 utf-8 + SetSignType(alipay.RSA2) // 设置签名类型,不设置默认 RSA2 if err = client.SetCertSnByPath(config.PublicKey, config.RootCert, config.AlipayPublicKey); err != nil { return nil, fmt.Errorf("error with load payment public key: %v", err) @@ -67,10 +67,8 @@ func (s *AlipayService) PayMobile(params AlipayParams) (string, error) { bm.Set("out_trade_no", params.OutTradeNo) bm.Set("quit_url", params.ReturnURL) bm.Set("total_amount", params.TotalFee) - bm.Set("return_url", params.ReturnURL) - bm.Set("notify_url", params.NotifyURL) bm.Set("product_code", "QUICK_WAP_WAY") - return s.client.TradeWapPay(context.Background(), bm) + return s.client.SetNotifyUrl(params.NotifyURL).SetReturnUrl(params.ReturnURL).TradeWapPay(context.Background(), bm) } func (s *AlipayService) PayPC(params AlipayParams) (string, error) { diff --git a/web/.env.development b/web/.env.development index 99693506..2d4761b3 100644 --- a/web/.env.development +++ b/web/.env.development @@ -1,5 +1,5 @@ -VUE_APP_API_HOST=http://localhost:5678 -VUE_APP_WS_HOST=ws://localhost:5678 +VUE_APP_API_HOST=http://www.geekai.me:6004 +VUE_APP_WS_HOST=ws://www.geekai.me:6004 VUE_APP_USER=18888888888 VUE_APP_PASS=12345678 VUE_APP_ADMIN_USER=admin diff --git a/web/src/App.vue b/web/src/App.vue index 31b11c03..a10fb64d 100644 --- a/web/src/App.vue +++ b/web/src/App.vue @@ -13,6 +13,7 @@ import {showMessageInfo} from "@/utils/dialog"; import {useSharedStore} from "@/store/sharedata"; import {getUserToken} from "@/store/session"; import {router} from "@/router"; +import {onBeforeRouteLeave, onBeforeRouteUpdate} from "vue-router"; const debounce = (fn, delay) => { let timer @@ -49,17 +50,11 @@ onMounted(() => { checkSession().then(() => { store.setIsLogin(true) - connect() }).catch(()=>{}) - - // 自动跳转到手机端 - if (isMobile() && !router.currentRoute.value.path.startsWith("/mobile")) { - router.push("/mobile/index") - } }) watch(() => store.isLogin, (val) => { - if (val) { + if (val && store.socket.readyState !== WebSocket.OPEN) { connect() } }) @@ -80,16 +75,25 @@ const connect = () => { _socket.addEventListener('open', () => { console.log('WebSocket 已连接') handler.value = setInterval(() => { - _socket.send(JSON.stringify({"type":"ping"})) + if (_socket.readyState === WebSocket.OPEN) { + _socket.send(JSON.stringify({"type":"ping"})) + } },5000) + // 绑定事件监听 for (const key in store.messageHandlers) { - console.log(key, store.messageHandlers[key]) + console.log(store.messageHandlers[key]) store.setMessageHandler(store.messageHandlers[key]) } }) _socket.addEventListener('close', () => { + // 移除事件监听 + for (const key in store.messageHandlers) { + if (store.socket) { + store.socket.removeEventListener('message', this.messageHandlers[key]) + } + } store.setSocket(null) clearInterval(handler.value) connect() diff --git a/web/src/store/sharedata.js b/web/src/store/sharedata.js index 2056db83..ca94943b 100644 --- a/web/src/store/sharedata.js +++ b/web/src/store/sharedata.js @@ -56,6 +56,9 @@ export const useSharedStore = defineStore('shared', { } }, removeMessageHandler(key) { + if (this.socket) { + this.socket.removeEventListener('message', this.messageHandlers[key]) + } delete this.messageHandlers[key] }, setMobileTheme(theme) { diff --git a/web/src/views/ChatPlus.vue b/web/src/views/ChatPlus.vue index c6c9a96f..1352f9b3 100644 --- a/web/src/views/ChatPlus.vue +++ b/web/src/views/ChatPlus.vue @@ -219,6 +219,7 @@ import ChatReply from "@/components/ChatReply.vue"; import {Delete, Edit, InfoFilled, More, Plus, Promotion, Search, Share, VideoPause} from '@element-plus/icons-vue' import 'highlight.js/styles/a11y-dark.css' import { + isMobile, randString, removeArrayItem, UUID @@ -274,6 +275,9 @@ watch(() => store.chatStream, (newValue) => { stream.value = newValue }); +if (isMobile()) { + router.push('/mobile/chat') +} // 初始化角色ID参数 if (router.currentRoute.value.query.role_id) { diff --git a/web/src/views/Index.vue b/web/src/views/Index.vue index bd037c05..04daec57 100644 --- a/web/src/views/Index.vue +++ b/web/src/views/Index.vue @@ -62,9 +62,14 @@ import FooterBar from "@/components/FooterBar.vue"; import {httpGet} from "@/utils/http"; import {ElMessage} from "element-plus"; import {checkSession, getLicenseInfo, getSystemInfo} from "@/store/cache"; +import {isMobile} from "@/utils/libs"; const router = useRouter() +if (isMobile()) { + router.push("/mobile/index") +} + const title = ref("") const logo = ref("") const slogan = ref("")