cm-share-popup.vue 16 KB

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