login.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. back:false,
  17. }
  18. },
  19. methods: {
  20. gotoHome(){
  21. uni.switchTab({
  22. url: '/pages/index/index'
  23. });
  24. },
  25. codeCheckForm() {
  26. let phoneResult = checkPhone(this.subFormCode.phone);
  27. if (typeof phoneResult == 'string') {
  28. this.$refs.common.alert(phoneResult)
  29. return false;
  30. } else if (!this.subFormCode.verifyCode) {
  31. this.$refs.common.alert('请输入验证码')
  32. return false;
  33. } else {
  34. return true;
  35. }
  36. },
  37. login() {
  38. this.codeSubmit();
  39. },
  40. //验证码登录
  41. codeSubmit() {
  42. if (this.codeCheckForm()) {
  43. this.$refs.common.showLoading()
  44. this.subFormCode.openId = this.carhelp.getOpenId();
  45. if (process.car.SIMPLE_RUN) {
  46. this.subFormCode.openId = "test";
  47. }
  48. API_user.validateCode(this.subFormCode).then(response => {
  49. if(response.data){
  50. var token = response.data ? response.data.token : '';
  51. this.carhelp.setToken(token);
  52. this.carhelp.setPersonInfo(response.data.token)
  53. this.loginSuccess();
  54. }else{
  55. this.$refs.common.showLoading(false,response.message);
  56. }
  57. }).catch(error => {
  58. this.$refs.common.showLoading(false,error);
  59. })
  60. }
  61. },
  62. //发送验证码
  63. sendMsg() {
  64. if (!this.isSendMsgIng) {
  65. let phoneResult = checkPhone(this.subFormCode.phone);
  66. if (typeof phoneResult == 'string') {
  67. this.$refs.common.alert(phoneResult)
  68. } else {
  69. this.$refs.common.showLoading()
  70. API_user.getVerifyCode(this.subFormCode).then(response => {
  71. this.$refs.common.showLoading(false)
  72. if(response.message){
  73. //倒计时
  74. this.msgTimeInterval();
  75. }else{
  76. this.$refs.common.alert("您的验证码已经发送[5分钟有效],请勿重复点击")
  77. }
  78. }).catch(error => {
  79. uni.hideLoading();
  80. this.$refs.common.alert(error)
  81. })
  82. }
  83. }
  84. },
  85. //倒计时
  86. msgTimeInterval() {
  87. this.isSendMsgIng = true;
  88. var time = this.sendMsgSecond;
  89. var _this = this;
  90. this.timer = setInterval(() => {
  91. if (time > 0) {
  92. _this.sendMsgSecond = time--;
  93. } else {
  94. _this.isSendMsgIng = false;
  95. _this.sendMsgSecond = 60;
  96. clearInterval(_this.timer)
  97. }
  98. }, 1000)
  99. }, //登录成功
  100. loginSuccess() {
  101. uni.switchTab({
  102. url: '/pages/index/index'
  103. });
  104. },
  105. },
  106. onLoad(temp) {
  107. var ob=this.carhelp.getPersonInfo()
  108. if(ob){
  109. this.loginSuccess()
  110. }
  111. if(temp.back){
  112. this.back=true;
  113. }
  114. console.log(ob)
  115. this.subFormCode.openId = this.carhelp.getOpenId();
  116. console.log(this.carhelp.getPrefix())
  117. }
  118. }