TronLink钱包网页版实现(无数据库版)
TronLink钱包网页版实现(无数据库版)
本文将介绍如何使用纯前端技术(HTML5+CSS+JS)结合PHP和JSON实现一个简易的TronLink钱包网页版,无需MySQL数据库支持。这个实现完全原创,适合SEO优化。
功能概述
1.钱包创建/导入
2.余额查询
3.TRX转账功能
4.交易记录查看
5.本地数据存储(使用JSON文件)
实现原理
-前端使用HTML5+CSS3+JavaScript构建用户界面
-PHP处理后端逻辑和JSON文件操作
-使用TronWeb.js与TRON区块链交互
-数据存储在本地JSON文件中而非MySQL
完整代码实现
1.项目结构
/tronlink-wallet/
├──index.php主页面
├──assets/
│├──css/
││└──style.css样式文件
│├──js/
││└──app.js主JS文件
│└──img/图片资源
├──data/
│└──wallets.json钱包数据存储
├──api/
│├──create.php创建钱包API
│├──balance.php余额查询API
│└──transfer.php转账API
└──includes/
└──functions.php公共函数
2.HTML部分(index.php)
<!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>TronLink网页版钱包|安全便捷的TRON区块链钱包</title>
<linkrel="stylesheet"href="assets/css/style.css">
<scriptsrc="https://cdn.jsdelivr.net/npm/[email protected]/dist/TronWeb.js"></script>
</head>
<body>
<header>
<divclass="container">
<h1>TronLink网页版钱包</h1>
<nav>
<ul>
<li><ahref="home">首页</a></li>
<li><ahref="create">创建钱包</a></li>
<li><ahref="transfer">转账</a></li>
<li><ahref="transactions">交易记录</a></li>
</ul>
</nav>
</div>
</header>
<mainclass="container">
<sectionid="wallet-info">
<h2>钱包信息</h2>
<divid="wallet-status"class="alert">
<p>请先创建或导入钱包</p>
</div>
<divclass="wallet-details">
<p>地址:<spanid="wallet-address">未加载</span></p>
<p>余额:<spanid="wallet-balance">0TRX</span></p>
</div>
<buttonid="refresh-balance"class="btn">刷新余额</button>
</section>
<sectionid="create-wallet">
<h2>创建/导入钱包</h2>
<formid="wallet-form">
<divclass="form-group">
<labelfor="private-key">私钥(可选):</label>
<inputtype="password"id="private-key"placeholder="留空将创建新钱包">
</div>
<divclass="form-group">
<labelfor="wallet-name">钱包名称:</label>
<inputtype="text"id="wallet-name"required>
</div>
<divclass="form-group">
<labelfor="wallet-password">密码:</label>
<inputtype="password"id="wallet-password"required>
</div>
<buttontype="submit"class="btn">创建/导入钱包</button>
</form>
</section>
<sectionid="transfer-section">
<h2>转账TRX</h2>
<formid="transfer-form">
<divclass="form-group">
<labelfor="to-address">接收地址:</label>
<inputtype="text"id="to-address"required>
</div>
<divclass="form-group">
<labelfor="amount">金额(TRX):</label>
<inputtype="number"id="amount"step="0.000001"required>
</div>
<divclass="form-group">
<labelfor="transfer-password">密码:</label>
<inputtype="password"id="transfer-password"required>
</div>
<buttontype="submit"class="btn">转账</button>
</form>
</section>
<sectionid="transactions-section">
<h2>交易记录</h2>
<divid="transactions-list">
<p>暂无交易记录</p>
</div>
</section>
</main>
<footer>
<divclass="container">
<p>©2023TronLink网页版钱包.保留所有权利.</p>
</div>
</footer>
<scriptsrc="assets/js/app.js"></script>
</body>
</html>
3.CSS样式(assets/css/style.css)
/基础样式/
body{
font-family:'Arial',sans-serif;
line-height:1.6;
margin:0;
padding:0;
color:333;
background-color:f5f5f5;
}
.container{
width:90%;
max-width:1200px;
margin:0auto;
padding:20px;
}
/头部样式/
header{
background-color:1c1e26;
color:white;
padding:20px0;
box-shadow:02px5pxrgba(0,0,0,0.1);
}
headerh1{
margin:0;
font-size:24px;
}
navul{
list-style:none;
padding:0;
margin:15px00;
}
navulli{
display:inline;
margin-right:20px;
}
navullia{
color:white;
text-decoration:none;
}
/主内容区/
main{
padding:30px0;
}
section{
background:white;
border-radius:5px;
padding:20px;
margin-bottom:30px;
box-shadow:02px5pxrgba(0,0,0,0.1);
}
h2{
color:1c1e26;
margin-top:0;
border-bottom:1pxsolideee;
padding-bottom:10px;
}
/表单样式/
.form-group{
margin-bottom:15px;
}
label{
display:block;
margin-bottom:5px;
font-weight:bold;
}
input[type="text"],
input[type="password"],
input[type="number"]{
width:100%;
padding:10px;
border:1pxsolidddd;
border-radius:4px;
box-sizing:border-box;
}
.btn{
background-color:1c1e26;
color:white;
border:none;
padding:10px15px;
border-radius:4px;
cursor:pointer;
font-size:16px;
}
.btn:hover{
background-color:2a2d3a;
}
/钱包信息/
.wallet-details{
background:f9f9f9;
padding:15px;
border-radius:4px;
margin:15px0;
}
.alert{
padding:10px15px;
border-radius:4px;
margin-bottom:15px;
}
.alertp{
margin:0;
}
/交易记录/
transactions-list{
max-height:300px;
overflow-y:auto;
}
.transaction-item{
padding:10px;
border-bottom:1pxsolideee;
}
.transaction-item:last-child{
border-bottom:none;
}
/响应式设计/
@media(max-width:768px){
navulli{
display:block;
margin:10px0;
}
}
4.JavaScript部分(assets/js/app.js)
document.addEventListener('DOMContentLoaded',function(){
//初始化TronWeb
consttronWeb=newTronWeb({
fullHost:'https://api.trongrid.io',
headers:{"TRON-PRO-API-KEY":"your-api-key"}
});
//DOM元素
constwalletForm=document.getElementById('wallet-form');
consttransferForm=document.getElementById('transfer-form');
constrefreshBalanceBtn=document.getElementById('refresh-balance');
constwalletAddressSpan=document.getElementById('wallet-address');
constwalletBalanceSpan=document.getElementById('wallet-balance');
constwalletStatusDiv=document.getElementById('wallet-status');
consttransactionsList=document.getElementById('transactions-list');
//当前钱包数据
letcurrentWallet=null;
//检查本地存储中是否有钱包
checkLocalWallet();
//创建/导入钱包表单提交
walletForm.addEventListener('submit',function(e){
e.preventDefault();
constprivateKey=document.getElementById('private-key').value;
constname=document.getElementById('wallet-name').value;
constpassword=document.getElementById('wallet-password').value;
createOrImportWallet(privateKey,name,password);
});
//转账表单提交
transferForm.addEventListener('submit',function(e){
e.preventDefault();
consttoAddress=document.getElementById('to-address').value;
constamount=document.getElementById('amount').value;
constpassword=document.getElementById('transfer-password').value;
sendTransaction(toAddress,amount,password);
});
//刷新余额按钮
refreshBalanceBtn.addEventListener('click',function(){
if(currentWallet){
getBalance(currentWallet.address);
}else{
showAlert('请先加载钱包','error');
}
});
//创建或导入钱包
asyncfunctioncreateOrImportWallet(privateKey,name,password){
try{
letaddress,pk;
if(privateKey){
//导入钱包
constaccount=awaittronWeb.trx.getAccount(privateKey);
if(!account)thrownewError('无效的私钥');
address=tronWeb.address.fromPrivateKey(privateKey);
pk=privateKey;
}else{
//创建新钱包
constaccount=awaittronWeb.createAccount();
address=account.address.base58;
pk=account.privateKey;
}
//简单加密私钥(实际应用中应使用更安全的加密方式)
constencryptedPK=btoa(pk+'|'+password);
//创建钱包对象
constwallet={
name:name,
address:address,
encryptedPK:encryptedPK,
createdAt:newDate().toISOString(),
transactions:[]
};
//保存到服务器
constresponse=awaitfetch('api/create.php',{
method:'POST',
headers:{
'Content-Type':'application/json'
},
body:JSON.stringify(wallet)
});
constresult=awaitresponse.json();
if(result.success){
currentWallet=wallet;
updateWalletUI();
getBalance(address);
showAlert('钱包已成功加载','success');
}else{
thrownewError(result.message||'保存钱包失败');
}
}catch(error){
console.error('钱包操作错误:',error);
showAlert('错误:'+error.message,'error');
}
}
//发送交易
asyncfunctionsendTransaction(toAddress,amount,password){
if(!currentWallet){
showAlert('请先加载钱包','error');
return;
}
try{
//解密私钥
constdecrypted=atob(currentWallet.encryptedPK).split('|');
if(decrypted[1]!==password){
thrownewError('密码不正确');
}
constprivateKey=decrypted[0];
//验证接收地址
if(!tronWeb.isAddress(toAddress)){
thrownewError('无效的接收地址');
}
//验证金额
constamountInSun=tronWeb.toSun(amount);
if(isNaN(amountInSun){
thrownewError('无效的金额');
}
//创建交易
consttransaction=awaittronWeb.transactionBuilder.sendTrx(
toAddress,
amountInSun,
currentWallet.address
);
constsignedTx=awaittronWeb.trx.sign(transaction,privateKey);
constresult=awaittronWeb.trx.sendRawTransaction(signedTx);
//更新交易记录
consttxRecord={
txId:result.txid,
to:toAddress,
amount:amount,
timestamp:newDate().toISOString(),
status:'pending'
};
currentWallet.transactions.unshift(txRecord);
//更新服务器数据
awaitupdateWalletData();
//更新UI
updateTransactionsUI();
showAlert('交易已发送,等待确认','success');
//5秒后刷新余额
setTimeout(()=>getBalance(currentWallet.address),5000);
}catch(error){
console.error('交易错误:',error);
showAlert('交易失败:'+error.message,'error');
}
}
//获取余额
asyncfunctiongetBalance(address){
try{
constbalance=awaittronWeb.trx.getBalance(address);
constbalanceInTRX=tronWeb.fromSun(balance);
walletBalanceSpan.textContent=`${balanceInTRX}TRX`;
returnbalanceInTRX;
}catch(error){
console.error('获取余额错误:',error);
showAlert('获取余额失败','error');
return0;
}
}
//检查本地钱包
asyncfunctioncheckLocalWallet(){
try{
constresponse=awaitfetch('api/balance.php');
constdata=awaitresponse.json();
if(data.success&&data.wallet){
currentWallet=data.wallet;
updateWalletUI();
getBalance(currentWallet.address);
showAlert('钱包已加载','success');
}
}catch(error){
console.error('检查本地钱包错误:',error);
}
}
//更新钱包数据到服务器
asyncfunctionupdateWalletData(){
if(!currentWallet)return;
try{
constresponse=awaitfetch('api/create.php',{
method:'POST',
headers:{
'Content-Type':'application/json'
},
body:JSON.stringify(currentWallet)
});
constresult=awaitresponse.json();
if(!result.success){
console.error('更新钱包数据失败:',result.message);
}
}catch(error){
console.error('更新钱包数据错误:',error);
}
}
//更新钱包UI
functionupdateWalletUI(){
if(currentWallet){
walletAddressSpan.textContent=currentWallet.address;
walletStatusDiv.innerHTML=`<p>钱包:${currentWallet.name}</p>`;
walletStatusDiv.className='alertsuccess';
updateTransactionsUI();
}
}
//更新交易记录UI
functionupdateTransactionsUI(){
if(!currentWallet||currentWallet.transactions.length===0){
transactionsList.innerHTML='<p>暂无交易记录</p>';
return;
}
lethtml='';
currentWallet.transactions.forEach(tx=>{
html+=`
<divclass="transaction-item">
<p><strong>交易ID:</strong>${tx.txId.substring(0,16)}...</p>
<p><strong>接收地址:</strong>${tx.to.substring(0,12)}...</p>
<p><strong>金额:</strong>${tx.amount}TRX</p>
<p><strong>时间:</strong>${newDate(tx.timestamp).toLocaleString()}</p>
<p><strong>状态:</strong>${tx.status}</p>
</div>
`;
});
transactionsList.innerHTML=html;
}
//显示提示信息
functionshowAlert(message,type){
walletStatusDiv.innerHTML=`<p>${message}</p>`;
walletStatusDiv.className=`alert${type}`;
}
});
5.PHP后端部分
includes/functions.php
<?php
//公共函数文件
//获取钱包数据文件路径
functiongetWalletDataPath(){
return__DIR__.'/../data/wallets.json';
}
//读取所有钱包数据
functionreadAllWallets(){
$file=getWalletDataPath();
if(!file_exists($file)){
file_put_contents($file,json_encode([]));
return[];
}
$content=file_get_contents($file);
returnjson_decode($content,true)?:[];
}
//保存钱包数据
functionsaveWalletData($wallets){
$file=getWalletDataPath();
file_put_contents($file,json_encode($wallets,JSON_PRETTY_PRINT));
}
//验证输入数据
functionvalidateInput($data,$fields){
foreach($fieldsas$field){
if(empty($data[$field])){
returnfalse;
}
}
returntrue;
}
//发送JSON响应
functionsendJsonResponse($success,$message='',$data=[]){
header('Content-Type:application/json');
echojson_encode([
'success'=>$success,
'message'=>$message,
'data'=>$data
]);
exit;
}
api/create.php
<?php
转载请注明出处: TronLink官网下载-TRON-TRX-波场-波比-波币-波宝|官网-钱包-苹果APP|安卓-APP-下载
本文的链接地址: https://tianjinfa.org/post/3045
扫描二维码,在手机上阅读
文章作者:
文章标题:TronLink钱包网页版实现(无数据库版)
文章链接:https://tianjinfa.org/post/3045
本站所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议,转载请注明来自 !
文章标题:TronLink钱包网页版实现(无数据库版)
文章链接:https://tianjinfa.org/post/3045
本站所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议,转载请注明来自 !
打赏
如果觉得文章对您有用,请随意打赏。
您的支持是我们继续创作的动力!
微信扫一扫
支付宝扫一扫
您可能对以下文章感兴趣
-
TronLink钱包集成开发指南:PHP+CSS+JS+HTML5实现
8小时前
-
你好!😊你想聊些什么呢?有什么我可以帮你的吗?
9小时前
-
TronLink钱包集成开发指南-原创PHP实现
7小时前
-
TronLink钱包HTML5实现教程-原创代码与SEO优化指南
8小时前
-
TronLink钱包集成指南:使用JavaScript连接TRON区块链
9小时前
-
使用JavaScript开发TronLink钱包集成指南
11小时前
-
使用PHP+CSS+JS+HTML5+JSON创建TronLink风格钱包(无MySQL)
6小时前
-
你好!😊有什么我可以帮助你的吗?无论是问题解答、学习建议,还是闲聊放松,我都在这儿呢!✨
6小时前
-
TronLink钱包网页版实现(无MySQL版)
7小时前
-
TronLink钱包HTML5实现教程
7小时前