Share.vue
7.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
<template>
<div class="share">
<van-share-sheet
v-model="showShare"
title="立即分享给好友"
:options="options"
@select="onSelect"
@cancel="cancel"
@click-overlay="cancel"
/>
<poster
:value="showPoster"
@close="showPoster = false"
:apiName="apiName"
:currentItem="currentItem"
:activityData="activityData"
/>
</div>
</template>
<script type='text/ecmascript-6'>
import { shopmallApi, activityApi, couponApi, liveApi, boutiquecouponApi, equityApi } from '@/api'
import { Notify } from 'vant'
const staticOptions = [
{ name: '企业微信', icon: require('@/assets/images/icon/wx.png'), type: 1 },
{ name: '分享海报', icon: require('@/assets/images/icon/poster.png'), type: 2 },
{ name: '复制链接', icon: require('@/assets/images/icon/copy.png'), type: 3 },
]
export default {
name: 'Share',
props: ['value', 'apiName', 'currentItem'],
data () {
return {
showPoster: false,
showShare: false,
options: [],
activityData: {},
component_id: '',
component_api_name: ''
}
},
watch: {
value (n) {
console.log(n)
let { currentItem: { id }, component_id, apiName, component_api_name } = this
if (component_id == id && n && component_api_name == apiName) {
this.showShare = true
return
}
if (n) {
let options = this.tools.deepClone(staticOptions)
if (apiName == 'couponApi' || apiName == 'boutiquecouponApi' || apiName == 'equityApi') {
options[1].className = 'disabled'
}
this.options = options
if (apiName == 'activityApi' || apiName == 'liveApi') {
this.showLoading()
let target = apiName == 'activityApi' ? activityApi : liveApi
target.getShared({ id }).then(({ code, data, message }) => {
if (code == '0') {
this.component_id = id
this.component_api_name = apiName
this.showShare = true
this.activityData = data
this.hideLoading()
} else {
this.hideLoading()
Notify(message)
this.$emit('cancel')
}
})
return
}
if (apiName == 'shopmallApi') {
shopmallApi.getGoodsLink({ id }).then(data => {
console.log(data)
this.activityData = {
url: data
}
})
shopmallApi.getGoodsPoster({ id }).then(res => {
console.log(res)
this.activityData = {
posterUrl: res
}
})
}
this.showShare = true
}
}
},
mounted () {
},
methods: {
closePoster () {
this.showPoster = false
},
onSelect ({ type, className }) {
let { currentItem, apiName, activityData } = this
switch (type) {
case 1:
let { userId } = currentItem
/**卡券分享微信 */
if (apiName == 'couponApi' || apiName == 'boutiquecouponApi') {
let { id, couponName } = currentItem
const desc = '您的专属服务顾问为您发来了优惠券,请点击领取~'
if (apiName == 'couponApi') {
this.couponShare(currentItem, 1, res => {
if (userId) this.sendChatMessage(res.url, desc, couponName)
else this.shareAppMessage(res.url, desc, couponName)
})
} else {
this.getBoutiqueCoupon(id, res => {
if (userId) this.sendChatMessage(res, desc, couponName)
else this.shareAppMessage(res, desc, couponName)
})
}
}
if (apiName == 'activityApi' || apiName == 'liveApi') {
let { activityData } = this
if (apiName == 'activityApi') {
if (userId) this.sendChatMessage(activityData.linkUrl, '', activityData.activityName)
else this.shareAppMessage(activityData.linkUrl, '', activityData.activityName)
} else {
if (userId) this.sendChatMessage(activityData.cdnUrl, '', currentItem.name, currentItem.imgUrl)
else this.shareAppMessage(activityData.cdnUrl, '', currentItem.name, currentItem.imgUrl)
}
}
if (apiName == 'equityApi' || apiName == 'shopmallApi') {
let { id } = currentItem
if (apiName == 'equityApi') {
equityApi.getEquityShared({ id }).then(res => {
let desc = `价值${currentItem.packagePrice}元的${currentItem.list.length || 0}项权益`
if (userId) this.sendChatMessage(res.url, desc, currentItem.packageName)
else this.shareAppMessage(res.url, desc, currentItem.packageName)
})
} else {
let { activityData } = this
if (userId) this.sendChatMessage(activityData.url, '', currentItem.goodsName, currentItem.goodsImage)
else this.shareAppMessage(activityData.url, '', currentItem.goodsName, currentItem.goodsImage)
}
}
this.cancel()
break;
case 2:
if (className) return
this.showPoster = true
break;
case 3:
let { id } = currentItem
this.showLoading()
if (apiName == 'shopmallApi') {
let { activityData } = this
this.setClipboardData(activityData.url)
} else if (apiName == 'activityApi') {
this.setClipboardData(activityData.linkUrl)
} else if (apiName == 'liveApi') {
this.setClipboardData(activityData.pagePath)
} else if (apiName == 'couponApi') {
this.couponShare(currentItem, 0, 3)
} else if (apiName == 'boutiquecouponApi') {
this.getBoutiqueCoupon(id, res => {
this.setClipboardData(res)
})
} else {
equityApi.getEquityShared({ id }).then(res => {
this.setClipboardData(res.url)
})
}
break;
}
},
cancel () {
this.$emit('cancel')
},
couponShare (item, shareType, callBack) {
let { userId, id } = item
let obj = {
couponId: id,
shareType
}
if (userId) obj.userList = [
{
userId
}
]
else obj.shareType = 0
couponApi.getCouponShared(obj).then(res => {
if (shareType == 0) this.setClipboardData(res.url)
if (shareType == 1) callBack(res.url)
})
},
getBoutiqueCoupon (id, callBack) {
boutiquecouponApi.getBoutiqueCoupon({ id }).then(res => {
callBack(res)
})
},
sendChatMessage (link, desc, title, imgUrl) {
window.$wx.sendChatMessage({
msgtype: 'news',
news: {
link, //H5消息页面url 必填
desc,
title
},
enterChat: false
}).then(() => {
this.showToast('发送成功')
})
},
shareAppMessage (link, desc, title, imgUrl) {
window.$wx.shareAppMessage({
title,
desc,
link,
imgUrl
})
}
}
}
</script>
<style lang='scss'>
.van-share-sheet__option.disabled {
opacity: 0.5;
}
</style>