poster.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. <template>
  2. <view class="content">
  3. <view class="bg" >
  4. <image :src="cardInfo.avatar" mode="aspectFit"></images>
  5. </view>
  6. <view>
  7. <view class="info">
  8. <!-- <view class="logo"><image :src="cardInfo.avatar ? cardInfo.avatar : '../../static/images/qy_logo.jpg'" mode="aspectFit"></image></view> -->
  9. <!-- <view class="brief" v-if="cardInfo.message">
  10. <image src="../static/images/yin_l.png" class="yin-l"></image>
  11. {{ cardInfo.message }}
  12. <image src="../static/images/yin_r.png" class="yin-r"></image>
  13. </view> -->
  14. <!-- codUrl -->
  15. <view class="code"><image :src="codUrl"></image></view>
  16. <view class="infos">
  17. <view class="name">{{ cardInfo.realName }}</view>
  18. <view class="company">{{ cardInfo.companyName }}</view>
  19. <view class="industry">{{ cardInfo.industryName }}</view>
  20. </view>
  21. </view>
  22. <view class="opt dflex">
  23. <!-- @click="downloadImg(codUrl)" -->
  24. <!-- #ifdef MP-WEIXIN -->
  25. <button class="li" hover-class="none" open-type="share">
  26. <image src="../static/images/hb_ico1.png"></image>
  27. <view>分享到微信</view>
  28. </button>
  29. <!-- #endif -->
  30. <!-- #ifdef APP-PLUS -->
  31. <view class="li" @click="appToShare" >
  32. <image src="../static/images/hb_ico1.png"></image>
  33. <view>分享到微信</view>
  34. </view>
  35. <!-- #endif -->
  36. <view class="li" @click="generatePoster()">
  37. <image src="../static/images/hb_ico2.png"></image>
  38. <view>保存图片</view>
  39. </view>
  40. </view>
  41. <canvas class="canvas" style="width:590px;height:736px;" canvas-id="share"></canvas>
  42. </view>
  43. </view>
  44. </template>
  45. <script>
  46. const app = getApp();
  47. const req = require('../../utils/request.js');
  48. const api = require('../../utils/api.js');
  49. const util = require('../../utils/util.js');
  50. export default {
  51. data() {
  52. return {
  53. id: '',
  54. cardInfo: '',
  55. codUrl: 'https://img.zhiqiyun.com/zqy3t9kp2zc5i/2023/03/20/be0cfb643cd38b97d5a7b14eecaaa8a1.png',
  56. isShowLoading: false
  57. };
  58. },
  59. onLoad(opt) {
  60. this.id = opt.id;
  61. this.getCardInfo();
  62. },
  63. onShareAppMessage(res) {
  64. if (res.from == 'button') {
  65. let userInfo = req.getStorage('userInfo');
  66. this.userBehavior(6)
  67. return {
  68. title: this.cardInfo.realName,
  69. path: '/card/index/index?cardId=' + this.cardInfo.id + '&userId=' + userInfo.id,
  70. imageUrl: this.cardInfo.avatar + '?x-oss-process=style/w375', // 分享图
  71. };
  72. }
  73. },
  74. methods: {
  75. appToShare() {
  76. let userInfo = req.getStorage('userInfo');
  77. uni.share({
  78. provider: 'weixin',
  79. scene: 'WXSceneSession',
  80. type: 5,
  81. imageUrl: this.cardInfo.avatar + '?x-oss-process=style/w375',
  82. title: this.cardInfo.realName,
  83. summary: this.cardInfo.realName,
  84. miniProgram: {
  85. id: req.public.miniProgramGh_id,
  86. path: '/card/index/index?cardId=' + this.cardInfo.id + '&userId=' + userInfo.id,
  87. type: req.env[req.env.NODE_ENV].typeMiniProgram,
  88. webUrl: req.public.homeWebUrl
  89. },
  90. success: ret => {
  91. this.userBehavior(6)
  92. }
  93. });
  94. },
  95. getCardInfo() {
  96. let dataP = {}
  97. let url = ''
  98. if (this.id) {
  99. url = '/api/v3/visiting/card/info';
  100. dataP.id = this.id;
  101. } else {
  102. url = '/api/v3/visiting/card/userInfo';
  103. dataP = {};
  104. }
  105. req.getRequest(url, dataP, data => {
  106. this.cardInfo = data;
  107. this.getCodeUrl()
  108. });
  109. },
  110. // 用户行为
  111. userBehavior(type){
  112. var dataP = {};
  113. dataP.type = 23 //、产品 2、活动 3、未知 4、内容 5、课程 6、老师 7、素材 8、题目 9、资料领取 10、招聘职位 12、用户须知 13、素材 15、医院科室 16、海报 20、医生 21 新闻 23名片
  114. dataP.behavior = type; //1、关注 2、收藏 3、点赞 4、浏览 5、确认 6、分享
  115. dataP.bindId = this.cardInfo.id;
  116. req.postRequestLoding('/api/v3/behavior/save', dataP, data => {
  117. });
  118. },
  119. getCodeUrl() {
  120. let that = this; //获取小程序码
  121. const params = {
  122. page: 'card/index/index',
  123. scene: this.cardInfo.id
  124. };
  125. return new Promise((resolve, reject) => {
  126. req.getRequest('/api/program/codev', params, url => {
  127. // console.log(url);
  128. this.code = url
  129. resolve();
  130. });
  131. });
  132. },
  133. generatePoster() {
  134. let that = this;
  135. if (!that.isShowLoading) {
  136. uni.showLoading({
  137. title: '生成中',
  138. mask: true
  139. });
  140. that.isShowLoading = true;
  141. }
  142. this.generate(imgUrl => {
  143. console.log(imgUrl)
  144. if (that.isShowLoading) {
  145. uni.hideLoading();
  146. that.isShowLoading = false;
  147. that.saveImage(imgUrl);
  148. }
  149. });
  150. },
  151. generate(success) {
  152. let that = this;
  153. let logoPromise = this.getImageInfo(this.cardInfo.avatar);
  154. let codePromise = this.getImageInfo(this.codUrl);
  155. Promise.all([logoPromise, codePromise]).then(([logo, code]) => {
  156. const ctx = uni.createCanvasContext('share', this); // 绘制背景,填充满整个canvas画布
  157. let width = 590;
  158. let height = 736;
  159. ctx.setFillStyle('#FFF');
  160. ctx.fillRect(0, 0, width, height);
  161. this.roundRect(ctx,0,0,width, height,16)
  162. // 渐变色
  163. var grd=ctx.createLinearGradient(0,height-250,0,736);
  164. grd.addColorStop(0,'#A4C8FF');
  165. grd.addColorStop(1,'#498FF8');
  166. ctx.setFillStyle(grd)
  167. ctx.fillRect(0,height-250,590,250);
  168. ctx.save();
  169. // 小程序码
  170. ctx.drawImage(code.path, (width - 304) / 2, 110, 304, 304);
  171. ctx.save();
  172. // 姓名
  173. ctx.fillStyle = '#fff';
  174. ctx.font = '38px PingFang SC';
  175. ctx.fillText(that.cardInfo.realName, 60 , height-250+100);
  176. ctx.save();
  177. ctx.fillStyle = '#fff';
  178. ctx.font = '28px PingFang SC';
  179. ctx.fillText(that.cardInfo.companyName, 60, height-250+100+50);
  180. ctx.save();
  181. // 边框文本
  182. ctx.setFontSize(20);
  183. ctx.setFillStyle('#ffffff');
  184. ctx.setTextAlign('left');
  185. var discountText = that.cardInfo.industryName
  186. var bdColor = '#fff';
  187. var bdBackground = 'transparent';
  188. var bdRadius = 5;
  189. var textPadding = 12;
  190. var boxHeight = 38;
  191. var boxWidth = ctx.measureText(discountText).width + textPadding * 2;
  192. ctx.fillText(discountText, 60+textPadding, height-250+90+80+25);
  193. that.borderRect(ctx, 60, height-250+90+80, boxWidth, boxHeight, bdRadius, bdBackground, bdColor)
  194. // 完成作画
  195. ctx.draw(false, function() {
  196. uni.canvasToTempFilePath(
  197. {
  198. canvasId: 'share',
  199. success: function(res) {
  200. success.call(this, res.tempFilePath);
  201. },
  202. fail: function(res) {}
  203. },
  204. that
  205. );
  206. });
  207. });
  208. },
  209. getImageInfo(url) {
  210. if (url) {
  211. return new Promise((resolve, reject) => {
  212. if (!url) {
  213. resolve();
  214. return '';
  215. }
  216. uni.getImageInfo({
  217. src: url,
  218. success: resolve,
  219. fail: reject
  220. });
  221. });
  222. } else {
  223. return '';
  224. }
  225. },
  226. /**
  227. *
  228. * @param {CanvasContext} ctx canvas上下文
  229. * @param {number} x 圆角矩形选区的左上角 x坐标
  230. * @param {number} y 圆角矩形选区的左上角 y坐标
  231. * @param {number} w 圆角矩形选区的宽度
  232. * @param {number} h 圆角矩形选区的高度
  233. * @param {number} r 圆角的半径
  234. */
  235. roundRect: function(ctx, x, y, w, h, r) {
  236. // 开始绘制
  237. ctx.beginPath(); // 因为边缘描边存在锯齿,最好指定使用 transparent 填充
  238. // 这里是使用 fill 还是 stroke都可以,二选一即可
  239. ctx.setFillStyle('#fff'); // ctx.setStrokeStyle('transparent')
  240. // 左上角
  241. ctx.arc(x + r, y + r, r, Math.PI, Math.PI * 1.5); // border-top
  242. ctx.moveTo(x + r, y);
  243. ctx.lineTo(x + w - r, y);
  244. ctx.lineTo(x + w, y + r); // 右上角
  245. ctx.arc(x + w - r, y + r, r, Math.PI * 1.5, Math.PI * 2); // border-right
  246. ctx.lineTo(x + w, y + h - r);
  247. ctx.lineTo(x + w - r, y + h); // 右下角
  248. ctx.arc(x + w - r, y + h - r, r, 0, Math.PI * 0.5); // border-bottom
  249. ctx.lineTo(x + r, y + h);
  250. ctx.lineTo(x, y + h - r); // 左下角
  251. ctx.arc(x + r, y + h - r, r, Math.PI * 0.5, Math.PI); // border-left
  252. ctx.lineTo(x, y + r);
  253. ctx.lineTo(x + r, y); // 这里是使用 fill 还是 stroke都可以,二选一即可,但是需要与上面对应
  254. ctx.fill(); // ctx.stroke()
  255. ctx.closePath(); // 剪切
  256. ctx.clip();
  257. },
  258. borderRect(ctx, x, y, w, h, r, fillColor, strokeColor) {
  259. // 画圆角 ctx、x起点、y起点、w宽度、y高度、r圆角半径、fillColor填充颜色、strokeColor边框颜色
  260. // 开始绘制
  261. ctx.beginPath()
  262. // 绘制左上角圆弧 Math.PI = 180度
  263. // 圆心x起点、圆心y起点、半径、以3点钟方向顺时针旋转后确认的起始弧度、以3点钟方向顺时针旋转后确认的终止弧度
  264. ctx.arc(x + r, y + r, r, Math.PI, Math.PI * 1.5)
  265. // 绘制border-top
  266. // 移动起点位置 x终点、y终点
  267. ctx.moveTo(x + r, y)
  268. // 画一条线 x终点、y终点
  269. ctx.lineTo(x + w - r, y)
  270. // ctx.lineTo(x + w, y + r)
  271. // 绘制右上角圆弧
  272. ctx.arc(x + w - r, y + r, r, Math.PI * 1.5, Math.PI * 2)
  273. // 绘制border-right
  274. ctx.lineTo(x + w, y + h - r)
  275. // ctx.lineTo(x + w - r, y + h)
  276. // 绘制右下角圆弧
  277. ctx.arc(x + w - r, y + h - r, r, 0, Math.PI * 0.5)
  278. // 绘制border-bottom
  279. ctx.lineTo(x + r, y + h)
  280. // ctx.lineTo(x, y + h - r)
  281. // 绘制左下角圆弧
  282. ctx.arc(x + r, y + h - r, r, Math.PI * 0.5, Math.PI)
  283. // 绘制border-left
  284. ctx.lineTo(x, y + r)
  285. // ctx.lineTo(x + r, y)
  286. if (fillColor) {
  287. // 因为边缘描边存在锯齿,最好指定使用 transparent 填充
  288. ctx.setFillStyle(fillColor)
  289. // 对绘画区域填充
  290. ctx.fill()
  291. }
  292. if (strokeColor) {
  293. // 因为边缘描边存在锯齿,最好指定使用 transparent 填充
  294. ctx.setStrokeStyle(strokeColor)
  295. // 画出当前路径的边框
  296. ctx.stroke()
  297. }
  298. // 关闭一个路径
  299. // ctx.closePath()
  300. // 剪切,剪切之后的绘画绘制剪切区域内进行,需要save与restore
  301. ctx.clip()
  302. },
  303. /**
  304. * 下载图片
  305. * @param {Object} url
  306. */
  307. downloadImg(url) {
  308. let that = this;
  309. console.log('下载图片>>>>>>',url);
  310. uni.downloadFile({
  311. url: url, //仅为示例,并非真实的资源
  312. success: res => {
  313. that.saveImage(res.tempFilePath);
  314. }
  315. });
  316. },
  317. /**
  318. * 保存图片
  319. * @param {Object} tempFilePath
  320. */
  321. saveImage(tempFilePath) {
  322. util.saveImage(tempFilePath);
  323. }
  324. }
  325. };
  326. </script>
  327. <style>
  328. @import './poster.css';
  329. </style>