C端小程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

465 regels
15 KiB

  1. var config = require("../../config/config.js");
  2. var app = getApp();
  3. const Http = require("../../utils/HttpBasics");
  4. const util = require("../../utils/util");
  5. const imgurl = require("../../utils/imgurl");
  6. Page({
  7. /**
  8. * 页面的初始数据
  9. */
  10. data: {
  11. poterbg: imgurl.poterbg.url,
  12. windowWidth: wx.getSystemInfoSync().windowWidth,
  13. windowHeight: wx.getSystemInfoSync().screenHeight,
  14. totalHeight: 0,
  15. mallname:'',//商场名字
  16. qrCodeL: "", //小程序码
  17. couponList: [], //活动劵列表
  18. canvasScale: 1.0 // 画布放大的倍数,因为如果保存的是一倍的分享图片的话,分享图会有点虚。所以保存的时候,canvasScale设置为2.0,wxss 里面的left: 500%;打开注释。就可保存两倍的分享图
  19. },
  20. /**
  21. * 生命周期函数--监听页面加载
  22. */
  23. onLoad: function(options) {
  24. let that = this;
  25. //劵详情
  26. if (options && options.couponChannelId) {
  27. that.getDetail(options.couponChannelId);
  28. } else if (options && options.merchantId) {
  29. that.getList(options.merchantId);
  30. that.getCouponList(options.merchantId);
  31. }
  32. this.getUserInfo();
  33. this.getMallInfo();
  34. this.begainDrawShareImage(options);
  35. },
  36. /**
  37. * 获取劵详情
  38. */
  39. getDetail: function(couponChannelId) {
  40. let that = this;
  41. // 获取用户信息
  42. Http.get({
  43. url: config.api.couponDetail,
  44. data: {
  45. couponChannelId: couponChannelId
  46. }
  47. }).then(res => {
  48. that.setData({
  49. data: res.data
  50. });
  51. });
  52. },
  53. /**
  54. * 获取商户详情
  55. */
  56. getList: function(id) {
  57. let that = this;
  58. let data;
  59. data = {
  60. pageNum: 1,
  61. pageSize: 15,
  62. id: id
  63. };
  64. Http.get({
  65. url: config.api.merchantList,
  66. data: data
  67. })
  68. .then(res => {
  69. that.setData({
  70. data: res.data.list[0]
  71. });
  72. })
  73. .catch(err => {
  74. wx.showToast({
  75. title: err.errMsg,
  76. icon: "none",
  77. duration: 2000,
  78. mask: false
  79. });
  80. });
  81. },
  82. /**
  83. * 绘制分享海报
  84. */
  85. begainDrawShareImage(options) {
  86. wx.showLoading({
  87. title: "生成中..."
  88. });
  89. var that = this;
  90. // 适配屏幕
  91. let scale = this.data.windowWidth / 375.0;
  92. this.setData({
  93. totalHeight: 767 * scale
  94. });
  95. // 获取Canvas
  96. let ctx = wx.createCanvasContext("myCanvas");
  97. // 获取宽高
  98. let wW = that.data.windowWidth;
  99. let wH = that.data.windowHeight;
  100. // 放大 因为不放大的话,生成的分享图会模糊。暂时先注释
  101. ctx.scale(this.data.canvasScale, this.data.canvasScale);
  102. // 首先要绘制顶部的背景图片,因为它在最底层,然后才能绘制其他内容
  103. let bgimg1 = that.data.poterbg + `?imageView/2/w/${wW}/h/${wH}`;
  104. let bgimg2 = util.getProxyImgUrl(bgimg1);
  105. wx.getImageInfo({
  106. src: bgimg2,
  107. success: function(res) {
  108. that.drawshopimg(ctx, scale, options);
  109. ctx.draw(true);
  110. }
  111. });
  112. },
  113. //绘制商品图片
  114. drawshopimg(ctx, scale, options) {
  115. var that = this;
  116. let topImageWidth = parseInt(375 * scale); // 因为小数有时候会请求不到图片,所以转成int
  117. let topImageHeight = parseInt(260 * scale);
  118. let src1 = "";
  119. let src2 = "";
  120. //劵详情海报
  121. if (options && options.couponChannelId) {
  122. src1 =that.data.data.coverImg +`?imageView/2/w/${topImageWidth}/h/${topImageHeight}`;
  123. src2 = util.getProxyImgUrl(src1);
  124. } else if (options && options.merchantId) {
  125. //商户详情海报
  126. src1 =that.data.data.merchantImgUrl +`?imageView/2/w/${topImageWidth}/h/${topImageHeight}`;
  127. src2 = util.getProxyImgUrl(src1);
  128. }
  129. wx.getImageInfo({
  130. src: src2,
  131. success: function(res) {
  132. // 绘制白色背景
  133. ctx.setFillStyle("#fbfbfb");
  134. ctx.fillRect(0, 0, that.data.windowWidth, that.data.windowHeight);
  135. // that.roundRect(ctx,0, 320, that.data.windowWidth, 320,50)
  136. ctx.drawImage(res.path, 0, 50, topImageWidth, topImageHeight);
  137. ctx.setFillStyle("blue");
  138. that.roundRect(ctx,12, 270, 330, 150,10)
  139. that.drawOtherImage(ctx, scale);
  140. that.drawOtherContent(ctx, scale,options);
  141. that.darwAvatarArc(ctx, that.data.avatarUrl, 10, 8, 35, 35);
  142. that.drawqrCode(ctx, scale);
  143. wx.hideLoading();
  144. }
  145. });
  146. },
  147. // 商户海报绘制
  148. drawOtherContent(ctx, scale,options) {
  149. //昵称
  150. this.drawNormalText(ctx,this.data.nickName,60 * scale,25 * scale,16 * scale,"#000","left","middle",scale);
  151. this.drawNormalText(ctx," 向您推荐",120 * scale,25 * scale,16 * scale,"#848484","left","middle",scale);
  152. this.drawNormalText(ctx,"---"+this.data.mallname+"---",175 * scale,530 * scale,16 * scale,"#000","left","middle",scale);
  153. if (options && options.merchantId) {
  154. //店铺名
  155. this.drawNormalText(ctx,"商户名称:"+this.data.data.merchantName,40 * scale,300 * scale,22 * scale,"#000","left","middle",scale);
  156. this.drawNormalText(ctx,"商户位置:"+this.data.data.shopVoList[0].buildingName+this.data.data.shopVoList[0].floorName+"--"+this.data.data.shopVoList[0].shopNumber,40 * scale,340 * scale,18 * scale,"#F4AA91","left","middle",scale);
  157. this.drawNormalText(ctx,"商户电话:"+this.data.data.merchantLinkPhone,40 * scale,380 * scale,16 * scale,"#000","left","middle",scale);
  158. this.drawNormalText(ctx,"长按识别,进店去看看",155 * scale,500 * scale,16 * scale,"#000","left","middle",scale);
  159. if (this.data.couponList.length > 0) {
  160. for (let i = 0; i < this.data.couponList.length; i++) {
  161. // 第一个商品信息
  162. this.drawNormalText(ctx,this.substrTile(this.data.couponList[1].title),30 * scale,396 * scale,14 * scale,"#000","left","middle",scale);
  163. this.drawNormalText(ctx,"价格:" + this.data.couponList[1].salePrice,30 * scale,416 * scale,14 * scale,"#FF3232","left","middle",scale);
  164. //第二个商品信息
  165. this.drawNormalText(ctx,this.substrTile(this.data.couponList[2].title),145 * scale,396 * scale,14 * scale,"#000","left","middle",scale);
  166. this.drawNormalText(ctx,"价格:" + this.data.couponList[2].salePrice,145 * scale,416 * scale,14 * scale,"#FF3232","left","middle",scale);
  167. //第三个商品信息
  168. this.drawNormalText(ctx,this.substrTile(this.data.couponList[3].title),260 * scale,396 * scale,14 * scale,"#000","left","middle",scale);
  169. this.drawNormalText(ctx,"价格:" + this.data.couponList[3].salePrice,260 * scale,416 * scale,14 * scale,"#FF3232","left","middle",scale);
  170. }
  171. }
  172. } else if (options && options.couponChannelId) {
  173. this.drawNormalText(ctx,"长按识别,享优惠",155 * scale,500 * scale,16 * scale,"#000","left","middle",scale);
  174. this.drawNormalText(ctx,this.data.data.title,40 * scale,300 * scale,23 * scale,"#000","left","middle",scale);
  175. this.drawNormalText(ctx,"售价:" + this.data.data.salePriceStr,40 * scale,330 * scale,20* scale,"#F4AA91","left","middle",scale);
  176. this.drawNormalText(ctx,"原价:" + this.data.data.salePriceStr,120 * scale,330 * scale,16 * scale,"#DFDCDB","left","middle",scale);
  177. this.drawNormalText(ctx,"适用门店:",40 * scale,360 * scale,16 * scale,"#000","left","middle",scale);
  178. for(var i=0;i<this.data.data.merchantVoList.length;i++){
  179. this.drawNormalText(ctx,this.data.data.merchantVoList[i].merchantName+"、",40 * scale+40*i,390 * scale,16 * scale,"#000","left","middle",scale);
  180. }
  181. ctx.draw(true);
  182. }
  183. ctx.draw(true);
  184. },
  185. //绘制小程序码
  186. drawqrCode(ctx, scale) {
  187. let coImageWidth = parseInt(120 * scale);
  188. let coImageHeight = parseInt(120 * scale);
  189. let src1 = "";
  190. let src2 = "";
  191. if (this.data.qrCode) {
  192. src1 =
  193. this.data.qrCode + `?imageView/2/w/${coImageWidth}/h/${coImageHeight}`;
  194. src2 = util.getProxyImgUrl(src1);
  195. } else {
  196. src1 =
  197. this.data.data.qrCode +
  198. `?imageView/2/w/${coImageWidth}/h/${coImageHeight}`;
  199. src2 = util.getProxyImgUrl(src1);
  200. }
  201. wx.getImageInfo({
  202. src: src2,
  203. success: function(res) {
  204. ctx.drawImage(res.path,25 * scale,435 * scale,coImageWidth,coImageHeight);
  205. ctx.draw(true);
  206. }
  207. });
  208. },
  209. // 绘制活动图片
  210. drawOtherImage(ctx, scale) {
  211. var that = this;
  212. // 如果该商户有活动商品
  213. if (that.data.couponList.length > 0) {
  214. let cotentImageWidth = parseInt(80 * scale);
  215. let cotentImageHeight = parseInt(80 * scale);
  216. for (let i = 0; i < that.data.couponList.length; i++) {
  217. let imageItem = that.data.couponList[i].coverImg;
  218. let src1 =imageItem +`?imageView/2/w/${cotentImageWidth}/h/${cotentImageHeight}`;
  219. let src2 = util.getProxyImgUrl(src1);
  220. wx.getImageInfo({
  221. src: src2,
  222. success: function(res) {
  223. ctx.drawImage(res.path,35 * scale + i * 110 * scale,300 * scale,cotentImageWidth,cotentImageHeight);
  224. }
  225. });
  226. }
  227. }
  228. that.drawqrCode(ctx, scale);
  229. },
  230. //截取商品名
  231. substrTile(str) {
  232. return str.substr(0, 5) + "...";
  233. },
  234. // 绘制只有一行的文字
  235. drawNormalText(ctx, str, x, y, font, style, align, baseLine) {
  236. ctx.setFontSize(font);
  237. ctx.setFillStyle(style);
  238. ctx.setTextAlign(align);
  239. ctx.setTextBaseline(baseLine);
  240. ctx.fillText(str, x, y);
  241. },
  242. /*
  243. * 绘制多行文本,自动换行,超出添加...
  244. *
  245. */
  246. canvasTextAutoLine: function(
  247. str,
  248. ctx,
  249. initX,
  250. initY,
  251. lineHeight,
  252. maxWidth,
  253. row = 1
  254. ) {
  255. var lineWidth = 0;
  256. var lastSubStrIndex = 0;
  257. var currentRow = 1;
  258. for (let i = 0; i < str.length; i++) {
  259. lineWidth += ctx.measureText(str[i]).width;
  260. if (lineWidth > maxWidth) {
  261. currentRow++;
  262. let newStr = str.substring(lastSubStrIndex, i);
  263. if (currentRow > row && str.length > i) {
  264. newStr = str.substring(lastSubStrIndex, i - 2) + "...";
  265. }
  266. ctx.fillText(newStr, initX, initY);
  267. initY += lineHeight;
  268. lineWidth = 0;
  269. lastSubStrIndex = i;
  270. if (currentRow > row) {
  271. break;
  272. }
  273. }
  274. if (i == str.length - 1) {
  275. ctx.fillText(str.substring(lastSubStrIndex, i + 1), initX, initY);
  276. }
  277. }
  278. },
  279. //绘制圆角图像
  280. darwAvatarArc: function(ctx, src, x, y, w, h = w) {
  281. /*
  282. ctx: 画布对象
  283. src: 头像缓存路径
  284. x: 头像起始位置 横坐标
  285. y: 头像起始位置 纵坐标
  286. w: 头像宽度
  287. h: 头像高度,不传为w
  288. */
  289. // 保存绘图上下文。
  290. ctx.save();
  291. // 开始创建一个路径。需要调用 fill 或者 stroke 才会使用路径进行填充或描边
  292. ctx.beginPath();
  293. // 设创建一个圆可以指定 起始弧度为 0,终止弧度为 2 * Math.PI。
  294. // 用 stroke 或者 fill 方法来在 canvas 中画弧线。
  295. ctx.arc(x + w / 2, y + h / 2, w / 2, 0, Math.PI * 2, false);
  296. /* 从原始画布中剪切任意形状和尺寸。
  297. 一旦剪切了某个区域,则所有之后的绘图都会被限制在被剪切的区域内(
  298. 不能访问画布上的其他区域)。可以在使用 clip 方法前通过使用 save 方法对当前画布区域进行保存,并在以后的任意时间通过restore方法对其进行恢复。
  299. */
  300. ctx.clip();
  301. // 画头像
  302. ctx.drawImage(src, x, y, w, h);
  303. // 恢复之前保存的绘图上下文。
  304. ctx.restore();
  305. // 将之前在绘图上下文中的描述(路径、变形、样式)画到 canvas 中。
  306. ctx.draw(true);
  307. },
  308. //绘制圆角矩形
  309. roundRect:function(ctx, x, y, w, h, r) {
  310. ctx.save();
  311. if (w < 2 * r) {r = w / 2;}
  312. if (h < 2 * r){ r = h / 2;}
  313. ctx.beginPath();
  314. // ctx.setStrokeStyle('white');
  315. ctx.setFillStyle('white')
  316. ctx.setLineWidth(4);
  317. ctx.moveTo(x+r, y);
  318. ctx.arcTo(x+w, y, x+w, y+h, r);
  319. ctx.arcTo(x+w, y+h, x, y+h, r);
  320. ctx.arcTo(x, y+h, x, y, r);
  321. ctx.arcTo(x, y, x+w, y, r);
  322. ctx.fill();
  323. ctx.closePath();
  324. ctx.draw(true);
  325. },
  326. //获取商户优惠劵信息
  327. getCouponList: function(id) {
  328. let that = this;
  329. let data;
  330. data = {
  331. status: 0,
  332. merchantId: id,
  333. pageNum: 1,
  334. pageSize: 15
  335. };
  336. Http.get({
  337. url: config.api.listByMerchant,
  338. data: data
  339. })
  340. .then(res => {
  341. that.setData({
  342. couponList: res.data.page.list,
  343. qrCode: res.data.qrCode
  344. });
  345. })
  346. .catch(err => {
  347. wx.showToast({
  348. title: err.errMsg,
  349. icon: "none",
  350. duration: 2000,
  351. mask: false
  352. });
  353. });
  354. },
  355. //获取头像和昵称
  356. getUserInfo: function() {
  357. let that = this;
  358. // 获取用户信息
  359. Http.get({
  360. url: config.api.getScore,
  361. data: {}
  362. }).then(res => {
  363. that.setData({
  364. nickName: res.data.nickName,
  365. avatarUrl: res.data.avatarUrl
  366. });
  367. });
  368. },
  369. //商场信息
  370. getMallInfo: function() {
  371. let that = this;
  372. // 获取用户信息
  373. Http.get({
  374. url: config.api.getMallInfo,
  375. data: {}
  376. }).then(res => {
  377. console.log(res);
  378. that.setData({
  379. mallname:res.data.name
  380. });
  381. });
  382. },
  383. /**
  384. * 保存图片
  385. */
  386. saveImage() {
  387. let that = this;
  388. wx.canvasToTempFilePath({
  389. x: 0,
  390. y: 0,
  391. width: this.data.windowWidth * this.data.canvasScale,
  392. height: this.data.totalHeight * this.data.canvasScale,
  393. canvasId: "myCanvas",
  394. success: function(res) {
  395. that.saveImageToPhotos(res.tempFilePath);
  396. },
  397. fail: function(res) {
  398. wx.showToast({
  399. title: "图片生成失败",
  400. icon: "none",
  401. duration: 2000
  402. });
  403. }
  404. });
  405. },
  406. saveImageToPhotos: function(tempFilePath) {
  407. wx.saveImageToPhotosAlbum({
  408. filePath: tempFilePath,
  409. success(result) {
  410. wx.showToast({
  411. title: "保存成功,从相册中分享到朋友圈吧",
  412. icon: "none",
  413. duration: 4000
  414. });
  415. },
  416. fail: function(err) {
  417. if (
  418. err.errMsg === "saveImageToPhotosAlbum:fail:auth denied" ||
  419. err.errMsg === "saveImageToPhotosAlbum:fail auth deny"
  420. ) {
  421. // 这边微信做过调整,必须要在按钮中触发,因此需要在弹框回调中进行调用
  422. wx.showModal({
  423. title: "提示",
  424. content: "需要您授权保存相册",
  425. showCancel: false,
  426. success: modalSuccess => {
  427. wx.openSetting({
  428. success(settingdata) {
  429. console.log("settingdata", settingdata);
  430. if (settingdata.authSetting["scope.writePhotosAlbum"]) {
  431. wx.showModal({
  432. title: "提示",
  433. content: "获取权限成功,再次点击图片即可保存",
  434. showCancel: false
  435. });
  436. } else {
  437. wx.showModal({
  438. title: "提示",
  439. content: "获取权限失败,将无法保存到相册哦~",
  440. showCancel: false
  441. });
  442. }
  443. },
  444. fail(failData) {
  445. console.log("failData", failData);
  446. },
  447. complete(finishData) {
  448. console.log("finishData", finishData);
  449. }
  450. });
  451. }
  452. });
  453. }
  454. }
  455. });
  456. }
  457. });