Login.vue 7.0 KB

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