在添加账户的添加二维码输入框右侧添加上传图片,跳转
This commit is contained in:
parent
6e73e36644
commit
678febf443
|
|
@ -1,318 +0,0 @@
|
|||
<template>
|
||||
<view class="usage-example">
|
||||
<view class="example-header">
|
||||
<text class="title">单图上传使用示例</text>
|
||||
<text class="desc">展示如何在页面中集成图片上传功能</text>
|
||||
</view>
|
||||
|
||||
<!-- 方法一:跳转到上传页面 -->
|
||||
<view class="method-section">
|
||||
<text class="method-title">方法一:跳转到上传页面</text>
|
||||
<text class="method-desc">跳转到专门的上传页面,上传完成后返回</text>
|
||||
|
||||
<button class="method-btn" @click="method1">跳转上传</button>
|
||||
|
||||
<view v-if="method1Result" class="result-box">
|
||||
<text class="result-label">上传结果:</text>
|
||||
<image :src="method1Result" mode="widthFix" class="result-image"></image>
|
||||
<text class="result-url">{{ method1Result }}</text>
|
||||
</view>
|
||||
</view>
|
||||
|
||||
<!-- 方法二:内嵌上传组件 -->
|
||||
<view class="method-section">
|
||||
<text class="method-title">方法二:内嵌上传组件</text>
|
||||
<text class="method-desc">在当前页面直接使用上传组件</text>
|
||||
|
||||
<view class="inline-upload">
|
||||
<view class="upload-area" @click="chooseImage">
|
||||
<view v-if="!selectedImage" class="upload-placeholder">
|
||||
<text>点击选择图片</text>
|
||||
</view>
|
||||
<image v-else :src="selectedImage" mode="aspectFit" class="preview-image"></image>
|
||||
</view>
|
||||
|
||||
<button
|
||||
class="upload-btn"
|
||||
@click="uploadSelectedImage"
|
||||
:disabled="!selectedImage || uploading"
|
||||
>
|
||||
{{ uploading ? '上传中...' : '上传' }}
|
||||
</button>
|
||||
</view>
|
||||
|
||||
<view v-if="method2Result" class="result-box">
|
||||
<text class="result-label">上传结果:</text>
|
||||
<image :src="method2Result" mode="widthFix" class="result-image"></image>
|
||||
<text class="result-url">{{ method2Result }}</text>
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import { getQiniuUploadToken, uploadToQiniu } from '@/api/upload.js'
|
||||
|
||||
export default {
|
||||
data() {
|
||||
return {
|
||||
// 方法一结果
|
||||
method1Result: '',
|
||||
|
||||
// 方法二数据
|
||||
selectedImage: '',
|
||||
uploading: false,
|
||||
method2Result: '',
|
||||
qiniuToken: '',
|
||||
}
|
||||
},
|
||||
|
||||
onLoad() {
|
||||
// 监听上传成功事件
|
||||
uni.$on('image-upload-success', this.onImageUploadSuccess)
|
||||
},
|
||||
|
||||
onUnload() {
|
||||
// 移除事件监听
|
||||
uni.$off('image-upload-success', this.onImageUploadSuccess)
|
||||
},
|
||||
|
||||
methods: {
|
||||
// 方法一:跳转到上传页面
|
||||
method1() {
|
||||
uni.navigateTo({
|
||||
url: '/pages/image-upload/image-upload'
|
||||
})
|
||||
},
|
||||
|
||||
// 监听上传成功事件
|
||||
onImageUploadSuccess(imageUrl) {
|
||||
this.method1Result = imageUrl
|
||||
uni.showToast({
|
||||
title: '图片上传成功',
|
||||
icon: 'success'
|
||||
})
|
||||
},
|
||||
|
||||
// 方法二:选择图片
|
||||
chooseImage() {
|
||||
uni.chooseImage({
|
||||
count: 1,
|
||||
sizeType: ['compressed'],
|
||||
sourceType: ['album', 'camera'],
|
||||
success: (res) => {
|
||||
this.selectedImage = res.tempFilePaths[0]
|
||||
this.method2Result = ''
|
||||
}
|
||||
})
|
||||
},
|
||||
|
||||
// 方法二:上传选中的图片
|
||||
async uploadSelectedImage() {
|
||||
if (!this.selectedImage) {
|
||||
uni.showToast({
|
||||
title: '请先选择图片',
|
||||
icon: 'none'
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
this.uploading = true
|
||||
|
||||
try {
|
||||
// 获取token
|
||||
if (!this.qiniuToken) {
|
||||
await this.getQiniuToken()
|
||||
}
|
||||
|
||||
// 生成文件名
|
||||
const key = `examples/${Date.now()}_${Math.random().toString(36).slice(2)}.jpg`
|
||||
|
||||
// 上传
|
||||
const result = await uploadToQiniu(this.selectedImage, this.qiniuToken, key)
|
||||
this.method2Result = `https://api.ccttiot.com/${result.key}`
|
||||
|
||||
uni.showToast({
|
||||
title: '上传成功',
|
||||
icon: 'success'
|
||||
})
|
||||
|
||||
} catch (error) {
|
||||
console.error('上传失败:', error)
|
||||
uni.showToast({
|
||||
title: '上传失败',
|
||||
icon: 'none'
|
||||
})
|
||||
} finally {
|
||||
this.uploading = false
|
||||
}
|
||||
},
|
||||
|
||||
// 获取七牛云token
|
||||
async getQiniuToken() {
|
||||
try {
|
||||
const res = await getQiniuUploadToken()
|
||||
if (res.code === 200) {
|
||||
const token = res.data?.token || res.data?.uploadToken || res.token || res.data
|
||||
if (token) {
|
||||
this.qiniuToken = token
|
||||
} else {
|
||||
throw new Error('Token字段不存在')
|
||||
}
|
||||
} else {
|
||||
throw new Error(res.msg || '获取Token失败')
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('获取Token失败:', error)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<style lang="scss" scoped>
|
||||
.usage-example {
|
||||
padding: 30rpx;
|
||||
background: #f5f5f5;
|
||||
min-height: 100vh;
|
||||
|
||||
.example-header {
|
||||
text-align: center;
|
||||
margin-bottom: 40rpx;
|
||||
|
||||
.title {
|
||||
display: block;
|
||||
font-size: 36rpx;
|
||||
font-weight: bold;
|
||||
color: #333;
|
||||
margin-bottom: 10rpx;
|
||||
}
|
||||
|
||||
.desc {
|
||||
display: block;
|
||||
font-size: 26rpx;
|
||||
color: #666;
|
||||
}
|
||||
}
|
||||
|
||||
.method-section {
|
||||
background: #fff;
|
||||
border-radius: 16rpx;
|
||||
padding: 30rpx;
|
||||
margin-bottom: 30rpx;
|
||||
|
||||
.method-title {
|
||||
display: block;
|
||||
font-size: 30rpx;
|
||||
font-weight: bold;
|
||||
color: #333;
|
||||
margin-bottom: 10rpx;
|
||||
}
|
||||
|
||||
.method-desc {
|
||||
display: block;
|
||||
font-size: 24rpx;
|
||||
color: #666;
|
||||
margin-bottom: 20rpx;
|
||||
}
|
||||
|
||||
.method-btn {
|
||||
width: 100%;
|
||||
height: 80rpx;
|
||||
line-height: 80rpx;
|
||||
background: #007aff;
|
||||
color: #fff;
|
||||
border-radius: 12rpx;
|
||||
font-size: 28rpx;
|
||||
border: none;
|
||||
margin-bottom: 20rpx;
|
||||
|
||||
&:active {
|
||||
background: #0056cc;
|
||||
}
|
||||
}
|
||||
|
||||
.inline-upload {
|
||||
margin-bottom: 20rpx;
|
||||
|
||||
.upload-area {
|
||||
width: 100%;
|
||||
height: 200rpx;
|
||||
background: #f8f8f8;
|
||||
border-radius: 12rpx;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
margin-bottom: 20rpx;
|
||||
border: 2rpx dashed #ddd;
|
||||
|
||||
.upload-placeholder {
|
||||
text-align: center;
|
||||
|
||||
text {
|
||||
font-size: 28rpx;
|
||||
color: #999;
|
||||
}
|
||||
}
|
||||
|
||||
.preview-image {
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
border-radius: 12rpx;
|
||||
}
|
||||
}
|
||||
|
||||
.upload-btn {
|
||||
width: 100%;
|
||||
height: 70rpx;
|
||||
line-height: 70rpx;
|
||||
background: #52c41a;
|
||||
color: #fff;
|
||||
border-radius: 8rpx;
|
||||
font-size: 26rpx;
|
||||
border: none;
|
||||
|
||||
&:active {
|
||||
background: #389e0d;
|
||||
}
|
||||
|
||||
&:disabled {
|
||||
background: #ccc;
|
||||
color: #999;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.result-box {
|
||||
border-top: 1rpx solid #eee;
|
||||
padding-top: 20rpx;
|
||||
|
||||
.result-label {
|
||||
display: block;
|
||||
font-size: 26rpx;
|
||||
font-weight: bold;
|
||||
color: #333;
|
||||
margin-bottom: 15rpx;
|
||||
}
|
||||
|
||||
.result-image {
|
||||
width: 100%;
|
||||
border-radius: 8rpx;
|
||||
margin-bottom: 15rpx;
|
||||
}
|
||||
|
||||
.result-url {
|
||||
display: block;
|
||||
font-size: 22rpx;
|
||||
color: #007aff;
|
||||
word-break: break-all;
|
||||
line-height: 1.4;
|
||||
background: #f8f8f8;
|
||||
padding: 15rpx;
|
||||
border-radius: 6rpx;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
</style>
|
||||
|
|
@ -98,13 +98,6 @@
|
|||
"style": {
|
||||
"navigationBarTitleText": "图片上传"
|
||||
}
|
||||
},
|
||||
{
|
||||
"path": "pages/image-upload-demo/image-upload-demo",
|
||||
"style": {
|
||||
"navigationBarTitleText": "图片上传演示",
|
||||
"navigationStyle": "custom"
|
||||
}
|
||||
}
|
||||
],
|
||||
"tabBar": {
|
||||
|
|
|
|||
|
|
@ -1,174 +0,0 @@
|
|||
<template>
|
||||
<view class="demo-page">
|
||||
<u-navbar
|
||||
:is-back="true"
|
||||
title="图片上传演示"
|
||||
title-color="#000"
|
||||
:border-bottom="false"
|
||||
:background="true"
|
||||
></u-navbar>
|
||||
|
||||
<view class="content">
|
||||
<view class="demo-section">
|
||||
<text class="section-title">图片上传功能演示</text>
|
||||
<text class="section-desc">点击下方按钮跳转到图片上传页面</text>
|
||||
</view>
|
||||
|
||||
<view class="button-section">
|
||||
<button class="demo-btn" @click="goToUpload">开始上传图片</button>
|
||||
</view>
|
||||
|
||||
<view v-if="uploadedImageUrl" class="result-section">
|
||||
<text class="result-title">上传结果</text>
|
||||
<image
|
||||
:src="uploadedImageUrl"
|
||||
mode="widthFix"
|
||||
class="result-image"
|
||||
></image>
|
||||
<text class="result-url">{{ uploadedImageUrl }}</text>
|
||||
<button class="copy-btn" @click="copyImageUrl">复制图片URL</button>
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
export default {
|
||||
data() {
|
||||
return {
|
||||
uploadedImageUrl: '', // 上传成功的图片URL
|
||||
}
|
||||
},
|
||||
|
||||
onLoad(options) {
|
||||
// 如果从上传页面返回,获取上传结果
|
||||
if (options.imageUrl) {
|
||||
this.uploadedImageUrl = decodeURIComponent(options.imageUrl)
|
||||
}
|
||||
},
|
||||
|
||||
methods: {
|
||||
// 跳转到上传页面
|
||||
goToUpload() {
|
||||
uni.navigateTo({
|
||||
url: '/pages/image-upload/image-upload'
|
||||
})
|
||||
},
|
||||
|
||||
// 复制图片URL
|
||||
copyImageUrl() {
|
||||
if (this.uploadedImageUrl) {
|
||||
uni.setClipboardData({
|
||||
data: this.uploadedImageUrl,
|
||||
success: () => {
|
||||
uni.showToast({
|
||||
title: '图片URL已复制',
|
||||
icon: 'success'
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<style lang="scss" scoped>
|
||||
.demo-page {
|
||||
min-height: 100vh;
|
||||
background: #f5f5f5;
|
||||
|
||||
.content {
|
||||
padding: 30rpx;
|
||||
}
|
||||
|
||||
.demo-section {
|
||||
background: #fff;
|
||||
border-radius: 16rpx;
|
||||
padding: 30rpx;
|
||||
margin-bottom: 30rpx;
|
||||
text-align: center;
|
||||
|
||||
.section-title {
|
||||
display: block;
|
||||
font-size: 32rpx;
|
||||
font-weight: bold;
|
||||
color: #333;
|
||||
margin-bottom: 20rpx;
|
||||
}
|
||||
|
||||
.section-desc {
|
||||
display: block;
|
||||
font-size: 26rpx;
|
||||
color: #666;
|
||||
line-height: 1.5;
|
||||
}
|
||||
}
|
||||
|
||||
.button-section {
|
||||
margin-bottom: 30rpx;
|
||||
|
||||
.demo-btn {
|
||||
width: 100%;
|
||||
height: 88rpx;
|
||||
line-height: 88rpx;
|
||||
background: #007aff;
|
||||
color: #fff;
|
||||
border-radius: 12rpx;
|
||||
font-size: 32rpx;
|
||||
border: none;
|
||||
|
||||
&:active {
|
||||
background: #0056cc;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
.result-section {
|
||||
background: #fff;
|
||||
border-radius: 16rpx;
|
||||
padding: 30rpx;
|
||||
|
||||
.result-title {
|
||||
display: block;
|
||||
font-size: 28rpx;
|
||||
font-weight: bold;
|
||||
color: #333;
|
||||
margin-bottom: 20rpx;
|
||||
}
|
||||
|
||||
.result-image {
|
||||
width: 100%;
|
||||
border-radius: 8rpx;
|
||||
margin-bottom: 20rpx;
|
||||
}
|
||||
|
||||
.result-url {
|
||||
display: block;
|
||||
font-size: 24rpx;
|
||||
color: #007aff;
|
||||
word-break: break-all;
|
||||
line-height: 1.4;
|
||||
margin-bottom: 20rpx;
|
||||
background: #f8f8f8;
|
||||
padding: 20rpx;
|
||||
border-radius: 8rpx;
|
||||
}
|
||||
|
||||
.copy-btn {
|
||||
width: 100%;
|
||||
height: 70rpx;
|
||||
line-height: 70rpx;
|
||||
background: #f0f0f0;
|
||||
color: #333;
|
||||
border-radius: 8rpx;
|
||||
font-size: 28rpx;
|
||||
border: none;
|
||||
|
||||
&:active {
|
||||
background: #e0e0e0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
</style>
|
||||
|
|
@ -117,36 +117,16 @@ export default {
|
|||
icon: 'success',
|
||||
})
|
||||
|
||||
// 返回上一页并传递图片URL
|
||||
const pages = getCurrentPages()
|
||||
const prevPage = pages[pages.length - 2]
|
||||
// 先触发事件,再返回上一页
|
||||
console.log('触发图片上传成功事件:', this.uploadResult)
|
||||
uni.$emit('image-upload-success', this.uploadResult)
|
||||
|
||||
if (prevPage) {
|
||||
// 如果上一页存在,可以通过事件总线或其他方式传递数据
|
||||
// 这里我们通过URL参数的方式返回
|
||||
uni.navigateBack({
|
||||
success: () => {
|
||||
// 延迟执行,确保页面已经返回
|
||||
setTimeout(() => {
|
||||
uni.$emit('image-upload-success', this.uploadResult)
|
||||
}, 100)
|
||||
},
|
||||
})
|
||||
} else {
|
||||
// 如果没有上一页,直接返回URL参数
|
||||
// 延迟返回,确保事件被处理
|
||||
setTimeout(() => {
|
||||
uni.navigateBack({
|
||||
delta: 1,
|
||||
success: () => {
|
||||
// 通过URL参数传递结果
|
||||
const currentPage = getCurrentPages()[getCurrentPages().length - 1]
|
||||
if (currentPage && currentPage.onLoad) {
|
||||
currentPage.onLoad({
|
||||
imageUrl: encodeURIComponent(this.uploadResult),
|
||||
})
|
||||
}
|
||||
},
|
||||
})
|
||||
}
|
||||
}, 2000)
|
||||
} catch (error) {
|
||||
console.error('上传失败:', error)
|
||||
this.errorMessage = error.message || '上传失败'
|
||||
|
|
|
|||
|
|
@ -16,12 +16,18 @@
|
|||
|
||||
<view class="form-item">
|
||||
<text class="form-label">{{ typeOptions[typeIndex].noLabel }}</text>
|
||||
<input
|
||||
v-model="bankForm.no"
|
||||
:placeholder="typeOptions[typeIndex].noPlaceholder"
|
||||
class="form-input"
|
||||
maxlength="30"
|
||||
/>
|
||||
<view class="input-group">
|
||||
<input
|
||||
v-model="bankForm.no"
|
||||
:placeholder="typeOptions[typeIndex].noPlaceholder"
|
||||
class="form-input"
|
||||
maxlength="30"
|
||||
/>
|
||||
<!-- 收款二维码类型时显示上传按钮 -->
|
||||
<view v-if="typeOptions[typeIndex].value === 'QR'" class="upload-btn" @click="goToImageUpload">
|
||||
<text class="upload-text">上传</text>
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
|
||||
<view class="form-item">
|
||||
|
|
@ -115,6 +121,22 @@ export default {
|
|||
}
|
||||
},
|
||||
},
|
||||
onLoad() {
|
||||
// 监听图片上传成功事件
|
||||
uni.$on('image-upload-success', this.onImageUploadSuccess)
|
||||
},
|
||||
onShow() {
|
||||
// 页面显示时也监听事件,确保弹窗重新打开时能接收到事件
|
||||
uni.$on('image-upload-success', this.onImageUploadSuccess)
|
||||
},
|
||||
onUnload() {
|
||||
// 移除事件监听
|
||||
uni.$off('image-upload-success', this.onImageUploadSuccess)
|
||||
},
|
||||
onHide() {
|
||||
// 页面隐藏时移除事件监听
|
||||
uni.$off('image-upload-success', this.onImageUploadSuccess)
|
||||
},
|
||||
methods: {
|
||||
// 重置表单
|
||||
resetForm() {
|
||||
|
|
@ -196,6 +218,23 @@ export default {
|
|||
return true
|
||||
},
|
||||
|
||||
// 跳转到图片上传页面
|
||||
goToImageUpload() {
|
||||
uni.navigateTo({
|
||||
url: '/pages/image-upload/image-upload'
|
||||
})
|
||||
},
|
||||
|
||||
// 处理图片上传成功
|
||||
onImageUploadSuccess(imageUrl) {
|
||||
console.log('接收到图片上传成功事件:', imageUrl)
|
||||
this.bankForm.no = imageUrl
|
||||
uni.showToast({
|
||||
title: '图片上传成功',
|
||||
icon: 'success'
|
||||
})
|
||||
},
|
||||
|
||||
// 提交表单
|
||||
async handleSubmit() {
|
||||
if (!this.validateForm()) {
|
||||
|
|
@ -321,6 +360,39 @@ export default {
|
|||
display: flex;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
.input-group {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 15rpx;
|
||||
|
||||
.form-input {
|
||||
flex: 1;
|
||||
}
|
||||
|
||||
.upload-btn {
|
||||
width: 120rpx;
|
||||
height: 80rpx;
|
||||
background: #ff803a;
|
||||
border-radius: 10rpx;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
cursor: pointer;
|
||||
transition: all 0.3s ease;
|
||||
|
||||
&:active {
|
||||
background: #e67333;
|
||||
transform: scale(0.95);
|
||||
}
|
||||
|
||||
.upload-text {
|
||||
color: #fff;
|
||||
font-size: 26rpx;
|
||||
font-weight: 500;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user