basic.js 4.7 KB

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