四零语境前端代码仓库
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.

304 lines
6.6 KiB

1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 week 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. return
  115. }
  116. // 等待一小段時間確保 store 數據加載完成
  117. await this.$nextTick()
  118. // 獲取圖片URL,優先使用配置,然後使用本地存儲
  119. let imageUrl = ''
  120. // 嘗試從配置獲取
  121. if (this.$store.state.configList && this.$store.state.configList['creen_image']) {
  122. imageUrl = this.configParamContent('creen_image')
  123. }
  124. // 如果配置中沒有,嘗試從本地存儲獲取
  125. if (!imageUrl) {
  126. imageUrl = uni.getStorageSync('screen_image')
  127. }
  128. console.log('開屏圖片URL:', imageUrl)
  129. // 如果有圖片URL才顯示開屏
  130. if (imageUrl && imageUrl !== 'undefined' && imageUrl !== '') {
  131. this.splashContent = imageUrl
  132. this.countdown = this.duration
  133. this.showSplash = true
  134. this.startCountdown()
  135. } else {
  136. console.log('沒有開屏圖片,跳過開屏動畫')
  137. this.$emit('close')
  138. }
  139. } catch (error) {
  140. console.error('獲取開動頁面內容失敗:', error)
  141. // 發生錯誤時直接關閉開屏
  142. this.$emit('close')
  143. }
  144. },
  145. // 開始倒計時
  146. startCountdown() {
  147. this.timer = setInterval(() => {
  148. this.countdown--
  149. if (this.countdown <= 0) {
  150. this.closeSplash()
  151. }
  152. }, 1000)
  153. },
  154. // 關閉開動頁面
  155. closeSplash() {
  156. this.showSplash = false
  157. // 在H5环境下设置开屏已显示标记
  158. this.setSplashShown()
  159. uni.showTabBar({
  160. animation: true
  161. })
  162. this.clearTimer()
  163. this.$emit('close')
  164. },
  165. // 清除計時器
  166. clearTimer() {
  167. if (this.timer) {
  168. clearInterval(this.timer)
  169. this.timer = null
  170. }
  171. },
  172. // 圖片加載成功
  173. onImageLoad() {
  174. console.log('開屏圖片加載成功')
  175. },
  176. // 圖片加載失敗
  177. onImageError(e) {
  178. console.error('開屏圖片加載失敗:', e)
  179. // 圖片加載失敗時直接關閉開屏
  180. this.closeSplash()
  181. }
  182. }
  183. }
  184. </script>
  185. <style lang="scss" scoped>
  186. .splash-screen {
  187. position: fixed;
  188. top: 0;
  189. left: 0;
  190. width: 100vw;
  191. height: 100vh;
  192. background-color: #fff;
  193. z-index: 9999;
  194. display: flex;
  195. flex-direction: column;
  196. .splash-image{
  197. width: 100%;
  198. height: 100%;
  199. }
  200. }
  201. .splash-content {
  202. flex: 1;
  203. width: 100%;
  204. height: 80%;
  205. overflow: hidden;
  206. // 富文本樣式
  207. :deep(rich-text) {
  208. width: 100%;
  209. height: 100%;
  210. display: block;
  211. img{
  212. width: 100%;
  213. height: 100%;
  214. }
  215. }
  216. }
  217. .skip-button {
  218. position: absolute;
  219. top: 40rpx;
  220. left: 40rpx;
  221. background-color: #0000004D;
  222. border-radius: 100rpx;
  223. width: 148rpx ;
  224. height: 60rpx;
  225. text-align: center;
  226. z-index: 10000;
  227. .skip-text {
  228. font-size: 24rpx;
  229. line-height: 60rpx;
  230. color: #fff;
  231. }
  232. }
  233. // 文字容器定位到左下角
  234. .text-container {
  235. // position: absolute;
  236. // bottom: 80rpx;
  237. // left: 40rpx;
  238. // z-index: 10001;
  239. height: 20%;
  240. width: 100%;
  241. padding: 20rpx;
  242. display: flex;
  243. flex-direction: column;
  244. gap: 30rpx;
  245. }
  246. // 英文文字样式
  247. .english-text {
  248. font-family: PingFang SC;
  249. font-weight: 600;
  250. font-size: 32rpx; // 16px转换为rpx
  251. line-height: 48rpx; // 24px转换为rpx
  252. letter-spacing: 0;
  253. color: black;
  254. background: transparent;
  255. }
  256. // 中文文字样式
  257. .chinese-text {
  258. font-family: PingFang SC;
  259. // font-weight: 600;
  260. font-size: 32rpx; // 16px转换为rpx
  261. line-height: 48rpx; // 24px转换为rpx
  262. letter-spacing: 0;
  263. color: black;
  264. background: transparent;
  265. }
  266. // 確保覆蓋 tabbar
  267. .splash-screen {
  268. // 覆蓋所有內容,包括 tabbar
  269. position: fixed !important;
  270. z-index: 9999 !important;
  271. }
  272. </style>