basic.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. const app = getApp()
  2. import WxValidate from '../../utils/wxValidate'
  3. const { gender } = require('../../utils/dict')
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. frameStyle: { useTop: true, name: '个人信息', leftArrow: true, useBar: false },
  10. form: { icon: [] },
  11. // 性别
  12. genderList: gender,
  13. },
  14. initValidate() {
  15. const rules = { icon: { required: true }, name: { required: true }, card: { required: true }, gender: { required: true }, phone: { required: true, tel: true } }
  16. // 验证字段的提示信息,若不传则调用默认的信息
  17. const messages = { icon: { required: '请选择头像', }, name: { required: '请输入用户姓名', }, card: { required: '请输入身份证号', }, gender: { required: '请选择性别', }, phone: { required: '请输入手机号', } };
  18. this.WxValidate = new WxValidate(rules, messages)
  19. },
  20. // 返回
  21. back: function () {
  22. wx.navigateBack({ delta: 1 })
  23. },
  24. imgUpl: function (e) {
  25. const that = this;
  26. let data = that.data.form.icon;
  27. data.push(e.detail)
  28. that.setData({ 'form.icon': data })
  29. },
  30. // 删除图片
  31. imgDel: function (e) {
  32. const that = this;
  33. let list = that.data.form.icon;
  34. let arr = list.filter((i, index) => index != e.detail.index)
  35. that.setData({ 'form.icon': arr })
  36. },
  37. // 选择性别
  38. genderChange: function (e) {
  39. const that = this;
  40. let data = that.data.genderList[e.detail.value];
  41. if (data) that.setData({ 'form.gender': data.value });
  42. },
  43. // 提交登录
  44. onSubmit: async function (e) {
  45. const that = this;
  46. const params = e.detail.value;
  47. const form = that.data.form;
  48. params.icon = form.icon;
  49. if (!this.WxValidate.checkForm(params)) {
  50. const error = this.WxValidate.errorList[0];
  51. wx.showToast({ title: `${error.msg}`, icon: 'error', duration: 2000 })
  52. return false
  53. } else {
  54. const arr = await app.$post(`/newCourt/api/user/${form._id}`, params);
  55. if (arr.errcode == '0') { wx.showToast({ title: `维护信息完成`, icon: 'success', duration: 2000 }); that.back(); }
  56. else wx.showToast({ title: `${arr.errmsg}`, icon: 'error', duration: 2000 })
  57. }
  58. },
  59. /**
  60. * 生命周期函数--监听页面加载
  61. */
  62. onLoad: function (options) {
  63. const that = this;
  64. //验证规则函数
  65. that.initValidate();
  66. // 监听用户是否登录
  67. that.watchLogin();
  68. },
  69. // 监听用户是否登录
  70. watchLogin: async function () {
  71. const that = this;
  72. wx.getStorage({
  73. key: 'user',
  74. success: async res => {
  75. const arr = await app.$get(`/newCourt/api/user/${res.data.openid}`);
  76. if (arr.errcode == '0') { that.setData({ form: arr.data }) }
  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. })