C端小程序
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.

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