Login.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <template>
  2. <div class="container">
  3. <div style="width: 400px; padding: 30px; background-color: white; border-radius: 5px;">
  4. <div style="text-align: center; font-size: 20px; margin-bottom: 20px; color: #333">欢迎登陆</div>
  5. <el-form :model="form" :rules="rules" ref="formRef">
  6. <el-form-item prop="username">
  7. <el-input prefix-icon="el-icon-user" placeholder="请输入账号" v-model="form.username"></el-input>
  8. </el-form-item>
  9. <el-form-item prop="password">
  10. <el-input prefix-icon="el-icon-lock" placeholder="请输入密码" show-password v-model="form.password"></el-input>
  11. </el-form-item>
  12. <el-form-item>
  13. <el-select prop= "role" v-model="form.role" placeholder="请选择权限" >
  14. <el-option
  15. v-for="item in roleOptions.options"
  16. :key="item.value"
  17. :label="item.label"
  18. :value="item.value">
  19. </el-option>
  20. </el-select>
  21. </el-form-item>
  22. <el-form-item>
  23. <el-button style="width: 100%; background-color: #333; border-color: #333; color: white" @click="login">登 录</el-button>
  24. </el-form-item>
  25. <div style="display: flex; align-items: center">
  26. <div style="flex: 1"></div>
  27. <div style="flex: 1; text-align: right">
  28. <!-- <el-link @click="wxLogin()"> 微信登录 </el-link> -->
  29. 注册店家: <a href="/register">注册</a>
  30. </div>
  31. </div>
  32. </el-form>
  33. </div>
  34. </div>
  35. </template>
  36. <script>
  37. export default {
  38. name: "Login",
  39. data() {
  40. return {
  41. form: { role: 'STOREKEEPER' },
  42. rules: {
  43. username: [
  44. { required: true, message: '请输入账号', trigger: 'blur' },
  45. ],
  46. password: [
  47. { required: true, message: '请输入密码', trigger: 'blur' },
  48. ]
  49. },
  50. roleOptions:{
  51. options:[
  52. {
  53. value:"STOREKEEPER",
  54. label:"店铺管理员"
  55. },
  56. {
  57. value:"ADMIN",
  58. label:"超级管理员"
  59. }
  60. ]
  61. }
  62. }
  63. },
  64. created() {
  65. },
  66. methods: {
  67. login() {
  68. // 登陆校验
  69. this.$refs['formRef'].validate((valid) => {
  70. if (valid) {
  71. // 验证通过
  72. this.$request.post('/login', this.form).then(res => {
  73. if (res.code === '200') {
  74. localStorage.setItem("xm-user", JSON.stringify(res.data)) // 存储用户数据
  75. console.log("yes")
  76. if(res.data.role == 'STOREKEEPER')
  77. this.$router.push('/') // 跳转主页
  78. else if(res.data.role== 'ADMIN')
  79. this.$router.push('/adminMgr')
  80. this.$message.success('登录成功')
  81. } else {
  82. this.$message.error(res.msg)
  83. }
  84. })
  85. }
  86. })
  87. },
  88. // 微信登陆接口
  89. wxLogin(){
  90. this.$request({
  91. method: 'get',
  92. url: '/wxLogin',
  93. responseType: 'blob' // 设置响应类型为blob,以接收二进制数据
  94. })
  95. .then(response => {
  96. // 创建URL对象,并将Blob对象转换为图片URL
  97. const imgUrl = URL.createObjectURL(response);
  98. // 打开一个新的浏览器窗口,并展示二维码图片
  99. const newWindow = window.open('', '微信登陆','width=400,height=400');
  100. newWindow.document.write(`<div class="container" style="display: flex;
  101. flex-direction: column;
  102. align-items: center;
  103. justify-content: center;
  104. height: 100vh;
  105. ">
  106. <h1 style="font-size: 36px; margin-bottom: 20px;">微信扫码登录</h1>
  107. <div>
  108. <img src="${imgUrl}" class="qrcode" alt="QR Code" style="width: 200px;height: auto;margin-top: 20px;">
  109. </div>
  110. </div>
  111. `);
  112. })
  113. .catch(error => {
  114. console.error('Error fetching QR code:', error);
  115. });
  116. }
  117. }
  118. }
  119. </script>
  120. <style scoped>
  121. .container {
  122. height: 100vh;
  123. overflow: hidden;
  124. background-image: url("@/assets/imgs/bg.jpg");
  125. background-size: 100%;
  126. display: flex;
  127. align-items: center;
  128. justify-content: center;
  129. color: #666;
  130. }
  131. a {
  132. color: #2a60c9;
  133. }
  134. </style>