feat: 提交模板代码
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"usingComponents": {}
|
||||
}
|
||||
@@ -0,0 +1,133 @@
|
||||
/* pages/index.wxss */
|
||||
.container {
|
||||
padding: 40rpx;
|
||||
background-color: #f5f5f5;
|
||||
min-height: 100vh;
|
||||
}
|
||||
|
||||
.header {
|
||||
text-align: center;
|
||||
margin-bottom: 40rpx;
|
||||
}
|
||||
|
||||
.title {
|
||||
font-size: 36rpx;
|
||||
font-weight: bold;
|
||||
color: #333;
|
||||
}
|
||||
|
||||
.section {
|
||||
width: 100%;
|
||||
background-color: #fff;
|
||||
border-radius: 16rpx;
|
||||
padding: 30rpx;
|
||||
margin-bottom: 30rpx;
|
||||
box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.1);
|
||||
}
|
||||
|
||||
.section-title {
|
||||
font-size: 32rpx;
|
||||
font-weight: 600;
|
||||
color: #333;
|
||||
margin-bottom: 24rpx;
|
||||
padding-bottom: 16rpx;
|
||||
border-bottom: 2rpx solid #eee;
|
||||
}
|
||||
|
||||
.input-textarea {
|
||||
width: 100%;
|
||||
min-height: 200rpx;
|
||||
padding: 20rpx;
|
||||
border: 2rpx solid #e0e0e0;
|
||||
border-radius: 8rpx;
|
||||
font-size: 28rpx;
|
||||
background-color: #fafafa;
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
.action-bar {
|
||||
display: flex;
|
||||
gap: 20rpx;
|
||||
margin-bottom: 24rpx;
|
||||
}
|
||||
|
||||
.btn {
|
||||
flex: 1;
|
||||
border-radius: 8rpx;
|
||||
font-size: 28rpx;
|
||||
border: none;
|
||||
}
|
||||
|
||||
.btn-primary {
|
||||
background-color: #07c160;
|
||||
color: #fff;
|
||||
}
|
||||
|
||||
.btn-primary:active {
|
||||
background-color: #06ad56;
|
||||
}
|
||||
|
||||
.btn-secondary {
|
||||
background-color: #576b95;
|
||||
color: #fff;
|
||||
}
|
||||
|
||||
.btn-secondary:active {
|
||||
background-color: #4a5d84;
|
||||
}
|
||||
|
||||
.result-box {
|
||||
margin-bottom: 24rpx;
|
||||
padding: 20rpx;
|
||||
background-color: #fafafa;
|
||||
border-radius: 8rpx;
|
||||
border: 2rpx solid #e0e0e0;
|
||||
}
|
||||
|
||||
.result-label {
|
||||
font-size: 24rpx;
|
||||
color: #666;
|
||||
margin-bottom: 12rpx;
|
||||
}
|
||||
|
||||
.result-content {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
gap: 16rpx;
|
||||
}
|
||||
|
||||
.result-text {
|
||||
flex: 1;
|
||||
font-size: 26rpx;
|
||||
color: #333;
|
||||
word-break: break-all;
|
||||
line-height: 1.6;
|
||||
}
|
||||
|
||||
.copy-btn {
|
||||
padding: 8rpx 20rpx;
|
||||
background-color: #07c160;
|
||||
color: #fff;
|
||||
font-size: 24rpx;
|
||||
border-radius: 6rpx;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
.copy-btn:active {
|
||||
background-color: #06ad56;
|
||||
}
|
||||
|
||||
.error-box {
|
||||
margin-top: 30rpx;
|
||||
padding: 20rpx;
|
||||
background-color: #fff3cd;
|
||||
border: 2rpx solid #ffc107;
|
||||
border-radius: 8rpx;
|
||||
}
|
||||
|
||||
.error-text {
|
||||
font-size: 26rpx;
|
||||
color: #856404;
|
||||
line-height: 1.6;
|
||||
}
|
||||
@@ -0,0 +1,301 @@
|
||||
// pages/index.ts
|
||||
import { Encrypt, Decrypt, BASE64Encrypt, BASE64Decrypt, AESError } from '../../utils/aes'
|
||||
|
||||
Page({
|
||||
/**
|
||||
* 页面的初始数据
|
||||
*/
|
||||
data: {
|
||||
// 输入原文
|
||||
plainText: 'Hello, 小程序 AES 加密示例!',
|
||||
// 标准 Base64 加密结果
|
||||
encryptedText: '',
|
||||
// 标准 Base64 解密结果
|
||||
decryptedText: '',
|
||||
// URL Safe Base64 加密结果
|
||||
base64EncryptedText: '',
|
||||
// URL Safe Base64 解密结果
|
||||
base64DecryptedText: '',
|
||||
// 错误信息
|
||||
errorMessage: '',
|
||||
},
|
||||
|
||||
/**
|
||||
* 生命周期函数--监听页面加载
|
||||
*/
|
||||
onLoad() {
|
||||
// 页面加载时自动执行一次加密解密示例
|
||||
this.handleEncrypt()
|
||||
},
|
||||
|
||||
/**
|
||||
* 生命周期函数--监听页面初次渲染完成
|
||||
*/
|
||||
onReady() {},
|
||||
|
||||
/**
|
||||
* 生命周期函数--监听页面显示
|
||||
*/
|
||||
onShow() {},
|
||||
|
||||
/**
|
||||
* 生命周期函数--监听页面隐藏
|
||||
*/
|
||||
onHide() {},
|
||||
|
||||
/**
|
||||
* 生命周期函数--监听页面卸载
|
||||
*/
|
||||
onUnload() {},
|
||||
|
||||
/**
|
||||
* 页面相关事件处理函数--监听用户下拉动作
|
||||
*/
|
||||
onPullDownRefresh() {},
|
||||
|
||||
/**
|
||||
* 页面上拉触底事件的处理函数
|
||||
*/
|
||||
onReachBottom() {},
|
||||
|
||||
/**
|
||||
* 用户点击右上角分享
|
||||
*/
|
||||
onShareAppMessage() {},
|
||||
|
||||
/**
|
||||
* 输入原文变化
|
||||
*/
|
||||
onPlainTextInput(e: WechatMiniprogram.Input) {
|
||||
this.setData({
|
||||
plainText: e.detail.value,
|
||||
errorMessage: '',
|
||||
})
|
||||
},
|
||||
|
||||
/**
|
||||
* 执行标准 Base64 加密
|
||||
*/
|
||||
handleEncrypt() {
|
||||
try {
|
||||
const { plainText } = this.data
|
||||
if (!plainText.trim()) {
|
||||
wx.showToast({
|
||||
title: '请输入要加密的内容',
|
||||
icon: 'none',
|
||||
}).catch(() => {
|
||||
console.error('showToast error')
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// 执行加密
|
||||
const encrypted = Encrypt(plainText)
|
||||
this.setData({
|
||||
encryptedText: encrypted,
|
||||
errorMessage: '',
|
||||
})
|
||||
|
||||
wx.showToast({
|
||||
title: '加密成功',
|
||||
icon: 'success',
|
||||
}).catch(() => {
|
||||
console.error('showToast error')
|
||||
})
|
||||
} catch (error) {
|
||||
const errorMsg = error instanceof AESError ? error.message : '加密失败'
|
||||
this.setData({
|
||||
errorMessage: errorMsg,
|
||||
})
|
||||
wx.showToast({
|
||||
title: errorMsg,
|
||||
icon: 'none',
|
||||
duration: 2000,
|
||||
}).catch(() => {
|
||||
console.error('showToast error')
|
||||
})
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* 执行标准 Base64 解密
|
||||
*/
|
||||
handleDecrypt() {
|
||||
try {
|
||||
const { encryptedText } = this.data
|
||||
if (!encryptedText.trim()) {
|
||||
wx.showToast({
|
||||
title: '请输入要解密的内容',
|
||||
icon: 'none',
|
||||
}).catch(() => {
|
||||
console.error('showToast error')
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// 执行解密
|
||||
const decrypted = Decrypt(encryptedText)
|
||||
if (decrypted === null) {
|
||||
this.setData({
|
||||
errorMessage: '解密失败:密钥不匹配或密文被篡改',
|
||||
})
|
||||
wx.showToast({
|
||||
title: '解密失败',
|
||||
icon: 'none',
|
||||
duration: 2000,
|
||||
}).catch(() => {
|
||||
console.error('showToast error')
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
this.setData({
|
||||
decryptedText: decrypted,
|
||||
errorMessage: '',
|
||||
})
|
||||
|
||||
wx.showToast({
|
||||
title: '解密成功',
|
||||
icon: 'success',
|
||||
}).catch(() => {
|
||||
console.error('showToast error')
|
||||
})
|
||||
} catch (error) {
|
||||
const errorMsg = error instanceof AESError ? error.message : '解密失败'
|
||||
this.setData({
|
||||
errorMessage: errorMsg,
|
||||
})
|
||||
wx.showToast({
|
||||
title: errorMsg,
|
||||
icon: 'none',
|
||||
duration: 2000,
|
||||
}).catch(() => {
|
||||
console.error('showToast error')
|
||||
})
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* 执行 URL Safe Base64 加密
|
||||
*/
|
||||
handleBase64Encrypt() {
|
||||
try {
|
||||
const { plainText } = this.data
|
||||
if (!plainText.trim()) {
|
||||
wx.showToast({
|
||||
title: '请输入要加密的内容',
|
||||
icon: 'none',
|
||||
}).catch(() => {
|
||||
console.error('showToast error')
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// 执行 URL Safe Base64 加密
|
||||
const encrypted = BASE64Encrypt(plainText)
|
||||
this.setData({
|
||||
base64EncryptedText: encrypted,
|
||||
errorMessage: '',
|
||||
})
|
||||
|
||||
wx.showToast({
|
||||
title: '加密成功',
|
||||
icon: 'success',
|
||||
}).catch(() => {
|
||||
console.error('showToast error')
|
||||
})
|
||||
} catch (error) {
|
||||
const errorMsg = error instanceof AESError ? error.message : '加密失败'
|
||||
this.setData({
|
||||
errorMessage: errorMsg,
|
||||
})
|
||||
wx.showToast({
|
||||
title: errorMsg,
|
||||
icon: 'none',
|
||||
duration: 2000,
|
||||
}).catch(() => {
|
||||
console.error('showToast error')
|
||||
})
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* 执行 URL Safe Base64 解密
|
||||
*/
|
||||
handleBase64Decrypt() {
|
||||
try {
|
||||
const { base64EncryptedText } = this.data
|
||||
if (!base64EncryptedText.trim()) {
|
||||
wx.showToast({
|
||||
title: '请输入要解密的内容',
|
||||
icon: 'none',
|
||||
}).catch(() => {
|
||||
console.error('showToast error')
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// 执行 URL Safe Base64 解密
|
||||
const decrypted = BASE64Decrypt(base64EncryptedText)
|
||||
if (decrypted === null) {
|
||||
this.setData({
|
||||
errorMessage: '解密失败:密钥不匹配或密文被篡改',
|
||||
})
|
||||
wx.showToast({
|
||||
title: '解密失败',
|
||||
icon: 'none',
|
||||
duration: 2000,
|
||||
}).catch(() => {
|
||||
console.error('showToast error')
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
this.setData({
|
||||
base64DecryptedText: decrypted,
|
||||
errorMessage: '',
|
||||
})
|
||||
|
||||
wx.showToast({
|
||||
title: '解密成功',
|
||||
icon: 'success',
|
||||
}).catch(() => {
|
||||
console.error('showToast error')
|
||||
})
|
||||
} catch (error) {
|
||||
const errorMsg = error instanceof AESError ? error.message : '解密失败'
|
||||
this.setData({
|
||||
errorMessage: errorMsg,
|
||||
})
|
||||
wx.showToast({
|
||||
title: errorMsg,
|
||||
icon: 'none',
|
||||
duration: 2000,
|
||||
}).catch(() => {
|
||||
console.error('showToast error')
|
||||
})
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* 复制文本到剪贴板
|
||||
*/
|
||||
copyText(e: WechatMiniprogram.Touch) {
|
||||
const { text } = e.currentTarget.dataset
|
||||
if (!text) {
|
||||
return
|
||||
}
|
||||
|
||||
wx.setClipboardData({
|
||||
data: text,
|
||||
success: () => {
|
||||
wx.showToast({
|
||||
title: '复制成功',
|
||||
icon: 'success',
|
||||
}).catch(() => {
|
||||
console.error('showToast error')
|
||||
})
|
||||
},
|
||||
})
|
||||
},
|
||||
})
|
||||
@@ -0,0 +1,104 @@
|
||||
<!--pages/index.wxml-->
|
||||
<view class="container">
|
||||
<view class="header">
|
||||
<text class="title">AES 加密解密示例</text>
|
||||
</view>
|
||||
|
||||
<!-- 原文输入区域 -->
|
||||
<view class="section">
|
||||
<view class="section-title">原文输入</view>
|
||||
<textarea
|
||||
class="input-textarea"
|
||||
value="{{plainText}}"
|
||||
placeholder="请输入要加密的内容"
|
||||
bindinput="onPlainTextInput"
|
||||
maxlength="500"
|
||||
show-confirm-bar="{{false}}"
|
||||
/>
|
||||
</view>
|
||||
|
||||
<!-- 标准 Base64 加密解密 -->
|
||||
<view class="section">
|
||||
<view class="section-title">标准 Base64 加密/解密</view>
|
||||
|
||||
<view class="action-bar">
|
||||
<button class="btn btn-primary" bindtap="handleEncrypt">加密</button>
|
||||
<button class="btn btn-secondary" bindtap="handleDecrypt">解密</button>
|
||||
</view>
|
||||
|
||||
<view class="result-box">
|
||||
<view class="result-label">加密结果:</view>
|
||||
<view class="result-content">
|
||||
<text class="result-text" selectable="{{true}}">{{encryptedText || '暂无'}}</text>
|
||||
<text
|
||||
class="copy-btn"
|
||||
data-text="{{encryptedText}}"
|
||||
bindtap="copyText"
|
||||
wx:if="{{encryptedText}}"
|
||||
>
|
||||
复制
|
||||
</text>
|
||||
</view>
|
||||
</view>
|
||||
|
||||
<view class="result-box">
|
||||
<view class="result-label">解密结果:</view>
|
||||
<view class="result-content">
|
||||
<text class="result-text" selectable="{{true}}">{{decryptedText || '暂无'}}</text>
|
||||
<text
|
||||
class="copy-btn"
|
||||
data-text="{{decryptedText}}"
|
||||
bindtap="copyText"
|
||||
wx:if="{{decryptedText}}"
|
||||
>
|
||||
复制
|
||||
</text>
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
|
||||
<!-- URL Safe Base64 加密解密 -->
|
||||
<view class="section">
|
||||
<view class="section-title">URL Safe Base64 加密/解密</view>
|
||||
|
||||
<view class="action-bar">
|
||||
<button class="btn btn-primary" bindtap="handleBase64Encrypt">加密</button>
|
||||
<button class="btn btn-secondary" bindtap="handleBase64Decrypt">解密</button>
|
||||
</view>
|
||||
|
||||
<view class="result-box">
|
||||
<view class="result-label">加密结果:</view>
|
||||
<view class="result-content">
|
||||
<text class="result-text" selectable="{{true}}">{{base64EncryptedText || '暂无'}}</text>
|
||||
<text
|
||||
class="copy-btn"
|
||||
data-text="{{base64EncryptedText}}"
|
||||
bindtap="copyText"
|
||||
wx:if="{{base64EncryptedText}}"
|
||||
>
|
||||
复制
|
||||
</text>
|
||||
</view>
|
||||
</view>
|
||||
|
||||
<view class="result-box">
|
||||
<view class="result-label">解密结果:</view>
|
||||
<view class="result-content">
|
||||
<text class="result-text" selectable="{{true}}">{{base64DecryptedText || '暂无'}}</text>
|
||||
<text
|
||||
class="copy-btn"
|
||||
data-text="{{base64DecryptedText}}"
|
||||
bindtap="copyText"
|
||||
wx:if="{{base64DecryptedText}}"
|
||||
>
|
||||
复制
|
||||
</text>
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
|
||||
<!-- 错误信息显示 -->
|
||||
<view class="error-box" wx:if="{{errorMessage}}">
|
||||
<text class="error-text">{{errorMessage}}</text>
|
||||
</view>
|
||||
</view>
|
||||
Reference in New Issue
Block a user