App.vue 9.42 KB
<!--
 * @Author: hookehuyr hookehuyr@gmail.com
 * @Date: 2022-05-26 23:52:36
 * @LastEditors: hookehuyr hookehuyr@gmail.com
 * @LastEditTime: 2024-12-20 17:53:24
 * @FilePath: /data-table/src/App.vue
 * @Description:
-->
<template>
  <router-view></router-view>
</template>

<script setup>
import { mainStore, useTitle } from "@/utils/generatePackage";
import { computed, watchEffect, onMounted } from "vue";
import { useRoute, 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, getUrlParams, stringifyQuery, 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';

// 使用 include + pinia 状态管理动态缓存页面
const store = mainStore();
const keepPages = computed(() => store.getKeepPages);

// TAG: 全局设置页面标题
const $route = useRoute();
// 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);

onMounted(async () => {
  const code = getUrlParams(location.href) ? getUrlParams(location.href).code : '';
  const model = getUrlParams(location.href) ? getUrlParams(location.href).model : '';
  const data_id = getUrlParams(location.href) ? getUrlParams(location.href).data_id : '';
  // 权限控制, 页面参数判断页面功能
  /**
   * add 新增页
   * info 详情页
   * edit 编辑页
   * flow 流程页
   */
  const page_type = getUrlParams(location.href) ? getUrlParams(location.href).page_type : '';
  const raw_url = encodeURIComponent(location.pathname + location.hash);
  const flow_node_code = getUrlParams(location.href) ? getUrlParams(location.href).flow_node_code : ''; // flow_node_code 表示随机选择的流程节点的ID
  const force_back = getUrlParams(location.href) ? getUrlParams(location.href).force_back : ''; // force_back=1 时,强制按照后台用户模式检查权限
  const x_cycle = getUrlParams(location.href) ? getUrlParams(location.href).x_cycle : ''; // 周期ID标识
  // 数据收集设置
  const { data } = await getFormSettingAPI({ form_code: code, page_type, data_id, flow_node_code, force_back, x_cycle });
  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);
    location.href = new_url;
  }
  // 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
  const iframe_openid = getUrlParams(location.href) ? getUrlParams(location.href).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) {
      $router.replace({
        path: '/auth',
        query: {
          href: location.hash,
          code
        }
      });
    }
  } else {
    // 启用微信增强,非预览模式
    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();
        // TEST:判断定位填表功能, 可能会弹出来上一次的表单提示,因为如果定位正确时还是需要恢复相应的表单
        let open_location = false;
        if (force_back !== '1' && open_location) { // 非后台用户模式
          const targetLat = 31.278001;
          const targetLng = 121.542778;
          const radius = 1000; // 半径 1000 米
          wx.getLocation({
            type: 'wgs84', // 默认为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");
              }
            },
            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) {
        // 表单已结束
        $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';
    }
  }

  // TAG:检查是否更新
  if (import.meta.env.PROD) {
    const 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;
}

.@{prefix} {
  color: red;
}

.global-center {
  position: relative;
  top: 50%;
  transform: translateY(-50%);
}

.zIndex {
  z-index: 4500 !important;
}
</style>