add.js 3.5 KB

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