C端小程序
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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