add.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. const app = getApp()
  2. import WxValidate from '../../../utils/wxValidate';
  3. Page({
  4. data: {
  5. frameStyle: { useTop: true, name: '信息维护', leftArrow: true, useBar: false },
  6. form: {},
  7. typeList: [],
  8. genderList: []
  9. },
  10. initValidate() {
  11. const rules = { name: { required: true }, type: { required: true }, age: { required: false }, gender: { required: false }, num: { required: false }, explain: { required: false } }
  12. // 验证字段的提示信息,若不传则调用默认的信息
  13. const messages = { name: { required: '未输入名称' }, type: { required: '未选择项目类别' }, age: { required: '未输入人数限制' }, gender: { required: '未选择性别限制' }, num: { required: '未输入人数限制' }, explain: { required: '未输入说明' } };
  14. this.WxValidate = new WxValidate(rules, messages)
  15. },
  16. // 返回
  17. back: function () {
  18. wx.navigateBack({ delta: 1 })
  19. },
  20. // 选择性别
  21. genderChange: function (e) {
  22. const that = this;
  23. let data = that.data.genderList[e.detail.value];
  24. if (data) {
  25. that.setData({ 'form.gender': data.value });
  26. that.setData({ 'form.zhGender': data.label });
  27. }
  28. },
  29. // 项目类别
  30. typeChange: function (e) {
  31. const that = this;
  32. let data = that.data.typeList[e.detail.value];
  33. if (data) {
  34. that.setData({ 'form.type': data.value })
  35. that.setData({ 'form.zhType': data.label })
  36. }
  37. },
  38. //提交
  39. onSubmit: async function (e) {
  40. const that = this;
  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 (params._id) { arr = await app.$post(`/matchProject/${params._id}`, params) }
  49. else { arr = await app.$post(`/matchProject`, 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: async function (options) {
  58. const that = this;
  59. that.setData({ id: options.id || '' })
  60. //验证规则函数
  61. that.initValidate();
  62. // 查询其他信息
  63. await that.searchOther();
  64. // 监听用户是否登录
  65. await that.watchLogin();
  66. },
  67. // 监听用户是否登录
  68. watchLogin: async function () {
  69. const that = this;
  70. wx.getStorage({
  71. key: 'user',
  72. success: async res => {
  73. if (that.data.id) {
  74. let arr = await app.$get(`/matchProject/${that.data.id}`);
  75. if (arr.errcode == '0') {
  76. // 项目类别
  77. let type = typeList.find(i => i.value == arr.data.type);
  78. if (type) arr.data.zhType = type.label;
  79. // 性别
  80. let gender = genderList.find(i => i.value == arr.data.gender);
  81. if (gender) arr.data.zhGender = gender.label;
  82. that.setData({ form: arr.data })
  83. }
  84. };
  85. },
  86. fail: async res => {
  87. wx.redirectTo({ url: '/pages/index/index' })
  88. }
  89. })
  90. },
  91. // 查询其他信息
  92. searchOther: async function () {
  93. const that = this;
  94. let arr;
  95. // 性别
  96. arr = await app.$get(`/dict`, { code: 'gender' });
  97. if (arr.errcode == '0' && arr.total > 0) that.setData({ genderList: arr.data[0].list });
  98. // 项目类别
  99. arr = await app.$get(`/dict`, { code: 'match_project_type' });
  100. if (arr.errcode == '0' && arr.total > 0) that.setData({ typeList: arr.data[0].list });
  101. },
  102. /**
  103. * 生命周期函数--监听页面初次渲染完成
  104. */
  105. onReady: function () {
  106. },
  107. /**
  108. * 生命周期函数--监听页面显示
  109. */
  110. onShow: function () {
  111. },
  112. /**
  113. * 生命周期函数--监听页面隐藏
  114. */
  115. onHide: function () {
  116. },
  117. /**
  118. * 生命周期函数--监听页面卸载
  119. */
  120. onUnload: function () {
  121. },
  122. /**
  123. * 页面相关事件处理函数--监听用户下拉动作
  124. */
  125. onPullDownRefresh: function () {
  126. },
  127. /**
  128. * 页面上拉触底事件的处理函数
  129. */
  130. onReachBottom: function () {
  131. },
  132. /**
  133. * 用户点击右上角分享
  134. */
  135. onShareAppMessage: function () {
  136. }
  137. })