index.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. define([
  2. 'store/index',
  3. 'api/my',
  4. 'text!./index.html',
  5. 'css!./index.css'
  6. ], function(store, myApi, html) {
  7. return {
  8. filters: {
  9. phoneEncrypt: function (phone) {
  10. if (!phone) {
  11. return '';
  12. }
  13. return phone.replace(/(\d{3})\d*(\d{4})/, '$1****$2');
  14. }
  15. },
  16. inject: ['getUserInfo', 'logout', 'getUserLogin'],
  17. props: {
  18. isLogin: {
  19. type: Boolean,
  20. default: false
  21. },
  22. userInfo: {
  23. type: Object,
  24. default: function () {
  25. return {};
  26. }
  27. }
  28. },
  29. data: function () {
  30. return {
  31. nicknameReadonly: true,
  32. avatar: '',
  33. nickname: ''
  34. };
  35. },
  36. watch: {
  37. 'userInfo.avatar': function () {
  38. this.avatar = this.userInfo.avatar;
  39. },
  40. 'userInfo.nickname': function () {
  41. this.nickname = this.userInfo.nickname;
  42. }
  43. },
  44. methods: {
  45. handleBeforeUpload: function (file) {
  46. if (file.type == 'image/jpeg') {
  47. return true;
  48. }
  49. if (file.type == 'image/jpg') {
  50. return true;
  51. }
  52. if (file.type == 'image/png') {
  53. return true;
  54. }
  55. this.$message.error('上传头像图片只能是 JPEG、JPG、PNG 格式!');
  56. return false;
  57. },
  58. handleSuccess: function (res) {
  59. var vm = this;
  60. if (typeof res == 'string') {
  61. return vm.$message.error(res);
  62. }
  63. if (res.code == 400) {
  64. vm.$message.error(res.msg)
  65. return;
  66. }
  67. vm.avatar = res.data.url;
  68. vm.$message.success(res.msg);
  69. },
  70. // 点击保存
  71. save: function () {
  72. var vm = this;
  73. myApi.saveUserInfo({
  74. avatar: this.avatar || this.userInfo.avatar,
  75. nickname: this.nickname
  76. }).then(function (res) {
  77. vm.$message.success(res.msg);
  78. vm.nicknameReadonly = true;
  79. vm.getUserInfo();
  80. }).catch(function (err) {
  81. window.location.replace(vm.$router.home);
  82. });
  83. },
  84. // 点击头像的修改
  85. updateAvatar: function () {
  86. this.$refs.upload.$refs['upload-inner'].$refs.input.click();
  87. },
  88. accountOpen: function (value) {
  89. this.getUserLogin();
  90. store.setIsAccountAction(value);
  91. store.setAccountAction(true);
  92. },
  93. handleLogin: function () {
  94. this.getUserLogin();
  95. },
  96. handleUpdate: function () {
  97. this.getUserLogin();
  98. this.nicknameReadonly = !this.nicknameReadonly;
  99. }
  100. },
  101. template: html
  102. };
  103. });