detail.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // pages/market/detail.js
  2. import WxValidate from '../../utils/wxValidate'
  3. const app = getApp()
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. frameStyle: { useTop: true, name: '信息管理', leftArrow: true, useBar: false },
  10. // 主体高度
  11. infoHeight: '',
  12. userInfo: { id: '00010102883', name: '供货单位' },
  13. form: {},
  14. // 商品类型
  15. typeList: [
  16. { code: "8", id: "61dd3480bf70154c7fa35ac5", name: "办公用品" },
  17. { code: "8", id: "61dd3480bf70154c7fa35ac5", name: "金融用品" },
  18. { code: "8", id: "61dd3480bf70154c7fa35ac5", name: "书写工具" },
  19. { code: "8", id: "61dd3480bf70154c7fa35ac5", name: "办公机械" },
  20. { code: "8", id: "61dd3480bf70154c7fa35ac5", name: "文件用品" },
  21. { code: "8", id: "61dd3480bf70154c7fa35ac5", name: "其他用品" },
  22. ],
  23. // 图片
  24. img_url: [],
  25. },
  26. initValidate() {
  27. const rules = { type_name: { required: true }, name: { required: true, }, brief: { required: true, }, money: { required: true, }, }
  28. // 验证字段的提示信息,若不传则调用默认的信息
  29. const messages = { type_name: { required: '请选择商品类型', }, name: { required: '请输入商品名称', }, brief: { required: '请输入商品简介', }, money: { required: '请输入商品价格', }, };
  30. this.WxValidate = new WxValidate(rules, messages)
  31. },
  32. back: function () {
  33. wx.navigateBack({ url: '/pages/market/index' })
  34. },
  35. // 类型选择
  36. typeChange: function (e) {
  37. let { value } = e.detail;
  38. let data = this.data.typeList[value];
  39. this.setData({ 'form.type_id': data.id })
  40. this.setData({ 'form.type_name': data.name })
  41. },
  42. // 上传图片
  43. afterRead: function (event) {
  44. const { file } = event.detail;
  45. // 当设置 mutiple 为 true 时, file 为数组格式,否则为对象格式
  46. wx.uploadFile({
  47. url: app.globalData.fileUrl + '/files/consumables/market/upload',
  48. filePath: file.url,
  49. name: 'file',
  50. formData: {},
  51. success: (res) => {
  52. if (res.statusCode == '200') {
  53. let data = [{ name: JSON.parse(res.data).name, url: `${app.globalData.fileUrl}` + JSON.parse(res.data).uri }]
  54. this.setData({ img_url: this.data.img_url.concat(data) })
  55. } else {
  56. wx.showToast({ title: `${res.data.errmsg}`, icon: 'fail', duration: 2000 })
  57. }
  58. },
  59. });
  60. },
  61. // 删除图片
  62. uploadDelete: function (e) {
  63. const index = e.detail.index;
  64. const imgList = this.data.img_url;
  65. if (imgList.length === 1) this.setData({ img_url: [] })
  66. else { let data = imgList.splice(0, 1); this.setData({ img_url: data }) }
  67. },
  68. // 取消保存
  69. onReset: function (e) {
  70. this.back()
  71. },
  72. // 提交保存
  73. onSubmit: function (e) {
  74. const params = e.detail.value;
  75. params.img_url = this.data.img_url;
  76. if (!this.WxValidate.checkForm(params)) {
  77. const error = this.WxValidate.errorList[0];
  78. wx.showToast({ title: `${error.msg}`, icon: 'error', duration: 2000 })
  79. return false
  80. } else {
  81. wx.showModal({
  82. title: '是否确认提交商品',
  83. success(res) {
  84. if (res.confirm) {
  85. wx.showToast({ title: `提交商品成功`, icon: 'success', duration: 2000 });
  86. } else if (res.cancel) {
  87. }
  88. }
  89. })
  90. }
  91. },
  92. /**
  93. * 生命周期函数--监听页面加载
  94. */
  95. onLoad: function (options) {
  96. this.setData({ 'form.mech_name': this.data.userInfo.name, 'form.mech_id': this.data.userInfo.id, })
  97. //验证规则函数
  98. this.initValidate()
  99. // 计算高度
  100. this.searchHeight()
  101. },
  102. // 计算高度
  103. searchHeight: function () {
  104. let frameStyle = this.data.frameStyle;
  105. let client = app.globalData.client;
  106. // 减去状态栏
  107. let infoHeight = client.windowHeight - (client.statusBarHeight + client.getMenu.height + (client.getMenu.top - client.statusBarHeight) * 2);
  108. // 是否减去底部菜单
  109. if (frameStyle.useBar) infoHeight = infoHeight - 50;
  110. if (infoHeight) this.setData({ infoHeight: infoHeight })
  111. },
  112. /**
  113. * 生命周期函数--监听页面初次渲染完成
  114. */
  115. onReady: function () {
  116. },
  117. /**
  118. * 生命周期函数--监听页面显示
  119. */
  120. onShow: function () {
  121. },
  122. /**
  123. * 生命周期函数--监听页面隐藏
  124. */
  125. onHide: function () {
  126. },
  127. /**
  128. * 生命周期函数--监听页面卸载
  129. */
  130. onUnload: function () {
  131. },
  132. /**
  133. * 页面相关事件处理函数--监听用户下拉动作
  134. */
  135. onPullDownRefresh: function () {
  136. },
  137. /**
  138. * 页面上拉触底事件的处理函数
  139. */
  140. onReachBottom: function () {
  141. },
  142. /**
  143. * 用户点击右上角分享
  144. */
  145. onShareAppMessage: function () {
  146. }
  147. })