login.vue 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 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. this.phone = localStorage.phone
  48. }
  49. if (localStorage.password) {
  50. this.password = localStorage.password
  51. }
  52. if (localStorage.rememberPassword) {
  53. this.rememberPassword = true
  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. const 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. if (res.code === 0) {
  132. localStorage.phone = this.phone
  133. if (this.rememberPassword) {
  134. localStorage.password = this.password
  135. localStorage.rememberPassword = this.rememberPassword
  136. } else {
  137. localStorage.removeItem('password')
  138. localStorage.removeItem('rememberPassword')
  139. }
  140. console.log(res.data)
  141. this.$message({
  142. showClose: true,
  143. message: '欢迎回来,' + res.data.name,
  144. type: 'success'
  145. })
  146. this.$store.commit('saveToken', res.data.token)
  147. this.$store.commit('saveUser', res.data.user)
  148. if (!res.data.redirectUri) {
  149. this.$router.push('/')
  150. } else {
  151. window.location.href = res.data.redirectUri + '?id_token=' + res.data.token
  152. }
  153. } else {
  154. this.$message({
  155. showClose: true,
  156. message: res.message,
  157. type: 'error'
  158. })
  159. this.refreshCode()
  160. }
  161. }
  162. )
  163. this.loading = false
  164. }
  165. }
  166. }
  167. </script>
  168. <style lang="scss" scoped>
  169. @import '~/assets/css/login.scss';
  170. </style>