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