add.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. const app = getApp()
  2. import WxValidate from '../../../utils/wxValidate'
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. frameStyle: { useTop: true, name: '信息管理', leftArrow: true, useBar: false },
  9. id: '',
  10. form: { yyzz: [], img_url: [] },
  11. },
  12. initValidate() {
  13. const rules = { name: { required: true }, brief: { required: true }, phone: { required: true }, address: { required: true }, coach_num: { required: true }, student_num: { required: true }, honor: { required: true }, url: { required: true }, yyzz: { required: true }, img_url: { required: true } }
  14. // 验证字段的提示信息,若不传则调用默认的信息
  15. const messages = { name: { required: '请输入机构名称', }, brief: { required: '请输入机构简介', }, phone: { required: '请输入联系方式', }, address: { required: '请输入训练地址', }, coach_num: { required: '请输入教练人数', }, student_num: { required: '请输入学员人数', }, honor: { required: '请输入过往荣誉', }, url: { required: '请输入网址', }, yyzz: { required: '请上传营业执照', }, img_url: { required: '请输入机构图片', } };
  16. this.WxValidate = new WxValidate(rules, messages)
  17. },
  18. // 返回
  19. back: function () {
  20. wx.navigateBack({ delta: 1 })
  21. },
  22. // 营业执照图片上传
  23. yzimgUpl: function (e) {
  24. const that = this;
  25. let data = that.data.form.yyzz;
  26. data.push(e.detail)
  27. that.setData({ 'form.yyzz': data })
  28. },
  29. // 营业执照图片删除
  30. yzimgDel: function (e) {
  31. const that = this;
  32. let list = that.data.form.yyzz;
  33. let arr = list.filter((i, index) => index != e.detail.index)
  34. that.setData({ 'form.yyzz': arr })
  35. },
  36. // 机构图片上传
  37. imgUpl: function (e) {
  38. const that = this;
  39. let data = that.data.form.img_url;
  40. data.push(e.detail)
  41. that.setData({ 'form.img_url': data })
  42. },
  43. // 机构图片删除
  44. imgDel: function (e) {
  45. const that = this;
  46. let list = that.data.form.img_url;
  47. let arr = list.filter((i, index) => index != e.detail.index)
  48. that.setData({ 'form.img_url': arr })
  49. },
  50. // 提交保运
  51. onSubmit: async function (e) {
  52. const that = this;
  53. const params = e.detail.value;
  54. const form = that.data.form;
  55. params.yyzz = form.yyzz;
  56. params.img_url = form.img_url;
  57. if (!this.WxValidate.checkForm(params)) {
  58. const error = this.WxValidate.errorList[0];
  59. wx.showToast({ title: `${error.msg}`, icon: 'error', duration: 2000 })
  60. return false
  61. } else {
  62. let arr;
  63. if (that.data.id) arr = await app.$post(`/school/${form._id}`, params);
  64. else arr = await app.$post(`/school`, params);
  65. if (arr.errcode == '0') { wx.showToast({ title: `维护信息完成`, icon: 'success', duration: 2000 }); that.back(); }
  66. else wx.showToast({ title: `${arr.errmsg}`, icon: 'error', duration: 2000 })
  67. }
  68. },
  69. /**
  70. * 生命周期函数--监听页面加载
  71. */
  72. onLoad: async function (options) {
  73. const that = this;
  74. that.setData({ id: options.id || null })
  75. //验证规则函数
  76. that.initValidate();
  77. // 监听用户是否登录
  78. await that.watchLogin();
  79. },
  80. // 监听用户是否登录
  81. watchLogin: async function () {
  82. const that = this;
  83. wx.getStorage({
  84. key: 'user',
  85. success: async res => {
  86. let arr;
  87. if (that.data.id) {
  88. arr = await app.$get(`/school/${that.data.id}`);
  89. if (arr.errcode == '0') {
  90. that.setData({ form: arr.data })
  91. } else { wx.showToast({ title: `${arr.errmsg}`, icon: 'error', duration: 2000 }) }
  92. }
  93. },
  94. fail: async res => {
  95. wx.redirectTo({ url: '/pages/index/index' })
  96. }
  97. })
  98. },
  99. /**
  100. * 生命周期函数--监听页面初次渲染完成
  101. */
  102. onReady: function () {
  103. },
  104. /**
  105. * 生命周期函数--监听页面显示
  106. */
  107. onShow: function () { },
  108. /**
  109. * 生命周期函数--监听页面隐藏
  110. */
  111. onHide: function () {
  112. },
  113. /**
  114. * 生命周期函数--监听页面卸载
  115. */
  116. onUnload: function () {
  117. },
  118. /**
  119. * 页面相关事件处理函数--监听用户下拉动作
  120. */
  121. onPullDownRefresh: function () {
  122. },
  123. /**
  124. * 页面上拉触底事件的处理函数
  125. */
  126. onReachBottom: function () {
  127. },
  128. /**
  129. * 用户点击右上角分享
  130. */
  131. onShareAppMessage: function () {
  132. }
  133. })