index.js 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import store from '../../../store'
  2. import create from '../../../utils/create'
  3. import fetch from '../../../utils/http'
  4. import { get as getApi } from '../../../config/api'
  5. const app = getApp()
  6. create(store, {
  7. data: {
  8. userInfo: null,
  9. },
  10. onReady() {
  11. wx.setNavigationBarTitle({
  12. title: '个人资料'
  13. })
  14. },
  15. SaveUserInfo() { // 提交表单数据
  16. if (this.store.data.userInfo.name === '') {
  17. wx.showToast({
  18. title: '昵称不能为空!',
  19. icon: 'none',
  20. })
  21. return false
  22. }
  23. if (!this.store.data.userInfo.phone || this.store.data.userInfo.phone == '') {
  24. wx.showToast({
  25. title: '手机号不能为空!',
  26. icon: 'none',
  27. })
  28. return false
  29. }
  30. var myreg = /^[1][3,4,5,7,8,9][0-9]{9}$/;
  31. if (!myreg.test(this.store.data.userInfo.phone)) {
  32. wx.showToast({
  33. title: '手机号格式不正确!',
  34. icon: 'none',
  35. })
  36. return false
  37. }
  38. if (this.store.data.userInfo.idNum && this.store.data.userInfo.idNum!='') {
  39. var idreg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
  40. if (!idreg.test(this.store.data.userInfo.idNum)) {
  41. wx.showToast({
  42. title: '身份证格式不正确!',
  43. icon: 'none',
  44. })
  45. return false
  46. }
  47. }
  48. fetch({ ...getApi('user.update'), data: this.store.data.userInfo }).then(() => {
  49. // 同步到 globalData
  50. app.globalData.UserInfo = { ...this.store.data.userInfo }
  51. wx.showToast({
  52. title: '更新成功',
  53. icon: 'success',
  54. })
  55. }).catch((err) => {
  56. wx.showToast({
  57. title: '更新失败',
  58. icon: 'none',
  59. })
  60. })
  61. },
  62. FormInput(e) { // 表单输入数据绑定
  63. const key = e.target.dataset.name
  64. const val = e.detail.value
  65. this.store.data.userInfo = {
  66. ...this.store.data.userInfo,
  67. [`${key}`]: val,
  68. }
  69. this.update()
  70. },
  71. onShareAppMessage: function () {
  72. return app.globalData.ShareDate
  73. }
  74. })