index.vue 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. <template>
  2. <view class="feedback-page">
  3. <!-- 页面标题 -->
  4. <view class="page-header">
  5. <view class="header-left" @click="goBack">
  6. <text class="back-icon">←</text>
  7. </view>
  8. <view class="header-title">用户反馈</view>
  9. <view class="header-right"></view>
  10. </view>
  11. <!-- 反馈内容 -->
  12. <view class="feedback-content">
  13. <view class="section">
  14. <view class="section-title">反馈类型</view>
  15. <view class="feedback-type">
  16. <view class="type-item"
  17. :class="{ active: feedbackType === '意见建议' }"
  18. @click="feedbackType = '意见建议'">
  19. <text class="type-text">意见建议</text>
  20. </view>
  21. <view class="type-item"
  22. :class="{ active: feedbackType === '功能改进' }"
  23. @click="feedbackType = '功能改进'">
  24. <text class="type-text">功能改进</text>
  25. </view>
  26. <view class="type-item"
  27. :class="{ active: feedbackType === 'bug报告' }"
  28. @click="feedbackType = 'bug报告'">
  29. <text class="type-text">bug报告</text>
  30. </view>
  31. </view>
  32. </view>
  33. <view class="section">
  34. <view class="section-title">反馈内容</view>
  35. <textarea
  36. class="feedback-input"
  37. placeholder="请详细描述您的问题或建议..."
  38. v-model="feedbackContent"
  39. maxlength="500"
  40. auto-height
  41. ></textarea>
  42. <view class="word-count">{{ feedbackContent.length }}/500</view>
  43. </view>
  44. <view class="section">
  45. <view class="section-title">上传图片(可选)</view>
  46. <view class="image-uploader">
  47. <view class="upload-item" v-for="(image, index) in images" :key="index">
  48. <image class="uploaded-image" :src="image" mode="aspectFill"></image>
  49. <view class="delete-icon" @click="deleteImage(index)">×</view>
  50. </view>
  51. <view class="upload-btn" @click="chooseImage" v-if="images.length < 3">
  52. <text class="upload-icon">+</text>
  53. <text class="upload-text">添加图片</text>
  54. </view>
  55. </view>
  56. <view class="upload-tip">最多可上传3张图片</view>
  57. </view>
  58. </view>
  59. <!-- 提交按钮 -->
  60. <view class="submit-section">
  61. <button class="submit-btn" @click="submitFeedback" :disabled="!feedbackContent.trim()">
  62. 提交反馈
  63. </button>
  64. </view>
  65. </view>
  66. </template>
  67. <script>
  68. import api from '@/utils/api.js';
  69. export default {
  70. data() {
  71. return {
  72. feedbackType: '意见建议',
  73. feedbackContent: '',
  74. images: [],
  75. uploadedImages: []
  76. };
  77. },
  78. onLoad() {
  79. // 确保 __route__ 属性存在,避免渲染错误
  80. if (!this.__route__) {
  81. this.__route__ = '/pages/feedback/index';
  82. }
  83. },
  84. methods: {
  85. // 返回上一页
  86. goBack() {
  87. uni.navigateBack();
  88. },
  89. // 选择图片
  90. chooseImage() {
  91. uni.chooseImage({
  92. count: 3 - this.images.length,
  93. sizeType: ['compressed'],
  94. sourceType: ['album', 'camera'],
  95. success: (res) => {
  96. this.images = [...this.images, ...res.tempFilePaths];
  97. }
  98. });
  99. },
  100. // 删除图片
  101. deleteImage(index) {
  102. this.images.splice(index, 1);
  103. this.uploadedImages.splice(index, 1);
  104. },
  105. // 上传单张图片
  106. async uploadSingleImage(filePath) {
  107. try {
  108. console.log('上传图片:', filePath);
  109. // 调用后端的图片上传接口
  110. const imageUrl = await api.feedback.uploadImage(filePath);
  111. return imageUrl;
  112. } catch (error) {
  113. console.error('上传图片失败:', error);
  114. throw error;
  115. }
  116. },
  117. // 上传所有图片
  118. async uploadAllImages() {
  119. const uploadedUrls = [];
  120. for (let i = 0; i < this.images.length; i++) {
  121. if (!this.uploadedImages[i]) {
  122. const url = await this.uploadSingleImage(this.images[i]);
  123. if (url) {
  124. this.uploadedImages[i] = url;
  125. }
  126. }
  127. uploadedUrls.push(this.uploadedImages[i]);
  128. }
  129. return uploadedUrls.filter(url => url);
  130. },
  131. // 提交反馈
  132. async submitFeedback() {
  133. if (!this.feedbackContent.trim()) {
  134. uni.showToast({
  135. title: '请输入反馈内容',
  136. icon: 'none'
  137. });
  138. return;
  139. }
  140. uni.showLoading({
  141. title: '提交中...'
  142. });
  143. try {
  144. // 上传图片
  145. const imageUrls = await this.uploadAllImages();
  146. // 获取当前用户ID
  147. const userInfo = uni.getStorageSync('userInfo');
  148. const userId = userInfo?.userId || uni.getStorageSync('userId');
  149. // 转换反馈类型为数字格式(1=意见建议,2=功能改进,3=bug报告)
  150. let feedbackTypeNum = 1;
  151. if (this.feedbackType === '功能改进') {
  152. feedbackTypeNum = 2;
  153. } else if (this.feedbackType === 'bug报告') {
  154. feedbackTypeNum = 3;
  155. }
  156. // 构造反馈数据,与后端接口字段匹配
  157. const feedbackData = {
  158. userId: userId,
  159. feedbackType: feedbackTypeNum,
  160. content: this.feedbackContent,
  161. imageUrls: imageUrls.join(',') // 将图片URL数组转换为逗号分隔的字符串
  162. };
  163. // 提交反馈
  164. await api.feedback.submit(feedbackData);
  165. uni.hideLoading();
  166. uni.showToast({
  167. title: '反馈提交成功',
  168. icon: 'success'
  169. });
  170. // 提交成功后返回上一页
  171. setTimeout(() => {
  172. this.goBack();
  173. }, 1500);
  174. } catch (error) {
  175. uni.hideLoading();
  176. console.error('提交反馈失败:', error);
  177. let errorMsg = '反馈提交失败,请稍后重试';
  178. if (error && error.message) {
  179. errorMsg = error.message;
  180. } else if (error && error.errMsg) {
  181. errorMsg = error.errMsg;
  182. }
  183. uni.showToast({
  184. title: errorMsg,
  185. icon: 'none'
  186. });
  187. }
  188. }
  189. }
  190. };
  191. </script>
  192. <style lang="scss" scoped>
  193. .feedback-page {
  194. min-height: 100vh;
  195. background: #F5F5F5;
  196. }
  197. /* 页面标题 */
  198. .page-header {
  199. display: flex;
  200. align-items: center;
  201. justify-content: space-between;
  202. height: 88rpx;
  203. background: #FFFFFF;
  204. padding: 0 30rpx;
  205. box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
  206. position: sticky;
  207. top: 0;
  208. z-index: 99;
  209. }
  210. .header-left,
  211. .header-right {
  212. width: 60rpx;
  213. }
  214. .back-icon {
  215. font-size: 40rpx;
  216. color: #333333;
  217. }
  218. .header-title {
  219. font-size: 32rpx;
  220. font-weight: bold;
  221. color: #333333;
  222. }
  223. /* 反馈内容 */
  224. .feedback-content {
  225. padding: 30rpx;
  226. }
  227. .section {
  228. background: #FFFFFF;
  229. border-radius: 12rpx;
  230. padding: 30rpx;
  231. margin-bottom: 30rpx;
  232. }
  233. .section-title {
  234. font-size: 28rpx;
  235. font-weight: bold;
  236. color: #333333;
  237. margin-bottom: 20rpx;
  238. }
  239. /* 反馈类型 */
  240. .feedback-type {
  241. display: flex;
  242. gap: 20rpx;
  243. flex-wrap: wrap;
  244. }
  245. .type-item {
  246. padding: 16rpx 32rpx;
  247. background: #F5F5F5;
  248. border-radius: 20rpx;
  249. border: 2rpx solid #E0E0E0;
  250. transition: all 0.2s ease;
  251. }
  252. .type-item.active {
  253. background: #FFE5F1;
  254. border-color: #E91E63;
  255. }
  256. .type-text {
  257. font-size: 26rpx;
  258. color: #666666;
  259. }
  260. .type-item.active .type-text {
  261. color: #E91E63;
  262. font-weight: 500;
  263. }
  264. /* 反馈输入 */
  265. .feedback-input {
  266. width: 100%;
  267. min-height: 200rpx;
  268. font-size: 28rpx;
  269. color: #333333;
  270. border: 2rpx solid #E0E0E0;
  271. border-radius: 12rpx;
  272. padding: 20rpx;
  273. box-sizing: border-box;
  274. resize: none;
  275. }
  276. .feedback-input::placeholder {
  277. color: #CCCCCC;
  278. }
  279. .word-count {
  280. text-align: right;
  281. font-size: 24rpx;
  282. color: #999999;
  283. margin-top: 10rpx;
  284. }
  285. /* 图片上传 */
  286. .image-uploader {
  287. display: flex;
  288. gap: 20rpx;
  289. flex-wrap: wrap;
  290. }
  291. .upload-item {
  292. width: 200rpx;
  293. height: 200rpx;
  294. position: relative;
  295. border-radius: 12rpx;
  296. overflow: hidden;
  297. border: 2rpx solid #E0E0E0;
  298. }
  299. .uploaded-image {
  300. width: 100%;
  301. height: 100%;
  302. }
  303. .delete-icon {
  304. position: absolute;
  305. top: 10rpx;
  306. right: 10rpx;
  307. width: 40rpx;
  308. height: 40rpx;
  309. background: rgba(0, 0, 0, 0.5);
  310. color: #FFFFFF;
  311. border-radius: 50%;
  312. display: flex;
  313. align-items: center;
  314. justify-content: center;
  315. font-size: 30rpx;
  316. font-weight: bold;
  317. }
  318. .upload-btn {
  319. width: 200rpx;
  320. height: 200rpx;
  321. border: 2rpx dashed #E0E0E0;
  322. border-radius: 12rpx;
  323. display: flex;
  324. flex-direction: column;
  325. align-items: center;
  326. justify-content: center;
  327. background: #F9F9F9;
  328. }
  329. .upload-icon {
  330. font-size: 60rpx;
  331. color: #CCCCCC;
  332. margin-bottom: 10rpx;
  333. }
  334. .upload-text {
  335. font-size: 24rpx;
  336. color: #999999;
  337. }
  338. .upload-tip {
  339. font-size: 22rpx;
  340. color: #999999;
  341. margin-top: 10rpx;
  342. }
  343. /* 提交按钮 */
  344. .submit-section {
  345. padding: 30rpx;
  346. background: #FFFFFF;
  347. margin-top: 30rpx;
  348. }
  349. .submit-btn {
  350. width: 100%;
  351. height: 90rpx;
  352. background: #E91E63;
  353. color: #FFFFFF;
  354. font-size: 32rpx;
  355. font-weight: bold;
  356. border-radius: 45rpx;
  357. border: none;
  358. }
  359. .submit-btn:disabled {
  360. background: #CCCCCC;
  361. }
  362. .submit-btn::after {
  363. border: none;
  364. }
  365. </style>