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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
| <template>
| <view class="coreshop-share-Box">
| <u-toast ref="uToast" />
| <view class="coreshop-share-pop">
| <view class="coreshop-share-item"
| v-for="(item, index) in providerList"
| :key="index"
| @click="clickHandler(item)">
| <image :src="item.img" mode=""></image>
| <view class="">{{ item.name }}</view>
| </view>
| </view>
| <view class="coreshop-bottomBox">
| <button class="coreshop-btn coreshop-btn-w coreshop-btn-square" @click="close()">关闭</button>
| </view>
| </view>
| </template>
|
| <script>
| export default {
| props: {
| // 商品id
| goodsId: {
| type: Number,
| default: 0
| },
| // 分享的图片
| shareImg: {
| type: String,
| default: ''
| },
| // 分享标题
| shareTitle: {
| type: String,
| default: ''
| },
| // 分享内容
| shareContent: {
| type: String,
| default: ''
| },
| // 分享链接
| shareHref: {
| type: String,
| default: ''
| },
| //分享类型
| shareType: {
| type: Number,
| default: 1
| },
| //拼团id
| groupId: {
| type: Number,
| default: 0
| },
| //拼团的团队id
| teamId: {
| type: Number,
| default: 0
| }
| },
| data() {
| return {
| providerList: [] // 分享通道 包含生成海报
| }
| },
| mounted() {
| /**
| *
| * H5+ 获取分享通道
| *
| */
| uni.getProvider({
| service: 'share',
| success: (e) => {
| let data = []
| for (let i = 0; i < e.provider.length; i++) {
| switch (e.provider[i]) {
| case 'weixin':
| data.push({
| name: '分享到微信好友',
| cate: 'share',
| id: 'weixin',
| img: '/static/images/common/share-wechat.png',
| sort: 0
| })
| data.push({
| name: '分享到微信朋友圈',
| cate: 'share',
| id: 'weixin',
| type: 'WXSenceTimeline',
| img: '/static/images/common/share--wechat-friends.png',
| sort: 1
| })
| break;
| case 'qq':
| data.push({
| name: '分享到QQ',
| cate: 'share',
| id: 'qq',
| img: '/static/images/common/share-qq.png',
| sort: 3
| })
| break;
| default:
| break;
| }
| }
|
| data.push({
| name: '生成海报',
| cate: 'poster',
| id: 'poster',
| img: '/static/images/common/share-poster.png',
| sort: 5
| })
| this.providerList = data.sort((x, y) => {
| return x.sort - y.sort
| });
| },
| fail: (e) => {
| // console.log('获取分享通道失败', e)
| }
| });
| },
| methods: {
| // 关闭弹出层
| close() {
| this.$emit('close')
| },
| // 点击操作
| clickHandler(e) {
| if (e.cate === 'poster') {
| this.createPoster()
| } else {
| // 去分享
| this.share(e)
| }
| },
| // 生成海报
| createPoster() {
| let data = {};
| if (this.shareType == 1) {
| //商品
| data = {
| page: 2, //商品
| url: this.$globalConstVars.shareUrl,
| params: {
| goodsId: this.goodsId
| },
| type: 3,//海报
| client: 2
| }
| let userToken = this.$db.get('userToken')
| if (userToken) {
| data.token = userToken
| }
| } else if (this.shareType == 3) {
| //拼团
| data = {
| page: 3, //商品
| url: this.$globalConstVars.shareUrl,
| params: {
| goodsId: this.goodsId,
| groupId: this.groupId,
| teamId: this.teamId
| },
| type: 3,//海报
| client: 2
| }
| let userToken = this.$db.get('userToken')
| if (userToken) {
| data.token = userToken
| }
| }
| this.$u.api.share(data).then(res => {
| if (res.status) {
| this.close()
| this.$u.route('/pages/share/sharePoster/sharePoster?poster=' + encodeURIComponent(res.data))
| } else {
| this.$u.toast(res.msg)
| }
| });
| },
| // 分享操作
| async share(e) {
| let shareOPtions = {
| provider: e.id,
| scene: e.type && e.type === 'WXSenceTimeline' ? 'WXSenceTimeline' : 'WXSceneSession', //WXSceneSession”分享到聊天界面,“WXSenceTimeline”分享到朋友圈,“WXSceneFavorite”分享到微信收藏
| type: this.shareType,
| success: (e) => {
| uni.showModal({
| content: '分享成功',
| showCancel: false
| })
| },
| fail: (e) => {
| uni.showModal({
| content: e.errMsg,
| showCancel: false
| })
| },
| complete: function () {
| // console.log('分享操作结束!')
| }
| }
| shareOPtions.summary = this.shareContent ? this.shareContent : ''
| shareOPtions.imageUrl = this.shareImg ? this.shareImg : ''
| shareOPtions.title = this.shareTitle ? this.shareTitle : ''
| shareOPtions.href = this.shareHref ? this.shareHref : ''
|
| if (shareOPtions.type === 0 && plus.os.name === 'iOS') {//如果是图文分享,且是ios平台,则压缩图片
| shareOPtions.imageUrl = await this.compress()
| }
| if (shareOPtions.type === 1 && shareOPtions.provider === 'qq') {//如果是分享文字到qq,则必须加上href和title
| shareOPtions.href = this.shareHref
| shareOPtions.title = this.shareTitle
| }
| uni.share(shareOPtions);
| },
| // 压缩图片 图文分享要求分享图片大小不能超过20Kb
| compress() {
| // console.log('开始压缩');
| let img = this.shareImg;
| return new Promise((res) => {
| var localPath = plus.io.convertAbsoluteFileSystem(img.replace('file://', ''));
| // console.log('after' + localPath);
| // 压缩size
| plus.io.resolveLocalFileSystemURL(localPath, (entry) => {
| entry.file((file) => {// 可通过entry对象操作图片
| // console.log('getFile:' + JSON.stringify(file));
| if (file.size > 20480) {// 压缩后size 大于20Kb
| plus.zip.compressImage({
| src: img,
| dst: img.replace('.jpg', '2222.jpg').replace('.JPG', '2222.JPG'),
| width: '10%',
| height: '10%',
| quality: 1,
| overwrite: true
| }, (event) => {
| // console.log('success zip****' + event.size);
| let newImg = img.replace('.jpg', '2222.jpg').replace('.JPG', '2222.JPG');
| res(newImg);
| }, function (error) {
| uni.showModal({
| content: '分享图片太大,需要请重新选择图片!',
| showCancel: false
| })
| });
| }
| });
| }, (e) => {
| // console.log('Resolve file URL failed: ' + e.message);
| uni.showModal({
| content: '分享图片太大,需要请重新选择图片!',
| showCancel: false
| })
| });
| })
| }
| }
| }
| </script>
|
|