Onlife/common/publicFunction.js

132 lines
4.2 KiB
JavaScript
Raw Normal View History

2025-04-19 15:38:48 +08:00
// 公共的方法
import axios from "axios"
const baseUrl = 'https://nfta.ikiry.com'
export default {
// 获取区块链钱包余额和交易记录 url=transactions 获取交易记录 url=balances 获取钱包余额
async getWeb3(url,address){
try {
const headers = {
'Content-Type': 'application/json' // 设置请求内容类型为 JSON
};
// const response = await axios.post(`https://nodejs.oxfma.com:3000/${url}`,{address:address},{ headers: headers });
// const response = await axios.get(`https://nfta.ikiry.com/${url}?address=${address}`);
const response = await axios.get(`${baseUrl}/${url}?address=${address}`);
console.log('response:',response)
// 处理 API 响应
if (response.status === 200) {
const res = response.data;
return res;
} else {
throw new Error(response.data.message);
}
} catch (error) {
console.error('Error fetching transactions:', error);
return [];
}
},
// 使用BNB打款
async BNBdakuan(from_addr,to_addr,coin,siyao){
try {
const response = await axios.get(`${baseUrl}/transfer?from_addr=${from_addr}&to_addr=${to_addr}&siyao=${siyao}&coin=${coin}`);
console.log('使用BNB打款:',response)
// 处理 API 响应
if (response.status === 200) {
const res = response.data;
return response;
} else {
throw new Error(response.data.message);
}
} catch (error) {
console.error('Error fetching transactions:', error);
return [];
}
},
// 使用UDST转账
async qukuailian(from_addr,to_addr,coin,siyao){
try {
// const response = await axios.get(`https://nfta.ikiry.com/qukuailian?from_addr=${from_addr}&to_addr=${to_addr}&coin=${coin}&siyao=${siyao}`);
const response = await axios.get(`${baseUrl}/qukuailian?from_addr=${from_addr}&to_addr=${to_addr}&coin=${coin}&siyao=${siyao}`);
console.log('qukuailian:',response)
// 处理 API 响应
if (response.status === 200) {
const res = response;
return res;
} else {
uni.showToast({ title:'交易失败', icon:'none' })
throw new Error(response.data.message);
}
} catch (error) {
// uni.showToast({ title:'交易失败', icon:'none' })
uni.showToast({ title:error.response.data.error, icon:'none' })
console.error('Error fetching transactions:', error);
return [];
}
},
// 数组根据id去重
newArrId (arr) {
let newArr = []
for(let i = 0;i<arr.length;i++){
const Index = newArr.findIndex((item) => {return item.id === arr[i].id})
// console.log("数组去重",Index)
if(Index==-1){
newArr.push(arr[i])
}
}
return newArr
},
// 金额千分位和保留两位小数点
formatPrice(price) {
console.log(price)
return String(Number(price).toFixed(2)).replace(/\B(?=(\d{3})+(?!\d))/g, ',');
},
// 时间戳转换时间 传毫秒值
getYMDHMS(timestamp){
let time = new Date(Number(timestamp) )
let year = time.getFullYear()
let month = time.getMonth() + 1
let date = time.getDate()
let hours = time.getHours()
let minute = time.getMinutes()
let second = time.getSeconds()
if (month < 10) { month = '0' + month }
if (date < 10) { date = '0' + date }
if (hours < 10) { hours = '0' + hours }
if (minute < 10) { minute = '0' + minute }
if (second < 10) { second = '0' + second }
return year + '-' + month + '-' + date + ' ' + hours + ':' + minute + ':' + second
},
// 检测手机号是否正确
checkModbile(mobile) {
var re = /^1[3,4,5,6,7,8,9][0-9]{9}$/;
var result = re.test(mobile);
if(!result) {
return false;//若手机号码格式不正确则返回false
}
return true;
},
// 电话号码做隐私处理
hiddenString(str){
return str.replace(/(\d{3})\d{4}(\d{4})/, "$1****$2");
},
// 封装数组对象排序方法
compare(prop) {
return function(a, b) {
return b[prop] - a[prop] // 降序
// return a[prop] - b[prop] // 升序
}
},
// 长按复制
onLongPress(text) {
uni.setClipboardData({
data: this.text,
success: function () {
uni.showToast({
title: '复制成功'
})
},
})
},
}