cm-share-popup.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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. // text = index === 1 ? text.slice(0, text.length - 1) + '...' : text
  297. ctx.fillText(text, 40 * scale, (732 + 26 + 40 * index) * scale)
  298. })
  299. // 绘制商品二维码
  300. ctx.drawImage(this.imageList[3].tempFilePath, 384 * scale, 702 * scale, 116 * scale, 116 * scale)
  301. },
  302. // 处理产品名称
  303. getProductNames(porductName) {
  304. const list = []
  305. if (porductName.length > 12) {
  306. for (let i = 0; i < porductName.length; i += 12) {
  307. if (list.length < 2) {
  308. list.push(porductName.slice(i, i + 12))
  309. }
  310. }
  311. } else {
  312. list.push(porductName)
  313. }
  314. return list
  315. },
  316. // 下载图片
  317. downloadImage(url) {
  318. return new Promise((resolve, reject) => {
  319. uni.downloadFile({
  320. url: url,
  321. success(data) {
  322. resolve(data)
  323. },
  324. fail(err) {
  325. reject(err)
  326. }
  327. })
  328. })
  329. },
  330. async createPoster() {
  331. if (this.isLoading) return
  332. this.isLoading = true
  333. // 合并海报数据
  334. this.posterData = { ...this.posterData, ...this.data }
  335. try {
  336. // 从本地缓存中获取微信用户基本信息
  337. let userProfile = this.$getStorage('USER_PROFILE')
  338. if (!userProfile) {
  339. userProfile = await getUserProfile()
  340. this.$setStorage('USER_PROFILE', userProfile)
  341. }
  342. this.posterData.avatar = userProfile.avatarUrl
  343. this.posterData.username = userProfile.nickName
  344. uni.showLoading({
  345. mask: true,
  346. title: '正在为您生成海报'
  347. })
  348. // 绘制海报
  349. this.initDrawPoster()
  350. } catch (e) {
  351. //TODO handle the exception
  352. uni.hideLoading()
  353. }
  354. }
  355. }
  356. }
  357. </script>
  358. <style lang="scss" scoped>
  359. .canvas {
  360. position: fixed;
  361. left: -600px;
  362. top: 0;
  363. width: 540px;
  364. height: 878px;
  365. opacity: 0;
  366. }
  367. .poster-mask {
  368. position: fixed;
  369. top: 0;
  370. left: 0;
  371. z-index: 96;
  372. width: 100%;
  373. height: 100vh;
  374. background-color: rgba(0, 0, 0, 0.4);
  375. }
  376. .poster-container {
  377. z-index: 99;
  378. position: fixed;
  379. top: 80rpx;
  380. left: 50%;
  381. transform: translateX(-50%);
  382. width: 540rpx;
  383. overflow: hidden;
  384. text-align: center;
  385. .poster-image {
  386. display: block;
  387. width: 540rpx;
  388. height: 878rpx;
  389. }
  390. .poster {
  391. display: inline-block;
  392. margin: 0 auto;
  393. margin-top: 48rpx;
  394. .label {
  395. color: #fff !important;
  396. }
  397. }
  398. }
  399. .popup-content {
  400. position: relative;
  401. padding: 40rpx;
  402. padding-bottom: 0;
  403. border-radius: 16rpx 16rpx 0 0;
  404. background-color: #fff;
  405. &.no-safe-area {
  406. transform: translateY(34px);
  407. }
  408. &::after {
  409. position: absolute;
  410. content: '';
  411. width: 100%;
  412. height: 80rpx;
  413. bottom: -80rpx;
  414. left: 0;
  415. background-color: #fff;
  416. }
  417. .title {
  418. .title-text {
  419. font-size: 34rpx;
  420. color: #666;
  421. text-align: center;
  422. padding-bottom: 28rpx;
  423. }
  424. }
  425. }
  426. .popup-content,
  427. .poster-container {
  428. .row {
  429. @extend .cm-flex-around;
  430. }
  431. .item {
  432. @extend .cm-flex-center;
  433. flex-direction: column;
  434. .label {
  435. color: #333;
  436. font-size: 26rpx;
  437. margin-top: 16rpx;
  438. }
  439. .icon-image {
  440. @extend .cm-flex-center;
  441. width: 100rpx;
  442. height: 100rpx;
  443. background-color: #f7f7f7;
  444. border-radius: 50%;
  445. image {
  446. width: 64rpx;
  447. height: 64rpx;
  448. display: block;
  449. }
  450. }
  451. &.share {
  452. line-height: inherit;
  453. padding: 0;
  454. margin: 0;
  455. border: 0;
  456. background: transparent;
  457. &::after {
  458. border: 0;
  459. }
  460. }
  461. }
  462. .cancel {
  463. font-size: 28rpx;
  464. color: #666;
  465. font-weight: bold;
  466. text-align: center;
  467. padding-bottom: 32rpx;
  468. }
  469. }
  470. </style>