login.vue 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. <div class="el-login-footer">
  70. <span>{{companyName}}</span>
  71. <a :href="icpUrl" target="_bank">{{icpRecord}}</a>
  72. </div>
  73. <!-- 微信扫码弹框 -->
  74. <WechatLoginDialog
  75. ref="wechatDialog"
  76. :ticket="loginForm.username"
  77. :visible.sync="wechatDialogVisible"
  78. @loginSuccess="handleWechatLoginSuccess"
  79. :redirect="redirect"
  80. />
  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 WechatLoginDialog from "@/views/WechatLoginDialog.vue";
  88. import { setToken } from "@/utils/auth";
  89. export default {
  90. name: "Login",
  91. components: { WechatLoginDialog },
  92. data() {
  93. return {
  94. codeUrl: "",
  95. vueAppTitle: process.env.VUE_APP_TITLE,
  96. companyName: process.env.VUE_APP_COMPANY_NAME,
  97. icpRecord: process.env.VUE_APP_ICP_RECORD,
  98. icpUrl: process.env.VUE_APP_ICP_URL,
  99. cookiePassword: "",
  100. wechatDialogVisible: false,
  101. loginForm: {
  102. username: "",
  103. password: "",
  104. rememberMe: false,
  105. code: "",
  106. uuid: ""
  107. },
  108. loginRules: {
  109. username: [
  110. { required: true, trigger: "blur", message: "用户名不能为空" }
  111. ],
  112. password: [
  113. { required: true, trigger: "blur", message: "密码不能为空" },
  114. {
  115. pattern: /^(?=.*[A-Za-z])(?=.*\d)(?=.*[^A-Za-z0-9]).{8,20}$/,
  116. message: "密码长度为8-20 位,必须包含字母、数字和特殊字符",
  117. trigger: ["blur", "change"],
  118. }
  119. ],
  120. code: [{ required: true, trigger: "change", message: "验证码不能为空" }]
  121. },
  122. loading: false,
  123. redirect: undefined,
  124. passwordtype:'password',
  125. ispassword:true
  126. };
  127. },
  128. watch: {
  129. $route: {
  130. handler: function(route) {
  131. this.redirect = route.query && route.query.redirect;
  132. },
  133. immediate: true
  134. }
  135. },
  136. mounted () {
  137. },
  138. created() {
  139. this.getCode();
  140. this.getCookie();
  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(res => {
  175. if (res.needSms){
  176. console.log("打开弹窗")
  177. this.wechatDialogVisible = true;
  178. // 等 visible 更新后,直接调用弹窗 open()
  179. this.$nextTick(() => {
  180. if (this.$refs.wechatDialog) {
  181. this.$refs.wechatDialog.open(this.loginForm.username);
  182. }
  183. });
  184. } else {
  185. this.$router.push({ path: this.redirect || "/" });
  186. }
  187. })
  188. .catch(() => {
  189. this.loading = false;
  190. this.getCode();
  191. });
  192. }
  193. });
  194. },
  195. changetype(){
  196. this.ispassword=!this.ispassword
  197. if(this.ispassword){
  198. this.passwordtype="password"
  199. }else{
  200. this.passwordtype="text"
  201. }
  202. },
  203. // 微信扫码成功回调
  204. handleWechatLoginSuccess(token) {
  205. this.loading = false
  206. console.log("父组件收到 loginSuccess:", token);
  207. this.$store.commit("SET_TOKEN", token);
  208. setToken(token);
  209. this.$router.push({ path: this.redirect || "/" });
  210. },
  211. }
  212. };
  213. </script>
  214. <style rel="stylesheet/scss" lang="scss">
  215. .login {
  216. .logopositon{
  217. position: fixed;
  218. left: 30px;
  219. top: 30px;
  220. }
  221. display: flex;
  222. justify-content: center;
  223. align-items: center;
  224. height: 100%;
  225. width: 100%;
  226. background-image: (url(../assets/images/login_bg.png) );
  227. background-size: 100% 100%;
  228. background-size: cover;
  229. .login-con{
  230. width: 880px;
  231. display: flex;
  232. align-items: center;
  233. justify-content: space-between;
  234. border-radius: 10px;
  235. .img-box{
  236. width: 440px;
  237. height: 500px;
  238. background-image: url(../assets/images/login_left.png);
  239. background-size: 100% 100%;
  240. color: #FFFFFF;
  241. .imgline1{
  242. font-size: 34px;
  243. font-weight:400;
  244. margin-left: 38px;
  245. margin-top: 38px;
  246. letter-spacing: 2px;
  247. }
  248. .imgline2{
  249. margin-left: 38px;
  250. font-size: 16px;
  251. margin-top: 6px;
  252. letter-spacing: 1px;
  253. }
  254. }
  255. }
  256. }
  257. .title {
  258. margin: 0px auto 15px auto;
  259. text-align: center;
  260. color: #202124;
  261. font-weight: 500;
  262. font-size: 20px;
  263. }
  264. .title-line{
  265. width: 40px;
  266. border-bottom: 2px solid #006CFF;
  267. margin: 0px auto 30px auto;
  268. }
  269. .login-form {
  270. box-sizing: border-box;
  271. background: #ffffff;
  272. width: 440px;
  273. height: 500px;
  274. padding: 40px 25px 5px 25px;
  275. box-shadow:4px -4px 10px rgba(0, 108, 255, 0.15);
  276. .el-input {
  277. height: 38px;
  278. input {
  279. height: 38px;
  280. }
  281. }
  282. .input-icon {
  283. height: 20px;
  284. width: 20px;
  285. margin-left: 2px;
  286. margin-top: 10px;
  287. }
  288. .input-icon2{
  289. height: 20px;
  290. width: 20px;
  291. margin-right: 4px;
  292. margin-top: 10px;
  293. }
  294. }
  295. .login-tip {
  296. font-size: 13px;
  297. text-align: center;
  298. color: #bfbfbf;
  299. }
  300. .login-code {
  301. width: 33%;
  302. height: 38px;
  303. float: right;
  304. img {
  305. cursor: pointer;
  306. vertical-align: middle;
  307. }
  308. }
  309. .el-login-footer {
  310. height: 40px;
  311. line-height: 40px;
  312. position: fixed;
  313. bottom: 0;
  314. width: 100%;
  315. text-align: center;
  316. color: #fff;
  317. font-family: Arial;
  318. font-size: 12px;
  319. letter-spacing: 1px;
  320. }
  321. .login-code-img {
  322. height: 38px;
  323. }
  324. </style>