login.vue 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. this.toggleAzureLogin();
  127. this.getCode();
  128. this.getCookie();
  129. if (!this.$store.getters.language) {
  130. console.log("默认中文")
  131. this.$i18n.locale = 'zh'
  132. this.$store.dispatch('app/setLanguage', 'zh')
  133. }
  134. },
  135. methods: {
  136. getCode() {
  137. getCodeImg().then(res => {
  138. this.codeUrl = "data:image/gif;base64," + res.img;
  139. this.loginForm.uuid = res.uuid;
  140. });
  141. },
  142. getCookie() {
  143. const username = Cookies.get("username");
  144. const password = Cookies.get("password");
  145. const rememberMe = Cookies.get('rememberMe')
  146. this.loginForm = {
  147. username: username === undefined ? this.loginForm.username : username,
  148. password: password === undefined ? this.loginForm.password : decrypt(password),
  149. rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
  150. };
  151. },
  152. handleLogin() {
  153. this.$refs.loginForm.validate(valid => {
  154. if (valid) {
  155. this.loading = true;
  156. if (this.loginForm.rememberMe) {
  157. Cookies.set("username", this.loginForm.username, {expires: 30});
  158. Cookies.set("password", encrypt(this.loginForm.password), {expires: 30});
  159. Cookies.set('rememberMe', this.loginForm.rememberMe, {expires: 30});
  160. } else {
  161. Cookies.remove("username");
  162. Cookies.remove("password");
  163. Cookies.remove('rememberMe');
  164. }
  165. this.$store
  166. .dispatch("Login", this.loginForm)
  167. .then(() => {
  168. this.$router.push({path: this.redirect || "/404"});
  169. })
  170. .catch(() => {
  171. this.loading = false;
  172. this.getCode();
  173. });
  174. }
  175. });
  176. },
  177. doSocialLogin() {
  178. 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';
  179. },
  180. /** Azure登录 */
  181. doAzureLogin() {
  182. // TODO: 1. 修改authorize请求链接、2. 修改client_id、3. 修改scope 4. 修改重定向地址
  183. // 1. https://login.microsoftonline.com/ecaa386b-c8df-4ce0-ad01-740cbdb5ba55/oauth2/v2.0/authorize
  184. // 2. client_id=?
  185. // 3. scope=profile openid offline_access
  186. // 4. redirect_uri=?
  187. 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';
  188. },
  189. /** Azure登录跳转 */
  190. toggleAzureLogin() {
  191. let code = window.location.search.replace("?code=" , '');
  192. let messageIndex = code.indexOf("message");
  193. if (messageIndex == -1) { // url不包含message参数
  194. if (code) { // url包含code参数
  195. // authorization_code
  196. code = code.substring(0, code.indexOf("&"));
  197. // redirect_url
  198. window.location.href = '#/azureLogin?code='+code;
  199. }
  200. } else {
  201. // 解决中文参数乱码问题
  202. let questionMarkSplitStrings = decodeURI(window.location.href).split("?");
  203. let hashTagSplitStrings = questionMarkSplitStrings[1].split("#");
  204. let equalSignSplitStrings = hashTagSplitStrings[0].split("=");
  205. // ajax error message
  206. let message = equalSignSplitStrings[1];
  207. this.msgError(message + ",请联系管理员");
  208. }
  209. },
  210. }
  211. };
  212. </script>
  213. <style rel="stylesheet/scss" lang="scss">
  214. .login {
  215. display: flex;
  216. justify-content: center;
  217. align-items: center;
  218. height: 100%;
  219. background-image: url("../assets/image/CPMS20210107.jpg");
  220. //background-image: url("../assets/image/cpms-test.jpg");
  221. background-size: cover;
  222. }
  223. .title {
  224. margin: 0px auto 15px auto;
  225. text-align: center;
  226. color: #ffffff;
  227. }
  228. .login-form {
  229. position: absolute;
  230. top: 50%;
  231. left: 50%;
  232. transform: translate(-50%, -50%);
  233. /*实现块元素百分比下居中*/
  234. width: 450px;
  235. padding: 50px;
  236. background: #2a8db9db;
  237. box-sizing: border-box;
  238. box-shadow: 0px 15px 25px rgba(0, 0, 0, .5);
  239. border-radius: 15px;
  240. .el-input {
  241. height: 38px;
  242. input {
  243. height: 38px;
  244. }
  245. }
  246. .input-icon {
  247. height: 39px;
  248. width: 14px;
  249. margin-left: 2px;
  250. }
  251. }
  252. .login-tip {
  253. font-size: 13px;
  254. text-align: center;
  255. color: #bfbfbf;
  256. }
  257. .login-code {
  258. width: 33%;
  259. height: 38px;
  260. float: right;
  261. img {
  262. cursor: pointer;
  263. vertical-align: middle;
  264. }
  265. }
  266. .el-dropdown {
  267. color: #ffffff;
  268. }
  269. .el-checkbox {
  270. color: #ffffff;
  271. }
  272. .el-login-footer {
  273. height: 40px;
  274. line-height: 40px;
  275. position: fixed;
  276. bottom: 0;
  277. width: 100%;
  278. text-align: center;
  279. color: #fff;
  280. font-family: Arial;
  281. font-size: 12px;
  282. letter-spacing: 1px;
  283. }
  284. .login-code-img {
  285. height: 38px;
  286. }
  287. </style>
  288. <style scoped>
  289. .el-button--primary {
  290. color: #FFFFFF;
  291. background-color: #40a9ff;
  292. border-color: #40a9ff;
  293. }
  294. .el-button:hover, .el-button:focus {
  295. border-color: #6abfff;
  296. background-color: #6abfff;
  297. }
  298. </style>