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.

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