contact.vue 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. <template>
  2. <view class="page-contact">
  3. <scroll-view class="scroll-view" scroll-y>
  4. <view class="content-wrapper">
  5. <!-- 反馈表单卡片 -->
  6. <view class="form-card">
  7. <view class="card-title">意见反馈</view>
  8. <!-- 文本输入框 -->
  9. <view class="form-item">
  10. <textarea
  11. class="feedback-textarea"
  12. v-model="feedbackText"
  13. placeholder="请输入您的意见或建议..."
  14. maxlength="500"
  15. :show-confirm-bar="false"
  16. ></textarea>
  17. <view class="char-count">{{ feedbackText.length }}/500</view>
  18. </view>
  19. <!-- 图片上传 -->
  20. <view class="form-item">
  21. <view class="upload-label">上传图片(最多6张)</view>
  22. <view class="image-upload-container">
  23. <view class="image-item" v-for="(image, index) in imageList" :key="index">
  24. <image class="upload-image" :src="image" mode="aspectFill"></image>
  25. <view class="delete-btn" @click="deleteImage(index)">×</view>
  26. </view>
  27. <view class="upload-btn" v-if="imageList.length < 6" @click="chooseImage">
  28. <text class="upload-icon">+</text>
  29. <text class="upload-text">添加图片</text>
  30. </view>
  31. </view>
  32. </view>
  33. </view>
  34. <!-- 提交按钮 -->
  35. <view class="submit-section">
  36. <button class="submit-btn" @click="handleSubmit" :disabled="submitting">
  37. {{ submitting ? '提交中...' : '提交反馈' }}
  38. </button>
  39. </view>
  40. <!-- 预留底部空间 -->
  41. <view class="bottom-safe-area"></view>
  42. </view>
  43. </scroll-view>
  44. </view>
  45. </template>
  46. <script setup>
  47. import { ref } from 'vue'
  48. import { onShow } from '@dcloudio/uni-app'
  49. import { submitFeedback, uploadFeedbackImage } from '../../utils/api.js'
  50. import { getToken } from '../../utils/auth.js'
  51. const feedbackText = ref('')
  52. const imageList = ref([])
  53. const submitting = ref(false)
  54. onShow(() => {
  55. uni.setNavigationBarTitle({ title: '联系我们' })
  56. })
  57. /**
  58. * 选择图片
  59. */
  60. const chooseImage = () => {
  61. const remainCount = 6 - imageList.value.length
  62. uni.chooseImage({
  63. count: remainCount,
  64. sizeType: ['compressed'],
  65. sourceType: ['album', 'camera'],
  66. success: (res) => {
  67. imageList.value = imageList.value.concat(res.tempFilePaths)
  68. }
  69. })
  70. }
  71. /**
  72. * 删除图片
  73. */
  74. const deleteImage = (index) => {
  75. imageList.value.splice(index, 1)
  76. }
  77. /**
  78. * 上传单张图片
  79. */
  80. const uploadImage = (filePath) => {
  81. return new Promise((resolve, reject) => {
  82. uni.uploadFile({
  83. url: uploadFeedbackImage.url,
  84. filePath: filePath,
  85. name: 'file',
  86. header: {
  87. 'Authorization': 'Bearer ' + getToken()
  88. },
  89. success: (res) => {
  90. try {
  91. const data = JSON.parse(res.data)
  92. if (data.code === 200) {
  93. resolve(data.data.url)
  94. } else {
  95. reject(data.message || '上传失败')
  96. }
  97. } catch (e) {
  98. reject('上传失败')
  99. }
  100. },
  101. fail: (err) => {
  102. reject(err.errMsg || '上传失败')
  103. }
  104. })
  105. })
  106. }
  107. /**
  108. * 提交反馈
  109. */
  110. const handleSubmit = async () => {
  111. if (!feedbackText.value.trim()) {
  112. uni.showToast({
  113. title: '请输入反馈内容',
  114. icon: 'none'
  115. })
  116. return
  117. }
  118. submitting.value = true
  119. try {
  120. // 上传所有图片
  121. const imageUrls = []
  122. if (imageList.value.length > 0) {
  123. uni.showLoading({ title: '上传图片中...' })
  124. for (const imagePath of imageList.value) {
  125. try {
  126. const url = await uploadImage(imagePath)
  127. imageUrls.push(url)
  128. } catch (err) {
  129. console.error('图片上传失败:', err)
  130. uni.hideLoading()
  131. uni.showToast({
  132. title: '图片上传失败',
  133. icon: 'none'
  134. })
  135. submitting.value = false
  136. return
  137. }
  138. }
  139. uni.hideLoading()
  140. }
  141. // 提交反馈
  142. uni.showLoading({ title: '提交中...' })
  143. const res = await submitFeedback({
  144. content: feedbackText.value.trim(),
  145. images: imageUrls.join(',')
  146. })
  147. uni.hideLoading()
  148. if (res.code === 200) {
  149. uni.showToast({
  150. title: '提交成功,感谢您的反馈',
  151. icon: 'success',
  152. duration: 2000
  153. })
  154. // 清空表单
  155. feedbackText.value = ''
  156. imageList.value = []
  157. // 延迟返回上一页
  158. setTimeout(() => {
  159. uni.navigateBack()
  160. }, 2000)
  161. } else {
  162. uni.showToast({
  163. title: res.message || '提交失败',
  164. icon: 'none'
  165. })
  166. }
  167. } catch (err) {
  168. uni.hideLoading()
  169. console.error('提交反馈失败:', err)
  170. uni.showToast({
  171. title: '提交失败,请稍后重试',
  172. icon: 'none'
  173. })
  174. } finally {
  175. submitting.value = false
  176. }
  177. }
  178. </script>
  179. <style>
  180. .page-contact {
  181. height: 100vh;
  182. display: flex;
  183. flex-direction: column;
  184. background: #f5f6fb;
  185. }
  186. .scroll-view {
  187. flex: 1;
  188. height: 0;
  189. }
  190. .content-wrapper {
  191. padding: 32rpx;
  192. background: #f5f6fb;
  193. min-height: 100%;
  194. }
  195. /* 表单卡片 */
  196. .form-card {
  197. background: #ffffff;
  198. border-radius: 24rpx;
  199. padding: 32rpx;
  200. margin-bottom: 32rpx;
  201. box-shadow: 0 16rpx 40rpx rgba(37, 52, 94, 0.08);
  202. }
  203. .card-title {
  204. font-size: 32rpx;
  205. font-weight: 600;
  206. color: #222222;
  207. margin-bottom: 32rpx;
  208. }
  209. .form-item {
  210. margin-bottom: 32rpx;
  211. }
  212. .form-item:last-child {
  213. margin-bottom: 0;
  214. }
  215. /* 文本输入框 */
  216. .feedback-textarea {
  217. width: 100%;
  218. min-height: 300rpx;
  219. padding: 24rpx;
  220. background: #f5f6fb;
  221. border-radius: 16rpx;
  222. font-size: 28rpx;
  223. color: #222222;
  224. line-height: 1.6;
  225. box-sizing: border-box;
  226. }
  227. .char-count {
  228. text-align: right;
  229. font-size: 24rpx;
  230. color: #9ca2b5;
  231. margin-top: 12rpx;
  232. }
  233. /* 图片上传 */
  234. .upload-label {
  235. font-size: 28rpx;
  236. color: #222222;
  237. margin-bottom: 16rpx;
  238. }
  239. .image-upload-container {
  240. display: flex;
  241. flex-wrap: wrap;
  242. gap: 16rpx;
  243. }
  244. .image-item {
  245. position: relative;
  246. width: 200rpx;
  247. height: 200rpx;
  248. }
  249. .upload-image {
  250. width: 100%;
  251. height: 100%;
  252. border-radius: 16rpx;
  253. background: #f5f6fb;
  254. }
  255. .delete-btn {
  256. position: absolute;
  257. top: -12rpx;
  258. right: -12rpx;
  259. width: 48rpx;
  260. height: 48rpx;
  261. background: #ff4444;
  262. color: #ffffff;
  263. border-radius: 50%;
  264. display: flex;
  265. align-items: center;
  266. justify-content: center;
  267. font-size: 36rpx;
  268. line-height: 1;
  269. box-shadow: 0 4rpx 12rpx rgba(255, 68, 68, 0.3);
  270. }
  271. .upload-btn {
  272. width: 200rpx;
  273. height: 200rpx;
  274. background: #f5f6fb;
  275. border-radius: 16rpx;
  276. display: flex;
  277. flex-direction: column;
  278. align-items: center;
  279. justify-content: center;
  280. border: 2rpx dashed #d0d4e0;
  281. }
  282. .upload-icon {
  283. font-size: 64rpx;
  284. color: #9ca2b5;
  285. line-height: 1;
  286. margin-bottom: 8rpx;
  287. }
  288. .upload-text {
  289. font-size: 24rpx;
  290. color: #9ca2b5;
  291. }
  292. /* 提交按钮 */
  293. .submit-section {
  294. margin-bottom: 32rpx;
  295. }
  296. .submit-btn {
  297. width: 100%;
  298. background: #5d55e8;
  299. color: #ffffff;
  300. border-radius: 16rpx;
  301. padding: 28rpx 0;
  302. text-align: center;
  303. font-size: 30rpx;
  304. font-weight: 600;
  305. box-shadow: 0 12rpx 24rpx rgba(93, 85, 232, 0.3);
  306. border: none;
  307. line-height: 1;
  308. }
  309. .submit-btn::after {
  310. border: none;
  311. }
  312. .submit-btn:active {
  313. opacity: 0.8;
  314. }
  315. .submit-btn[disabled] {
  316. opacity: 0.6;
  317. }
  318. .bottom-safe-area {
  319. height: 80rpx;
  320. }
  321. </style>