Index.vue 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <template>
  2. <div style="height: 100vh; overflow: hidden; display: flex; flex-direction: column;">
  3. <!-- 用户名称 -->
  4. <div style="background-image: url('static/images/bg-login.png');
  5. background-repeat: no-repeat;
  6. background-position-y: 0px;
  7. background-size: 100%">
  8. <div style="padding: 100px 16px 30px 32px; display: flex;
  9. justify-content:space-between;
  10. align-items:center;">
  11. <div>
  12. <div style="color: #5C677C; height: 30px;">
  13. <van-tag type="primary" size="large">欢迎登录</van-tag>
  14. </div>
  15. <div style="font-size: 1.1rem; font-weight: bold;">
  16. <span>驼人物联网平台</span>
  17. </div>
  18. </div>
  19. </div>
  20. <div style="margin-top: 60px;">
  21. <van-form ref="formRef" @submit="onSubmit">
  22. <van-cell-group inset>
  23. <van-field
  24. v-model="username"
  25. name="username"
  26. label="账号"
  27. label-width="70"
  28. placeholder="请输入账号"
  29. :rules="[{ required: true, message: '请填写账号' }]"
  30. />
  31. <van-field
  32. v-model="password"
  33. name="password"
  34. center
  35. clearable
  36. label="密码"
  37. label-width="70"
  38. type="password"
  39. placeholder="请输入密码"
  40. :rules="[{ required: true, message: '请输入密码' }]"
  41. >
  42. </van-field>
  43. <van-field style="align-items: center;"
  44. v-model="captchaCode"
  45. name="captchaCode"
  46. label="验证码"
  47. label-width="70"
  48. placeholder="请输入验证码"
  49. :rules="[{ required: true, message: '请填写账号' }]">
  50. <template #button>
  51. <van-image
  52. width="100"
  53. height="30"
  54. fit="contain"
  55. :src="captchaSrc"
  56. @click="onClickVerifyCode"
  57. />
  58. </template>
  59. </van-field>
  60. <div style="padding: 16px;">
  61. <van-checkbox v-model="rememberPasswordChecked" shape="square">记住密码</van-checkbox>
  62. </div>
  63. </van-cell-group>
  64. <div style="margin: 16px;">
  65. <van-button
  66. round block type="primary" size="large"
  67. native-type="submit">登录</van-button>
  68. </div>
  69. <div style="padding: 0px 16px; display: flex; justify-content: space-between;">
  70. <div>
  71. <a @click="onFindpassword">忘记密码?</a>
  72. </div>
  73. <div>
  74. <a @click="onRegister">注册账号</a>
  75. </div>
  76. </div>
  77. </van-form>
  78. </div>
  79. </div>
  80. </div>
  81. </template>
  82. <script setup lang="ts">
  83. import { onMounted, ref } from 'vue'
  84. import { useLocalStore } from '@/store';
  85. import { useRouter } from 'vue-router'
  86. import { post, post_promise } from '@/network/axios';
  87. import type { FormInstance } from 'vant';
  88. // 路由
  89. const router = useRouter();
  90. const localStore = useLocalStore();
  91. // 表单实例
  92. const formRef = ref<FormInstance>();
  93. // 用户名和验证码
  94. const username = ref('')
  95. const password = ref('')
  96. const captchaCode = ref('')
  97. const captchaKey = ref('')
  98. const rememberPasswordChecked = ref(true);
  99. // 验证吗
  100. const captchaSrc = ref<string>('');
  101. const refreshVerifycode = () => {
  102. post({url: '/open/getCaptcha'}, (result: any) => {
  103. console.log(result.data.captchaImg)
  104. captchaSrc.value = result.data.captchaImg
  105. captchaKey.value = result.data.captchaKey
  106. });
  107. }
  108. // 点击了验证码
  109. const onClickVerifyCode = (event: any) => {
  110. console.log(event)
  111. refreshVerifycode();
  112. }
  113. // 点击了注册
  114. const onRegister = () => {
  115. // 跳转到注册
  116. router.replace('/register');
  117. }
  118. // 点击了忘记密码
  119. const onFindpassword = () => {
  120. // 跳转到注册
  121. router.replace('/findpassword');
  122. }
  123. // 生命周期
  124. onMounted(() => {
  125. console.log('onMounted');
  126. refreshVerifycode();
  127. rememberPasswordChecked.value = localStore.$state.rememberPassword;
  128. if(rememberPasswordChecked.value){
  129. username.value = localStore.$state.username
  130. password.value = localStore.$state.password
  131. }
  132. })
  133. // 登录提交
  134. const onSubmit = () => {
  135. formRef.value?.validate().then(() => {
  136. // 记住密码
  137. localStore.$patch({
  138. rememberPassword: rememberPasswordChecked.value
  139. })
  140. // 记住账号密码
  141. if(rememberPasswordChecked.value){
  142. localStore.$patch({
  143. username: username.value,
  144. password: password.value
  145. })
  146. }
  147. // 登录
  148. post_promise({url: '/user/login', data: {
  149. username: username.value,
  150. password: password.value,
  151. }})
  152. .then(({data}) => {
  153. console.log(data)
  154. if(data.code == 0){
  155. // 存储token
  156. localStore.$patch((state) => {
  157. state.token = data.data.token
  158. })
  159. // 跳转到首页
  160. router.push('/')
  161. }
  162. })
  163. }).catch((ex) => {
  164. console.log(ex)
  165. })
  166. }
  167. </script>
  168. <style scoped>
  169. .van-tabbar--fixed{
  170. left: auto;
  171. max-width: 475px;
  172. }
  173. </style>