UAW/src/utils/webapi.js
2024-04-13 16:44:49 +08:00

256 lines
6.6 KiB
JavaScript

const API_BASE = process.env.VUE_APP_API_URL.replace('/api', '');
import CryptoJS from 'crypto-js'
import axios from 'axios'
import store from '@/store'
const toJson = res => res.json();
const httpPost = async (url, data) => {
const token = store.state.user.token;
let headers = {"Content-Type": "application/json"};
// let token = token;
if (token) {
headers['Authorization'] = `Bearer ${token}`;
}
return fetch(url, {
method: "POST",
body: JSON.stringify(data),
headers
}).then(toJson);
}
const httpGet = async (url) => {
const token = store.state.user.token;
let headers = {"Content-Type": "application/json"};
if (token) {
headers['Authorization'] = `Bearer ${token}`;
}
return fetch(url, {
method: "GET",
headers
}).then(toJson);
}
const base58Alphabet = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';
export const hexToBase58 = (hexString) => {
const bytes = hexString.match(/.{1,2}/g).map(byte => parseInt(byte, 16));
let base58String = '';
let num = BigInt('0x' + hexString);
while (num > 0n) {
const remainder = num % 58n;
num = num / 58n;
base58String = base58Alphabet[Number(remainder)] + base58String;
}
return base58String;
}
export const aesEncrypt = (plaintText, key) => {
key = CryptoJS.SHA1(key).toString().substring(0,16)
key = CryptoJS.enc.Base64.parse(key)
let encryptedData = CryptoJS.AES.encrypt(plaintText, key, {
mode: CryptoJS.mode.ECB,
padding: CryptoJS.pad.Pkcs7
});
return encryptedData.toString(CryptoJS.format.Hex);
}
export const loginNonce = async (address) => {
const nonceRes = await fetch(`${API_BASE}/api/wallet/nonce?address=${address}`).then(toJson);
const { nonce } = nonceRes.data;
return nonce;
}
export const loginWithSignature = async (message, signature, activity) => {
return fetch(`${API_BASE}/api/wallet/login`, {
method: "POST",
body: JSON.stringify({ message, signature, activity }),
headers: {
"Content-Type": "application/json",
},
}).then(toJson);
}
export const beginPost = async (activity) => {
return fetch(`${API_BASE}/api/tasks/begin_task`, {
method: "POST",
body: JSON.stringify({ activity }),
headers: {
"Content-Type": "application/json",
},
}).then(toJson);
}
/**
* past 方法
* @param {string} url [请求地址]
* @param {object} params [请求参数]
*/
function post(url, params) {
return new Promise((resolve, reject) => {
axios
.post(url, QS.stringify(params))
.then((res) => {
resolve(res.data);
})
.catch((err) => {
reject(err.data);
});
});
}
// export async function apiBeginActivity( data) {
// axios.post('/api/tasks/begin_task',{ params: data,
// headers: { Authorization: `Bearer ${token}` },
// }).then(res => {
// return res.data.data
// })
// }
// export async function apiCheckActivity( data) {
// console.log(data)
// axios.get('/api/tasks/check_task',{ params: {task: data},
// headers: { Authorization: `Bearer ${token}` },
// }).then(res => {
// return res.data.data
// })
// }
// 开始任务
export const apiBeginActivity = async (task) => {
const url = `${API_BASE}/api/tasks/begin_task`;
return httpPost(url, { task })
}
// 检查任务状态
export const apiCheckActivity = async (task) => {
const url = `${API_BASE}/api/tasks/check_task`;
return httpPost(url, { task })
}
// 查看任务进度
export const apiProgress = async () => {
const url = `${API_BASE}/api/tasks/progress`;
return httpPost(url, { })
}
// 领取奖励
export const apiTaskClaim = async (task) => {
const url = `${API_BASE}/api/tasks/claim`;
return httpPost(url, { task })
}
// 用户状态
export const apiUserState = async () => {
const url = `${API_BASE}/api/user/state`;
return httpGet(url)
}
// 探索状态
export const apiGameStat = async () => {
const url = `${API_BASE}/api/game/stat`;
return httpGet(url)
}
// 开启地图
export const apiUsercheckin = async () => {
const url = `${API_BASE}/api/user/checkin`;
return httpPost(url, { })
}
// 探索
export const apiGameStep = async (id) => {
const url = `${API_BASE}/api/game/step`;
return httpPost(url, { id })
}
// 探索前置
export const apiPreStep = async (step) => {
const url = `${API_BASE}/api/game/pre_step`;
return httpPost(url, { step })
}
// 签到列表
export const apiCheckin = async (tag) => {
const url = `${API_BASE}/api/user/checkin/list/${tag}`;
return httpGet(url)
}
// 连续签到奖励
export const apiCheckinClaimSeq = async (days) => {
const url = `${API_BASE}/api/user/checkin/claim_seq`;
return httpPost(url, { days })
}
// 累计签到奖励
export const apiCheckinClaim = async (days) => {
const url = `${API_BASE}/api/user/checkin/claim`;
return httpPost(url, { days })
}
// 积分排行榜
export const apiLeaderBoard = async (data) => {
const url = `${API_BASE}/api/activity/leaderboard/${data.activity}/${data.page}`;
return httpPost(url, { })
}
// 宝箱列表
export const apiBoxList = async () => {
const url = `${API_BASE}/api/chest/list`;
return httpPost(url, { })
}
// 宝箱助力
export const apiEnhanceBox = async (coder) => {
const url = `${API_BASE}/api/chest/enhance`;
return httpPost(url, {coder})
}
// 开启宝箱
export const apiBoxOpen = async (chestId) => {
const url = `${API_BASE}/api/chest/open`;
return httpPost(url, {chestId})
}
export const checkReCaptcha = async(action) =>{
const site_key = '6Ld3xoIpAAAAABW7f5ImgAC6GcWLeDCbt5HPXqe2'
return new Promise((resolve,reject) => {
window.grecaptcha.ready(function() {
window.grecaptcha.execute(site_key, { action }).then(function(token) {
resolve && resolve(token)
}).catch(e => {
reject && reject(e)
})
});
})
}
// 转发推特
export const retweetTwitter = async(desc) => {
const url = 'https://twitter.com/intent/tweet?text=';
const hashtags = encodeURIComponent(`${desc}`);
// const imageUrl = encodeURIComponent(`https://opensea.io/assets/ethereum/0xec23679653337d4c6390d0eeba682246a6067777/${this.NFTID}`);
// const params = `${hashtags}&url=${imageUrl}`;
const params = `${hashtags}`;
window.open(`${url}${params}`, '_blank');
}
export const followTwitter = async(name) => {
const url = `https://twitter.com/intent/follow?screen_name=${encodeURIComponent(name)}`
window.open(url, '_blank');
}
export const joinDiscord = (id) => {
const url = `https://discord.gg/${id}`
window.open(url, '_blank');
}
// NFT 合作伙伴列表
export const apiNftList = async () => {
const url = `${API_BASE}/api/partner/nfts`;
return httpPost(url, {})
}