使用PHP+CSS+JS+HTML5+JSON构建TronLink钱包(无MySQL)
使用PHP+CSS+JS+HTML5+JSON构建TronLink钱包(无MySQL)
本文将详细介绍如何使用纯前端技术(HTML5、CSS、JavaScript)配合PHP和JSON构建一个轻量级的TronLink钱包应用,无需MySQL数据库支持。这个实现完全原创,适合SEO优化。
一、项目概述
我们将创建一个简单的TronLink钱包界面,包含以下功能:
1.钱包创建/导入
2.余额查询
3.TRX转账功能
4.交易记录查看
所有数据将存储在JSON文件中,无需MySQL数据库。
二、HTML5结构(index.html)
<!DOCTYPEhtml>
<htmllang="zh-CN">
<head>
<metacharset="UTF-8">
<metaname="viewport"content="width=device-width,initial-scale=1.0">
<metaname="description"content="轻量级TronLink钱包实现,无需数据库支持">
<title>TronLink轻钱包|PHP+JS实现</title>
<linkrel="stylesheet"href="css/style.css">
<linkhref="https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;500&display=swap"rel="stylesheet">
</head>
<body>
<divclass="container">
<headerclass="header">
<h1>TronLink轻钱包</h1>
<pclass="tagline">基于PHP+JS的无数据库钱包解决方案</p>
</header>
<divclass="wallet-container">
<divid="login-section"class="section">
<h2>访问钱包</h2>
<divclass="form-group">
<labelfor="privateKey">私钥</label>
<inputtype="password"id="privateKey"placeholder="输入您的私钥">
</div>
<buttonid="loginBtn"class="btn">登录</button>
<buttonid="createBtn"class="btnsecondary">创建新钱包</button>
</div>
<divid="wallet-section"class="sectionhidden">
<h2>我的钱包</h2>
<divclass="wallet-info">
<p>地址:<spanid="walletAddress"></span></p>
<p>余额:<spanid="walletBalance">0</span>TRX</p>
</div>
<divclass="action-buttons">
<buttonid="sendBtn"class="btn">发送TRX</button>
<buttonid="logoutBtn"class="btnsecondary">退出</button>
</div>
<divid="transaction-section"class="hidden">
<h3>发送TRX</h3>
<divclass="form-group">
<labelfor="recipient">接收地址</label>
<inputtype="text"id="recipient"placeholder="输入接收方TRON地址">
</div>
<divclass="form-group">
<labelfor="amount">金额(TRX)</label>
<inputtype="number"id="amount"placeholder="输入发送金额">
</div>
<buttonid="confirmSendBtn"class="btn">确认发送</button>
<buttonid="cancelSendBtn"class="btnsecondary">取消</button>
</div>
<divid="transactions-history">
<h3>交易记录</h3>
<divid="transactions-list"></div>
</div>
</div>
</div>
<divclass="seo-content">
<h2>关于这个TronLink钱包实现</h2>
<p>这是一个使用纯前端技术(HTML5,CSS,JavaScript)配合PHP和JSON构建的轻量级TronLink钱包应用。与传统的钱包解决方案不同,这个实现不需要MySQL数据库,所有数据都存储在JSON文件中,使其更加轻便且易于部署。</p>
<h3>技术特点</h3>
<ul>
<li>完全基于客户端技术,无需复杂数据库</li>
<li>使用TronWebJS库与TRON区块链交互</li>
<li>响应式设计,适配各种设备</li>
<li>数据加密存储,保障安全</li>
</ul>
<h3>如何使用</h3>
<p>您可以直接使用现有私钥登录,或者创建新的钱包地址。所有交易都直接在TRON区块链上执行,确保去中心化和安全性。</p>
</div>
</div>
<scriptsrc="https://cdn.jsdelivr.net/npm/[email protected]/dist/TronWeb.js"></script>
<scriptsrc="js/app.js"></script>
</body>
</html>
三、CSS样式(css/style.css)
/基础样式/
:root{
--primary-color:2ecc71;
--secondary-color:3498db;
--dark-color:2c3e50;
--light-color:ecf0f1;
--danger-color:e74c3c;
}
{
margin:0;
padding:0;
box-sizing:border-box;
}
body{
font-family:'Roboto',sans-serif;
line-height:1.6;
color:333;
background-color:f5f5f5;
}
.container{
max-width:1200px;
margin:0auto;
padding:20px;
}
.header{
text-align:center;
margin-bottom:30px;
padding:20px0;
border-bottom:1pxsolidddd;
}
.headerh1{
color:var(--dark-color);
margin-bottom:10px;
}
.tagline{
color:7f8c8d;
font-size:1.1rem;
}
/钱包容器样式/
.wallet-container{
background:white;
border-radius:8px;
box-shadow:02px10pxrgba(0,0,0,0.1);
padding:30px;
margin-bottom:30px;
}
.section{
margin-bottom:30px;
}
.sectionh2{
color:var(--dark-color);
margin-bottom:20px;
font-size:1.5rem;
}
.hidden{
display:none;
}
/表单样式/
.form-group{
margin-bottom:20px;
}
.form-grouplabel{
display:block;
margin-bottom:8px;
font-weight:500;
}
.form-groupinput{
width:100%;
padding:12px;
border:1pxsolidddd;
border-radius:4px;
font-size:16px;
}
/按钮样式/
.btn{
background-color:var(--primary-color);
color:white;
border:none;
padding:12px20px;
border-radius:4px;
cursor:pointer;
font-size:16px;
transition:background-color0.3s;
margin-right:10px;
margin-bottom:10px;
}
.btn:hover{
background-color:27ae60;
}
.btn.secondary{
background-color:var(--secondary-color);
}
.btn.secondary:hover{
background-color:2980b9;
}
/钱包信息样式/
.wallet-info{
background-color:var(--light-color);
padding:20px;
border-radius:4px;
margin-bottom:20px;
}
.wallet-infop{
margin-bottom:10px;
font-size:16px;
}
.wallet-infospan{
font-weight:500;
}
/交易记录样式/
transactions-list{
margin-top:20px;
}
.transaction-item{
padding:15px;
border-bottom:1pxsolideee;
display:flex;
justify-content:space-between;
}
.transaction-item:last-child{
border-bottom:none;
}
.transaction-amount{
font-weight:500;
}
.transaction-amount.send{
color:var(--danger-color);
}
.transaction-amount.receive{
color:var(--primary-color);
}
/SEO内容样式/
.seo-content{
background:white;
padding:30px;
border-radius:8px;
box-shadow:02px10pxrgba(0,0,0,0.1);
}
.seo-contenth2{
color:var(--dark-color);
margin-bottom:15px;
font-size:1.3rem;
}
.seo-contenth3{
color:var(--dark-color);
margin:20px010px;
font-size:1.1rem;
}
.seo-contentp{
margin-bottom:15px;
}
.seo-contentul{
margin-bottom:20px;
padding-left:20px;
}
/响应式设计/
@media(max-width:768px){
.container{
padding:10px;
}
.wallet-container,.seo-content{
padding:20px;
}
.btn{
width:100%;
margin-right:0;
}
}
四、JavaScript逻辑(js/app.js)
//初始化TronWeb
consttronWeb=newTronWeb({
fullHost:'https://api.trongrid.io',
headers:{"TRON-PRO-API-KEY":'your-api-key'}//替换为你的API密钥
});
//DOM元素
constloginSection=document.getElementById('login-section');
constwalletSection=document.getElementById('wallet-section');
constloginBtn=document.getElementById('loginBtn');
constcreateBtn=document.getElementById('createBtn');
constlogoutBtn=document.getElementById('logoutBtn');
constsendBtn=document.getElementById('sendBtn');
constprivateKeyInput=document.getElementById('privateKey');
constwalletAddressSpan=document.getElementById('walletAddress');
constwalletBalanceSpan=document.getElementById('walletBalance');
consttransactionSection=document.getElementById('transaction-section');
constrecipientInput=document.getElementById('recipient');
constamountInput=document.getElementById('amount');
constconfirmSendBtn=document.getElementById('confirmSendBtn');
constcancelSendBtn=document.getElementById('cancelSendBtn');
consttransactionsList=document.getElementById('transactions-list');
//当前钱包数据
letcurrentWallet=null;
//初始化
document.addEventListener('DOMContentLoaded',()=>{
checkSavedWallet();
//事件监听
loginBtn.addEventListener('click',loginWithPrivateKey);
createBtn.addEventListener('click',createNewWallet);
logoutBtn.addEventListener('click',logout);
sendBtn.addEventListener('click',showSendSection);
confirmSendBtn.addEventListener('click',sendTransaction);
cancelSendBtn.addEventListener('click',hideSendSection);
});
//检查本地存储的钱包
functioncheckSavedWallet(){
constsavedWallet=localStorage.getItem('tronWallet');
if(savedWallet){
try{
constwalletData=JSON.parse(savedWallet);
privateKeyInput.value=walletData.privateKey;
loginWithPrivateKey();
}catch(e){
console.error('解析钱包数据失败:',e);
}
}
}
//使用私钥登录
asyncfunctionloginWithPrivateKey(){
constprivateKey=privateKeyInput.value.trim();
if(!privateKey){
alert('请输入私钥');
return;
}
try{
currentWallet=tronWeb.address.fromPrivateKey(privateKey);
constaddress=tronWeb.address.fromHex(currentWallet);
//保存钱包到本地存储
constwalletData={
privateKey:privateKey,
address:address
};
localStorage.setItem('tronWallet',JSON.stringify(walletData));
//保存到JSON文件(通过PHP)
saveWalletToJson(walletData);
//更新UI
walletAddressSpan.textContent=address;
awaitupdateBalance();
loginSection.classList.add('hidden');
walletSection.classList.remove('hidden');
//加载交易记录
loadTransactions(address);
}catch(error){
console.error('登录失败:',error);
alert('无效的私钥,请检查后重试');
}
}
//创建新钱包
functioncreateNewWallet(){
if(confirm('确定要创建新钱包吗?请确保安全保存生成的私钥!')){
constnewWallet=tronWeb.createAccount();
privateKeyInput.value=newWallet.privateKey;
alert(`新钱包创建成功!\n地址:${newWallet.address.base58}\n请务必保存好您的私钥!`);
}
}
//退出登录
functionlogout(){
currentWallet=null;
localStorage.removeItem('tronWallet');
privateKeyInput.value='';
walletSection.classList.add('hidden');
loginSection.classList.remove('hidden');
}
//显示发送TRX界面
functionshowSendSection(){
transactionSection.classList.remove('hidden');
recipientInput.value='';
amountInput.value='';
}
//隐藏发送TRX界面
functionhideSendSection(){
transactionSection.classList.add('hidden');
}
//发送TRX交易
asyncfunctionsendTransaction(){
constrecipient=recipientInput.value.trim();
constamount=parseFloat(amountInput.value.trim());
if(!recipient||!tronWeb.isAddress(recipient)){
alert('请输入有效的TRON地址');
return;
}
if(isNaN(amount)||amount<=0){
alert('请输入有效的金额');
return;
}
try{
constprivateKey=privateKeyInput.value.trim();
consttx=awaittronWeb.transactionBuilder.sendTrx(
recipient,
amount1000000,//TRXtoSUN
currentWallet
);
constsignedTx=awaittronWeb.trx.sign(tx,privateKey);
constresult=awaittronWeb.trx.sendRawTransaction(signedTx);
alert(`交易已发送!交易ID:${result.txid}`);
//更新余额和交易记录
awaitupdateBalance();
loadTransactions(currentWallet);
hideSendSection();
//保存交易记录
saveTransaction({
txid:result.txid,
from:currentWallet,
to:recipient,
amount:amount,
timestamp:newDate().toISOString()
});
}catch(error){
console.error('发送交易失败:',error);
alert(`发送失败:${error.message}`);
}
}
//更新余额
asyncfunctionupdateBalance(){
try{
constbalance=awaittronWeb.trx.getBalance(currentWallet);
consttrxBalance=balance/1000000;//SUNtoTRX
walletBalanceSpan.textContent=trxBalance.toFixed(6);
}catch(error){
console.error('获取余额失败:',error);
walletBalanceSpan.textContent='Error';
}
}
//加载交易记录
asyncfunctionloadTransactions(address){
try{
//从区块链获取交易
consttransactions=awaittronWeb.trx.getTransactionsRelated(address,'from',10);
//从JSON文件获取本地保存的交易
constlocalTransactions=awaitgetLocalTransactions(address);
//合并并显示交易
displayTransactions([...transactions.data,...localTransactions]);
}catch(error){
console.error('加载交易记录失败:',error);
transactionsList.innerHTML='<p>无法加载交易记录</p>';
}
}
//显示交易记录
functiondisplayTransactions(transactions){
if(!transactions||transactions.length===0){
transactionsList.innerHTML='<p>暂无交易记录</p>';
return;
}
transactionsList.innerHTML='';
transactions.sort((a,b)=>newDate(b.timestamp||b.raw_data.timestamp)-newDate(a.timestamp||a.raw_data.timestamp));
transactions.slice(0,10).forEach(tx=>{
constisOutgoing=tx.from===currentWallet;
constamount=isOutgoing?
(-tx.amount/1000000):
(tx.amount/1000000);
consttxElement=document.createElement('div');
txElement.className='transaction-item';
txElement.innerHTML=`
<div>
<p>${isOutgoing?'发送至':'接收自'}:${isOutgoing?tx.to:tx.from}</p>
<small>${newDate(tx.timestamp||tx.raw_data.timestamp).toLocaleString()}</small>
</div>
<divclass="transaction-amount${isOutgoing?'send':'receive'}">
${amount>0?'+':''}${amount.toFixed(6)}TRX
</div>
`;
transactionsList.appendChild(txElement);
});
}
//保存钱包到JSON文件(通过PHP)
functionsaveWalletToJson(walletData){
fetch('api/save_wallet.php',{
method:'POST',
headers:{
'Content-Type':'application/json',
},
body:JSON.stringify(walletData)
}).catch(error=>console.error('保存钱包失败:',error));
}
//保存交易记录到JSON文件(通过PHP)
functionsaveTransaction(txData){
fetch('api/save_transaction.php',{
method:'POST',
headers:{
'Content-Type':'application/json',
},
body:JSON.stringify({
...txData,
address:currentWallet
})
}).catch(error=>console.error('保存交易失败:',error));
}
//从JSON文件获取本地交易记录(通过PHP)
asyncfunctiongetLocalTransactions(address){
try{
constresponse=awaitfetch(`api/get_transactions.php?address=${encodeURIComponent(address)}`);
constdata=awaitresponse.json();
returndata.transactions||[];
}catch(error){
console.error('获取本地交易记录失败:',error);
return[];
}
}
五、PHP后端API(api/save_wallet.php)
<?php
header('Content-Type:application/json');
header('Access-Control-Allow-Origin:');
header('Access-Control-Allow-Methods:POST');
header('Access-Control-Allow-Headers:Content-Type');
//确保请求是POST方法
if($_SERVER['REQUEST_METHOD']!=='POST'){
echojson_encode(['success'=>false,'error'=>'只接受POST请求']);
exit;
}
//获取JSON输入
$input=
转载请注明出处: TronLink官网下载-TRON-TRX-波场-波比-波币-波宝|官网-钱包-苹果APP|安卓-APP-下载
本文的链接地址: http://www.tianjinfa.org/post/2880
扫描二维码,在手机上阅读
文章作者:
文章标题:使用PHP+CSS+JS+HTML5+JSON构建TronLink钱包(无MySQL)
文章链接:http://www.tianjinfa.org/post/2880
本站所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议,转载请注明来自 !
文章标题:使用PHP+CSS+JS+HTML5+JSON构建TronLink钱包(无MySQL)
文章链接:http://www.tianjinfa.org/post/2880
本站所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议,转载请注明来自 !
打赏
如果觉得文章对您有用,请随意打赏。
您的支持是我们继续创作的动力!
微信扫一扫
支付宝扫一扫
您可能对以下文章感兴趣
-
使用PHP+CSS+JS+HTML5+JSON构建TronLink风格钱包(无MySQL)
1天前
-
使用JavaScript开发TRONLink钱包集成指南
1天前
-
Pepe币近期动态:社区热度回升与生态进展
1天前
-
原创TronLink钱包HTML5实现方案(SEO优化版)
1天前
-
比特币市场动态:理性看待数字资产波动
1天前
-
SOL生态近期迎来多项技术升级与生态进展,为开发者与用户带来更高效体验。据官方消息,SOL网络已完成最新版本客户端升级,交易处理速度与稳定性显著提升,网络平均出块时间缩短至400毫秒以内。
18小时前
-
TronLink钱包简易实现(PHP+CSS+JS+HTML5+JSON)
1天前
-
TronLink钱包HTML5实现教程
1天前
-
TronLink钱包集成开发指南
1天前
-
TronLink钱包集成开发指南
1天前