cm-share-popup.vue 15 KB

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