DownProjectReport.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <template>
  2. <TabActionItem
  3. show-text
  4. :direction="direction"
  5. class="download-project-doc"
  6. @click.native.stop="doAction"
  7. >
  8. <AppIcon slot="icon" name="xiazaixiangmubaogao" size="20" />
  9. <template #text>
  10. <EmailDialog
  11. v-model="dialog.show"
  12. :email.sync="dialog.email"
  13. title="发送邮箱地址"
  14. content-tip-text="项目报告文件将以邮件的形式发送至您的邮箱"
  15. :before-close="beforeEmailDialogClose"
  16. />
  17. <span>下载项目报告</span>
  18. </template>
  19. </TabActionItem>
  20. </template>
  21. <script>
  22. import { mapGetters } from 'vuex'
  23. import TabActionItem from '@/views/article/ui/TabActionItem.vue'
  24. import { AppIcon } from '@/ui'
  25. import { getProjectReport, sendProjectPdfFile } from '@/api/modules/'
  26. import EmailDialog from '@/components/common/EmailDialog.vue'
  27. import { emailRegExp } from '@/utils/constant/'
  28. import downloadApp from '@/utils/mixins/modules/to-download-app'
  29. export default {
  30. name: 'DownloadProjectReport',
  31. components: {
  32. TabActionItem,
  33. EmailDialog,
  34. AppIcon
  35. },
  36. mixins: [downloadApp],
  37. props: {
  38. id: {
  39. type: String,
  40. default: '',
  41. required: true
  42. },
  43. name: {
  44. type: String,
  45. default: '',
  46. required: true
  47. },
  48. direction: {
  49. type: String,
  50. default: 'row',
  51. validator(d) {
  52. return ['row', 'column'].includes(d)
  53. }
  54. },
  55. beforeAction: Function,
  56. beforeLeavePage: Function
  57. },
  58. data() {
  59. return {
  60. dialog: {
  61. show: false,
  62. email: ''
  63. },
  64. link: ''
  65. }
  66. },
  67. computed: {
  68. ...mapGetters('user', ['isFree'])
  69. },
  70. methods: {
  71. // 下载(下载前校验)
  72. async doAction() {
  73. if (this.beforeAction) {
  74. const r = await this.beforeAction()
  75. if (!r) {
  76. return
  77. }
  78. }
  79. if (this.isFree) {
  80. return this.goToSvipBuy()
  81. }
  82. if (this.$envs.inWxMini) {
  83. return this.toFollowGuidePage()
  84. }
  85. if (this.link) {
  86. return this.downFile(this.name, this.link)
  87. }
  88. const loading = this.$toast.loading({
  89. message: '报告正在生成,请稍后~'
  90. })
  91. try {
  92. const {
  93. data,
  94. error_code: code,
  95. error_msg: msg
  96. } = await getProjectReport({ sid: this.id })
  97. if (code === 0 && data) {
  98. this.link = data.path
  99. this.doDownload(this.name, this.link)
  100. loading.clear()
  101. } else {
  102. this.$toast(msg || '获取报告失败,请稍后再试')
  103. }
  104. } catch (error) {
  105. loading.clear()
  106. console.error(error)
  107. }
  108. },
  109. doDownload(name, link) {
  110. if (this.$envs.inIOS) {
  111. this.dialog.show = true
  112. } else {
  113. this.downFile(name, link)
  114. }
  115. },
  116. /**
  117. * 下载文件
  118. * @param name
  119. * @param url
  120. */
  121. downFile(name, url) {
  122. const a = document.createElement('a')
  123. const filename = name
  124. a.href = url
  125. a.target = '_blank'
  126. if (name) {
  127. a.download = filename
  128. }
  129. a.click()
  130. },
  131. async beforeEmailDialogClose(action, done) {
  132. if (action === 'cancel') {
  133. return done()
  134. }
  135. const { email } = this.dialog
  136. if (!emailRegExp.test(email)) {
  137. done(false)
  138. return this.$toast('邮箱地址格式错误')
  139. } else {
  140. await this.sendEmailConfirm(done)
  141. }
  142. },
  143. async sendEmailConfirm(done) {
  144. const { email } = this.dialog
  145. const fileUrl = this.link
  146. if (!email) {
  147. done(false)
  148. this.$toast('请输入邮箱地址')
  149. }
  150. if (!fileUrl) {
  151. done(false)
  152. this.$toast('报告附件异常')
  153. }
  154. const params = {
  155. downurl: fileUrl,
  156. email
  157. }
  158. const { error_msg: msg, error_code: code } =
  159. await sendProjectPdfFile(params)
  160. if (code === 0) {
  161. done && done()
  162. // this.$toast(`已发送至 ${email}`)
  163. this.$toast('邮件已发送,请注意查收')
  164. } else {
  165. done && done(false)
  166. this.$toast(msg || '发送失败')
  167. }
  168. },
  169. async goToSvipBuy() {
  170. if (this.beforeLeavePage) {
  171. await this.beforeLeavePage()
  172. }
  173. this.$router.push('/common/order/create/svip')
  174. }
  175. }
  176. }
  177. </script>
  178. <style lang="scss" scoped></style>