messagePublish.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. const util = require('../../utils/util.js')
  2. Page({
  3. /**
  4. * 页面的初始数据
  5. */
  6. data: {
  7. publisherId: '',
  8. messageId: '',
  9. messageInfo: {},
  10. currentTab: 0,
  11. winWidth: 0,
  12. winHeight: 0,
  13. types1: [{
  14. value: '志愿活动',
  15. checked: false
  16. }, {
  17. value: '文艺活动',
  18. checked: false
  19. }, {
  20. value: '体育活动',
  21. checked: false
  22. }, {
  23. value: '学术活动',
  24. checked: false
  25. }, {
  26. value: '素质拓展',
  27. checked: false
  28. }, {
  29. value: '精品课程',
  30. checked: false
  31. }, {
  32. value: '其他活动',
  33. checked: false
  34. }],
  35. types2: [{
  36. value: '二课分',
  37. checked: false
  38. }, {
  39. value: '三课分',
  40. checked: false
  41. }, {
  42. value: '四课分',
  43. checked: false
  44. }, {
  45. value: '勤工助学',
  46. checked: false
  47. }, {
  48. value: '志愿者小时数',
  49. checked: false
  50. }, {
  51. value: '综素',
  52. checked: false
  53. }],
  54. types3: [{
  55. value: '水电通知',
  56. checked: false
  57. }, {
  58. value: '缴费通知',
  59. checked: false
  60. }, {
  61. value: '假期通知',
  62. checked: false
  63. }, {
  64. value: '后勤通知',
  65. checked: false
  66. }, {
  67. value: '图书馆通知',
  68. checked: false
  69. }, {
  70. value: '选课通知',
  71. checked: false
  72. }, {
  73. value: '竞赛通知',
  74. checked: false
  75. }, {
  76. value: '考试通知',
  77. checked: false
  78. }, {
  79. value: '其他通知',
  80. checked: false
  81. }]
  82. },
  83. switch1: function (e) {
  84. this.setData({
  85. currentTab: 1
  86. })
  87. },
  88. switch2: function (e) {
  89. this.setData({
  90. currentTab: 2
  91. })
  92. },
  93. switch3: function (e) {
  94. this.setData({
  95. currentTab: 3
  96. })
  97. },
  98. switch4: function (e) {
  99. const value = e.detail.value
  100. if (this.data.currentTab === 2) {
  101. value.type = ['纳新']
  102. }
  103. if (value.name === '' || value.type.length === 0 || this.data.detail === '') {
  104. wx.showToast({
  105. title: '请确认信息填写完整',
  106. icon: 'none'
  107. })
  108. return
  109. }
  110. wx.showLoading({
  111. title: '上传图片'
  112. })
  113. const arr = []
  114. if (value.poster.length !== 0 && value.poster[0].substr(0, 8) !== 'cloud://') {
  115. arr.push(wx.cloud.uploadFile({
  116. cloudPath: `messagePoster/${util.randomString(16)}.jpg`,
  117. filePath: value.poster[0]
  118. }))
  119. }
  120. for (let i = 0; i < value.photo.length; i++) {
  121. if (value.photo[i].substr(0, 8) !== 'cloud://') {
  122. arr.push(wx.cloud.uploadFile({
  123. cloudPath: `messagePhoto/${util.randomString(16)}.jpg`,
  124. filePath: value.photo[i]
  125. }))
  126. }
  127. }
  128. Promise.all(arr).then(res => {
  129. wx.hideLoading()
  130. wx.showLoading({
  131. title: '正在' + (this.data.messageId === '' ? '发布' : '修改')
  132. })
  133. if (this.data.messageId === '') {
  134. value.pub_id = this.data.publisherId
  135. } else {
  136. value.msg_id = this.data.messageId
  137. }
  138. if (value.poster.length !== 0 && value.poster[0].substr(0, 8) !== 'cloud://') {
  139. value.poster[0] = res[0].fileID
  140. res.splice(0, 1)
  141. }
  142. for (let i = 0; i < value.photo.length; i++) {
  143. if (value.photo[i].substr(0, 8) !== 'cloud://') {
  144. value.photo[i] = res[0].fileID
  145. res.splice(0, 1)
  146. }
  147. }
  148. if (this.data.messageId === '') {
  149. return wx.cloud.callFunction({
  150. name: 'createMessage',
  151. data: util.msgToDb(value)
  152. })
  153. } else {
  154. return wx.cloud.callFunction({
  155. name: 'updateMessage',
  156. data: util.msgToDb(value)
  157. })
  158. }
  159. }).then(res => {
  160. wx.hideLoading()
  161. if (res.result.status !== 'OK') {
  162. wx.showToast({
  163. title: res.result.errMsg,
  164. icon: 'none'
  165. })
  166. return
  167. }
  168. this.setData({
  169. currentTab: 4
  170. })
  171. })
  172. },
  173. returnToPublisher: function (e) {
  174. wx.navigateBack()
  175. },
  176. /**
  177. * 生命周期函数--监听页面加载
  178. */
  179. onLoad: function (options) {
  180. this.setData({
  181. publisherId: options.id
  182. })
  183. if (options.msg_id) {
  184. this.setData({
  185. messageId: options.msg_id,
  186. currentTab: -1
  187. })
  188. wx.showLoading({
  189. title: '正在加载'
  190. })
  191. wx.cloud.callFunction({
  192. name: 'getMessage',
  193. data: {
  194. msg_id: this.data.messageId
  195. }
  196. }).then(res => {
  197. wx.hideLoading()
  198. if (res.result.status !== 'OK') {
  199. wx.showToast({
  200. title: res.result.errMsg,
  201. icon: 'none'
  202. })
  203. return
  204. }
  205. res.result.data = util.dbToMsg(res.result.data)
  206. const type = res.result.data.type[0].substr(res.result.data.type[0].length - 2)
  207. for (let i = 0; i < this.data.types1.length; i++) {
  208. this.data.types1[i].checked = res.result.data.type.indexOf(this.data.types1[i].value) !== -1
  209. }
  210. for (let i = 0; i < this.data.types2.length; i++) {
  211. this.data.types2[i].checked = res.result.data.tag.indexOf(this.data.types2[i].value) !== -1
  212. }
  213. for (let i = 0; i < this.data.types3.length; i++) {
  214. this.data.types3[i].checked = res.result.data.type.indexOf(this.data.types3[i].value) !== -1
  215. }
  216. this.setData({
  217. types1: this.data.types1,
  218. types2: this.data.types2,
  219. types3: this.data.types3,
  220. currentTab: type === '通知' ? 3 : type === '纳新' ? 2 : 1,
  221. messageInfo: res.result.data
  222. })
  223. })
  224. }
  225. wx.getSystemInfo({
  226. success: function (res) {
  227. this.setData({
  228. winWidth: res.windowWidth
  229. })
  230. this.setData({
  231. winHeight: res.windowHeight
  232. })
  233. }.bind(this),
  234. })
  235. }
  236. })