feedback.vue 11 KB

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