index.js 2.6 KB

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