C端小程序
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

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