index.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. file.path = ''
  29. files.push(file)
  30. this.setData({
  31. files
  32. })
  33. },
  34. delete: function (e) {
  35. var files = this.data.files
  36. var index = e.detail.index
  37. files.splice(index, 1)
  38. this.setData({ files })
  39. },
  40. updateInput(e) {
  41. getApp().updateInput(this, e)
  42. },
  43. save: function () {
  44. if (!this.data.content) {
  45. util.error('反馈意见必填')
  46. return false
  47. }
  48. if (!this.data.contact) {
  49. util.error('联系方式必填')
  50. return false
  51. }
  52. var files = this.data.files
  53. var that = this
  54. if (files.length != this.data.imgs.length) {
  55. for (var i = 0; i < files.length; ++i) {
  56. util.uploadFile(files[i].url, function (res) {
  57. var imgs = that.data.imgs
  58. imgs.push(res.data.path)
  59. that.setData({ imgs })
  60. console.log(imgs)
  61. that.updateInfo()
  62. })
  63. }
  64. // files.forEach(item => {
  65. // wx.uploadFile({
  66. // filePath: item.url,
  67. // name: 'file',
  68. // url: baseUrl + 'uploadFile',
  69. // success: (res) => {
  70. // var data = JSON.parse(res.data)
  71. // if (data.code == 0) {
  72. // var imgs = that.data.imgs
  73. // imgs.push(res.data.path)
  74. // that.setData({ imgs })
  75. // console.log(imgs)
  76. // // that.updateInfo()
  77. // } else {
  78. // util.error('上传文件失败')
  79. // }
  80. // }
  81. // })
  82. // })
  83. } else {
  84. that.updateInfo()
  85. }
  86. },
  87. updateInfo: function () {
  88. var files = this.data.files
  89. var imgs = this.data.imgs
  90. if (imgs.length != files.length) return false;
  91. http({
  92. url: 'feedback/create',
  93. data: {
  94. content: this.data.content,
  95. contact: this.data.contact,
  96. imgs: imgs.join(',')
  97. },
  98. success: function (res) {
  99. if (res.code == 0) {
  100. util.success('操作成功')
  101. } else {
  102. util.error('操作失败')
  103. }
  104. }
  105. })
  106. },
  107. /**
  108. * 生命周期函数--监听页面初次渲染完成
  109. */
  110. onReady: function () {
  111. },
  112. /**
  113. * 生命周期函数--监听页面显示
  114. */
  115. onShow: function () {
  116. },
  117. /**
  118. * 生命周期函数--监听页面隐藏
  119. */
  120. onHide: function () {
  121. },
  122. /**
  123. * 生命周期函数--监听页面卸载
  124. */
  125. onUnload: function () {
  126. },
  127. /**
  128. * 页面相关事件处理函数--监听用户下拉动作
  129. */
  130. onPullDownRefresh: function () {
  131. },
  132. /**
  133. * 页面上拉触底事件的处理函数
  134. */
  135. onReachBottom: function () {
  136. },
  137. /**
  138. * 用户点击右上角分享
  139. */
  140. onShareAppMessage: function () {
  141. }
  142. })