login.vue
6.77 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<template>
<view class="login-page" :style="{backgroundImage:setLoginBg}">
<!-- 公共组件-每个页面必须引入 -->
<public-module></public-module>
<view class="u-flex login-main">
<view class="content">
<view class="hello login-text-muted">{{ $t('login.hello') }}</view>
<view style="width: 587rpx;" class="text-clip hello-welcome login-text-muted">{{$t('login.welcomeTo')}} {{setHeadTitle}}
</view>
</view>
</view>
<view class="login-body">
<view class="login-phone">
<view class="form-row u-flex">
<u-input :adjust-position="false" v-model="loginForm.username" type="text" :placeholder="$t('login.pleaseEnterAccount')"
border="bottom" />
</view>
<view class="form-row u-flex">
<u-input :adjust-position="false" v-model="loginForm.password" :password="showPassword"
:placeholder="$t('login.pleaseEnterPassword')" border="bottom">
<template slot="suffix">
<view @click="showPasswordMode" style="padding: 10rpx">
<u-icon width="18" height="14" :name="passwordIcon"></u-icon>
</view>
</template>
</u-input>
</view>
<button class="submit" size="default" @click="onSubmitFunc">
<text class="text">{{ $t('login.login') }}</text>
</button>
<view class="u-flex row-item">
<view class="row-phone login-text-gray" @click="openCodeFunc">{{ $t('login.phoneLogin') }}</view>
<view class="row-reset login-text-gray" @click="findPassrordFunc">{{ $t('login.forgotPassword') }}</view>
</view>
<view class="u-flex link-login">
<!-- #ifdef MP-WEIXIN -->
<view class="link-text login-text-gray">{{ $t('login.thirdAccountLogin') }}</view>
<view style="height: 20rpx"></view>
<button class="link-image" @tap="handleWenxinAuthorization">
<image class="image" src="/static/weixin.png" mode="aspectFill"></image>
</button>
<!-- #endif -->
</view>
</view>
</view>
</view>
</template>
<script>
import {mapMutations,mapActions,mapState} from "vuex";
import api from '@/api'
import {loginPasswordReg,useReLaunch,useShowToast,useShowModal,useNavigateTo} from '@/plugins/utils.js'
export default {
data() {
return {
loginForm: {
username: "",
password: "",
},
showPassword: true,
code: "",
openid: "",
mpOwnConfig: {},
defaultLogo: "/static/login.png",
};
},
onLoad() {
//#ifdef MP-WEIXIN
wx.login({
success: (res) => {
if (!res.code) return
this.code = res.code;
},
});
//#endif
uni.setStorageSync('getConfiguration', {
isConfiguration: false
});
this.getPlateForm();
},
computed: {
passwordIcon() {
return this.showPassword ? '/static/eye-hide.png' : '/static/eye.png'
},
setHeadTitle() {
return this.mpOwnConfig.name !== undefined ? this.mpOwnConfig.name : "ThingsKit"
},
setLoginBg() {
return 'url(' + (this.mpOwnConfig.bg !== undefined ? this.mpOwnConfig.bg : `${this.defaultLogo}`) + ')'
}
},
onShow() {
this.$nextTick(()=>{
uni.setNavigationBarTitle({
title:this.$t('menu.login')
})
})
uni.setStorageSync('getConfiguration', {
isConfiguration: false
});
},
methods: {
...mapState(["plateInfo"]),
...mapMutations(["setUserInfo", "setPlateInfo"]),
...mapActions(["updateBadgeTotal"]),
//获取平台定制信息
async getPlateForm() {
const res = await api.loginApi.getPlateCustomApi()
if (!res) return
this.mpOwnConfig = {
bg: res.background,
logo: res.logo,
name: res.name,
};
},
saveLoginInfo(res, isThirdLoginAndNoDind, isThirdLogin, toastText) {
let tokenInfo = {
refreshToken: res.refreshToken,
isToken: res.token,
};
let userInfo = {
...tokenInfo,
token: true, //token用于判断是否登录
isThirdLoginAndNoDind, //用于判断是否是第三方登录并且不需要绑定账号
isThirdLogin, //用于判断是否是第三方登录并且需要绑定账号
thirdUserId: res.thirdUserId,
};
//设置全局变量openId
getApp().globalData.openId = res.thirdUserId;
if (userInfo.token) {
this.setUserInfo(userInfo);
}
useShowToast(toastText).then(async (res) => {
this.saveUserInfo();
await this.getAlarmTotalData();
useReLaunch("../../pages/index/index")
});
},
//微信授权登录
//#ifdef MP-WEIXIN
handleWenxinAuthorization() {
wx.getUserProfile({
desc: "微信第三方授权",
success: async (reswenxin) => {
if (
reswenxin.errMsg === "getUserProfile:ok" &&
reswenxin.encryptedData
) {
//获取用户信息
let wenxinUserInfo = {
avatarUrl: reswenxin.userInfo.avatarUrl,
thirdUserId: this.openid,
};
const res = await api.loginApi.getThirdLoginApi(this.code)
if (!res.token && res.thirdUserId) {
//需要进行三方绑定
let userInfo = {
isThirdLogin: true, //用于判断是否是第三方登录并且需要绑定账号
avatar: wenxinUserInfo.avatarUrl,
thirdUserId: res.thirdUserId,
};
this.setUserInfo(userInfo);
//设置全局变量openId
getApp().globalData.openId = res.thirdUserId;
useReLaunch("/pages/index/index")
} else {
// 不需要绑定,直接第三方登录使用
this.saveLoginInfo(res, true, null, this.$t('login.thirdAccountLoginSuccess'))
}
}
},
fail: (res) => {
//拒绝授权
return;
},
});
},
//#endif
async saveUserInfo() {
const userInfoRes = await api.loginApi.setUserInfoApi()
const plateInfoRes = await api.loginApi.setPlateInfoApi()
Promise.all([userInfoRes, plateInfoRes]).then(res => {
this.setUserInfo(res[0])
this.setPlateInfo(res[1])
})
},
async getAlarmTotalData() {
const res = await api.loginApi.getAlarmTotalApi()
if (!res) return
this.updateBadgeTotal(res.totalAlarm?.activedAlarm);
},
async onSubmitFunc() {
const validateValue = Object.values(this.loginForm)
if (!validateValue[0]) return uni.$u.toast(this.$t('login.pleaseEnterAccount'));
if (!validateValue[1]) return uni.$u.toast($t('login.pleaseEnterPassword'));
if (!loginPasswordReg.test(validateValue[1])) return useShowModal(this.$t('login.passwordRule'))
const res = await api.loginApi.postLoginApi(this.loginForm)
if (res) {
// 储存登录信息
this.saveLoginInfo(res, false, false, this.$t('login.loginSuccess'))
}
},
openCodeFunc() {
useNavigateTo("../other/code")
},
findPassrordFunc() {
useNavigateTo("../other/find-password")
},
showPasswordMode() {
this.showPassword = !this.showPassword;
},
},
};
</script>
<style lang="scss" scoped>
@import "./static/login.scss";
::v-deep button {
background: rgba(0, 0, 0, 0);
}
</style>