zhb 2 月之前
父節點
當前提交
24a8176b2c
共有 2 個文件被更改,包括 104 次插入34 次删除
  1. 88 19
      components/cwg-link.vue
  2. 16 15
      pages/common/download.vue

+ 88 - 19
components/cwg-link.vue

@@ -1,8 +1,33 @@
 <template>
-    <view class="pdf-link" @click="handleClick">
-        <slot v-if="slots['default']"></slot>
-        <view v-else >{{ t(title) || title }}</view>
-    </view>
+  <!-- #ifdef H5 -->
+  <!-- H5 端:支持普通跳转 / PDF预览 / 文件下载 -->
+  <a
+    v-if="isDownload"
+    class="pdf-link"
+    :href="url"
+    :download="downloadName"
+    target="_blank"
+  >
+    <slot v-if="slots['default']"></slot>
+    <view v-else>{{ t(title) || title }}</view>
+  </a>
+  <view
+    v-else
+    class="pdf-link"
+    @click="handleClick"
+  >
+    <slot v-if="slots['default']"></slot>
+    <view v-else>{{ t(title) || title }}</view>
+  </view>
+  <!-- #endif -->
+
+  <!-- #ifdef APP-PLUS -->
+  <!-- APP 端:统一点击处理 -->
+  <view class="pdf-link" @click="handleClick">
+    <slot v-if="slots['default']"></slot>
+    <view v-else>{{ t(title) || title }}</view>
+  </view>
+  <!-- #endif -->
 </template>
 
 <script setup>
@@ -10,32 +35,76 @@ import { useSlots } from 'vue'
 import { openLocalPdf } from '@/utils/pdf'
 import { openExternalUrl } from '@/utils/openExternalUrl'
 import { useI18n } from 'vue-i18n'
+
 const { t } = useI18n()
+const slots = useSlots()
 
 const props = defineProps({
-    url: String,
-    type: {
-        type: String,
-        default: 'pdf'
-    },
-    title: String,
-    target: String
+  url: String,
+  type: {
+    type: String,
+    default: 'pdf' // pdf / pdf1 / html / download
+  },
+  title: String,
+  target: String,
+  // 下载文件名(下载时必填)
+  downloadName: String
 })
-const slots = useSlots()
+
+// 判断是否是下载类型
+const isDownload = props.type === 'download'
 
 const handleClick = () => {
-    if (!props.url) return
-    if (props.type == 'pdf' || props.type == 'pdf1') {
-        openLocalPdf(props.url, props.title, props.type)
-    } else if (props.type === 'html') {
-        openExternalUrl(props.url)
+  if (!props.url) return
+
+  // 下载逻辑(APP端)
+  if (props.type === 'download') {
+    startDownload()
+    return
+  }
+
+  // PDF 预览
+  if (props.type == 'pdf' || props.type == 'pdf1') {
+    openLocalPdf(props.url, props.title, props.type)
+  }
+
+  // 外链打开
+  else if (props.type === 'html') {
+    openExternalUrl(props.url)
+  }
+}
+
+// APP 端下载方法
+const startDownload = () => {
+  uni.showLoading({ title: '下载中...', mask: true })
+
+  uni.downloadFile({
+    url: props.url,
+    success: (res) => {
+      uni.hideLoading()
+      if (res.statusCode === 200) {
+        uni.openDocument({
+          filePath: res.tempFilePath,
+          fileName: props.downloadName,
+          success: () => {
+            uni.showToast({ icon: 'none', title: '下载完成' })
+          }
+        })
+      } else {
+        uni.showToast({ icon: 'none', title: '下载失败' })
+      }
+    },
+    fail: () => {
+      uni.hideLoading()
+      uni.showToast({ icon: 'none', title: '下载链接异常' })
     }
+  })
 }
 </script>
 
 <style scoped>
 .pdf-link {
-    display: inline-block;
-    cursor: pointer;
+  display: inline-block;
+  cursor: pointer;
 }
 </style>

+ 16 - 15
pages/common/download.vue

@@ -11,8 +11,9 @@
                             <view class="section-subtitle" v-t="'Downloadpage.item3'" />
                         </view>
                         <view class="download-cards">
-                            <cwg-link type="html" class="download-card" :url="myLink + '/mt/cwgmarketssvgltd4setup.exe'"
-                                download="cwgmarketssvgltd4setup.exe">
+                            <cwg-link type="download" class="download-card"
+                                :url="myLink + '/mt/cwgmarketssvgltd4setup.exe'"
+                                downloadName="cwgmarketssvgltd4setup.exe">
                                 <image class="card-icon" src="/static/images/windows-os-1-48.png" alt=""
                                     mode="widthFix" />
                                 <view class="card-info">
@@ -21,8 +22,8 @@
                                 </view>
                                 <view class="download-badge">Windows</view>
                             </cwg-link>
-                            <cwg-link type="html" class="download-card" :url="myLink + '/mt/mt4.zip'"
-                                download="mt4.zip">
+                            <cwg-link type="download" class="download-card" :url="myLink + '/mt/mt4.zip'"
+                                downloadName="mt4.zip">
                                 <image class="card-icon" src="/static/images/windows-os-1-48.png" alt=""
                                     mode="widthFix" />
                                 <view class="card-info">
@@ -31,8 +32,8 @@
                                 </view>
                                 <view class="download-badge">ZIP</view>
                             </cwg-link>
-                            <cwg-link type="html" class="download-card" :url="myLink + '/mt/MetaTrader4.pkg'"
-                                download="MetaTrader4.pkg">
+                            <cwg-link type="download" class="download-card" :url="myLink + '/mt/MetaTrader4.pkg'"
+                                downloadName="MetaTrader4.pkg">
                                 <image class="card-icon" src="/static/images/apple-os-1-48.png" alt=""
                                     mode="widthFix" />
                                 <view class="card-info">
@@ -109,8 +110,8 @@
                             <view class="section-subtitle" v-t="'Downloadpage.item12'" />
                         </view>
                         <view class="download-cards">
-                            <cwg-link type="html" class="download-card" :url="myLink + '/mt/cwgmarketssvg5setup.exe'"
-                                download="cwgmarketssvg5setup.exe">
+                            <cwg-link type="download" class="download-card"
+                                :url="myLink + '/mt/cwgmarketssvg5setup.exe'" downloadName="cwgmarketssvg5setup.exe">
                                 <image class="card-icon" src="/static/images/windows-os-1-48.png" alt=""
                                     mode="widthFix" />
                                 <view class="card-info">
@@ -119,8 +120,8 @@
                                 </view>
                                 <view class="download-badge">Windows</view>
                             </cwg-link>
-                            <cwg-link type="html" class="download-card" :url="myLink + '/mt/mt5.zip'"
-                                download="mt5.zip">
+                            <cwg-link type="download" class="download-card" :url="myLink + '/mt/mt5.zip'"
+                                downloadName="mt5.zip">
                                 <image class="card-icon" src="/static/images/windows-os-1-48.png" alt=""
                                     mode="widthFix" />
                                 <view class="card-info">
@@ -129,8 +130,8 @@
                                 </view>
                                 <view class="download-badge">ZIP</view>
                             </cwg-link>
-                            <cwg-link type="html" class="download-card" :url="myLink + '/mt/MetaTrader5.pkg'"
-                                download="MetaTrader5.dmg">
+                            <cwg-link type="download" class="download-card" :url="myLink + '/mt/MetaTrader5.pkg'"
+                                downloadName="MetaTrader5.dmg">
                                 <image class="card-icon" src="/static/images/apple-os-1-48.png" alt=""
                                     mode="widthFix" />
                                 <view class="card-info">
@@ -206,19 +207,19 @@
                             <view class="banner-subtitle" v-t="'Downloadpage.item19'" />
                         </view>
                         <view class="banner-buttons">
-                            <cwg-link type="html" class="banner-btn" style="padding: 10px 25px;min-width: 220px;"
+                            <cwg-link type="download" class="banner-btn" style="padding: 10px 25px;min-width: 220px;"
                                 url="https://mt.tradingcentral.cn/download"
                                 v-if="['cn', 'zhHant'].indexOf(Session.Get('lang')) != -1">
                                 <view v-t="'Downloadpage.item20'" />
                                 <view class="btn-tag">中国大陆用户</view>
                             </cwg-link>
-                            <cwg-link type="html" class="banner-btn" style="padding: 10px 25px;min-width: 220px;"
+                            <cwg-link type="download" class="banner-btn" style="padding: 10px 25px;min-width: 220px;"
                                 url="https://mt.tradingcentral.com/download"
                                 v-if="['cn', 'zhHant'].indexOf(Session.Get('lang')) != -1">
                                 <view v-t="'Downloadpage.item20'" />
                                 <view class="btn-tag">港澳台地区及海外用户</view>
                             </cwg-link>
-                            <cwg-link type="html" class="banner-btn" url="https://mt.tradingcentral.com/download"
+                            <cwg-link type="download" class="banner-btn" url="https://mt.tradingcentral.com/download"
                                 v-if="['cn', 'zhHant'].indexOf(Session.Get('lang')) == -1">
                                 <span v-t="'Downloadpage.item20'"></span>
                             </cwg-link>