Login.vue 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <template>
  2. <div class="login" id="login">
  3. <div class="limiter">
  4. <div class="container-login100">
  5. <div class="wrap-login100">
  6. <span class="login100-form-title p-b-26">{{$t('title')}}</span>
  7. <span class="login100-form-title p-b-48">
  8. <i class="fa fa-video-camera"></i>
  9. </span>
  10. <div class="wrap-input100 validate-input" data-validate = "Valid email is: a@b.c">
  11. <input :class="'input100 ' + (username==''?'':'has-val')" type="text" v-model="username" name="username">
  12. <span class="focus-input100" :data-placeholder="$t('username')"></span>
  13. </div>
  14. <div class="wrap-input100 validate-input" data-validate="Enter password">
  15. <span class="btn-show-pass">
  16. <i :class="'fa ' + (!showPassword?'fa-eye':'fa-eye-slash')" @click="showPassword = !showPassword"></i>
  17. </span>
  18. <input :class="'input100 ' + (password==''?'':'has-val')" :type="(!showPassword?'password':'text')" v-model="password" name="password">
  19. <span class="focus-input100" :data-placeholder="$t('password')"></span>
  20. </div>
  21. <div class="container-login100-form-btn">
  22. <div class="wrap-login100-form-btn" :class="{'login-loading': isLoging}" v-loading="isLoging"
  23. element-loading-background="rgb(0 0 0 / 0%);" element-loading-custom-class="login-loading-class">
  24. <div class="login100-form-bgbtn"></div>
  25. <button class="login100-form-btn" @click="handleClick">
  26. {{ isDefault ? $t("register") : $t('login') }}
  27. </button>
  28. </div>
  29. </div>
  30. <div class="text-center p-t-10">
  31. <el-dropdown class="dropdown-lang mt-2">
  32. <span>
  33. <svg-icon class="text-xl" icon-class="global"></svg-icon>
  34. <span class="ml-1">{{$t('lang')}}</span>
  35. </span>
  36. <el-dropdown-menu slot="dropdown">
  37. <el-dropdown-item
  38. v-for="lang in langList() "
  39. :key="lang.value"
  40. @click.native="changeLang(lang.value)">
  41. {{lang.label}}
  42. </el-dropdown-item>
  43. </el-dropdown-menu>
  44. </el-dropdown>
  45. </div>
  46. <!-- 下拉菜单选择语言-->
  47. </div>
  48. </div>
  49. </div>
  50. </div>
  51. </template>
  52. <script>
  53. import crypto from 'crypto'
  54. import api_user from "@/apiStore/api_user";
  55. import userService from "./service/UserService";
  56. import handle from "@/until/handle";
  57. import querystring from "querystring";
  58. import {langList, loadLanguageAsync} from "../assets/i18/i18n";
  59. export default {
  60. name: 'Login',
  61. data() {
  62. return {
  63. isLoging: false,
  64. showPassword: false,
  65. loginLoading: false,
  66. username: '',
  67. password: '',
  68. isDefault: false,
  69. }
  70. },
  71. created() {
  72. var that = this;
  73. document.onkeydown = function (e) {
  74. var key = window.event.keyCode;
  75. if (key == 13) {
  76. that.handleClick();
  77. }
  78. }
  79. },
  80. beforeMount() {
  81. this.executeIsDefault()
  82. },
  83. methods: {
  84. langList() {
  85. return langList
  86. },
  87. changeLang(lang) {
  88. loadLanguageAsync(lang)
  89. },
  90. async executeIsDefault() {
  91. let url = "/api/user/default"
  92. let [err, res] = await handle(
  93. this.$axios.get(url)
  94. )
  95. if (err) {
  96. this.$message.error(`获取服务器信息失败`);
  97. return;
  98. }
  99. let response = res.data
  100. if (response.code !== 0) {
  101. this.$message.error(`获取服务器信息失败 ${response.msg}`);
  102. return;
  103. }
  104. if (response.data) {
  105. this.$message.success("没有管理员账户, 开始注册管理员账户")
  106. this.$notify({
  107. title: '提示',
  108. message: '没有管理员账户, 请先注册账户',
  109. duration: 0
  110. });
  111. this.isDefault = true;
  112. }
  113. },
  114. //登录请求
  115. async toLogin() {
  116. //需要想后端发送的登录参数
  117. let loginParam = {
  118. username: this.username,
  119. password: crypto.createHash('md5').update(this.password, "utf8").digest('hex')
  120. }
  121. let that = this;
  122. //设置在登录状态
  123. this.isLoging = true;
  124. let timeoutTask = setTimeout(()=>{
  125. this.$message.error("登录超时");
  126. this.isLoging = false;
  127. }, 2000)
  128. let [error,res] = await api_user.login(this,loginParam);
  129. this.isLoging = false;
  130. if(error){
  131. console.log(error)
  132. window.clearTimeout(timeoutTask)
  133. that.$message.error(error.response.data.msg);
  134. return;
  135. }
  136. window.clearTimeout(timeoutTask)
  137. console.log(JSON.stringify(res));
  138. if (res.data.code === 0 ) {
  139. userService.setUser(res.data.data)
  140. //登录成功后
  141. that.cancelEnterkeyDefaultAction();
  142. await that.$router.push('/');
  143. } else {
  144. that.isLoging = false;
  145. that.$message({
  146. showClose: true,
  147. message: '登录失败,用户名或密码错误',
  148. type: 'error'
  149. });
  150. }
  151. },
  152. async executeRegister() {
  153. let url = "/api/user/register"
  154. let loginParam = {
  155. username: this.username,
  156. password: crypto.createHash('md5').update(this.password, "utf8").digest('hex')
  157. }
  158. loginParam = querystring.stringify(loginParam)
  159. let [err, res] = await handle(
  160. this.$axios.post(url, loginParam))
  161. if (err) {
  162. this.$message.error(`注册账户失败`);
  163. console.log(err);
  164. return;
  165. }
  166. let response = res.data
  167. if (response.code !== 0) {
  168. this.$message.error(`注册账户失败 ${response.msg}`);
  169. return;
  170. }
  171. this.$message.success("管理员账户注册成功")
  172. this.isDefault = false;
  173. },
  174. handleClick() {
  175. if (this.username == '' || this.password == '') {
  176. this.$message.warning("请输入账号密码");
  177. return;
  178. }
  179. if (this.isDefault) {
  180. this.executeRegister()
  181. } else {
  182. this.toLogin()
  183. }
  184. },
  185. setCookie: function (cname, cvalue, exdays) {
  186. let d = new Date();
  187. d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
  188. let expires = "expires=" + d.toUTCString();
  189. console.info(cname + "=" + cvalue + "; " + expires);
  190. document.cookie = cname + "=" + cvalue + "; " + expires;
  191. console.info(document.cookie);
  192. },
  193. cancelEnterkeyDefaultAction: function () {
  194. document.onkeydown = function(e) {
  195. let key = window.event.keyCode;
  196. if (key == 13) {
  197. return false;
  198. }
  199. }
  200. }
  201. }
  202. }
  203. </script>