cm-share-popup.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. <template>
  2. <view class="share-popup">
  3. <!-- 弹窗 -->
  4. <uni-popup ref="sharePopup" type="bottom" @change="popupChange">
  5. <view class="popup-content">
  6. <view class="title" v-if="title" v-text="title"></view>
  7. <view class="content">
  8. <view class="row">
  9. <button class="share item" open-type="share" @click="close">
  10. <view class="icon-image"><image :src="staticUrl + 'icon-share-wechat.png'"></image></view>
  11. <text class="label">微信</text>
  12. </button>
  13. <view class="poster item" @click="createPoster" v-if="!posterUrl">
  14. <view class="icon-image"><image :src="staticUrl + 'icon-poster.png'"></image></view>
  15. <text class="label">生成海报</text>
  16. </view>
  17. <view class="poster item" @click="savePoster" v-else>
  18. <view class="icon-image"><image :src="staticUrl + 'icon-download.png'"></image></view>
  19. <text class="label">保存相册</text>
  20. </view>
  21. </view>
  22. <tui-divider :height="64"></tui-divider>
  23. <view class="cancel" @click="close">取消</view>
  24. </view>
  25. </view>
  26. </uni-popup>
  27. <canvas canvas-id="poster" id="poster" class="canvas"></canvas>
  28. <!-- 海报 -->
  29. <view class="poster-container" v-show="visiable">
  30. <!-- 画布 -->
  31. <image :src="posterUrl" class="poster-image"></image>
  32. </view>
  33. </view>
  34. </template>
  35. <script>
  36. import { mapGetters } from 'vuex'
  37. import { getUserProfile } from '@/common/auth.js'
  38. import { generateWxUnlimited } from '@/common/share.helper.js'
  39. export default {
  40. props: {
  41. title: {
  42. type: String,
  43. default: ''
  44. },
  45. data: {
  46. type: Object,
  47. default: () => {}
  48. },
  49. type: {
  50. type: String,
  51. default: 'normal',
  52. validator: value => {
  53. return ['product', 'normal', 'activity'].indexOf(value) > -1
  54. }
  55. }
  56. },
  57. data() {
  58. return {
  59. visiable: false,
  60. imageList: [],
  61. posterUrl: '',
  62. // 海报数据信息
  63. posterData: {
  64. query: '', // 查询参数字符串
  65. path: '', // 页面路径
  66. avatar: '', // 用户头像
  67. username: '', // 用户名
  68. porductName: '', // 产品名
  69. productPrice: '', // 产品价格
  70. productOriginPrice: '', // 产品原价
  71. productImage: '', // 产品图片
  72. qrCodeImage: '' //页面二维码,小程序二维码
  73. }
  74. }
  75. },
  76. computed: {
  77. ...mapGetters(['systemInfo'])
  78. },
  79. methods: {
  80. // 绘制海报 初始化
  81. initDrawPoster() {
  82. this.downLoadImageTask()
  83. },
  84. // 下载图片任务
  85. async downLoadImageTask() {
  86. const { avatar, productImage, qrCodeImage } = this.posterData
  87. // 背景图片
  88. const bgImageUrl = this.staticUrl + 'bg-share-01.png'
  89. // 用户头像
  90. const avatarUrl = avatar || this.staticUrl + 'icon-join-us.png'
  91. // 分享封面
  92. let coverUrl = productImage || this.staticUrl + 'icon-share.png'
  93. // 分享二维码
  94. let ewmUrl = qrCodeImage || this.staticUrl + 'icon-ewm-hehe.jpg'
  95. // 下载图片任务
  96. const taskList = [this.downloadImage(bgImageUrl), this.downloadImage(avatarUrl)]
  97. // 默认分享封面二维码
  98. if (this.type === 'normal') {
  99. coverUrl = this.staticUrl + 'icon-share.png' // 默认封面
  100. ewmUrl = this.staticUrl + 'icon-ewm-hehe.jpg' // 默认二维码
  101. taskList.push(this.downloadImage(coverUrl))
  102. taskList.push(this.downloadImage(ewmUrl))
  103. } else {
  104. taskList.push(this.downloadImage(coverUrl))
  105. taskList.push(
  106. generateWxUnlimited({
  107. pagePath: this.posterData.path,
  108. queryStr: this.posterData.query
  109. })
  110. )
  111. }
  112. // 执行下载图片
  113. try {
  114. this.imageList = await Promise.all(taskList)
  115. this.drawPoster()
  116. } catch (e) {
  117. console.log(e)
  118. // this.$toast('分享失败,请换用其它分享方式')
  119. uni.showToast({
  120. title: JSON.stringify(e),
  121. duration: 10000
  122. })
  123. this.$refs.sharePopup.close()
  124. }
  125. },
  126. // 绘制海报
  127. drawPoster() {
  128. const windowWidth = this.systemInfo.windowWidth
  129. // const scale = this.systemInfo.windowWidth / 750
  130. const scale = 1
  131. var ctx = uni.createCanvasContext('poster', this)
  132. // 绘制背景
  133. ctx.drawImage(this.imageList[0].tempFilePath, 0, 0, 540 * scale, 878 * scale)
  134. ctx.save()
  135. // 绘制用户信息
  136. this.drawPosterHead(ctx, scale)
  137. // 绘制白色矩形区域
  138. ctx.beginPath()
  139. ctx.rect(20 * scale, 154 * scale, 500 * scale, 704 * scale)
  140. ctx.setFillStyle('#FFFFFF')
  141. ctx.fill()
  142. ctx.clip()
  143. const type = this.type
  144. // 绘制底部内容
  145. if (!type || type === 'normal') {
  146. this.drawPosterFoot(ctx, scale)
  147. } else {
  148. this.drawGoodsInfo(ctx, scale)
  149. }
  150. ctx.restore()
  151. ctx.draw(true, () => {
  152. uni.canvasToTempFilePath(
  153. {
  154. canvasId: 'poster',
  155. success: res => {
  156. this.posterUrl = res.tempFilePath
  157. this.visiable = true
  158. },
  159. complete: () => {
  160. uni.hideLoading()
  161. }
  162. },
  163. this
  164. )
  165. })
  166. },
  167. // 绘制海报头部
  168. drawPosterHead(ctx, scale) {
  169. const { username } = this.posterData
  170. // 绘制头像
  171. ctx.beginPath()
  172. ctx.arc(40 * scale + (90 * scale) / 2, 32 * scale + (90 * scale) / 2, (90 * scale) / 2, 0, 2 * Math.PI)
  173. ctx.setFillStyle('#fff')
  174. ctx.fill()
  175. ctx.clip()
  176. ctx.drawImage(this.imageList[1].tempFilePath, 40 * scale, 32 * scale, 90 * scale, 90 * scale)
  177. ctx.restore()
  178. ctx.save()
  179. // 绘制用户名和推荐语
  180. ctx.setFillStyle('#FFFFFF')
  181. ctx.font = 'bold 10px sans-serif'
  182. ctx.setFontSize(30 * scale)
  183. ctx.fillText(username, 146 * scale, (30 + 34) * scale, 350 * scale)
  184. ctx.font = 'normal 10px sans-serif'
  185. ctx.setFontSize(24 * scale)
  186. let txt = '强烈为你推荐该商城'
  187. if (this.type === 'product') {
  188. txt = '强烈为你推荐该商品'
  189. } else if (this.type === 'activity') {
  190. txt = '强烈为你推荐该活动'
  191. }
  192. ctx.fillText(txt, 146 * scale, (87 + 24) * scale, 350 * scale)
  193. },
  194. // 绘制海报底部
  195. drawPosterFoot(ctx, scale) {
  196. // 绘制中心图片
  197. ctx.drawImage(this.imageList[2].tempFilePath, 40 * scale, 174 * scale, 460 * scale, 460 * scale)
  198. // 绘制底部
  199. ctx.setFontSize(24 * scale)
  200. ctx.setFillStyle('#333')
  201. ctx.fillText('护肤上颜选,正品', 60 * scale, (710 + 24) * scale)
  202. ctx.fillText('有好货', 60 * scale, (710 + 24 + 40) * scale)
  203. // 绘制二维码
  204. ctx.drawImage(this.imageList[3].tempFilePath, 364 * scale, 690 * scale, 116 * scale, 116 * scale)
  205. },
  206. // 绘制商品信息
  207. drawGoodsInfo(ctx, scale) {
  208. // 参数处理
  209. let { porductName, productPrice, productOriginPrice } = this.posterData
  210. const porductNames = this.getProductNames(porductName)
  211. console.log(porductNames)
  212. // 绘制中心图片
  213. ctx.drawImage(this.imageList[2].tempFilePath, 40 * scale, 174 * scale, 460 * scale, 460 * scale)
  214. if (this.type === 'product') {
  215. productPrice = productPrice.toFixed(2)
  216. if (productOriginPrice) {
  217. productOriginPrice = '¥' + productOriginPrice.toFixed(2)
  218. }
  219. // 绘制价格符号
  220. ctx.setFillStyle('#FF457B')
  221. ctx.setFontSize(24 * scale)
  222. ctx.fillText('¥', 40 * scale, (680 + 24) * scale)
  223. // 绘制购买价格
  224. ctx.setFontSize(40 * scale)
  225. ctx.fillText(productPrice, 62 * scale, (665 + 40) * scale)
  226. // 绘制原价
  227. if (productOriginPrice) {
  228. ctx.setFillStyle('#999')
  229. ctx.setFontSize(24 * scale)
  230. ctx.fillText(productOriginPrice, 200 * scale, (680 + 24) * scale)
  231. let m = ctx.measureText(productOriginPrice)
  232. ctx.fillRect(200 * scale, (680 + 16) * scale, parseInt(m.width), 1)
  233. }
  234. }
  235. // 绘制商品标题
  236. ctx.setFillStyle('#333')
  237. ctx.setFontSize(26 * scale)
  238. porductNames.forEach((text, index) => {
  239. text = index === 1 ? text.slice(0, text.length - 1) + '...' : text
  240. ctx.fillText(text, 40 * scale, (732 + 26 + 40 * index) * scale)
  241. })
  242. // 绘制商品二维码
  243. ctx.drawImage(this.imageList[3].tempFilePath, 384 * scale, 702 * scale, 116 * scale, 116 * scale)
  244. },
  245. // 处理产品名称
  246. getProductNames(porductName) {
  247. const list = []
  248. if (porductName.length > 12) {
  249. for (let i = 0; i < porductName.length; i += 12) {
  250. if (list.length < 2) {
  251. list.push(porductName.slice(i, i + 12))
  252. }
  253. }
  254. } else {
  255. list.push(porductName)
  256. }
  257. return list
  258. },
  259. // 下载图片
  260. downloadImage(url) {
  261. return new Promise((resolve, reject) => {
  262. uni.downloadFile({
  263. url: url,
  264. success(data) {
  265. resolve(data)
  266. },
  267. fail(err) {
  268. reject(err)
  269. }
  270. })
  271. })
  272. },
  273. open() {
  274. this.$refs.sharePopup.open()
  275. this.$emit('open')
  276. },
  277. close() {
  278. this.$refs.sharePopup.close()
  279. this.$emit('close')
  280. this.visiable = false
  281. },
  282. popupChange(e) {
  283. if (!e.show) {
  284. this.visiable = false
  285. this.posterUrl = ''
  286. }
  287. this.$emit('change', e)
  288. },
  289. async createPoster() {
  290. // 合并海报数据
  291. this.posterData = { ...this.posterData, ...this.data }
  292. try {
  293. // 从本地缓存中获取微信用户基本信息
  294. let userProfile = this.$getStorage('USER_PROFILE')
  295. if (!userProfile) {
  296. userProfile = await getUserProfile()
  297. this.$setStorage('USER_PROFILE', userProfile)
  298. }
  299. this.posterData.avatar = userProfile.avatarUrl
  300. this.posterData.username = userProfile.nickName
  301. uni.showLoading({
  302. mask: true,
  303. title: '正在为您生成海报'
  304. })
  305. // 绘制海报
  306. this.initDrawPoster()
  307. } catch (e) {
  308. //TODO handle the exception
  309. uni.hideLoading()
  310. }
  311. },
  312. savePoster() {
  313. console.log('保存图片到本地')
  314. uni.saveImageToPhotosAlbum({
  315. filePath: this.posterUrl,
  316. success: res => {
  317. this.close()
  318. this.$emit('saveSuccess', this.posterUrl)
  319. }
  320. })
  321. }
  322. }
  323. }
  324. </script>
  325. <style lang="scss" scoped>
  326. .canvas {
  327. position: fixed;
  328. left: -600px;
  329. top: 0;
  330. width: 540px;
  331. height: 878px;
  332. opacity: 0;
  333. }
  334. .poster-container {
  335. z-index: 100;
  336. position: fixed;
  337. top: 80rpx;
  338. left: 50%;
  339. transform: translateX(-50%);
  340. width: 540rpx;
  341. height: 878rpx;
  342. background-color: #fff;
  343. overflow: hidden;
  344. .poster-image {
  345. display: block;
  346. width: 540rpx;
  347. height: 878rpx;
  348. }
  349. }
  350. .popup-content {
  351. position: relative;
  352. padding: 40rpx;
  353. padding-bottom: 0;
  354. border-radius: 16rpx 16rpx 0 0;
  355. background-color: #fff;
  356. &::after {
  357. position: absolute;
  358. content: '';
  359. width: 100%;
  360. height: 80rpx;
  361. bottom: -80rpx;
  362. left: 0;
  363. background-color: #fff;
  364. }
  365. .title {
  366. font-size: 34rpx;
  367. color: #666;
  368. text-align: center;
  369. padding-bottom: 28rpx;
  370. }
  371. .content {
  372. .row {
  373. @extend .cm-flex-around;
  374. }
  375. .item {
  376. @extend .cm-flex-center;
  377. flex-direction: column;
  378. .label {
  379. color: #333;
  380. font-size: 26rpx;
  381. margin-top: 16rpx;
  382. }
  383. .icon-image {
  384. @extend .cm-flex-center;
  385. width: 100rpx;
  386. height: 100rpx;
  387. background-color: #f7f7f7;
  388. image {
  389. width: 64rpx;
  390. height: 64rpx;
  391. display: block;
  392. }
  393. }
  394. &.share {
  395. line-height: inherit;
  396. padding: 0;
  397. margin: 0;
  398. border: 0;
  399. background: transparent;
  400. &::after {
  401. border: 0;
  402. }
  403. }
  404. }
  405. .cancel {
  406. font-size: 28rpx;
  407. color: #666;
  408. font-weight: bold;
  409. text-align: center;
  410. padding-bottom: 32rpx;
  411. }
  412. }
  413. }
  414. </style>