login.vue 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. </div>
  20. </template>
  21. <script>
  22. import { mapState } from 'vuex'
  23. import SIdentify from "~/components/Identify";
  24. export default {
  25. name: "login",
  26. layout: "blank",
  27. data(){
  28. return{
  29. phone: '',
  30. password: '',
  31. identifyCodes: ['0','1', '2', '3', '4', '5', '6', '7', '8', '9'],
  32. identifyCode: '',
  33. iCode: '',
  34. rememberPassword: false,
  35. loading: false,
  36. canvasWidth: 0,
  37. }
  38. },
  39. components: {
  40. SIdentify: SIdentify
  41. },
  42. mounted(){
  43. this.identifyCode = "";
  44. this.makeCode(this.identifyCodes, 4);
  45. this.canvasWidth = this.$refs.canvas.clientWidth;
  46. if(localStorage.phone){
  47. localStorage.phone = this.phone;
  48. }
  49. if(localStorage.password){
  50. localStorage.password = this.password;
  51. }
  52. if(localStorage.rememberPassword){
  53. localStorage.rememberPassword = this.rememberPassword;
  54. }
  55. },
  56. methods: {
  57. randomNum(min, max) {
  58. return Math.floor(Math.random() * (max - min) + min);
  59. },
  60. refreshCode() {
  61. this.identifyCode = "";
  62. this.makeCode(this.identifyCodes, 4);
  63. },
  64. makeCode(o, l) {
  65. for (let i = 0; i < l; i++) {
  66. this.identifyCode += this.identifyCodes[
  67. this.randomNum(0, this.identifyCodes.length)
  68. ];
  69. }
  70. },
  71. checkValid(){
  72. if(this.iCode.length === 0){
  73. this.$message({
  74. showClose: true,
  75. message: '未填写验证码',
  76. type: 'error'
  77. });
  78. this.$refs.iCode.focus();
  79. return false;
  80. }
  81. if(this.iCode !== this.identifyCode){
  82. this.$message({
  83. showClose: true,
  84. message: '验证码有误',
  85. type: 'error'
  86. });
  87. this.$refs.iCode.focus();
  88. this.refreshCode();
  89. return false;
  90. }
  91. if(this.phone.length < 11){
  92. if(this.phone.length === 0){
  93. this.$message({
  94. showClose: true,
  95. message: '手机号码不得为空',
  96. type: 'error'
  97. });
  98. }else{
  99. this.$message({
  100. showClose: true,
  101. message: '手机号码格式有误',
  102. type: 'error'
  103. });
  104. }
  105. this.$refs.phone.focus();
  106. this.refreshCode();
  107. return false;
  108. }
  109. if(this.password.length === 0){
  110. this.$message({
  111. showClose: true,
  112. message: '密码不得为空',
  113. type: 'error'
  114. });
  115. this.$refs.password.focus();
  116. this.refreshCode();
  117. return false;
  118. }
  119. return true;
  120. },
  121. login() {
  122. let isValid = this.checkValid();
  123. if(!isValid){
  124. return;
  125. }
  126. this.loading = true;
  127. this.$api.$post('/user/login', {
  128. phone: this.phone,
  129. password: this.password
  130. }).then((res) =>
  131. {
  132. if(res.code === 0){
  133. localStorage.phone=this.phone;
  134. if(this.rememberPassword){
  135. localStorage.password = this.password;
  136. localStorage.rememberPassword = this.rememberPassword;
  137. }else{
  138. localStorage.removeItem("password");
  139. }
  140. this.$store.commit("saveUser", res.data.user);
  141. this.$store.commit("saveToken", res.data.token);
  142. this.$message({
  143. showClose: true,
  144. message: "欢迎回来," + res.data.name,
  145. type: 'success'
  146. });
  147. }else{
  148. this.$message({
  149. showClose: true,
  150. message: res.message,
  151. type: 'error'
  152. });
  153. this.refreshCode();
  154. }
  155. }
  156. );
  157. this.loading = false;
  158. },
  159. }
  160. };
  161. </script>
  162. <style lang="scss" scoped>
  163. @import '~/assets/css/login.scss';
  164. </style>