index.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. // pages/feedback/index.js
  2. import http from '../../utils/http'
  3. import util from '../../utils/util'
  4. import api from '../../utils/api'
  5. import baseUrl from "../../utils/env"
  6. Page({
  7. /**
  8. * 页面的初始数据
  9. */
  10. data: {
  11. files: [],
  12. content: '',
  13. contact: '',
  14. imgs: []
  15. },
  16. /**
  17. * 生命周期函数--监听页面加载
  18. */
  19. onLoad: function (options) {
  20. },
  21. afterRead: function (e) {
  22. const { file } = e.detail;
  23. var files = this.data.files
  24. if (files.length >= 4) {
  25. util.error('最多上传4张图片')
  26. return false
  27. }
  28. if (Array.isArray(file)) {
  29. if (file.length < 4) {
  30. files = files.concat(file)
  31. } else {
  32. files = file
  33. }
  34. } else {
  35. file.path = ''
  36. files.push(file)
  37. }
  38. this.setData({
  39. files
  40. })
  41. },
  42. delete: function (e) {
  43. var files = this.data.files
  44. var index = e.detail.index
  45. files.splice(index, 1)
  46. this.setData({ files })
  47. },
  48. updateInput(e) {
  49. getApp().updateInput(this, e)
  50. },
  51. save: function () {
  52. if (!this.data.content) {
  53. util.error('反馈意见必填')
  54. return false
  55. }
  56. if (!this.data.contact) {
  57. util.error('联系方式必填')
  58. return false
  59. }
  60. var files = this.data.files
  61. var that = this
  62. if (files.length != this.data.imgs.length) {
  63. for (var i = 0; i < files.length; ++i) {
  64. util.uploadFile(files[i].url, function (res) {
  65. var imgs = that.data.imgs
  66. imgs.push(res.data.path)
  67. that.setData({ imgs })
  68. console.log(imgs)
  69. that.updateInfo()
  70. })
  71. }
  72. // files.forEach(item => {
  73. // wx.uploadFile({
  74. // filePath: item.url,
  75. // name: 'file',
  76. // url: baseUrl + 'uploadFile',
  77. // success: (res) => {
  78. // var data = JSON.parse(res.data)
  79. // if (data.code == 0) {
  80. // var imgs = that.data.imgs
  81. // imgs.push(res.data.path)
  82. // that.setData({ imgs })
  83. // console.log(imgs)
  84. // // that.updateInfo()
  85. // } else {
  86. // util.error('上传文件失败')
  87. // }
  88. // }
  89. // })
  90. // })
  91. } else {
  92. that.updateInfo()
  93. }
  94. },
  95. updateInfo: function () {
  96. var files = this.data.files
  97. var imgs = this.data.imgs
  98. if (imgs.length != files.length) return false;
  99. http({
  100. url: 'feedback/create',
  101. data: {
  102. content: this.data.content,
  103. contact: this.data.contact,
  104. imgs: imgs.join(',')
  105. },
  106. success: function (res) {
  107. if (res.code == 0) {
  108. util.success('操作成功')
  109. setTimeout(()=>{
  110. wx.navigateBack({
  111. delta: 1,
  112. })
  113. },1000)
  114. } else {
  115. util.error('操作失败')
  116. }
  117. }
  118. })
  119. },
  120. /**
  121. * 生命周期函数--监听页面初次渲染完成
  122. */
  123. onReady: function () {
  124. },
  125. /**
  126. * 生命周期函数--监听页面显示
  127. */
  128. onShow: function () {
  129. },
  130. /**
  131. * 生命周期函数--监听页面隐藏
  132. */
  133. onHide: function () {
  134. },
  135. /**
  136. * 生命周期函数--监听页面卸载
  137. */
  138. onUnload: function () {
  139. },
  140. /**
  141. * 页面相关事件处理函数--监听用户下拉动作
  142. */
  143. onPullDownRefresh: function () {
  144. },
  145. /**
  146. * 页面上拉触底事件的处理函数
  147. */
  148. onReachBottom: function () {
  149. },
  150. /**
  151. * 用户点击右上角分享
  152. */
  153. onShareAppMessage: function () {
  154. }
  155. })