cm-share-popup.vue 13 KB

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