addNews.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. const uploadJS = require('../../mixin/upload.js')
  2. const { postAddNews } = require('./api/index')
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. form: {
  9. 'track_content': '',
  10. 'track_img_url': []
  11. },
  12. track_img_url_max: 6
  13. },
  14. /**
  15. * 生命周期函数--监听页面加载
  16. */
  17. onLoad(options) {
  18. },
  19. /**
  20. * 生命周期函数--监听页面初次渲染完成
  21. */
  22. onReady() {
  23. },
  24. /**
  25. * 生命周期函数--监听页面显示
  26. */
  27. onShow() {
  28. },
  29. /**
  30. * 生命周期函数--监听页面隐藏
  31. */
  32. onHide() {
  33. },
  34. /**
  35. * 生命周期函数--监听页面卸载
  36. */
  37. onUnload() {
  38. },
  39. ...uploadJS,
  40. uploadCallBack(res) {
  41. const temp = res.map(item => {
  42. return {
  43. 'url': item.url,
  44. 'formkey': item.formkey
  45. }
  46. })
  47. let tempForm = {}
  48. let formkey = ''
  49. if (temp.length > 0) {
  50. formkey = temp[0].formkey
  51. }
  52. switch (formkey) {
  53. case 'track_img_url':
  54. const formkeyData = this.data.form[formkey]
  55. tempForm[`form.${formkey}`] = formkeyData.concat(...temp)
  56. break
  57. default:
  58. }
  59. if (Object.keys(tempForm).length > 0) {
  60. this.setData(tempForm)
  61. }
  62. },
  63. handleInput(e) {
  64. const { value } = e.detail
  65. this.setData({
  66. 'form.track_content': value.trim()
  67. })
  68. },
  69. handleCancel() {
  70. wx.navigateBack({
  71. delta: 1
  72. })
  73. },
  74. getForm() {
  75. const { track_content, track_img_url } = this.data.form
  76. return {
  77. track_content: track_content,
  78. track_img_url: track_img_url.map(item => item.url)
  79. }
  80. },
  81. verify() {
  82. let errorList = []
  83. const { track_content, track_img_url } = this.data.form
  84. if (!track_content) {
  85. errorList.push('请写下此刻的想法')
  86. }
  87. if (track_img_url.length < 1) {
  88. errorList.push('请上传图片')
  89. }
  90. return errorList
  91. },
  92. async onSubmit() {
  93. const temp = this.getForm()
  94. const verifyList = this.verify()
  95. if (verifyList.length >= 2) {
  96. wx.showToast({
  97. title: verifyList[0],
  98. icon: 'none'
  99. })
  100. return
  101. }
  102. try {
  103. const { status, msg } = await postAddNews(temp)
  104. if (status) {
  105. const page = getCurrentPages()
  106. if (page.length >= 2 && page[page.length - 2].route === 'pages/news/news') {
  107. page[page.length - 2].refreshOrderList && page[page.length - 2].refreshOrderList()
  108. }
  109. wx.switchTab({
  110. url: '/pages/news/news'
  111. })
  112. } else {
  113. wx.showToast({
  114. title: msg,
  115. icon: 'none'
  116. })
  117. }
  118. } catch (err) {}
  119. }
  120. })