Login.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. <a :href="oauth2_url">{{ oauth2_entrance_tips }}</a>
  53. </el-form-item>
  54. </el-form>
  55. </el-card>
  56. </el-container>
  57. <Footer></Footer>
  58. </div>
  59. </template>
  60. <script>
  61. export default {
  62. name: 'Login',
  63. components: {},
  64. data() {
  65. return {
  66. username: '',
  67. password: '',
  68. v_code: '',
  69. v_code_img: DocConfig.server + '/api/common/verify',
  70. show_v_code: false,
  71. is_show_alert: false,
  72. oauth2_entrance_tips: '',
  73. oauth2_url: DocConfig.server + '/api/ExtLogin/oauth2'
  74. }
  75. },
  76. methods: {
  77. onSubmit() {
  78. if (this.is_show_alert) {
  79. return
  80. }
  81. // this.$message.success(this.username);
  82. var that = this
  83. var url = DocConfig.server + '/api/user/login'
  84. var params = new URLSearchParams()
  85. params.append('username', this.username)
  86. params.append('password', this.password)
  87. params.append('v_code', this.v_code)
  88. that.axios.post(url, params).then(function(response) {
  89. if (response.data.error_code === 0) {
  90. // that.$message.success("登录成功");
  91. localStorage.setItem('userinfo', JSON.stringify(response.data.data))
  92. let redirect = decodeURIComponent(
  93. that.$route.query.redirect || '/item/index'
  94. )
  95. that.$router.replace({
  96. path: redirect
  97. })
  98. } else {
  99. if (
  100. response.data.error_code === 10206 ||
  101. response.data.error_code === 10210
  102. ) {
  103. that.show_v_code = true
  104. that.change_v_code_img()
  105. }
  106. that.is_show_alert = true
  107. that.$alert(response.data.error_message, {
  108. callback: function() {
  109. setTimeout(function() {
  110. that.is_show_alert = false
  111. }, 500)
  112. }
  113. })
  114. }
  115. })
  116. },
  117. change_v_code_img() {
  118. var rand = '&rand=' + Math.random()
  119. this.v_code_img += rand
  120. },
  121. script_cron() {
  122. var url = DocConfig.server + '/api/ScriptCron/run'
  123. this.axios.get(url)
  124. },
  125. checkDb() {
  126. var url = DocConfig.server + '/api/update/checkDb'
  127. this.axios.get(url)
  128. },
  129. getOauth() {
  130. var url = DocConfig.server + '/api/user/oauthInfo'
  131. this.axios.get(url).then(response => {
  132. if (response.data.error_code === 0) {
  133. if (response.data.data.oauth2_open > 0) {
  134. this.oauth2_entrance_tips = response.data.data.oauth2_entrance_tips
  135. }
  136. }
  137. })
  138. }
  139. },
  140. mounted() {
  141. var that = this
  142. this.get_user_info(function(response) {
  143. if (response.data.error_code === 0) {
  144. let redirect = decodeURIComponent(
  145. that.$route.query.redirect || '/item/index'
  146. )
  147. that.$router.replace({
  148. path: redirect
  149. })
  150. }
  151. })
  152. this.script_cron()
  153. this.checkDb()
  154. this.getOauth()
  155. },
  156. watch: {
  157. $route(to, from) {
  158. this.$router.go(0)
  159. }
  160. },
  161. beforeDestroy() {}
  162. }
  163. </script>
  164. <!-- Add "scoped" attribute to limit CSS to this component only -->
  165. <style scoped>
  166. .center-card a {
  167. font-size: 12px;
  168. }
  169. .center-card {
  170. text-align: center;
  171. }
  172. .v_code_img {
  173. margin-top: 20px;
  174. }
  175. </style>