App.uvue 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <script lang="uts">
  2. import { state, setLifeCycleNum, checkSystemTheme, setNetless } from '@/store/index.uts'
  3. // #ifdef APP-ANDROID || APP-HARMONY
  4. let firstBackTime = 0
  5. // #endif
  6. export default {
  7. globalData: {
  8. str: 'default globalData str',
  9. num: 0,
  10. bool: false,
  11. obj: {
  12. str: 'default globalData obj str',
  13. num: 0,
  14. bool: false,
  15. } as UTSJSONObject,
  16. null: null as string | null,
  17. arr: [] as number[],
  18. mySet: new Set<string>(),
  19. myMap: new Map<string, any>(),
  20. func: () : string => {
  21. return 'globalData func'
  22. },
  23. launchOptions: {
  24. path: '',
  25. } as OnLaunchOptions,
  26. onShowOption: {
  27. path: ''
  28. } as OnShowOptions
  29. },
  30. onLaunch: function (res : OnLaunchOptions) {
  31. this.globalData.launchOptions = res
  32. // 自动化测试
  33. setLifeCycleNum(state.lifeCycleNum + 1000)
  34. console.log('App Launch')
  35. // 页面性能分析
  36. // const performance = uni.getPerformance()
  37. // const observer1: PerformanceObserver = performance.createObserver(
  38. // (entryList: PerformanceObserverEntryList) => {
  39. // console.log('observer1:entryList.getEntries()' +JSON.stringify(entryList.getEntries()))
  40. // }
  41. // )
  42. // observer1.observe({
  43. // entryTypes: ['render', 'navigation'],
  44. // } as PerformanceObserverOptions)
  45. // 统计上报 - 应用启动
  46. // #ifdef APP-ANDROID || APP-IOS || WEB || MP-WEIXIN
  47. uni.report({
  48. name: 'uni-app-launch',
  49. options: res,
  50. success(res_data) {
  51. console.log(res_data);
  52. }, fail(err) {
  53. console.log(err);
  54. }
  55. })
  56. // #endif
  57. // #ifdef APP
  58. if (process.env.NODE_ENV !== 'development') { //真机运行可以注释此条件
  59. uni.getPrivacySetting({
  60. success(res1){
  61. if(res1.needAuthorization){
  62. uni.openDialogPage({
  63. url: '/pages/component/button/privacy',
  64. })
  65. }
  66. }
  67. })
  68. // #ifdef APP-HARMONY
  69. uni.getNetworkType({
  70. success(res) {
  71. setNetless(res.networkType === 'none')
  72. }
  73. })
  74. uni.onNetworkStatusChange((res) => {
  75. setNetless(res.isConnected === false)
  76. })
  77. // #endif
  78. }
  79. // #endif
  80. // 获取系统主题
  81. checkSystemTheme();
  82. },
  83. onShow: function (res : OnShowOptions) {
  84. this.globalData.onShowOption = res
  85. // 处理scheme或通用链接直达
  86. let url = this.getRedirectUrl(res.appScheme, res.appLink);
  87. if (null != url) {
  88. uni.navigateTo({
  89. url: url
  90. })
  91. }
  92. // 自动化测试
  93. setLifeCycleNum(state.lifeCycleNum + 100)
  94. console.log('App Show')
  95. // #ifdef APP-ANDROID || APP-IOS || WEB || MP-WEIXIN
  96. // 统计上报 - 应用显示
  97. uni.report({
  98. name: 'uni-app-show',
  99. success(res_data) {
  100. console.log(res_data);
  101. }, fail(err) {
  102. console.log(err);
  103. }
  104. })
  105. // #endif
  106. },
  107. onHide: function () {
  108. // 自动化测试
  109. setLifeCycleNum(state.lifeCycleNum - 100)
  110. console.log('App Hide')
  111. // #ifdef APP-ANDROID || APP-IOS || WEB || MP-WEIXIN
  112. // 统计上报 - 应用进入后台
  113. uni.report({
  114. name: 'uni-app-hide',
  115. success(res) {
  116. console.log(res);
  117. }, fail(err) {
  118. console.log(err);
  119. }
  120. })
  121. // #endif
  122. },
  123. // #ifdef APP-ANDROID || APP-HARMONY
  124. onLastPageBackPress: function () {
  125. // 自动化测试
  126. setLifeCycleNum(state.lifeCycleNum - 1000)
  127. console.log('App LastPageBackPress')
  128. if (firstBackTime == 0) {
  129. uni.showToast({
  130. title: '再按一次退出应用',
  131. position: 'bottom',
  132. })
  133. firstBackTime = Date.now()
  134. setTimeout(() => {
  135. firstBackTime = 0
  136. }, 2000)
  137. } else if (Date.now() - firstBackTime < 2000) {
  138. firstBackTime = Date.now()
  139. uni.exit()
  140. }
  141. },
  142. onExit() {
  143. console.log('App Exit')
  144. },
  145. // #endif
  146. onError(err : any) {
  147. // console.log('App onError', err)
  148. // #ifdef APP-ANDROID || APP-IOS || WEB || MP-WEIXIN
  149. // 统计上报 - 应用发生错误
  150. uni.report({
  151. name: 'uni-app-error',
  152. options: err,
  153. success(res) {
  154. console.log(res);
  155. }, fail(err) {
  156. console.log(err);
  157. }
  158. })
  159. // #endif
  160. },
  161. methods: {
  162. increasetLifeCycleNum() {
  163. setLifeCycleNum(state.lifeCycleNum + 100)
  164. console.log('App increasetLifeCycleNum')
  165. },
  166. getRedirectUrl(scheme : string | null, ulink : string | null) : string | null {
  167. //解析scheme或universal link启动直达页面:
  168. //scheme格式:uniappx://redirect/pages/component/view/view?key=value //其中redirect后为页面路径
  169. //universal link格式:https://uniappx.dcloud.net.cn/ulink/redirect.html?url=%2Fpages%2Fcomponent%2Fview%2Fview%3Fkey%3Dvalue //通用链接路径需固定,?后面的url参数为直达页面路径,注意url字段值需做url编码(可使用encodeURIComponent方法)
  170. let url : string | null = null;
  171. if (null != scheme && scheme.length > 0) {
  172. const PATHPRE = 'redirect';
  173. let parts : string | null = null;
  174. let pos = scheme.search('//');
  175. if (pos > 0) {
  176. parts = scheme.substring(pos + 2);
  177. }
  178. if (null != parts && parts.startsWith(PATHPRE)) {
  179. url = parts.substring(PATHPRE.length);
  180. }
  181. } else if (null != ulink && ulink.length > 0) {
  182. const PATH = 'ulink/redirect.html';
  183. let parts = ulink.split('?');
  184. if (parts.length > 1 && parts[0].endsWith(PATH) && parts[1].length > 0) {
  185. parts[1].split('&').forEach((e) => {
  186. let params = e.split('=');
  187. if (params.length > 1 && params[0].length > 0 && params[1].length > 0) {
  188. if ('url' == params[0]) {
  189. if (null == url) {
  190. url = decodeURIComponent(params[1]);
  191. }
  192. }
  193. }
  194. });
  195. }
  196. }
  197. return url;
  198. }
  199. }
  200. }
  201. </script>
  202. <style>
  203. /*每个页面公共css */
  204. @import "./common/uni.css";
  205. /* #ifdef WEB */
  206. .uni-top-window uni-tabbar .uni-tabbar {
  207. background-color: #fff !important;
  208. }
  209. /* #endif */
  210. /* #ifdef MP-WEIXIN */
  211. page {
  212. background-color: #efeff4;
  213. }
  214. /* #endif */
  215. </style>