Login.vue 9.8 KB

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