Login.vue 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. <template>
  2. <div class="main">
  3. <a-form
  4. id="formLogin"
  5. class="user-layout-login"
  6. ref="formLogin"
  7. :form="form"
  8. @submit="handleSubmit"
  9. >
  10. <a-tabs
  11. :activeKey="customActiveKey"
  12. :tabBarStyle="{ textAlign: 'center', borderBottom: 'unset' }"
  13. @change="handleTabClick"
  14. >
  15. <a-tab-pane key="tab1" tab="账号密码登录">
  16. <a-alert v-if="isLoginError" type="error" showIcon style="margin-bottom: 24px;" message="账户或密码错误(admin/ant.design )" />
  17. <a-form-item>
  18. <a-input
  19. size="large"
  20. type="text"
  21. placeholder="账户: admin"
  22. v-decorator="[
  23. 'username',
  24. {rules: [{ required: true, message: '请输入帐户名或邮箱地址' }, { validator: handleUsernameOrEmail }], validateTrigger: 'change'}
  25. ]"
  26. >
  27. <a-icon slot="prefix" type="user" :style="{ color: 'rgba(0,0,0,.25)' }"/>
  28. </a-input>
  29. </a-form-item>
  30. <a-form-item>
  31. <a-input
  32. size="large"
  33. type="password"
  34. autocomplete="false"
  35. placeholder="密码: admin or ant.design"
  36. v-decorator="[
  37. 'password',
  38. {rules: [{ required: true, message: '请输入密码' }], validateTrigger: 'blur'}
  39. ]"
  40. >
  41. <a-icon slot="prefix" type="lock" :style="{ color: 'rgba(0,0,0,.25)' }"/>
  42. </a-input>
  43. </a-form-item>
  44. </a-tab-pane>
  45. <a-tab-pane key="tab2" tab="手机号登录">
  46. <a-form-item>
  47. <a-input size="large" type="text" placeholder="手机号" v-decorator="['mobile', {rules: [{ required: true, pattern: /^1[34578]\d{9}$/, message: '请输入正确的手机号' }], validateTrigger: 'change'}]">
  48. <a-icon slot="prefix" type="mobile" :style="{ color: 'rgba(0,0,0,.25)' }"/>
  49. </a-input>
  50. </a-form-item>
  51. <a-row :gutter="16">
  52. <a-col class="gutter-row" :span="16">
  53. <a-form-item>
  54. <a-input size="large" type="text" placeholder="验证码" v-decorator="['captcha', {rules: [{ required: true, message: '请输入验证码' }], validateTrigger: 'blur'}]">
  55. <a-icon slot="prefix" type="mail" :style="{ color: 'rgba(0,0,0,.25)' }"/>
  56. </a-input>
  57. </a-form-item>
  58. </a-col>
  59. <a-col class="gutter-row" :span="8">
  60. <a-button
  61. class="getCaptcha"
  62. tabindex="-1"
  63. :disabled="state.smsSendBtn"
  64. @click.stop.prevent="getCaptcha"
  65. v-text="!state.smsSendBtn && '获取验证码' || (state.time+' s')"
  66. ></a-button>
  67. </a-col>
  68. </a-row>
  69. </a-tab-pane>
  70. </a-tabs>
  71. <a-form-item>
  72. <a-checkbox v-decorator="['rememberMe']">自动登录</a-checkbox>
  73. <router-link
  74. :to="{ name: 'recover', params: { user: 'aaa'} }"
  75. class="forge-password"
  76. style="float: right;"
  77. >忘记密码</router-link>
  78. </a-form-item>
  79. <a-form-item style="margin-top:24px">
  80. <a-button
  81. size="large"
  82. type="primary"
  83. htmlType="submit"
  84. class="login-button"
  85. :loading="state.loginBtn"
  86. :disabled="state.loginBtn"
  87. >确定</a-button>
  88. </a-form-item>
  89. <div class="user-login-other">
  90. <span>其他登录方式</span>
  91. <a>
  92. <a-icon class="item-icon" type="alipay-circle"></a-icon>
  93. </a>
  94. <a>
  95. <a-icon class="item-icon" type="taobao-circle"></a-icon>
  96. </a>
  97. <a>
  98. <a-icon class="item-icon" type="weibo-circle"></a-icon>
  99. </a>
  100. <router-link class="register" :to="{ name: 'register' }">注册账户</router-link>
  101. </div>
  102. </a-form>
  103. <two-step-captcha
  104. v-if="requiredTwoStepCaptcha"
  105. :visible="stepCaptchaVisible"
  106. @success="stepCaptchaSuccess"
  107. @cancel="stepCaptchaCancel"
  108. ></two-step-captcha>
  109. </div>
  110. </template>
  111. <script>
  112. import md5 from 'md5'
  113. import TwoStepCaptcha from '@/components/tools/TwoStepCaptcha'
  114. import { mapActions } from 'vuex'
  115. import { timeFix } from '@/utils/util'
  116. import { getSmsCaptcha, get2step } from '@/api/login'
  117. export default {
  118. components: {
  119. TwoStepCaptcha
  120. },
  121. data () {
  122. return {
  123. customActiveKey: 'tab1',
  124. loginBtn: false,
  125. // login type: 0 email, 1 username, 2 telephone
  126. loginType: 0,
  127. isLoginError: false,
  128. requiredTwoStepCaptcha: false,
  129. stepCaptchaVisible: false,
  130. form: this.$form.createForm(this),
  131. state: {
  132. time: 60,
  133. loginBtn: false,
  134. // login type: 0 email, 1 username, 2 telephone
  135. loginType: 0,
  136. smsSendBtn: false
  137. }
  138. }
  139. },
  140. created () {
  141. get2step({ })
  142. .then(res => {
  143. this.requiredTwoStepCaptcha = res.result.stepCode
  144. })
  145. .catch(() => {
  146. this.requiredTwoStepCaptcha = false
  147. })
  148. // this.requiredTwoStepCaptcha = true
  149. },
  150. methods: {
  151. ...mapActions(['Login', 'Logout']),
  152. // handler
  153. handleUsernameOrEmail (rule, value, callback) {
  154. const { state } = this
  155. const regex = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$/
  156. if (regex.test(value)) {
  157. state.loginType = 0
  158. } else {
  159. state.loginType = 1
  160. }
  161. callback()
  162. },
  163. handleTabClick (key) {
  164. this.customActiveKey = key
  165. // this.form.resetFields()
  166. },
  167. handleSubmit (e) {
  168. e.preventDefault()
  169. const {
  170. form: { validateFields },
  171. state,
  172. customActiveKey,
  173. Login
  174. } = this
  175. state.loginBtn = true
  176. const validateFieldsKey = customActiveKey === 'tab1' ? ['username', 'password'] : ['mobile', 'captcha']
  177. validateFields(validateFieldsKey, { force: true }, (err, values) => {
  178. if (!err) {
  179. console.log('login form', values)
  180. const loginParams = { ...values }
  181. delete loginParams.username
  182. loginParams[!state.loginType ? 'email' : 'username'] = values.username
  183. loginParams.password = md5(values.password)
  184. Login(loginParams)
  185. .then((res) => this.loginSuccess(res))
  186. .catch(err => this.requestFailed(err))
  187. .finally(() => {
  188. state.loginBtn = false
  189. })
  190. } else {
  191. setTimeout(() => {
  192. state.loginBtn = false
  193. }, 600)
  194. }
  195. })
  196. },
  197. getCaptcha (e) {
  198. e.preventDefault()
  199. const { form: { validateFields }, state } = this
  200. validateFields(['mobile'], { force: true }, (err, values) => {
  201. if (!err) {
  202. state.smsSendBtn = true
  203. const interval = window.setInterval(() => {
  204. if (state.time-- <= 0) {
  205. state.time = 60
  206. state.smsSendBtn = false
  207. window.clearInterval(interval)
  208. }
  209. }, 1000)
  210. const hide = this.$message.loading('验证码发送中..', 0)
  211. getSmsCaptcha({ mobile: values.mobile }).then(res => {
  212. setTimeout(hide, 2500)
  213. this.$notification['success']({
  214. message: '提示',
  215. description: '验证码获取成功,您的验证码为:' + res.result.captcha,
  216. duration: 8
  217. })
  218. }).catch(err => {
  219. setTimeout(hide, 1)
  220. clearInterval(interval)
  221. state.time = 60
  222. state.smsSendBtn = false
  223. this.requestFailed(err)
  224. })
  225. }
  226. })
  227. },
  228. stepCaptchaSuccess () {
  229. this.loginSuccess()
  230. },
  231. stepCaptchaCancel () {
  232. this.Logout().then(() => {
  233. this.loginBtn = false
  234. this.stepCaptchaVisible = false
  235. })
  236. },
  237. loginSuccess (res) {
  238. console.log(res)
  239. this.$router.push({ name: 'dashboard' })
  240. this.isLoginError = false
  241. // 延迟 1 秒显示欢迎信息
  242. setTimeout(() => {
  243. this.$notification.success({
  244. message: '欢迎',
  245. description: `${timeFix()},欢迎回来`
  246. })
  247. }, 1000)
  248. },
  249. requestFailed (err) {
  250. this.isLoginError = true
  251. this.$notification['error']({
  252. message: '错误',
  253. description: ((err.response || {}).data || {}).message || '请求出现错误,请稍后再试',
  254. duration: 4
  255. })
  256. }
  257. }
  258. }
  259. </script>
  260. <style lang="less" scoped>
  261. .user-layout-login {
  262. label {
  263. font-size: 14px;
  264. }
  265. .getCaptcha {
  266. display: block;
  267. width: 100%;
  268. height: 40px;
  269. }
  270. .forge-password {
  271. font-size: 14px;
  272. }
  273. button.login-button {
  274. padding: 0 15px;
  275. font-size: 16px;
  276. height: 40px;
  277. width: 100%;
  278. }
  279. .user-login-other {
  280. text-align: left;
  281. margin-top: 24px;
  282. line-height: 22px;
  283. .item-icon {
  284. font-size: 24px;
  285. color: rgba(0, 0, 0, 0.2);
  286. margin-left: 16px;
  287. vertical-align: middle;
  288. cursor: pointer;
  289. transition: color 0.3s;
  290. &:hover {
  291. color: #1890ff;
  292. }
  293. }
  294. .register {
  295. float: right;
  296. }
  297. }
  298. }
  299. </style>