Login.vue 11 KB

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