login.vue 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. <template>
  2. <div class="login" id="loginBox">
  3. <div class="logopositon">
  4. <!-- <img src="../assets/images/yunlian_logo.png" alt="">-->
  5. </div>
  6. <div class="login-con">
  7. <div class="img-box">
  8. <div class="imgline1">
  9. 一站式私域平台
  10. </div>
  11. <div class="imgline2">
  12. 助力企业打造自主客户资产池!
  13. </div>
  14. </div>
  15. <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
  16. <div class="title">{{vueAppTitle}}</div>
  17. <div class="title-line"></div>
  18. <el-form-item prop="username">
  19. <el-input v-model="loginForm.username" type="text" auto-complete="off" placeholder="账号" class="username" >
  20. <img slot="prefix" src="../assets/images/user.png" class="input-icon "/>
  21. </el-input>
  22. </el-form-item>
  23. <el-form-item prop="password">
  24. <el-input
  25. v-model="loginForm.password"
  26. :type="passwordtype"
  27. auto-complete="off"
  28. placeholder="密码"
  29. @keyup.enter.native="handleLogin"
  30. class="password"
  31. >
  32. <!-- <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" /> -->
  33. <img slot="prefix" src="../assets/images/pass.png" class="input-icon" />
  34. <img slot="suffix" src="../assets/images/eyeoff.png" class="input-icon2" v-if="ispassword" @click.stop="changetype()"/>
  35. <img slot="suffix" src="../assets/images/eyeopen.png" class="input-icon2" v-else @click.stop="changetype()"/>
  36. </el-input>
  37. </el-form-item>
  38. <el-form-item prop="code">
  39. <el-input
  40. v-model="loginForm.code"
  41. auto-complete="off"
  42. placeholder="验证码"
  43. style="width: 63%"
  44. @keyup.enter.native="handleLogin"
  45. >
  46. <!-- <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon"/> -->
  47. <img slot="prefix" src="../assets/images/code.png" class="input-icon" />
  48. </el-input>
  49. <div class="login-code">
  50. <img :src="codeUrl" @click="getCode" class="login-code-img"/>
  51. </div>
  52. </el-form-item>
  53. <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">记住密码</el-checkbox>
  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="handleLogin"
  61. >
  62. <span v-if="!loading">登 录</span>
  63. <span v-else>登 录 中...</span>
  64. </el-button>
  65. </el-form-item>
  66. </el-form>
  67. </div>
  68. <!-- 微信扫码弹框 -->
  69. <WechatLoginDialog
  70. ref="wechatDialog"
  71. :ticket="loginForm.username"
  72. :visible.sync="wechatDialogVisible"
  73. @loginSuccess="handleWechatLoginSuccess"
  74. :redirect="redirect"
  75. />
  76. <!-- 底部 -->
  77. <div class="el-login-footer">
  78. <span>{{companyName}}</span>
  79. <a :href="icpUrl" target="_bank">{{icpRecord}}</a>
  80. </div>
  81. </div>
  82. </template>
  83. <script>
  84. import { getCodeImg } from "@/api/login";
  85. import Cookies from "js-cookie";
  86. import { encrypt, decrypt } from '@/utils/jsencrypt'
  87. import { getFirstLogin } from "@/api/login";
  88. import WechatLoginDialog from "@/views/WechatLoginDialog";
  89. import { setToken } from "@/utils/auth";
  90. export default {
  91. name: "Login",
  92. components: { WechatLoginDialog },
  93. data() {
  94. return {
  95. codeUrl: "",
  96. vueAppTitle: process.env.VUE_APP_TITLE,
  97. companyName: process.env.VUE_APP_COMPANY_NAME,
  98. icpRecord: process.env.VUE_APP_ICP_RECORD,
  99. icpUrl: process.env.VUE_APP_ICP_URL,
  100. cookiePassword: "",
  101. wechatDialogVisible: false,
  102. loginForm: {
  103. username: "",
  104. password: "",
  105. rememberMe: false,
  106. code: "",
  107. uuid: ""
  108. },
  109. loginRules: {
  110. username: [
  111. { required: true, trigger: "blur", message: "用户名不能为空" }
  112. ],
  113. password: [
  114. { required: true, trigger: "blur", message: "密码不能为空" },
  115. {
  116. pattern: /^(?=.*[A-Za-z])(?=.*\d)(?=.*[^A-Za-z0-9]).{8,20}$/,
  117. message: "密码长度为8-20 位,必须包含字母、数字和特殊字符",
  118. trigger: ["blur", "change"],
  119. }
  120. ],
  121. code: [{ required: true, trigger: "change", message: "验证码不能为空" }]
  122. },
  123. loading: false,
  124. redirect: undefined,
  125. passwordtype:'password',
  126. ispassword:true
  127. };
  128. },
  129. watch: {
  130. $route: {
  131. handler: function(route) {
  132. this.redirect = route.query && route.query.redirect;
  133. },
  134. immediate: true
  135. }
  136. },
  137. mounted () {
  138. },
  139. created() {
  140. this.getCode();
  141. this.getCookie();
  142. },
  143. methods: {
  144. checkFirstLogin() {
  145. getFirstLogin().then(res => {
  146. if (res.code === 200 && res.data) {
  147. // 将首次登录状态存储到本地
  148. localStorage.setItem('isFirstLogin', res.data ? 'true' : 'false');
  149. // 如果是首次登录,跳转到设置密码页面
  150. this.$router.push('/set-password');
  151. } else {
  152. // 否则进入首页
  153. this.$router.push({ path: this.redirect || "/" });
  154. }
  155. }).catch(() => {
  156. this.loading = false;
  157. this.getCode();
  158. });
  159. },
  160. getCode() {
  161. getCodeImg().then(res => {
  162. this.codeUrl = "data:image/gif;base64," + res.img;
  163. this.loginForm.uuid = res.uuid;
  164. });
  165. },
  166. getCookie() {
  167. const username = Cookies.get("username");
  168. const password = Cookies.get("password");
  169. const rememberMe = Cookies.get('rememberMe')
  170. this.loginForm = {
  171. username: username === undefined ? this.loginForm.username : username,
  172. password: password === undefined ? this.loginForm.password : decrypt(password),
  173. rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
  174. };
  175. },
  176. // 微信扫码成功回调
  177. handleWechatLoginSuccess(token) {
  178. this.loading = false
  179. console.log("父组件收到 loginSuccess:", token);
  180. this.$store.commit("SET_TOKEN", token);
  181. setToken(token);
  182. // 登录成功后检查是否是首次登录
  183. this.checkFirstLogin();
  184. },
  185. handleLogin() {
  186. console.log("handleLogin")
  187. this.$refs.loginForm.validate(valid => {
  188. if (valid) {
  189. this.loading = true;
  190. if (this.loginForm.rememberMe) {
  191. Cookies.set("username", this.loginForm.username, { expires: 30 });
  192. Cookies.set("password", encrypt(this.loginForm.password), { expires: 30 });
  193. Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 });
  194. } else {
  195. Cookies.remove("username");
  196. Cookies.remove("password");
  197. Cookies.remove('rememberMe');
  198. }
  199. this.$store
  200. .dispatch("Login", this.loginForm)
  201. .then(res => {
  202. if (res.needSms) {
  203. console.log("打开弹窗")
  204. this.wechatDialogVisible = true;
  205. // 等 visible 更新后,直接调用弹窗 open()
  206. this.$nextTick(() => {
  207. if (this.$refs.wechatDialog) {
  208. this.$refs.wechatDialog.open(this.loginForm.username);
  209. }
  210. });
  211. } else {
  212. // 登录成功后检查是否是首次登录
  213. this.checkFirstLogin();
  214. }
  215. })
  216. .catch(res => {
  217. console.log("登录失败")
  218. console.log(res)
  219. this.loading = false;
  220. this.getCode();
  221. });
  222. }
  223. });
  224. },
  225. changetype(){
  226. this.ispassword=!this.ispassword
  227. if(this.ispassword){
  228. this.passwordtype="password"
  229. }else{
  230. this.passwordtype="text"
  231. }
  232. }
  233. }
  234. };
  235. </script>
  236. <style rel="stylesheet/scss" lang="scss">
  237. .login {
  238. .logopositon{
  239. position: fixed;
  240. left: 30px;
  241. top: 30px;
  242. }
  243. display: flex;
  244. justify-content: center;
  245. align-items: center;
  246. height: 100%;
  247. width: 100%;
  248. background-image: (url(../assets/images/login_bg.png) );
  249. background-size: 100% 100%;
  250. background-size: cover;
  251. .login-con{
  252. width: 880px;
  253. display: flex;
  254. align-items: center;
  255. justify-content: space-between;
  256. border-radius: 10px;
  257. .img-box{
  258. width: 440px;
  259. height: 500px;
  260. background-image: url(../assets/images/login_left.png);
  261. background-size: 100% 100%;
  262. color: #FFFFFF;
  263. .imgline1{
  264. font-size: 34px;
  265. font-weight:400;
  266. margin-left: 38px;
  267. margin-top: 38px;
  268. letter-spacing: 2px;
  269. }
  270. .imgline2{
  271. margin-left: 38px;
  272. font-size: 16px;
  273. margin-top: 6px;
  274. letter-spacing: 1px;
  275. }
  276. }
  277. }
  278. }
  279. .title {
  280. margin: 0px auto 15px auto;
  281. text-align: center;
  282. color: #202124;
  283. font-weight: 500;
  284. font-size: 20px;
  285. }
  286. .title-line{
  287. width: 40px;
  288. border-bottom: 2px solid #006CFF;
  289. margin: 0px auto 30px auto;
  290. }
  291. .login-form {
  292. box-sizing: border-box;
  293. background: #ffffff;
  294. width: 440px;
  295. height: 500px;
  296. padding: 40px 25px 5px 25px;
  297. box-shadow:4px -4px 10px rgba(0, 108, 255, 0.15);
  298. .el-input {
  299. height: 38px;
  300. input {
  301. height: 38px;
  302. }
  303. }
  304. .input-icon {
  305. height: 20px;
  306. width: 20px;
  307. margin-left: 2px;
  308. margin-top: 10px;
  309. }
  310. .input-icon2{
  311. height: 20px;
  312. width: 20px;
  313. margin-right: 4px;
  314. margin-top: 10px;
  315. }
  316. }
  317. .login-tip {
  318. font-size: 13px;
  319. text-align: center;
  320. color: #bfbfbf;
  321. }
  322. .login-code {
  323. width: 33%;
  324. height: 38px;
  325. float: right;
  326. img {
  327. cursor: pointer;
  328. vertical-align: middle;
  329. }
  330. }
  331. .el-login-footer {
  332. height: 40px;
  333. line-height: 40px;
  334. position: fixed;
  335. bottom: 0;
  336. width: 100%;
  337. text-align: center;
  338. color: #fff;
  339. font-family: Arial;
  340. font-size: 12px;
  341. letter-spacing: 1px;
  342. }
  343. .login-code-img {
  344. height: 38px;
  345. }
  346. </style>