loading

Loading

首页 TronLink钱包

使用PHP+CSS+JS+HTML5+JSON构建TronLink钱包(无MySQL)

字数: (12434)
阅读: (3)
0

使用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


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


    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钱包下载