poster.vue 12 KB

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