Login.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <template>
  2. <div class="login-wrapper">
  3. <div class="login-box">
  4. <!-- 头像容器 -->
  5. <!-- 表单容器 -->
  6. <div class="login-form-box">
  7. <div class="flex nav">
  8. <div v-for="(item,index) in navList" :class="{'action': index == currentIndex}" class="nav-item hand" @click="currentIndex = index">{{item.tit}}</div>
  9. </div>
  10. <el-form label-width="0px" ref="loginFormRef" class="login-form" :model="loginForm" v-if="currentIndex == 0">
  11. <!--用户名-->
  12. <el-form-item prop="account">
  13. <el-input placeholder="请输入用户名" prefix-icon="el-icon-s-custom"
  14. v-model="loginForm.username"></el-input>
  15. </el-form-item>
  16. <!--密码-->
  17. <el-form-item prop="password">
  18. <el-input placeholder="请输入密码" prefix-icon='el-icon-s-goods' v-model="loginForm.password"
  19. type="password"></el-input>
  20. </el-form-item>
  21. <!--按钮-->
  22. <el-form-item class="btns">
  23. <span class="go-new hand" @click="currentIndex = 1">没有账号?立即注册</span>
  24. <el-button type="primary" @click="login">登录</el-button>
  25. <!-- <el-button type="info" @click="resetLoginForm">重置</el-button> -->
  26. </el-form-item>
  27. </el-form>
  28. <el-form label-width="0px" ref="loginFormReg" class="login-form" :model="loginForm" v-if="currentIndex == 1">
  29. <!--用户名-->
  30. <el-form-item prop="account">
  31. <el-input placeholder="请输入用户名" prefix-icon="el-icon-s-custom"
  32. v-model="loginForm.username" type="text"></el-input>
  33. </el-form-item>
  34. <el-form-item prop="email">
  35. <el-input placeholder="请输入邮箱" prefix-icon="el-icon-message"
  36. v-model="loginForm.email" type="email"></el-input>
  37. </el-form-item>
  38. <!-- 邀请码 -->
  39. <el-form-item prop="email">
  40. <el-input placeholder="请输入邀请码" prefix-icon="el-icon-user"
  41. v-model="loginForm.code" type="text"></el-input>
  42. </el-form-item>
  43. <!--密码-->
  44. <el-form-item prop="password">
  45. <el-input placeholder="请输入密码" prefix-icon='el-icon-s-goods' v-model="loginForm.password"
  46. type="password"></el-input>
  47. </el-form-item>
  48. <el-form-item prop="repassword">
  49. <el-input placeholder="请重新输入密码" prefix-icon='el-icon-s-goods' v-model="loginForm.repassword"
  50. type="password"></el-input>
  51. </el-form-item>
  52. <!--按钮-->
  53. <el-form-item class="btns">
  54. <el-button type="primary" @click="reg">注册</el-button>
  55. <!-- <el-button type="info" @click="resetLoginForm">重置</el-button> -->
  56. </el-form-item>
  57. </el-form>
  58. </div>
  59. </div>
  60. </div>
  61. </template>
  62. <script>
  63. import {
  64. mapState,
  65. mapMutations
  66. } from 'vuex';
  67. import { validateEmail} from '@/assets/js/tools.js'
  68. import {
  69. login,
  70. reg
  71. } from '@/request/agent.js'
  72. export default {
  73. name: "Login",
  74. data() {
  75. return {
  76. currentIndex: 0,
  77. navList: [{
  78. tit: '登录',
  79. state: 0,
  80. }],
  81. loginForm: {
  82. email: '',
  83. code: '',
  84. username: '',
  85. repassword: '',
  86. password: ''
  87. }
  88. }
  89. },
  90. methods: {
  91. ...mapMutations(['setUserInfo']),
  92. resetLoginForm() {
  93. this.$refs.loginFormRef.resetFields()
  94. },
  95. reg() {
  96. let that = this
  97. if (that.loginForm.username == '') {
  98. return that.$msg.error('请输入用户名')
  99. }
  100. if(that.loginForm.email == '') {
  101. return that.$msg.error('请输入邮箱')
  102. }
  103. if(!validateEmail(that.loginForm.email)) {
  104. return that.$msg.error('请输入正确的邮箱')
  105. }
  106. if (that.loginForm.code == '') {
  107. return that.$msg.error('请输入邀请码')
  108. }
  109. if (that.loginForm.password == '') {
  110. return that.$msg.error('请输入密码')
  111. }
  112. if (that.loginForm.repassword == '') {
  113. return that.$msg.error('请再次输入密码')
  114. }
  115. if(that.loginForm.password != that.loginForm.repassword) {
  116. return that.$msg.error('两次密码不一致')
  117. }
  118. reg(that.loginForm).then(res => {
  119. that.$msg.success('注册成功')
  120. that.currentIndex = 0
  121. that.setUserInfo(res.data.userinfo)
  122. window.sessionStorage.setItem('token', res.data.userinfo.token)
  123. window.sessionStorage.setItem('user', res.data.userinfo)
  124. that.$router.push("/home")
  125. }).catch(err => {
  126. console.log();
  127. })
  128. },
  129. login() {
  130. let that = this
  131. if (that.loginForm.account == '') {
  132. return that.$msg.error('请输入用户名')
  133. }
  134. if (that.loginForm.password == '') {
  135. return that.$msg.error('请输入密码')
  136. }
  137. login({
  138. account: that.loginForm.username,
  139. password: that.loginForm.password
  140. }).then(res => {
  141. that.$msg.success('登录成功')
  142. console.log(res);
  143. that.setUserInfo(res.data.userinfo)
  144. window.sessionStorage.setItem('token', res.data.userinfo.token)
  145. that.$router.push("/home")
  146. })
  147. // splist({
  148. // page: 1
  149. // })
  150. // console.log(login);
  151. // this.$router.push("/home")
  152. }
  153. }
  154. };
  155. </script>
  156. <style lang='scss' scoped>
  157. /* body {
  158. } */
  159. .login-wrapper {
  160. /* background-color: ; */
  161. position: absolute;
  162. width: 100%;
  163. height: 100%;
  164. background-image: url(../assets/img/basebg.png) ;
  165. background-size:100% 100%;
  166. }
  167. .login-box {
  168. width: 450px;
  169. border-radius: 3px;
  170. display: flex;
  171. flex-direction: column;
  172. position: absolute;
  173. left: 50%;
  174. top: 0;
  175. bottom: 0;
  176. margin: auto;
  177. align-items: center;
  178. justify-content: center;
  179. }
  180. .avatar-box {
  181. width: 130px;
  182. height: 130px;
  183. border: 1px solid #eee;
  184. border-radius: 50%;
  185. /* position: absolute; */
  186. box-shadow: 0 0 10px #ddd;
  187. align-self: center;
  188. transform: translate(0, -50%);
  189. background-color: #fff;
  190. }
  191. .avatar-box img {
  192. width: 100%;
  193. height: 100%;
  194. border-radius: 50%;
  195. }
  196. .login-form-box {
  197. width: 450px;
  198. /* height: 200px; */
  199. justify-self: end;
  200. transform: translate(0, -10%);
  201. background-color: #fff;
  202. border-radius: 20px;
  203. .login-form {
  204. padding: 20px ;
  205. }
  206. }
  207. .btns {
  208. display: flex;
  209. justify-content: flex-end;
  210. }
  211. .nav {
  212. justify-content: center;
  213. font-size: 16px;
  214. color: #444;
  215. border-radius: 20px 20px 0 0;
  216. overflow: hidden;
  217. }
  218. .nav-item {
  219. width: 100%;
  220. position: relative;
  221. text-align: center;
  222. line-height: 50px;
  223. /* width: 64px;
  224. height: ; */
  225. }
  226. .action {
  227. font-weight: bold;
  228. color: black;
  229. color: #056de8;
  230. /* color: #fff; */
  231. &::after {
  232. content: '';
  233. position: absolute;
  234. width: 100%;
  235. height: 3px;
  236. bottom: 0px;
  237. left: 0;
  238. right: 0;
  239. margin: auto;
  240. }
  241. }
  242. .go-new {
  243. padding-right: 20px;
  244. color: #056de8;
  245. }
  246. </style>