Login.vue 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <template>
  2. <div class="login-container">
  3. <banner hideLg="hideLg"></banner>
  4. <div class="login-img fl"><img src="../../images/loginImg.png"/></div>
  5. <div class="login-box">
  6. <!-- <div class="box-le"><img src="../../images/left.jpg"/></div> -->
  7. <div class="box-ri">
  8. <div class="title-box clearfix"> <h3 class="login-title fl">登录</h3> <h4 class="num fr" @click="toRegister">立即注册</h4></div>
  9. <div class="ri-center">
  10. <el-form :model="ruleForm2" :rules="rules2" ref="ruleFormReg">
  11. <el-form-item prop="tel">
  12. <el-input v-model="ruleForm2.tel" name="username" autocomplete="off" maxlength="11"
  13. placeholder="请输入用户名或手机号"
  14. clearable class="ri-tel" @keyup.enter.native="submitForm('ruleForm2')"
  15. ref="username"></el-input>
  16. </el-form-item>
  17. <div style="height: 20px;"></div>
  18. <el-form-item prop="pwd">
  19. <el-input v-model="ruleForm2.pwd" name="password" autocomplete="off" maxlength="16"
  20. placeholder="请输入密码"
  21. clearable type="password" class="ri-pwd"
  22. @keyup.enter.native="submitForm('ruleForm2')" ref="password"></el-input>
  23. </el-form-item>
  24. <el-form-item>
  25. <!-- <span class="num" @click="toRegister">注册账号</span> -->
  26. <span class="pwd" @click="toForgetPsw">忘记密码?</span>
  27. </el-form-item>
  28. <el-form-item>
  29. <el-button plain v-if="otherCor" >登录</el-button>
  30. <el-button ref="btn" v-else class="otherCor" plain @click="submitForm('ruleForm2')">登录
  31. </el-button>
  32. </el-form-item>
  33. </el-form>
  34. </div>
  35. </div>
  36. </div>{{hideLg}}
  37. <copy-right-info v-if="!hideLg"></copy-right-info>
  38. </div>
  39. </template>
  40. <script>
  41. import Vue from 'vue';
  42. import banner from '../common/Banner.vue';
  43. import copyRightInfo from '../common/CopyRightInfo.vue';
  44. import api from '../../api/index.js';
  45. import './login.less';
  46. import md5 from 'js-md5'
  47. const qs = require('qs');
  48. export default {
  49. name: 'Login',
  50. data() {
  51. //手机号码验证
  52. var myreg = /^1[0-9]{10}$/;
  53. //密码验证 纯字母纯英文
  54. var numreg = /^(?![0-9]+$)(?![a-zA-Z]+$)/;
  55. //密码验证 密码长度
  56. var lengreg = /^[0-9A-Za-z]{6,16}$/;
  57. var validatePass = (rule, value, callback) => {
  58. // if(isNaN(value) == false){ //手机号
  59. // 正常情况
  60. if (value.trim() == '') {
  61. callback(new Error('用户名不能为空'));
  62. this.$refs.username.value = "";
  63. }
  64. // 手机号
  65. if (isNaN(value) == false) {
  66. if (value.length !== 11 || !myreg.test(value)) {
  67. callback(new Error('手机号格式有误'));
  68. } else {
  69. callback();
  70. }
  71. } else if (isNaN(value) == true) {
  72. callback();
  73. }
  74. };
  75. var validatePass2 = (rule, value, callback) => {
  76. if (value.trim() == '') {
  77. callback(new Error('密码不能为空'));
  78. this.$refs.password.value = "";
  79. } else if (value.length < 6) {
  80. callback(new Error('密码长度应为6位至16位之间'));
  81. } else if (!numreg.test(value)) {
  82. callback(new Error('密码不能为纯数字或纯英文'));
  83. } else {
  84. callback();
  85. }
  86. };
  87. return {
  88. otherCor: true,
  89. hideLg:false,
  90. ruleForm2: {
  91. tel: '',
  92. pwd: ''
  93. },
  94. rules2: {
  95. tel: [{
  96. required: true,
  97. validator: validatePass,
  98. trigger: 'blur'
  99. }],
  100. pwd: [{
  101. required: true,
  102. validator: validatePass2,
  103. trigger: 'blur'
  104. }],
  105. }
  106. };
  107. },
  108. mounted() {
  109. this.hideLg = window.location.hash.indexOf("hideLg=")>-1;
  110. if (window.history && window.history.pushState) {
  111. history.pushState(null, null, document.URL);
  112. window.addEventListener('popstate', function () {
  113. history.pushState(null, null, document.URL);
  114. });
  115. }
  116. },
  117. computed: {
  118. nextButton() {
  119. const {
  120. tel,
  121. pwd
  122. } = this.ruleForm2;
  123. return {
  124. tel,
  125. pwd
  126. }
  127. }
  128. },
  129. watch: {
  130. nextButton: {
  131. handler: function (val) {
  132. if (val.tel.trim() && val.pwd.trim()) {
  133. this.otherCor = false;
  134. } else {
  135. this.otherCor = true;
  136. }
  137. },
  138. deep: true
  139. }
  140. },
  141. methods: {
  142. CalcuMD5(password){
  143. // password = password.toUpperCase();
  144. password = md5(password);
  145. return password;
  146. },
  147. submitForm: function () {
  148. const username = this.ruleForm2.tel;
  149. const password = this.CalcuMD5(this.ruleForm2.pwd);
  150. let params = {
  151. "username": username,
  152. "password": password
  153. }
  154. //重新登录清空原有token;
  155. localStorage.removeItem('token');
  156. this.$refs.ruleFormReg.validate((valid) => {
  157. if (valid) {
  158. api.loginMess(params).then((res) => {
  159. if (res.status == 200) {
  160. // console.log(res)
  161. if (res.data.code == '10020000') { //未注册
  162. // this.$message.error(res.data.msg);
  163. this.$message.error("该账号或手机号未注册!")
  164. } else if (res.data.code == '10020001') { //密码错误
  165. this.$message.error(res.data.msg);
  166. } else if (res.data.code == '0') {
  167. const token = JSON.stringify(res.data.data);
  168. localStorage.setItem("token", token);
  169. this.$message({
  170. message: '登录成功!',
  171. type: 'success',
  172. duration: 1000,
  173. onClose: function () {
  174. const type = res.data.data.type;
  175. if (type == 0) {
  176. this.$router.push({
  177. path: '/user'
  178. });
  179. } else if (type == 1) {
  180. this.$router.push({
  181. path: '/admin'
  182. });
  183. }
  184. }.bind(this)
  185. });
  186. }
  187. }
  188. }).catch((err) => {
  189. console.log(err);
  190. })
  191. } else {
  192. console.log('faild')
  193. return false;
  194. }
  195. });
  196. },
  197. toRegister() { //点击注册
  198. this.$router.push({
  199. path: '/register'
  200. });
  201. },
  202. toForgetPsw() {//忘记密码
  203. this.$router.push({
  204. path: '/forgetPassword'
  205. });
  206. }
  207. },
  208. components: {
  209. 'banner': banner,
  210. 'copyRightInfo': copyRightInfo
  211. },
  212. }
  213. </script>