add.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. const app = getApp();
  2. import WxValidate from '../../utils/wxValidate';
  3. import { isuse } from '../../utils/dict'
  4. Page({
  5. data: {
  6. frameStyle: { useTop: true, name: '信息管理', leftArrow: true, useBar: false },
  7. id: '',
  8. form: {},
  9. // 裁判信息
  10. refereeList: [],
  11. // 是否启用
  12. isuseList: isuse
  13. },
  14. initValidate() {
  15. const rules = { name: { required: true }, is_use: { required: true } }
  16. // 验证字段的提示信息,若不传则调用默认的信息
  17. const messages = { name: { required: '请输入场地名称' }, is_use: { required: '请选择是否启用' } };
  18. this.WxValidate = new WxValidate(rules, messages)
  19. },
  20. back: function () {
  21. wx.navigateBack({ delta: 1 })
  22. },
  23. // 选择裁判
  24. referee_idChange: function (e) {
  25. const that = this;
  26. let index = e.detail.value;
  27. let value = that.data.refereeList[index];
  28. that.setData({ 'form.referee_id': value.openid });
  29. that.setData({ 'form.referee_name': value.name });
  30. },
  31. // 是否使用
  32. is_useChange: function (e) {
  33. const that = this;
  34. let index = e.detail.value;
  35. let value = that.data.isuseList[index];
  36. that.setData({ 'form.is_use': value.value });
  37. },
  38. toSubmit: async function (e) {
  39. const that = this;
  40. const form = that.data.form;
  41. const params = e.detail.value;
  42. if (!this.WxValidate.checkForm(params)) {
  43. const error = this.WxValidate.errorList[0];
  44. wx.showToast({ title: `${error.msg}`, icon: 'error', duration: 2000 })
  45. return false
  46. } else {
  47. let arr;
  48. if (form._id) arr = await app.$post(`/newCourt/api/ground/${form._id}`, params);
  49. else arr = await app.$post(`/newCourt/api/ground`, params);
  50. if (arr.errcode == '0') { wx.showToast({ title: `维护信息成功`, icon: 'success', duration: 2000 }); that.back() }
  51. else { wx.showToast({ title: `${arr.errmsg}`, icon: 'error', duration: 2000 }) }
  52. }
  53. },
  54. /**
  55. * 生命周期函数--监听页面加载
  56. */
  57. onLoad: function (options) {
  58. const that = this;
  59. that.setData({ id: options.id || '' })
  60. //验证规则函数
  61. that.initValidate();
  62. // 监听用户是否登录
  63. that.watchLogin();
  64. },
  65. // 监听用户是否登录
  66. watchLogin: async function () {
  67. const that = this;
  68. wx.getStorage({
  69. key: 'user',
  70. success: async res => {
  71. // 查询裁判信息
  72. let arr;
  73. arr = await app.$get(`/newCourt/api/user`, { type: '-1' });
  74. if (arr.errcode == '0') { that.setData({ refereeList: arr.data }) }
  75. else { wx.showToast({ title: `${arr.errmsg}`, icon: 'error', duration: 2000 }) }
  76. if (that.data.id) {
  77. arr = await app.$get(`/newCourt/api/ground/${that.data.id}`);
  78. if (arr.errcode == '0') {
  79. let user = that.data.refereeList.find((i) => i.openid == arr.data.referee_id);
  80. if (user) arr.data.referee_name = user.name;
  81. that.setData({ form: arr.data });
  82. }
  83. }
  84. },
  85. fail: async res => {
  86. wx.redirectTo({ url: '/pages/index/index' })
  87. }
  88. })
  89. },
  90. /**
  91. * 生命周期函数--监听页面初次渲染完成
  92. */
  93. onReady: function () {
  94. },
  95. /**
  96. * 生命周期函数--监听页面显示
  97. */
  98. onShow: function () {
  99. },
  100. /**
  101. * 生命周期函数--监听页面隐藏
  102. */
  103. onHide: function () {
  104. },
  105. /**
  106. * 生命周期函数--监听页面卸载
  107. */
  108. onUnload: function () {
  109. },
  110. /**
  111. * 页面相关事件处理函数--监听用户下拉动作
  112. */
  113. onPullDownRefresh: function () {
  114. },
  115. /**
  116. * 页面上拉触底事件的处理函数
  117. */
  118. onReachBottom: function () {
  119. },
  120. /**
  121. * 用户点击右上角分享
  122. */
  123. onShareAppMessage: function () {
  124. }
  125. })