add.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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: { icon: [] },
  11. // 性别
  12. genderList: [],
  13. levelList: [],
  14. },
  15. initValidate() {
  16. const rules = { icon: { required: true }, name: { required: true }, gender: { required: true }, age: { required: true }, phone: { required: true, tel: true } }
  17. // 验证字段的提示信息,若不传则调用默认的信息
  18. const messages = { icon: { required: '请选择头像', }, name: { required: '请输入用户姓名', }, gender: { required: '请选择性别', }, age: { required: '请输入年龄', }, phone: { required: '请输入联系电话', } };
  19. this.WxValidate = new WxValidate(rules, messages)
  20. },
  21. // 返回
  22. back: function () {
  23. wx.navigateBack({ delta: 1 })
  24. },
  25. imgUpl: function (e) {
  26. const that = this;
  27. let data = that.data.form.icon;
  28. data.push(e.detail)
  29. that.setData({ 'form.icon': data })
  30. },
  31. // 删除图片
  32. imgDel: function (e) {
  33. const that = this;
  34. let list = that.data.form.icon;
  35. let arr = list.filter((i, index) => index != e.detail.index)
  36. that.setData({ 'form.icon': arr })
  37. },
  38. // 选择性别
  39. genderChange: function (e) {
  40. const that = this;
  41. let data = that.data.genderList[e.detail.value];
  42. if (data) that.setData({ 'form.gender': data.value });
  43. },
  44. //选择等级
  45. coachChange: function (e) {
  46. const that = this;
  47. let data = that.data.levelList[e.detail.value];
  48. if (data) that.setData({ 'form.level': data.value });
  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.icon = form.icon;
  56. if (!this.WxValidate.checkForm(params)) {
  57. const error = this.WxValidate.errorList[0];
  58. wx.showToast({ title: `${error.msg}`, icon: 'error', duration: 2000 })
  59. return false
  60. } else {
  61. let arr;
  62. if (form._id) { arr = await app.$post(`/coach/${form._id}`, params); }
  63. else { arr = await app.$post(`/coach`, params) }
  64. if (arr.errcode == '0') { wx.showToast({ title: `维护信息完成`, icon: 'success', duration: 2000 }); that.back(); }
  65. else wx.showToast({ title: `${error.msg}`, icon: 'error', duration: 2000 })
  66. }
  67. },
  68. /**
  69. * 生命周期函数--监听页面加载
  70. */
  71. onLoad: function (options) {
  72. const that = this;
  73. that.setData({ id: options.id || '' })
  74. //验证规则函数
  75. that.initValidate();
  76. // 监听用户是否登录
  77. that.watchLogin();
  78. },
  79. // 监听用户是否登录
  80. watchLogin: async function () {
  81. const that = this;
  82. wx.getStorage({
  83. key: 'user',
  84. success: async res => {
  85. if (that.data.id) {
  86. const arr = await app.$get(`/coach/${that.data.id}`);
  87. if (arr.errcode == '0') that.setData({ form: arr.data });
  88. }
  89. const aee = await app.$get(`/dict`, { code: "coach_grade" });
  90. if (aee.errcode == '0') that.setData({ levelList: aee.data[0].list });
  91. const ree = await app.$get(`/dict`, { code: "gender" });
  92. if (ree.errcode == '0') that.setData({ genderList: ree.data[0].list });
  93. },
  94. fail: 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. */
  112. onHide: function () {
  113. },
  114. /**
  115. * 生命周期函数--监听页面卸载
  116. */
  117. onUnload: function () {
  118. },
  119. /**
  120. * 页面相关事件处理函数--监听用户下拉动作
  121. */
  122. onPullDownRefresh: function () {
  123. },
  124. /**
  125. * 页面上拉触底事件的处理函数
  126. */
  127. onReachBottom: function () {
  128. },
  129. /**
  130. * 用户点击右上角分享
  131. */
  132. onShareAppMessage: function () {
  133. }
  134. })