Login.vue 9.8 KB

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