Login.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <template>
  2. <div class="hello">
  3. <Header></Header>
  4. <el-container>
  5. <el-card class="center-card">
  6. <el-form
  7. status-icon
  8. label-width="0px"
  9. class="demo-ruleForm"
  10. @keyup.enter.native="onSubmit"
  11. >
  12. <h2>{{ $t('login') }}</h2>
  13. <el-form-item label>
  14. <el-input
  15. type="text"
  16. auto-complete="off"
  17. :placeholder="$t('username_description')"
  18. v-model="username"
  19. ></el-input>
  20. </el-form-item>
  21. <el-form-item label>
  22. <el-input
  23. type="password"
  24. auto-complete="off"
  25. v-model="password"
  26. :placeholder="$t('password')"
  27. ></el-input>
  28. </el-form-item>
  29. <el-form-item label v-if="show_v_code">
  30. <el-input
  31. type="text"
  32. auto-complete="off"
  33. v-model="v_code"
  34. :placeholder="$t('verification_code')"
  35. ></el-input>
  36. <img
  37. v-bind:src="v_code_img"
  38. class="v_code_img"
  39. v-on:click="change_v_code_img"
  40. />
  41. </el-form-item>
  42. <el-form-item label>
  43. <el-button type="primary" style="width:100%;" @click="onSubmit">{{
  44. $t('login')
  45. }}</el-button>
  46. </el-form-item>
  47. <el-form-item label>
  48. <router-link to="/user/register">{{
  49. $t('register_new_account')
  50. }}</router-link
  51. >&nbsp;&nbsp;&nbsp;
  52. </el-form-item>
  53. </el-form>
  54. </el-card>
  55. </el-container>
  56. <Footer></Footer>
  57. </div>
  58. </template>
  59. <script>
  60. export default {
  61. name: 'Login',
  62. components: {},
  63. data() {
  64. return {
  65. username: '',
  66. password: '',
  67. v_code: '',
  68. v_code_img: DocConfig.server + '/api/common/verify',
  69. show_v_code: false,
  70. is_show_alert: false
  71. }
  72. },
  73. methods: {
  74. onSubmit() {
  75. if (this.is_show_alert) {
  76. return
  77. }
  78. // this.$message.success(this.username);
  79. var that = this
  80. var url = DocConfig.server + '/api/user/login'
  81. var params = new URLSearchParams()
  82. params.append('username', this.username)
  83. params.append('password', this.password)
  84. params.append('v_code', this.v_code)
  85. that.axios.post(url, params).then(function(response) {
  86. if (response.data.error_code === 0) {
  87. // that.$message.success("登录成功");
  88. localStorage.setItem('userinfo', JSON.stringify(response.data.data))
  89. let redirect = decodeURIComponent(
  90. that.$route.query.redirect || '/item/index'
  91. )
  92. that.$router.replace({
  93. path: redirect
  94. })
  95. } else {
  96. if (
  97. response.data.error_code === 10206 ||
  98. response.data.error_code === 10210
  99. ) {
  100. that.show_v_code = true
  101. that.change_v_code_img()
  102. }
  103. that.is_show_alert = true
  104. that.$alert(response.data.error_message, {
  105. callback: function() {
  106. setTimeout(function() {
  107. that.is_show_alert = false
  108. }, 500)
  109. }
  110. })
  111. }
  112. })
  113. },
  114. change_v_code_img() {
  115. var rand = '&rand=' + Math.random()
  116. this.v_code_img += rand
  117. },
  118. script_cron() {
  119. var url = DocConfig.server + '/api/ScriptCron/run'
  120. this.axios.get(url)
  121. },
  122. checkDb() {
  123. var url = DocConfig.server + '/api/update/checkDb'
  124. this.axios.get(url)
  125. }
  126. },
  127. mounted() {
  128. var that = this
  129. this.get_user_info(function(response) {
  130. if (response.data.error_code === 0) {
  131. let redirect = decodeURIComponent(
  132. that.$route.query.redirect || '/item/index'
  133. )
  134. that.$router.replace({
  135. path: redirect
  136. })
  137. }
  138. })
  139. this.script_cron()
  140. this.checkDb()
  141. },
  142. watch: {
  143. $route(to, from) {
  144. this.$router.go(0)
  145. }
  146. },
  147. beforeDestroy() {}
  148. }
  149. </script>
  150. <!-- Add "scoped" attribute to limit CSS to this component only -->
  151. <style scoped>
  152. .center-card a {
  153. font-size: 12px;
  154. }
  155. .center-card {
  156. text-align: center;
  157. }
  158. .v_code_img {
  159. margin-top: 20px;
  160. }
  161. </style>