猎豆优选小程序

createShareImage.vue 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. <template>
  2. <view>
  3. <!-- 二维码 -->
  4. <tkiQrcode style="position:fixed;left:-9999px;top:0;" ref="qrcode" :val="qrcodeVal" :size="90" unit="px" background="#fff" foreground="#000" pdground="#000" lv="3" @result="onResultQrCode" icon="/static/imgs/avatar-login.png" iconSize="15" />
  5. <!-- 海报 -->
  6. <canvas v-if="refreshCanvas" canvas-id="myCanvas" class="myCanvas" style="width:375px;height:500px;position:fixed;left:-9999px;top:0;" />
  7. </view>
  8. </template>
  9. <script>
  10. import { canvasTextAutoLine, circleImgOne } from '../utils/tools'
  11. import tkiQrcode from 'tki-qrcode'
  12. const app = getApp()
  13. try{
  14. const $router = app.globalData.$router
  15. } catch (error) {
  16. }
  17. export default {
  18. name: 'createShareImage',
  19. props: {
  20. goodsParams: {
  21. type: Object,
  22. default: () => ({})
  23. },
  24. isJdGoods: {
  25. type: Boolean,
  26. default: () => false
  27. },
  28. isPddGoods: {
  29. type: Boolean,
  30. default: () => false
  31. },
  32. isDyGoods : {
  33. type: Boolean,
  34. default: () => false
  35. },
  36. isWphGoods : {
  37. type: Boolean,
  38. default: () => false
  39. }
  40. },
  41. components: {
  42. tkiQrcode,
  43. },
  44. data() {
  45. return {
  46. search_goods:{},
  47. search_con: '',
  48. show:false,
  49. isLogin: false,
  50. isShowTbAuth: false, // 控制是否展示淘宝授权弹框
  51. refreshCanvas: true, // 更新canvas
  52. canvasImg: '', // 海报图片
  53. qrcodeVal: '', // 二维码链接
  54. qrcodeImg: '', // 二维码图片
  55. posterInfo: {}, // 海报信息
  56. }
  57. },
  58. mounted(){
  59. this.handleShareGoods()
  60. },
  61. methods:{
  62. // 分享商品海报
  63. async handleShareGoods() {
  64. try {
  65. // 获取商品海报参数
  66. if (this.isJdGoods) { // 京东商品 => 构造京东海报参数
  67. this.posterInfo = await this.handleGetJdPosterInfo()
  68. // 获取二维码图片
  69. this.handleGetQrCodeImg(this.posterInfo.url_info.shortURL)
  70. } else if(this.isPddGoods){//拼多多
  71. this.posterInfo = await this.handleGetPddPosterInfo()
  72. // 获取二维码图片
  73. this.handleGetQrCodeImg(this.posterInfo.url_info.short_url)
  74. } else if (this.isDyGoods) { // 抖音
  75. this.posterInfo = await this.handleGetDyPosterInfo()
  76. // 获取二维码图片
  77. this.handleGetQrCodeImg(this.posterInfo.url_info.share_link)
  78. } else if (this.isWphGoods) { // 唯品会
  79. this.posterInfo = await this.handleGetWphPosterInfo()
  80. // 获取二维码图片
  81. this.handleGetQrCodeImg(this.posterInfo.url_info.url)
  82. } else {
  83. this.posterInfo = await this.handleGetPosterInfo()
  84. // 获取二维码图片
  85. this.handleGetQrCodeImg(this.posterInfo.url)
  86. }
  87. } catch (error) {
  88. console.log('error => ', error)
  89. }
  90. },
  91. // 获取分享商品海报的参数信息(唯品会)
  92. handleGetWphPosterInfo() {
  93. const { goods_id } = this.goodsParams
  94. return new Promise(async (resolve, reject) => {
  95. try {
  96. const url = this.$api_wph.vipShopShareCommonGoods;
  97. const params = { goods_id }
  98. uni.showLoading({ title: '加载中...', mask: true })
  99. const { data: res } = await this.$postJSON(url, params)
  100. uni.hideLoading()
  101. if (res && res.errno == 0) {
  102. let data = res.rst.data;
  103. resolve({ ...data })
  104. } else {
  105. uni.showToast({ title: res.err || '操作失败', icon: 'none' })
  106. reject(res.err || '操作失败')
  107. }
  108. } catch (error) {
  109. reject(error)
  110. }
  111. })
  112. },
  113. // 获取分享商品海报的参数信息(拼多多)
  114. handleGetPddPosterInfo() {
  115. const { goods_id } = this.goodsParams
  116. return new Promise(async (resolve, reject) => {
  117. try {
  118. const url = this.$api_pdd.shareCommonGoods;
  119. const params = { goods_id }
  120. uni.showLoading({ title: '加载中...', mask: true })
  121. const { data: res } = await this.$postJSON(url, params)
  122. uni.hideLoading()
  123. if (res && res.errno == 0) {
  124. let data = res.rst.data;
  125. data.small_images = data.small_images.map((v)=>{
  126. return v
  127. })
  128. resolve({ ...data })
  129. } else {
  130. uni.showToast({ title: res.err || '操作失败', icon: 'none' })
  131. reject(res.err || '操作失败')
  132. }
  133. } catch (error) {
  134. reject(error)
  135. }
  136. })
  137. },
  138. // 获取分享商品海报的参数信息(抖音)
  139. handleGetDyPosterInfo() {
  140. const { goods_id } = this.goodsParams
  141. return new Promise(async (resolve, reject) => {
  142. try {
  143. const url = this.$api_dy.shareCommonGoods;
  144. const params = { product_id: goods_id }
  145. uni.showLoading({ title: '加载中...', mask: true })
  146. const { data: res } = await this.$postJSON(url, params)
  147. uni.hideLoading()
  148. if (res && res.errno == 0) {
  149. let data = res.rst;
  150. data.small_images = data.small_images.map((v)=>{
  151. return v
  152. })
  153. resolve({ ...data })
  154. } else {
  155. uni.showToast({ title: res.err || '操作失败', icon: 'none' })
  156. reject(res.err || '操作失败')
  157. }
  158. } catch (error) {
  159. reject(error)
  160. }
  161. })
  162. },
  163. // 获取分享商品海报的参数信息(京东)
  164. handleGetJdPosterInfo() {
  165. const { goods_id } = this.goodsParams
  166. return new Promise(async (resolve, reject) => {
  167. try {
  168. const url = this.$api_jd.jdShareCommonGoods;
  169. const params = { goods_id }
  170. uni.showLoading({ title: '加载中...', mask: true })
  171. const { data: res } = await this.$postJSON(url, params)
  172. uni.hideLoading()
  173. if (res && res.errno == 0) {
  174. let data = res.rst.data;
  175. data.small_images = data.small_images.map((v)=>{
  176. return v.url
  177. })
  178. resolve({ ...data })
  179. } else {
  180. uni.showToast({ title: res.err || '操作失败', icon: 'none' })
  181. reject(res.err || '操作失败')
  182. }
  183. } catch (error) {
  184. reject(error)
  185. }
  186. })
  187. },
  188. // 获取分享商品海报的参数信息
  189. handleGetPosterInfo() {
  190. const { goods_id, is_coupon, coupon_price, price, discount_price, commission_rate, coupon_end_time, coupon_start_time } = this.goodsParams
  191. return new Promise(async (resolve, reject) => {
  192. try {
  193. const url = this.$api.goodsItem_shareCommonGoods;
  194. const params = { goods_id, is_coupon, coupon_price, price, discount_price, commission_rate, coupon_end_time, coupon_start_time }
  195. uni.showLoading({ title: '加载中...', mask: true })
  196. const { data: res } = await this.$postJSON(url, params)
  197. uni.hideLoading()
  198. if (res && res.errno == 0 && res.rst.data.goods_id) {
  199. resolve({ ...res.rst.data })
  200. } else {
  201. uni.showToast({ title: res.err || '操作失败', icon: 'none' })
  202. reject(res.err || '操作失败')
  203. }
  204. } catch (error) {
  205. reject(error)
  206. }
  207. })
  208. },
  209. // 根据url链接生成二维码图片
  210. async handleGetQrCodeImg(url) {
  211. try{
  212. this.qrcodeVal = url
  213. await this.$nextTick()
  214. this.$refs.qrcode._makeCode()
  215. }catch(e){
  216. // 绘制二维码图片失败
  217. this.$emit('getCanvasImage', {image: '', posterInfo: this.posterInfo})
  218. }
  219. },
  220. // 监听二维码生成完毕
  221. onResultQrCode(res){
  222. this.qrcodeImg = res
  223. // 开始绘制海报
  224. this.handleDrawPoster(this.posterInfo)
  225. },
  226. // 重新挂载canvas组件(刷新canvas内容)
  227. async handleRefreshCanvas() {
  228. this.refreshCanvas = false
  229. await this.$nextTick()
  230. this.refreshCanvas = true
  231. },
  232. // 绘制海报
  233. async handleDrawPoster(posterInfo) {
  234. const shopTypeLogos = {
  235. 4: '/static/imgs/jd-icon.png',
  236. 1: '/static/imgs/icon-tmall.png',
  237. 0: '/static/imgs/icon-tb.png',
  238. 6: '/static/imgs/pdd-icon.png',
  239. 5: '/static/imgs/icon-dy.png',
  240. 7: '/static/imgs/icon-wph.png',
  241. }
  242. try {
  243. const goodsTitle = posterInfo.title
  244. const shopTypeLogo = shopTypeLogos[posterInfo.shop_type]
  245. const goodsImg = posterInfo.img
  246. const discountPrice = posterInfo.discount_price
  247. const price = posterInfo.price
  248. const userAvatar = posterInfo.userinfo.headimg
  249. const userName = posterInfo.userinfo.nickname
  250. const userCode = posterInfo.userinfo.invite_code
  251. const qrcodeImg = this.qrcodeImg
  252. uni.showLoading({ title: '加载中...', mask: true })
  253. this.handleRefreshCanvas() // 强制更新canvas内容
  254. const myCanvas = uni.createCanvasContext('myCanvas', this)
  255. myCanvas.beginPath();
  256. const canvasW = 375
  257. const canvasH = 500
  258. myCanvas.width = canvasW
  259. myCanvas.height = canvasH
  260. myCanvas.clearRect(0, 0, canvasW, canvasH);
  261. myCanvas.setFillStyle("#ffffff"); // 背景色
  262. myCanvas.fillRect(0, 0, canvasW, canvasH);
  263. myCanvas.closePath();
  264. // 店铺类型logo
  265. myCanvas.beginPath()
  266. myCanvas.drawImage(shopTypeLogo, 20, 8, 14, 14)
  267. myCanvas.closePath()
  268. // 商品名称
  269. myCanvas.beginPath()
  270. myCanvas.setFillStyle("#333");
  271. myCanvas.font = '13px Arial'
  272. canvasTextAutoLine(
  273. myCanvas,
  274. ` ${goodsTitle}`,
  275. 20, // x轴坐标位置(视实际情况而定)
  276. 20, // y轴坐标位置(视实际情况而定)
  277. 260, // 文字显示最大宽度(视实际情况而定)
  278. 20 // 文字行高(视实际情况而定)
  279. )
  280. myCanvas.closePath()
  281. // 商品主图
  282. myCanvas.beginPath()
  283. const goodsImgRes = await uni.getImageInfo({ src: goodsImg })
  284. if (goodsImgRes[1] && goodsImgRes[1].errMsg === 'getImageInfo:ok') {
  285. myCanvas.drawImage(goodsImgRes[1].path, 30, 55, canvasW - 60, canvasW - 60)
  286. }
  287. myCanvas.closePath()
  288. // 券后价
  289. myCanvas.beginPath()
  290. const priceTop = 55 + canvasW - 60 + 30
  291. myCanvas.setFillStyle("#EC0B00");
  292. myCanvas.font = '14px Arial'
  293. myCanvas.fillText(`券后 ¥${discountPrice}`, 20, priceTop)
  294. myCanvas.closePath()
  295. // 原价
  296. myCanvas.beginPath()
  297. const discountPriceWidth = myCanvas.measureText(`券后 ¥${discountPrice}`).width + 40
  298. const priceWidth = myCanvas.measureText(price).width + 20
  299. myCanvas.setFillStyle("#333");
  300. myCanvas.font = '14px Arial'
  301. myCanvas.fillText(`¥${price}`, discountPriceWidth, priceTop)
  302. myCanvas.rect(discountPriceWidth, priceTop - 5, priceWidth, 1)
  303. myCanvas.setFillStyle('#333')
  304. myCanvas.fill()
  305. myCanvas.closePath()
  306. // 二维码
  307. const qrcodeRes = await uni.getImageInfo({ src: qrcodeImg })
  308. if (qrcodeRes[1] && qrcodeRes[1].errMsg === 'getImageInfo:ok') {
  309. myCanvas.drawImage(qrcodeRes[1].path, 270, priceTop - 10, 90, 90)
  310. }
  311. // 用户昵称
  312. myCanvas.beginPath()
  313. const userNameTop = priceTop + 20 + 16
  314. myCanvas.setFillStyle("#333");
  315. myCanvas.font = '13px Arial'
  316. myCanvas.fillText(userName, 20 + 12 + 12 + 10, userNameTop)
  317. myCanvas.closePath()
  318. // 邀请码
  319. myCanvas.beginPath()
  320. const userCodeTop = userNameTop + 34
  321. myCanvas.setFillStyle("#333");
  322. myCanvas.font = '13px Arial'
  323. myCanvas.fillText(`邀请码:${userCode}`, 20, userCodeTop)
  324. myCanvas.closePath()
  325. // 用户头像
  326. myCanvas.beginPath()
  327. const userAvatarTop = priceTop + 20
  328. const userAvatarRes = await uni.getImageInfo({ src: userAvatar })
  329. if (userAvatarRes[1] && userAvatarRes[1].errMsg === 'getImageInfo:ok') {
  330. circleImgOne(myCanvas, userAvatarRes[1].path, 20, userAvatarTop, 12)
  331. }
  332. myCanvas.closePath()
  333. myCanvas.draw(false, async () => {
  334. await this.$nextTick()
  335. uni.canvasToTempFilePath({
  336. fileType: 'png',
  337. canvasId: 'myCanvas',
  338. success: (res) => {
  339. this.canvasImg = res.tempFilePath
  340. this.$emit('getCanvasImage', {image: this.canvasImg, posterInfo: this.posterInfo})
  341. // wx.showShareImageMenu({
  342. // path: this.canvasImg
  343. // })
  344. },
  345. fail: (err) => {
  346. this.$emit('getCanvasImage', {image: this.canvasImg, posterInfo: this.posterInfo})
  347. }
  348. }, this)
  349. })
  350. } catch (error) {
  351. this.$emit('getCanvasImage', {image: this.canvasImg, posterInfo: this.posterInfo})
  352. } finally {
  353. uni.hideLoading()
  354. }
  355. },
  356. }
  357. }
  358. </script>
  359. <style scoped lang='scss'>
  360. .clipboard-container {
  361. position: absolute;
  362. top: 50%;
  363. left: 50%;
  364. transform: translate(-50%, -50%);
  365. width: 648rpx;
  366. min-height: 400rpx;
  367. padding: 52rpx 28rpx;
  368. border-radius: 120rpx 120rpx 32rpx 32rpx;
  369. background-color: #FFFFFF;
  370. z-index: 999;
  371. .clipbpard_bg{
  372. border-radius: 120rpx 120rpx 0 0;
  373. position: absolute;
  374. top: 0;
  375. left: 0;
  376. width: 100%;
  377. }
  378. .content{
  379. position: relative;
  380. z-index: 9;
  381. }
  382. .mask-title{
  383. display: flex;
  384. align-items: center;
  385. justify-content: center;
  386. color: #181818;
  387. font-size: 40rpx;
  388. line-height: 56rpx;
  389. font-weight: bold;
  390. letter-spacing: 2rpx;
  391. .clipbpard_title_img{
  392. width: 30rpx;
  393. height: 30rpx;
  394. margin: 0 16rpx;
  395. }
  396. }
  397. .close {
  398. position: absolute;
  399. bottom: -100rpx;
  400. left: 0;
  401. right: 0;
  402. margin: auto;
  403. width: 60rpx;
  404. height: 60rpx;
  405. }
  406. .goods-container{
  407. display: flex;
  408. align-items: center;
  409. margin-top: 84rpx;
  410. .goods-img{
  411. width: 200rpx;
  412. height: 200rpx;
  413. border-radius: 18rpx;
  414. }
  415. .goods-info{
  416. flex: 1;
  417. margin-left: 20rpx;
  418. .goods-title{
  419. font-size: 28rpx;
  420. color: #181818;
  421. line-height: 40rpx;
  422. /* // 超出两行省略号 */
  423. word-break: break-all;
  424. overflow: hidden;
  425. -webkit-line-clamp: 2;
  426. text-overflow: ellipsis;
  427. display: -webkit-box;
  428. -webkit-box-orient: vertical;
  429. }
  430. .goods-price{
  431. color: #FF4040;
  432. font-size: 36rpx;
  433. line-height: 50rpx;
  434. margin-top: 14rpx;
  435. }
  436. .goods-quan{
  437. display: flex;
  438. align-items: center;
  439. margin-top: 14rpx;
  440. .coupon-wrap {
  441. margin-right: 30rpx;
  442. display: flex;
  443. align-items: center;
  444. width: 155rpx;
  445. height: 36rpx;
  446. background-image: url("https://ld.726p.com/ldyx_static/imgs/quan.png");
  447. background-repeat: no-repeat;
  448. background-size: 100% 100%;
  449. color: #FF4040;
  450. font-size: 26rpx;
  451. &.coupon-fan{
  452. background-image: url("https://ld.726p.com/ldyx_static/imgs/fan.png");
  453. color: #AE4300;
  454. }
  455. .label {
  456. width: 48rpx;
  457. text-align: center;
  458. }
  459. .value {
  460. flex: 1;
  461. text-align: center;
  462. }
  463. }
  464. }
  465. }
  466. }
  467. .clipbpard-buttones{
  468. display: flex;
  469. align-items: center;
  470. justify-content: center;
  471. margin-top: 70rpx;
  472. .clipbpard-button{
  473. width: 252rpx;
  474. height: 80rpx;
  475. background: linear-gradient(90deg, #FF9048 0%, #FB3F3C 100%);
  476. border-radius: 40rpx;
  477. color: #FFFFFF;
  478. font-size: 32rpx;
  479. line-height: 80rpx;
  480. text-align: center;
  481. margin: 0 10rpx;
  482. &.share{
  483. background: #FFEED9;
  484. color: #AE4300;
  485. }
  486. text{
  487. margin-left: 10rpx;
  488. }
  489. }
  490. }
  491. .search_con{
  492. color: #181818;
  493. font-size: 28rpx;
  494. line-height: 40rpx;
  495. padding: 90rpx 24rpx 0;
  496. text-overflow: ellipsis;
  497. display: -webkit-box;
  498. -webkit-line-clamp: 6;
  499. overflow: hidden;
  500. /*! autoprefixer: off */
  501. -webkit-box-orient: vertical;
  502. /*! autoprefixer: on */
  503. word-break: break-all ;
  504. }
  505. }
  506. </style>