li.shaoyi 2 vuotta sitten
vanhempi
commit
dbef08060c

+ 2 - 2
oem/sbyj/config/appconfig.json

@@ -1,8 +1,8 @@
 {
   "appId": "com.muchinfo.sbyj",
   "appName": "水贝亿爵",
-  "version": "1.0.2",
-  "versionCode": "100002",
+  "version": "1.0.3",
+  "versionCode": "100003",
   "apiUrl": "http://192.168.31.171:8080/cfg?key=test_171",
   "tradeChannel": "ws",
   "modules": [

+ 1 - 1
public/config/appconfig.json

@@ -1,6 +1,6 @@
 {
   "appId": "com.muchinfo.app",
-  "appName": "多元世纪交易中心",
+  "appName": "多元世纪",
   "version": "1.0.5",
   "versionCode": "10005",
   "apiUrl": "http://192.168.31.204:8080/cfg?key=test_204",

+ 36 - 0
src/packages/mobile/components/base/updater/index.less

@@ -0,0 +1,36 @@
+.app-updater {
+    &-message {
+        font-size: .32rem;
+        text-align: center;
+    }
+
+    &-progress {
+        position: relative;
+        display: flex;
+        justify-content: center;
+        align-items: center;
+        width: 100%;
+        height: .48rem;
+        background-color: #ebedf0;
+        border-radius: .24rem;
+        overflow: hidden;
+
+        &__bar {
+            position: absolute;
+            left: 0;
+            display: inline-block;
+            height: 100%;
+            background-color: #ff8400;
+        }
+
+        &__text {
+            z-index: 1;
+            font-size: .24rem;
+            color: #fff;
+        }
+    }
+
+    .van-dialog__content {
+        padding: .64rem;
+    }
+}

+ 123 - 0
src/packages/mobile/components/base/updater/index.vue

@@ -0,0 +1,123 @@
+<template>
+    <Dialog class="app-updater" v-model:show="show" :title="message ? '更新' : '下载中'" :show-confirm-button="showConfirmButton"
+        :confirm-button-text="confirmButtonText" show-cancel-button :before-close="onbeforeClose" @confirm="onConfirm"
+        @cancel="onCancel">
+        <div class="app-updater-message" v-if="message">
+            <span>{{ message }}</span>
+        </div>
+        <div class="app-updater-progress" v-else>
+            <span class="app-updater-progress__bar" :style="styles"></span>
+            <span class="app-updater-progress__text">{{ downloadProgress.toFixed(0) }}%</span>
+        </div>
+    </Dialog>
+</template>
+
+<script lang="ts" setup>
+import { shallowRef, computed, onMounted, onUnmounted } from 'vue'
+import { Dialog } from 'vant'
+import { versionToNumber } from '@/filters'
+import { getAppUpdateInfo } from '@/services/api/common'
+import plus from '@/utils/h5plus'
+
+const props = defineProps({
+    iosUpdateUrl: String
+})
+
+const show = shallowRef(false)
+const showConfirmButton = shallowRef(true)
+const message = shallowRef('')
+const confirmButtonText = shallowRef('')
+const downloadUrl = shallowRef('') // 下载地址
+const downloadProgress = shallowRef(0) // 下载进度
+const fileUrl = shallowRef('') // 文件安装地址
+
+const styles = computed(() => ({
+    width: downloadProgress.value + '%'
+}))
+
+// 监听下载进度
+const ondownload = plus.onDownloadProgress((progress) => {
+    downloadProgress.value = progress
+})
+
+const onbeforeClose = () => {
+    return false
+}
+
+const onConfirm = () => {
+    const os = plus.getSystemInfo('os')
+    if (os === 'iOS') {
+        show.value = false
+        plus.openURL(downloadUrl.value)
+    } else {
+        if (downloadProgress.value === 100) {
+            show.value = false
+            plus.installApp(fileUrl.value)
+        } else {
+            message.value = ''
+            showConfirmButton.value = false
+            plus.createDownload(downloadUrl.value).then((res) => {
+                fileUrl.value = res.filename
+                message.value = '下载成功,是否立即安装?'
+                confirmButtonText.value = '安装'
+                showConfirmButton.value = true
+            }).catch((err) => {
+                message.value = err
+            })
+        }
+    }
+}
+
+const onCancel = () => {
+    if (fileUrl.value) {
+        plus.deleteFile(fileUrl.value)
+    }
+    ondownload.cancel()
+    show.value = false
+}
+
+onMounted(() => {
+    const os = plus.getSystemInfo('os')
+    const currentVersion = plus.getSystemInfo('version')
+    const currentVersionCode = plus.getSystemInfo('versionCode')
+
+    if (os === 'Android') {
+        // 获取应用更新信息
+        getAppUpdateInfo().then((res) => {
+            const data = JSON.parse(res)
+            if (data) {
+                const { LastVersionCode, ApkUrl } = data[0] as Model.AppUpdateInfo
+                if (Number(LastVersionCode) > Number(currentVersionCode)) {
+                    downloadUrl.value = ApkUrl
+                    message.value = '发现新版本,是否下载?'
+                    confirmButtonText.value = '下载'
+                    show.value = true
+                }
+            }
+        })
+    }
+
+    if (os === 'iOS' && props.iosUpdateUrl) {
+        plus.httpRequest({
+            url: props.iosUpdateUrl
+        }).then((res) => {
+            const results = res.data.results
+            if (results?.length) {
+                const { version, trackViewUrl } = results[0]
+                if (versionToNumber(version) > versionToNumber(currentVersion)) {
+                    downloadUrl.value = trackViewUrl
+                    message.value = '发现新版本,是否更新?'
+                    confirmButtonText.value = '更新'
+                    show.value = true
+                }
+            }
+        })
+    }
+})
+
+onUnmounted(() => ondownload.cancel())
+</script>
+
+<style lang="less">
+@import './index.less';
+</style>

+ 4 - 65
src/packages/mobile/components/layouts/home/index.vue

@@ -9,22 +9,21 @@
       </RouterTransition>
     </router-view>
     <app-tabbar :data-list="tabs" :data-index="currentTab" @click="onTabClick" />
+    <app-updater :ios-update-url="iosUpdateUrl" />
   </div>
 </template>
-  
+
 <script lang="ts" setup>
 import { shallowRef, nextTick, watch, onMounted, onUnmounted, computed, PropType } from 'vue'
 import { closeToast } from 'vant'
-import { showLoading, fullloading, dialog } from '@/utils/vant'
-import { versionToNumber } from '@/filters'
+import { showLoading, fullloading } from '@/utils/vant'
 import { Tabbar } from '@mobile/components/base/tabbar/types'
-import { getAppUpdateInfo } from '@/services/api/common'
 import { useNavigation } from '@mobile/router/navigation'
 import { useLogin } from '@/business/login'
 import { useLoginStore } from '@/stores'
-import plus from '@/utils/h5plus'
 import eventBus from '@/services/bus'
 import AppTabbar from '@mobile/components/base/tabbar/index.vue'
+import AppUpdater from '@mobile/components/base/updater/index.vue'
 import RouterTransition from '@mobile/components/base/router-transition/index.vue'
 
 const props = defineProps({
@@ -97,66 +96,6 @@ onMounted(() => {
   } else {
     currentTab.value = tabIndex.value
   }
-
-  const os = plus.getSystemInfo('os')
-  const currentVersion = plus.getSystemInfo('version')
-  const currentVersionCode = plus.getSystemInfo('versionCode')
-
-  if (os === 'Android') {
-    // 监听下载进度
-    const ondownload = plus.onDownload((filename, progress) => {
-      if (progress === 100) {
-        dialog({
-          message: '新版本下载完成,是否安装?',
-          showCancelButton: true,
-          confirmButtonText: '安装'
-        }).then(() => {
-          plus.installApp(filename)
-        }).catch(() => {
-          plus.deleteFile(filename)
-        })
-      }
-    })
-
-    // 获取应用更新信息
-    getAppUpdateInfo().then((res) => {
-      const data = JSON.parse(res)
-      if (data) {
-        const { LastVersionCode, ApkUrl } = data[0] as Model.AppUpdateInfo
-        if (Number(LastVersionCode) > Number(currentVersionCode)) {
-          dialog({
-            message: '发现新版本,是否下载?',
-            showCancelButton: true,
-            confirmButtonText: '下载'
-          }).then(() => {
-            plus.createDownload(ApkUrl)
-          }).catch(() => {
-            ondownload.cancel()
-          })
-        }
-      }
-    })
-  }
-
-  if (os === 'iOS' && props.iosUpdateUrl) {
-    plus.httpRequest({
-      url: props.iosUpdateUrl
-    }).then((res) => {
-      const results = res.data.results
-      if (results?.length) {
-        const { version, trackViewUrl } = results[0]
-        if (versionToNumber(version) > versionToNumber(currentVersion)) {
-          dialog({
-            message: '发现新版本,是否更新?',
-            showCancelButton: true,
-            confirmButtonText: '更新'
-          }).then(() => {
-            plus.openURL(trackViewUrl)
-          })
-        }
-      }
-    })
-  }
 })
 
 onUnmounted(() => {

+ 2 - 2
src/packages/mobile/router/navigation.ts

@@ -70,10 +70,10 @@ export function useNavigation() {
     // }
 
     // 返回主页
-    const backHome = () => {
+    const backHome = (isLogout = false) => {
         const state = animateRouter.getState()
         const delta = state.historyRoutes.length - 1
-        const params = { tabIndex: 0 }
+        const params = { tabIndex: 0, isLogout }
 
         if (delta) {
             setGlobalUrlParams(params)

+ 3 - 77
src/packages/sbyj/views/home/index.vue

@@ -9,19 +9,19 @@
       </RouterTransition>
     </router-view>
     <app-tabbar class="home-tabbar" :data-list="tabList" :data-index="currentTab" @click="onTabClick" />
+    <app-updater />
   </div>
 </template>
 
 <script lang="ts" setup>
 import { shallowRef, nextTick, watch, onMounted ,computed} from 'vue'
-import { fullloading, dialog } from '@/utils/vant'
+import { fullloading } from '@/utils/vant'
 import { Tabbar } from '@mobile/components/base/tabbar/types'
-import { getAppUpdateInfo } from '@/services/api/common'
 import { useNavigation } from '@mobile/router/navigation'
 import { useLogin } from '@/business/login'
 import { useLoginStore } from '@/stores'
-import plus from '@/utils/h5plus'
 import AppTabbar from '@mobile/components/base/tabbar/index.vue'
+import AppUpdater from '@mobile/components/base/updater/index.vue'
 import RouterTransition from '@mobile/components/base/router-transition/index.vue'
 
 const { route, routerTo, getGlobalUrlParams, setGlobalUrlParams } = useNavigation()
@@ -80,82 +80,8 @@ const onTabClick = (index: number) => {
   }
 }
 
-// 版本号转数值
-const versionToNumber = (value: number | string) => {
-  if (value) {
-    const num = value.toString().split(/\D/)
-    // 版本号位数
-    const place = ['', '0', '00', '000', '0000', '00000', '000000'].reverse()
-    for (let i = 0; i < num.length; i++) {
-      const len = num[i].length
-      num[i] = place[len] + num[i]
-    }
-    return +num.join('')
-  }
-  return 0
-}
-
 onMounted(() => {
   currentTab.value = tabIndex.value
-  const os = plus.getSystemInfo('os')
-  const currentVersion = plus.getSystemInfo('version')
-  const currentVersionCode = plus.getSystemInfo('versionCode')
-
-  if (os === 'Android') {
-    // 监听下载进度
-    const ondownload = plus.onDownload((filename, progress) => {
-      if (progress === 100) {
-        dialog({
-          message: '新版本下载完成,是否安装?',
-          showCancelButton: true,
-          confirmButtonText: '安装'
-        }).then(() => {
-          plus.installApp(filename)
-        }).catch(() => {
-          plus.deleteFile(filename)
-        })
-      }
-    })
-
-    // 获取应用更新信息
-    getAppUpdateInfo().then((res) => {
-      const data = JSON.parse(res)
-      if (data) {
-        const { LastVersionCode, ApkUrl } = data[0] as Model.AppUpdateInfo
-        if (Number(LastVersionCode) > Number(currentVersionCode)) {
-          dialog({
-            message: '发现新版本,是否下载?',
-            showCancelButton: true,
-            confirmButtonText: '下载'
-          }).then(() => {
-            plus.createDownload(ApkUrl)
-          }).catch(() => {
-            ondownload.cancel()
-          })
-        }
-      }
-    })
-  }
-
-  if (os === 'iOS') {
-    // plus.httpRequest({
-    //   url: ''
-    // }).then((res) => {
-    //   const results = res.data.results
-    //   if (results?.length) {
-    //     const { version, trackViewUrl } = results[0]
-    //     if (versionToNumber(version) > versionToNumber(currentVersion)) {
-    //       dialog({
-    //         message: '发现新版本,是否更新?',
-    //         showCancelButton: true,
-    //         confirmButtonText: '更新'
-    //       }).then(() => {
-    //         plus.openURL(trackViewUrl)
-    //       })
-    //     }
-    //   }
-    // })
-  }
 })
 
 watch(() => route.name, () => {

+ 37 - 38
src/utils/h5plus/index.ts

@@ -1,6 +1,6 @@
 /* eslint-disable */
 import { v4 } from 'uuid'
-import { SystemInfo, ShareMessage, HttpRequestConfig } from './types'
+import { SystemInfo, ShareMessage, HttpRequestConfig, Download } from './types'
 import { urlScheme } from './constants'
 
 declare global {
@@ -29,9 +29,9 @@ export default new (class {
     private xhr = new XMLHttpRequest()
 
     /**
-     * 当前下载任务
+     * 当前下载进度任务
      */
-    private downloadTask = new Map()
+    private progressTask = new Map()
 
     /**
      * 系统信息
@@ -223,14 +223,14 @@ export default new (class {
      * @param callback 
      * @returns 
      */
-    onDownload(callback: (filename: string, progress: number) => void) {
+    onDownloadProgress(callback: (progress: number) => void) {
         const uuid = v4()
-        this.downloadTask.set(uuid, callback)
+        this.progressTask.set(uuid, callback)
 
         /** 注意离开页面时销毁监听事件,防止事件重复触发 */
         return {
             uuid,
-            cancel: () => this.downloadTask.delete(uuid)
+            cancel: () => this.progressTask.delete(uuid)
         }
     }
 
@@ -240,40 +240,39 @@ export default new (class {
      * @param url 
      */
     createDownload(url: string) {
-        this.onPlusReady((plus) => {
-            // plus.downloader.enumerate((downloads: any) => {
-            //     if (downloads.length) {
-            //         plus.nativeUI.toast('正在下载')
-            //     } else {
-
-            //     }
-            // })
-            const task = plus.downloader.createDownload(url, {
-                filename: '_downloads/', // 非系统 Download 目录
-                retry: 1,
-            }, (d: any, status: number) => {
-                if (status !== 200) {
-                    plus.nativeUI.toast('下载失败,请稍后再试')
-                }
-            })
-            // 监听下载状态
-            task.addEventListener('statechanged', (task: any) => {
-                console.log(task.state, task.downloadedSize / task.totalSize * 100)
-                switch (task.state) {
-                    case 3:
-                        const progress = task.downloadedSize / task.totalSize * 100
-                        for (const fn of this.downloadTask.values()) {
-                            fn(task.filename, progress) // 推送下载进度
+        return new Promise<Download>((resolve, reject) => {
+            this.onPlusReady((plus) => {
+                // plus.downloader.enumerate((downloads: any) => {
+                //     if (downloads.length) {
+                //         plus.nativeUI.toast('正在下载')
+                //     } else {
+
+                //     }
+                // })
+                const task = plus.downloader.createDownload(url, {
+                    filename: '_downloads/', // 非系统 Download 目录
+                    retry: 1,
+                }, (download: Download, status: number) => {
+                    if (status === 200) {
+                        resolve(download)
+                    } else {
+                        reject('下载失败,请稍后再试')
+                    }
+                    this.progressTask.clear()
+                })
+                // 监听下载状态
+                task.addEventListener('statechanged', (e: Download) => {
+                    //console.log(e.state, e.downloadedSize / e.totalSize * 100)
+                    if (e.state === 3) {
+                        const progress = e.downloadedSize / e.totalSize * 100
+                        for (const fn of this.progressTask.values()) {
+                            fn(progress) // 推送下载进度
                         }
-                        break
-                    case 4:
-                        console.log('下载完成', task.filename)
-                        this.downloadTask.clear()
-                        break
-                }
+                    }
+                })
+                // 开始下载
+                task.start()
             })
-            // 开始下载
-            task.start()
         })
     }
 

+ 9 - 0
src/utils/h5plus/types.ts

@@ -59,4 +59,13 @@ export interface HttpRequestConfig {
     method?: string;
     responseType?: XMLHttpRequestResponseType;
     header?: { [key: string]: string };
+}
+
+export interface Download {
+    id: string;
+    url: string;
+    state: number;
+    filename: string;
+    downloadedSize: number;
+    totalSize: number;
 }