login.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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. show-password
  18. auto-complete="off"
  19. :placeholder="$t('login.password')"
  20. @keyup.enter.native="handleLogin"
  21. >
  22. <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon"/>
  23. </el-input>
  24. </el-form-item>
  25. <!-- <el-form-item prop="code">-->
  26. <!-- <el-input-->
  27. <!-- v-model="loginForm.code"-->
  28. <!-- auto-complete="off"-->
  29. <!-- :placeholder="$t('login.code')"-->
  30. <!-- style="width: 63%"-->
  31. <!-- @keyup.enter.native="handleLogin"-->
  32. <!-- >-->
  33. <!-- <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />-->
  34. <!-- </el-input>-->
  35. <!-- <div class="login-code">-->
  36. <!-- <img :src="codeUrl" @click="getCode" class="login-code-img"/>-->
  37. <!-- </div>-->
  38. <!-- </el-form-item>-->
  39. <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">{{
  40. $t('login.rememberPassword')
  41. }}
  42. </el-checkbox>
  43. <el-form-item style="width:100%;">
  44. <el-button
  45. :loading="loading"
  46. size="medium"
  47. type="primary"
  48. style="width:100%;"
  49. @click.native.prevent="handleLogin"
  50. >
  51. <span v-if="!loading"> {{ $t('login.logIn') }}</span>
  52. <span v-else>{{ $t('login.loading') }}</span>
  53. </el-button>
  54. </el-form-item>
  55. <!-- <el-form-item style="width:100%;">-->
  56. <!-- <el-button-->
  57. <!-- :loading="loading"-->
  58. <!-- size="medium"-->
  59. <!-- type="primary"-->
  60. <!-- style="width:100%;"-->
  61. <!-- @click.native.prevent="doSocialLogin"-->
  62. <!-- >-->
  63. <!-- <span v-if="!loading"> 员工卡登录 </span>-->
  64. <!-- <span v-else>{{ $t('login.loading') }}</span>-->
  65. <!-- </el-button>-->
  66. <!-- </el-form-item>-->
  67. <!--<el-form-item style="width:100%;">-->
  68. <!--<el-button-->
  69. <!--:loading="loading"-->
  70. <!--size="medium"-->
  71. <!--type="primary"-->
  72. <!--style="width:100%;"-->
  73. <!--@click.native.prevent="doAzureLogin"-->
  74. <!--&gt;-->
  75. <!--<span v-if="!loading"> Azure登录 </span>-->
  76. <!--<span v-else>{{ $t('login.loading') }}</span>-->
  77. <!--</el-button>-->
  78. <!--</el-form-item>-->
  79. <el-form-item style="width:100%;">
  80. <el-button
  81. :loading="loading"
  82. size="medium"
  83. type="primary"
  84. style="width:100%;"
  85. @click.native.prevent="doAzureLogin"
  86. >
  87. <span v-if="!loading">员工卡登录</span>
  88. <span v-else>{{ $t('login.loading') }}</span>
  89. </el-button>
  90. </el-form-item>
  91. </el-form>
  92. <!-- 底部 -->
  93. <div class="el-login-footer">
  94. <span>Copyright © 2020-2024 Seashore.ept All Rights Reserved.</span>
  95. </div>
  96. </div>
  97. </template>
  98. <script>
  99. import {getCodeImg} from "@/api/login";
  100. import Cookies from "js-cookie";
  101. import {encrypt, decrypt} from '@/utils/jsencrypt'
  102. import LangSelect from '@/components/LangSelect'
  103. export default {
  104. name: "Login",
  105. components: {LangSelect},
  106. data() {
  107. return {
  108. codeUrl: "",
  109. cookiePassword: "",
  110. loginForm: {
  111. username: "",
  112. password: "",
  113. rememberMe: false,
  114. code: "",
  115. uuid: ""
  116. },
  117. loginRules: {
  118. username: [
  119. {required: true, trigger: "blur", message: this.$t('login.usernameNotEmpty')}
  120. ],
  121. password: [
  122. {required: true, trigger: "blur", message: this.$t('login.passwordNotEmpty')}
  123. ],
  124. code: [{required: true, trigger: "change", message: this.$t('login.codeNotEmpty')}]
  125. },
  126. loading: false,
  127. redirect: undefined
  128. };
  129. },
  130. watch: {
  131. $route: {
  132. handler: function (route) {
  133. this.redirect = route.query && route.query.redirect;
  134. },
  135. immediate: true
  136. }
  137. },
  138. created() {
  139. this.toggleAzureLogin();
  140. this.getCode();
  141. this.getCookie();
  142. if (!this.$store.getters.language) {
  143. console.log("默认中文")
  144. this.$i18n.locale = 'zh'
  145. this.$store.dispatch('app/setLanguage', 'zh')
  146. }
  147. },
  148. methods: {
  149. getCode() {
  150. getCodeImg().then(res => {
  151. this.codeUrl = "data:image/gif;base64," + res.img;
  152. this.loginForm.uuid = res.uuid;
  153. });
  154. },
  155. getCookie() {
  156. const username = Cookies.get("username");
  157. const password = Cookies.get("password");
  158. const rememberMe = Cookies.get('rememberMe')
  159. this.loginForm = {
  160. username: username === undefined ? this.loginForm.username : username,
  161. password: password === undefined ? this.loginForm.password : decrypt(password),
  162. rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
  163. };
  164. },
  165. handleLogin() {
  166. this.$refs.loginForm.validate(valid => {
  167. if (valid) {
  168. this.loading = true;
  169. if (this.loginForm.rememberMe) {
  170. Cookies.set("username", this.loginForm.username, {expires: 30});
  171. Cookies.set("password", encrypt(this.loginForm.password), {expires: 30});
  172. Cookies.set('rememberMe', this.loginForm.rememberMe, {expires: 30});
  173. } else {
  174. Cookies.remove("username");
  175. Cookies.remove("password");
  176. Cookies.remove('rememberMe');
  177. }
  178. this.$store
  179. .dispatch("Login", this.loginForm)
  180. .then(() => {
  181. this.$router.push({path: this.redirect || "/404"});
  182. })
  183. .catch(() => {
  184. this.loading = false;
  185. this.getCode();
  186. });
  187. }
  188. });
  189. },
  190. doSocialLogin() {
  191. 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';
  192. },
  193. /** Azure登录 */
  194. doAzureLogin() {
  195. // console.log("===> login.vue doAzureLogin()方法开始执行")
  196. // 1. authorize请求链接
  197. // https://login.microsoftonline.com/ecaa386b-c8df-4ce0-ad01-740cbdb5ba55/oauth2/v2.0/authorize
  198. // 2. client_id
  199. // client_id=13848745-b09e-4105-a48b-180c0c9d13fd
  200. // 3. scope
  201. // scope=openid profile
  202. // 4. 重定向地址
  203. // redirect_uri=https://cpms.basf-ypc.net.cn/cpms/index.html
  204. // http%3A%2F%2Fcpms.basf-ypc.net.cn%2Fcpms%2Findex.html
  205. // 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';
  206. window.location.href = 'https://login.microsoftonline.com/ecaa386b-c8df-4ce0-ad01-740cbdb5ba55/oauth2/v2.0/authorize?client_id=13848745-b09e-4105-a48b-180c0c9d13fd&redirect_uri=https%3A%2F%2Fcpms.basf-ypc.net.cn%2Fcpms%2Findex.html&scope=openid%20profile&response_type=code';
  207. // console.log("===> login.vue doAzureLogin()方法执行结束")
  208. },
  209. /** Azure登录跳转 */
  210. toggleAzureLogin() {
  211. // console.log("===> login.vue toggleAzureLogin()方法开始执行")
  212. let code = window.location.search.replace("?code=" , '');
  213. // console.log("code:");
  214. // console.log(code);
  215. let messageIndex = code.indexOf("message");
  216. // console.log("messageIndex:");
  217. // console.log(messageIndex);
  218. if (messageIndex == -1) { // url不包含message参数
  219. if (code) { // url包含code参数
  220. // authorization_code
  221. code = code.substring(0, code.indexOf("&"));
  222. // console.log("===> messageIndex == -1");
  223. // console.log("code:");
  224. // console.log(code);
  225. // redirect_url
  226. window.location.href = '#/azureLogin?code='+code;
  227. }
  228. } else {
  229. // console.log("===> messageIndex != -1");
  230. // 解决中文参数乱码问题
  231. let questionMarkSplitStrings = decodeURI(window.location.href).split("?");
  232. // console.log("questionMarkSplitStrings:");
  233. // console.log(questionMarkSplitStrings);
  234. let hashTagSplitStrings = questionMarkSplitStrings[1].split("#");
  235. // console.log("hashTagSplitStrings:");
  236. // console.log(hashTagSplitStrings);
  237. let equalSignSplitStrings = hashTagSplitStrings[0].split("=");
  238. // console.log("equalSignSplitStrings:");
  239. // console.log(equalSignSplitStrings);
  240. // ajax error message
  241. let message = equalSignSplitStrings[1];
  242. // console.log("message:");
  243. // console.log(message);
  244. this.msgError(message + ",请联系管理员");
  245. }
  246. // console.log("===> login.vue toggleAzureLogin()方法执行结束")
  247. },
  248. }
  249. };
  250. </script>
  251. <style rel="stylesheet/scss" lang="scss" scoped>
  252. .login {
  253. display: flex;
  254. justify-content: center;
  255. align-items: center;
  256. height: 100%;
  257. background-image: url("../assets/image/CPMS20210107.jpg");
  258. //background-image: url("../assets/image/cpms-test.jpg");
  259. background-size: cover;
  260. }
  261. .title {
  262. margin: 0px auto 15px auto;
  263. text-align: center;
  264. color: #ffffff;
  265. }
  266. .login-form {
  267. position: absolute;
  268. top: 50%;
  269. left: 50%;
  270. transform: translate(-50%, -50%);
  271. /*实现块元素百分比下居中*/
  272. width: 450px;
  273. padding: 50px;
  274. background: #2a8db9db;
  275. box-sizing: border-box;
  276. box-shadow: 0px 15px 25px rgba(0, 0, 0, .5);
  277. border-radius: 15px;
  278. .el-input {
  279. height: 38px;
  280. input {
  281. height: 38px;
  282. }
  283. }
  284. .input-icon {
  285. height: 39px;
  286. width: 14px;
  287. margin-left: 2px;
  288. }
  289. }
  290. .login-tip {
  291. font-size: 13px;
  292. text-align: center;
  293. color: #bfbfbf;
  294. }
  295. .login-code {
  296. width: 33%;
  297. height: 38px;
  298. float: right;
  299. img {
  300. cursor: pointer;
  301. vertical-align: middle;
  302. }
  303. }
  304. .el-dropdown {
  305. color: #ffffff;
  306. }
  307. .el-checkbox {
  308. color: #ffffff;
  309. }
  310. .el-login-footer {
  311. height: 40px;
  312. line-height: 40px;
  313. position: fixed;
  314. bottom: 0;
  315. width: 100%;
  316. text-align: center;
  317. color: #fff;
  318. font-family: Arial;
  319. font-size: 12px;
  320. letter-spacing: 1px;
  321. }
  322. .login-code-img {
  323. height: 38px;
  324. }
  325. </style>
  326. <style scoped>
  327. .el-button--primary {
  328. color: #FFFFFF;
  329. background-color: #40a9ff;
  330. border-color: #40a9ff;
  331. }
  332. .el-button:hover, .el-button:focus {
  333. border-color: #6abfff;
  334. background-color: #6abfff;
  335. }
  336. </style>