C端小程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

416 lines
13 KiB

  1. var config = require("../../config/config.js");
  2. var app = getApp();
  3. const Http = require("../../utils/HttpBasics");
  4. const util = require("../../utils/util");
  5. const imgurl = require("../../utils/imgurl");
  6. const extConfig = wx.getExtConfigSync ? wx.getExtConfigSync() : {}
  7. let imgOrgUrl = extConfig.attr.imgOrgUrl;
  8. let imgOrgUrl1 = extConfig.attr.imgOrgUrl1;
  9. let imgNewUrl = extConfig.attr.imgNewUrl;
  10. let imgNewUrl1 = extConfig.attr.imgNewUrl1;
  11. Page({
  12. /**
  13. * 页面的初始数据
  14. */
  15. data: {
  16. poterbg: imgurl.poterbg.url,
  17. windowWidth: wx.getSystemInfoSync().windowWidth,
  18. windowHeight: wx.getSystemInfoSync().screenHeight,
  19. totalHeight: 0,
  20. qrCodeL: '', //小程序码
  21. couponList: [], //活动劵列表
  22. canvasScale: 1.0, // 画布放大的倍数,因为如果保存的是一倍的分享图片的话,分享图会有点虚。所以保存的时候,canvasScale设置为2.0,wxss 里面的left: 500%;打开注释。就可保存两倍的分享图
  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();
  37. this.begainDrawShareImage(options);
  38. },
  39. /**
  40. * 获取劵详情
  41. */
  42. getDetail: function (couponChannelId) {
  43. let that = this;
  44. // 获取用户信息
  45. Http.get({
  46. url: config.api.couponDetail,
  47. data: {
  48. couponChannelId: couponChannelId
  49. }
  50. })
  51. .then(res => {
  52. console.log(res)
  53. that.setData({
  54. data: res.data,
  55. })
  56. })
  57. },
  58. /**
  59. * 获取商户详情
  60. */
  61. getList: function(id) {
  62. let that = this;
  63. let data;
  64. data = {
  65. pageNum: 1,
  66. pageSize: 15,
  67. id: id
  68. }
  69. Http.get({
  70. url: config.api.merchantList,
  71. data: data
  72. }).then(res => {
  73. that.setData({
  74. data: res.data.list[0],
  75. })
  76. })
  77. .catch(err => {
  78. wx.showToast({
  79. title: err.errMsg,
  80. icon: 'none',
  81. duration: 2000,
  82. mask: false
  83. });
  84. })
  85. },
  86. /**
  87. * 绘制分享海报
  88. */
  89. begainDrawShareImage(options) {
  90. wx.showLoading({
  91. title: '生成中...',
  92. })
  93. var that = this
  94. // 适配屏幕
  95. let scale = this.data.windowWidth / 375.0
  96. this.setData({
  97. totalHeight: 767 * scale
  98. })
  99. // 获取Canvas
  100. let ctx = wx.createCanvasContext('myCanvas')
  101. // 获取宽高
  102. let wW = that.data.windowWidth;
  103. let wH = that.data.windowHeight;
  104. // 放大 因为不放大的话,生成的分享图会模糊。暂时先注释
  105. ctx.scale(this.data.canvasScale, this.data.canvasScale)
  106. // 首先要绘制顶部的背景图片,因为它在最底层,然后才能绘制其他内容
  107. let bgimg1 = that.data.poterbg + `?imageView/2/w/${wW}/h/${wH}`
  108. let bgimg2 = bgimg1.replace(this.getimgurl(this.data.poterbg), this.getnewimgurl(this.data.poterbg))
  109. wx.getImageInfo({
  110. src: bgimg2,
  111. success: function(res) {
  112. ctx.drawImage(res.path, 0, 0, wW-20, wH);
  113. that.drawshopimg(ctx, scale,options);
  114. }
  115. })
  116. },
  117. //判断图片路径
  118. getimgurl(str){
  119. if(str.indexOf(imgOrgUrl)!=-1){
  120. return imgOrgUrl
  121. }else if(str.indexOf(imgOrgUrl1)!=-1){
  122. return imgOrgUrl1;
  123. }
  124. },
  125. getnewimgurl(str){
  126. if(str.indexOf(imgOrgUrl)!=-1){
  127. return imgNewUrl
  128. }else if(str.indexOf(imgOrgUrl1)!=-1){
  129. return imgNewUrl1;
  130. }
  131. },
  132. //绘制商品图片
  133. drawshopimg(ctx, scale,options){
  134. var that = this
  135. let topImageWidth = parseInt(315 * scale) // 因为小数有时候会请求不到图片,所以转成int
  136. let topImageHeight = parseInt(200 * scale)
  137. let src1=""
  138. let src2=''
  139. //劵详情海报
  140. if(options && options.couponChannelId){
  141. src1 = that.data.data.coverImg + `?imageView/2/w/${topImageWidth}/h/${topImageHeight}`
  142. src2 = src1.replace(that.getimgurl(that.data.data.coverImg), that.getnewimgurl(that.data.data.coverImg))
  143. this.drawNormalText(ctx, "长按二维码识别小程序享优惠", 85 * scale, 570 * scale, 16 * scale, '#000', 'left', 'middle', scale);
  144. }else if(options && options.merchantId){//商户详情海报
  145. src1 = that.data.data.merchantImgUrl + `?imageView/2/w/${topImageWidth}/h/${topImageHeight}`
  146. src2 = src1.replace(that.getimgurl(that.data.data.merchantImgUrl), that.getnewimgurl(that.data.data.merchantImgUrl))
  147. this.drawNormalText(ctx, "长按二维码识别小程序进店逛逛", 85 * scale, 570 * scale, 16 * scale, '#000', 'left', 'middle', scale);
  148. }
  149. wx.getImageInfo({
  150. src: src2,
  151. success: function(res) {
  152. if(options && options.merchantId){
  153. that.drawOtherImage(ctx, scale)
  154. that.drawOtherContent(ctx, scale)
  155. // 绘制白色背景
  156. ctx.setFillStyle('#fff')
  157. ctx.fillRect(15, 45, topImageWidth + 10, topImageHeight + 10)
  158. ctx.drawImage(res.path, 20, 50, topImageWidth, topImageHeight)
  159. ctx.draw()
  160. }else if((options && options.couponChannelId)){
  161. ctx.setFillStyle('#fff')
  162. ctx.fillRect(15, 95, topImageWidth + 10, topImageHeight + 10)
  163. ctx.drawImage(res.path, 20, 100, topImageWidth, topImageHeight)
  164. ctx.draw()
  165. that.drawCouponContent(ctx, scale)
  166. that.drawqrCode(ctx, scale);
  167. }
  168. wx.hideLoading()
  169. }
  170. })
  171. },
  172. // 商户海报绘制
  173. drawOtherContent(ctx, scale) {
  174. //昵称
  175. this.drawNormalText(ctx, this.data.nickName+" 向您推荐", 20 * scale, 25 * scale, 16 * scale, '#fff', 'left', 'middle', scale);
  176. //店铺名
  177. this.drawNormalText(ctx, this.data.data.merchantName, 180 * scale, 280 * scale, 30 * scale, '#fff', 'center', 'middle', scale);
  178. if (this.data.couponList.length > 0) {
  179. for (let i = 0; i < this.data.couponList.length; i++) {
  180. // 第一个商品信息
  181. this.drawNormalText(ctx, this.substrTile(this.data.couponList[1].title), 30 * scale, 396 * scale, 14 * scale, '#000', 'left', 'middle', scale);
  182. this.drawNormalText(ctx, "价格:" + this.data.couponList[1].salePrice, 30 * scale, 416 * scale, 14 * scale, '#FF3232', 'left', 'middle', scale);
  183. //第二个商品信息
  184. this.drawNormalText(ctx, this.substrTile(this.data.couponList[2].title), 145 * scale, 396 * scale, 14 * scale, '#000', 'left', 'middle', scale);
  185. this.drawNormalText(ctx, "价格:" + this.data.couponList[2].salePrice, 145 * scale, 416 * scale, 14 * scale, '#FF3232', 'left', 'middle', scale);
  186. //第三个商品信息
  187. this.drawNormalText(ctx, this.substrTile(this.data.couponList[3].title), 260 * scale, 396 * scale, 14 * scale, '#000', 'left', 'middle', scale);
  188. this.drawNormalText(ctx, "价格:" + this.data.couponList[3].salePrice, 260 * scale, 416 * scale, 14 * scale, '#FF3232', 'left', 'middle', scale);
  189. }
  190. }
  191. ctx.draw(true)
  192. },
  193. // 劵海报绘制
  194. drawCouponContent(ctx, scale) {
  195. //昵称
  196. this.drawNormalText(ctx, this.data.data.title, 20 * scale, 25 * scale, 30 * scale, '#fff', 'left', 'middle', scale);
  197. this.drawNormalText(ctx, "售价:"+this.data.data.salePriceStr, 50 * scale, 65 * scale, 30 * scale, '#fff', 'left', 'middle', scale);
  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. if(this.data.qrCode){
  207. src1 = this.data.qrCode + `?imageView/2/w/${coImageWidth}/h/${coImageHeight}`
  208. src2 = src1.replace(this.getimgurl(this.data.qrCode), this.getnewimgurl(this.data.qrCode))
  209. }else{
  210. src1 = this.data.data.qrCode + `?imageView/2/w/${coImageWidth}/h/${coImageHeight}`
  211. src2 = src1.replace(this.getimgurl(this.data.data.qrCode), this.getnewimgurl(this.data.data.qrCode))
  212. }
  213. wx.getImageInfo({
  214. src: src2,
  215. success: function(res) {
  216. ctx.drawImage(res.path, 125 * scale, 425 * scale, coImageWidth, coImageHeight)
  217. ctx.draw(true)
  218. }
  219. })
  220. },
  221. // 绘制活动图片
  222. drawOtherImage(ctx, scale) {
  223. var that = this
  224. // 如果该商户有活动商品
  225. if (that.data.couponList.length > 0) {
  226. let cotentImageWidth = parseInt(80 * scale)
  227. let cotentImageHeight = parseInt(80 * scale)
  228. for (let i = 0; i < that.data.couponList.length; i++) {
  229. let imageItem = that.data.couponList[i].coverImg
  230. let src1 = imageItem + `?imageView/2/w/${cotentImageWidth}/h/${cotentImageHeight}`
  231. let src2 = src1.replace(this.getimgurl(imageItem), this.getnewimgurl(imageItem))
  232. wx.getImageInfo({
  233. src: src2,
  234. success: function(res) {
  235. ctx.drawImage(res.path, 35 * scale + i * 110 * scale, 300 * scale, cotentImageWidth, cotentImageHeight)
  236. }
  237. })
  238. }
  239. }
  240. that.drawqrCode(ctx, scale);
  241. },
  242. //截取商品名
  243. substrTile(str) {
  244. return str.substr(0, 5) + '...'
  245. },
  246. // 绘制只有一行的文字
  247. drawNormalText(ctx, str, x, y, font, style, align, baseLine) {
  248. ctx.setFontSize(font);
  249. ctx.setFillStyle(style);
  250. ctx.setTextAlign(align);
  251. ctx.setTextBaseline(baseLine);
  252. ctx.fillText(str, x, y);
  253. },
  254. /*
  255. * 绘制多行文本,自动换行,超出添加...
  256. *
  257. */
  258. canvasTextAutoLine: function(str, ctx, initX, initY, lineHeight, maxWidth, row = 1) {
  259. var lineWidth = 0;
  260. var lastSubStrIndex = 0;
  261. var currentRow = 1;
  262. for (let i = 0; i < str.length; i++) {
  263. lineWidth += ctx.measureText(str[i]).width;
  264. if (lineWidth > maxWidth) {
  265. currentRow++;
  266. let newStr = str.substring(lastSubStrIndex, i)
  267. if (currentRow > row && str.length > i) {
  268. newStr = str.substring(lastSubStrIndex, i - 2) + '...'
  269. }
  270. ctx.fillText(newStr, initX, initY);
  271. initY += lineHeight;
  272. lineWidth = 0;
  273. lastSubStrIndex = i;
  274. if (currentRow > row) {
  275. break;
  276. }
  277. }
  278. if (i == str.length - 1) {
  279. ctx.fillText(str.substring(lastSubStrIndex, i + 1), initX, initY);
  280. }
  281. }
  282. },
  283. //获取商户优惠劵信息
  284. getCouponList: function(id) {
  285. let that = this;
  286. let data;
  287. data = {
  288. status: 0,
  289. merchantId: id,
  290. pageNum: 1,
  291. pageSize: 15,
  292. }
  293. Http.get({
  294. url: config.api.listByMerchant,
  295. data: data
  296. }).then(res => {
  297. that.setData({
  298. couponList: res.data.page.list,
  299. qrCode: res.data.qrCode,
  300. })
  301. })
  302. .catch(err => {
  303. wx.showToast({
  304. title: err.errMsg,
  305. icon: 'none',
  306. duration: 2000,
  307. mask: false
  308. });
  309. })
  310. },
  311. //获取头像和昵称
  312. getUserInfo: function() {
  313. let that = this;
  314. // 获取用户信息
  315. Http.get({
  316. url: config.api.getScore,
  317. data: {}
  318. })
  319. .then(res => {
  320. console.log(res)
  321. that.setData({
  322. nickName: res.data.nickName,
  323. avatarUrl: res.data.avatarUrl
  324. })
  325. })
  326. },
  327. /**
  328. * 保存图片
  329. */
  330. saveImage() {
  331. let that = this
  332. wx.canvasToTempFilePath({
  333. x: 0,
  334. y: 0,
  335. width: this.data.windowWidth * this.data.canvasScale,
  336. height: this.data.totalHeight * this.data.canvasScale,
  337. canvasId: 'myCanvas',
  338. success: function(res) {
  339. that.saveImageToPhotos(res.tempFilePath);
  340. },
  341. fail: function(res) {
  342. wx.showToast({
  343. title: '图片生成失败',
  344. icon: 'none',
  345. duration: 2000
  346. })
  347. }
  348. })
  349. },
  350. saveImageToPhotos: function(tempFilePath) {
  351. wx.saveImageToPhotosAlbum({
  352. filePath: tempFilePath,
  353. success(result) {
  354. wx.showToast({
  355. title: '保存成功,从相册中分享到朋友圈吧',
  356. icon: 'none',
  357. duration: 4000
  358. })
  359. },
  360. fail: function(err) {
  361. if (err.errMsg === "saveImageToPhotosAlbum:fail:auth denied" || err.errMsg === "saveImageToPhotosAlbum:fail auth deny") {
  362. // 这边微信做过调整,必须要在按钮中触发,因此需要在弹框回调中进行调用
  363. wx.showModal({
  364. title: '提示',
  365. content: '需要您授权保存相册',
  366. showCancel: false,
  367. success: modalSuccess => {
  368. wx.openSetting({
  369. success(settingdata) {
  370. console.log("settingdata", settingdata)
  371. if (settingdata.authSetting['scope.writePhotosAlbum']) {
  372. wx.showModal({
  373. title: '提示',
  374. content: '获取权限成功,再次点击图片即可保存',
  375. showCancel: false,
  376. })
  377. } else {
  378. wx.showModal({
  379. title: '提示',
  380. content: '获取权限失败,将无法保存到相册哦~',
  381. showCancel: false,
  382. })
  383. }
  384. },
  385. fail(failData) {
  386. console.log("failData", failData)
  387. },
  388. complete(finishData) {
  389. console.log("finishData", finishData)
  390. }
  391. })
  392. }
  393. })
  394. }
  395. },
  396. })
  397. },
  398. /**
  399. * 用户点击右上角分享
  400. */
  401. onShareAppMessage: function () {
  402. }
  403. })