Login.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <template>
  2. <div class="container">
  3. <el-row>
  4. <el-col :span="13">
  5. &nbsp;
  6. </el-col>
  7. <el-col :span="11">
  8. <div id="login_form">
  9. <div id="logo"></div>
  10. <div id="title">南昌交通学院热水端管理后台</div>
  11. <el-form label-width="0px" :model="ruleForm" status-icon :rules="rules" ref="ruleForm"
  12. class="demo-ruleForm">
  13. <el-form-item prop="uname">
  14. <el-input ref="zhanghao" placeholder="请输入账号" maxlength="16" v-model="ruleForm.uname"
  15. prefix-icon="el-icon-login-user"></el-input>
  16. </el-form-item>
  17. <el-form-item prop="upass">
  18. <el-input ref="mima" placeholder="请输入密码" maxlength="16" type="password"
  19. v-model="ruleForm.upass" prefix-icon="el-icon-login-pass"
  20. @keyup.enter.native="submitForm('ruleForm')">
  21. </el-input>
  22. </el-form-item>
  23. <el-form-item>
  24. <el-button type="primary" @click="submitForm('ruleForm')">登 陆</el-button>
  25. </el-form-item>
  26. </el-form>
  27. </div>
  28. </el-col>
  29. </el-row>
  30. </div>
  31. </template>
  32. <script>
  33. export default {
  34. data() {
  35. var checkName = (rule, value, callback) => {
  36. // console.log(rule, value, callback);
  37. if (!value) {
  38. return callback(new Error('请输入账号'));
  39. }
  40. setTimeout(() => {
  41. if (value.length < 5) {
  42. callback(new Error('账号长度不小于5位'));
  43. } else {
  44. callback();
  45. }
  46. }, 30);
  47. };
  48. var validatePass = (rule, value, callback) => {
  49. // console.log(rule, value, callback);
  50. if (value === '') {
  51. callback(new Error('请输入密码'));
  52. }
  53. setTimeout(() => {
  54. if (value.length < 5) {
  55. callback(new Error('密码长度不小于5位'));
  56. } else {
  57. callback();
  58. }
  59. }, 30);
  60. };
  61. return {
  62. ruleForm: {
  63. uname: '',
  64. upass: ''
  65. },
  66. rules: {
  67. uname: [{
  68. validator: checkName,
  69. trigger: 'blur'
  70. }],
  71. upass: [{
  72. validator: validatePass,
  73. trigger: 'blur'
  74. }]
  75. }
  76. };
  77. },
  78. methods: {
  79. submitForm(formName) {
  80. let _this = this;
  81. let params = {
  82. number: '',
  83. password: ''
  84. }
  85. var username = String(_this.ruleForm.uname).trim()
  86. var userpass = String(_this.ruleForm.upass).trim()
  87. if (username && userpass) {
  88. params.number = username
  89. params.password = userpass
  90. } else {
  91. _this.$message.warning('请输入账号和密码!')
  92. if (!username)
  93. _this.$refs['zhanghao'].focus()
  94. else
  95. _this.$refs['mima'].focus()
  96. return
  97. }
  98. _this.$refs[formName].validate((valid) => {
  99. _this.$axios.get('/shuidian/HotWaters/userlog.action', {
  100. params: params
  101. })
  102. .then(res => {
  103. // console.log(res.data);
  104. if (typeof(res.data.msg) != 'undefined' && res.data.msg != '' && JSON.stringify(res
  105. .data) != '{}') {
  106. // console.log(typeof(res.data.msg));
  107. if (res.data.msg == '用户名或密码不正确') {
  108. _this.$message.error(res.data.msg);
  109. return
  110. }
  111. _this.$message.success(res.data.msg);
  112. sessionStorage.setItem('uname', username);
  113. // 请求头部携带token
  114. _this.$axios.defaults.headers['token'] = res.data.access_token;
  115. // console.log(_this.$axios.defaults);
  116. if (typeof(res.data.name) == 'undefined') {
  117. sessionStorage.setItem('unic', '未设置昵称');
  118. } else {
  119. sessionStorage.setItem('unic', res.data.name);
  120. }
  121. _this.$router.replace('/index'); //跳转到首页
  122. } else {
  123. _this.$message({
  124. showClose: true,
  125. message: "登陆异常:" + res.data.msg,
  126. type: 'error'
  127. });
  128. }
  129. })
  130. .catch(err => {
  131. // console.log(err);
  132. _this.$message.error('捕捉异常:' + err)
  133. })
  134. });
  135. }
  136. }
  137. }
  138. </script>
  139. <style scoped>
  140. @import url("./login.css");
  141. </style>