verification.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import * as API_user from '@/apis/user'
  2. import {
  3. checkPhone,
  4. } from '@/utils'
  5. export default {
  6. data() {
  7. return {
  8. openId:"",
  9. subFormCode: {
  10. phone: '',
  11. openId: '',
  12. verifyCode: ''
  13. },
  14. isSendMsgIng: false,
  15. sendMsgSecond: 60,
  16. user:{},
  17. }
  18. },
  19. methods: {
  20. codeCheckForm() {
  21. let phoneResult = checkPhone(this.subFormCode.phone);
  22. if (typeof phoneResult == 'string') {
  23. this.$refs.common.alert(phoneResult)
  24. return false;
  25. } else if (!this.subFormCode.verifyCode) {
  26. this.$refs.common.alert('请输入验证码')
  27. return false;
  28. } else {
  29. return true;
  30. }
  31. },
  32. login() {
  33. this.codeSubmit();
  34. },
  35. //验证码登录
  36. codeSubmit() {
  37. if (this.codeCheckForm()) {
  38. this.$refs.common.showLoading()
  39. API_user.validateCode(this.subFormCode).then(response => {
  40. if(response.data){
  41. var token = response.data ? response.data.token : '';
  42. this.carhelp.setToken(token);
  43. this.carhelp.setPersonInfo(response.data.userInfo)
  44. this.loginSuccess();
  45. }else{
  46. this.$refs.common.showLoading(false,response.message);
  47. }
  48. }).catch(error => {
  49. this.$refs.common.showLoading(false,error);
  50. })
  51. }
  52. },
  53. //发送验证码
  54. sendMsg() {
  55. if (!this.isSendMsgIng) {
  56. let phoneResult = checkPhone(this.subFormCode.phone);
  57. if (typeof phoneResult == 'string') {
  58. this.$refs.common.alert(phoneResult)
  59. } else {
  60. this.$refs.common.showLoading()
  61. API_user.getVerifyCode(this.subFormCode).then(response => {
  62. this.$refs.common.showLoading(false)
  63. if(response.message){
  64. //倒计时
  65. this.msgTimeInterval();
  66. }else{
  67. this.$refs.common.alert("您的验证码已经发送[5分钟有效],请勿重复点击")
  68. }
  69. }).catch(error => {
  70. this.$refs.common.alert(error)
  71. })
  72. }
  73. }
  74. },
  75. //倒计时
  76. msgTimeInterval() {
  77. this.isSendMsgIng = true;
  78. var time = this.sendMsgSecond;
  79. var _this = this;
  80. this.timer = setInterval(() => {
  81. if (time > 0) {
  82. _this.sendMsgSecond = time--;
  83. } else {
  84. _this.isSendMsgIng = false;
  85. _this.sendMsgSecond = 60;
  86. clearInterval(_this.timer)
  87. }
  88. }, 1000)
  89. }, //登录成功
  90. loginSuccess() {
  91. uni.redirectTo({
  92. url: '/pages/my/user/updatephone/change'
  93. })
  94. },
  95. showphone(phone){
  96. if(!phone){
  97. return "";
  98. }
  99. if(phone.length!=11){
  100. return "";
  101. }
  102. return phone.substring(0,3)+'****'+phone.substring(8);
  103. },
  104. },
  105. onLoad() {
  106. },onReady(){
  107. this.subFormCode.openId = this.carhelp.getOpenId();
  108. this.user=this.carhelp.getPersonInfo();
  109. this.subFormCode.phone=this.user.phone;
  110. }
  111. }