index.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. define([
  2. 'store/index',
  3. 'api/login',
  4. 'api/my',
  5. 'plugins/blueimp-md5/js/md5',
  6. 'text!./index.html',
  7. 'css!./index.css'
  8. ], function(store, loginApi, myApi, md5, html) {
  9. return {
  10. inject: ['logout'],
  11. props: {
  12. currentPhone: {
  13. type: String,
  14. default: ''
  15. }
  16. },
  17. data: function () {
  18. return {
  19. state: true,
  20. phone: '',
  21. code: '',
  22. pwd: '',
  23. count: -1,
  24. TIME_COUNT: 60,
  25. sharedState: store.state
  26. };
  27. },
  28. methods: {
  29. // 获取验证码
  30. getCode: function () {
  31. var vm = this;
  32. this.count = this.TIME_COUNT;
  33. this.timer = setInterval(function () {
  34. vm.count--;
  35. if (vm.count < 0) {
  36. clearInterval(vm.timer);
  37. vm.timer = null;
  38. }
  39. }, 1000);
  40. loginApi.code({
  41. phone: this.currentPhone
  42. }).then(function (res) {
  43. vm.$message.success(res.msg);
  44. }).catch(function (err) {
  45. vm.$message.error(err.msg);
  46. clearInterval(vm.timer);
  47. vm.timer = null;
  48. vm.count = -1;
  49. });
  50. },
  51. submit: function () {
  52. var vm = this;
  53. if (!this.state) {
  54. if (!this.phone) {
  55. return this.$message.warning('请输入手机号');
  56. }
  57. if (!/^1[3456789]\d{9}$/.test(this.phone)) {
  58. return this.$message.warning('手机号错误');
  59. }
  60. }
  61. if (!this.code) {
  62. return this.$message.warning('请输入验证码');
  63. }
  64. if (!/^\d{6}$/.test(this.code)) {
  65. return this.$message.warning('验证码错误');
  66. }
  67. if (this.timer) {
  68. clearInterval(this.timer);
  69. this.timer = null;
  70. }
  71. if (this.phonePassword) {
  72. if (this.state) {
  73. // 验证旧手机号
  74. myApi.validate_code({
  75. phone: this.currentPhone,
  76. code: this.code
  77. }).then(function () {
  78. vm.state = false;
  79. vm.phone = '';
  80. vm.code = '';
  81. }).catch(function (err) {
  82. vm.$message.error(err.msg);
  83. vm.count = -1;
  84. });
  85. return;
  86. }
  87. // 保存新手机号
  88. myApi.save_phone({
  89. phone: this.phone,
  90. code: this.code
  91. }).then(function (res) {
  92. vm.$message.success(res.msg);
  93. vm.accountClose();
  94. vm.logout();
  95. }).catch(function (err) {
  96. vm.$message.error(err.msg);
  97. vm.count = -1;
  98. });
  99. }
  100. if (!this.pwd) {
  101. return this.$message.warning('请输入密码');
  102. }
  103. if (!/^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,16}$/.test(this.pwd)) {
  104. return this.$message.warning('请输入8-16位字母加数字组合新密码');
  105. }
  106. // 修改密码
  107. loginApi.register({
  108. account: this.currentPhone,
  109. code: this.code,
  110. pwd: md5(this.pwd),
  111. type: 2
  112. }).then(function (res) {
  113. vm.$message.success(res.msg);
  114. vm.accountClose();
  115. vm.logout();
  116. }).catch(function (err) {
  117. vm.$message.error(err.msg);
  118. vm.count = -1;
  119. });
  120. },
  121. accountClose: function () {
  122. store.setAccountAction(false);
  123. }
  124. },
  125. template: html
  126. };
  127. });