login.vue 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. <template>
  2. <div class="page-container login-container">
  3. <el-card class="login-card">
  4. <h2 class="login-card__title login-card__item">登 录</h2>
  5. <el-input ref="phone" v-model="phone" oninput="value=value.replace(/[^\d]/g,'')" placeholder="请输入手机号" minlength='11' maxlength='11' class="login-card__item" autofocus></el-input>
  6. <el-input ref="password" v-model="password" type="password" placeholder="请输入密码" maxlength="20" class="login-card__item"></el-input>
  7. <el-input ref="iCode" v-model="iCode" placeholder="请输入验证码" class="login-card__short-item"></el-input>
  8. <div class="login-card__canvas" @click="refreshCode" ref="canvas">
  9. <SIdentify :identifyCode="identifyCode" :contentWidth="canvasWidth"></SIdentify>
  10. </div>
  11. <div class="login-card__item">
  12. <el-checkbox v-model="rememberPassword"></el-checkbox> 记住密码
  13. <router-link to="/resetPassword" class="login-card__right-link">找回密码</router-link>
  14. </div>
  15. <el-button type="primary" class="login-card__item" :loading="loading" @click="login" round>登 录</el-button>
  16. <div class="login-card__bottom"><router-link to="/register" class="login-card__link">立即注册</router-link></div>
  17. </el-card>
  18. <div class="clear"></div>
  19. <router-link to="/"><img src="/image/seec-logo--horizontal.png" class="login-card__logo"/></router-link>
  20. </div>
  21. </template>
  22. <script>
  23. import { mapState } from 'vuex'
  24. import SIdentify from "~/components/Identify";
  25. export default {
  26. name: "login",
  27. layout: "blank",
  28. data(){
  29. return{
  30. phone: '',
  31. password: '',
  32. identifyCodes: ['0','1', '2', '3', '4', '5', '6', '7', '8', '9'],
  33. identifyCode: '',
  34. iCode: '',
  35. rememberPassword: false,
  36. loading: false,
  37. canvasWidth: 0,
  38. }
  39. },
  40. components: {
  41. SIdentify: SIdentify
  42. },
  43. mounted(){
  44. this.identifyCode = "";
  45. this.makeCode(this.identifyCodes, 4);
  46. this.canvasWidth = this.$refs.canvas.clientWidth;
  47. if(localStorage.phone){
  48. this.phone = localStorage.phone;
  49. }
  50. if(localStorage.password){
  51. this.password = localStorage.password;
  52. }
  53. if(localStorage.rememberPassword){
  54. this.rememberPassword = true;
  55. }
  56. },
  57. methods: {
  58. randomNum(min, max) {
  59. return Math.floor(Math.random() * (max - min) + min);
  60. },
  61. refreshCode() {
  62. this.identifyCode = "";
  63. this.makeCode(this.identifyCodes, 4);
  64. },
  65. makeCode(o, l) {
  66. for (let i = 0; i < l; i++) {
  67. this.identifyCode += this.identifyCodes[
  68. this.randomNum(0, this.identifyCodes.length)
  69. ];
  70. }
  71. },
  72. checkValid(){
  73. if(this.iCode.length === 0){
  74. this.$message({
  75. showClose: true,
  76. message: '未填写验证码',
  77. type: 'error'
  78. });
  79. this.$refs.iCode.focus();
  80. return false;
  81. }
  82. if(this.iCode !== this.identifyCode){
  83. this.$message({
  84. showClose: true,
  85. message: '验证码有误',
  86. type: 'error'
  87. });
  88. this.$refs.iCode.focus();
  89. this.refreshCode();
  90. return false;
  91. }
  92. if(this.phone.length < 11){
  93. if(this.phone.length === 0){
  94. this.$message({
  95. showClose: true,
  96. message: '手机号码不得为空',
  97. type: 'error'
  98. });
  99. }else{
  100. this.$message({
  101. showClose: true,
  102. message: '手机号码格式有误',
  103. type: 'error'
  104. });
  105. }
  106. this.$refs.phone.focus();
  107. this.refreshCode();
  108. return false;
  109. }
  110. if(this.password.length === 0){
  111. this.$message({
  112. showClose: true,
  113. message: '密码不得为空',
  114. type: 'error'
  115. });
  116. this.$refs.password.focus();
  117. this.refreshCode();
  118. return false;
  119. }
  120. return true;
  121. },
  122. login() {
  123. let isValid = this.checkValid();
  124. if(!isValid){
  125. return;
  126. }
  127. this.loading = true;
  128. this.$api.$post('/user/login', {
  129. phone: this.phone,
  130. password: this.password
  131. }).then((res) =>
  132. {
  133. if(res.code === 0){
  134. localStorage.phone=this.phone;
  135. if(this.rememberPassword){
  136. localStorage.password = this.password;
  137. localStorage.rememberPassword = this.rememberPassword;
  138. }else{
  139. localStorage.removeItem("password");
  140. localStorage.removeItem("rememberPassword");
  141. }
  142. // this.$store.commit("saveUser", res.data.user);
  143. // this.$store.commit("saveToken", res.data.token);
  144. this.$message({
  145. showClose: true,
  146. message: "欢迎回来," + res.data.name,
  147. type: 'success'
  148. });
  149. window.location.href = res.data.path + "?id_token=" + res.data.token;
  150. }else{
  151. this.$message({
  152. showClose: true,
  153. message: res.message,
  154. type: 'error'
  155. });
  156. this.refreshCode();
  157. }
  158. }
  159. );
  160. this.loading = false;
  161. },
  162. }
  163. };
  164. </script>
  165. <style lang="scss" scoped>
  166. @import '~/assets/css/login.scss';
  167. </style>