From 6e73e3664472b1fcc1a85f9041d74887474f404f Mon Sep 17 00:00:00 2001
From: WindowBird <13870814+windows-bird@user.noreply.gitee.com>
Date: Thu, 21 Aug 2025 13:37:29 +0800
Subject: [PATCH] =?UTF-8?q?=E5=9B=BE=E7=89=87=E4=B8=8A=E4=BC=A0=E4=B8=83?=
=?UTF-8?q?=E7=89=9B=E4=BA=91=E6=88=90=E5=8A=9F?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
README_IMAGE_UPLOAD.md | 189 ++++++++++
api/upload.js | 44 +++
examples/avatar-upload-example.vue | 1 -
examples/single-image-upload-usage.vue | 318 ++++++++++++++++
pages.json | 69 ++--
pages/image-upload-demo/image-upload-demo.vue | 174 +++++++++
pages/image-upload/image-upload.vue | 339 ++++++++++++++++++
7 files changed, 1104 insertions(+), 30 deletions(-)
create mode 100644 README_IMAGE_UPLOAD.md
create mode 100644 api/upload.js
delete mode 100644 examples/avatar-upload-example.vue
create mode 100644 examples/single-image-upload-usage.vue
create mode 100644 pages/image-upload-demo/image-upload-demo.vue
create mode 100644 pages/image-upload/image-upload.vue
diff --git a/README_IMAGE_UPLOAD.md b/README_IMAGE_UPLOAD.md
new file mode 100644
index 0000000..fb4f1c0
--- /dev/null
+++ b/README_IMAGE_UPLOAD.md
@@ -0,0 +1,189 @@
+# 单图上传功能说明
+
+## 功能概述
+
+本项目提供了完整的单图上传功能,支持选择图片、上传到七牛云、返回图片URL。
+
+## 文件结构
+
+```
+├── api/upload.js # 上传相关API
+├── pages/image-upload/ # 上传页面
+│ └── image-upload.vue
+├── pages/image-upload-demo/ # 演示页面
+│ └── image-upload-demo.vue
+└── examples/ # 使用示例
+ └── single-image-upload-usage.vue
+```
+
+## 核心功能
+
+### 1. API接口 (`api/upload.js`)
+
+#### 获取七牛云上传Token
+```javascript
+import { getQiniuUploadToken } from '@/api/upload.js'
+
+const res = await getQiniuUploadToken()
+if (res.code === 200) {
+ const token = res.data?.token || res.data?.uploadToken || res.token || res.data
+}
+```
+
+#### 上传图片到七牛云
+```javascript
+import { uploadToQiniu } from '@/api/upload.js'
+
+const result = await uploadToQiniu(filePath, token, key)
+const imageUrl = `https://api.ccttiot.com/${result.key}`
+```
+
+### 2. 上传页面 (`pages/image-upload/image-upload.vue`)
+
+完整的图片上传页面,包含:
+- 图片选择功能
+- 预览功能
+- 上传进度显示
+- 错误处理
+- 结果展示
+
+## 使用方法
+
+### 方法一:跳转到上传页面
+
+```javascript
+// 跳转到上传页面
+uni.navigateTo({
+ url: '/pages/image-upload/image-upload'
+})
+
+// 监听上传成功事件
+uni.$on('image-upload-success', (imageUrl) => {
+ console.log('上传成功:', imageUrl)
+})
+```
+
+### 方法二:内嵌上传功能
+
+```javascript
+import { getQiniuUploadToken, uploadToQiniu } from '@/api/upload.js'
+
+export default {
+ data() {
+ return {
+ selectedImage: '',
+ qiniuToken: '',
+ uploading: false
+ }
+ },
+
+ methods: {
+ // 选择图片
+ chooseImage() {
+ uni.chooseImage({
+ count: 1,
+ success: (res) => {
+ this.selectedImage = res.tempFilePaths[0]
+ }
+ })
+ },
+
+ // 上传图片
+ async uploadImage() {
+ if (!this.selectedImage) return
+
+ this.uploading = true
+
+ try {
+ // 获取token
+ if (!this.qiniuToken) {
+ const res = await getQiniuUploadToken()
+ this.qiniuToken = res.data?.token || res.data?.uploadToken || res.token || res.data
+ }
+
+ // 生成文件名
+ const key = `uploads/${Date.now()}_${Math.random().toString(36).slice(2)}.jpg`
+
+ // 上传
+ const result = await uploadToQiniu(this.selectedImage, this.qiniuToken, key)
+ const imageUrl = `https://api.ccttiot.com/${result.key}`
+
+ console.log('上传成功:', imageUrl)
+
+ } catch (error) {
+ console.error('上传失败:', error)
+ } finally {
+ this.uploading = false
+ }
+ }
+ }
+}
+```
+
+## 页面配置
+
+确保在 `pages.json` 中注册了相关页面:
+
+```json
+{
+ "pages": [
+ {
+ "path": "pages/image-upload/image-upload",
+ "style": {
+ "navigationBarTitleText": "图片上传",
+ "navigationStyle": "custom"
+ }
+ },
+ {
+ "path": "pages/image-upload-demo/image-upload-demo",
+ "style": {
+ "navigationBarTitleText": "图片上传演示",
+ "navigationStyle": "custom"
+ }
+ }
+ ]
+}
+```
+
+## 功能特点
+
+### ✅ 已实现功能
+- [x] 图片选择和预览
+- [x] 七牛云上传
+- [x] 上传进度显示
+- [x] 错误处理和提示
+- [x] 结果URL展示
+- [x] URL复制功能
+- [x] 多种使用方式
+- [x] 完整的示例代码
+
+### 🔧 技术特性
+- 支持相册和相机选择
+- 自动获取七牛云上传Token
+- 智能Token字段解析
+- 唯一文件名生成
+- 完整的错误处理
+- 响应式UI设计
+
+## 注意事项
+
+1. **Token获取**:确保后端API `/common/qiniu/uploadInfo` 正常工作
+2. **域名配置**:七牛云上传域名已配置为 `https://up-z2.qiniup.com`
+3. **图片域名**:图片访问域名配置为 `https://api.ccttiot.com`
+4. **文件格式**:支持 JPG、PNG 等常见图片格式
+5. **文件大小**:建议图片大小不超过 5MB
+
+## 调试
+
+如果遇到上传问题,可以:
+
+1. 检查控制台日志
+2. 验证Token获取是否成功
+3. 确认网络连接正常
+4. 检查七牛云配置是否正确
+
+## 示例页面
+
+访问以下页面查看完整示例:
+- `/pages/image-upload-demo/image-upload-demo` - 演示页面
+- `/examples/single-image-upload-usage.vue` - 使用示例
\ No newline at end of file
diff --git a/api/upload.js b/api/upload.js
new file mode 100644
index 0000000..3ebfb84
--- /dev/null
+++ b/api/upload.js
@@ -0,0 +1,44 @@
+import request from '@/utils/request.js'
+
+/**
+ * 获取七牛云上传token
+ * @returns {Promise}
+ */
+export function getQiniuUploadToken() {
+ return request({
+ url: '/common/qiniuToken',
+ method: 'GET'
+ })
+}
+
+/**
+ * 上传图片到七牛云
+ * @param {string} filePath - 文件路径
+ * @param {string} token - 七牛云上传token
+ * @param {string} key - 文件key
+ * @returns {Promise}
+ */
+export function uploadToQiniu(filePath, token, key) {
+ return new Promise((resolve, reject) => {
+ uni.uploadFile({
+ url: 'https://up-z2.qiniup.com',
+ filePath: filePath,
+ name: 'file',
+ formData: {
+ token: token,
+ key: key,
+ },
+ success: (res) => {
+ try {
+ const data = JSON.parse(res.data)
+ resolve(data)
+ } catch (error) {
+ reject(new Error('响应数据解析失败'))
+ }
+ },
+ fail: (error) => {
+ reject(error)
+ },
+ })
+ })
+}
\ No newline at end of file
diff --git a/examples/avatar-upload-example.vue b/examples/avatar-upload-example.vue
deleted file mode 100644
index 0519ecb..0000000
--- a/examples/avatar-upload-example.vue
+++ /dev/null
@@ -1 +0,0 @@
-
\ No newline at end of file
diff --git a/examples/single-image-upload-usage.vue b/examples/single-image-upload-usage.vue
new file mode 100644
index 0000000..05bfb37
--- /dev/null
+++ b/examples/single-image-upload-usage.vue
@@ -0,0 +1,318 @@
+
+
+
+
+
+
+ 方法一:跳转到上传页面
+ 跳转到专门的上传页面,上传完成后返回
+
+
+
+
+ 上传结果:
+
+ {{ method1Result }}
+
+
+
+
+
+ 方法二:内嵌上传组件
+ 在当前页面直接使用上传组件
+
+
+
+
+ 点击选择图片
+
+
+
+
+
+
+
+
+ 上传结果:
+
+ {{ method2Result }}
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/pages.json b/pages.json
index d31a12c..e193c88 100644
--- a/pages.json
+++ b/pages.json
@@ -1,17 +1,17 @@
{
"pages": [
- {
- "path": "pages/index/index",
- "style": {
+ {
+ "path": "pages/index/index",
+ "style": {
"navigationBarTitleText": "设备租赁",
"navigationStyle": "custom"
- }
- },
- {
- "path": "pages/login/login",
- "style": {
- "navigationStyle": "custom"
- }
+ }
+ },
+ {
+ "path": "pages/login/login",
+ "style": {
+ "navigationStyle": "custom"
+ }
},
{
"path": "pages/lease/lease",
@@ -80,22 +80,33 @@
"style": {
"navigationStyle": "custom"
}
- },
- {
- "path" : "pages/announcementList/announcementList",
- "style" :
- {
- "navigationBarTitleText": "公告列表"
- }
- },
- {
- "path" : "pages/announcementList/announcementDetail",
- "style" :
- {
- "navigationBarTitleText" : "公告详细"
- }
- }
- ],
+ },
+ {
+ "path": "pages/announcementList/announcementList",
+ "style": {
+ "navigationBarTitleText": "公告列表"
+ }
+ },
+ {
+ "path": "pages/announcementList/announcementDetail",
+ "style": {
+ "navigationBarTitleText": "公告详细"
+ }
+ },
+ {
+ "path": "pages/image-upload/image-upload",
+ "style": {
+ "navigationBarTitleText": "图片上传"
+ }
+ },
+ {
+ "path": "pages/image-upload-demo/image-upload-demo",
+ "style": {
+ "navigationBarTitleText": "图片上传演示",
+ "navigationStyle": "custom"
+ }
+ }
+ ],
"tabBar": {
"color": "#999999",
"selectedColor": "#ff6b6b",
@@ -121,11 +132,11 @@
}
]
},
- "globalStyle": {
- "navigationBarTextStyle": "black",
+ "globalStyle": {
+ "navigationBarTextStyle": "black",
"navigationBarTitleText": "设备租赁",
"navigationBarBackgroundColor": "#fff"
- },
+ },
"uniIdRouter": {},
"mp-weixin": {
"requiredPrivateInfos": [
diff --git a/pages/image-upload-demo/image-upload-demo.vue b/pages/image-upload-demo/image-upload-demo.vue
new file mode 100644
index 0000000..48f73a8
--- /dev/null
+++ b/pages/image-upload-demo/image-upload-demo.vue
@@ -0,0 +1,174 @@
+
+
+
+
+
+
+ 图片上传功能演示
+ 点击下方按钮跳转到图片上传页面
+
+
+
+
+
+
+
+ 上传结果
+
+ {{ uploadedImageUrl }}
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/pages/image-upload/image-upload.vue b/pages/image-upload/image-upload.vue
new file mode 100644
index 0000000..a084a7f
--- /dev/null
+++ b/pages/image-upload/image-upload.vue
@@ -0,0 +1,339 @@
+
+
+
+
+
+
+
+ 点击选择图片
+ 支持 JPG、PNG 格式
+
+
+
+
+
+
+
+
+
+
+ 上传状态:
+ 成功
+
+
+ 图片URL:
+ {{ uploadResult }}
+
+
+
+
+
+
+ {{ errorMessage }}
+
+
+
+
+
+
+
+