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

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