OriginLink.vue 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. <template>
  2. <span class="origin-link-container">
  3. <span class="highlight-text origin-link clickable" @click="viewOriginLink">
  4. 查看原文链接
  5. </span>
  6. <van-dialog
  7. v-model="dialog.show"
  8. title="查看原文链接"
  9. show-cancel-button
  10. @confirm="useFreeChance"
  11. >
  12. <p class="content-text">
  13. 确定消耗
  14. <span class="highlight-text">1次</span>
  15. 查看原文链接的机会吗?
  16. </p>
  17. <p class="sub-content-text">
  18. 您当前是免费用户,有1次查看原文链接的机会,如需更多查看次数,您可点击
  19. <span
  20. class="highlight-text"
  21. @click="leaveInfo('article_original_more_membership')"
  22. >升级大会员 ></span
  23. >
  24. </p>
  25. </van-dialog>
  26. <van-dialog
  27. v-model="dialog.see"
  28. title=""
  29. class="see-dialog"
  30. close-on-click-overlay
  31. confirm-button-text="立即前往"
  32. @confirm="leaveSource('article_original')"
  33. >
  34. <p class="see-content">
  35. 为给您匹配精准的推荐信息,请完善个人信息,免费查看原文
  36. <AppIcon class="icons" name="close_heidi" @click="dialog.see = false" />
  37. </p>
  38. </van-dialog>
  39. </span>
  40. </template>
  41. <script>
  42. import { mapGetters, mapState } from 'vuex'
  43. import { LINKS } from '@/data'
  44. import { AppIcon } from '@/ui/'
  45. import { appCallOpenWindow } from '@/utils/callFn/appFn'
  46. import { openAppOrWxPage } from '@/utils/'
  47. import { getArticleOriginalText } from '@/api/modules/article'
  48. import EventBus from '@/utils/eventBus'
  49. export default {
  50. name: 'OriginLink',
  51. components: {
  52. AppIcon
  53. },
  54. props: {
  55. id: {
  56. type: String,
  57. required: true,
  58. default: ''
  59. },
  60. beforeLeavePage: Function
  61. },
  62. data() {
  63. return {
  64. dialog: {
  65. show: false,
  66. see: false
  67. },
  68. o_url: ''
  69. }
  70. },
  71. computed: {
  72. ...mapState({
  73. content: (state) => state.article.mainModel.content
  74. }),
  75. ...mapGetters('user', ['isFree']),
  76. url() {
  77. return this.content.originalUrl || this.o_url
  78. },
  79. toBCustom() {
  80. return this.IsCustomTopNet || this.yyszbContent
  81. },
  82. yyszbContent() {
  83. return this.content.yyszbContent || false
  84. },
  85. IsCustomTopNet() {
  86. return this.content.IsCustomTopNet || false
  87. }
  88. },
  89. created() {
  90. window.t = this
  91. this.eventBusListening()
  92. },
  93. methods: {
  94. eventBusListening() {
  95. EventBus.$on('originLink:view', () => {
  96. this.viewOriginLink()
  97. })
  98. // 及时解绑 EventBus 的事件监听
  99. this.$once('hook:beforeDestroy', () => {
  100. EventBus.$off('originLink:view')
  101. })
  102. },
  103. async viewOriginLink() {
  104. if (this.url) {
  105. // 如果已经获取过链接,则可以直接打开
  106. return this.openUrl()
  107. }
  108. const { inH5 } = this.$envs
  109. if (inH5) {
  110. if (this.beforeLeavePage) {
  111. await this.beforeLeavePage()
  112. }
  113. openAppOrWxPage(LINKS.下载APP, {
  114. query: {
  115. source: this.$env.platform.toUpperCase()
  116. }
  117. })
  118. } else {
  119. const loading = this.$toast.loading()
  120. try {
  121. const {
  122. error_code: code,
  123. error_msg: msg,
  124. data
  125. } = await this.getText()
  126. loading.clear()
  127. if (code === 0 && data) {
  128. if (data.url) {
  129. this.o_url = data.url
  130. this.openUrlAuth()
  131. } else if (data.status === 1) {
  132. this.leaveInfo('article_original_one', {
  133. signId: encodeURIComponent(this.id),
  134. article: this.extractString(location.href)
  135. })
  136. } else if (data.status === 2) {
  137. this.dialog.show = true
  138. } else if (data.status === 3) {
  139. this.leaveInfo('article_original_more', {
  140. signId: encodeURIComponent(this.id),
  141. article: this.extractString(location.href)
  142. })
  143. } else if (data.status === 4) {
  144. this.$dialog
  145. .confirm({
  146. title: '暂无更多查看原文链接权限',
  147. message: '如需开通更多权限获得商机,请联系客服。',
  148. className: 'j-confirm-dialog',
  149. showCancelButton: true,
  150. confirmButtonText: '联系客服',
  151. cancelButtonText: '返回',
  152. confirmButtonColor: '#2ABDD1',
  153. cancelButtonColor: '#171826',
  154. width: 303
  155. })
  156. .then(async () => {
  157. if (this.beforeLeavePage) {
  158. await this.beforeLeavePage()
  159. }
  160. openAppOrWxPage(LINKS.客服)
  161. })
  162. } else {
  163. this.$toast(data.msg)
  164. }
  165. } else {
  166. console.log(msg)
  167. }
  168. } catch (error) {
  169. loading.clear()
  170. }
  171. }
  172. },
  173. async getText(use) {
  174. const param = {
  175. id: this.id
  176. }
  177. if (use) {
  178. param.use = true
  179. }
  180. return getArticleOriginalText(param)
  181. },
  182. async openUrlAuth() {
  183. if (!this.url) return
  184. if (this.toBCustom) {
  185. return this.openUrl()
  186. }
  187. if (this.isFree) {
  188. // 检查是否已经留资。已留资返回true
  189. const source = 'article_original'
  190. try {
  191. const leavedInfo = await this.$leaveInfo.checkNeedLeaveInfo(source)
  192. if (!leavedInfo) {
  193. // 没有留资,弹窗留资
  194. this.dialog.see = true
  195. return
  196. }
  197. } catch (error) {
  198. console.log(error)
  199. }
  200. }
  201. this.openUrl()
  202. },
  203. async useFreeChance() {
  204. try {
  205. const {
  206. error_code: code,
  207. error_msg: msg,
  208. data
  209. } = await this.getText(true)
  210. if (code === 0 && data) {
  211. if (data.url) {
  212. this.o_url = data.url
  213. this.openUrlAuth()
  214. } else {
  215. if (msg) {
  216. console.log(msg)
  217. }
  218. }
  219. }
  220. } catch (error) {
  221. console.log(error)
  222. }
  223. },
  224. openUrl() {
  225. if (!this.url) return
  226. const { inApp } = this.$envs
  227. if (inApp) {
  228. appCallOpenWindow(this.url, '查看原文')
  229. } else {
  230. this.clickA(this.url)
  231. }
  232. },
  233. clickA(link) {
  234. const eleLink = document.createElement('a') // 新建A标签
  235. eleLink.href = link
  236. eleLink.rel = 'no-referrer'
  237. eleLink.style.display = 'none'
  238. document.body.appendChild(eleLink)
  239. eleLink.click() // 触发点击事件
  240. },
  241. extractString(str) {
  242. const regex = /\/article\/(.*?)\//
  243. const match = str.match(regex)
  244. return match ? match[1] : null
  245. },
  246. async leaveSource(source, query = {}) {
  247. if (this.beforeLeavePage) {
  248. await this.beforeLeavePage()
  249. }
  250. openAppOrWxPage(LINKS.留资, {
  251. query: {
  252. source,
  253. ...query
  254. }
  255. })
  256. },
  257. leaveInfo(key, query = {}) {
  258. const { platform } = this.$env
  259. const source = `${platform}_${key}`
  260. this.leaveSource(source, query)
  261. }
  262. }
  263. }
  264. </script>
  265. <style lang="scss" scoped>
  266. ::v-deep {
  267. .van-dialog__header {
  268. font-size: 18px;
  269. }
  270. .van-dialog__content {
  271. padding: 8px 16px;
  272. }
  273. .van-dialog__confirm {
  274. color: $main;
  275. }
  276. }
  277. .content-text {
  278. font-size: 14px;
  279. color: #5f5e64;
  280. text-align: center;
  281. }
  282. .sub-content-text {
  283. margin-top: 16px;
  284. width: 270px;
  285. font-size: 12px;
  286. line-height: 18px;
  287. color: #9b9ca3;
  288. text-align: center;
  289. }
  290. .see-dialog {
  291. width: 270px;
  292. }
  293. .see-content {
  294. position: relative;
  295. padding: 22px 12px;
  296. font-size: 15px;
  297. line-height: 22px;
  298. color: #5f5e64;
  299. .icons {
  300. position: absolute;
  301. top: 0;
  302. right: -8px;
  303. font-size: 20px;
  304. color: #ccc;
  305. }
  306. }
  307. .origin-link {
  308. color: #05a6f3;
  309. }
  310. </style>