poster.vue 11 KB

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