index.vue 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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. // 调用后端的图片上传接口
  109. const imageUrl = await api.feedback.uploadImage(filePath);
  110. return imageUrl;
  111. } catch (error) {
  112. throw error;
  113. }
  114. },
  115. // 上传所有图片
  116. async uploadAllImages() {
  117. const uploadedUrls = [];
  118. for (let i = 0; i < this.images.length; i++) {
  119. if (!this.uploadedImages[i]) {
  120. const url = await this.uploadSingleImage(this.images[i]);
  121. if (url) {
  122. this.uploadedImages[i] = url;
  123. }
  124. }
  125. uploadedUrls.push(this.uploadedImages[i]);
  126. }
  127. return uploadedUrls.filter(url => url);
  128. },
  129. // 提交反馈
  130. async submitFeedback() {
  131. if (!this.feedbackContent.trim()) {
  132. uni.showToast({
  133. title: '请输入反馈内容',
  134. icon: 'none'
  135. });
  136. return;
  137. }
  138. uni.showLoading({
  139. title: '提交中...'
  140. });
  141. try {
  142. // 上传图片
  143. const imageUrls = await this.uploadAllImages();
  144. // 获取当前用户ID
  145. const userInfo = uni.getStorageSync('userInfo');
  146. const userId = userInfo?.userId || uni.getStorageSync('userId');
  147. // 转换反馈类型为数字格式(1=意见建议,2=功能改进,3=bug报告)
  148. let feedbackTypeNum = 1;
  149. if (this.feedbackType === '功能改进') {
  150. feedbackTypeNum = 2;
  151. } else if (this.feedbackType === 'bug报告') {
  152. feedbackTypeNum = 3;
  153. }
  154. // 构造反馈数据,与后端接口字段匹配
  155. const feedbackData = {
  156. userId: userId,
  157. feedbackType: feedbackTypeNum,
  158. content: this.feedbackContent,
  159. imageUrls: imageUrls.join(',') // 将图片URL数组转换为逗号分隔的字符串
  160. };
  161. // 提交反馈
  162. await api.feedback.submit(feedbackData);
  163. uni.hideLoading();
  164. uni.showToast({
  165. title: '反馈提交成功',
  166. icon: 'success'
  167. });
  168. // 提交成功后返回上一页
  169. setTimeout(() => {
  170. this.goBack();
  171. }, 1500);
  172. } catch (error) {
  173. uni.hideLoading();
  174. let errorMsg = '反馈提交失败,请稍后重试';
  175. if (error && error.message) {
  176. errorMsg = error.message;
  177. } else if (error && error.errMsg) {
  178. errorMsg = error.errMsg;
  179. }
  180. uni.showToast({
  181. title: errorMsg,
  182. icon: 'none'
  183. });
  184. }
  185. }
  186. }
  187. };
  188. </script>
  189. <style lang="scss" scoped>
  190. .feedback-page {
  191. min-height: 100vh;
  192. background: #F5F5F5;
  193. }
  194. /* 页面标题 */
  195. .page-header {
  196. display: flex;
  197. align-items: center;
  198. justify-content: space-between;
  199. height: 88rpx;
  200. background: #FFFFFF;
  201. padding: 0 30rpx;
  202. box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
  203. position: sticky;
  204. top: 0;
  205. z-index: 99;
  206. }
  207. .header-left,
  208. .header-right {
  209. width: 60rpx;
  210. }
  211. .back-icon {
  212. font-size: 40rpx;
  213. color: #333333;
  214. }
  215. .header-title {
  216. font-size: 32rpx;
  217. font-weight: bold;
  218. color: #333333;
  219. }
  220. /* 反馈内容 */
  221. .feedback-content {
  222. padding: 30rpx;
  223. }
  224. .section {
  225. background: #FFFFFF;
  226. border-radius: 12rpx;
  227. padding: 30rpx;
  228. margin-bottom: 30rpx;
  229. }
  230. .section-title {
  231. font-size: 28rpx;
  232. font-weight: bold;
  233. color: #333333;
  234. margin-bottom: 20rpx;
  235. }
  236. /* 反馈类型 */
  237. .feedback-type {
  238. display: flex;
  239. gap: 20rpx;
  240. flex-wrap: wrap;
  241. }
  242. .type-item {
  243. padding: 16rpx 32rpx;
  244. background: #F5F5F5;
  245. border-radius: 20rpx;
  246. border: 2rpx solid #E0E0E0;
  247. transition: all 0.2s ease;
  248. }
  249. .type-item.active {
  250. background: #FFE5F1;
  251. border-color: #E91E63;
  252. }
  253. .type-text {
  254. font-size: 26rpx;
  255. color: #666666;
  256. }
  257. .type-item.active .type-text {
  258. color: #E91E63;
  259. font-weight: 500;
  260. }
  261. /* 反馈输入 */
  262. .feedback-input {
  263. width: 100%;
  264. min-height: 200rpx;
  265. font-size: 28rpx;
  266. color: #333333;
  267. border: 2rpx solid #E0E0E0;
  268. border-radius: 12rpx;
  269. padding: 20rpx;
  270. box-sizing: border-box;
  271. resize: none;
  272. }
  273. .feedback-input::placeholder {
  274. color: #CCCCCC;
  275. }
  276. .word-count {
  277. text-align: right;
  278. font-size: 24rpx;
  279. color: #999999;
  280. margin-top: 10rpx;
  281. }
  282. /* 图片上传 */
  283. .image-uploader {
  284. display: flex;
  285. gap: 20rpx;
  286. flex-wrap: wrap;
  287. }
  288. .upload-item {
  289. width: 200rpx;
  290. height: 200rpx;
  291. position: relative;
  292. border-radius: 12rpx;
  293. overflow: hidden;
  294. border: 2rpx solid #E0E0E0;
  295. }
  296. .uploaded-image {
  297. width: 100%;
  298. height: 100%;
  299. }
  300. .delete-icon {
  301. position: absolute;
  302. top: 10rpx;
  303. right: 10rpx;
  304. width: 40rpx;
  305. height: 40rpx;
  306. background: rgba(0, 0, 0, 0.5);
  307. color: #FFFFFF;
  308. border-radius: 50%;
  309. display: flex;
  310. align-items: center;
  311. justify-content: center;
  312. font-size: 30rpx;
  313. font-weight: bold;
  314. }
  315. .upload-btn {
  316. width: 200rpx;
  317. height: 200rpx;
  318. border: 2rpx dashed #E0E0E0;
  319. border-radius: 12rpx;
  320. display: flex;
  321. flex-direction: column;
  322. align-items: center;
  323. justify-content: center;
  324. background: #F9F9F9;
  325. }
  326. .upload-icon {
  327. font-size: 60rpx;
  328. color: #CCCCCC;
  329. margin-bottom: 10rpx;
  330. }
  331. .upload-text {
  332. font-size: 24rpx;
  333. color: #999999;
  334. }
  335. .upload-tip {
  336. font-size: 22rpx;
  337. color: #999999;
  338. margin-top: 10rpx;
  339. }
  340. /* 提交按钮 */
  341. .submit-section {
  342. background: #FFFFFF;
  343. border-radius: 12rpx;
  344. padding: 30rpx;
  345. margin: 0 30rpx 30rpx;
  346. }
  347. .submit-btn {
  348. width: 100%;
  349. height: 90rpx;
  350. background: #E91E63;
  351. color: #FFFFFF;
  352. font-size: 32rpx;
  353. font-weight: bold;
  354. border-radius: 45rpx;
  355. border: none;
  356. }
  357. .submit-btn:disabled {
  358. background: #CCCCCC;
  359. }
  360. .submit-btn::after {
  361. border: none;
  362. }
  363. </style>