index.js 4.4 KB

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