Login.vue 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <template>
  2. <div class="login-container">
  3. <banner></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>
  37. <copy-right-info></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. export default {
  48. name: 'Login',
  49. data() {
  50. //手机号码验证
  51. var myreg = /^1[0-9]{10}$/;
  52. //密码验证 纯字母纯英文
  53. var numreg = /^(?![0-9]+$)(?![a-zA-Z]+$)/;
  54. //密码验证 密码长度
  55. var lengreg = /^[0-9A-Za-z]{6,16}$/;
  56. var validatePass = (rule, value, callback) => {
  57. // if(isNaN(value) == false){ //手机号
  58. // 正常情况
  59. if (value.trim() == '') {
  60. callback(new Error('用户名不能为空'));
  61. this.$refs.username.value = "";
  62. }
  63. // 手机号
  64. if (isNaN(value) == false) {
  65. if (value.length !== 11 || !myreg.test(value)) {
  66. callback(new Error('手机号格式有误'));
  67. } else {
  68. callback();
  69. }
  70. } else if (isNaN(value) == true) {
  71. callback();
  72. }
  73. };
  74. var validatePass2 = (rule, value, callback) => {
  75. if (value.trim() == '') {
  76. callback(new Error('密码不能为空'));
  77. this.$refs.password.value = "";
  78. } else if (value.length < 6) {
  79. callback(new Error('密码长度应为6位至16位之间'));
  80. } else if (!numreg.test(value)) {
  81. callback(new Error('密码不能为纯数字或纯英文'));
  82. } else {
  83. callback();
  84. }
  85. };
  86. return {
  87. otherCor: true,
  88. ruleForm2: {
  89. tel: '',
  90. pwd: ''
  91. },
  92. rules2: {
  93. tel: [{
  94. required: true,
  95. validator: validatePass,
  96. trigger: 'blur'
  97. }],
  98. pwd: [{
  99. required: true,
  100. validator: validatePass2,
  101. trigger: 'blur'
  102. }],
  103. }
  104. };
  105. },
  106. mounted() {
  107. if (window.history && window.history.pushState) {
  108. history.pushState(null, null, document.URL);
  109. window.addEventListener('popstate', function () {
  110. history.pushState(null, null, document.URL);
  111. });
  112. }
  113. },
  114. computed: {
  115. nextButton() {
  116. const {
  117. tel,
  118. pwd
  119. } = this.ruleForm2;
  120. return {
  121. tel,
  122. pwd
  123. }
  124. }
  125. },
  126. watch: {
  127. nextButton: {
  128. handler: function (val) {
  129. if (val.tel.trim() && val.pwd.trim()) {
  130. this.otherCor = false;
  131. } else {
  132. this.otherCor = true;
  133. }
  134. },
  135. deep: true
  136. }
  137. },
  138. methods: {
  139. CalcuMD5(password){
  140. // password = password.toUpperCase();
  141. password = md5(password);
  142. return password;
  143. },
  144. submitForm: function () {
  145. const username = this.ruleForm2.tel;
  146. const password = this.CalcuMD5(this.ruleForm2.pwd);
  147. let params = {
  148. "username": username,
  149. "password": password
  150. }
  151. //重新登录清空原有token;
  152. localStorage.removeItem('token');
  153. this.$refs.ruleFormReg.validate((valid) => {
  154. if (valid) {
  155. api.loginMess(params).then((res) => {
  156. if (res.status == 200) {
  157. // console.log(res)
  158. if (res.data.code == '10020000') { //未注册
  159. // this.$message.error(res.data.msg);
  160. this.$message.error("该账号或手机号未注册!")
  161. } else if (res.data.code == '10020001') { //密码错误
  162. this.$message.error(res.data.msg);
  163. } else if (res.data.code == '0') {
  164. const token = JSON.stringify(res.data.data);
  165. localStorage.setItem("token", token);
  166. this.$message({
  167. message: '登录成功!',
  168. type: 'success',
  169. duration: 1000,
  170. onClose: function () {
  171. const type = res.data.data.type;
  172. if (type == 0) {
  173. this.$router.push({
  174. path: '/user'
  175. });
  176. } else if (type == 1) {
  177. this.$router.push({
  178. path: '/admin'
  179. });
  180. }
  181. }.bind(this)
  182. });
  183. }
  184. }
  185. }).catch((err) => {
  186. console.log(err);
  187. })
  188. } else {
  189. console.log('faild')
  190. return false;
  191. }
  192. });
  193. },
  194. toRegister() { //点击注册
  195. this.$router.push({
  196. path: '/register'
  197. });
  198. },
  199. toForgetPsw() {//忘记密码
  200. this.$router.push({
  201. path: '/forgetPassword'
  202. });
  203. }
  204. },
  205. components: {
  206. 'banner': banner,
  207. 'copyRightInfo': copyRightInfo
  208. },
  209. }
  210. </script>