loading

Loading

首页 TronLink官网

TronLink钱包Web实现(PHP+CSS+JS+HTML5+JSON)

字数: (12147)
阅读: (1)
0

TronLink钱包Web实现(PHP+CSS+JS+HTML5+JSON)

本文将介绍如何不使用MySQL数据库,仅使用PHP、CSS、JavaScript、HTML5和JSON技术栈实现一个简单的TronLink钱包Web界面。这个实现包含了基本的钱包功能,如创建账户、显示余额和简单的交易功能。

一、项目概述

功能特点
1.纯前端钱包实现(使用TronWebJS库)
2.账户信息存储在浏览器的localStorage中
3.交易历史记录存储在JSON文件中
4.响应式设计,适配移动端和桌面端
5.SEO优化结构

技术栈
-前端:HTML5,CSS3,JavaScript(ES6+)
-后端:PHP(仅用于JSON文件处理)
-数据存储:JSON文件+localStorage

二、完整代码实现

1.目录结构

/tronlink-wallet/
├──index.php主页面
├──transactions.json交易记录存储
├──css/
│└──style.css样式表
└──js/
└──app.js主JavaScript文件

2.index.php(主HTML文件)

<!DOCTYPEhtml>
<htmllang="zh-CN">
<head>
<metacharset="UTF-8">
<metaname="viewport"content="width=device-width,initial-scale=1.0">
<metaname="description"content="轻量级TronLink钱包实现,无需安装扩展,直接在浏览器中使用TRON区块链">
<metaname="keywords"content="TronLink,TRON钱包,波场钱包,区块链钱包,加密货币">
<title>TronLinkWebWallet|轻量级TRON钱包</title>
<linkrel="stylesheet"href="css/style.css">
<scriptsrc="https://cdn.jsdelivr.net/npm/[email protected]/dist/TronWeb.js"></script>
</head>
<body>
<header>
<h1>TronLinkWebWallet</h1>
<nav>
<ul>
<li><ahref="dashboard">仪表盘</a></li>
<li><ahref="send">发送</a></li>
<li><ahref="receive">接收</a></li>
<li><ahref="transactions">交易记录</a></li>
</ul>
</nav>
</header>

<main>
<sectionid="dashboard"class="active">
<h2>账户概览</h2>
<divclass="account-info">
<divid="accountAddress">未连接钱包</div>
<divid="accountBalance">0TRX</div>
<buttonid="connectWallet">连接钱包</button>
<buttonid="createWallet"class="secondary">创建新钱包</button>
</div>
</section>

<sectionid="send">
<h2>发送TRX</h2>
<formid="sendForm">
<divclass="form-group">
<labelfor="recipient">接收地址</label>
<inputtype="text"id="recipient"placeholder="T..."required>
</div>
<divclass="form-group">
<labelfor="amount">金额(TRX)</label>
<inputtype="number"id="amount"step="0.000001"min="0.000001"required>
</div>
<buttontype="submit">发送</button>
</form>
</section>

<sectionid="receive">
<h2>接收TRX</h2>
<divclass="qr-code-container">
<canvasid="qrCode"></canvas>
</div>
<divid="receiveAddress">连接钱包后显示地址</div>
<buttonid="copyAddress">复制地址</button>
</section>

<sectionid="transactions">
<h2>交易记录</h2>
<divclass="transactions-list"id="transactionsList">
<p>加载中...</p>
</div>
</section>
</main>

<footer>
<p>&copy;2023TronLinkWebWallet.基于TRON区块链技术构建。</p>
</footer>

<scriptsrc="js/app.js"></script>
<scriptsrc="https://cdn.jsdelivr.net/npm/[email protected]/dist/qrious.min.js"></script>
</body>
</html>

3.css/style.css(样式表)

/基础样式/
:root{
--primary-color:2e86de;
--secondary-color:54a0ff;
--dark-color:222f3e;
--light-color:f5f6fa;
--success-color:26de81;
--danger-color:e74c3c;
}

{
margin:0;
padding:0;
box-sizing:border-box;
font-family:'SegoeUI',Tahoma,Geneva,Verdana,sans-serif;
}

body{
background-color:var(--light-color);
color:var(--dark-color);
line-height:1.6;
}

/布局样式/
header{
background-color:var(--primary-color);
color:white;
padding:1rem;
text-align:center;
}

navul{
display:flex;
justify-content:center;
list-style:none;
margin-top:1rem;
}

navulli{
margin:01rem;
}

navullia{
color:white;
text-decoration:none;
font-weight:bold;
padding:0.5rem1rem;
border-radius:4px;
transition:background-color0.3s;
}

navullia:hover{
background-color:var(--secondary-color);
}

main{
max-width:1200px;
margin:2remauto;
padding:01rem;
}

section{
display:none;
padding:1rem;
background-color:white;
border-radius:8px;
box-shadow:02px10pxrgba(0,0,0,0.1);
margin-bottom:2rem;
}

section.active{
display:block;
}

/账户信息样式/
.account-info{
text-align:center;
padding:2rem;
}

accountAddress{
font-family:monospace;
margin:1rem0;
padding:1rem;
background-color:f0f0f0;
border-radius:4px;
word-break:break-all;
}

accountBalance{
font-size:2rem;
font-weight:bold;
margin:1rem0;
color:var(--primary-color);
}

/按钮样式/
button,.button{
background-color:var(--primary-color);
color:white;
border:none;
padding:0.75rem1.5rem;
border-radius:4px;
cursor:pointer;
font-size:1rem;
transition:background-color0.3s;
margin:0.5rem;
}

button:hover{
background-color:var(--secondary-color);
}

button.secondary{
background-color:95a5a6;
}

button.secondary:hover{
background-color:7f8c8d;
}

/表单样式/
.form-group{
margin-bottom:1rem;
}

label{
display:block;
margin-bottom:0.5rem;
font-weight:bold;
}

input{
width:100%;
padding:0.75rem;
border:1pxsolidddd;
border-radius:4px;
font-size:1rem;
}

/QR码样式/
.qr-code-container{
text-align:center;
margin:2rem0;
}

qrCode{
width:200px;
height:200px;
margin:0auto;
}

/交易记录样式/
.transactions-list{
max-height:500px;
overflow-y:auto;
}

.transaction-item{
padding:1rem;
border-bottom:1pxsolideee;
display:flex;
justify-content:space-between;
}

.transaction-item:last-child{
border-bottom:none;
}

.transaction-amount{
font-weight:bold;
}

.transaction-amount.incoming{
color:var(--success-color);
}

.transaction-amount.outgoing{
color:var(--danger-color);
}

/响应式设计/
@media(max-width:768px){
navul{
flex-direction:column;
align-items:center;
}

navulli{
margin:0.5rem0;
}

.account-info{
padding:1rem;
}
}

/通知样式/
.notification{
position:fixed;
top:1rem;
right:1rem;
padding:1rem;
background-color:var(--success-color);
color:white;
border-radius:4px;
box-shadow:02px10pxrgba(0,0,0,0.2);
transform:translateX(200%);
transition:transform0.3sease-in-out;
z-index:1000;
}

.notification.show{
transform:translateX(0);
}

.notification.error{
background-color:var(--danger-color);
}

4.js/app.js(主JavaScript文件)


//初始化TronWeb
lettronWeb;
letcurrentAccount=null;

//DOM元素
constsections=document.querySelectorAll('mainsection');
constnavLinks=document.querySelectorAll('navullia');
constconnectWalletBtn=document.getElementById('connectWallet');
constcreateWalletBtn=document.getElementById('createWallet');
constaccountAddressEl=document.getElementById('accountAddress');
constaccountBalanceEl=document.getElementById('accountBalance');
constsendForm=document.getElementById('sendForm');
constrecipientInput=document.getElementById('recipient');
constamountInput=document.getElementById('amount');
constreceiveAddressEl=document.getElementById('receiveAddress');
constcopyAddressBtn=document.getElementById('copyAddress');
consttransactionsList=document.getElementById('transactionsList');
constqrCodeCanvas=document.getElementById('qrCode');

//初始化应用
document.addEventListener('DOMContentLoaded',()=>{
//导航切换
navLinks.forEach(link=>{
link.addEventListener('click',(e)=>{
e.preventDefault();
consttargetId=link.getAttribute('href').substring(1);

//更新活动状态
navLinks.forEach(l=>l.parentElement.classList.remove('active'));
link.parentElement.classList.add('active');

//显示对应部分
sections.forEach(section=>section.classList.remove('active'));
document.getElementById(targetId).classList.add('active');
});
});

//检查localStorage中是否有钱包
checkSavedWallet();

//连接钱包按钮
connectWalletBtn.addEventListener('click',connectWallet);

//创建钱包按钮
createWalletBtn.addEventListener('click',createWallet);

//发送表单
sendForm.addEventListener('submit',sendTransaction);

//复制地址按钮
copyAddressBtn.addEventListener('click',copyAddress);
});

//检查本地存储的钱包
functioncheckSavedWallet(){
constsavedWallet=localStorage.getItem('tronWebWallet');
if(savedWallet){
try{
constwallet=JSON.parse(savedWallet);
initTronWeb(wallet.privateKey);
showNotification('找到已保存的钱包,请点击"连接钱包"');
}catch(e){
console.error('解析钱包数据失败:',e);
}
}
}

//初始化TronWeb
functioninitTronWeb(privateKey){
tronWeb=newTronWeb({
fullHost:'https://api.trongrid.io',
privateKey:privateKey
});

currentAccount=tronWeb.defaultAddress.base58;
}

//连接钱包
asyncfunctionconnectWallet(){
if(!tronWeb){
showNotification('请先创建或导入钱包','error');
return;
}

try{
//获取账户信息
constbalance=awaittronWeb.trx.getBalance();
constbalanceInTRX=tronWeb.fromSun(balance);

//更新UI
accountAddressEl.textContent=currentAccount;
accountBalanceEl.textContent=`${balanceInTRX}TRX`;
receiveAddressEl.textContent=currentAccount;

//生成QR码
generateQRCode(currentAccount);

//加载交易记录
loadTransactions();

showNotification('钱包连接成功');
}catch(error){
console.error('连接钱包失败:',error);
showNotification('连接钱包失败:'+error.message,'error');
}
}

//创建新钱包
functioncreateWallet(){
if(confirm('确定要创建新钱包吗?请确保安全保存私钥!')){
try{
constaccount=tronWeb.createAccount();

//保存到localStorage
constwalletData={
privateKey:account.privateKey,
address:account.address.base58
};
localStorage.setItem('tronWebWallet',JSON.stringify(walletData));

//初始化TronWeb
initTronWeb(account.privateKey);

//显示私钥给用户(实际应用中应该更安全地处理)
alert(`新钱包创建成功!\n\n地址:${account.address.base58}\n私钥:${account.privateKey}\n\n请务必保存好私钥!`);

showNotification('新钱包创建成功');
}catch(error){
console.error('创建钱包失败:',error);
showNotification('创建钱包失败:'+error.message,'error');
}
}
}

//发送交易
asyncfunctionsendTransaction(e){
e.preventDefault();

if(!tronWeb){
showNotification('请先连接钱包','error');
return;
}

constrecipient=recipientInput.value.trim();
constamount=parseFloat(amountInput.value);

if(!recipient||isNaN(amount){
showNotification('请输入有效的地址和金额','error');
return;
}

try{
//验证地址
if(!tronWeb.isAddress(recipient)){
showNotification('无效的TRON地址','error');
return;
}

//转换为sun(1TRX=1,000,000SUN)
constamountInSun=tronWeb.toSun(amount);

showNotification('交易处理中...');

//发送交易
consttx=awaittronWeb.trx.sendTransaction(recipient,amountInSun);

//保存交易记录
saveTransaction({
txId:tx.transaction.txID,
from:currentAccount,
to:recipient,
amount:amount,
timestamp:newDate().toISOString(),
status:'pending'
});

//更新余额
constbalance=awaittronWeb.trx.getBalance();
constbalanceInTRX=tronWeb.fromSun(balance);
accountBalanceEl.textContent=`${balanceInTRX}TRX`;

//重置表单
sendForm.reset();

showNotification('交易发送成功!交易ID:'+tx.transaction.txID);

//重新加载交易记录
loadTransactions();
}catch(error){
console.error('发送交易失败:',error);
showNotification('发送交易失败:'+error.message,'error');
}
}

//生成QR码
functiongenerateQRCode(address){
if(window.QRious){
newQRious({
element:qrCodeCanvas,
value:address,
size:200
});
}
}

//复制地址
functioncopyAddress(){
if(!currentAccount){
showNotification('请先连接钱包','error');
return;
}

navigator.clipboard.writeText(currentAccount)
.then(()=>showNotification('地址已复制到剪贴板'))
.catch(err=>{
console.error('复制失败:',err);
showNotification('复制失败','error');
});
}

//保存交易到JSON文件
functionsaveTransaction(transaction){
//首先从localStorage中获取交易记录
lettransactions=JSON.parse(localStorage.getItem('tronTransactions')||'[]');

//添加新交易
transactions.unshift(transaction);

//保存回localStorage
localStorage.setItem('tronTransactions',JSON.stringify(transactions));

//同时发送到服务器保存(PHP处理)
fetch(window.location.href,{
method:'POST',
headers:{
'Content-Type':'application/json',
},
body:JSON.stringify({
action:'save_transaction',
transaction:transaction
})
}).catch(error=>console.error('保存交易到服务器失败:',error));
}

//加载交易记录
asyncfunctionloadTransactions(){
try{
//首先尝试从服务器获取
constresponse=awaitfetch('transactions.json');
lettransactions=awaitresponse.json();

//合并本地交易记录
constlocalTransactions=JSON.parse(localStorage.getItem('tronTransactions')||'[]');
transactions=[...localTransactions,...transactions];

//过滤当前账户的交易
constaccountTransactions=transactions.filter(tx=>
tx.from===currentAccount||tx.to===currentAccount
);

//渲染交易列表
renderTransactions(accountTransactions);
}catch(error){
console.error('加载交易记录失败:',error);
//回退到仅显示本地交易
constlocalTransactions=JSON.parse(localStorage.getItem('tronTransactions')||'[]');
constaccountTransactions=localTransactions.filter(tx=>
tx.from===currentAccount||tx.to===currentAccount
);
renderTransactions(accountTransactions);
}
}

//渲染交易列表
functionrenderTransactions(transactions){
if(transactions.length===0){
transactionsList.innerHTML='<p>没有找到交易记录</p>';
return;
}

transactionsList.innerHTML='';

transactions.forEach(tx=>{
constisOutgoing=tx.from===currentAccount;
constotherAddress=isOutgoing?tx.to:tx

转载请注明出处: TronLink官网下载-TRON-TRX-波场-波比-波币-波宝|官网-钱包-苹果APP|安卓-APP-下载

本文的链接地址: https://tianjinfa.org/post/3098


扫描二维码,在手机上阅读


    TronLink TronLink 官网 TronLink 下载 TronLink 钱包 波场 TRON TRX 波币 波比 波宝 波场钱包 苹果 APP 下载 安卓 APP 下载 数字货币钱包 区块链钱包 去中心化钱包 数字资产管理 加密货币存储 波场生态 TRC-20 代币 TRC-10 代币 波场 DApp 波场智能合约 钱包安全 私钥管理 钱包备份 钱包恢复 多账户管理 代币转账 波场超级代表 波场节点 波场跨链 波场 DeFi 波场 NFT 波场测试网 波场开发者 钱包教程 新手入门 钱包使用指南 波场交易手续费 波场价格 波场行情 波场生态合作 波场应用 波场质押 波场挖矿 波场冷钱包 硬件钱包连接 波场钱包对比 波场钱包更新 波场链上数据 TronLink 官网下载 TronLink 安卓 APP TronLink 苹果 APP TRON 区块链 TRX 下载 TRX 交易 波场官方 波场钱包下载 波比钱包 波币官网 波宝钱包 APP 波宝钱包下载 波场 TRC20 代币 波场 TRC10 代币 波场 TRC721 代币 波场 DApp 浏览器 波场去中心化应用 TronLink 钱包安全 TronLink 钱包教程 TronLink 私钥管理 TronLink 多账户管理 TronLink 交易手续费 波场超级代表投票 波场去中心化存储 波场跨链交易 波场 DeFi 应用 波场 NFT 市场 波场质押挖矿 波场钱包备份 波场钱包恢复 波场硬件钱包连接 波场开发者工具 波场节点搭建 波场钱包使用指南 波场代币转账 波场钱包创建 波场钱包导入 波场 DApp 推荐 波场 TRX 价格走势 波场生态发展 TronLink 钱包更新 波场链上数据查询 波场钱包安全防护 波场钱包对比评测 TronLink钱包下载