time.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import verification from '@/utils/test';
  2. export default {
  3. data(){
  4. return {
  5. timeConfig:{
  6. default:'获取验证码',
  7. format:'还剩',
  8. afterFormat:'s',
  9. time:60
  10. },
  11. timeFormat:''
  12. }
  13. },
  14. methods:{
  15. // 获取验证码
  16. triggerTimeDate(item,data){
  17. if (this.timeFormat) return;
  18. let resultData = undefined;
  19. for (let i=0,count=data.length;i<count;i++) {
  20. if (item.code === data[i].key) {
  21. resultData = {
  22. ...data[i]
  23. };
  24. break;
  25. }
  26. }
  27. if (resultData) {
  28. resultData.key = 'mobile';
  29. verification.verificationPromise(resultData,true).then((data)=>{
  30. data.type = item.codeType;
  31. this.$request({
  32. url:'login/send_sms',
  33. data,
  34. message:2,
  35. failMessage:true
  36. }).catch(()=>{
  37. // 如果发生错误触发
  38. this.endTimeDate = 0
  39. });
  40. return this.triggerTimeType();
  41. });
  42. }
  43. },
  44. // 触发倒计时
  45. triggerTimeType(){
  46. clearTimeout(this._time_format);
  47. if (this.endTimeDate === undefined) {
  48. this.endTimeDate = this.getTimeDate() + this.timeConfig.time;
  49. }
  50. let diff = this.endTimeDate - this.getTimeDate();
  51. if (diff <= 0) {
  52. this.timeFormat = '';
  53. this.endTimeDate = undefined;
  54. } else {
  55. this.timeFormat = this.timeConfig.format + diff + this.timeConfig.afterFormat;
  56. this._time_format = setTimeout(()=> this.triggerTimeType(),1000);
  57. }
  58. },
  59. // 获取时间
  60. getTimeDate(){
  61. return Math.ceil(new Date().getTime() / 1000);
  62. }
  63. },
  64. beforeUnmount() {
  65. clearTimeout(this._time_format);
  66. }
  67. }