AttachmentDownload.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. <template>
  2. <section class="attachment-download-container" v-if="renderAttachList.length">
  3. <div class="others-header flex flex-(items-center)">
  4. <div class="content-file-attachment-left flex flex-items-center">
  5. <span class="left-icon flex flex-items-center">
  6. <span class="file-attachment-text text-nowrap">附件下载</span>
  7. </span>
  8. <div class="right-content flex flex-items-center" v-if="canShowTip">
  9. <!-- 免费用户,无体验次数(没体验过) -->
  10. <template v-if="isFree && freeFileNum === 0">
  11. <span class="attachment-tag text-nowrap">
  12. <span class="attachment-tag-text">
  13. 免费用户享有{{ freeFileNum || 1 }}次附件下载权益
  14. </span>
  15. </span>
  16. </template>
  17. <!-- 免费用户,无体验次数(体验过) -->
  18. <template v-else-if="isFree && freeFileNum < 0">
  19. <span class="attachment-tag text-nowrap">
  20. <span class="attachment-tag-text">下载更多附件</span>
  21. <button class="open-vip-btn" @click="toBuySvip">
  22. 开通超级订阅
  23. </button>
  24. </span>
  25. </template>
  26. <!-- 新超级订阅,并且不是大会员(或者是大会员没有附件下载权限) -->
  27. <template v-else-if="isNewSuper && !memberHasAttachPower">
  28. <span class="attachment-tag text-nowrap">
  29. <span class="attachment-tag-text">本月剩余:{{ fileNum }}个</span>
  30. </span>
  31. <i class="iconfont icon-help" @click="fileDownloadHelp"></i>
  32. </template>
  33. </div>
  34. </div>
  35. <div class="content-file-attachment-actions" v-if="canShowTip">
  36. <span
  37. class="action-button"
  38. @click="chargeFilePack"
  39. v-if="isNewSuper && !memberHasAttachPower"
  40. >
  41. 立即充值
  42. </span>
  43. </div>
  44. </div>
  45. <div class="file-attachment-list">
  46. <div
  47. class="file-attachment-item highlight-text underline clickable"
  48. v-for="(attach, index) in renderAttachList"
  49. @click="startDownloadFile(attach)"
  50. :key="index"
  51. >
  52. {{ attach.name }}
  53. </div>
  54. </div>
  55. </section>
  56. </template>
  57. <script>
  58. import { mapState, mapGetters } from 'vuex'
  59. import { useGetContentAttachment } from '@/composables/attachment-download/'
  60. import {
  61. IsCustomTopNet,
  62. IsSunPublishContent
  63. } from '@/views/article-content/composables/useContentStore'
  64. export default {
  65. name: 'AttachmentDownload',
  66. props: {
  67. id: {
  68. type: String,
  69. required: true,
  70. default: ''
  71. },
  72. type: {
  73. type: String,
  74. default: ''
  75. },
  76. title: {
  77. type: String,
  78. required: true,
  79. default: ''
  80. },
  81. attachmentList: {
  82. type: Array,
  83. default() {
  84. return [
  85. // {
  86. // fileName: '附件1.pdf',
  87. // fileSize: '1.9 M',
  88. // fileType: 'pdf'
  89. // },
  90. // {
  91. // fileName: '附件2.pdf',
  92. // fileSize: '129 KB',
  93. // fileType: 'pdf'
  94. // }
  95. ]
  96. }
  97. }
  98. },
  99. data() {
  100. return {
  101. loading: false,
  102. attachment: {
  103. fileUrl: '', // 当前附件真实url
  104. file: {}, // 当前需要下载的附件信息
  105. downloaded: false
  106. },
  107. resourcePack: {
  108. exchangeNum: 0,
  109. freeNum: 0,
  110. grantNum: 0,
  111. name: '附件下载包',
  112. number: 0,
  113. purchaseNum: 0,
  114. resourceType: '附件下载包',
  115. thirtyNum: 0
  116. }
  117. }
  118. },
  119. computed: {
  120. ...mapGetters('user', [
  121. 'isFree',
  122. 'isSuper',
  123. 'isMember',
  124. 'bigMemberPower',
  125. 'isBusiness'
  126. ]),
  127. ...mapState({
  128. power: (state) => state.user.info
  129. }),
  130. // 免费用户免费体验次数
  131. freeFileNum() {
  132. if (this.resourcePack.number > 0) {
  133. return this.resourcePack.number
  134. } else {
  135. if (this.attachment.downloaded) {
  136. return -1
  137. } else {
  138. return this.power?.freeFile
  139. }
  140. }
  141. },
  142. fileNum() {
  143. return this.resourcePack.number
  144. },
  145. isNewSuper() {
  146. return this.power.viper && this.isSuper
  147. },
  148. memberHasAttachPower() {
  149. return this.isMember && this.bigMemberPower.indexOf(3) !== -1
  150. },
  151. renderAttachList() {
  152. return this.attachmentList.map((a) => {
  153. let size = a.fileSize
  154. return {
  155. name: a.fileName,
  156. size: size,
  157. type: a.fileType
  158. }
  159. })
  160. },
  161. canShowTip() {
  162. // 定制化用户不展示提示和留资
  163. if (IsCustomTopNet.value) {
  164. return false
  165. }
  166. if (IsSunPublishContent.value) {
  167. return false
  168. }
  169. return true
  170. }
  171. },
  172. created() {
  173. this.getInfo()
  174. },
  175. methods: {
  176. replaceSpace(n) {
  177. return n.trim().replace(/\s+/g, '')
  178. },
  179. async getInfo() {
  180. const { attachment } = useGetContentAttachment({ id: this.id })
  181. this.attachmentInstance = attachment
  182. await this.attachmentInstance.getResourcePackAccount()
  183. this.resourcePack = this.attachmentInstance.resourcePack
  184. },
  185. async refreshResourcePackCount() {
  186. if (!this.attachmentInstance) {
  187. return this.getInfo()
  188. }
  189. await this.attachmentInstance.getResourcePackAccount()
  190. this.resourcePack = this.attachmentInstance.resourcePack
  191. },
  192. showDialog(conf = {}) {
  193. const defaultConf = {
  194. title: '',
  195. message: '',
  196. customClass: 'custom-message-box',
  197. confirmButtonText: '我知道了',
  198. confirmButtonClass: 'custom-confirm-btn',
  199. cancelButtonClass: 'custom-cancel-btn',
  200. showClose: false,
  201. showCancelButton: false,
  202. closeOnClickModal: false,
  203. center: true
  204. }
  205. Object.assign(defaultConf, conf)
  206. return this.$confirm(defaultConf.message, defaultConf.title, defaultConf)
  207. },
  208. fileDownloadHelp() {
  209. this.$alert(
  210. '点击附件即为下载,系统会扣除当月附件下载个数;每月1号上月余额清零重新计算,请合理使用。',
  211. '',
  212. {
  213. confirmButtonText: '我知道了',
  214. confirmButtonColor: '#2ABDD1',
  215. showClose: false,
  216. center: true
  217. }
  218. )
  219. },
  220. async startDownloadFile(file) {
  221. // 定制化用户直接下载
  222. if (IsCustomTopNet.value) {
  223. return this.downloadFile(file)
  224. }
  225. if (IsSunPublishContent.value) {
  226. return this.downloadFile(file)
  227. }
  228. // 大客户直接下载
  229. if (location.pathname.indexOf('entservice') !== -1) {
  230. return this.downloadFile(file)
  231. }
  232. if (this.isFree) {
  233. // 免费用户
  234. // 判断有无体验过 0:未体验过
  235. if (this.freeFileNum === 0) {
  236. // TODO 判断有无留过资 且未体验过 - 去留资 source: 'article_attach_freeuser'
  237. this.$emit('doOpenCollect', {
  238. source: 'article_attach_freeuser',
  239. reload: true
  240. })
  241. } else if (this.freeFileNum < 0 && this.resourcePack.number <= 0) {
  242. // 免费用户 体验过 下载次数为-1 弹框提醒跳至超级订阅购买页
  243. // 并且剑鱼币兑换的附件下载权益没有余额
  244. return this.showDialog({
  245. title: '开通超级订阅',
  246. message:
  247. '您的免费【附件下载】次数已使用完,暂无免费查看权限。如需查看更多,请开通超级订阅获取更多权限。',
  248. showCancelButton: true,
  249. confirmButtonText: '去开通'
  250. })
  251. .then(() => {
  252. this.toBuySvip()
  253. })
  254. .catch(() => {})
  255. } else {
  256. // P317版本改为免费用户只要有下载次数,均可正常下载
  257. this.downloadFile(file)
  258. this.attachment.downloaded = true
  259. }
  260. } else {
  261. // 付费用户
  262. // 大会员用户 有下载个数
  263. if (this.memberHasAttachPower) {
  264. return this.downloadFile(file)
  265. }
  266. // 超级订阅用户
  267. if (this.isSuper) {
  268. // 新超级订阅用户
  269. if (this.isNewSuper) {
  270. // 是否用完弹窗放到请求之后,根据请求返回值进行判断
  271. return this.downloadFile(file)
  272. // if (this.fileNum > 0) {
  273. // this.downloadFile(file)
  274. // } else {
  275. // // 次数用完
  276. // return this.showDialog({
  277. // message:
  278. // '您本月附件下载机会已消耗完毕,如需下载更多附件,请前往充值。',
  279. // showCancelButton: true,
  280. // confirmButtonText: '立即充值'
  281. // })
  282. // .then(() => {
  283. // // this.concatKf()
  284. // this.chargeFilePack()
  285. // })
  286. // .catch(() => {})
  287. // }
  288. } else {
  289. // 老超级订阅用户 提醒升级
  290. return this.showDialog({
  291. title: '升级超级订阅',
  292. message: '对不起,暂无权限,您可升级超级订阅解锁附件下载',
  293. showCancelButton: true,
  294. confirmButtonText: '前往升级'
  295. })
  296. .then(() => {
  297. this.toUpgradeSvip()
  298. })
  299. .catch(() => {})
  300. }
  301. }
  302. // 商机管理只要有个数就能下载
  303. if (this.isBusiness && this.resourcePack.number > 0) {
  304. return this.downloadFile(file)
  305. }
  306. // 大会员自定义版本没有下载权限 或 非超级订阅的商机管理用户 (弹框提醒联系客服)
  307. const isMemberButNoPower = this.isMember && !this.memberHasAttachPower
  308. const noAttachmentDownloadPower = isMemberButNoPower && !this.isNewSuper
  309. if (noAttachmentDownloadPower || (!this.isSuper && this.isBusiness)) {
  310. // 老超级订阅用户 提醒升级
  311. return this.showDialog({
  312. message:
  313. '您未购买此服务,如需使用请联系您的客户经理或客服升级套餐,谢谢!',
  314. confirmButtonText: '我知道了'
  315. })
  316. .then(() => {})
  317. .catch(() => {})
  318. }
  319. }
  320. },
  321. async downloadFile(file) {
  322. this.attachment.file = file
  323. // downUrl: 原始url
  324. // fileUrl: 下载地址
  325. const result = await this.getAttachmentInfo(file)
  326. if (result) {
  327. const { downUrl, fileUrl } = result
  328. let skipRefresh = false
  329. // 定制化用户不展示提示和留资
  330. if (IsCustomTopNet.value) {
  331. skipRefresh = true
  332. }
  333. if (IsSunPublishContent.value) {
  334. skipRefresh = true
  335. }
  336. if (!skipRefresh) {
  337. this.refreshResourcePackCount()
  338. }
  339. if (downUrl && fileUrl) {
  340. this._downloadFile(fileUrl, result.fileName)
  341. } else {
  342. console.log('获取附件fid失败')
  343. }
  344. }
  345. },
  346. _downloadFile(url, filename) {
  347. if (!url) return
  348. const link = document.createElement('a')
  349. link.style.display = 'none'
  350. link.href = url
  351. // 同源download才会生效
  352. link.setAttribute('download', filename)
  353. document.body.appendChild(link)
  354. link.click()
  355. document.body.removeChild(link)
  356. return link
  357. },
  358. async getAttachmentInfo(file) {
  359. this.loading = true
  360. if (!this.loading) return
  361. const params = {
  362. id: this.id, // 附件详情页id
  363. fileName: file.name, // 附件名称
  364. infoType: this.type === 'issued' ? 'S' : '', // 信息类型:默认为空; 供应信息:S
  365. productName: '附件下载包',
  366. platform: 'PC', // 平台:PC;APP;WX
  367. title: this.title // 附件详情页标题附件详情页标题
  368. }
  369. try {
  370. const { r: data, m: msg } =
  371. await this.attachmentInstance.useResourcePack(params)
  372. // 各种余额不足提示
  373. if (data) {
  374. if (data.code && data.code < 0) {
  375. if (this.isFree) {
  376. this.showDialog({
  377. title: '开通超级订阅',
  378. message:
  379. '您的免费【附件下载】次数已使用完,暂无免费查看权限。如需查看更多,请开通超级订阅获取更多权限。',
  380. showCancelButton: true,
  381. confirmButtonText: '去开通'
  382. })
  383. .then(() => {
  384. this.toBuySvip()
  385. })
  386. .catch(() => {})
  387. } else if (this.isSuper) {
  388. this.showDialog({
  389. title: '开通超级订阅',
  390. message:
  391. '您本月附件下载机会已消耗完毕,如需下载更多附件,请前往充值。',
  392. showCancelButton: true,
  393. confirmButtonText: '立即充值'
  394. })
  395. .then(() => {
  396. this.chargeFilePack()
  397. })
  398. .catch(() => {})
  399. } else {
  400. this.$toast(msg || '获取附件地址失败')
  401. }
  402. } else if (!msg && data.downUrl) {
  403. const downUrl = data.downUrl
  404. const fileUrl = downUrl
  405. ? `${downUrl}?response-content-type=application/octet-stream`
  406. : ''
  407. return {
  408. ...data,
  409. fileUrl // 真实下载地址
  410. }
  411. } else {
  412. this.$toast(msg || '获取附件地址失败')
  413. }
  414. } else {
  415. this.$toast(msg || '获取附件地址失败')
  416. }
  417. } catch (error) {
  418. console.log(error)
  419. } finally {
  420. this.loading = false
  421. }
  422. },
  423. toBuySvip() {
  424. window.open('/swordfish/page_big_pc/free/svip/buy', '_blank')
  425. },
  426. chargeFilePack() {
  427. window.open('/swordfish/page_big_pc/free/filePack/buy', '_blank')
  428. }
  429. }
  430. }
  431. </script>
  432. <style lang="scss" scoped>
  433. .attachment-download-container {
  434. margin-top: 40px;
  435. margin-bottom: 70px;
  436. }
  437. .file-attachment-text {
  438. margin-right: 12px;
  439. font-size: 16px;
  440. line-height: 24px;
  441. }
  442. .attachment-tag {
  443. margin: 0 8px;
  444. padding: 2px 0;
  445. font-size: 12px;
  446. line-height: 18px;
  447. color: $color-main;
  448. background: $color_main_background;
  449. border-radius: 10px;
  450. &-text {
  451. padding: 0 8px;
  452. color: $color-main;
  453. }
  454. }
  455. .action-button {
  456. flex-shrink: 0;
  457. display: inline-block;
  458. margin-left: 24px;
  459. text-align: center;
  460. cursor: pointer;
  461. padding: 0 16px;
  462. min-width: 64px;
  463. font-size: 12px;
  464. line-height: 30px;
  465. border-radius: 4px;
  466. color: #fff;
  467. background-color: $color-main;
  468. }
  469. .file-attachment-list {
  470. margin-top: 16px;
  471. font-size: 15px;
  472. line-height: 22px;
  473. .file-attachment-item {
  474. display: block;
  475. cursor: pointer;
  476. margin-bottom: 16px;
  477. }
  478. }
  479. .right-content {
  480. .iconfont {
  481. cursor: pointer;
  482. color: $color-main;
  483. }
  484. }
  485. .open-vip-btn {
  486. color: #fff;
  487. padding: 0 8px;
  488. border-radius: inherit;
  489. background-color: $color-main;
  490. }
  491. </style>