App.uvue 5.7 KB
Newer Older
辛宝Otto's avatar
辛宝Otto 已提交
1 2
<script lang="uts">
  import { state, setLifeCycleNum } from '@/store/index.uts'
3

辛宝Otto's avatar
辛宝Otto 已提交
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
  // #ifdef APP-ANDROID
  let firstBackTime = 0
  // #endif
  export default {
    globalData: {
      str: 'default globalData str',
      num: 0,
      bool: false,
      obj: {
        str: 'default globalData obj str',
        num: 0,
        bool: false,
      },
      null: null as string | null,
      arr: [] as number[],
      mySet: new Set<string>(),
      myMap: new Map<string, any>(),
      func: () : string => {
        return 'globalData func'
      },
      launchOptions: {
        path: '',
      } as OnLaunchOptions,
      onShowOption: {
        path: ''
      } as OnShowOptions
    },
    onLaunch: function (res : OnLaunchOptions) {
DCloud-WZF's avatar
DCloud-WZF 已提交
32

辛宝Otto's avatar
辛宝Otto 已提交
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
      this.globalData.launchOptions = res

      // 自动化测试
      setLifeCycleNum(state.lifeCycleNum + 1000)
      console.log('App Launch')

      // 页面性能分析
      // const performance = uni.getPerformance()
      // const observer1: PerformanceObserver = performance.createObserver(
      //   (entryList: PerformanceObserverEntryList) => {
      //     console.log('observer1:entryList.getEntries()' +JSON.stringify(entryList.getEntries()))
      //   }
      // )
      // observer1.observe({
      //   entryTypes: ['render', 'navigation'],
      // } as PerformanceObserverOptions)
M
mehaotian 已提交
49 50

      // 统计上报 - 应用启动
M
mehaotian 已提交
51 52 53 54 55 56 57 58 59
      uni.report({
      	name: 'uni-app-launch',
      	options: res,
      	success(res_data) {
      		console.log(res_data);
      	}, fail(err) {
      		console.log(err);
      	}
      })
M
mahaifeng 已提交
60
      // #ifdef APP
61 62 63 64 65
      if (process.env.NODE_ENV !== 'development') { //真机运行可以注释此条件
        uni.getPrivacySetting({
          success(res1){
             if(res1.needAuthorization){
               uni.openDialogPage({
M
mahaifeng 已提交
66
                 url: '/pages/component/button/privacy',
67 68 69 70
               })
             }
          }
        })
M
mahaifeng 已提交
71
      }
DCloud-WZF's avatar
DCloud-WZF 已提交
72
      // #endif
辛宝Otto's avatar
辛宝Otto 已提交
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
    },
    onShow: function (res : OnShowOptions) {
      this.globalData.onShowOption = res

      // 处理scheme或通用链接直达
      let url = this.getRedirectUrl(res.appScheme, res.appLink);
      if (null != url) {
        uni.navigateTo({
          url: url
        })
      }

      // 自动化测试
      setLifeCycleNum(state.lifeCycleNum + 100)
      console.log('App Show')
M
mehaotian 已提交
88 89

      // 统计上报 - 应用显示
M
mehaotian 已提交
90 91 92 93 94 95 96 97
      uni.report({
      	name: 'uni-app-show',
      	success(res_data) {
      		console.log(res_data);
      	}, fail(err) {
      		console.log(err);
      	}
      })
辛宝Otto's avatar
辛宝Otto 已提交
98 99 100 101 102
    },
    onHide: function () {
      // 自动化测试
      setLifeCycleNum(state.lifeCycleNum - 100)
      console.log('App Hide')
M
mehaotian 已提交
103 104

      // 统计上报 - 应用进入后台
M
mehaotian 已提交
105 106 107 108 109 110 111 112
      // uni.report({
      // 	name: 'uni-app-hide',
      // 	success(res) {
      // 		console.log(res);
      // 	}, fail(err) {
      // 		console.log(err);
      // 	}
      // })
辛宝Otto's avatar
辛宝Otto 已提交
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
    },
    // #ifdef APP-ANDROID
    onLastPageBackPress: function () {
      // 自动化测试
      setLifeCycleNum(state.lifeCycleNum - 1000)
      console.log('App LastPageBackPress')
      if (firstBackTime == 0) {
        uni.showToast({
          title: '再按一次退出应用',
          position: 'bottom',
        })
        firstBackTime = Date.now()
        setTimeout(() => {
          firstBackTime = 0
        }, 2000)
      } else if (Date.now() - firstBackTime < 2000) {
        firstBackTime = Date.now()
        uni.exit()
      }
    },
    onExit() {
      console.log('App Exit')
    },
M
mehaotian 已提交
136 137 138 139 140 141 142 143 144 145 146 147
    onError(err : any) {
    	// 统计上报 - 应用发生错误
    	uni.report({
    		name: 'uni-app-error',
    		options: err,
    		success(res) {
    			console.log(res);
    		}, fail(err) {
    			console.log(err);
    		}
    	})
    },
辛宝Otto's avatar
辛宝Otto 已提交
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
    // #endif
    methods: {
      increasetLifeCycleNum() {
        setLifeCycleNum(state.lifeCycleNum + 100)
        console.log('App increasetLifeCycleNum')
      },
      getRedirectUrl(scheme : string | null, ulink : string | null) : string | null {
        //解析scheme或universal link启动直达页面:
        //scheme格式:uniappx://redirect/pages/component/view/view?key=value  //其中redirect后为页面路径
        //universal link格式:https://uniappx.dcloud.net.cn/ulink/redirect.html?url=%2Fpages%2Fcomponent%2Fview%2Fview%3Fkey%3Dvalue  //通用链接路径需固定,?后面的url参数为直达页面路径,注意url字段值需做url编码(可使用encodeURIComponent方法)
        let url : string | null = null;
        if (null != scheme && scheme.length > 0) {
          const PATHPRE = 'redirect';
          let parts : string | null = null;
          let pos = scheme.search('//');
          if (pos > 0) {
            parts = scheme.substring(pos + 2);
          }
          if (null != parts && parts.startsWith(PATHPRE)) {
            url = parts.substring(PATHPRE.length);
          }
        } else if (null != ulink && ulink.length > 0) {
          const PATH = 'ulink/redirect.html';
          let parts = ulink.split('?');
          if (parts.length > 1 && parts[0].endsWith(PATH) && parts[1].length > 0) {
            parts[1].split('&').forEach((e) => {
              let params = e.split('=');
              if (params.length > 1 && params[0].length > 0 && params[1].length > 0) {
                if ('url' == params[0]) {
                  if (null == url) {
                    url = decodeURIComponent(params[1]);
                  }
                }
              }
            });
          }
        }
        return url;
      }
    }
  }
</script>

<style>
  /*每个页面公共css */
  @import "./common/uni.css";

  /* #ifdef WEB */
  .uni-top-window uni-tabbar .uni-tabbar {
    background-color: #fff !important;
  }
  /* #endif */
DCloud-WZF's avatar
DCloud-WZF 已提交
200
</style>