|
@@ -1,39 +1,126 @@
|
|
|
<template>
|
|
|
- <div>
|
|
|
- <van-form @submit="onSubmit">
|
|
|
- <van-cell-group inset>
|
|
|
- <van-field
|
|
|
- v-model="username"
|
|
|
- name="用户名"
|
|
|
- label="用户名"
|
|
|
- placeholder="用户名"
|
|
|
- :rules="[{ required: true, message: '请填写用户名' }]"
|
|
|
- />
|
|
|
- <van-field
|
|
|
- v-model="password"
|
|
|
- type="password"
|
|
|
- name="密码"
|
|
|
- label="密码"
|
|
|
- placeholder="密码"
|
|
|
- :rules="[{ required: true, message: '请填写密码' }]"
|
|
|
- />
|
|
|
- </van-cell-group>
|
|
|
- <div style="margin: 16px">
|
|
|
- <van-button round block type="primary" native-type="submit">
|
|
|
- 提交
|
|
|
- </van-button>
|
|
|
- </div>
|
|
|
- </van-form>
|
|
|
- </div>
|
|
|
- </template>
|
|
|
-
|
|
|
- <script setup>
|
|
|
- import { ref } from 'vue'
|
|
|
- const password = ref('')
|
|
|
- const username = ref('')
|
|
|
-
|
|
|
- function onSubmit() {
|
|
|
+ <div class="login-box">
|
|
|
+ <van-form @submit="onSubmit" v-if="!isRegister">
|
|
|
+ <van-cell-group inset>
|
|
|
+ <van-field
|
|
|
+ v-model="account"
|
|
|
+ name="account"
|
|
|
+ label="用户名"
|
|
|
+ placeholder="用户名"
|
|
|
+ :rules="[{ required: true, message: '请填写用户名' }]"
|
|
|
+ />
|
|
|
+ <van-field
|
|
|
+ v-model="password"
|
|
|
+ type="password"
|
|
|
+ name="password"
|
|
|
+ label="密码"
|
|
|
+ placeholder="密码"
|
|
|
+ :rules="[{ required: true, message: '请填写密码' }]"
|
|
|
+ />
|
|
|
+ </van-cell-group>
|
|
|
+ <div style="margin: 16px">
|
|
|
+ <van-button round block type="primary" native-type="submit">
|
|
|
+ 提交
|
|
|
+ </van-button>
|
|
|
+ <div class="register-btn" @click="isRegister = true">注册</div>
|
|
|
+ </div>
|
|
|
+ </van-form>
|
|
|
|
|
|
+ <van-form @submit="onRegisterSubmit" v-else>
|
|
|
+ <van-cell-group inset>
|
|
|
+ <van-field
|
|
|
+ v-model="account"
|
|
|
+ name="account"
|
|
|
+ label="账号"
|
|
|
+ placeholder="账号"
|
|
|
+ :rules="[{ required: true, message: '请填写账号' }]"
|
|
|
+ /> <van-field
|
|
|
+ v-model="name"
|
|
|
+ name="name"
|
|
|
+ label="用户名"
|
|
|
+ placeholder="用户名"
|
|
|
+ :rules="[{ required: true, message: '请填写用户名' }]"
|
|
|
+ />
|
|
|
+ <van-field
|
|
|
+ v-model="password"
|
|
|
+ type="password"
|
|
|
+ name="password"
|
|
|
+ label="密码"
|
|
|
+ placeholder="密码"
|
|
|
+ :rules="[{ required: true, message: '请填写密码' }]"
|
|
|
+ />
|
|
|
+ <van-field
|
|
|
+ v-model="againPassword"
|
|
|
+ type="password"
|
|
|
+ name="againPassword"
|
|
|
+ label="密码"
|
|
|
+ placeholder="密码"
|
|
|
+ :rules="[{ required: true, message: '请再次填写密码' }]"
|
|
|
+ />
|
|
|
+ </van-cell-group>
|
|
|
+ <div style="margin: 16px">
|
|
|
+ <van-button round block type="primary" native-type="submit">
|
|
|
+ 提交
|
|
|
+ </van-button>
|
|
|
+ <div class="register-btn" @click="isRegister = false">去登录</div>
|
|
|
+ </div>
|
|
|
+ </van-form>
|
|
|
+ </div>
|
|
|
+</template>
|
|
|
+
|
|
|
+<script setup>
|
|
|
+import { ref } from 'vue'
|
|
|
+import { register, login } from '@/api/base'
|
|
|
+import { showSuccessToast, showFailToast } from 'vant'
|
|
|
+import { useRouter, useRoute } from 'vue-router'
|
|
|
+
|
|
|
+const password = ref('')
|
|
|
+const againPassword = ref('')
|
|
|
+const account = ref('')
|
|
|
+const name = ref('')
|
|
|
+const isRegister = ref(false)
|
|
|
+const router = useRouter()
|
|
|
+
|
|
|
+async function onSubmit (params) {
|
|
|
+ const res = await login(params)
|
|
|
+ if (res.code === 200) {
|
|
|
+ showSuccessToast('登录成功');
|
|
|
+ window.localStorage.setItem('token', res.data.token)
|
|
|
+ } else {
|
|
|
+ showFailToast('登录失败')
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+async function onRegisterSubmit (params) {
|
|
|
+ if(params.password !== params.againPassword) {
|
|
|
+ showFailToast('2次输入的密码不一样')
|
|
|
+ return
|
|
|
+ }
|
|
|
+ const res = await register({
|
|
|
+ ...params,
|
|
|
+ account_type: params.password.indexOf('@') > -1 ? 2 : 1
|
|
|
+ })
|
|
|
+ if (res.code === 200) {
|
|
|
+ showSuccessToast('注册成功');
|
|
|
+ console.log(104, res);
|
|
|
+ window.localStorage.setItem('token', res.data.token)
|
|
|
+ router.push('/')
|
|
|
+ } else {
|
|
|
+ showFailToast('注册失败')
|
|
|
}
|
|
|
+}
|
|
|
+</script>
|
|
|
+
|
|
|
+<style scoped lang="scss">
|
|
|
+
|
|
|
+.login-box {
|
|
|
+ padding-top: 20px;
|
|
|
+}
|
|
|
|
|
|
- </script>
|
|
|
+.register-btn {
|
|
|
+ margin-top: 12px;
|
|
|
+ color: var(--van-button-primary-background);
|
|
|
+ font-size: 14px;
|
|
|
+ font-weight: bold;
|
|
|
+}
|
|
|
+</style>
|