Sfoglia il codice sorgente

替换报名和领奖的调用

congjiang 4 giorni fa
parent
commit
65ca3593f6
6 ha cambiato i file con 169 aggiunte e 58 eliminazioni
  1. 7 0
      package-lock.json
  2. 1 0
      package.json
  3. 18 0
      src/api/game.js
  4. 2 1
      src/components/Header/index.vue
  5. 112 57
      src/pages/trade/index.vue
  6. 29 0
      src/utils/utils.js

+ 7 - 0
package-lock.json

@@ -23,6 +23,7 @@
         "@dcloudio/uni-mp-weixin": "3.0.0-4030620241128001",
         "@dcloudio/uni-mp-xhs": "3.0.0-4030620241128001",
         "@dcloudio/uni-quickapp-webview": "3.0.0-4030620241128001",
+        "crypto-js": "^4.2.0",
         "dayjs": "^1.11.13",
         "pinia": "^2.0.36",
         "pinia-plugin-persistedstate": "^2.4.0",
@@ -6457,6 +6458,12 @@
         "url": "https://github.com/sponsors/ljharb"
       }
     },
+    "node_modules/crypto-js": {
+      "version": "4.2.0",
+      "resolved": "https://registry.npmmirror.com/crypto-js/-/crypto-js-4.2.0.tgz",
+      "integrity": "sha512-KALDyEYgpY+Rlob/iriUtjV6d5Eq+Y191A5g4UqLAi8CyGP9N1+FdVbkc1SxKc2r4YAYqG8JzO2KGL+AizD70Q==",
+      "license": "MIT"
+    },
     "node_modules/css-font-size-keywords": {
       "version": "1.0.0",
       "resolved": "https://registry.npmjs.org/css-font-size-keywords/-/css-font-size-keywords-1.0.0.tgz",

+ 1 - 0
package.json

@@ -50,6 +50,7 @@
     "@dcloudio/uni-mp-weixin": "3.0.0-4030620241128001",
     "@dcloudio/uni-mp-xhs": "3.0.0-4030620241128001",
     "@dcloudio/uni-quickapp-webview": "3.0.0-4030620241128001",
+    "crypto-js": "^4.2.0",
     "dayjs": "^1.11.13",
     "pinia": "^2.0.36",
     "pinia-plugin-persistedstate": "^2.4.0",

+ 18 - 0
src/api/game.js

@@ -73,4 +73,22 @@ export const reportLogger = (data) => {
     data,
     showLoading:false
   })
+}
+// 直接报名游戏竞赛
+export const enrollGameMatch = (data) => {
+  return http({
+    url: '/api/game/enroll/match',
+    method: 'post',
+    data,
+    showLoading: false
+  })
+}
+// 直接领取游戏奖金
+export const receiveGamePrize = (data) => {
+  return http({
+    url: '/api/game/prize/receive',
+    method: 'post',
+    data,
+    showLoading: false
+  })
 }

+ 2 - 1
src/components/Header/index.vue

@@ -32,7 +32,8 @@ const props = defineProps({
 const emit = defineEmits(['update:address']);
 const address = ref("");
 const getAddress = async () =>{
-  const addr = await getWalletAddress();
+  // const addr = await getWalletAddress();
+  let addr = '0x01Ce8d3Ae9240B029E0868f811dfF77a4F5320f2';
   if (addr) {
     console.log(addr);
     emit('update:address', addr);

+ 112 - 57
src/pages/trade/index.vue

@@ -276,8 +276,8 @@
 import { ref, onMounted,computed, reactive } from "vue";
 import Header from "@/components/Header";
 import { sendSingleReward,WGTForToken,STTForToken,getBalance } from "@/utils/web.js";
-import { matchList, matchRank, matchInfo,verifyEnroll,matchEnroll,userPrize,claimPrize,sysSetting } from "@/api/game";
-import { getreportlogger } from "@/utils/utils.js";
+import { matchList, matchRank, matchInfo,verifyEnroll,matchEnroll,userPrize,claimPrize,sysSetting,enrollGameMatch,receiveGamePrize } from "@/api/game";
+import { getreportlogger,AES_CBC_ENCRYPT } from "@/utils/utils.js";
 import dayjs from "dayjs";
 const timeData = ref({});
 const onChange = (e) => {
@@ -293,12 +293,12 @@ const WGTbalance = ref(0);
 const STTbalance = ref(0);
 const entry_fee = ref(0);
 const wgt_price = ref(0);
-const privateKey = ref('');
 const triggered = ref(false);
 const received = ref(false);
 const receivedItem = ref({});
 const GasRate = ref(1);
-// const privateKey = ref("4d8d38c6433c9552a11181b64a234814cd83f5b0d2c7306545ee097da9c0e4b2");//H5
+// const privateKey = ref('');
+const privateKey = ref("4d8d38c6433c9552a11181b64a234814cd83f5b0d2c7306545ee097da9c0e4b2");//H5
 
 const bgClass = computed(() => {
   if (gameCheck.value === 0) return 'bg1';
@@ -306,7 +306,7 @@ const bgClass = computed(() => {
   return 'bg3';
 });
 const handleAddressUpdate = (addr) => {
-  privateKey.value = window.android.getPrivateKey();//app
+  // privateKey.value = window.android.getPrivateKey();//app
   walletAddress.value = addr;
   getSysSetting();
   getGameMatchList();
@@ -389,27 +389,27 @@ const changeSign = async () =>{
   }
 }
 // 报名
-const getMatchEnroll = async (hash) =>{
-  let res = await matchEnroll({
-    dapp_id:15,
-    match_id:gameList.value[gameCheck.value]?.id,
-    address:walletAddress.value,
-    entry_hash:hash
-  });
-  if (res.status == 200) {
-    uni.hideLoading()
-    uni.showToast({
-      title: '报名成功',
-      icon: 'none',
-      duration: 1000
-    })
-    setTimeout(() => {
-      getGameMatchList(false);
-    }, 1500);
-  }else{
-    uni.hideLoading()
-  }
-}
+// const getMatchEnroll = async (hash) =>{
+//   let res = await matchEnroll({
+//     dapp_id:15,
+//     match_id:gameList.value[gameCheck.value]?.id,
+//     address:walletAddress.value,
+//     entry_hash:hash
+//   });
+//   if (res.status == 200) {
+//     uni.hideLoading()
+//     uni.showToast({
+//       title: '报名成功',
+//       icon: 'none',
+//       duration: 1000
+//     })
+//     setTimeout(() => {
+//       getGameMatchList(false);
+//     }, 1500);
+//   }else{
+//     uni.hideLoading()
+//   }
+// }
 
 const changeWGT = async () => {
   if(WGTbalance.value < wgt_price.value){
@@ -420,7 +420,8 @@ const changeWGT = async () => {
     });
     return;
   }
-  await handleTokenSignUp(WGTForToken, wgt_price.value);
+  // await handleTokenSignUp(WGTForToken, wgt_price.value);
+  await handleTokenSignUp('wgt', wgt_price.value);
 };
 
 const changeSTT = async () => {
@@ -432,7 +433,8 @@ const changeSTT = async () => {
     });
     return;
   }
-  await handleTokenSignUp(STTForToken, entry_fee.value);
+  // await handleTokenSignUp(STTForToken, entry_fee.value);
+  await handleTokenSignUp('stt', entry_fee.value);
 };
 
 const handleTokenSignUp = async (signUpFn, price) => {
@@ -442,18 +444,45 @@ const handleTokenSignUp = async (signUpFn, price) => {
     mask: true
   });
   try {
-    const result = await signUpFn(privateKey.value, price,GasRate.value);
-    if (result.status === "success") {
-      await getMatchEnroll(result.hash);
-    } else {
-      uni.hideLoading();
+    // 加密
+    let {ciphertext, iv} = await AES_CBC_ENCRYPT(privateKey.value);
+    // console.log(signUpFn, ciphertext, iv);
+    let res = await enrollGameMatch({
+      dapp_id:15,match_id:gameDetail.value?.id,address:walletAddress.value,
+      key: ciphertext, _m: iv, type: signUpFn
+    });
+    // console.log(res);
+    if (res.status == 200) {
+      uni.hideLoading()
       uni.showToast({
-        title: result.message || "报名失败",
+        title: '报名成功',
+        icon: 'none',
+        duration: 1000
+      })
+      setTimeout(() => {
+        getGameMatchList(false);
+      }, 1000);
+    }else{
+      uni.hideLoading()
+      uni.showToast({
+        title: res.message || "领取失败",
         icon: 'none',
         duration: 5000
       });
     }
+    // const result = await signUpFn(privateKey.value, price,GasRate.value);
+    // if (result.status === "success") {
+    //   await getMatchEnroll(result.hash);
+    // } else {
+    //   uni.hideLoading();
+    //   uni.showToast({
+    //     title: result.message || "报名失败",
+    //     icon: 'none',
+    //     duration: 5000
+    //   });
+    // }
   } catch (error) {
+    console.log(error)
     uni.hideLoading();
     uni.showToast({
       title: error.message || "报名失败",
@@ -481,24 +510,24 @@ const awardFun = async (item) => {
   }
 };
 // 领取用户奖金
-const getClaimPrize = async (hash) =>{
-  let res = await claimPrize({
-    dapp_id:15,
-    match_id:gameDetail.value?.id,
-    address:walletAddress.value,
-    receive_hash:hash
-  });
-  if (res.status == 200) {
-    uni.showToast({
-      title: '领取成功',
-      icon: 'none',
-      duration: 1000
-    })
-    setTimeout(() => {
-      getGameRank(gameDetail.value?.id)
-    }, 500);
-  }
-}
+// const getClaimPrize = async (hash) =>{
+//   let res = await claimPrize({
+//     dapp_id:15,
+//     match_id:gameDetail.value?.id,
+//     address:walletAddress.value,
+//     receive_hash:hash
+//   });
+//   if (res.status == 200) {
+//     uni.showToast({
+//       title: '领取成功',
+//       icon: 'none',
+//       duration: 1000
+//     })
+//     setTimeout(() => {
+//       getGameRank(gameDetail.value?.id)
+//     }, 500);
+//   }
+// }
 
 const getsendSingleReward = async (prize_money) =>{
   uni.showLoading({
@@ -506,16 +535,42 @@ const getsendSingleReward = async (prize_money) =>{
     mask: true
   });
   try {
-    const result = await sendSingleReward(privateKey.value,prize_money,GasRate.value);
-    if (result.status === "success") {
-      await getClaimPrize(result.hash);
-    }else{
+    // 加密
+    let {ciphertext, iv} = await AES_CBC_ENCRYPT(privateKey.value);
+    // console.log(ciphertext, iv);
+    let res = await receiveGamePrize({
+      dapp_id:15,match_id:gameDetail.value?.id,address:walletAddress.value,
+      key: ciphertext, _m: iv,
+    });
+    console.log(res);
+    if (res.status == 200) {
+      uni.hideLoading()
       uni.showToast({
-        title: result.message || "领取失败",
+        title: '领取成功',
+        icon: 'none',
+        duration: 1000
+      })
+      setTimeout(() => {
+        getGameRank(gameDetail.value?.id)
+      }, 1000);
+    } else {
+      uni.hideLoading()
+      uni.showToast({
+        title: res.message || "领取失败",
         icon: 'none',
         duration: 5000
       });
     }
+    // const result = await sendSingleReward(privateKey.value,prize_money,GasRate.value);
+    // if (result.status === "success") {
+    //   await getClaimPrize(result.hash);
+    // }else{
+    //   uni.showToast({
+    //     title: result.message || "领取失败",
+    //     icon: 'none',
+    //     duration: 5000
+    //   });
+    // }
   } catch (error) {
     uni.showToast({
       title: error.message || "领取失败",

+ 29 - 0
src/utils/utils.js

@@ -1,5 +1,6 @@
 import Web3 from "web3";
 import { reportLogger } from "@/api/game";
+import CryptoJS from "crypto-js";
 export const getWeb3 = async () => {
     const rpcUrl = "https://api.angeltokens.io";
     const web3 = new Web3(new Web3.providers.HttpProvider(rpcUrl));
@@ -109,3 +110,31 @@ export const buildSignatureParams = async (senderAddress, amountWei, contractAdd
         throw new Error("构建签名参数失败:" + (err.message || JSON.stringify(err)));
     }
 };
+
+async function getRandomString16() {
+	const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
+	let result = '';
+	for (let i = 0; i < 16; i++) {
+		result += chars.charAt(Math.floor(Math.random() * chars.length));
+	}
+	return result;
+}
+export async function AES_CBC_ENCRYPT(val) {
+	const IV = await getRandomString16();
+	// const KEY = window.android.getGlobalParam('key');
+    const KEY = '05f8f563b95103e1';
+	const secretKey = CryptoJS.enc.Utf8.parse(KEY);
+	const ivk = CryptoJS.enc.Utf8.parse(IV);
+	let ivHex = CryptoJS.enc.Base64.stringify(ivk);
+	let messageHex = CryptoJS.enc.Utf8.parse(val);
+	let encrypted = CryptoJS.AES.encrypt(messageHex, secretKey, {
+		iv: ivk,
+		mode: CryptoJS.mode.CBC,
+		padding: CryptoJS.pad.Pkcs7
+	});
+	let result = CryptoJS.enc.Base64.stringify(encrypted.ciphertext);
+	return {
+		ciphertext: result,
+		iv:ivHex,
+	};
+}