login.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. <template>
  2. <div class="backdrop" :style="{backgroundImage: 'url('+(this.BackgroundImg ? this.BackgroundImg : this.baseImg)+')'}">
  3. <el-container>
  4. <el-header style="display:block;position:relative;margin:auto;">
  5. <!-- logo -->
  6. <img :src="this.logo ? this.logo : this.baseLogo" alt="" class="rightulliimg" />
  7. </el-header>
  8. <el-main>
  9. <!-- 中间部分 -->
  10. <div class="login">
  11. <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
  12. <el-tabs v-model="activeName" style="margin-left: -61px;margin-top:-45px" @tab-click="changeLoginType">
  13. <el-tab-pane label="账号密码登录" name="login_password">
  14. <el-form-item prop="username">
  15. <el-input v-model="loginForm.username" type="text" auto-complete="off" placeholder="账号">
  16. <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
  17. </el-input>
  18. </el-form-item>
  19. <el-form-item prop="password">
  20. <el-input v-model="loginForm.password" type="password" auto-complete="off" placeholder="密码" @keyup.enter.native="handleLogin" style=" width: 260px; ">
  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 v-model="loginForm.code" auto-complete="off" placeholder="验证码" style="width: 53%;" @keyup.enter.native="handleLogin">
  26. <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
  27. </el-input>
  28. <div class="login-code">
  29. <img :src="codeUrl" @click="getCode" class="login-code-img" />
  30. </div>
  31. </el-form-item>
  32. <el-checkbox v-model="loginForm.rememberMe">记住密码</el-checkbox>
  33. <el-form-item style="width:100%;">
  34. <el-button :loading="loading" size="medium" type="primary" @click.native.prevent="handleLogin" style="width: 190px;border-radius: 50px;margin-left:10%">
  35. <span v-if="!loading">登 录</span>
  36. <span v-else>登 录 中...</span>
  37. </el-button>
  38. </el-form-item>
  39. <el-divider class="divider_left"><span style="font-size:10px;word-break: keep-all;">第三方快捷登录</span></el-divider>
  40. <el-form-item style="width:100%;">
  41. <img :src="wxSrc" @mouseover="changeImg" @mouseleave="returnImg" @click="getWeChatImg" style="width:30px;margin-left: 77px;">
  42. </el-form-item>
  43. </el-tab-pane>
  44. <el-tab-pane label="手机验证码" name="login_shortMessage">
  45. <el-form-item prop="username">
  46. <el-input v-model="loginForm.username" type="text" auto-complete="off" placeholder="手机号">
  47. <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
  48. </el-input>
  49. </el-form-item>
  50. <el-form-item prop="code">
  51. <el-input v-model="loginForm.code" auto-complete="off" placeholder="验证码" style="width: 53%;" @keyup.enter.native="handleLogin">
  52. <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
  53. </el-input>
  54. <div class="login-code">
  55. <img :src="codeUrl" @click="getCode" class="login-code-img" />
  56. </div>
  57. </el-form-item>
  58. <el-form-item prop="cade">
  59. <el-input v-model="loginForm.shortMessageCode" type="text" maxlength="" auto-complete="off" placeholder="短信验证码" id="" onkeydown="enterHandler(event)" style=" width: 120px; ">
  60. <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
  61. </el-input>
  62. <el-button @click="sendMessage" :disabled="sendShortMessageBtn">{{codeBtnWord}}</el-button>
  63. </el-form-item>
  64. <el-button :loading="loading" size="medium" type="primary" @click.native.prevent="handleLogin" style="width: 190px;border-radius: 50px;margin-left:10%;margin-top:10%">
  65. <span v-if="!loading">登 录</span>
  66. <span v-else>登 录 中...</span>
  67. </el-button>
  68. </el-tab-pane>
  69. </el-tabs>
  70. </el-form>
  71. <!-- 底部 -->
  72. </div>
  73. </el-main>
  74. <el-dialog
  75. title="选择企业"
  76. :visible.sync="dialogVisible"
  77. width="30%">
  78. <el-select v-model="chooseCompanyId" placeholder="请选择">
  79. <el-option
  80. v-for="item in companyList"
  81. :key="item.scyId"
  82. :label="item.scyName"
  83. :value="item.scyId">
  84. </el-option>
  85. </el-select>
  86. <span slot="footer" class="dialog-footer">
  87. <el-button type="primary" @click="chooseLogin()">确 定</el-button>
  88. </span>
  89. </el-dialog>
  90. </el-container>
  91. <el-footer></el-footer>
  92. </div>
  93. </template>
  94. <script>
  95. import { getCodeImg, sendShortMessage, chooseCompanyLogin} from "@/api/login";
  96. import { wxImg } from "@/api/system/weChat";
  97. import Cookies from "js-cookie";
  98. import { encrypt, decrypt } from "@/utils/jsencrypt";
  99. export default {
  100. name: "Login",
  101. data() {
  102. return {
  103. //默认背景图
  104. activeName: "login_password",
  105. baseImg: require('../assets/images/logi_bg1.jpg'),
  106. BackgroundImg : require('../assets/images/logi_bg1.jpg'),
  107. logo: '',
  108. baseLogo: require('../assets/images/lgo.png'),
  109. codeUrl: "",
  110. cookiePassword: "",
  111. loginForm: {
  112. shortMessageCode: '',
  113. username: "",
  114. password: "",
  115. rememberMe: false,
  116. code: "",
  117. uuid: "",
  118. },
  119. codeBtnWord: '获取验证码', // 获取验证码按钮文字
  120. waitTime:2, // 获取验证码按钮失效时间
  121. loginRules: {
  122. username: [
  123. {
  124. required: true,
  125. trigger: "blur",
  126. message: "用户名不能为空",
  127. },
  128. ],
  129. code: [
  130. {
  131. required: true,
  132. trigger: "change",
  133. message: "验证码不能为空",
  134. },
  135. ]
  136. },
  137. loading: false,
  138. redirect: undefined,
  139. wxSrc: require("../assets/images/wx1.png"),
  140. sendShortMessageBtn : false,
  141. dialogVisible : false,
  142. companyList : [],
  143. chooseCompanyId : ''
  144. };
  145. },
  146. watch: {
  147. $route: {
  148. handler: function (route) {
  149. this.redirect = route.query && route.query.redirect;
  150. },
  151. immediate: true,
  152. },
  153. },
  154. created() {
  155. this.getCode();
  156. this.getCookie();
  157. // this.getlistDomain();
  158. },
  159. methods: {
  160. changeImg(){
  161. this.wxSrc = require("../assets/images/wx.png")
  162. },
  163. returnImg(){
  164. this.wxSrc = require("../assets/images/wx1.png")
  165. },
  166. getCode() {
  167. getCodeImg().then((res) => {
  168. this.codeUrl = "data:image/gif;base64," + res.img;
  169. this.loginForm.uuid = res.uuid;
  170. });
  171. },
  172. getCookie() {
  173. const username = Cookies.get("username");
  174. const password = Cookies.get("password");
  175. const rememberMe = Cookies.get("rememberMe");
  176. this.loginForm = {
  177. username:
  178. username === undefined ? this.loginForm.username : username,
  179. password:
  180. password === undefined
  181. ? this.loginForm.password
  182. : decrypt(password),
  183. rememberMe:
  184. rememberMe === undefined ? false : Boolean(rememberMe),
  185. };
  186. },
  187. handleLogin() {
  188. this.$refs.loginForm.validate((valid) => {
  189. if (valid) {
  190. this.loading = true;
  191. // 密码登陆
  192. if (this.activeName == "login_password") {
  193. this.loginForm.type = "0";
  194. // 密码不能为空
  195. if (this.loginForm.password == null || this.loginForm.password == "") {
  196. this.msgError("请输入密码!");
  197. this.loading = false;
  198. return;
  199. }
  200. // 短信验证码登陆
  201. } else{
  202. this.loginForm.type = "1";
  203. // 密码不能为空
  204. if (this.loginForm.shortMessageCode == null || this.loginForm.shortMessageCode == "") {
  205. this.msgError("请输入短信验证码!");
  206. this.loading = false;
  207. return;
  208. }
  209. }
  210. if (this.loginForm.rememberMe) {
  211. Cookies.set("username", this.loginForm.username, {
  212. expires: 30,
  213. });
  214. Cookies.set(
  215. "password",
  216. encrypt(this.loginForm.password),
  217. { expires: 30 }
  218. );
  219. Cookies.set("rememberMe", this.loginForm.rememberMe, {
  220. expires: 30,
  221. });
  222. } else {
  223. Cookies.remove("username");
  224. Cookies.remove("password");
  225. Cookies.remove("rememberMe");
  226. }
  227. this.$store
  228. .dispatch("Login", this.loginForm)
  229. .then((data) => {
  230. console.log(data)
  231. if (data.code == "0") {
  232. console.log("未认证!")
  233. this.$router.push({ path: "/certification" });
  234. // 认证单个企业
  235. } else if (data.code == "1") {
  236. // 认证通过
  237. if (data.loginUser.sysUser.companyStatus == "00") {
  238. /* if(this.redirect == '/index'){
  239. this.$router.push({ path: "/homePage"+this.redirect || "/" });
  240. }else{
  241. this.$router.push({ path: this.redirect || "/" });
  242. } */
  243. this.$router.push({ path: this.redirect || "/" });
  244. // 认证未通过
  245. } else {
  246. console.log("未认证通过!")
  247. this.$router.push({ path: "/certification" });
  248. }
  249. // 多加企业
  250. } else {
  251. this.companyList = data.companyList;
  252. this.chooseCompanyId = this.companyList[0].scyId;
  253. this.dialogVisible = true;
  254. }
  255. this.loading = false;
  256. })
  257. .catch(() => {
  258. this.loading = false;
  259. this.getCode();
  260. });
  261. }
  262. });
  263. },
  264. getUrlKey(name) {
  265. return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.href) || [, ""])[1].replace(/\+/g, '%20')) || null
  266. },
  267. getWeChatImg(){
  268. wxImg().then((res) => {
  269. // console.log(res)
  270. location.href = res
  271. })
  272. },
  273. changeLoginType(e){
  274. this.getCode();
  275. },
  276. sendMessage(){
  277. this.$refs.loginForm.validate((valid) => {
  278. if (valid) {
  279. // 调用获取短信验证码接口
  280. sendShortMessage(this.loginForm.username, this.loginForm.code, this.loginForm.uuid).then(response => {
  281. this.sendShortMessageBtn = true;
  282. this.msgSuccess("发送成功!");
  283. // 因为下面用到了定时器,需要保存this指向
  284. let that = this
  285. that.waitTime = 60;
  286. this.codeBtnWord = `${this.waitTime}s 后重新获取`
  287. let timer = setInterval(function(){
  288. if(that.waitTime>1){
  289. that.waitTime--
  290. that.codeBtnWord = `${that.waitTime}s 后重新获取`
  291. }else{
  292. clearInterval(timer)
  293. that.codeBtnWord = '获取验证码'
  294. that.waitTime = 60
  295. that.getCode();
  296. that.sendShortMessageBtn = false;
  297. }
  298. },1000)
  299. }).catch((response)=>{
  300. this.getCode();
  301. });
  302. }
  303. })
  304. },
  305. chooseLogin(){
  306. chooseCompanyLogin(this.chooseCompanyId).then(response => {
  307. if (response.code == "200") {
  308. // 认证通过
  309. if (response.data.sysUser.companyStatus == "00") {
  310. this.$store.dispatch('GetInfo').then(res => {
  311. this.$router.push({ path: this.redirect || "/" });
  312. })
  313. // 认证未通过
  314. } else {
  315. console.log("未认证通过!")
  316. this.$router.push({ path: "/certification" });
  317. }
  318. }
  319. }).catch((response)=>{
  320. });
  321. }
  322. },
  323. };
  324. </script>
  325. <style rel="stylesheet/scss" lang="scss">
  326. .login {
  327. display: flex;
  328. justify-content: center;
  329. align-items: center;
  330. height: 100%;
  331. background-size: cover;
  332. }
  333. // 中间背景图
  334. .login-form {
  335. border-radius: 6px;
  336. width: 935px;
  337. height: 434px;
  338. padding: 73px 138px 103px 597px;
  339. background-image: url("../assets/images/login_bg2.png");
  340. background-size: 935px 434px;
  341. background-repeat: no-repeat;
  342. margin-top: 40px;
  343. // 输入框大小
  344. .el-input {
  345. height: 40px;
  346. width: 100%;
  347. input {
  348. height: 40px;
  349. width: 100%;
  350. }
  351. }
  352. .input-icon {
  353. height: 39px;
  354. width: 14px;
  355. margin-left: 2px;
  356. }
  357. }
  358. .login-tip {
  359. font-size: 13px;
  360. text-align: center;
  361. }
  362. // 验证码
  363. .login-code {
  364. width: 33%;
  365. height: 38px;
  366. float: right;
  367. margin-right: 6%;
  368. img {
  369. cursor: pointer;
  370. vertical-align: middle;
  371. }
  372. }
  373. .login-code-img {
  374. height: 38px;
  375. }
  376. // 大背景图
  377. .backdrop {
  378. background-repeat: no-repeat;
  379. background-size: 1536px 752px;
  380. background-size: cover;
  381. width: 100%;
  382. height: 100%;
  383. display: flex;
  384. justify-content: center;
  385. align-items: center;
  386. background-position: right top;
  387. background-attachment: fixed;
  388. }
  389. .rightulliimg{
  390. width: 860px;
  391. }
  392. .divider_left{
  393. margin-left: -40px;
  394. }.el-button.disabled-style {
  395. background-color: #EEEEEE;
  396. color: #CCCCCC;
  397. }
  398. </style>