四零语境前端代码仓库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

305 lines
6.6 KiB

1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
2 weeks ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
  1. <template>
  2. <view v-if="showSplash" class="splash-screen">
  3. <!-- 富文本內容 -->
  4. <view class="splash-content">
  5. <image
  6. :src="splashContent"
  7. mode="aspectFit"
  8. class="splash-image"
  9. @error="onImageError"
  10. @load="onImageLoad"
  11. />
  12. </view>
  13. <!-- 跳過按鈕 -->
  14. <view class="skip-button" >
  15. <text class="skip-text">跳过 ({{ countdown }}s)</text>
  16. </view>
  17. <!-- 定位英語文字和中文文字到左下角 -->
  18. <view class="text-container">
  19. <text class="english-text">
  20. {{ configParamContent('creen_en') }}
  21. </text>
  22. <text class="chinese-text">
  23. {{ configParamContent('creen_zh') }}
  24. </text>
  25. </view>
  26. </view>
  27. </template>
  28. <script>
  29. // import config from '../../mixins/config.js'
  30. export default {
  31. name: 'SplashScreen',
  32. // mixins: [config],
  33. props: {
  34. // 顯示時長(秒)
  35. duration: {
  36. type: Number,
  37. default: 2
  38. }
  39. },
  40. data() {
  41. return {
  42. showSplash: false,
  43. countdown: 5,
  44. timer: null,
  45. splashContent: ''
  46. }
  47. },
  48. computed: {
  49. image() {
  50. try {
  51. // 優先從配置獲取
  52. const configImage = this.configParamContent && this.configParamContent('creen_image')
  53. if (configImage && configImage !== 'undefined' && configImage !== '') {
  54. return configImage
  55. }
  56. // 備用方案:從本地存儲獲取
  57. const storageImage = uni.getStorageSync('screen_image')
  58. if (storageImage && storageImage !== 'undefined' && storageImage !== '') {
  59. return storageImage
  60. }
  61. return ''
  62. } catch (error) {
  63. console.error('獲取開屏圖片失敗:', error)
  64. return ''
  65. }
  66. }
  67. },
  68. mounted() {
  69. this.initSplash()
  70. uni.hideTabBar()
  71. },
  72. beforeDestroy() {
  73. this.clearTimer()
  74. },
  75. methods: {
  76. // 檢查是否為H5环境
  77. isH5() {
  78. // #ifdef H5
  79. return true
  80. // #endif
  81. // #ifndef H5
  82. return false
  83. // #endif
  84. },
  85. // 檢查是否已显示过开屏
  86. hasShownSplash() {
  87. if (this.isH5()) {
  88. try {
  89. return sessionStorage.getItem('splash_shown') === 'true'
  90. } catch (error) {
  91. console.error('读取sessionStorage失败:', error)
  92. return false
  93. }
  94. }
  95. return false
  96. },
  97. // 设置开屏已显示标记
  98. setSplashShown() {
  99. if (this.isH5()) {
  100. try {
  101. sessionStorage.setItem('splash_shown', 'true')
  102. } catch (error) {
  103. console.error('设置sessionStorage失败:', error)
  104. }
  105. }
  106. },
  107. // 初始化开动頁面
  108. async initSplash() {
  109. try {
  110. // 在H5环境下检查是否已显示过开屏
  111. if (this.hasShownSplash()) {
  112. console.log('开屏已显示过,跳过开屏动画')
  113. this.$emit('close')
  114. this.closeSplash()
  115. return
  116. }
  117. // 等待一小段時間確保 store 數據加載完成
  118. await this.$nextTick()
  119. // 獲取圖片URL,優先使用配置,然後使用本地存儲
  120. let imageUrl = ''
  121. // 嘗試從配置獲取
  122. if (this.$store.state.configList && this.$store.state.configList['creen_image']) {
  123. imageUrl = this.configParamContent('creen_image')
  124. }
  125. // 如果配置中沒有,嘗試從本地存儲獲取
  126. if (!imageUrl) {
  127. imageUrl = uni.getStorageSync('screen_image')
  128. }
  129. console.log('開屏圖片URL:', imageUrl)
  130. // 如果有圖片URL才顯示開屏
  131. if (imageUrl && imageUrl !== 'undefined' && imageUrl !== '') {
  132. this.splashContent = imageUrl
  133. this.countdown = this.duration
  134. this.showSplash = true
  135. this.startCountdown()
  136. } else {
  137. console.log('沒有開屏圖片,跳過開屏動畫')
  138. this.$emit('close')
  139. }
  140. } catch (error) {
  141. console.error('獲取開動頁面內容失敗:', error)
  142. // 發生錯誤時直接關閉開屏
  143. this.$emit('close')
  144. }
  145. },
  146. // 開始倒計時
  147. startCountdown() {
  148. this.timer = setInterval(() => {
  149. this.countdown--
  150. if (this.countdown <= 0) {
  151. this.closeSplash()
  152. }
  153. }, 1000)
  154. },
  155. // 關閉開動頁面
  156. closeSplash() {
  157. this.showSplash = false
  158. // 在H5环境下设置开屏已显示标记
  159. this.setSplashShown()
  160. uni.showTabBar({
  161. animation: true
  162. })
  163. this.clearTimer()
  164. this.$emit('close')
  165. },
  166. // 清除計時器
  167. clearTimer() {
  168. if (this.timer) {
  169. clearInterval(this.timer)
  170. this.timer = null
  171. }
  172. },
  173. // 圖片加載成功
  174. onImageLoad() {
  175. console.log('開屏圖片加載成功')
  176. },
  177. // 圖片加載失敗
  178. onImageError(e) {
  179. console.error('開屏圖片加載失敗:', e)
  180. // 圖片加載失敗時直接關閉開屏
  181. this.closeSplash()
  182. }
  183. }
  184. }
  185. </script>
  186. <style lang="scss" scoped>
  187. .splash-screen {
  188. position: fixed;
  189. top: 0;
  190. left: 0;
  191. width: 100vw;
  192. height: 100vh;
  193. background-color: #fff;
  194. z-index: 9999;
  195. display: flex;
  196. flex-direction: column;
  197. .splash-image{
  198. width: 100%;
  199. height: 100%;
  200. }
  201. }
  202. .splash-content {
  203. flex: 1;
  204. width: 100%;
  205. height: 80%;
  206. overflow: hidden;
  207. // 富文本樣式
  208. :deep(rich-text) {
  209. width: 100%;
  210. height: 100%;
  211. display: block;
  212. img{
  213. width: 100%;
  214. height: 100%;
  215. }
  216. }
  217. }
  218. .skip-button {
  219. position: absolute;
  220. top: 40rpx;
  221. left: 40rpx;
  222. background-color: #0000004D;
  223. border-radius: 100rpx;
  224. width: 148rpx ;
  225. height: 60rpx;
  226. text-align: center;
  227. z-index: 10000;
  228. .skip-text {
  229. font-size: 24rpx;
  230. line-height: 60rpx;
  231. color: #fff;
  232. }
  233. }
  234. // 文字容器定位到左下角
  235. .text-container {
  236. // position: absolute;
  237. // bottom: 80rpx;
  238. // left: 40rpx;
  239. // z-index: 10001;
  240. height: 20%;
  241. width: 100%;
  242. padding: 20rpx;
  243. display: flex;
  244. flex-direction: column;
  245. gap: 30rpx;
  246. }
  247. // 英文文字样式
  248. .english-text {
  249. font-family: PingFang SC;
  250. font-weight: 600;
  251. font-size: 32rpx; // 16px转换为rpx
  252. line-height: 48rpx; // 24px转换为rpx
  253. letter-spacing: 0;
  254. color: black;
  255. background: transparent;
  256. }
  257. // 中文文字样式
  258. .chinese-text {
  259. font-family: PingFang SC;
  260. // font-weight: 600;
  261. font-size: 32rpx; // 16px转换为rpx
  262. line-height: 48rpx; // 24px转换为rpx
  263. letter-spacing: 0;
  264. color: black;
  265. background: transparent;
  266. }
  267. // 確保覆蓋 tabbar
  268. .splash-screen {
  269. // 覆蓋所有內容,包括 tabbar
  270. position: fixed !important;
  271. z-index: 9999 !important;
  272. }
  273. </style>