basic.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. form: { icon: [] },
  10. // 性别
  11. genderList: [],
  12. },
  13. initValidate() {
  14. const rules = { icon: { required: true }, name: { required: true }, gender: { required: true }, phone: { required: true, tel: true } }
  15. // 验证字段的提示信息,若不传则调用默认的信息
  16. const messages = { icon: { required: '请选择头像', }, name: { required: '请输入用户姓名', }, gender: { required: '请选择性别', }, phone: { required: '请输入手机号', } };
  17. this.WxValidate = new WxValidate(rules, messages)
  18. },
  19. // 返回
  20. back: function () {
  21. wx.navigateBack({ delta: 1 })
  22. },
  23. imgUpl: function (e) {
  24. const that = this;
  25. let data = that.data.form.icon;
  26. data.push(e.detail)
  27. that.setData({ 'form.icon': data })
  28. },
  29. // 删除图片
  30. imgDel: function (e) {
  31. const that = this;
  32. let list = that.data.form.icon;
  33. let arr = list.filter((i, index) => index != e.detail.index)
  34. that.setData({ 'form.icon': arr })
  35. },
  36. // 选择性别
  37. genderChange: function (e) {
  38. const that = this;
  39. let data = that.data.genderList[e.detail.value];
  40. if (data) {
  41. that.setData({ 'form.gender': data.value });
  42. that.setData({ 'form.zhGender': data.label })
  43. }
  44. },
  45. // 提交登录
  46. onSubmit: async function (e) {
  47. const that = this;
  48. const params = e.detail.value;
  49. const form = that.data.form;
  50. params.icon = form.icon;
  51. if (!this.WxValidate.checkForm(params)) {
  52. const error = this.WxValidate.errorList[0];
  53. wx.showToast({ title: `${error.msg}`, icon: 'error', duration: 2000 })
  54. return false
  55. } else {
  56. const arr = await app.$post(`/user/${form._id}`, params);
  57. if (arr.errcode == '0') { wx.showToast({ title: `维护信息完成`, icon: 'success', duration: 2000 }); that.back(); }
  58. else wx.showToast({ title: `${arr.errmsg}`, icon: 'error', duration: 2000 })
  59. }
  60. },
  61. /**
  62. * 生命周期函数--监听页面加载
  63. */
  64. onLoad: function (options) {
  65. const that = this;
  66. //验证规则函数
  67. that.initValidate();
  68. // 查询其他信息
  69. that.searchOther();
  70. // 监听用户是否登录
  71. that.watchLogin();
  72. },
  73. searchOther: async function () {
  74. const that = this;
  75. let arr;
  76. arr = await app.$get(`/dict`, { code: 'gender' });
  77. if (arr.errcode == '0' && arr.total > 0) {
  78. let list = arr.data[0].list;
  79. that.setData({ genderList: list })
  80. }
  81. },
  82. // 监听用户是否登录
  83. watchLogin: async function () {
  84. const that = this;
  85. wx.getStorage({
  86. key: 'user',
  87. success: async res => {
  88. const arr = await app.$get(`/user/${res.data.id}`);
  89. if (arr.errcode == '0') {
  90. let gender = that.data.genderList.find(i => i.value == arr.data.gender);
  91. if (gender) arr.data.zhGender = gender.label;
  92. that.setData({ form: arr.data })
  93. }
  94. },
  95. fail: res => {
  96. wx.redirectTo({ url: '/pages/index/index', })
  97. }
  98. })
  99. },
  100. /**
  101. * 生命周期函数--监听页面初次渲染完成
  102. */
  103. onReady: function () {
  104. },
  105. /**
  106. * 生命周期函数--监听页面显示
  107. */
  108. onShow: function () {
  109. },
  110. /**
  111. * 生命周期函数--监听页面隐藏
  112. */
  113. onHide: function () {
  114. },
  115. /**
  116. * 生命周期函数--监听页面卸载
  117. */
  118. onUnload: function () {
  119. },
  120. /**
  121. * 页面相关事件处理函数--监听用户下拉动作
  122. */
  123. onPullDownRefresh: function () {
  124. },
  125. /**
  126. * 页面上拉触底事件的处理函数
  127. */
  128. onReachBottom: function () {
  129. },
  130. /**
  131. * 用户点击右上角分享
  132. */
  133. onShareAppMessage: function () {
  134. }
  135. })