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.

476 lines
15 KiB

  1. const navigationBarHeight = (getApp().statusBarHeight + 44) + 'px'
  2. var config = require("../../config/config.js");
  3. var app = getApp();
  4. const Http = require("../../utils/HttpBasics");
  5. const util = require("../../utils/util");
  6. const imgurl = require("../../utils/imgurl");
  7. Page({
  8. /**
  9. * 页面的初始数据
  10. */
  11. data: {
  12. navigationBarHeight,
  13. poterbg: imgurl.poterbg.url,
  14. windowWidth: wx.getSystemInfoSync().windowWidth,
  15. windowHeight: wx.getSystemInfoSync().screenHeight,
  16. totalHeight: 0,
  17. isdisable:true,
  18. mallname: '', //商场名字
  19. qrCodeL: "", //小程序码
  20. couponList: [], //活动劵列表
  21. canvasScale: 1.0 // 画布放大的倍数,因为如果保存的是一倍的分享图片的话,分享图会有点虚。所以保存的时候,canvasScale设置为2.0,wxss 里面的left: 500%;打开注释。就可保存两倍的分享图
  22. },
  23. /**
  24. * 生命周期函数--监听页面加载
  25. */
  26. onLoad: function(options) {
  27. let that = this;
  28. //劵详情
  29. if (options && options.couponChannelId) {
  30. that.getDetail(options.couponChannelId);
  31. } else if (options && options.merchantId) {
  32. that.getList(options.merchantId);
  33. that.getCouponList(options.merchantId);
  34. }
  35. this.getUserInfo();
  36. this.getMallInfo();
  37. this.begainDrawShareImage(options);
  38. },
  39. /**
  40. * 获取劵详情
  41. */
  42. getDetail: function(couponChannelId) {
  43. let that = this;
  44. // 获取用户信息
  45. Http.get({
  46. url: config.api.couponDetail,
  47. data: {
  48. couponChannelId: couponChannelId
  49. }
  50. }).then(res => {
  51. that.setData({
  52. data: res.data
  53. });
  54. });
  55. },
  56. /**
  57. * 获取商户详情
  58. */
  59. getList: function(id) {
  60. let that = this;
  61. let data;
  62. data = {
  63. pageNum: 1,
  64. pageSize: 15,
  65. id: id
  66. };
  67. Http.get({
  68. url: config.api.merchantList,
  69. data: data
  70. })
  71. .then(res => {
  72. that.setData({
  73. data: res.data.list[0]
  74. });
  75. })
  76. .catch(err => {
  77. wx.showToast({
  78. title: err.errMsg,
  79. icon: "none",
  80. duration: 2000,
  81. mask: false
  82. });
  83. });
  84. },
  85. /**
  86. * 绘制分享海报
  87. */
  88. begainDrawShareImage(options) {
  89. wx.showLoading({
  90. title: "生成中..."
  91. });
  92. var that = this;
  93. // 适配屏幕
  94. let scale = this.data.windowWidth / 375.0
  95. console.log(scale)
  96. this.setData({
  97. totalHeight: 667 * scale
  98. })
  99. // 获取Canvas
  100. let ctx = wx.createCanvasContext("myCanvas");
  101. // 获取宽高
  102. let wW = that.data.windowWidth;
  103. let wH = that.data.windowHeight;
  104. // 放大 因为不放大的话,生成的分享图会模糊。暂时先注释
  105. ctx.scale(this.data.canvasScale, this.data.canvasScale);
  106. // 首先要绘制顶部的背景图片,因为它在最底层,然后才能绘制其他内容
  107. let bgimg1 = that.data.poterbg + `?imageView/2/w/${wW}/h/${wH}`;
  108. let bgimg2 = util.getProxyImgUrl(bgimg1);
  109. wx.getImageInfo({
  110. src: bgimg2,
  111. success: function(res) {
  112. that.drawshopimg(ctx, scale, options);
  113. ctx.draw(true);
  114. }
  115. });
  116. },
  117. //绘制商品图片
  118. drawshopimg(ctx, scale, options) {
  119. var that = this;
  120. let topImageWidth = parseInt(375 * scale); // 因为小数有时候会请求不到图片,所以转成int
  121. let topImageHeight = parseInt(260 * scale);
  122. let src1 = "";
  123. let src2 = "";
  124. //劵详情海报
  125. if (options && options.couponChannelId) {
  126. src1 = that.data.data.coverImg + `?imageView/2/w/${topImageWidth}/h/${topImageHeight}`;
  127. src2 = util.getProxyImgUrl(src1);
  128. } else if (options && options.merchantId) {
  129. //商户详情海报
  130. src1 = that.data.data.merchantImgUrl + `?imageView/2/w/${topImageWidth}/h/${topImageHeight}`;
  131. src2 = util.getProxyImgUrl(src1);
  132. }
  133. wx.getImageInfo({
  134. src: src2,
  135. success: function(res) {
  136. // 绘制白色背景
  137. ctx.setFillStyle("#fbfbfb");
  138. ctx.fillRect(0, 0, that.data.windowWidth, that.data.windowHeight);
  139. ctx.drawImage(res.path, 0, 50, topImageWidth, topImageHeight);
  140. that.roundRect(ctx, 15 * scale, 270 * scale, 325 * scale, 125 * scale, 10* scale);
  141. that.drawOtherContent(ctx, scale, options);
  142. that.drawAvatarimg(ctx, scale);
  143. that.drawqrCode(ctx, scale);
  144. wx.hideLoading();
  145. }
  146. });
  147. this.setData({
  148. isdisable:false,
  149. })
  150. },
  151. //绘制微信头像
  152. drawAvatarimg(ctx, scale) {
  153. var that = this;
  154. //商户详情海报
  155. let src1 = that.data.avatarUrl + `?imageView/2/w/${100}/h/${100}`;
  156. let src2 = util.getProxyImgUrl(src1);
  157. wx.getImageInfo({
  158. src: src2,
  159. success: function(res) {
  160. that.darwAvatarArc(ctx, res.path, 10, 8, 35, 35);
  161. }
  162. });
  163. },
  164. // 商户海报绘制
  165. drawOtherContent(ctx, scale, options) {
  166. let that = this;
  167. console.log(ctx.measureText(this.data.nickName).width);
  168. //昵称
  169. this.drawNormalText(ctx, this.data.nickName, 48 * scale, 24 * scale, 16 * scale, "#000", "left", "middle", scale);
  170. this.drawNormalText(ctx, " 向您推荐", (ctx.measureText(this.data.nickName).width+60) * scale, 25 * scale, 16 * scale, "#848484", "left", "middle", scale);
  171. this.drawNormalText(ctx, "---" + this.data.mallname + "---", 175 * scale, 500 * scale, 16 * scale, "#000", "left", "middle", scale);
  172. if (options && options.merchantId) {
  173. //店铺名
  174. this.drawNormalText(ctx, "商户名称:" + this.data.data.merchantName, 30 * scale, 295 * scale, 20 * scale, "#000", "left", "middle", scale);
  175. // 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);
  176. this.drawNormalText(ctx, "商户电话:" + this.data.data.merchantLinkPhone, 30 * scale, 348 * scale, 16 * scale, "#000", "left", "middle", scale);
  177. this.drawNormalText(ctx, "长按识别,进店去看看", 155 * scale, 470 * scale, 16 * scale, "#000", "left", "middle", scale);
  178. } else if (options && options.couponChannelId) {
  179. this.drawNormalText(ctx, "长按识别,进店去看看", 155 * scale, 470 * scale, 16 * scale, "#000", "left", "middle", scale);
  180. this.drawNormalText(ctx, this.data.data.title, 30 * scale, 295 * scale, 20 * scale, "#000", "left", "middle", scale);
  181. this.drawNormalText(ctx, "售价:" + this.data.data.salePriceStr + "元", 30 * scale, 320 * scale, 15 * scale, "#F4AA91", "left", "middle", scale);
  182. this.drawNormalText(ctx, "适用门店:", 30 * scale, 348 * scale, 16 * scale, "#000", "left", "middle", scale);
  183. //适用门店字体的排列
  184. for (let i = 0; i < that.data.data.merchantVoList.length;i++){
  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. let width0=ctx.measureText(that.data.data.merchantVoList[0].merchantName).width
  189. that.drawNormalText(ctx, that.data.data.merchantVoList[i].merchantName, (width0+40)*scale, 375 * scale, 14 * scale, "#000", "left", "middle", scale);
  190. }else if(i==2){
  191. let width0=ctx.measureText(that.data.data.merchantVoList[0].merchantName).width
  192. let width1=ctx.measureText(that.data.data.merchantVoList[1].merchantName).width
  193. that.drawNormalText(ctx, that.data.data.merchantVoList[i].merchantName+"...", (width1+width0+45)*scale, 375 * scale, 14 * scale, "#000", "left", "middle", scale);
  194. }
  195. }
  196. ctx.draw(true);
  197. }
  198. ctx.draw(true);
  199. },
  200. //绘制小程序码
  201. drawqrCode(ctx, scale) {
  202. let coImageWidth = parseInt(120 * scale);
  203. let coImageHeight = parseInt(120 * scale);
  204. let src1 = "";
  205. let src2 = "";
  206. if (this.data.qrCode) {
  207. src1 =
  208. this.data.qrCode + `?imageView/2/w/${coImageWidth}/h/${coImageHeight}`;
  209. src2 = util.getProxyImgUrl(src1);
  210. } else {
  211. src1 =this.data.data.qrCode +`?imageView/2/w/${coImageWidth}/h/${coImageHeight}`;
  212. src2 = util.getProxyImgUrl(src1);
  213. }
  214. wx.getImageInfo({
  215. src: src2,
  216. success: function(res) {
  217. ctx.drawImage(res.path, 25 * scale, 420 * scale, coImageWidth, coImageHeight);
  218. ctx.draw(true);
  219. }
  220. });
  221. },
  222. //截取商品名
  223. substrTile(str) {
  224. return str.substr(0, 5) + "...";
  225. },
  226. // 绘制只有一行的文字
  227. drawNormalText(ctx, str, x, y, font, style, align, baseLine) {
  228. ctx.setFontSize(font);
  229. ctx.setFillStyle(style);
  230. ctx.setTextAlign(align);
  231. ctx.setTextBaseline(baseLine);
  232. ctx.fillText(str, x, y);
  233. },
  234. /*
  235. * 绘制多行文本,自动换行,超出添加...
  236. *
  237. */
  238. canvasTextAutoLine: function(str,ctx,initX,initY,font,style,align,baseLine,lineHeight,maxWidth,row = 1) {
  239. var lineWidth = 0;
  240. var lastSubStrIndex = 0;
  241. var currentRow = 1;
  242. for (let i = 0; i < str.length; i++) {
  243. lineWidth += ctx.measureText(str[i]).width;
  244. if (lineWidth > maxWidth) {
  245. currentRow++;
  246. let newStr = str.substring(lastSubStrIndex, i);
  247. if (currentRow > row && str.length > i) {
  248. newStr = str.substring(lastSubStrIndex, i - 2) + "...";
  249. }
  250. ctx.setFontSize(font);
  251. ctx.setFillStyle(style);
  252. ctx.setTextAlign(align);
  253. ctx.setTextBaseline(baseLine);
  254. ctx.fillText(newStr, initX, initY);
  255. initY += lineHeight;
  256. lineWidth = 0;
  257. lastSubStrIndex = i;
  258. if (currentRow > row) {
  259. break;
  260. }
  261. }
  262. if (i == str.length - 1) {
  263. ctx.setFontSize(font);
  264. ctx.setFillStyle(style);
  265. ctx.setTextAlign(align);
  266. ctx.setTextBaseline(baseLine);
  267. ctx.fillText(str.substring(lastSubStrIndex, i + 1), initX, initY);
  268. }
  269. }
  270. },
  271. //绘制圆角图像
  272. darwAvatarArc: function(ctx, src, x, y, w, h = w) {
  273. /*
  274. ctx: 画布对象
  275. src: 头像缓存路径
  276. x: 头像起始位置 横坐标
  277. y: 头像起始位置 纵坐标
  278. w: 头像宽度
  279. h: 头像高度,不传为w
  280. */
  281. // 保存绘图上下文。
  282. ctx.save();
  283. // 开始创建一个路径。需要调用 fill 或者 stroke 才会使用路径进行填充或描边
  284. ctx.beginPath();
  285. // 设创建一个圆可以指定 起始弧度为 0,终止弧度为 2 * Math.PI。
  286. // 用 stroke 或者 fill 方法来在 canvas 中画弧线。
  287. ctx.arc(x + w / 2, y + h / 2, w / 2, 0, Math.PI * 2, false);
  288. /* 从原始画布中剪切任意形状和尺寸。
  289. 一旦剪切了某个区域,则所有之后的绘图都会被限制在被剪切的区域内(
  290. 不能访问画布上的其他区域)。可以在使用 clip 方法前通过使用 save 方法对当前画布区域进行保存,并在以后的任意时间通过restore方法对其进行恢复。
  291. */
  292. ctx.clip();
  293. // 画头像
  294. ctx.drawImage(src, x, y, w, h);
  295. // 恢复之前保存的绘图上下文。
  296. ctx.restore();
  297. // 将之前在绘图上下文中的描述(路径、变形、样式)画到 canvas 中。
  298. ctx.draw(true);
  299. },
  300. //绘制圆角矩形
  301. roundRect: function(ctx, x, y, w, h, r, c = '#fff') {
  302. if (w < 2 * r) { r = w / 2; }
  303. if (h < 2 * r) { r = h / 2; }
  304. ctx.beginPath();
  305. ctx.fillStyle = c;
  306. ctx.arc(x + r, y + r, r, Math.PI, Math.PI * 1.5);
  307. ctx.moveTo(x + r, y);
  308. ctx.lineTo(x + w - r, y);
  309. ctx.lineTo(x + w, y + r);
  310. ctx.arc(x + w - r, y + r, r, Math.PI * 1.5, Math.PI * 2);
  311. ctx.lineTo(x + w, y + h - r);
  312. ctx.lineTo(x + w - r, y + h);
  313. ctx.arc(x + w - r, y + h - r, r, 0, Math.PI * 0.5);
  314. ctx.lineTo(x + r, y + h);
  315. ctx.lineTo(x, y + h - r);
  316. ctx.arc(x + r, y + h - r, r, Math.PI * 0.5, Math.PI);
  317. ctx.lineTo(x, y + r);
  318. ctx.lineTo(x + r, y);
  319. ctx.fill();
  320. ctx.closePath();
  321. },
  322. //获取商户优惠劵信息
  323. getCouponList: function(id) {
  324. let that = this;
  325. let data;
  326. data = {
  327. status: 0,
  328. merchantId: id,
  329. pageNum: 1,
  330. pageSize: 15
  331. };
  332. Http.get({
  333. url: config.api.listByMerchant,
  334. data: data
  335. })
  336. .then(res => {
  337. if (res.data && res.data.qrCode) {
  338. that.setData({
  339. qrCode: res.data.qrCode,
  340. })
  341. }
  342. that.setData({
  343. couponList: res.data.page.list
  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. });