collectBox.vue 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. <template>
  2. <view class="collect-box" @click.stop.prevent="handleClick">
  3. <image class="icon" :src="cid>0?require('@/static/collect-s.png'):require('@/static/collect.png')" mode="aspectFill" />
  4. </view>
  5. </template>
  6. <script>
  7. import {apiSetCollect,apiCancelCollect} from '@/api/user'
  8. export default {
  9. props:{
  10. type:{//收藏类型:1-研报; 2-视频社区; 3-微路演视频
  11. type:Number,
  12. default:0
  13. },
  14. primaryId:{// 藏类型主ID(如报告id,视频id)
  15. default:0
  16. },
  17. extendId:{//扩展ID-如研报章节ID
  18. default:0
  19. },
  20. collectId:{//收藏的id 大于0说明收藏了
  21. default:0,
  22. }
  23. },
  24. data() {
  25. return {
  26. cid:this.collectId
  27. }
  28. },
  29. methods: {
  30. handleClick(){
  31. if(this.cid>0){
  32. apiCancelCollect({
  33. collection_id:Number(this.cid)
  34. }).then(res=>{
  35. if(res.code===200){
  36. uni.showToast({
  37. title:'取消收藏!',
  38. icon:'none'
  39. })
  40. this.cid=0
  41. }
  42. })
  43. }else{
  44. apiSetCollect({
  45. collection_type:this.type,
  46. primary_id:Number(this.primaryId),
  47. extend_id:Number(this.extendId)
  48. }).then(res=>{
  49. if(res.code===200){
  50. uni.showToast({
  51. title:'收藏成功!',
  52. icon:'none'
  53. })
  54. this.cid=res.data
  55. }
  56. })
  57. }
  58. }
  59. },
  60. }
  61. </script>
  62. <style lang="scss" scoped>
  63. .collect-box{
  64. .icon{
  65. width: 34rpx;
  66. height: 34rpx;
  67. }
  68. }
  69. </style>