index.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import WxValidate from '../../utils/WxValidate.js';
  2. var app = getApp();
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. realname: "",
  9. telephone: ""
  10. },
  11. /**
  12. * 生命周期函数--监听页面加载
  13. */
  14. onLoad: function (options) {
  15. this.getData();
  16. },
  17. /**
  18. * 生命周期函数--监听页面初次渲染完成
  19. */
  20. onReady: function () {
  21. this.initValidate();
  22. },
  23. getData: function() {
  24. let token = wx.getStorageSync('token');
  25. app.util.ProReq('user.get_realname_tel', { token }).then(res => {
  26. this.setData({
  27. realname: res.data.realname,
  28. telephone: res.data.telephone
  29. })
  30. }).catch(err => {
  31. app.util.message(err.message || '请先登录', 'switchTo:/lionfish_comshop/pages/user/me', 'error');
  32. })
  33. },
  34. //报错
  35. showModal(error) {
  36. wx.showModal({
  37. content: error.msg,
  38. showCancel: false,
  39. })
  40. },
  41. //资料验证函数
  42. initValidate() {
  43. const rules = {
  44. realname: {
  45. required: true,
  46. minlength: 1
  47. },
  48. telephone: {
  49. required: true,
  50. tel: true
  51. }
  52. }
  53. const messages = {
  54. realname: {
  55. required: '请填写真实姓名',
  56. minlength: '请输入正确的姓名'
  57. },
  58. telephone: {
  59. required: '请填写手机号',
  60. tel: '请填写正确的手机号'
  61. }
  62. }
  63. this.WxValidate = new WxValidate(rules, messages)
  64. },
  65. /**
  66. * 资料修改表单提交
  67. */
  68. formSubmit(e) {
  69. const params = e.detail.value;
  70. //校验表单
  71. if (!this.WxValidate.checkForm(params)) {
  72. const error = this.WxValidate.errorList[0];
  73. this.showModal(error);
  74. return false;
  75. }
  76. this.setData({
  77. btnLoading: true
  78. })
  79. let token = wx.getStorageSync('token');
  80. params.token = token;
  81. app.util.ProReq('user.update_realname_tel', params).then(res => {
  82. this.setData({ btnLoading: false });
  83. wx.showModal({
  84. title: "提示",
  85. content: res.message || '更改成功',
  86. showCancel: false
  87. })
  88. }).catch(err => {
  89. this.setData({
  90. btnLoading: false
  91. })
  92. app.util.message(err.msg || '提交失败,请重试', '', 'error');
  93. })
  94. }
  95. })