|
|
@@ -20,12 +20,10 @@
|
|
|
</div>
|
|
|
|
|
|
<div class="check">
|
|
|
- <n-checkbox v-model:checked="checked">
|
|
|
- 已阅读同意 <span class="prim" @click.stop="">《用户服务协议》</span>和<span
|
|
|
- class="prim"
|
|
|
- @click.stop=""
|
|
|
- >《平台隐私协》</span
|
|
|
- >
|
|
|
+ <n-checkbox v-model:checked="agree">
|
|
|
+ 已阅读同意
|
|
|
+ <span class="prim" @click.stop="">《用户服务协议》</span>和
|
|
|
+ <span class="prim" @click.stop=""> 《平台隐私协》 </span>
|
|
|
</n-checkbox>
|
|
|
</div>
|
|
|
</n-form>
|
|
|
@@ -42,7 +40,6 @@
|
|
|
<div class="code">
|
|
|
<n-input
|
|
|
class="code-input"
|
|
|
- :input-props="{ type: 'tel' }"
|
|
|
v-model:value="form.code"
|
|
|
maxlength="4"
|
|
|
clearable
|
|
|
@@ -60,146 +57,95 @@
|
|
|
</div>
|
|
|
</template>
|
|
|
|
|
|
-<script>
|
|
|
-import { defineComponent, ref, computed } from 'vue'
|
|
|
+<script setup lang="ts">
|
|
|
+import { ref, computed } from 'vue'
|
|
|
import { NForm, NFormItem, NInput, NButton, NCheckbox, useMessage } from 'naive-ui'
|
|
|
-import { http } from '@/plugins/http'
|
|
|
-import { useUserStore } from '@/store'
|
|
|
-let fromRoute = null
|
|
|
-export default defineComponent({
|
|
|
- components: {
|
|
|
- NForm,
|
|
|
- NFormItem,
|
|
|
- NInput,
|
|
|
- NButton,
|
|
|
- NCheckbox
|
|
|
- },
|
|
|
- data() {
|
|
|
- return {
|
|
|
- rules: {
|
|
|
- phone: [
|
|
|
- {
|
|
|
- validator(rule, value) {
|
|
|
- if (!value) {
|
|
|
- return new Error('请输入手机号')
|
|
|
- } else if (!/^[1][3,4,5,6,7,8,9][0-9]{9}$/.test(value)) {
|
|
|
- return new Error('手机号格式错误')
|
|
|
- }
|
|
|
+import { fetchSendVerify } from '@/api'
|
|
|
+import { useUserStore, useAuthStore } from '@/store'
|
|
|
+import { useRouter } from 'vue-router'
|
|
|
|
|
|
- return true
|
|
|
- },
|
|
|
- trigger: ['blur']
|
|
|
- }
|
|
|
- ]
|
|
|
- }
|
|
|
- }
|
|
|
- },
|
|
|
- setup() {
|
|
|
- const form = ref({
|
|
|
- phone: '',
|
|
|
- code: ''
|
|
|
- })
|
|
|
- const step = ref(0)
|
|
|
- const checked = ref(false)
|
|
|
- const message = useMessage()
|
|
|
- const formRef = ref(null)
|
|
|
- const loading = ref(false)
|
|
|
- function send(e) {
|
|
|
- e.preventDefault()
|
|
|
- loading.value = true
|
|
|
- formRef.value
|
|
|
- ?.validate(errors => {
|
|
|
- if (!errors) {
|
|
|
- if (!checked.value) {
|
|
|
- message.error('请先阅读并同意协议')
|
|
|
- } else {
|
|
|
- http.post(
|
|
|
- '/api/sms/sendVerify',
|
|
|
- {
|
|
|
- phone: form.value.phone
|
|
|
- },
|
|
|
- {
|
|
|
- body: 'json'
|
|
|
- }
|
|
|
- )
|
|
|
- .then(res => {
|
|
|
- loading.value = false
|
|
|
- step.value = 1
|
|
|
- form.value.msgCode = res
|
|
|
- })
|
|
|
- .catch(e => {
|
|
|
- loading.value = false
|
|
|
- message.error(e.error)
|
|
|
- })
|
|
|
- }
|
|
|
- }
|
|
|
- })
|
|
|
- .catch(() => {
|
|
|
- loading.value = false
|
|
|
- })
|
|
|
- }
|
|
|
+const router = useRouter()
|
|
|
+const userStore = useUserStore()
|
|
|
+const authStore = useAuthStore()
|
|
|
|
|
|
- const showPhone = computed(() => {
|
|
|
- return form.value.phone.substr(0, 3) + '****' + form.value.phone.substr(-4)
|
|
|
- })
|
|
|
+const form = ref({
|
|
|
+ phone: '',
|
|
|
+ code: ''
|
|
|
+})
|
|
|
+const step = ref(0)
|
|
|
+const agree = ref(false)
|
|
|
+const message = useMessage()
|
|
|
+const formRef: any = ref(null)
|
|
|
+const loading = ref(false)
|
|
|
+const rules = {
|
|
|
+ phone: [
|
|
|
+ {
|
|
|
+ validator(rule: any, value: any) {
|
|
|
+ if (!value) {
|
|
|
+ return new Error('请输入手机号')
|
|
|
+ } else if (!/^1[3-9]\d{9}$/.test(value)) {
|
|
|
+ return new Error('手机号格式错误')
|
|
|
+ }
|
|
|
|
|
|
- const codeStr = computed(() => {
|
|
|
- let str = ''
|
|
|
- for (let i = 0; i < 4; i++) {
|
|
|
- str += '<span>' + (form.value.code.length > i ? form.value.code[i] : '') + '</span>'
|
|
|
- }
|
|
|
- return str
|
|
|
+ return true
|
|
|
+ },
|
|
|
+ trigger: ['blur']
|
|
|
+ }
|
|
|
+ ]
|
|
|
+}
|
|
|
+async function send() {
|
|
|
+ if (!agree.value) {
|
|
|
+ message.error('请先阅读并同意协议')
|
|
|
+ return
|
|
|
+ }
|
|
|
+ try {
|
|
|
+ loading.value = true
|
|
|
+ await formRef.value.validate().catch((e: any) => {
|
|
|
+ throw 'validate error'
|
|
|
})
|
|
|
+ await fetchSendVerify(form.value.phone)
|
|
|
+ loading.value = false
|
|
|
+ step.value = 1
|
|
|
+ } catch (e: any) {
|
|
|
+ loading.value = false
|
|
|
+ if (e === 'validate error') return
|
|
|
+ message.error(e.message)
|
|
|
+ }
|
|
|
+}
|
|
|
|
|
|
- const userStore = useUserStore()
|
|
|
+const showPhone = computed(() => {
|
|
|
+ return form.value.phone.substr(0, 3) + '****' + form.value.phone.substr(-4)
|
|
|
+})
|
|
|
|
|
|
- return {
|
|
|
- send,
|
|
|
- form,
|
|
|
- formRef,
|
|
|
- message,
|
|
|
- checked,
|
|
|
- step,
|
|
|
- showPhone,
|
|
|
- codeStr,
|
|
|
- userStore,
|
|
|
- loading
|
|
|
- }
|
|
|
- },
|
|
|
- beforeRouteEnter(to, from) {
|
|
|
- fromRoute = from
|
|
|
- },
|
|
|
- methods: {
|
|
|
- onCodeInput(e) {
|
|
|
- if (e.length === 4) {
|
|
|
- this.loading = true
|
|
|
- this.userStore
|
|
|
- .loginByCode(this.form.phone, this.form.code)
|
|
|
- .then(() => {
|
|
|
- this.loading = false
|
|
|
- this.message.success('登录成功')
|
|
|
- this.goBack()
|
|
|
- })
|
|
|
- .catch(e => {
|
|
|
- this.loading = false
|
|
|
- console.log(e)
|
|
|
- if (e && e.message) {
|
|
|
- this.message.error(e.message)
|
|
|
- this.step = 0
|
|
|
- this.form.code = ''
|
|
|
- }
|
|
|
- })
|
|
|
- }
|
|
|
- },
|
|
|
- goBack() {
|
|
|
- if (!fromRoute || !fromRoute.name || fromRoute.name === 'userRegister' || fromRoute.name === 'userLogin') {
|
|
|
- this.$router.replace('/')
|
|
|
- } else {
|
|
|
- this.$router.back()
|
|
|
- }
|
|
|
- }
|
|
|
+const codeStr = computed(() => {
|
|
|
+ let str = ''
|
|
|
+ for (let i = 0; i < 4; i++) {
|
|
|
+ str += '<span>' + (form.value.code.length > i ? form.value.code[i] : '') + '</span>'
|
|
|
}
|
|
|
+ return str
|
|
|
})
|
|
|
+
|
|
|
+async function onCodeInput(e: any) {
|
|
|
+ if (e.length === 4) {
|
|
|
+ try {
|
|
|
+ loading.value = true
|
|
|
+ await authStore.phoneLogin(form.value.phone, form.value.code)
|
|
|
+ message.success('登录成功')
|
|
|
+ router.replace({ name: 'Chat' })
|
|
|
+ } catch (e: any) {
|
|
|
+ console.log(e)
|
|
|
+ loading.value = false
|
|
|
+ message.error(e.message)
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+function goBack() {
|
|
|
+ if (step.value === 1) {
|
|
|
+ step.value = 0
|
|
|
+ } else {
|
|
|
+ router.back()
|
|
|
+ }
|
|
|
+}
|
|
|
</script>
|
|
|
|
|
|
<style lang="less" scoped>
|
|
|
@@ -315,4 +261,4 @@ export default defineComponent({
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
-</style>
|
|
|
+</style>
|