App.vue
12.1 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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
<!--
* @Author: hookehuyr hookehuyr@gmail.com
* @Date: 2022-05-26 23:52:36
* @LastEditors: hookehuyr hookehuyr@gmail.com
* @LastEditTime: 2025-10-01 15:41:19
* @FilePath: /data-table/src/App.vue
* @Description:
-->
<template>
<div class="app-shell">
<router-view v-if="app_bootstrap_ready"></router-view>
<div v-else class="app-bootstrap-loading" :style="{ background: styleColor.backgroundColor }">
<van-loading size="24px" vertical :color="styleColor.baseColor">页面授权中...</van-loading>
</div>
</div>
</template>
<script setup>
import { mainStore, useTitle } from "@/utils/generatePackage";
import { computed, ref, watchEffect, onMounted, onUnmounted } from "vue";
import { useRouter } from "vue-router";
// import { Toast } from "vant";
// 会根据配置判断是否显示调试控件
// eslint-disable-next-line no-unused-vars
import vConsole from "@/utils/vconsole";
// 初始化WX环境
import wx from 'weixin-js-sdk'
import { wxJsAPI } from '@/api/wx/config'
import { apiList } from '@/api/wx/jsApiList.js'
import { wxInfo, isWithinRadius } from "@/utils/tools";
import { styleColor } from "@/constant.js";
import { getFormSettingAPI } from "@/api/form.js";
import { showDialog, showConfirmDialog } from 'vant';
// import fp3 from '@/utils/fp3';
import { Updater } from '@/utils/versionUpdater';
import { resetDialogState } from '@/utils/dialogControl.js';
import { useAuthRedirect } from '@/composables';
// 使用 include + pinia 状态管理动态缓存页面
const store = mainStore();
const keepPages = computed(() => store.getKeepPages);
// 表单配置和授权判断完成前,先只显示加载态,避免用户先看到表单又被授权回跳打断
const app_bootstrap_ready = ref(false);
// TAG: 全局设置页面标题
// watchEffect(() => useTitle("表单标题"));
// 监听路由变化
// 切换路由页面返回顶部
const $router = useRouter();
// watch(
// () => $router.currentRoute.value,
// (newValue, oldValue) => {
// nextTick(() => {
// // document.getElementById('app')?.scrollIntoView();
// });
// },
// { immediate: true }
// );
// TAG: 全局配置Toast
// Toast.setDefaultOptions({
// duration: 2000,
// className: 'zIndex'
// });
// web端判断
const is_pc = computed(() => wxInfo().isPC);
// 微信端判断
const is_wx = computed(() => wxInfo().isWeiXin);
// iframe模式判断
const is_iframe = computed(() => window.self !== window.top);
const {
getQueryValue,
getCurrentTargetHash,
redirectToOpenIdAuth,
savePendingAuthReturnGuard,
consumePendingAuthReturnGuard,
installAuthBackHistoryGuard,
markSkipCycleCheckForAuth,
} = useAuthRedirect();
// 组件卸载时清理定时器(必须在 setup 同步阶段注册钩子)
let upDater = null;
onUnmounted(() => {
if (upDater) {
upDater.destroy();
}
});
onMounted(async () => {
let leaving_current_page = false;
try {
// 重置弹框状态,确保每次访问页面时都能正常显示未完成表单弹框
resetDialogState();
const code = getQueryValue('code');
const model = getQueryValue('model');
const data_id = getQueryValue('data_id');
// 权限控制, 页面参数判断页面功能
/**
* add 新增页
* info 详情页
* edit 编辑页
* flow 流程页
*/
const page_type = getQueryValue('page_type');
const raw_url = encodeURIComponent(location.pathname + location.hash);
const flow_node_code = getQueryValue('flow_node_code'); // flow_node_code 表示随机选择的流程节点的ID
const force_back = getQueryValue('force_back'); // force_back=1 时,强制按照后台用户模式检查权限
const x_cycle = getQueryValue('x_cycle'); // 周期ID标识
const volunteer_source = getQueryValue('volunteer_source'); // 义工来源
// iframe传值openid
const iframe_openid = getQueryValue('openid');
// 数据收集设置
const { data } = await getFormSettingAPI({ form_code: code, page_type, data_id, flow_node_code, force_back, x_cycle, volunteer_source, openid: iframe_openid });
const form_setting = {};
if (data.length) {
Object.assign(form_setting, data[0]['property_list'], data[0]['extend']);
}
// TAG: 西园寺特有,拿到新域名跳转
if (!import.meta.env.DEV && form_setting.redirect_host) {
let host = location.host;
let url = location.href;
let new_url = url.replace(host, form_setting.redirect_host);
leaving_current_page = true;
location.href = new_url;
return;
}
// TAG: 是否显示流程按钮
if (page_type === 'add' && form_setting.flow_id) {
form_setting.is_flow = true;
}
if (page_type === 'flow') {
form_setting.is_flow = true;
}
// 缓存表单设置
store.changeFormSetting(form_setting);
// TAG: 跳转未授权页
if (form_setting.auth_error) { // 权限报错信息存在
$router.replace({
path: '/no_auth',
query: {
code,
data_id
}
});
}
// 没有授权判断
let open_auth = form_setting.wxzq_enable && !form_setting.x_field_weixin_openid;
// iframe传值openid
if (iframe_openid) { // 如果获取到iframe传值openid 不再校验授权
open_auth = false;
}
const no_preview_model = model !== 'preview';
let record_openid = false; // 是否记录open_id
/**
* is_back_user 用户登录情况
* 1. 后台用户已登录, 新增表单(无data_id), 为了记录 openid 如果有微信增强设置,则启用微信增强
* 2. 后台用户未登录, 启用微信增强
*/
// if (!form_setting.is_back_user) { // 用户未登录
if (force_back !== '1') { // 非后台用户模式
record_openid = true;
} else { // 用户已登录
if (!is_iframe.value && page_type === 'add') { // 非iframe里面新增表单
record_openid = true;
}
}
// 需要网页授权-必须要域名相同,需要上传到线上测试
/**
* wxzq_scope 微信公众号授权模式
* 空字符串=不授权,snsapi_base=静默授权,snsapi_userinfo=显式授权
*/
// 非测试环境,没有openid信息,需要授权
if (!import.meta.env.DEV && open_auth && form_setting.wxzq_scope && record_openid) {
// 预览模式不开启
if (no_preview_model) {
const currentTargetHash = getCurrentTargetHash();
// 记录这次授权前的目标页和历史长度,授权成功回跳后要用来修正浏览器后退行为
savePendingAuthReturnGuard(code, currentTargetHash);
// 设置标识,让路由守卫在授权返回后的首轮路由里跳过一次周期检查
markSkipCycleCheckForAuth();
// 直接跳去后端授权地址,不再先经过站内 /auth 中转页,避免浏览器历史里留下 auth 记录
leaving_current_page = true;
redirectToOpenIdAuth(code, currentTargetHash);
return;
}
} else {
if (no_preview_model) {
const currentTargetHash = getCurrentTargetHash();
const authReturnGuard = consumePendingAuthReturnGuard(code, currentTargetHash);
// 只有授权前原本就有上一页时,才需要拦住第一次后退并跨过中间的授权地址
if (authReturnGuard?.historyLength > 1) {
installAuthBackHistoryGuard();
}
}
// 启用微信增强,非预览模式
if (form_setting.wxzq_enable && no_preview_model) {
const wxJs = await wxJsAPI({ form_code: code, url: raw_url });
wxJs.data.jsApiList = apiList;
wx.config(wxJs.data);
wx.ready(() => {
wx.showAllNonBaseMenuItem();
// TAG:判断定位填表功能, 可能会弹出来上一次的表单提示,因为如果定位正确时还是需要恢复相应的表单
let open_location = form_setting.geofence_enable;
if (force_back !== '1' && open_location) { // 非后台用户模式
const targetLat = form_setting.geofence_center_latitude;
const targetLng = form_setting.geofence_center_longitude;
const radius = form_setting.geofence_circle_radius; // 半径 1000 米
wx.getLocation({
type: 'gcj02', // 默认为wgs84的gps坐标,如果要返回直接给openLocation用的火星坐标,可传入'gcj02'
success: function (res) {
let currentLat = res.latitude; // 纬度,浮点数,范围为90 ~ -90
let currentLng = res.longitude; // 经度,浮点数,范围为180 ~ -180。
let is_in = isWithinRadius(currentLat, currentLng, targetLat, targetLng, radius);
if (!is_in) {
// 表单定位错误
$router.push("/stop?status=location_error&latitude=" + targetLat + "&longitude=" + targetLng);
}
},
fail: function (res) {
$router.push("/stop?status=get_location_fail");
}
});
}
});
wx.error((err) => {
console.warn(err);
});
}
// 判断跳转页面
// if (!form_setting.is_back_user) { // 用户未登录
if (force_back !== '1') { // 非后台用户模式
// 开启后有开始和结束时间,不在时间范围的显示表单还未开始或者已经结束
if (form_setting.sjsj_is_time_range && form_setting.sjsj_is_time_range) {
// 未开始
if (form_setting.server_time < form_setting.sjsj_begin_time) {
$router.push("/stop?status=apply");
}
// 已结束
if (form_setting.server_time > form_setting.sjsj_end_time) {
$router.push("/stop?status=finish");
}
}
if (form_setting.sjsj_enable === 0 && !form_setting.sjsj_enable && (page_type === 'add' || page_type === '')) { // 新增的时候才判断
// 表单已结束
$router.push("/stop?status=disable");
}
}
// 当数据量达到限额时,该表单将不能继续提交数据。
if (form_setting.sjsj_max_count_error) {
showDialog({
title: '温馨提示',
message: form_setting.sjsj_max_count_error,
theme: 'round-button',
confirmButtonColor: styleColor.baseColor
});
}
// 设定填写次数
if (form_setting.wxzq_scope && no_preview_model) {
if (form_setting.fill_error) {
showDialog({
title: '温馨提示',
message: form_setting.fill_error,
theme: 'round-button',
confirmButtonColor: styleColor.baseColor
});
}
}
if (is_wx.value) {
document.getElementById('app').style.maxWidth = '100vw';
}
}
} finally {
// 只有在当前页不需要立刻跳去授权/跳域名时,才让真实页面开始渲染
if (!leaving_current_page && !app_bootstrap_ready.value) {
app_bootstrap_ready.value = true;
}
}
// TAG:检查是否更新
if (import.meta.env.PROD) {
upDater = new Updater({
time: 30000
})
upDater.on('no-update', () => {
// console.log('还没更新')
})
upDater.on('update', () => {
showConfirmDialog({
title: '温馨提示',
message: '检测到新版本,是否刷新页面!',
confirmButtonColor: styleColor.baseColor
}).then(() => {
window.location.reload();
});
})
}
});
</script>
<style lang="less">
@prefix: ~"@{namespace}-x";
html,
body {
width: 100%;
// height: 100%;
color: @base-font-color;
// background-color: #f7f8fa;
// background-color: #fff9ef;
padding: 0;
margin: 0;
}
body {
position: relative;
display: flex;
justify-content: center;
p {
margin: 0;
padding: 0;
}
}
#app {
min-height: calc(100vh);
// max-width: 800px;
position: relative;
}
.app-shell {
min-height: 100vh;
}
.app-bootstrap-loading {
position: fixed;
inset: 0;
z-index: 5000;
display: flex;
align-items: center;
justify-content: center;
padding: 0 24px;
box-sizing: border-box;
background: #ffffff;
}
.@{prefix} {
color: red;
}
.global-center {
position: relative;
top: 50%;
transform: translateY(-50%);
}
.zIndex {
z-index: 4500 !important;
}
</style>