Login.vue 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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" style="margin-bottom: 24px;" message="账户或密码错误" /> -->
  17. <a-form-item>
  18. <a-input
  19. size="large"
  20. type="text"
  21. placeholder="账户 "
  22. v-decorator="[
  23. 'username',
  24. {initialValue: username ,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="密码"
  36. v-decorator="[
  37. 'password',
  38. {initialValue: password ,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 style="color: #ccc;" @change="rememberMe=!rememberMe" :checked="rememberMe" v-decorator="['rememberMe',{initialValue: 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. import { updateTheme } from '@/components/SettingDrawer/settingConfig'
  118. export default {
  119. components: {
  120. TwoStepCaptcha
  121. },
  122. data () {
  123. return {
  124. customActiveKey: 'tab1',
  125. loginBtn: false,
  126. // login type: 0 email, 1 username, 2 telephone
  127. loginType: 0,
  128. isLoginError: false,
  129. requiredTwoStepCaptcha: false,
  130. stepCaptchaVisible: false,
  131. form: this.$form.createForm(this),
  132. state: {
  133. time: 60,
  134. loginBtn: false,
  135. // login type: 0 email, 1 username, 2 telephone
  136. loginType: 0,
  137. smsSendBtn: false
  138. },
  139. rememberMe: true,
  140. username: '',
  141. password: ''
  142. }
  143. },
  144. created () {
  145. // get2step({ })
  146. // .then(res => {
  147. // this.requiredTwoStepCaptcha = res.result.stepCode
  148. // })
  149. // .catch(() => {
  150. // this.requiredTwoStepCaptcha = false
  151. // })
  152. // this.requiredTwoStepCaptcha = true
  153. const rem = this.$cookies.get('REMEMBER_ME')
  154. console.log(rem)
  155. if (rem && rem.rememberMe) {
  156. this.username = rem.username
  157. this.password = rem.password
  158. this.rememberMe = rem.rememberMe
  159. }
  160. },
  161. methods: {
  162. ...mapActions(['Login', 'Logout']),
  163. // handler
  164. handleUsernameOrEmail (rule, value, callback) {
  165. const { state } = this
  166. const regex = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$/
  167. if (regex.test(value)) {
  168. state.loginType = 0
  169. } else {
  170. state.loginType = 1
  171. }
  172. callback()
  173. },
  174. handleTabClick (key) {
  175. this.customActiveKey = key
  176. // this.form.resetFields()
  177. },
  178. handleSubmit (e) {
  179. e.preventDefault()
  180. const {
  181. form: { validateFields },
  182. state,
  183. customActiveKey,
  184. Login
  185. } = this
  186. // 清空仓库缓存数据
  187. this.$store.state.app.warehouseAuthList = null
  188. this.$store.state.app.warehouseAllList = null
  189. this.$store.state.app.productTypeAllList = null
  190. this.$store.state.app.productBrandAllList = null
  191. state.loginBtn = true
  192. const validateFieldsKey = customActiveKey === 'tab1' ? ['username', 'password', 'rememberMe'] : ['mobile', 'captcha']
  193. validateFields(validateFieldsKey, { force: true }, (err, values) => {
  194. if (!err) {
  195. const loginParams = { ...values }
  196. delete loginParams.username
  197. loginParams[!state.loginType ? 'email' : 'username'] = values.username
  198. loginParams.password = values.password
  199. console.log(loginParams, 'loginParams')
  200. Login(loginParams)
  201. .then((res) => this.loginSuccess(res))
  202. .catch(err => this.requestFailed(err))
  203. .finally(() => {
  204. state.loginBtn = false
  205. })
  206. } else {
  207. setTimeout(() => {
  208. state.loginBtn = false
  209. }, 600)
  210. }
  211. })
  212. },
  213. getCaptcha (e) {
  214. e.preventDefault()
  215. const { form: { validateFields }, state } = this
  216. validateFields(['mobile'], { force: true }, (err, values) => {
  217. if (!err) {
  218. state.smsSendBtn = true
  219. const interval = window.setInterval(() => {
  220. if (state.time-- <= 0) {
  221. state.time = 60
  222. state.smsSendBtn = false
  223. window.clearInterval(interval)
  224. }
  225. }, 1000)
  226. const hide = this.$message.loading('验证码发送中..', 0)
  227. getSmsCaptcha({ mobile: values.mobile }).then(res => {
  228. setTimeout(hide, 2500)
  229. this.$notification['success']({
  230. message: '提示',
  231. description: '验证码获取成功,您的验证码为:' + res.result.captcha,
  232. duration: 8
  233. })
  234. }).catch(err => {
  235. setTimeout(hide, 1)
  236. clearInterval(interval)
  237. state.time = 60
  238. state.smsSendBtn = false
  239. this.requestFailed(err)
  240. })
  241. }
  242. })
  243. },
  244. stepCaptchaSuccess () {
  245. this.loginSuccess()
  246. },
  247. stepCaptchaCancel () {
  248. this.Logout().then(() => {
  249. this.loginBtn = false
  250. this.stepCaptchaVisible = false
  251. })
  252. },
  253. loginSuccess (res) {
  254. console.log(res)
  255. // check res.homePage define, set $router.push name res.homePage
  256. // Why not enter onComplete
  257. this.$router.push({ name: 'index' }, () => {
  258. console.log('onComplete')
  259. this.$notification.success({
  260. message: '欢迎',
  261. description: `${timeFix()},欢迎回来`
  262. })
  263. })
  264. this.isLoginError = false
  265. },
  266. requestFailed (err) {
  267. console.log(err, 'error')
  268. this.isLoginError = true
  269. this.$notification['error']({
  270. message: '提示',
  271. description: ((err.response || {}).data || {}).message || err.message || '请求出现错误,请稍后再试',
  272. duration: 4
  273. })
  274. }
  275. }
  276. }
  277. </script>
  278. <style lang="less" scoped>
  279. .user-layout-login {
  280. label {
  281. font-size: 14px;
  282. }
  283. .getCaptcha {
  284. display: block;
  285. width: 100%;
  286. height: 40px;
  287. }
  288. .forge-password {
  289. font-size: 14px;
  290. }
  291. button.login-button {
  292. padding: 0 15px;
  293. font-size: 16px;
  294. height: 40px;
  295. width: 100%;
  296. }
  297. .user-login-other {
  298. text-align: left;
  299. margin-top: 24px;
  300. line-height: 22px;
  301. .item-icon {
  302. font-size: 24px;
  303. color: rgba(0, 0, 0, 0.2);
  304. margin-left: 16px;
  305. vertical-align: middle;
  306. cursor: pointer;
  307. transition: color 0.3s;
  308. &:hover {
  309. color: #1890ff;
  310. }
  311. }
  312. .register {
  313. float: right;
  314. }
  315. }
  316. }
  317. </style>