user.vue
4.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<!-- 账号登录页 -->
<template>
<view class="logo">地利快递员</view>
<view class="loginBox">
<view class="tit">
<text>账号登录</text>
<!-- 一期不做,暂时隐藏 -->
<!-- <text class="text" @click="goLogin">
手机号登录
<icon></icon>
</text> -->
</view>
<!-- 登录表单 -->
<view class="loginMain">
<uni-forms ref="customForm" :rules="customRules" :modelValue="fromInfo">
<uni-forms-item name="account"><uni-easyinput class="item" v-model="fromInfo.account" placeholder="请输入账号" /></uni-forms-item>
<uni-forms-item name="password" class="pwdBox">
<uni-easyinput class="item" type="password" v-model="fromInfo.password" placeholder="请输入密码" />
<text class="clearIcon" v-if="fromInfo.password.length > 0" @click="handlePwd"></text>
</uni-forms-item>
</uni-forms>
<!-- 按钮 -->
<view class="btnBox">
<button
class="btn-default"
:disabled="fromInfo.account.length === 0 || fromInfo.password.length === 0"
:class="fromInfo.account.length === 0 || fromInfo.password.length === 0 ? 'disabled' : ''"
type="primary"
@click="handleSubmit"
>
登录
</button>
</view>
<!-- end -->
<!-- 更新请求Url - 教学需求 -->
<view class="setUrl" @click="inputDialogToggle">配置请求url</view>
<uni-popup ref="inputDialog" type="dialog">
<uni-popup-dialog ref="inputClose" mode="input" title="配置URL" :value="baseURL" placeholder="请输入baseURL" @confirm="dialogInputConfirm"></uni-popup-dialog>
</uni-popup>
<!-- 更新请求Url - 教学需求 -->
</view>
<!-- end -->
</view>
</template>
<script setup>
import { ref, reactive, onMounted } from 'vue';
import { useStore } from 'vuex';
import storage from '@/utils/storage.js';
// 接口
import { userLogins } from '../api/user.js';
// 导入接口
import { getHomeInfo } from '@/pages/api/index.js';
import { encrypt, decrypt } from '@/utils/crypto.js'
// ------定义变量------
const store = useStore(); //vuex获取储存数据
let showPassword = ref(false); //控制密码右侧图标显示隐藏
const customForm = ref(); //表单校验
// 表单数据
let fromInfo = reactive({
account: '', //账号
password: '' // 密码
});
// 表单校验
const customRules = reactive({
account: {
rules: [
{
required: true,
errorMessage: '请输入手机号'
}
]
},
password: {
rules: [
{
required: true,
errorMessage: '请输入验证码'
}
]
}
});
// ------声明周期------
onMounted(() => {
// 进入登录页面配置默认的请求url
// uni.setStorageSync('baseUrl', 'http://slwl-geteway-t.itheima.net/courier');
// 处理定时上报位置的定时器
if(uni.getStorageSync('positions')?.timer) {
clearInterval(uni.getStorageSync('positions').timer);
uni.setStorageSync('positions', null);
}
const accountHistory = uni.getStorageSync('accountHistory')
if(accountHistory) {
fromInfo.account = accountHistory.account
fromInfo.password = decrypt(accountHistory.password)
// console.log(accountHistory)
}
});
// ------定义方法------
// 登录
const handleSubmit = async () => {
// // 表单校验
const valid = await customForm.value.validate();
if (valid) {
// 登录接口
// 网络慢的时候添加按钮loading
let times =
setTimeout(()=>{
uni.showLoading({
title: 'loading',
});
},500)
await userLogins(fromInfo)
.then(async res => {
if (res.code === 200) {
// 操作成功后清除loading
setTimeout(function () {
uni.hideLoading();
}, 500);
clearTimeout(times)
// 存储token
uni.setStorageSync('token', res.data.token);
store.commit('user/setToken', res.data.token);
//存储用户账号和密码,密码加密存储
const data = { account: fromInfo.account, password: encrypt(fromInfo.password) }
uni.setStorageSync('accountHistory', data)
// // 通过vuex获取用户信息
store.dispatch('user/GetUsersInfo');
await getHomeInfo().then(res => {
if (res.code === 200) {
// 跳转到首页
uni.redirectTo({
url: '/pages/index/index'
});
}
});
} else {
uni.showToast({
title: res.msg,
duration: 1000,
icon: 'none'
});
}
})
.catch(err => {
// uni.showToast({
// title: err.msg,
// duration: 15000,
// icon: 'none'
// });
});
}
};
// 去手机登录页面
const goLogin = () => {
uni.redirectTo({
url: '/pages/login/index'
});
};
// 设置密码
const handlePwd = () => {
fromInfo.password = '';
};
// 打开设置Url窗口
const baseURL = ref(uni.getStorageSync('baseUrl'));
const inputDialog = ref(null);
const inputDialogToggle = () => {
inputDialog.value.open();
};
// 报错配置的Url
const dialogInputConfirm = val => {
baseURL.value = val;
uni.setStorageSync('baseUrl', val);
};
</script>
<style src="./index.scss" lang="scss"></style>