猎豆优选小程序

createShareImage.vue 14KB

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