basic.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. typeChange: function (e) {
  46. const { value } = e.detail;
  47. if (value == '0') wx.showToast({ title: `如进行身份切换,请在维护信息成功后退出登录,系统会自动进行身份置换!`, icon: 'none', duration: 2000 })
  48. },
  49. // 提交登录
  50. onSubmit: async function (e) {
  51. const that = this;
  52. const params = e.detail.value;
  53. const form = that.data.form;
  54. params.icon = form.icon;
  55. if (!this.WxValidate.checkForm(params)) {
  56. const error = this.WxValidate.errorList[0];
  57. wx.showToast({ title: `${error.msg}`, icon: 'error', duration: 2000 })
  58. return false
  59. } else {
  60. const arr = await app.$post(`/user/${form._id}`, params);
  61. if (arr.errcode == '0') { wx.showToast({ title: `维护信息完成`, icon: 'success', duration: 2000 }); that.back(); }
  62. else wx.showToast({ title: `${arr.errmsg}`, icon: 'error', duration: 2000 })
  63. }
  64. },
  65. /**
  66. * 生命周期函数--监听页面加载
  67. */
  68. onLoad: function (options) {
  69. const that = this;
  70. //验证规则函数
  71. that.initValidate();
  72. // 查询其他信息
  73. that.searchOther();
  74. // 监听用户是否登录
  75. that.watchLogin();
  76. },
  77. searchOther: async function () {
  78. const that = this;
  79. let arr;
  80. arr = await app.$get(`/dict`, { code: 'gender' });
  81. if (arr.errcode == '0' && arr.total > 0) {
  82. let list = arr.data[0].list;
  83. that.setData({ genderList: list })
  84. }
  85. },
  86. // 监听用户是否登录
  87. watchLogin: async function () {
  88. const that = this;
  89. wx.getStorage({
  90. key: 'user',
  91. success: async res => {
  92. const arr = await app.$get(`/user/${res.data.id}`);
  93. if (arr.errcode == '0') {
  94. let gender = that.data.genderList.find(i => i.value == arr.data.gender);
  95. if (gender) arr.data.zhGender = gender.label;
  96. that.setData({ form: arr.data })
  97. }
  98. },
  99. fail: res => {
  100. wx.redirectTo({ url: '/pages/index/index', })
  101. }
  102. })
  103. },
  104. /**
  105. * 生命周期函数--监听页面初次渲染完成
  106. */
  107. onReady: function () {
  108. },
  109. /**
  110. * 生命周期函数--监听页面显示
  111. */
  112. onShow: function () {
  113. },
  114. /**
  115. * 生命周期函数--监听页面隐藏
  116. */
  117. onHide: function () {
  118. },
  119. /**
  120. * 生命周期函数--监听页面卸载
  121. */
  122. onUnload: function () {
  123. },
  124. /**
  125. * 页面相关事件处理函数--监听用户下拉动作
  126. */
  127. onPullDownRefresh: function () {
  128. },
  129. /**
  130. * 页面上拉触底事件的处理函数
  131. */
  132. onReachBottom: function () {
  133. },
  134. /**
  135. * 用户点击右上角分享
  136. */
  137. onShareAppMessage: function () {
  138. }
  139. })