C端小程序
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

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