login.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. <template>
  2. <div class="login">
  3. <div class="login-bg">
  4. <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
  5. <i class="el-icon-setting"
  6. @click="editHostEvent"
  7. style="float: right; margin-top: -17px; margin-right: -10px; cursor: pointer; display: none;"></i>
  8. <h3 class="title">{{ applicationName }}</h3>
  9. <div class="title-bg"></div>
  10. <el-form-item prop="username">
  11. <el-input
  12. v-model="loginForm.username"
  13. type="text"
  14. auto-complete="off"
  15. placeholder="帐号"
  16. >
  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" v-if="captchaEnabled">
  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-form-item prop="ip">
  46. <el-input
  47. v-model="loginForm.ip"
  48. type="text"
  49. auto-complete="off"
  50. placeholder="IP地址"
  51. >
  52. <svg-icon slot="prefix" icon-class="ip" class="el-input__icon input-icon"/>
  53. </el-input>
  54. </el-form-item>
  55. <el-form-item prop="port">
  56. <el-input
  57. v-model="loginForm.port"
  58. type="text"
  59. auto-complete="off"
  60. placeholder="端口号"
  61. >
  62. <svg-icon slot="prefix" icon-class="port" class="el-input__icon input-icon"/>
  63. </el-input>
  64. </el-form-item>
  65. <el-checkbox v-model="loginForm.rememberMe" v-if="rememberMeView" style="margin:0px 0px 25px 0px;">记住帐号
  66. </el-checkbox>
  67. <el-form-item style="width:100%;">
  68. <el-button
  69. :loading="loading"
  70. size="medium"
  71. type="primary"
  72. style="width:100%;"
  73. @click.native.prevent="handleLogin"
  74. >
  75. <span v-if="!loading">登 录</span>
  76. <span v-else>登录中</span>
  77. </el-button>
  78. <div style="float: right;" v-if="register">
  79. <router-link class="link-type" :to="'/register'">立即注册</router-link>
  80. </div>
  81. </el-form-item>
  82. </el-form>
  83. </div>
  84. <!-- 底部 -->
  85. <div class="el-login-footer">
  86. <span>Copyright © {{ copyrightDate }} 重庆川仪控制系统有限公司版权所有</span>
  87. </div>
  88. </div>
  89. </template>
  90. <script>
  91. import cqcyCode from '@/utils/cqcyCode'
  92. import {getCode} from '@/api/user'
  93. import {showAlertMsgWin, showAlertWin, showLoading, showPromptWin, testHost} from '@/utils/cqcy'
  94. export default {
  95. name: "Login",
  96. inject: ['getWebSocket'],
  97. data() {
  98. /** 表单验证判断:空格 */
  99. let testKeyBySpace = (rule, value, callback) => {
  100. if (!value || !value.trim()) {
  101. if (rule.field === 'username') {
  102. callback(new Error('帐号不能为空'))
  103. } else {
  104. callback(new Error('验证码不能为空'))
  105. }
  106. } else {
  107. callback()
  108. }
  109. }
  110. return {
  111. codeUrl: "",
  112. publicKey: "",
  113. loginForm: {
  114. username: "",
  115. password: "",
  116. ip: "127.0.0.1",
  117. port: "8081",
  118. rememberMe: false,
  119. code: "",
  120. uid: "",
  121. publicKey: ""
  122. },
  123. loginRules: {
  124. username: [
  125. {required: true, trigger: "blur", message: "请输入您的帐号"},
  126. {validator: testKeyBySpace}
  127. ],
  128. password: [
  129. {required: true, trigger: "blur", message: "请输入您的密码"}
  130. ],
  131. code: [
  132. {required: true, trigger: "change", message: "请输入验证码"},
  133. {validator: testKeyBySpace}
  134. ],
  135. ip: [
  136. {required: true, message: '请输入IP地址', trigger: 'blur'},
  137. {validator: this.testKeyByIp}
  138. ],
  139. port: [
  140. {required: true, message: '请输入端口号', trigger: 'blur'},
  141. {validator: this.testKeyByPort}
  142. ],
  143. },
  144. loading: false,
  145. // 验证码开关
  146. captchaEnabled: false,
  147. // 注册开关
  148. register: false,
  149. // 记住帐号开关
  150. rememberMeView: true,
  151. // 版权信息
  152. copyrightDate: new Date().getFullYear(),
  153. applicationName: '',
  154. dialogServerVisible: false,
  155. redirect: undefined
  156. };
  157. },
  158. watch: {
  159. $route: {
  160. handler: function (route) {
  161. // this.redirect = route.query && route.query.redirect;
  162. },
  163. immediate: true
  164. }
  165. },
  166. created() {
  167. // this.getCode();
  168. this.getCookie();
  169. this.applicationName = process.env.VUE_APP_SOFTWARENAME
  170. },
  171. methods: {
  172. /** 表单验证判断:IP */
  173. testKeyByIp(rule, value, callback) {
  174. const key = /^((25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))\.){3}(25[0-5]|2[0-4]\d|((1\d{2})|([1-9]?\d)))$/
  175. if (!key.test(value)) {
  176. callback(new Error('IP 地址不合法'))
  177. } else {
  178. callback()
  179. }
  180. },
  181. /** 表单验证判断:端口 */
  182. testKeyByPort(rule, value, callback) {
  183. const key = /^([0-9]|[1-9]\d{1,3}|[1-5]\d{4}|6[0-4]\d{4}|65[0-4]\d{2}|655[0-2]\d|6553[0-5])$/
  184. if (!key.test(value)) {
  185. callback(new Error('端口号不合法'))
  186. } else {
  187. callback()
  188. }
  189. },
  190. /** 获取验证码 */
  191. getCode(callback) {
  192. const loading = showLoading(this, '正在初始化环境···')
  193. getCode(true).then(res => {
  194. loading.close()
  195. this.captchaEnabled = res.captchaEnabled === undefined ? this.captchaEnabled : res.captchaEnabled;
  196. if (this.captchaEnabled) {
  197. this.codeUrl = "data:image/gif;base64," + data.verifyCode;
  198. }
  199. let data = res.data;
  200. this.loginForm.uid = data.uid;
  201. this.loginForm.publicKey = data.publicKey;
  202. if (callback) callback(true)
  203. }).catch((e) => {
  204. loading.close()
  205. showAlertWin(this, null, e)
  206. if (callback) callback(false)
  207. })
  208. },
  209. /** 获取记住的帐号信息 */
  210. getCookie() {
  211. const username = localStorage.getItem('J_USER_NAME')
  212. const rememberMe = localStorage.getItem('J_BTN')
  213. const ip = localStorage.getItem('REQ_IP')
  214. const port = localStorage.getItem('REQ_PORT')
  215. this.loginForm = {
  216. username: username === undefined ? this.loginForm.username : username,
  217. rememberMe: rememberMe === undefined ? false : Boolean(rememberMe),
  218. ip: ip ? ip : this.loginForm.ip,
  219. port: port ? port : this.loginForm.port
  220. };
  221. },
  222. /** 登录 */
  223. handleLogin() {
  224. let _this = this
  225. _this.$refs.loginForm.validate(valid => {
  226. if (valid) {
  227. localStorage.setItem('REQ_IP', _this.loginForm.ip)
  228. localStorage.setItem('REQ_PORT', _this.loginForm.port)
  229. let h = _this.loginForm.ip + ':' + _this.loginForm.port
  230. localStorage.setItem('SYS_HOST', cqcyCode['protocol'] + h)
  231. localStorage.setItem('SYS_HOST_BASE', h)
  232. if (_this.loginForm.rememberMe) {
  233. localStorage.setItem('J_USER_NAME', _this.loginForm.username)
  234. localStorage.setItem('J_BTN', 'true')
  235. } else {
  236. localStorage.removeItem('J_USER_NAME')
  237. localStorage.removeItem('J_BTN')
  238. }
  239. if (!_this.loginForm.publicKey) {
  240. _this.getCode((result) => {
  241. if (!result) {
  242. // const msg = cqcyCode[900];
  243. // Message.error(msg)
  244. return
  245. }
  246. _this.withLogin()
  247. })
  248. return
  249. }
  250. _this.withLogin()
  251. }
  252. });
  253. },
  254. withLogin() {
  255. this.loading = true;
  256. this.$store.dispatch('user/Login', this.loginForm).then(() => {
  257. this.getWebSocket.initWebSocket()
  258. this.$router.push({path: this.redirect || '/', query: {}})
  259. this.loading = false
  260. }).catch((e) => {
  261. showAlertWin(this, null, e)
  262. this.loading = false
  263. if (this.captchaEnabled) {
  264. this.getCode()
  265. }
  266. })
  267. },
  268. /** 设置服务端请求地址 */
  269. editHostEvent() {
  270. let sysHost = localStorage.getItem('SYS_HOST')
  271. showPromptWin(this, null, '请输入服务端地址', sysHost, (val) => {
  272. if (!testHost(val)) {
  273. return '请输入合法的地址信息'
  274. }
  275. }, (value) => {
  276. localStorage.setItem('SYS_HOST', value)
  277. // localStorage.setItem('SYS_HOST_BASE', value)
  278. showAlertMsgWin(this, null, '设置成功!')
  279. setTimeout(() => {
  280. window.location.reload()
  281. }, 500)
  282. })
  283. }
  284. }
  285. };
  286. </script>
  287. <style rel="stylesheet/scss" lang="scss">
  288. .login {
  289. height: 100%;
  290. background-image: url("../assets/images/cqcybg.png");
  291. background-size: cover;
  292. }
  293. .title {
  294. margin: 0px auto 5px auto;
  295. text-align: center;
  296. font-weight: 600;
  297. color: black;
  298. }
  299. .title-bg {
  300. height: 4px;
  301. width: 35px;
  302. background: #1890ff;
  303. margin: auto auto 25px;
  304. }
  305. .login-bg {
  306. display: flex;
  307. justify-content: flex-end;
  308. height: 100%;
  309. align-items: center;
  310. margin-right: 10%;
  311. }
  312. .login-form {
  313. border-radius: 6px;
  314. background: #ffffff;
  315. width: 400px;
  316. height: 425px;
  317. padding: 25px 25px 5px 25px;
  318. .el-input {
  319. height: 38px;
  320. input {
  321. height: 38px;
  322. }
  323. }
  324. .input-icon {
  325. height: 39px;
  326. width: 14px;
  327. margin-left: 2px;
  328. }
  329. }
  330. .login-tip {
  331. font-size: 13px;
  332. text-align: center;
  333. color: #bfbfbf;
  334. }
  335. .login-code {
  336. width: 33%;
  337. height: 38px;
  338. float: right;
  339. img {
  340. cursor: pointer;
  341. vertical-align: middle;
  342. }
  343. }
  344. .el-login-footer {
  345. height: 40px;
  346. line-height: 40px;
  347. position: fixed;
  348. bottom: 0;
  349. width: 100%;
  350. text-align: center;
  351. color: #4a4a4a;
  352. font-family: Arial;
  353. font-size: 12px;
  354. letter-spacing: 1px;
  355. }
  356. .login-code-img {
  357. height: 38px;
  358. }
  359. </style>