basic.js 4.5 KB

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