feedback.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. <template>
  2. <view>
  3. <view class="container" v-if="formInfo&&formInfo.length>0">
  4. <view class="list-item title">投诉原因:{{ text }}</view>
  5. </view>
  6. <view class="formbox" v-if="formInfo&&formInfo.length>0">
  7. <u-form labelPosition="left" labelWidth='80' :model="formdata" :rules="rules" ref="uForm" errorType="toast">
  8. <u-form-item :required="item.isRequire == 1" :label="item.name" :prop="item.desc"
  9. v-for="(item,i) in formInfo" :key="i">
  10. <template v-if="item.type=='文本框'">
  11. <u-input v-model="formdata[item.desc]" border="none" :clearable="true"
  12. :placeholder="'请填写'+item.name"></u-input>
  13. </template>
  14. <template v-if="item.type=='图片'">
  15. <view class="imgitem">
  16. <u-upload :key="imgdata[item.desc].key" :fileList="imgdata[item.desc]" @afterRead="afterRead" @delete="deletePic"
  17. :name="item.desc" multiple :maxCount="9">
  18. </u-upload>
  19. </view>
  20. </template>
  21. </u-form-item>
  22. </u-form>
  23. <view class="footer-btn">
  24. <button class="submit-btn" @click="submit">提交</button>
  25. <button class="submit-btn back-btn" @click="goBack">返回</button>
  26. </view>
  27. </view>
  28. <view class="container" v-else>
  29. <view class="list-item title">请选择投诉原因</view>
  30. <view class="list-item" v-for="(item, index) in feedbackItems" :key="index"
  31. @click="handleClick(item,index)">
  32. <view>{{ item.name }}</view>
  33. <uni-icons type="right" size="20" color="rgba(0,0,0,.3)" v-if="formInfo.length==0"></uni-icons>
  34. </view>
  35. <view class="list-item" v-if="pageIndex!=0&&formInfo.length==0" @click="goBack">
  36. 返回上一层
  37. </view>
  38. </view>
  39. </view>
  40. </template>
  41. <script>
  42. import { templateList, complaint } from "@/api/user.js"
  43. export default {
  44. data() {
  45. return {
  46. statusBarHeight: uni.getSystemInfoSync().statusBarHeight,
  47. menuButtonH: 45,
  48. pageIndex: 0,
  49. list: [],
  50. feedbackItems: [],
  51. userId: '',
  52. courseId: '',
  53. videoId: '',
  54. formInfo: [],
  55. formdata: {},
  56. imgdata: {},
  57. rules: {},
  58. text: '',
  59. templateId: 0,
  60. user: {},
  61. isLastChild: 0
  62. };
  63. },
  64. onLoad(option) {
  65. if(!this.$isLogin()){
  66. uni.navigateTo({
  67. url: '/pages/auth/loginIndex'
  68. })
  69. return;
  70. }
  71. this.user = this.$getUserInfo()
  72. this.getList()
  73. },
  74. onReady() {
  75. //onReady 为uni-app支持的生命周期之一
  76. // this.$refs.uForm.setRules(this.rules)
  77. },
  78. methods: {
  79. goBack() {
  80. // 返回上一层逻辑
  81. if (this.pageIndex == 0) {
  82. uni.navigateBack();
  83. } else {
  84. this.pageIndex--;
  85. this.formInfo = []
  86. this.rules = {}
  87. this.formdata = {}
  88. this.imgdata = {}
  89. if (this.isLastChild == 1) {
  90. this.isLastChild = 0
  91. } else {
  92. if (this.pageIndex == 0) {
  93. this.feedbackItems = this.list
  94. this.templateId = 0
  95. } else {
  96. const list = this.findGrandparentOrAllData(this.list, this.templateId)
  97. this.feedbackItems = list.children
  98. this.templateId = list.id
  99. }
  100. }
  101. }
  102. },
  103. findGrandparentOrAllData(data, targetId) {
  104. // 递归函数,用于查找目标节点的父级节点
  105. function findParent(node, targetId) {
  106. if (!node || !node.children) return null;
  107. for (let child of node.children) {
  108. if (child.id === targetId) {
  109. return node; // 找到目标节点的父级节点
  110. }
  111. const result = findParent(child, targetId); // 递归查找子节点
  112. if (result) return result;
  113. }
  114. return null;
  115. }
  116. // 遍历顶层节点,查找目标节点的父级和祖父级节点
  117. for (let root of data) {
  118. if (root.id === targetId) {
  119. return data; // 如果目标节点是顶层节点,返回所有数据
  120. }
  121. const parent = findParent(root, targetId); // 查找目标节点的父级节点
  122. if (parent) {
  123. const grandparent = findParent(root, parent.id); // 查找父级节点的父级节点
  124. return grandparent || data; // 如果找到祖父节点返回祖父节点,否则返回所有数据
  125. }
  126. }
  127. return data; // 如果没有找到目标节点,返回所有数据
  128. },
  129. handleClick(item, index) {
  130. if (this.isLastChild == 1) return
  131. if (this.pageIndex >= 0) {
  132. this.pageIndex++
  133. let children = this.feedbackItems[index].children || [];
  134. this.templateId = this.feedbackItems[index].id
  135. this.formInfo = []
  136. this.rules = {}
  137. this.formdata = {}
  138. this.imgdata = {}
  139. this.text = this.feedbackItems[index].name
  140. if (children.length > 0) {
  141. this.isLastChild = 0
  142. this.feedbackItems = children
  143. this.templateId = this.feedbackItems[0].id
  144. } else {
  145. this.isLastChild = 1
  146. if (this.feedbackItems[index].description) {
  147. this.formInfo = JSON.parse(this.feedbackItems[index].description)
  148. const rules = {};
  149. const formdata = {};
  150. // 遍历description中的每个对象
  151. this.formInfo.forEach(descObj => {
  152. const {
  153. isRequire,
  154. desc,
  155. name,
  156. type
  157. } = descObj;
  158. formdata[desc] = ""
  159. if (type == '图片') {
  160. this.imgdata[desc] = []
  161. this.imgdata[desc].key = 1
  162. this.$forceUpdate()
  163. }
  164. // 如果isRequire为"1",则添加到rules中
  165. if (isRequire == 1) {
  166. rules[desc] = [{
  167. required: true,
  168. message: name + '不能为空',
  169. trigger: ["change", "blur"]
  170. }];
  171. }
  172. });
  173. this.rules = rules
  174. this.formdata = formdata
  175. setTimeout(() => {
  176. this.$refs.uForm.setRules(this.rules)
  177. }, 200)
  178. } else {
  179. this.formInfo = []
  180. this.rules = {}
  181. this.formdata = {}
  182. this.imgdata = {}
  183. this.$forceUpdate()
  184. }
  185. }
  186. }
  187. },
  188. getList() {
  189. templateList().then(res => {
  190. if (res.code == 200) {
  191. this.list = res.data
  192. this.pageIndex = 0
  193. this.feedbackItems = this.list
  194. } else {
  195. this.list = []
  196. }
  197. })
  198. },
  199. submit() {
  200. const imgs = this.convertArrayToString(this.imgdata)
  201. this.formdata = {
  202. ...this.formdata,
  203. ...imgs
  204. };
  205. this.$refs.uForm.validate().then(res => {
  206. if (res) {
  207. const param = {
  208. userId: this.user.userId,
  209. userName: this.user.nickName,
  210. templateId: this.templateId,
  211. ...this.formdata
  212. }
  213. complaint(param).then(res => {
  214. if (res.code == 200) {
  215. uni.showModal({
  216. title: '',
  217. content: '我们已收到您的反馈,谢谢',
  218. showCancel: false,
  219. success: (res) => {
  220. this.formInfo = []
  221. this.rules = {}
  222. this.formdata = {}
  223. this.imgdata = {}
  224. this.text = ''
  225. this.templateId = 0
  226. this.isLastChild = 0
  227. this.pageIndex = 0
  228. this.feedbackItems = this.list
  229. }
  230. });
  231. } else {
  232. uni.showToast({
  233. title: res.msg,
  234. icon: 'none'
  235. })
  236. }
  237. })
  238. }
  239. })
  240. },
  241. convertArrayToString(obj) {
  242. const result = {};
  243. for (const key in obj) {
  244. if (obj.hasOwnProperty(key)) {
  245. const value = obj[key];
  246. // 如果值是数组,则将其转换为逗号分隔的字符串
  247. if (Array.isArray(value)) {
  248. result[key] = value.filter(item => item.status == 'success').map(it => it.url).join(
  249. ","); // 使用逗号分隔数组元素
  250. } else {
  251. result[key] = value; // 保持原样
  252. }
  253. }
  254. }
  255. return result;
  256. },
  257. deletePic(event) {
  258. const fieldName = event.name;
  259. const index = event.index;
  260. if (this.imgdata[fieldName] && Array.isArray(this.imgdata[fieldName])) {
  261. this.imgdata[fieldName].splice(index, 1);
  262. }
  263. this.$forceUpdate()
  264. this.imgdata[fieldName].key --
  265. },
  266. async afterRead(event) {
  267. const fieldName = event.name;
  268. if (!this.imgdata[fieldName]) {
  269. this.imgdata[fieldName] = [];
  270. this.imgdata[fieldName].key = 1
  271. }
  272. let lists = Array.isArray(event.file) ? event.file : [event.file];
  273. for (let i = 0; i < lists.length; i++) {
  274. this.imgdata[fieldName].push({
  275. ...lists[i],
  276. status: 'uploading',
  277. message: '上传中...'
  278. });
  279. this.$forceUpdate()
  280. this.imgdata[fieldName].key ++
  281. // 调用上传方法
  282. const result = await this.uploadFilePromise(lists[i].url);
  283. if (result.code == 200) {
  284. this.imgdata[fieldName][this.imgdata[fieldName].length - 1] = {
  285. ...this.imgdata[fieldName][this.imgdata[fieldName].length - 1],
  286. url: result.url,
  287. status: 'success',
  288. message: ''
  289. };
  290. this.$forceUpdate()
  291. this.imgdata[fieldName].key ++
  292. } else {
  293. this.imgdata[fieldName][this.imgdata[fieldName].length - 1] = {
  294. ...this.imgdata[fieldName][this.imgdata[fieldName].length - 1],
  295. status: 'fail',
  296. message: '上传失败'
  297. };
  298. this.$forceUpdate()
  299. this.imgdata[fieldName].key ++
  300. uni.showToast({
  301. title: '上传失败',
  302. icon: 'error'
  303. });
  304. }
  305. }
  306. },
  307. uploadFilePromise(url) {
  308. return new Promise((resolve, reject) => {
  309. let a = uni.uploadFile({
  310. url: uni.getStorageSync('requestPath') + '/app/common/uploadOSS', // 仅为示例,非真实的接口地址
  311. filePath: url,
  312. name: 'file',
  313. formData: {
  314. user: 'test'
  315. },
  316. success: (res) => {
  317. resolve(JSON.parse(res.data))
  318. },
  319. fail: (err) => {
  320. uni.showToast({
  321. title: '上传失败',
  322. icon: 'error'
  323. })
  324. }
  325. });
  326. })
  327. }
  328. }
  329. };
  330. </script>
  331. <style scoped lang="scss">
  332. .formbox {
  333. background-color: #fff;
  334. padding: 0 30rpx;
  335. }
  336. .footer-btn {
  337. width: 100%;
  338. box-sizing: border-box;
  339. padding: 32rpx;
  340. display: flex;
  341. flex-direction: column;
  342. align-items: center;
  343. }
  344. .back-btn {
  345. margin-top: 40rpx;
  346. color: #2583EB !important;
  347. background: #fff !important;
  348. border: 1rpx solid #2583EB;
  349. }
  350. .submit-btn {
  351. width: 100%;
  352. height: 88rpx;
  353. line-height: 88rpx;
  354. text-align: center;
  355. font-size: 30rpx;
  356. font-family: PingFang SC;
  357. font-weight: bold;
  358. color: #FFFFFF;
  359. background: #2583EB;
  360. border-radius: 44rpx;
  361. border: 1rpx solid #2583EB;
  362. &::after {
  363. border: none;
  364. }
  365. }
  366. .arrow-left {
  367. position: absolute;
  368. left: 24rpx;
  369. height: 88rpx;
  370. display: flex;
  371. align-items: center;
  372. justify-content: center;
  373. overflow: hidden;
  374. }
  375. .list-item {
  376. background-color: #fff;
  377. padding: 24rpx;
  378. border-bottom: 1rpx solid #f4f4f4;
  379. font-size: 15px;
  380. color: #333;
  381. display: flex;
  382. align-items: center;
  383. justify-content: space-between;
  384. }
  385. .title {
  386. color: rgba(0, 0, 0, .5);
  387. background-color: transparent;
  388. border-top: 1rpx solid #f4f4f4;
  389. }
  390. .imgitem {
  391. display: flex;
  392. align-items: center;
  393. justify-content: flex-start;
  394. .icon {
  395. min-width: 30rpx;
  396. margin-right: 15rpx;
  397. width: 30rpx;
  398. height: 30rpx;
  399. }
  400. }
  401. </style>