shareMixin.js
7.76 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
import { wxConfig, marketingApi } from '@/api'
// import { resolve } from 'core-js/fn/promise'
export default {
data () {
return {
entry: 'normal'
}
},
mounted () {
this.wxInit()
},
methods: {
wxInit () {
let _this = this
wxConfig.init(['getContext', 'shareAppMessage', 'sendChatMessage', 'shareToExternalMoments']).then(() => {
wxConfig.getContext().then(({ entry }) => {
_this.$parent.entry = entry
_this.entry = entry
})
})
},
shareToExternalMoments (i) {
this.sendMessage(i, true)
},
async sendMessage (i, isPyq) {
const { shopmall_appid, envVersion } = window.$fetchConfig
const path = 'pages/psdView/psdView'
const { merchantId, uuid, sapCode, userName } = this.getLocalStorage('dj_scrm_userInfo')
const { entry } = this
const { materialName, id, titleUrl, materialTitle, description, videoUrl, imageUrlList } = i
// let fisrtFrame = videoUrl && await this.getVideoFirstFrame(videoUrl)
const imgUrl = 'https://img01.glsx.com.cn/weapp/resource/scrm-mobile/test/test.jpg'
let frameUrl = (imageUrlList && imageUrlList.length) ? imageUrlList[0] : imgUrl
let query = `merchantId=${merchantId}&id=${id}&staffUuid=${uuid}&shareTime=${Date.now()}`
if (sapCode) query = `${query}&storeCode=${sapCode}`
if (isPyq) {
const data = {
path,
query: encodeURIComponent(query),
appId: shopmall_appid,
envVersion
}
let url = window.location.origin.replace('scrm', 'wechatlink')
wxConfig.shareToExternalMoments({
attachments: [
{
msgtype: 'link', // 消息类型,必填
link: {
title: materialTitle || `「${userName}」邀请您查看「${materialName}」`, // H5消息标题
imgUrl: titleUrl || frameUrl, // H5消息封面图片URL
url: `${url}?linkData=${encodeURIComponent(JSON.stringify(data))}` // H5消息页面url 必填
},
}
]
}).then(() => {
this.sendReadBuriedPoint({ id, merchantId, uuid })
this.hideLoading()
})
.catch(err => {
this.hideLoading()
})
} else {
wxConfig.createMiniprogramUrlLink({
path,
query,
appId: shopmall_appid,
envVersion
})
.then(link => {
// console.log('再次进来了吗')
// this.showLoading()
let params = {
link,
title: materialTitle || `「${userName}」邀请您查看「${materialName}」`,
desc: description || '',
imgUrl: titleUrl || frameUrl
}
// if (entry != 'normal') this.sendChatMessage(params, { id, merchantId, uuid })
// else this.shareAppMessage(params, { id, merchantId, uuid })
this.shareAppMessage(params, { id, merchantId, uuid })
// wxConfig.getContext().then((res) => {
// if (res.entry != 'normal'){
// let pdfParam = {
// msgtype: 'news',
// news: params,
// enterChat: false
// }
// if(i.materialType==1){
// this.sendChatMessage(pdfParam, { id, merchantId, uuid })
// }else{
// this.getParam(i, { id, merchantId, uuid })
// }
// }else{
// this.shareAppMessage(params, { id, merchantId, uuid })
// }
// })
})
}
},
sendReadBuriedPoint ({ id, merchantId, uuid }) {
marketingApi.sendReadBuriedPoint({
materialId: id,
type: 1,
merchantId,
staffUuid: uuid
})
},
getParam (item, sendParams) {
let str = decodeURIComponent(item.materialUrl)
let testReg = /\>(.*?)\<|img(.*?)src.*?(.jpg|.peg|.png|.gif|.webp|.pjp|.jfif|.pjpeg|.bmp|.dib)|video(.*?)src.*?(.ogm|.wmv|.asx|.mpg|.webm|.mp4|.ogv|.mpeg|.mov|.m4v|.avi)/g
let arr = str.match(testReg)
let imgLabelReg = /img(.*?)src.*?(.jpg|.peg|.png|.gif|.webp|.pjp|.jfif|.pjpeg|.bmp|.dib)/
let videoLabelReg = /video(.*?)src.*?(.ogm|.wmv|.asx|.mpg|.webm|.mp4|.ogv|.mpeg|.mov|.m4v|.avi)/
let imgLinkReg = /https?:\/\/(.*?)(.jpg|.peg|.png|.gif|.webp|.pjp|.jfif|.pjpeg|.bmp|.dib)/
let videoLinkReg = /https?:\/\/(.*?)(.ogm|.wmv|.asx|.mpg|.webm|.mp4|.ogv|.mpeg|.mov|.m4v|.avi)/
arr = arr.map(item => {
if (imgLabelReg.test(item)) {
if (item.match(imgLinkReg)) return item.match(imgLinkReg)[0]
} else if (videoLabelReg.test(item)) {
if (item.match(videoLinkReg)) return item.match(videoLinkReg)[0]
} else if (item && item.length > 2 && item != '><') {
return item.slice(1, -1) ? item.slice(1, -1).replaceAll('&', '&').replaceAll('>', '>').replaceAll('<', '<') : ''
}
})
arr = arr.filter(item => item)
let newArr = []
arr.forEach((ele, id) => {
let mediaType = 5
let myParam = {}
// let fileReg = /^https?:\/\/[\s\S]+\./
if (!ele) return
if (imgLinkReg.test(ele) || videoLinkReg.test(ele)) {
let imgArr = ['jpg', 'jpeg', 'png', 'gif', 'webp', 'pjp', 'jfif', 'pjpeg', 'bmp', 'dib']
let idx = ele.lastIndexOf('.')
if (idx > -1 && imgArr.includes(ele.slice(idx + 1))) {
mediaType = 3
} else {
mediaType = 4
}
myParam = this.uploadMaterial({ materialName: item.materialName, data: ele, mediaType }, mediaType)
} else {
myParam = this.getTextPromise(ele)
}
newArr.push(myParam)
});
Promise.allSettled(newArr).then(async res => {
for (let i = 0; i < res.length; i++) {
await this.sendChatMessage(res[i].value, sendParams)
}
})
},
getTextPromise (ele) {
return new Promise((resolve, reject) => {
resolve({
msgtype: 'text',
text: { content: ele },
enterChat: false
})
})
},
uploadMaterial (param, type) {
return new Promise((resolve, reject) => {
marketingApi.uploadMaterial(param).then(res => {
if (res?.mediaId) {
if (type == 1) {
resolve({
msgtype: 'file',
file: { mediaid: res.mediaId },
enterChat: false
})
} else if (type == 3) {
resolve({
msgtype: 'image',
image: { mediaid: res.mediaId },
enterChat: false
})
} else {
resolve({
msgtype: 'video',
video: { mediaid: res.mediaId },
enterChat: false
})
}
}
})
})
},
sendChatMessage (params, sendParams) {
return new Promise((resolve, reject) => {
this.showLoading()
wxConfig
.sendChatMessage(params)
.then(res => {
this.hideLoading()
this.sendReadBuriedPoint(sendParams)
resolve()
}).catch(err => {
this.hideLoading()
})
})
},
shareAppMessage (params, sendParams) {
wxConfig
.shareAppMessage(params)
.then(() => {
this.sendReadBuriedPoint(sendParams)
this.hideLoading()
})
.catch(() => {
this.hideLoading()
})
},
toUnicode (chineseStr = '') {
let unicodeStr = ''
for (let i = 0, iLength = chineseStr.length; i < iLength; i++) {
unicodeStr += '@' + chineseStr.charCodeAt(i).toString(16)
}
return unicodeStr
}
}
}