login.vue 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. <template>
  2. <div class="login">
  3. <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
  4. <div class="title-container">
  5. <h3 class="title">{{ $t('login.title') }}</h3>
  6. <lang-select class="set-language"/>
  7. </div>
  8. <el-form-item prop="username">
  9. <el-input v-model="loginForm.username" type="text" auto-complete="off" :placeholder="$t('login.username')">
  10. <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon"/>
  11. </el-input>
  12. </el-form-item>
  13. <el-form-item prop="password">
  14. <el-input
  15. v-model="loginForm.password"
  16. type="password"
  17. auto-complete="off"
  18. :placeholder="$t('login.password')"
  19. @keyup.enter.native="handleLogin"
  20. >
  21. <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon"/>
  22. </el-input>
  23. </el-form-item>
  24. <!-- <el-form-item prop="code">-->
  25. <!-- <el-input-->
  26. <!-- v-model="loginForm.code"-->
  27. <!-- auto-complete="off"-->
  28. <!-- :placeholder="$t('login.code')"-->
  29. <!-- style="width: 63%"-->
  30. <!-- @keyup.enter.native="handleLogin"-->
  31. <!-- >-->
  32. <!-- <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />-->
  33. <!-- </el-input>-->
  34. <!-- <div class="login-code">-->
  35. <!-- <img :src="codeUrl" @click="getCode" class="login-code-img"/>-->
  36. <!-- </div>-->
  37. <!-- </el-form-item>-->
  38. <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">{{
  39. $t('login.rememberPassword')
  40. }}
  41. </el-checkbox>
  42. <el-form-item style="width:100%;">
  43. <el-button
  44. :loading="loading"
  45. size="medium"
  46. type="primary"
  47. style="width:100%;"
  48. @click.native.prevent="handleLogin"
  49. >
  50. <span v-if="!loading"> {{ $t('login.logIn') }}</span>
  51. <span v-else>{{ $t('login.loading') }}</span>
  52. </el-button>
  53. </el-form-item>
  54. <!-- <el-form-item style="width:100%;">-->
  55. <!-- <el-button-->
  56. <!-- :loading="loading"-->
  57. <!-- size="medium"-->
  58. <!-- type="primary"-->
  59. <!-- style="width:100%;"-->
  60. <!-- @click.native.prevent="doSocialLogin"-->
  61. <!-- >-->
  62. <!-- <span v-if="!loading"> 员工卡登录 </span>-->
  63. <!-- <span v-else>{{ $t('login.loading') }}</span>-->
  64. <!-- </el-button>-->
  65. <!-- </el-form-item>-->
  66. <el-form-item style="width:100%;">
  67. <el-button
  68. :loading="loading"
  69. size="medium"
  70. type="primary"
  71. style="width:100%;"
  72. @click.native.prevent="doAzureLogin"
  73. >
  74. <span v-if="!loading"> Azure登录 </span>
  75. <span v-else>{{ $t('login.loading') }}</span>
  76. </el-button>
  77. </el-form-item>
  78. </el-form>
  79. <!-- 底部 -->
  80. <div class="el-login-footer">
  81. <span>Copyright © 2020-2022 Seashore.ept All Rights Reserved.</span>
  82. </div>
  83. </div>
  84. </template>
  85. <script>
  86. import {getCodeImg} from "@/api/login";
  87. import Cookies from "js-cookie";
  88. import {encrypt, decrypt} from '@/utils/jsencrypt'
  89. import LangSelect from '@/components/LangSelect'
  90. export default {
  91. name: "Login",
  92. components: {LangSelect},
  93. data() {
  94. return {
  95. codeUrl: "",
  96. cookiePassword: "",
  97. loginForm: {
  98. username: "",
  99. password: "",
  100. rememberMe: false,
  101. code: "",
  102. uuid: ""
  103. },
  104. loginRules: {
  105. username: [
  106. {required: true, trigger: "blur", message: this.$t('login.usernameNotEmpty')}
  107. ],
  108. password: [
  109. {required: true, trigger: "blur", message: this.$t('login.passwordNotEmpty')}
  110. ],
  111. code: [{required: true, trigger: "change", message: this.$t('login.codeNotEmpty')}]
  112. },
  113. loading: false,
  114. redirect: undefined
  115. };
  116. },
  117. watch: {
  118. $route: {
  119. handler: function (route) {
  120. this.redirect = route.query && route.query.redirect;
  121. },
  122. immediate: true
  123. }
  124. },
  125. created() {
  126. // Azure登录跳转
  127. let code = window.location.search.replace("?code=" , '');
  128. if (code) {
  129. // authorization_code
  130. code = code.substring(0, code.indexOf("&"));
  131. // redirect_url
  132. window.location.href = '#/azureLogin?code='+code;
  133. }
  134. this.getCode();
  135. this.getCookie();
  136. if (!this.$store.getters.language) {
  137. console.log("默认中文")
  138. this.$i18n.locale = 'zh'
  139. this.$store.dispatch('app/setLanguage', 'zh')
  140. }
  141. },
  142. methods: {
  143. getCode() {
  144. getCodeImg().then(res => {
  145. this.codeUrl = "data:image/gif;base64," + res.img;
  146. this.loginForm.uuid = res.uuid;
  147. });
  148. },
  149. getCookie() {
  150. const username = Cookies.get("username");
  151. const password = Cookies.get("password");
  152. const rememberMe = Cookies.get('rememberMe')
  153. this.loginForm = {
  154. username: username === undefined ? this.loginForm.username : username,
  155. password: password === undefined ? this.loginForm.password : decrypt(password),
  156. rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
  157. };
  158. },
  159. handleLogin() {
  160. this.$refs.loginForm.validate(valid => {
  161. if (valid) {
  162. this.loading = true;
  163. if (this.loginForm.rememberMe) {
  164. Cookies.set("username", this.loginForm.username, {expires: 30});
  165. Cookies.set("password", encrypt(this.loginForm.password), {expires: 30});
  166. Cookies.set('rememberMe', this.loginForm.rememberMe, {expires: 30});
  167. } else {
  168. Cookies.remove("username");
  169. Cookies.remove("password");
  170. Cookies.remove('rememberMe');
  171. }
  172. this.$store
  173. .dispatch("Login", this.loginForm)
  174. .then(() => {
  175. this.$router.push({path: this.redirect || "/404"});
  176. })
  177. .catch(() => {
  178. this.loading = false;
  179. this.getCode();
  180. });
  181. }
  182. });
  183. },
  184. doSocialLogin() {
  185. window.location.href = 'https://gitee.com/oauth/authorize?client_id=e7faeabf239846288ee07e6c40066cbd0dcc46cb1c1dea37c602c29a2368c6b8&redirect_uri=http%3A%2F%2Flocalhost%2Fcpms%2Findex.html%23%2FsocialLogin&response_type=code';
  186. },
  187. /** Azure登录 */
  188. doAzureLogin() {
  189. // TODO: 1. 修改authorize请求链接、2. 修改client_id、3. 修改scope
  190. // 1. https://login.microsoftonline.com/ecaa386b-c8df-4ce0-ad01-740cbdb5ba55/oauth2/v2.0/authorize
  191. // 2. client_id=?
  192. // 3. scope=profile openid offline_access
  193. window.location.href = 'https://login.microsoftonline.com/7503e40a-97ec-4eb9-bf6d-2836e57e882d/oauth2/v2.0/authorize?client_id=3db6f125-db4d-456b-a76e-a6d03182e845&redirect_uri=http%3A%2F%2Flocalhost%2Fcpms%2Findex.html&scope=api://3db6f125-db4d-456b-a76e-a6d03182e845/User.Read&response_type=code';
  194. },
  195. }
  196. };
  197. </script>
  198. <style rel="stylesheet/scss" lang="scss">
  199. .login {
  200. display: flex;
  201. justify-content: center;
  202. align-items: center;
  203. height: 100%;
  204. background-image: url("../assets/image/CPMS20210107.jpg");
  205. //background-image: url("../assets/image/cpms-test.jpg");
  206. background-size: cover;
  207. }
  208. .title {
  209. margin: 0px auto 15px auto;
  210. text-align: center;
  211. color: #ffffff;
  212. }
  213. .login-form {
  214. position: absolute;
  215. top: 50%;
  216. left: 50%;
  217. transform: translate(-50%, -50%);
  218. /*实现块元素百分比下居中*/
  219. width: 450px;
  220. padding: 50px;
  221. background: #2a8db9db;
  222. box-sizing: border-box;
  223. box-shadow: 0px 15px 25px rgba(0, 0, 0, .5);
  224. border-radius: 15px;
  225. .el-input {
  226. height: 38px;
  227. input {
  228. height: 38px;
  229. }
  230. }
  231. .input-icon {
  232. height: 39px;
  233. width: 14px;
  234. margin-left: 2px;
  235. }
  236. }
  237. .login-tip {
  238. font-size: 13px;
  239. text-align: center;
  240. color: #bfbfbf;
  241. }
  242. .login-code {
  243. width: 33%;
  244. height: 38px;
  245. float: right;
  246. img {
  247. cursor: pointer;
  248. vertical-align: middle;
  249. }
  250. }
  251. .el-dropdown {
  252. color: #ffffff;
  253. }
  254. .el-checkbox {
  255. color: #ffffff;
  256. }
  257. .el-login-footer {
  258. height: 40px;
  259. line-height: 40px;
  260. position: fixed;
  261. bottom: 0;
  262. width: 100%;
  263. text-align: center;
  264. color: #fff;
  265. font-family: Arial;
  266. font-size: 12px;
  267. letter-spacing: 1px;
  268. }
  269. .login-code-img {
  270. height: 38px;
  271. }
  272. </style>
  273. <style scoped>
  274. .el-button--primary {
  275. color: #FFFFFF;
  276. background-color: #40a9ff;
  277. border-color: #40a9ff;
  278. }
  279. .el-button:hover, .el-button:focus {
  280. border-color: #6abfff;
  281. background-color: #6abfff;
  282. }
  283. </style>