| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749 |
- <template>
- <view class="pay-container">
- <!-- 顶部导航栏 -->
- <view class="nav-bar">
- <view class="nav-title">付款</view>
- </view>
- <!-- 支付金额区域 -->
- <view class="amount-section">
- <view class="amount-label">支付金额</view>
- <view class="amount-display">
- <text class="currency">¥</text>
- <text class="amount">{{ displayAmount }}</text>
- </view>
- <view class="amount-tip">请输入金额</view>
- </view>
- <!-- 支付方式展示 -->
- <view class="payment-methods">
- <view class="method-list">
- <view class="method-item active">
- <view class="method-icon alipay">
- <text class="iconfont icon-zhifubao"></text>
- </view>
- <view class="method-info">
- <text class="method-name">支付宝</text>
- <text class="method-desc">安全便捷的支付方式</text>
- </view>
- </view>
- </view>
- </view>
- <!-- 数字键盘 - 使用直接事件绑定,确保兼容性 -->
- <view class="keyboard-section">
- <view class="keyboard">
- <view class="keyboard-row" v-for="(row, rowIndex) in keyboardKeys" :key="rowIndex">
- <view
- class="key-item"
- v-for="(key, keyIndex) in row"
- :key="`${rowIndex}-${keyIndex}`"
- :class="{
- 'key-number': key.type === 'number',
- 'key-delete': key.type === 'delete',
- 'key-dot': key.type === 'dot',
- 'key-confirm': key.type === 'confirm'
- }"
- @click="handleKeyPress(key)"
- @touchstart="handleTouchStart"
- @touchend="handleTouchEnd"
- >
- <text v-if="key.type === 'delete'" class="iconfont icon-delete"></text>
- <text v-else-if="key.type === 'confirm'">确认</text>
- <text v-else>{{ key.value }}</text>
- </view>
- </view>
- </view>
- </view>
- <!-- 支付按钮 -->
- <view class="pay-button-section">
- <button
- class="pay-button"
- :class="{ disabled: !canPay }"
- :disabled="!canPay"
- @click="showConfirmDialog"
- >
- 支付
- </button>
- </view>
- <!-- 确认支付弹框 -->
- <view class="confirm-dialog" v-if="showConfirm" @click="hideConfirmDialog">
- <view class="dialog-content" @click.stop>
- <view class="dialog-header">
- <text class="dialog-title">提示</text>
- <view class="close-btn" @click="hideConfirmDialog">
- <text class="iconfont icon-close">×</text>
- </view>
- </view>
-
- <view class="dialog-body">
- <view class="confirm-amount-section">
- <text class="confirm-label">支付金额</text>
- <view class="confirm-amount-display">
- <text class="confirm-currency">¥</text>
- <text class="confirm-amount">{{ displayAmount }}</text>
- </view>
- </view>
-
- <view class="confirm-tips">
- <text class="tip-text">请确认金额无误</text>
- </view>
- </view>
-
- <view class="dialog-footer">
- <button class="cancel-btn" @click="hideConfirmDialog">取消</button>
- <button class="confirm-btn" @click="handlePay">确认</button>
- </view>
- </view>
- </view>
- </view>
- </template>
- <script>
- import { getGhsData } from '@/api/ghs';
- // 优化:使用常量避免重复创建
- const KEY_TYPES = {
- NUMBER: 'number',
- DELETE: 'delete',
- DOT: 'dot',
- CONFIRM: 'confirm'
- };
- const MAX_INTEGER_LENGTH = 8;
- const MAX_DECIMAL_LENGTH = 2;
- export default {
- name: 'pay',
- data() {
- return {
- currentAmount: '0',
- displayAmount: '0',
- // 优化:使用静态数据,避免重复创建
- keyboardKeys: [
- [
- { type: KEY_TYPES.NUMBER, value: '1' },
- { type: KEY_TYPES.NUMBER, value: '2' },
- { type: KEY_TYPES.NUMBER, value: '3' }
- ],
- [
- { type: KEY_TYPES.NUMBER, value: '4' },
- { type: KEY_TYPES.NUMBER, value: '5' },
- { type: KEY_TYPES.NUMBER, value: '6' }
- ],
- [
- { type: KEY_TYPES.NUMBER, value: '7' },
- { type: KEY_TYPES.NUMBER, value: '8' },
- { type: KEY_TYPES.NUMBER, value: '9' }
- ],
- [
- { type: KEY_TYPES.DOT, value: '.' },
- { type: KEY_TYPES.NUMBER, value: '0' },
- { type: KEY_TYPES.DELETE, value: '' }
- ]
- ],
- // 优化:使用更高效的状态管理
- amountBuffer: '0',
- updateScheduled: false,
- updateTimer: null,
- // 优化:缓存计算结果
- _cachedCanPay: false,
- _lastAmount: '0',
- // 弹框状态
- showConfirm: false,
- id:0,
- salt:''
- }
- },
- computed: {
- // 优化:使用缓存的计算属性
- canPay() {
- // 只有当金额变化时才重新计算
- if (this.currentAmount !== this._lastAmount) {
- const amount = parseFloat(this.currentAmount);
- this._cachedCanPay = amount > 0 && amount <= 99999999.99;
- this._lastAmount = this.currentAmount;
- }
- return this._cachedCanPay;
- }
- },
- onLoad(options) {
- if (options.id) {
- this.id = options.id;
- }
- if (options.salt) {
- this.salt = options.salt;
- }
- if (this.id && this.salt) {
- getGhsData({id:this.id,salt:this.salt}).then(res => {
- console.log(res)
- })
- }
- },
- methods: {
- // 优化:添加触摸反馈
- handleTouchStart(e) {
- const target = e.currentTarget;
- if (target && target.style) {
- target.style.transform = 'scale(0.95)';
- }
- },
-
- handleTouchEnd(e) {
- const target = e.currentTarget;
- if (target && target.style) {
- setTimeout(() => {
- target.style.transform = 'scale(1)';
- }, 100);
- }
- },
- // 优化:简化的键盘处理逻辑
- handleKeyPress(key) {
- // 防抖处理
- if (this.updateTimer) {
- clearTimeout(this.updateTimer);
- }
-
- if (key.type === KEY_TYPES.NUMBER) {
- this.addNumber(key.value);
- } else if (key.type === KEY_TYPES.DELETE) {
- this.deleteNumber();
- } else if (key.type === KEY_TYPES.DOT) {
- this.addDecimal();
- }
-
- // 使用 requestAnimationFrame 优化更新
- this.scheduleUpdate();
- },
- // 优化:使用 requestAnimationFrame 调度更新
- scheduleUpdate() {
- if (!this.updateScheduled) {
- this.updateScheduled = true;
- requestAnimationFrame(() => {
- this.updateDisplay();
- this.updateScheduled = false;
- });
- }
- },
- // 优化:更高效的数字添加逻辑
- addNumber(num) {
- const buffer = this.amountBuffer;
-
- // 快速路径:如果当前是0,直接替换
- if (buffer === '0') {
- this.amountBuffer = num;
- this.currentAmount = num;
- return;
- }
- // 检查长度限制
- const parts = buffer.split('.');
- const integerPart = parts[0];
- const decimalPart = parts[1];
- // 整数部分限制
- if (!decimalPart && integerPart.length >= MAX_INTEGER_LENGTH) {
- return;
- }
- // 小数部分限制
- if (decimalPart && decimalPart.length >= MAX_DECIMAL_LENGTH) {
- return;
- }
- // 添加数字
- this.amountBuffer = buffer + num;
- this.currentAmount = this.amountBuffer;
- },
- // 优化:更高效的小数点添加逻辑
- addDecimal() {
- if (!this.amountBuffer.includes('.')) {
- this.amountBuffer += '.';
- this.currentAmount = this.amountBuffer;
- }
- },
- // 优化:更高效的删除逻辑
- deleteNumber() {
- const buffer = this.amountBuffer;
- if (buffer.length > 1) {
- this.amountBuffer = buffer.slice(0, -1);
- } else {
- this.amountBuffer = '0';
- }
- this.currentAmount = this.amountBuffer;
- },
- // 优化:更高效的显示更新逻辑
- updateDisplay() {
- const amount = this.currentAmount;
-
- // 快速路径:如果是0,直接显示
- if (amount === '0') {
- this.displayAmount = '0';
- return;
- }
- // 检查是否包含小数点
- if (amount.includes('.')) {
- const numAmount = parseFloat(amount) || 0;
- this.displayAmount = numAmount.toFixed(2);
- } else {
- // 整数显示
- this.displayAmount = amount;
- }
- },
- // 优化:更高效的金额设置
- setAmount(amount) {
- const numAmount = parseFloat(amount);
- if (!isNaN(numAmount) && numAmount > 0) {
- const amountStr = numAmount.toString();
- this.amountBuffer = amountStr;
- this.currentAmount = amountStr;
- this.updateDisplay();
- }
- },
- // 显示确认弹框
- showConfirmDialog() {
- if (!this.canPay) return;
- this.showConfirm = true;
- },
- // 隐藏确认弹框
- hideConfirmDialog() {
- this.showConfirm = false;
- },
- // 处理支付
- async handlePay() {
- if (!this.canPay) return;
- const amount = parseFloat(this.currentAmount);
- // 隐藏弹框
- this.hideConfirmDialog();
- },
- // 请求支付
- async requestPayment(amount) {
- },
- // 生成订单ID
- generateOrderId() {
- const timestamp = Date.now();
- const random = Math.floor(Math.random() * 10000);
- return `ORDER${timestamp}${random}`;
- }
- }
- }
- </script>
- <style lang="scss" scoped>
- .pay-container {
- min-height: 100vh;
- background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
- position: relative;
- }
- .nav-bar {
- display: flex;
- align-items: center;
- justify-content: center;
- padding: 20rpx 30rpx;
- background: rgba(255, 255, 255, 0.1);
- backdrop-filter: blur(10px);
-
- .nav-title {
- color: #fff;
- font-size: 36rpx;
- font-weight: 600;
- }
- }
- .amount-section {
- padding: 60rpx 40rpx;
- text-align: center;
-
- .amount-label {
- color: rgba(255, 255, 255, 0.8);
- font-size: 28rpx;
- margin-bottom: 20rpx;
- }
-
- .amount-display {
- display: flex;
- align-items: baseline;
- justify-content: center;
- margin-bottom: 20rpx;
-
- .currency {
- color: #fff;
- font-size: 48rpx;
- font-weight: 600;
- margin-right: 10rpx;
- }
-
- .amount {
- color: #fff;
- font-size: 80rpx;
- font-weight: 700;
- font-family: 'Arial', sans-serif;
- }
- }
-
- .amount-tip {
- color: rgba(255, 255, 255, 0.6);
- font-size: 24rpx;
- }
- }
- .payment-methods {
- background: rgba(255, 255, 255, 0.95);
- margin: 0 30rpx 20rpx;
- border-radius: 15rpx;
- padding: 20rpx 30rpx;
-
- .method-list {
- .method-item {
- display: flex;
- align-items: center;
- padding: 20rpx 0;
- transition: all 0.3s ease;
-
- &.active {
- background: rgba(102, 126, 234, 0.1);
- border-radius: 12rpx;
- padding: 20rpx 15rpx;
- margin: 0 -15rpx;
- }
-
- .method-icon {
- width: 60rpx;
- height: 60rpx;
- border-radius: 12rpx;
- display: flex;
- align-items: center;
- justify-content: center;
- margin-right: 20rpx;
-
- &.alipay {
- background: linear-gradient(135deg, #1677ff 0%, #4096ff 100%);
- }
-
- .iconfont {
- color: #fff;
- font-size: 32rpx;
- }
- }
-
- .method-info {
- flex: 1;
-
- .method-name {
- display: block;
- font-size: 28rpx;
- font-weight: 600;
- color: #333;
- margin-bottom: 4rpx;
- }
-
- .method-desc {
- display: block;
- font-size: 22rpx;
- color: #999;
- }
- }
-
- .method-check {
- width: 50rpx;
- height: 50rpx;
- display: flex;
- align-items: center;
- justify-content: center;
-
- .iconfont {
- color: #1677ff;
- font-size: 32rpx;
- }
- }
- }
- }
- }
- .keyboard-section {
- position: fixed;
- bottom: 160rpx;
- left: 0;
- right: 0;
- background: rgba(255, 255, 255, 0.95);
- backdrop-filter: blur(10px);
- padding: 30rpx;
-
- .keyboard {
- .keyboard-row {
- display: flex;
- margin-bottom: 20rpx;
-
- &:last-child {
- margin-bottom: 0;
- }
-
- .key-item {
- flex: 1;
- height: 120rpx;
- margin: 0 10rpx;
- border-radius: 20rpx;
- display: flex;
- align-items: center;
- justify-content: center;
- font-size: 40rpx;
- font-weight: 600;
- transition: transform 0.08s ease;
- user-select: none;
- -webkit-user-select: none;
- cursor: pointer;
- will-change: transform;
-
- &.key-number {
- background: #fff;
- color: #333;
- box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.1);
- }
-
- &.key-delete {
- background: #ff6b6b;
- color: #fff;
- }
-
- &.key-dot {
- background: #fff;
- color: #333;
- box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.1);
- }
-
- &.key-confirm {
- background: #667eea;
- color: #fff;
- }
- }
- }
- }
- }
- .pay-button-section {
- position: fixed;
- bottom: 0;
- left: 0;
- right: 0;
- padding: 30rpx;
- background: rgba(255, 255, 255, 0.95);
- backdrop-filter: blur(10px);
-
- .pay-button {
- width: 100%;
- height: 100rpx;
- background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
- color: #fff;
- font-size: 36rpx;
- font-weight: 600;
- border-radius: 50rpx;
- border: none;
- transition: all 0.3s ease;
-
- &.disabled {
- background: #ccc;
- color: #999;
- }
-
- &:not(.disabled):active {
- transform: scale(0.98);
- }
- }
- }
- // 确认支付弹框样式
- .confirm-dialog {
- position: fixed;
- top: 0;
- left: 0;
- right: 0;
- bottom: 0;
- background: rgba(0, 0, 0, 0.6);
- display: flex;
- align-items: center;
- justify-content: center;
- z-index: 9999;
- animation: fadeIn 0.3s ease;
-
- .dialog-content {
- width: 85%;
- max-width: 700rpx;
- background: #fff;
- border-radius: 20rpx;
- overflow: hidden;
- animation: slideUp 0.3s ease;
-
- .dialog-header {
- display: flex;
- align-items: center;
- justify-content: space-between;
- padding: 30rpx 40rpx 20rpx;
- border-bottom: 1rpx solid #f0f0f0;
-
- .dialog-title {
- font-size: 36rpx;
- font-weight: 600;
- color: #333;
- }
-
- .close-btn {
- width: 60rpx;
- height: 60rpx;
- display: flex;
- align-items: center;
- justify-content: center;
- border-radius: 50%;
- background: #f5f5f5;
- cursor: pointer;
- transition: all 0.2s ease;
-
- &:active {
- background: #e0e0e0;
- transform: scale(0.95);
- }
-
- .iconfont {
- font-size: 32rpx;
- color: #666;
- font-weight: bold;
- }
- }
- }
-
- .dialog-body {
- padding: 40rpx;
-
- .confirm-amount-section {
- text-align: center;
- margin-bottom: 30rpx;
-
- .confirm-label {
- display: block;
- font-size: 28rpx;
- color: #666;
- margin-bottom: 20rpx;
- }
-
- .confirm-amount-display {
- display: flex;
- align-items: baseline;
- justify-content: center;
-
- .confirm-currency {
- font-size: 60rpx;
- font-weight: 600;
- color: #ff6b6b;
- margin-right: 10rpx;
- }
-
- .confirm-amount {
- font-size: 100rpx;
- font-weight: 700;
- color: #ff6b6b;
- font-family: 'Arial', sans-serif;
- text-shadow: 0 2rpx 10rpx rgba(255, 107, 107, 0.3);
- }
- }
- }
-
- .confirm-tips {
- text-align: center;
-
- .tip-text {
- font-size: 26rpx;
- color: #999;
- line-height: 1.5;
- }
- }
- }
-
- .dialog-footer {
- display: flex;
- border-top: 1rpx solid #f0f0f0;
- padding: 30rpx 40rpx;
- gap: 20rpx;
-
- .cancel-btn, .confirm-btn {
- flex: 1;
- height: 100rpx;
- border: none;
- font-size: 32rpx;
- font-weight: 600;
- transition: all 0.2s ease;
- border-radius: 50rpx;
- display: flex;
- align-items: center;
- justify-content: center;
-
- &:active {
- transform: scale(0.98);
- }
- }
-
- .cancel-btn {
- background: #f5f5f5;
- color: #666;
- border: 2rpx solid #e0e0e0;
-
- &:active {
- background: #e0e0e0;
- border-color: #d0d0d0;
- }
- }
-
- .confirm-btn {
- background: linear-gradient(135deg, #ff6b6b 0%, #ff5252 100%);
- color: #fff;
-
- &:active {
- background: linear-gradient(135deg, #ff5252 0%, #ff4444 100%);
- }
- }
- }
- }
- }
- // 弹框动画
- @keyframes fadeIn {
- from {
- opacity: 0;
- }
- to {
- opacity: 1;
- }
- }
- @keyframes slideUp {
- from {
- opacity: 0;
- transform: translateY(50rpx) scale(0.9);
- }
- to {
- opacity: 1;
- transform: translateY(0) scale(1);
- }
- }
- </style>
|