TronLink钱包Web实现(PHP+CSS+JS+HTML5+JSON)
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>©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钱包Web实现(PHP+CSS+JS+HTML5+JSON)
文章链接:https://tianjinfa.org/post/3098
本站所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议,转载请注明来自 !
文章标题:TronLink钱包Web实现(PHP+CSS+JS+HTML5+JSON)
文章链接:https://tianjinfa.org/post/3098
本站所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议,转载请注明来自 !
打赏
如果觉得文章对您有用,请随意打赏。
您的支持是我们继续创作的动力!
微信扫一扫
支付宝扫一扫
您可能对以下文章感兴趣
-
TronLink钱包集成开发指南
9小时前
-
使用Go语言构建TronLink钱包:完整源码与实现指南
10小时前
-
TronLink钱包Web版实现(无MySQL)
10小时前
-
TronLink钱包网页版实现(PHP+CSS+JS+HTML5+JSON)
8小时前
-
TronLink钱包集成开发指南:PHP+CSS+JS+HTML5实现
8小时前
-
TronLink钱包集成开发指南:PHP+CSS+JS+HTML5实现
8小时前
-
使用Go语言构建TronLink钱包:完整源码与实现指南
9小时前
-
原创TronLink钱包HTML5实现方案-SEO优化版
9小时前
-
TronLink钱包集成开发指南:使用PHP+CSS+JS+HTML5+JSON实现
9小时前
-
TronLink钱包集成开发指南
9小时前