C端小程序
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

497 行
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.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. console.log(this.data.qrCode, this.data.data.qrCode,"qrCode")
  207. if (this.data.qrCode) {
  208. src1 =
  209. this.data.qrCode + `?imageView/2/w/${coImageWidth}/h/${coImageHeight}`;
  210. src2 = util.getProxyImgUrl(src1);
  211. } else {
  212. src1 =this.data.data.qrCode +`?imageView/2/w/${coImageWidth}/h/${coImageHeight}`;
  213. src2 = util.getProxyImgUrl(src1);
  214. }
  215. wx.getImageInfo({
  216. src: src2,
  217. success: function(res) {
  218. ctx.drawImage(res.path, 25 * scale, 420 * scale, coImageWidth, coImageHeight);
  219. ctx.draw(true);
  220. }
  221. });
  222. },
  223. //截取商品名
  224. substrTile(str) {
  225. return str.substr(0, 5) + "...";
  226. },
  227. // 绘制只有一行的文字
  228. drawNormalText(ctx, str, x, y, font, style, align, baseLine) {
  229. ctx.setFontSize(font);
  230. ctx.setFillStyle(style);
  231. ctx.setTextAlign(align);
  232. ctx.setTextBaseline(baseLine);
  233. ctx.fillText(str, x, y);
  234. },
  235. /*
  236. * 绘制多行文本,自动换行,超出添加...
  237. *
  238. */
  239. canvasTextAutoLine: function(str,ctx,initX,initY,font,style,align,baseLine,lineHeight,maxWidth,row = 1) {
  240. var lineWidth = 0;
  241. var lastSubStrIndex = 0;
  242. var currentRow = 1;
  243. for (let i = 0; i < str.length; i++) {
  244. lineWidth += ctx.measureText(str[i]).width;
  245. if (lineWidth > maxWidth) {
  246. currentRow++;
  247. let newStr = str.substring(lastSubStrIndex, i);
  248. if (currentRow > row && str.length > i) {
  249. newStr = str.substring(lastSubStrIndex, i - 2) + "...";
  250. }
  251. ctx.setFontSize(font);
  252. ctx.setFillStyle(style);
  253. ctx.setTextAlign(align);
  254. ctx.setTextBaseline(baseLine);
  255. ctx.fillText(newStr, initX, initY);
  256. initY += lineHeight;
  257. lineWidth = 0;
  258. lastSubStrIndex = i;
  259. if (currentRow > row) {
  260. break;
  261. }
  262. }
  263. if (i == str.length - 1) {
  264. ctx.setFontSize(font);
  265. ctx.setFillStyle(style);
  266. ctx.setTextAlign(align);
  267. ctx.setTextBaseline(baseLine);
  268. ctx.fillText(str.substring(lastSubStrIndex, i + 1), initX, initY);
  269. }
  270. }
  271. },
  272. //绘制圆角图像
  273. darwAvatarArc: function(ctx, src, x, y, w, h = w) {
  274. /*
  275. ctx: 画布对象
  276. src: 头像缓存路径
  277. x: 头像起始位置 横坐标
  278. y: 头像起始位置 纵坐标
  279. w: 头像宽度
  280. h: 头像高度,不传为w
  281. */
  282. // 保存绘图上下文。
  283. ctx.save();
  284. // 开始创建一个路径。需要调用 fill 或者 stroke 才会使用路径进行填充或描边
  285. ctx.beginPath();
  286. // 设创建一个圆可以指定 起始弧度为 0,终止弧度为 2 * Math.PI。
  287. // 用 stroke 或者 fill 方法来在 canvas 中画弧线。
  288. ctx.arc(x + w / 2, y + h / 2, w / 2, 0, Math.PI * 2, false);
  289. /* 从原始画布中剪切任意形状和尺寸。
  290. 一旦剪切了某个区域,则所有之后的绘图都会被限制在被剪切的区域内(
  291. 不能访问画布上的其他区域)。可以在使用 clip 方法前通过使用 save 方法对当前画布区域进行保存,并在以后的任意时间通过restore方法对其进行恢复。
  292. */
  293. ctx.clip();
  294. // 画头像
  295. ctx.drawImage(src, x, y, w, h);
  296. // 恢复之前保存的绘图上下文。
  297. ctx.restore();
  298. // 将之前在绘图上下文中的描述(路径、变形、样式)画到 canvas 中。
  299. ctx.draw(true);
  300. },
  301. //绘制圆角矩形
  302. roundRect: function(ctx, x, y, w, h, r, c = '#fff') {
  303. if (w < 2 * r) { r = w / 2; }
  304. if (h < 2 * r) { r = h / 2; }
  305. ctx.beginPath();
  306. ctx.fillStyle = c;
  307. ctx.arc(x + r, y + r, r, Math.PI, Math.PI * 1.5);
  308. ctx.moveTo(x + r, y);
  309. ctx.lineTo(x + w - r, y);
  310. ctx.lineTo(x + w, y + r);
  311. ctx.arc(x + w - r, y + r, r, Math.PI * 1.5, Math.PI * 2);
  312. ctx.lineTo(x + w, y + h - r);
  313. ctx.lineTo(x + w - r, y + h);
  314. ctx.arc(x + w - r, y + h - r, r, 0, Math.PI * 0.5);
  315. ctx.lineTo(x + r, y + h);
  316. ctx.lineTo(x, y + h - r);
  317. ctx.arc(x + r, y + h - r, r, Math.PI * 0.5, Math.PI);
  318. ctx.lineTo(x, y + r);
  319. ctx.lineTo(x + r, y);
  320. ctx.fill();
  321. ctx.closePath();
  322. },
  323. //获取商户优惠劵信息
  324. getCouponList: function(id) {
  325. let that = this;
  326. let data;
  327. data = {
  328. status: 0,
  329. merchantId: id,
  330. pageNum: 1,
  331. pageSize: 15
  332. };
  333. Http.post({
  334. url: config.api.listByMerchant,
  335. data: data
  336. })
  337. .then(res => {
  338. if (res.data && res.data.qrCode) {
  339. that.setData({
  340. qrCode: res.data.qrCode,
  341. })
  342. }
  343. that.setData({
  344. couponList: res.data.page.list
  345. });
  346. })
  347. .catch(err => {
  348. wx.showToast({
  349. title: err.errMsg,
  350. icon: "none",
  351. duration: 2000,
  352. mask: false
  353. });
  354. });
  355. },
  356. //获取头像和昵称
  357. getUserInfo: function (options) {
  358. let that = this;
  359. // 获取用户信息
  360. Http.get({
  361. url: config.api.getScore,
  362. data: {}
  363. }).then(res => {
  364. let nickName = '';
  365. if (res.data.nickName == undefined || res.data.nickName == undefined) {
  366. wx.getUserInfo({
  367. success: function (data) {
  368. nickName = data.userInfo.nickName;
  369. that.setData({
  370. nickName: nickName,
  371. avatarUrl: data.userInfo.avatarUrl
  372. });
  373. that.getMallInfo(options);
  374. }
  375. })
  376. } else {
  377. nickName = res.data.nickName;
  378. that.setData({
  379. nickName: nickName,
  380. avatarUrl: res.data.avatarUrl
  381. });
  382. that.getMallInfo(options);
  383. }
  384. });
  385. },
  386. //商场信息
  387. getMallInfo: function (options) {
  388. let that = this;
  389. // 获取用户信息
  390. Http.get({
  391. url: config.api.getMallInfo,
  392. data: {}
  393. }).then(res => {
  394. console.log(res);
  395. that.setData({
  396. mallname: res.data.name
  397. });
  398. that.begainDrawShareImage(options);
  399. });
  400. },
  401. /**
  402. * 保存图片
  403. */
  404. saveImage() {
  405. let that = this;
  406. wx.canvasToTempFilePath({
  407. x: 0,
  408. y: 0,
  409. width: this.data.windowWidth * this.data.canvasScale,
  410. height: this.data.totalHeight * this.data.canvasScale,
  411. canvasId: "myCanvas",
  412. success: function(res) {
  413. that.saveImageToPhotos(res.tempFilePath);
  414. },
  415. fail: function(res) {
  416. wx.showToast({
  417. title: "图片生成失败",
  418. icon: "none",
  419. duration: 2000
  420. });
  421. }
  422. });
  423. },
  424. saveImageToPhotos: function(tempFilePath) {
  425. wx.saveImageToPhotosAlbum({
  426. filePath: tempFilePath,
  427. success(result) {
  428. wx.showToast({
  429. title: "保存成功,从相册中分享到朋友圈吧",
  430. icon: "none",
  431. duration: 4000
  432. });
  433. },
  434. fail: function(err) {
  435. if (
  436. err.errMsg === "saveImageToPhotosAlbum:fail:auth denied" ||
  437. err.errMsg === "saveImageToPhotosAlbum:fail auth deny"
  438. ) {
  439. // 这边微信做过调整,必须要在按钮中触发,因此需要在弹框回调中进行调用
  440. wx.showModal({
  441. title: "提示",
  442. content: "需要您授权保存相册",
  443. showCancel: false,
  444. success: modalSuccess => {
  445. wx.openSetting({
  446. success(settingdata) {
  447. console.log("settingdata", settingdata);
  448. if (settingdata.authSetting["scope.writePhotosAlbum"]) {
  449. wx.showModal({
  450. title: "提示",
  451. content: "获取权限成功,再次点击图片即可保存",
  452. showCancel: false
  453. });
  454. } else {
  455. wx.showModal({
  456. title: "提示",
  457. content: "获取权限失败,将无法保存到相册哦~",
  458. showCancel: false
  459. });
  460. }
  461. },
  462. fail(failData) {
  463. console.log("failData", failData);
  464. },
  465. complete(finishData) {
  466. console.log("finishData", finishData);
  467. }
  468. });
  469. }
  470. });
  471. }
  472. }
  473. });
  474. }
  475. });