App.vue
6.52 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
<script>
import cache from '@/utils/cache';
import config from '@/config/index';
import { mapState } from 'vuex';
// #ifdef MP-DINGTALK
import { onAuthAppBack, openAuthMiniApp } from 'dingtalk-design-libs/biz/openAuthMiniApp';
// #endif
export default {
computed: {
...mapState(['authed', 'userInfo']),
},
onLaunch(options) {
const systemInfo = uni.getSystemInfoSync();
let origin = systemInfo?.environment === 'wxwork' ? 'QY_FREGHT_MA' : 'FREGHT_MA';
// #ifdef MP-DINGTALK
origin = 'DINGTALK_FREGHT_MA';
// #endif
// 设置小程序来源,企业微信小程序或微信小程序
this.$store.commit('SET_ORIGIN', origin);
this.$store.commit('SET_SCENE', options.scene);
if (!this.authed) {
if (origin === 'QY_FREGHT_MA') {
wx.qy.login({
suiteId: config.suiteId,
success: res => {
if (res.code) {
uni.$u.api.login.loginByCode({ code: res.code }).then(response => {
if (!response.result) return this.handleLogin();
const result = response.result || {};
this.$store.commit('SET_AUTHED', result.hasBind);
this.$store.commit('SET_USER_INFO', result);
this.getPermission();
if (result.hasBind) {
uni.$u.api.user.detail().then(res => {
this.$store.commit('SET_FREGHT_INFO', res.result);
});
this.loadCache();
}
});
}
},
fail: () => {
this.handleLogin();
},
});
} else {
this.handleLogin();
}
} else {
this.loadCache();
}
// 监听刷新权限
uni.$on('refresh-permission', () => {
this.getPermission();
this.loadCache();
});
// 监听检测更新
uni.$on('refresh-version', params => {
const { notifyType = 'toast' } = params || {};
this.handleRefreshVersion(notifyType);
});
// 初始化检测更新
uni.$emit('refresh-version', { notifyType: 'silent' });
},
onShow(options) {
// #ifdef MP-DINGTALK
// 监听钉钉统一授权小程序返回结果的方法
onAuthAppBack(options, data => {
const { status, result } = data || {};
if (status === 'ok' && result && result.authCode) {
uni.$u.api.login.dingtalk({ authCode: result.authCode, corpId: dd.corpId }).then(response => {
const result = response.result || {};
this.$store.commit('SET_AUTHED', result.hasBind);
this.$store.commit('SET_USER_INFO', result);
this.getPermission();
if (result.hasBind) {
uni.$u.api.user.detail().then(res => {
this.$store.commit('SET_FREGHT_INFO', res.result);
});
}
});
}
return data;
});
// #endif
},
methods: {
loadCache() {
uni.$u.api.user.detail().then(res => {
this.$store.commit('SET_FREGHT_INFO', res.result);
});
this.$u.api.dict.all().then(response => {
cache.put('DICT', response.result);
this.$store.commit('SET_DICT', response.result);
});
this.$u.api.district.all().then(response => {
cache.put('DISTRICT', response.result);
this.$store.commit('SET_DISTRICT', response.result);
});
},
// 检测更新
handleRefreshVersion(notifyType) {
const showToast = options => {
if (notifyType === 'silent') return; // 静默
wx.showToast(options);
};
if (wx.canIUse('getUpdateManager')) {
const updateManager = wx.getUpdateManager();
showToast({ title: '检测中,请稍后', icon: 'none', duration: 2000 });
updateManager.onCheckForUpdate(ret => {
// 请求完新版本信息的回调
if (ret.hasUpdate) {
updateManager.onUpdateReady(() => {
wx.hideToast();
wx.showModal({
title: '更新提示',
content: '新版本已经准备好,是否重启应用?',
success(res) {
if (res.confirm) {
updateManager.applyUpdate();
}
},
});
});
updateManager.onUpdateFailed(() => {
// 新版本下载失败
showToast({ title: '网络状况不佳,稍后再试', icon: 'none', duration: 2000 });
});
} else {
showToast({ title: '当前已是最新版本', icon: 'none', duration: 2000 });
}
});
} else {
showToast({ title: `当前${wx.getSystemInfoSync()?.environment === 'wxwork' ? '企业' : ''}微信版本过低,请升级到最新版本后重试`, icon: 'none', duration: 3000 });
}
},
// 查询权限
getPermission() {
return new Promise(resolve => {
uni.$u.api.user
.getPermission({ systemCode: 'freight-web', userId: this.userInfo.username })
.then((response = {}) => {
const { result = [] } = response;
this.$store.commit('SET_PERMISSION', result);
resolve(result);
})
.catch(() => {
resolve([]);
});
});
},
handleLogin() {
// #ifdef MP-WEIXIN
uni.login({
provider: 'weixin',
success: response => {
const { code } = response;
if (code) {
uni.$u.api.login.wechat({ code }).then(response => {
const result = response.result || {};
this.$store.commit('SET_AUTHED', result.hasBind);
if (result.hasBind) {
this.$store.commit('SET_USER_INFO', result);
uni.$emit('refresh-permission');
}
});
}
},
});
// #endif
// #ifdef MP-DINGTALK
// 打开钉钉统一授权小程序
openAuthMiniApp({
path: 'pages/home/home', //不要改,这里是小程序dingwlanwvdmrtjjwdmd下的一个页面地址
panelHeight: 'percent50',
extraData: {
clientId: config.clientId, // 应用ID(唯一标识)
rpcScope: 'Contact.User.Read',
fieldScope: 'Contact.User.mobile',
type: 0,
ext: JSON.stringify({}),
from: '',
},
});
// #endif
},
},
};
</script>
<style lang="scss">
@import '@/uni_modules/uview-ui/index.scss';
@import '~styles/common.scss';
@import './iconfont/iconfont.css';
@import '~styles/common.scss';
.u-radio + .u-radio {
margin-left: 40upx !important;
}
</style>