adddictionary.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. const app = getApp()
  2. import WxValidate from '../../../utils/wxValidate'
  3. const moment = require("../../../utils/moment.min")
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. frameStyle: { useTop: true, name: '信息管理', leftArrow: true, useBar: false },
  10. id: '',
  11. form: { list: [] },
  12. // dialog弹框
  13. dialog: { title: '弹框标题', show: false, type: '1' },
  14. listForm: {}
  15. },
  16. initValidate() {
  17. const rules = { label: { required: true }, code: { required: true } }
  18. // 验证字段的提示信息,若不传则调用默认的信息
  19. const messages = { label: { required: '请输入字典名称', }, code: { required: '请输入字典编码', } };
  20. this.WxValidate = new WxValidate(rules, messages)
  21. },
  22. // 返回
  23. back: function () {
  24. wx.navigateBack({ delta: 1 })
  25. },
  26. // 添加选项
  27. listAdd: function () {
  28. const that = this;
  29. that.setData({ dialog: { title: '字典选项', show: true, type: '1' } })
  30. },
  31. // 提交保存
  32. listSubmit: function (e) {
  33. const that = this;
  34. const params = e.detail.value;
  35. let listForm = that.data.listForm;
  36. let form = that.data.form;
  37. if (params.label && params.value) {
  38. if (listForm.id) {
  39. let list = form.list.filter(i => i.id != listForm.id);
  40. that.setData({ 'form.list': [params, ...list] })
  41. } else {
  42. params.id = moment().valueOf();
  43. that.setData({ 'form.list': [...form.list, params] })
  44. }
  45. that.toClose()
  46. } else {
  47. wx.showToast({ title: `缺少必填项`, icon: 'error', duration: 2000 })
  48. }
  49. },
  50. // 修改
  51. listEdit: function (e) {
  52. const that = this;
  53. const { item } = e.currentTarget.dataset;
  54. that.setData({ listForm: item })
  55. that.setData({ dialog: { title: '字典选项', show: true, type: '1' } })
  56. },
  57. // 删除
  58. listDel: function (e) {
  59. const that = this;
  60. let form = that.data.form;
  61. const { item } = e.currentTarget.dataset;
  62. let list = form.list.filter(i => i.id != item.id)
  63. that.setData({ 'form.list': list })
  64. },
  65. // 关闭弹框
  66. toClose: function () {
  67. const that = this;
  68. that.setData({ listForm: {} })
  69. that.setData({ dialog: { title: '字典选项', show: false, type: '1' } })
  70. },
  71. // 提交登录
  72. onSubmit: async function (e) {
  73. const that = this;
  74. const params = e.detail.value;
  75. const form = that.data.form;
  76. params.list = form.list;
  77. if (!this.WxValidate.checkForm(params)) {
  78. const error = this.WxValidate.errorList[0];
  79. wx.showToast({ title: `${error.msg}`, icon: 'error', duration: 2000 })
  80. return false
  81. } else {
  82. let arr;
  83. if (that.data.id) arr = await app.$post(`/dict/${form._id}`, params);
  84. else arr = await app.$post(`/dict`, params);
  85. if (arr.errcode == '0') { wx.showToast({ title: `维护信息完成`, icon: 'success', duration: 2000 }); that.back(); }
  86. else wx.showToast({ title: `${arr.errmsg}`, icon: 'error', duration: 2000 })
  87. }
  88. },
  89. /**
  90. * 生命周期函数--监听页面加载
  91. */
  92. onLoad: function (options) {
  93. const that = this;
  94. that.setData({ id: options.id || null })
  95. //验证规则函数
  96. that.initValidate();
  97. // 监听用户是否登录
  98. that.watchLogin();
  99. },
  100. // 监听用户是否登录
  101. watchLogin: async function () {
  102. const that = this;
  103. wx.getStorage({
  104. key: 'user',
  105. success: async res => {
  106. let arr;
  107. if (that.data.id) {
  108. arr = await app.$get(`/dict/${that.data.id}`);
  109. if (arr.errcode == '0') {
  110. that.setData({ form: arr.data })
  111. } else { wx.showToast({ title: `${arr.errmsg}`, icon: 'error', duration: 2000 }) }
  112. }
  113. },
  114. fail: async res => {
  115. wx.redirectTo({ url: '/pages/index/index' })
  116. }
  117. })
  118. },
  119. /**
  120. * 生命周期函数--监听页面初次渲染完成
  121. */
  122. onReady: function () {
  123. },
  124. /**
  125. * 生命周期函数--监听页面显示
  126. */
  127. onShow: function () { },
  128. /**
  129. * 生命周期函数--监听页面隐藏
  130. */
  131. onHide: function () {
  132. },
  133. /**
  134. * 生命周期函数--监听页面卸载
  135. */
  136. onUnload: function () {
  137. },
  138. /**
  139. * 页面相关事件处理函数--监听用户下拉动作
  140. */
  141. onPullDownRefresh: function () {
  142. },
  143. /**
  144. * 页面上拉触底事件的处理函数
  145. */
  146. onReachBottom: function () {
  147. },
  148. /**
  149. * 用户点击右上角分享
  150. */
  151. onShareAppMessage: function () {
  152. }
  153. })