login.vue 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <template>
  2. <div id="login-box">
  3. <div class="header">登录</div>
  4. <div class="main">
  5. <el-form ref="form" :model="form" :rules="rules">
  6. <el-form-item prop="principal">
  7. <el-input
  8. v-model="form.principal"
  9. placeholder="手机号"
  10. class="cuborder-radius"
  11. @keyup.enter.native="onSubmit('form')"
  12. />
  13. </el-form-item>
  14. <el-form-item prop="credential">
  15. <el-input
  16. v-model="form.credential"
  17. placeholder="验证码"
  18. class="cuborder-radius"
  19. maxlength="6"
  20. style="width: 205px"
  21. @keyup.enter.native="onSubmit('form')"
  22. />
  23. <div v-if="smsLock" class="send-code-btn send-sms-disable">
  24. 正在发送 ...
  25. </div>
  26. <div
  27. v-else-if="smsLock === false && smsLockObj.time === null"
  28. class="send-code-btn"
  29. @click="sendSms"
  30. >
  31. 获取验证码
  32. </div>
  33. <div v-else class="send-code-btn send-sms-disable">
  34. 重新发送({{ smsLockObj.time }}s)
  35. </div>
  36. </el-form-item>
  37. <el-form-item prop="captchaCode">
  38. <el-row>
  39. <el-col :span="12">
  40. <el-input
  41. v-model="form.captchaCode"
  42. placeholder="图形验证码"
  43. class="cuborder-radius"
  44. maxlength="11"
  45. @keyup.enter.native="onSubmit('form')"
  46. />
  47. </el-col>
  48. <el-col :span="12">
  49. <img :src="captchaCode" alt="图形验证码" title="点击刷新" style="cursor:pointer;" @click="getCaptcha">
  50. </el-col>
  51. </el-row>
  52. </el-form-item>
  53. <el-form-item>
  54. <el-button
  55. type="primary"
  56. class="submit-btn"
  57. :loading="loginLoading"
  58. @click="onSubmit('form')"
  59. >立即登录
  60. </el-button>
  61. </el-form-item>
  62. <el-form-item>
  63. <div class="links">
  64. <el-link
  65. type="primary"
  66. :underline="false"
  67. @click="toLink('/sso/forget')"
  68. >找回密码
  69. </el-link>
  70. <el-link
  71. type="primary"
  72. :underline="false"
  73. @click="toLink('/sso/login1')"
  74. >密码登录
  75. </el-link>
  76. </div>
  77. </el-form-item>
  78. </el-form>
  79. </div>
  80. </div>
  81. </template>
  82. <script>
  83. import { ServeCaptcha, ServeLogin, ServePubkey, ServeSendVerifyCode } from '@/api/account'
  84. import { userMixin } from 'assets/js/mixin'
  85. import { isMobile } from '@/utils/validate'
  86. import SmsLock from '@/utils/sms-lock'
  87. import { JSEncrypt } from 'jsencrypt'
  88. import Vue from 'vue'
  89. export default {
  90. mixins: [userMixin],
  91. data() {
  92. const validateMobile = (rule, value, callback) => {
  93. if (value === '') {
  94. callback(new Error('登录手机号不能为空!'))
  95. } else {
  96. // isMobile(value) ? callback() : callback(new Error('登录手机号格式不正确!'))
  97. callback()
  98. }
  99. }
  100. return {
  101. loginLoading: false,
  102. form: {
  103. loginType: 1,
  104. plat: 1,
  105. principal: '',
  106. credential: '',
  107. captchaCode: ''
  108. },
  109. pubkey: {
  110. key: '',
  111. r: ''
  112. },
  113. captchaCode: '',
  114. rules: {
  115. principal: [
  116. {
  117. validator: validateMobile,
  118. trigger: 'blur'
  119. },
  120. {
  121. min: 1,
  122. max: 100,
  123. message: '手机号格式不正确!',
  124. trigger: 'blur'
  125. }
  126. ],
  127. credential: [
  128. {
  129. required: true,
  130. message: '登录密码不能为空!',
  131. trigger: 'blur'
  132. }
  133. ]
  134. },
  135. smsLock: false,
  136. smsLockObj: null
  137. }
  138. },
  139. created() {
  140. const userdata = Vue.$cookies.get('USERDATA')
  141. if (userdata !== null) {
  142. this.$router.push('/')
  143. } else {
  144. this.getPubkey()
  145. this.smsLockObj = new SmsLock('LOGIN_SMS', 60)
  146. }
  147. },
  148. destroyed() {
  149. this.smsLockObj.clearInterval()
  150. },
  151. methods: {
  152. onSubmit(formName) {
  153. if (this.loginLoading) return false
  154. this.$refs[formName].validate(valid => {
  155. if (!valid) return false
  156. this.loginLoading = true
  157. this.login()
  158. })
  159. },
  160. getCaptcha() {
  161. ServeCaptcha().then(res => {
  162. if (res.code === 0) {
  163. this.captchaCode = res.data
  164. } else {
  165. this.$notify.info({
  166. title: '提示',
  167. message: '获取 Captcha 失败, 请重新刷新页面...'
  168. })
  169. }
  170. }).finally(() => {
  171. })
  172. },
  173. getPubkey() {
  174. ServePubkey().then(res => {
  175. if (res.code === 0) {
  176. this.pubkey.key = res.data.pubkey
  177. this.pubkey.r = res.data.r
  178. this.getCaptcha()
  179. } else {
  180. this.$notify.info({
  181. title: '提示',
  182. message: '获取公钥失败, 请重新刷新页面...'
  183. })
  184. }
  185. }).finally(() => {
  186. })
  187. },
  188. encryptPassword(password) {
  189. var encryptor = new JSEncrypt()
  190. encryptor.setPublicKey(this.pubkey.key)
  191. return encryptor.encrypt(this.pubkey.r + password)
  192. },
  193. async login() {
  194. ServeLogin({
  195. loginType: this.form.loginType,
  196. plat: this.form.plat,
  197. principal: this.form.principal,
  198. credential: this.encryptPassword(this.form.credential),
  199. captchaCode: this.form.captchaCode
  200. }).then(res => {
  201. if (res.code === 0) {
  202. const resData = res.data
  203. /* const userInfo = resData.userInfo
  204. const userToken = resData.userToken
  205. setUserToken(userToken)
  206. this.$store.commit('UPDATE_USER_INFO', userInfo)*/
  207. this.$router.push('/')
  208. } else {
  209. this.$notify.info({
  210. title: '提示',
  211. message: res.msg
  212. })
  213. }
  214. }).finally(() => {
  215. this.loginLoading = false
  216. })
  217. },
  218. sendSms() {
  219. if (this.smsLock) return false
  220. if (!isMobile(this.form.username)) {
  221. this.$refs.form.validateField('username', (val) => {
  222. return !val
  223. })
  224. }
  225. this.smsLock = true
  226. ServeSendVerifyCode({
  227. receiver: this.form.principal,
  228. notifyType: 3
  229. }).then(res => {
  230. if (res.code === 0) {
  231. this.$notify({
  232. title: '成功',
  233. message: '验证码发送成功...',
  234. type: 'success'
  235. })
  236. this.smsLockObj.start()
  237. } else {
  238. this.$notify({
  239. title: '提示',
  240. message: res.message,
  241. customClass: 'cus-notifyclass'
  242. })
  243. }
  244. }).finally(() => {
  245. this.smsLock = false
  246. })
  247. },
  248. toLink(url) {
  249. this.$router.push({
  250. path: url
  251. })
  252. }
  253. }
  254. }
  255. </script>
  256. <style lang="scss" scoped>
  257. @import '~@/assets/css/login-auth.scss';
  258. </style>