Login.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. <template>
  2. <div>
  3. <div class="fh-login">
  4. <div class="fh-login-left">
  5. <h2 style='padding-bottom: 0px'>CNTHB HITCH ABB</h2>
  6. <h2 style='font-size:30px'>设备管理系统</h2>
  7. </div>
  8. <div class="fh-login-right">
  9. <div class="login-title">欢迎使用</div>
  10. <a-form
  11. id="formLogin"
  12. ref="formLogin"
  13. :form="form"
  14. class="login-form"
  15. @submit="handleSubmit">
  16. <a-form-item>
  17. <a-input
  18. size="large"
  19. type="text"
  20. placeholder="请输入账户"
  21. class="login-input"
  22. style="margin-top:40px"
  23. v-decorator="[
  24. 'username',
  25. {rules: [{ required: true, message: '请输入帐户名或邮箱地址' }, { validator: handleUsernameOrEmail }], validateTrigger: 'change'}
  26. ]"
  27. >
  28. <a-icon slot="prefix" type="user" :style="{ color: 'rgba(0,0,0,.25)' }"/>
  29. </a-input>
  30. </a-form-item>
  31. <a-form-item>
  32. <a-input
  33. size="large"
  34. type="password"
  35. autocomplete="false"
  36. placeholder="请输入密码"
  37. class="login-input"
  38. max="6"
  39. v-decorator="[
  40. 'password',
  41. {rules: [{ required: true, message: '请输入密码' }], validateTrigger: 'blur'}
  42. ]"
  43. >
  44. <a-icon slot="prefix" type="lock" :style="{ color: 'rgba(0,0,0,.25)' }"/>
  45. </a-input>
  46. </a-form-item>
  47. <a-form-item>
  48. <a-checkbox :checked="remberMe" v-model="remberMe">记住密码</a-checkbox>
  49. </a-form-item>
  50. <a-form-item>
  51. <a-button class="btn" type="primary" htmlType="submit">
  52. 登录
  53. </a-button>
  54. </a-form-item>
  55. </a-form>
  56. </div>
  57. </div>
  58. </div>
  59. </template>
  60. <script>
  61. import TwoStepCaptcha from '@/components/tools/TwoStepCaptcha'
  62. import { mapActions } from 'vuex'
  63. import { timeFix } from '@/utils/util'
  64. import { getSmsCaptcha, checkCaptchaCode } from '@/api/upms/login'
  65. import { encryptPassword } from '@/common/common'
  66. import cookie from 'vue-cookie'
  67. import BaseTool from '@/utils/tool'
  68. export default {
  69. components: {
  70. TwoStepCaptcha
  71. },
  72. data () {
  73. return {
  74. customActiveKey: 'tab1',
  75. loginBtn: false,
  76. // login type: 0 email, 1 username, 2 telephone
  77. loginType: 0,
  78. randomStr: '',
  79. captchaImage: null,
  80. isLoginError: false,
  81. requiredTwoStepCaptcha: false,
  82. stepCaptchaVisible: false,
  83. form: this.$form.createForm(this),
  84. state: {
  85. time: 60,
  86. loginBtn: false,
  87. // login type: 0 email, 1 username, 2 telephone
  88. loginType: 0,
  89. smsSendBtn: false
  90. },
  91. remberMe: null,
  92. password: null
  93. }
  94. },
  95. created () {
  96. // get2step({ })
  97. // .then(res => {
  98. // this.requiredTwoStepCaptcha = res.result.stepCode
  99. // })
  100. // .catch(() => {
  101. // this.requiredTwoStepCaptcha = false
  102. // })
  103. // this.requiredTwoStepCaptcha = true
  104. // 获取用户名和密码
  105. let username = cookie.get('username')
  106. if (BaseTool.Object.isBlank(username)) {
  107. username = ''
  108. }
  109. const password = cookie.get('password')
  110. console.log(username, password, 33333)
  111. this.remberMe = this.getRemberMe()
  112. const { form: { setFieldsValue } } = this
  113. this.$nextTick(() => {
  114. setFieldsValue({
  115. username: username,
  116. password
  117. })
  118. })
  119. this.getCaptchaImage()
  120. },
  121. methods: {
  122. ...mapActions(['Login', 'Logout']),
  123. // handler
  124. handleUsernameOrEmail (rule, value, callback) {
  125. const { state } = this
  126. const regex = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((\.[a-zA-Z0-9_-]{2,3}){1,2})$/
  127. if (regex.test(value)) {
  128. state.loginType = 0
  129. } else {
  130. state.loginType = 1
  131. }
  132. callback()
  133. },
  134. handleTabClick (key) {
  135. this.customActiveKey = key
  136. // this.form.resetFields()
  137. },
  138. handleSubmit (e) {
  139. e.preventDefault()
  140. const {
  141. form: { validateFields },
  142. state,
  143. customActiveKey,
  144. Login
  145. } = this
  146. state.loginBtn = true
  147. const validateFieldsKey = customActiveKey === 'tab1' ? ['username', 'password', 'captchaCode'] : ['mobile', 'smsCode', 'captchaCode']
  148. validateFields(validateFieldsKey, { force: true }, (err, values) => {
  149. if (!err) {
  150. const loginParams = {
  151. ...values,
  152. randomStr: this.randomStr,
  153. password: values.password ? encryptPassword(values.password) : '',
  154. grant_type: 'password',
  155. scope: 'server',
  156. loginType: customActiveKey === 'tab1' ? 1 : 2
  157. }
  158. // 记住密码的时候直接使用cookie中的密码
  159. if (values.password === cookie.get('password')) {
  160. loginParams.password = cookie.get('password')
  161. }
  162. this.password = loginParams.password
  163. delete loginParams.username
  164. loginParams[!state.loginType ? 'email' : 'username'] = values.username
  165. // loginParams.password = md5(values.password)
  166. Login(loginParams)
  167. .then((res) => this.loginSuccess(res))
  168. .catch(err => console.log(err))
  169. .finally(() => {
  170. state.loginBtn = false
  171. this.getCaptchaImage()
  172. })
  173. } else {
  174. setTimeout(() => {
  175. state.loginBtn = false
  176. }, 600)
  177. }
  178. })
  179. },
  180. getCaptcha (e) {
  181. e.preventDefault()
  182. const { form: { validateFields }, state } = this
  183. validateFields(['mobile', 'captchaCode'], { force: true }, (err, values) => {
  184. if (!err) {
  185. checkCaptchaCode({ captchaCode: values.captchaCode, randomStr: this.randomStr }).then(res => {
  186. const flag = res.data
  187. if (flag) {
  188. state.smsSendBtn = true
  189. const interval = window.setInterval(() => {
  190. if (state.time-- <= 0) {
  191. state.time = 60
  192. state.smsSendBtn = false
  193. window.clearInterval(interval)
  194. }
  195. }, 1000)
  196. const hide = this.$message.loading('验证码发送中..', 0)
  197. getSmsCaptcha({ mobile: values.mobile, captchaCode: values.captchaCode, randomStr: this.randomStr }).then(res => {
  198. setTimeout(hide, 2500)
  199. this.$notification['success']({
  200. message: '提示',
  201. description: '验证码获取成功,您的验证码为:' + res.data,
  202. duration: 8
  203. })
  204. }).catch(err => {
  205. setTimeout(hide, 1)
  206. clearInterval(interval)
  207. state.time = 60
  208. state.smsSendBtn = false
  209. this.requestFailed(err)
  210. })
  211. } else {
  212. this.$message.error('图片验证码错误')
  213. this.getCaptchaImage()
  214. }
  215. })
  216. }
  217. })
  218. },
  219. stepCaptchaSuccess () {
  220. this.loginSuccess()
  221. },
  222. stepCaptchaCancel () {
  223. this.Logout().then(() => {
  224. this.loginBtn = false
  225. this.stepCaptchaVisible = false
  226. })
  227. },
  228. loginSuccess (res) {
  229. this.$router.push({ path: '/WorkplaceBacklog' })
  230. // 延迟 1 秒显示欢迎信息
  231. setTimeout(() => {
  232. this.$notification.success({
  233. message: '欢迎',
  234. description: `${timeFix()},欢迎回来`
  235. })
  236. }, 1000)
  237. this.isLoginError = false
  238. // 记住密码
  239. cookie.set('remberMe', this.remberMe, 7)
  240. cookie.set('username', this.form.getFieldValue('username'), 7)
  241. if (this.remberMe) {
  242. cookie.set('password', this.password, 7)
  243. } else {
  244. cookie.delete('password')
  245. }
  246. },
  247. requestFailed (err) {
  248. console.log(err)
  249. this.isLoginError = true
  250. this.$notification['error']({
  251. message: '错误',
  252. description: ((err.response || {}).data || {}).message || '请求出现错误,请稍后再试',
  253. duration: 4
  254. })
  255. },
  256. getCaptchaImage () {
  257. this.randomStr = this.getRandomStr()
  258. // this.captchaImage = location.protocol + '//' + location.host + '/api/verify/captcha/' + this.randomStr
  259. this.captchaImage = process.env.VUE_APP_API_BASE_URL + '/verify/captcha/' + this.randomStr
  260. // getCaptchaImage(this.randomStr).then(res)
  261. },
  262. getRandomStr () {
  263. return new Date().getTime() + Math.random() * 1000
  264. },
  265. remberPassword (param) {
  266. console.log(param)
  267. },
  268. getRemberMe () {
  269. const remberMe = cookie.get('remberMe')
  270. if (BaseTool.Object.isBlank(remberMe)) {
  271. return true
  272. } else {
  273. return remberMe === 'true'
  274. }
  275. }
  276. }
  277. }
  278. </script>
  279. <style scoped>
  280. .fh-login {
  281. width: 830px;
  282. height: 435px;
  283. position: fixed;
  284. top: 50%;
  285. left: 50%;
  286. margin-top: -250px;
  287. margin-left: -440px;
  288. background: url('~@/assets/logn.png') no-repeat;
  289. }
  290. .fh-login .fh-login-left {
  291. float: left;
  292. width: 50%;
  293. }
  294. .fh-login .fh-login-left img {
  295. margin: 132px auto 0;
  296. display: block;
  297. }
  298. .fh-login .fh-login-left h2 {
  299. margin-top: 65px;
  300. margin-left: 8%;
  301. padding-bottom: 20px;
  302. color: #ffffff;
  303. font-size: 40px;
  304. font-weight: 600;
  305. text-align: center;
  306. }
  307. .fh-login .fh-login-right {
  308. float: right;
  309. width: 48%;
  310. }
  311. .fh-login-right .login-title {
  312. font-size: 24px;
  313. color: #333;
  314. padding: 15px 0px;
  315. border-bottom: 1px solid #ccc;
  316. text-align: center;
  317. }
  318. .fh-login .btn {
  319. display: inline-block;
  320. color: #FFF !important;
  321. text-shadow: 0 -1px 0 rgba(0, 0, 0, 0.25) !important;
  322. border: 5px solid #FFF;
  323. border-radius: 0;
  324. box-shadow: none !important;
  325. -webkit-transition: background-color 0.15s, border-color 0.15s, opacity 0.15s;
  326. -o-transition: background-color 0.15s, border-color 0.15s, opacity 0.15s;
  327. transition: background-color 0.15s, border-color 0.15s, opacity 0.15s;
  328. cursor: pointer;
  329. vertical-align: middle;
  330. width: 83%;
  331. height: 52px;
  332. border: none;
  333. margin-top:15px;
  334. margin-left: 12px;
  335. background: rgb(82, 166, 212) !important;
  336. color: rgb(255, 255, 255);
  337. user-select: none;
  338. }
  339. .fh-login .login-input{
  340. border: none!important;
  341. outline: none!important;
  342. background-color: #fff!important;
  343. height: 42px!important;
  344. width: 87%!important;
  345. /*margin-left: 35px;*/
  346. }
  347. /*设置输入图标大小*/
  348. .ant-input-affix-wrapper{
  349. font-size: 24px;
  350. }
  351. .login-form{
  352. margin-left: 35px;
  353. }
  354. /*调整图标位置*/
  355. .login-form .ant-input-affix-wrapper .ant-input-prefix {
  356. left: 9px !important;
  357. }
  358. .login-form .ant-input-affix-wrapper .ant-input:not(:first-child){
  359. padding-left: 38px;
  360. }
  361. </style>