qingyiay
2023-03-22 8e095c1fd6164fafc7d5cfd144528a0d67afef7c
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
<template>
    <view>
        <view class="list">
            <view class="list-call">
                <u--input
                    v-model="username"
                    clearable
                    maxlength="32"
                    type="text"
                    placeholder="请输入用户名"
                    prefixIcon="account"
                    prefixIconStyle="font-size: 22px;color: #909399"
                ></u--input>
            </view>
            <view class="list-call">
                <u--input
                    v-model="password"
                    clearable
                    maxlength="32"
                    type="password"
                    placeholder="请输入密码"
                    prefixIcon="lock"
                    prefixIconStyle="font-size: 22px;color: #909399"
                ></u--input>
            </view>
        </view>
        <view class="loginBtn"><u-button type="primary" :text="btnText == 1 ? '绑定' : '登录'" @click="submit()"></u-button></view>
    </view>
</template>
 
<script>
import { apiLoginPassword } from '@/api/publicInterface.js';
import { mapMutations } from 'vuex';
import { setToken, setRefreshToken, setUsernameKey, setCustomerId, redirectHome } from '@/utils/status.js';
export default {
    name: 'userPassword',
    props: {
        //0:正常,1:微信绑定
        btnText: {
            type: Number,
            default: 0
        }
    },
    data() {
        return {
            username: 'appuser', // 客户
            // username: '18805080506',// 王楠(司机)
            password: '123456',
            remember: true,
            baseUrl: ''
        };
    },
    methods: {
        ...mapMutations(['setUserTabbar']),
        //登录
        async submit() {
            uni.showLoading({
                title: '登录中...'
            });
            //表单校验
            if (this.username.length === 0) {
                this.$u.toast('请输入账号');
                return;
            }
            if (this.password.length === 0) {
                this.$u.toast('请输入密码');
                return;
            }
            let grant_type = 'password';
            //登录接口
            await apiLoginPassword({
                username: this.username,
                password: this.password,
                grant_type,
                scope: 'server'
            })
                .then(res => {
                    setToken(res.access_token);
                    setRefreshToken(res.refresh_token);
                    if (this.btnText == 0) {
                        if (res.code != 1) {
                            // 登陆成功,存储相关信息
                            setToken(res.access_token);
                            setRefreshToken(res.refresh_token);
                            setUsernameKey(res.username);
                            //查询用户详细信息并储存
                            this.$reqGet('getUserEntity')
                                .then(res => {
                                    this.$u.toast('恭喜您,登录成功!');
                                    this.setUserTabbar(res.data.type);
                                    uni.setStorageSync('roleType', res.data.type);
                                    setCustomerId(res.data.customerid);
                                    uni.hideLoading();
                                    //跳转页面
                                    this.$nextTick(() => {
                                        redirectHome();
                                    });
                                })
                                .catch(err => {
                                    this.$u.toast('登录异常!');
                                    console.log(err);
                                });
                        } else {
                            uni.hideLoading();
                            this.$u.toast(res.msg);
                        }
                    } else {
                        this.$nextTick(() => {
                            this.$reqPost('wxBind', { state: 'MINI', code: uni.getStorageSync('code') }, 'params').then(res => {
                                uni.hideLoading();
                                if (res.code == 0) {
                                    this.$u.toast('绑定成功!即将回到登录页!');
                                    setTimeout(() => {
                                        uni.navigateBack();
                                    }, 1000);
                                } else {
                                    this.$u.toast(res.msg ? res.msg : '绑定失败');
                                }
                            });
                        });
                    }
                })
                .catch(e => {
                    this.$u.toast(e);
                });
        }
    }
};
</script>
 
<style lang="scss">
@import 'index.scss';
.dengluBtn {
    margin-top: 80rpx;
}
</style>