projectadd.js 3.8 KB

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