login.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. <template>
  2. <div class="login">
  3. <el-form
  4. ref="loginForm"
  5. :model="loginForm"
  6. :rules="loginRules"
  7. class="login-form"
  8. >
  9. <div v-if="state === 1">
  10. <h3 class="title">用户登录</h3>
  11. <el-form-item prop="username">
  12. <el-input
  13. v-model="loginForm.username"
  14. type="text"
  15. auto-complete="off"
  16. placeholder="账号"
  17. >
  18. <svg-icon
  19. slot="prefix"
  20. icon-class="user"
  21. class="el-input__icon input-icon"
  22. />
  23. </el-input>
  24. </el-form-item>
  25. <el-form-item prop="password">
  26. <el-input
  27. v-model="loginForm.password"
  28. type="password"
  29. auto-complete="off"
  30. placeholder="密码"
  31. @keyup.enter.native="handleLogin"
  32. >
  33. <svg-icon
  34. slot="prefix"
  35. icon-class="password"
  36. class="el-input__icon input-icon"
  37. />
  38. </el-input>
  39. </el-form-item>
  40. <el-form-item prop="code" v-if="captchaOnOff">
  41. <el-input
  42. v-model="loginForm.code"
  43. auto-complete="off"
  44. placeholder="验证码"
  45. style="width: 63%"
  46. @keyup.enter.native="handleLogin"
  47. >
  48. <svg-icon
  49. slot="prefix"
  50. icon-class="validCode"
  51. class="el-input__icon input-icon"
  52. />
  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. </div>
  59. <div v-else>
  60. <h3 class="title">忘记密码</h3>
  61. <el-form-item prop="phone">
  62. <el-input
  63. v-model="loginForm.phone"
  64. type="text"
  65. auto-complete="off"
  66. placeholder="请输入手机号"
  67. >
  68. <svg-icon
  69. slot="prefix"
  70. icon-class="user"
  71. class="el-input__icon input-icon"
  72. />
  73. </el-input>
  74. </el-form-item>
  75. <el-form-item>
  76. <el-input
  77. v-model="loginForm.codeFor"
  78. auto-complete="off"
  79. placeholder="请输入验证码"
  80. class="inputName"
  81. >
  82. <svg-icon
  83. slot="prefix"
  84. icon-class="validCode"
  85. class="el-input__icon input-icon"
  86. />
  87. <template slot="append">
  88. <span v-show="show" @click="getCodePassword" class="code"
  89. >获取验证码</span
  90. >
  91. <span v-show="!show" class="count code">{{ count }} s</span>
  92. </template>
  93. </el-input>
  94. <!-- <template slot="append">.com</template> -->
  95. </el-form-item>
  96. <el-form-item prop="pwdFor">
  97. <el-input
  98. v-model="loginForm.pwdFor"
  99. type="password"
  100. auto-complete="off"
  101. placeholder="请输入新密码"
  102. >
  103. <svg-icon
  104. slot="prefix"
  105. icon-class="password"
  106. class="el-input__icon input-icon"
  107. />
  108. </el-input>
  109. </el-form-item>
  110. <el-form-item prop="passwordFor">
  111. <el-input
  112. v-model="loginForm.passwordFor"
  113. type="password"
  114. auto-complete="off"
  115. placeholder="确认密码"
  116. f
  117. >
  118. <svg-icon
  119. slot="prefix"
  120. icon-class="password"
  121. class="el-input__icon input-icon"
  122. />
  123. </el-input>
  124. </el-form-item>
  125. </div>
  126. <div class="forget-box">
  127. <div>
  128. <el-checkbox
  129. v-model="loginForm.rememberMe"
  130. style="margin: 0px 0px 25px 0px"
  131. >记住密码</el-checkbox
  132. >
  133. </div>
  134. <div class="forget-box__password" @click="state=3-state">{{state==1?'忘记密码':'返回登录'}}</div>
  135. </div>
  136. <el-form-item style="width: 100%">
  137. <el-button
  138. :loading="loading"
  139. size="medium"
  140. type="primary"
  141. style="width: 100%"
  142. @click.native.prevent="handleLogin"
  143. >
  144. <span v-if="!loading">登 录</span>
  145. <span v-else>登 录 中...</span>
  146. </el-button>
  147. <div style="float: right" v-if="register">
  148. <router-link class="link-type" :to="'/register'"
  149. >立即注册</router-link
  150. >
  151. </div>
  152. </el-form-item>
  153. </el-form>
  154. <!-- 底部 -->
  155. <div class="el-login-footer">
  156. <span style="color: #2c3e50">
  157. 重药控股(浙江)有限公司 & 杭州六智科技有限公司
  158. </span>
  159. </div>
  160. </div>
  161. </template>
  162. <script>
  163. import { getCodeImg, forgetPwd, sendCode } from "@/api/login";
  164. import Cookies from "js-cookie";
  165. import { encrypt, decrypt } from "@/utils/jsencrypt";
  166. export default {
  167. name: "Login",
  168. data() {
  169. return {
  170. codeUrl: "",
  171. cookiePassword: "",
  172. loginForm: {
  173. username: "",
  174. password: "",
  175. rememberMe: false,
  176. code: "",
  177. uuid: "",
  178. phone: "", //手机号码
  179. codeFor: "", //验证码
  180. passwordFor: "", //确认密码
  181. pwdFor: "", //密码
  182. },
  183. state: 1,
  184. show: true,
  185. count: "",
  186. timer: null,
  187. loginRules: {
  188. username: [
  189. { required: true, trigger: "blur", message: "请输入您的账号" },
  190. ],
  191. password: [
  192. { required: true, trigger: "blur", message: "请输入您的密码" },
  193. ],
  194. code: [{ required: true, trigger: "change", message: "请输入验证码" }],
  195. },
  196. loading: false,
  197. // 验证码开关
  198. captchaOnOff: true,
  199. // 注册开关
  200. register: false,
  201. redirect: undefined,
  202. };
  203. },
  204. watch: {
  205. $route: {
  206. handler: function (route) {
  207. this.redirect = route.query && route.query.redirect;
  208. },
  209. immediate: true,
  210. },
  211. },
  212. created() {
  213. this.getCode();
  214. this.getCookie();
  215. },
  216. methods: {
  217. getCodePassword() {
  218. const TIME_COUNT = 60;
  219. if (this.loginForm.phone) {
  220. if (!this.timer) {
  221. this.count = TIME_COUNT;
  222. this.show = false;
  223. sendCode(this.loginForm.phone).then((res) => {
  224. //console.log(res);
  225. });
  226. this.timer = setInterval(() => {
  227. if (this.count > 0 && this.count <= TIME_COUNT) {
  228. this.count--;
  229. } else {
  230. this.show = true;
  231. clearInterval(this.timer);
  232. this.timer = null;
  233. }
  234. }, 1000);
  235. }
  236. } else {
  237. this.msgError("请先输入手机号");
  238. }
  239. },
  240. getCode() {
  241. getCodeImg().then((res) => {
  242. this.captchaOnOff =
  243. res.captchaOnOff === undefined ? true : res.captchaOnOff;
  244. if (this.captchaOnOff) {
  245. this.codeUrl = "data:image/gif;base64," + res.img;
  246. this.loginForm.uuid = res.uuid;
  247. }
  248. });
  249. },
  250. getCookie() {
  251. const username = Cookies.get("username");
  252. const password = Cookies.get("password");
  253. const rememberMe = Cookies.get("rememberMe");
  254. this.loginForm = {
  255. username: username === undefined ? this.loginForm.username : username,
  256. password:
  257. password === undefined ? this.loginForm.password : decrypt(password),
  258. rememberMe: rememberMe === undefined ? false : Boolean(rememberMe),
  259. };
  260. },
  261. handleLogin() {
  262. this.$refs.loginForm.validate((valid) => {
  263. if (valid) {
  264. if (this.state === 1) {
  265. this.loading = true;
  266. if (this.loginForm.rememberMe) {
  267. Cookies.set("username", this.loginForm.username, { expires: 30 });
  268. Cookies.set("password", encrypt(this.loginForm.password), {
  269. expires: 30,
  270. });
  271. Cookies.set("rememberMe", this.loginForm.rememberMe, {
  272. expires: 30,
  273. });
  274. } else {
  275. Cookies.remove("username");
  276. Cookies.remove("password");
  277. Cookies.remove("rememberMe");
  278. window.localStorage.setItem("userName", this.loginForm.username);
  279. }
  280. this.$store
  281. .dispatch("Login", this.loginForm)
  282. .then(() => {
  283. this.$router
  284. .push({ path: this.redirect || "/" })
  285. .catch(() => {});
  286. })
  287. .catch(() => {
  288. this.loading = false;
  289. if (this.captchaOnOff) {
  290. this.getCode();
  291. }
  292. });
  293. } else {
  294. if (!this.loginForm.phone || !this.loginForm.codeFor || !this.loginForm.pwdFor || !this.loginForm.passwordFor) {
  295. return this.msgError("请完善表单输入");
  296. }
  297. this.loading = true
  298. forgetPwd(
  299. this.loginForm.codeFor,
  300. this.loginForm.phone,
  301. this.loginForm.pwdFor,
  302. this.loginForm.passwordFor
  303. )
  304. .then((res) => {
  305. this.loading = false
  306. this.state = 1
  307. })
  308. .catch((res) => {
  309. this.loading = false;
  310. //this.msgError(res.msg);
  311. });
  312. }
  313. }
  314. });
  315. },
  316. //忘记密码
  317. onPassword() {
  318. this.state = 2;
  319. // this.loginForm.codeFor = null;
  320. // this.loginForm.phone = null;
  321. },
  322. },
  323. };
  324. </script>
  325. <style rel="stylesheet/scss" lang="scss">
  326. .login {
  327. // display: flex;
  328. // justify-content: end;
  329. // align-items: center;
  330. height: 100%;
  331. background-image: url("../assets/images/login-zy.png");
  332. background-size: contain;
  333. background-repeat: no-repeat;
  334. margin-right: 100px;
  335. .el-input-group__append,
  336. .el-input-group__prepend {
  337. background: transparent;
  338. border-radius: 0 8px 8px 0;
  339. border-left: none;
  340. }
  341. .inputName {
  342. .el-input__inner {
  343. border-radius: 8px 0 0 8px;
  344. border-right: none;
  345. }
  346. }
  347. }
  348. .title {
  349. margin: 0px auto 30px auto;
  350. text-align: center;
  351. color: #707070;
  352. }
  353. .login-form {
  354. position: absolute;
  355. right: 50px;
  356. top: 15%;
  357. border-radius: 6px;
  358. // background: #ffffff;
  359. background-image: url("../assets/images/login-form.png");
  360. background-size: contain;
  361. background-repeat: no-repeat;
  362. width: 400px;
  363. height: 600px;
  364. padding: 55px 25px 5px 25px;
  365. .el-input {
  366. height: 38px;
  367. input {
  368. height: 38px;
  369. }
  370. }
  371. .input-icon {
  372. height: 39px;
  373. width: 14px;
  374. margin-left: 2px;
  375. }
  376. .forget-box {
  377. display: flex;
  378. justify-content: space-between;
  379. &__password {
  380. font-size: 14px;
  381. color: #999;
  382. cursor: pointer;
  383. }
  384. }
  385. // .code{
  386. // position: absolute;
  387. // right: 5px;
  388. // top: 0;
  389. // height: 100%;
  390. // width: 50px;
  391. // }
  392. }
  393. .login-tip {
  394. font-size: 13px;
  395. text-align: center;
  396. color: #bfbfbf;
  397. }
  398. .login-code {
  399. width: 33%;
  400. height: 38px;
  401. float: right;
  402. img {
  403. cursor: pointer;
  404. vertical-align: middle;
  405. }
  406. }
  407. .el-login-footer {
  408. height: 40px;
  409. line-height: 40px;
  410. position: fixed;
  411. bottom: 0;
  412. width: 100%;
  413. text-align: center;
  414. color: #fff;
  415. font-family: Arial;
  416. font-size: 12px;
  417. letter-spacing: 1px;
  418. }
  419. .login-code-img {
  420. height: 38px;
  421. }
  422. .el-input__inner:hover {
  423. border-color: #dcdfe6;
  424. }
  425. // .el-input:hover {
  426. // border:1px solid #c0c4cc;
  427. // }
  428. </style>