login.vue 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <template>
  2. <div class="login" id="loginBox">
  3. <div class="login-con">
  4. <div class="img-box">
  5. <img src="../assets/images/login_left.png" alt="">
  6. </div>
  7. <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
  8. <h3 class="title">互联网医院医生端</h3>
  9. <el-form-item>
  10. <el-radio-group v-model="loginForm.type">
  11. <el-radio-button label="1">医生</el-radio-button>
  12. <el-radio-button label="2">药剂师</el-radio-button>
  13. </el-radio-group>
  14. </el-form-item>
  15. <el-form-item prop="account">
  16. <el-input v-model="loginForm.account" type="text" auto-complete="off" placeholder="账号">
  17. <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
  18. </el-input>
  19. </el-form-item>
  20. <el-form-item prop="password">
  21. <el-input
  22. v-model="loginForm.password"
  23. type="password"
  24. auto-complete="off"
  25. placeholder="密码"
  26. @keyup.enter.native="handleLogin"
  27. >
  28. <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
  29. </el-input>
  30. </el-form-item>
  31. <el-form-item prop="code">
  32. <el-input
  33. v-model="loginForm.code"
  34. auto-complete="off"
  35. placeholder="验证码"
  36. style="width: 63%"
  37. @keyup.enter.native="handleLogin"
  38. >
  39. <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
  40. </el-input>
  41. <div class="login-code">
  42. <img :src="codeUrl" @click="getCode" class="login-code-img"/>
  43. </div>
  44. </el-form-item>
  45. <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">记住密码</el-checkbox>
  46. <el-form-item style="width:100%;">
  47. <el-button
  48. :loading="loading"
  49. size="medium"
  50. type="primary"
  51. style="width:100%;"
  52. @click.native.prevent="handleLogin"
  53. >
  54. <span v-if="!loading">登 录</span>
  55. <span v-else>登 录 中...</span>
  56. </el-button>
  57. </el-form-item>
  58. </el-form>
  59. </div>
  60. <!-- 底部 -->
  61. <div class="el-login-footer">
  62. <span>{{companyName}}</span>
  63. <a :href="icpUrl" target="_bank">{{icpRecord}}</a>
  64. </div>
  65. </div>
  66. </template>
  67. <script>
  68. import { imConfig } from '@/utils/im'
  69. import { sendHeartbeat } from "@/api/heartbeat";
  70. import { getCodeImg } from "@/api/common";
  71. import Cookies from "js-cookie";
  72. import { encrypt, decrypt } from '@/utils/jsencrypt'
  73. export default {
  74. name: "Login",
  75. data() {
  76. return {
  77. companyName: process.env.VUE_APP_COMPANY_NAME,
  78. icpRecord: process.env.VUE_APP_ICP_RECORD,
  79. icpUrl: process.env.VUE_APP_ICP_URL,
  80. codeUrl: "",
  81. cookiePassword: "",
  82. loginForm: {
  83. account: "",
  84. password: "",
  85. rememberMe: false,
  86. code: "",
  87. uuid: "",
  88. type:'1',
  89. },
  90. loginRules: {
  91. account: [
  92. { required: true, trigger: "blur", message: "用户名不能为空" }
  93. ],
  94. password: [
  95. { required: true, trigger: "blur", message: "密码不能为空" }
  96. ],
  97. code: [{ required: true, trigger: "change", message: "验证码不能为空" }]
  98. },
  99. loading: false,
  100. };
  101. },
  102. watch: {
  103. $route: {
  104. handler: function(route) {
  105. },
  106. immediate: true
  107. }
  108. },
  109. mounted () {
  110. },
  111. created() {
  112. this.getCode();
  113. this.getCookie();
  114. },
  115. methods: {
  116. getCode() {
  117. getCodeImg().then(res => {
  118. this.codeUrl = "data:image/gif;base64," + res.img;
  119. this.loginForm.uuid = res.uuid;
  120. console.log(res.uuid)
  121. });
  122. },
  123. getCookie() {
  124. const account = Cookies.get("account");
  125. const password = Cookies.get("password");
  126. const rememberMe = Cookies.get('rememberMe');
  127. // 使用扩展运算符保留原来 loginForm 中的属性,包括 type
  128. this.loginForm = {
  129. ...this.loginForm,
  130. account: account === undefined ? this.loginForm.account : account,
  131. password: password === undefined ? this.loginForm.password : decrypt(password),
  132. rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
  133. };
  134. },
  135. handleLogin() {
  136. this.$refs.loginForm.validate(valid => {
  137. if (valid) {
  138. this.loading = true;
  139. if (this.loginForm.rememberMe) {
  140. Cookies.set("account", this.loginForm.account, { expires: 30 });
  141. Cookies.set("password", encrypt(this.loginForm.password), { expires: 30 });
  142. Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 });
  143. } else {
  144. Cookies.remove("account");
  145. Cookies.remove("password");
  146. Cookies.remove('rememberMe');
  147. }
  148. // 记录登录类型以便 Sidebar Logo 动态展示标题
  149. try {
  150. localStorage.setItem('loginType', this.loginForm.type)
  151. } catch (e) {}
  152. this.$store
  153. .dispatch("Login", this.loginForm)
  154. .then((res) => {
  155. //登录成功,启动心跳
  156. this.startHeartbeat();
  157. this.$router.push({ path: "/index" });
  158. })
  159. .catch(() => {
  160. this.loading = false;
  161. this.getCode();
  162. });
  163. }
  164. });
  165. },
  166. // 启动心跳(只执行一次)
  167. startHeartbeat() {
  168. sendHeartbeat().catch(err => {
  169. console.warn('心跳失败:', err);
  170. });
  171. },
  172. // 启动心跳(每30秒一次)
  173. // startHeartbeat() {
  174. // const tick = () => {
  175. // sendHeartbeat().catch(err => {
  176. // console.warn('心跳失败:', err);
  177. // // 可选:如果 token 过期,跳转登录页
  178. // // if (err.code === 401) { this.$store.dispatch('LogOut'); }
  179. // });
  180. // // 继续下一次心跳
  181. // setTimeout(tick, 30 * 1000);
  182. // };
  183. // tick(); // 立即执行第一次
  184. // }
  185. }
  186. };
  187. </script>
  188. <style rel="stylesheet/scss" lang="scss">
  189. .login {
  190. display: flex;
  191. justify-content: center;
  192. align-items: center;
  193. height: 100%;
  194. width: 100%;
  195. background-image: linear-gradient(#0e68c3, #1e99f5);
  196. background-size: cover;
  197. .login-con{
  198. width: 750px;
  199. display: flex;
  200. align-items: center;
  201. justify-content: space-between;
  202. border-radius: 10px;
  203. overflow: hidden;
  204. background: #ffffff;
  205. .img-box{
  206. width: 460px;
  207. height: 460px;
  208. img{
  209. width: 100%;
  210. height: 100%;
  211. object-fit: cover;
  212. }
  213. }
  214. }
  215. }
  216. .title {
  217. margin: 0px auto 30px auto;
  218. text-align: center;
  219. color: #707070;
  220. }
  221. .login-form {
  222. box-sizing: border-box;
  223. background: #ffffff;
  224. width: 330px;
  225. padding: 25px 25px 5px 25px;
  226. .el-input {
  227. height: 38px;
  228. input {
  229. height: 38px;
  230. }
  231. }
  232. .input-icon {
  233. height: 39px;
  234. width: 14px;
  235. margin-left: 2px;
  236. }
  237. }
  238. .login-tip {
  239. font-size: 13px;
  240. text-align: center;
  241. color: #bfbfbf;
  242. }
  243. .login-code {
  244. width: 33%;
  245. height: 38px;
  246. float: right;
  247. img {
  248. cursor: pointer;
  249. vertical-align: middle;
  250. }
  251. }
  252. .el-login-footer {
  253. height: 40px;
  254. line-height: 40px;
  255. position: fixed;
  256. bottom: 0;
  257. width: 100%;
  258. text-align: center;
  259. color: #fff;
  260. font-family: Arial;
  261. font-size: 12px;
  262. letter-spacing: 1px;
  263. }
  264. .login-code-img {
  265. height: 38px;
  266. }
  267. </style>