C端小程序
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

443 行
12 KiB

  1. const Http = require("../../../../utils/HttpBasics");
  2. const imgurl = require("../../../../utils/imgurl");
  3. const config = require("../../../../config/config");
  4. const extConfig = wx.getExtConfigSync ? wx.getExtConfigSync() : {}
  5. let imgOrgUrl = extConfig.attr.imgOrgUrl;
  6. let imgNewUrl = extConfig.attr.imgNewUrl;
  7. let app = getApp();
  8. Page({
  9. /**
  10. * 页面的初始数据
  11. */
  12. data: {
  13. teljpgUrl: imgurl.teljpg.url,
  14. share01: imgurl.share01.url,
  15. actUrl: imgurl.act.url,
  16. poster: './../../../../assets/images/picture.png',
  17. page: 1,
  18. imglist: null,
  19. shopVoList: [],
  20. couponList: [], //活动劵列表
  21. qrCodeL:'',//小程序码
  22. currentTab: 0,
  23. isshare: false,
  24. showpost: false,
  25. imgHeight: 0,
  26. id: null,
  27. windowWidth: 0,
  28. windowHeight: 0,
  29. totalHeight: 0,
  30. canvasScale: 1.0, // 画布放大的倍数,因为如果保存的是一倍的分享图片的话,分享图会有点虚。所以保存的时候,canvasScale设置为2.0,wxss 里面的left: 500%;打开注释。就可保存两倍的分享图
  31. },
  32. /**
  33. * 分享海报的生成
  34. */
  35. //关闭海报
  36. closePoste: function() {
  37. this.setData({
  38. showpost: false
  39. })
  40. },
  41. /**
  42. * 生命周期函数--监听页面加载
  43. */
  44. onLoad: function(options) {
  45. let that = this;
  46. if (options && options.id) {
  47. this.setData({
  48. id: options.id
  49. });
  50. that.getList(options.id);
  51. that.getCouponList(options.id);
  52. this.setData({
  53. currentTab: 0
  54. })
  55. }
  56. // 获取到屏幕的宽高等信息
  57. wx.getSystemInfo({
  58. success: function(res) {
  59. that.setData({
  60. windowWidth: res.windowWidth,
  61. windowHeight: res.windowHeight
  62. })
  63. }
  64. })
  65. },
  66. /**
  67. * 绘制分享海报
  68. */
  69. begainDrawShareImage() {
  70. wx.showLoading({
  71. title: '生成中...',
  72. })
  73. var that = this
  74. // 适配屏幕
  75. let scale = this.data.windowWidth / 375.0
  76. this.setData({
  77. totalHeight: 767 * scale
  78. })
  79. // 获取Canvas
  80. let ctx = wx.createCanvasContext('myCanvas')
  81. // 放大 因为不放大的话,生成的分享图会模糊。暂时先注释
  82. ctx.scale(this.data.canvasScale, this.data.canvasScale)
  83. // 绘制主背景白色
  84. ctx.setFillStyle('#ffffff')
  85. ctx.fillRect(0, 0, this.data.windowWidth, this.data.totalHeight)
  86. ctx.draw()
  87. // 首先要绘制顶部的背景图片,因为它在最底层,然后才能绘制其他内容
  88. let topImageWidth = parseInt(375 * scale) // 因为小数有时候会请求不到图片,所以转成int
  89. let topImageHeight = parseInt(250 * scale)
  90. let src1 = this.data.data.merchantImgUrl + `?imageView/2/w/${topImageWidth}/h/${topImageHeight}`
  91. let src2 = src1.replace(imgOrgUrl, imgNewUrl)
  92. wx.getImageInfo({
  93. src: src2,
  94. success: function(res) {
  95. ctx.drawImage(res.path, 0, 0, topImageWidth, topImageHeight)
  96. // 覆盖黑色蒙层
  97. ctx.setFillStyle('rgba(0,0,0,0.3)')
  98. ctx.fillRect(0, 0, topImageWidth, topImageHeight)
  99. ctx.draw(true)
  100. that.drawOtherContent(ctx, scale)
  101. that.drawOtherImage(ctx, scale)
  102. }
  103. })
  104. },
  105. // 绘制除了图片之外的剩余内容
  106. drawOtherContent(ctx, scale) {
  107. // 绘制中间的灰色背景
  108. ctx.setFillStyle('rgba(246,246,246,1)')
  109. //(x轴,y轴,宽,高)
  110. ctx.fillRect(14 * scale, 230 * scale, 347 * scale, 58 * scale)
  111. //name
  112. ctx.setFillStyle('white');
  113. ctx.setFontSize(30 * scale);
  114. this.canvasTextAutoLine(this.data.data.merchantName, ctx, 20 * scale, 210 * scale, 30 * scale, 258 * scale, 1)
  115. // address
  116. ctx.setFillStyle('#000');
  117. ctx.setFontSize(18 * scale);
  118. this.canvasTextAutoLine("地址:" + this.data.data.shopVoList[0].floorName + this.data.data.shopVoList[0].buildingName + this.data.data.shopVoList[0].shopNumber, ctx, 20 * scale, 265 * scale, 20 * scale, 305 * scale, 1)
  119. // if (this.data.couponList) {
  120. // this.drawNormalText(ctx, '长按右侧小程序码', 102 * scale, 596 * scale, 18 * scale, '#A3A3A3', 'left', 'middle', scale);
  121. // }else{
  122. // this.drawNormalText(ctx, '长按识别小程序码', 102 * scale, 580 * scale, 18 * scale, '#1E1E1E', 'left', 'middle', scale);
  123. // }
  124. ctx.draw(true)
  125. },
  126. // 绘制活动图片
  127. drawOtherImage(ctx, scale) {
  128. var that = this
  129. // 如果该商户有活动商品
  130. if (that.data.couponList.length>0) {
  131. let cotentImageWidth = parseInt(100 * scale)
  132. let cotentImageHeight = parseInt(100 * scale)
  133. for (let i = 0; i < that.data.couponList.length; i++) {
  134. let imageItem = that.data.couponList[i].coverImg
  135. let src1 = imageItem + `?imageView/2/w/${cotentImageWidth}/h/${cotentImageHeight}`
  136. let src2 = src1.replace(imgOrgUrl, imgNewUrl)
  137. wx.getImageInfo({
  138. src: src2,
  139. success: function(res) {
  140. ctx.drawImage(res.path, 15 * scale + i * 120 * scale, 300 * scale, cotentImageWidth, cotentImageHeight)
  141. ctx.draw(true)
  142. }
  143. })
  144. }
  145. //没有活动商品生成小程序码
  146. } else {
  147. if (this.data.qrCode) {
  148. let coImageWidth = parseInt(260 * scale)
  149. let coImageHeight = parseInt(260 * scale)
  150. let src1 = this.data.qrCode + `?imageView/2/w/${coImageWidth}/h/${coImageHeight}`
  151. let src2 = src1.replace(imgOrgUrl, imgNewUrl)
  152. wx.getImageInfo({
  153. src: src2,
  154. success: function(res) {
  155. ctx.drawImage(res.path, 50 * scale, 320 * scale, coImageWidth, coImageHeight)
  156. ctx.draw(true)
  157. }
  158. })
  159. }
  160. }
  161. wx.hideLoading()
  162. },
  163. // 绘制只有一行的文字
  164. drawNormalText(ctx, str, x, y, font, style, align, baseLine) {
  165. ctx.setFontSize(font);
  166. ctx.setFillStyle(style);
  167. ctx.setTextAlign(align);
  168. ctx.setTextBaseline(baseLine);
  169. ctx.fillText(str, x, y);
  170. },
  171. /*
  172. * 绘制多行文本,自动换行,超出添加...
  173. *
  174. str:要绘制的字符串
  175. canvas:canvas对象
  176. initX:绘制字符串起始x坐标
  177. initY:绘制字符串起始y坐标
  178. lineHeight:字行高,自己定义个值即可
  179. maxWidth: 文本最大宽度
  180. row: 最大行数
  181. */
  182. canvasTextAutoLine: function(str, ctx, initX, initY, lineHeight, maxWidth, row = 1) {
  183. var lineWidth = 0;
  184. var lastSubStrIndex = 0;
  185. var currentRow = 1;
  186. for (let i = 0; i < str.length; i++) {
  187. lineWidth += ctx.measureText(str[i]).width;
  188. if (lineWidth > maxWidth) {
  189. currentRow++;
  190. let newStr = str.substring(lastSubStrIndex, i)
  191. if (currentRow > row && str.length > i) {
  192. newStr = str.substring(lastSubStrIndex, i - 2) + '...'
  193. }
  194. ctx.fillText(newStr, initX, initY);
  195. initY += lineHeight;
  196. lineWidth = 0;
  197. lastSubStrIndex = i;
  198. if (currentRow > row) {
  199. break;
  200. }
  201. }
  202. if (i == str.length - 1) {
  203. ctx.fillText(str.substring(lastSubStrIndex, i + 1), initX, initY);
  204. }
  205. }
  206. },
  207. // 保存图片
  208. saveImage(){
  209. let that = this
  210. wx.canvasToTempFilePath({
  211. x: 0,
  212. y: 0,
  213. width: this.data.windowWidth * this.data.canvasScale,
  214. height: this.data.totalHeight * this.data.canvasScale,
  215. canvasId: 'myCanvas',
  216. success: function (res) {
  217. that.saveImageToPhotos(res.tempFilePath);
  218. },
  219. fail: function (res) {
  220. wx.showToast({
  221. title: '图片生成失败',
  222. icon: 'none',
  223. duration: 2000
  224. })
  225. }
  226. })
  227. },
  228. saveImageToPhotos: function (tempFilePath) {
  229. wx.saveImageToPhotosAlbum({
  230. filePath: tempFilePath,
  231. success(result) {
  232. wx.showToast({
  233. title: '保存成功,从相册中分享到朋友圈吧',
  234. icon: 'none',
  235. duration: 4000
  236. })
  237. },
  238. fail: function (err) {
  239. if (err.errMsg === "saveImageToPhotosAlbum:fail:auth denied" || err.errMsg === "saveImageToPhotosAlbum:fail auth deny") {
  240. // 这边微信做过调整,必须要在按钮中触发,因此需要在弹框回调中进行调用
  241. wx.showModal({
  242. title: '提示',
  243. content: '需要您授权保存相册',
  244. showCancel: false,
  245. success:modalSuccess=>{
  246. wx.openSetting({
  247. success(settingdata) {
  248. console.log("settingdata", settingdata)
  249. if (settingdata.authSetting['scope.writePhotosAlbum']) {
  250. wx.showModal({
  251. title: '提示',
  252. content: '获取权限成功,再次点击图片即可保存',
  253. showCancel: false,
  254. })
  255. } else {
  256. wx.showModal({
  257. title: '提示',
  258. content: '获取权限失败,将无法保存到相册哦~',
  259. showCancel: false,
  260. })
  261. }
  262. },
  263. fail(failData) {
  264. console.log("failData",failData)
  265. },
  266. complete(finishData) {
  267. console.log("finishData", finishData)
  268. }
  269. })
  270. }
  271. })
  272. }
  273. },
  274. })
  275. },
  276. /**
  277. * 拨打电话
  278. */
  279. phone: function(e) {
  280. let that = this;
  281. wx.makePhoneCall({
  282. phoneNumber: e.target.dataset.merchantlinkphone
  283. });
  284. },
  285. /**
  286. * 显示分享弹框
  287. */
  288. showshare: function() {
  289. this.setData({
  290. isshare: true,
  291. })
  292. },
  293. /**
  294. * 隐藏分享弹框
  295. */
  296. hidemodal: function() {
  297. this.setData({
  298. isshare: false,
  299. })
  300. },
  301. //滑动切换
  302. swiperTabView: function(e) {
  303. this.setData({
  304. currentTab: e.detail.current
  305. });
  306. },
  307. /**
  308. * 隐藏分享海报
  309. */
  310. hideposter: function() {
  311. this.setData({
  312. showpost: false,
  313. })
  314. },
  315. /**
  316. * 显示分享海报
  317. */
  318. showPoster: function() {
  319. this.setData({
  320. showpost: true,
  321. })
  322. this.begainDrawShareImage();
  323. },
  324. //点击切换
  325. clickTab: function(e) {
  326. if (this.data.currentTab === e.target.dataset.current) {
  327. return false;
  328. } else {
  329. this.setData({
  330. currentTab: e.target.dataset.current
  331. })
  332. }
  333. },
  334. /**
  335. * 获取商户详情
  336. */
  337. getList: function(id) {
  338. let that = this;
  339. let data;
  340. data = {
  341. pageNum: that.data.page,
  342. pageSize: 15,
  343. id: id
  344. }
  345. Http.get({
  346. url: config.api.merchantList,
  347. data: data
  348. }).then(res => {
  349. that.setData({
  350. data: res.data.list[0],
  351. shopVoList: res.data.list[0].shopVoList,
  352. imglist: JSON.parse(res.data.list[0].coverPicture) ? JSON.parse(res.data.list[0].coverPicture) : res.data.list[0].merchantImgUrl,
  353. })
  354. })
  355. .catch(err => {
  356. wx.showToast({
  357. title: err.errMsg,
  358. icon: 'none',
  359. duration: 2000,
  360. mask: false
  361. });
  362. })
  363. },
  364. /**
  365. * 获取商户活动信息 券类型(1.满减券,2.代金券,3.团购券,4.礼品券,5.停车券,6.多商户券,8.砍价券,9.团购券,50.积分券,51.积分停车券 100.消费卡)
  366. * 投放频道:(1.列表, 2.限时抢购, 3. banner图 4. 游戏 5.卡频道 6.砍价频道 7.拼团频道 8专题)
  367. */
  368. getCouponList: function(id) {
  369. let that = this;
  370. let data;
  371. data = {
  372. status: 0,
  373. merchantId: id,
  374. pageNum: that.data.page,
  375. pageSize: 15,
  376. }
  377. Http.get({
  378. url: config.api.listByMerchant,
  379. data: data
  380. }).then(res => {
  381. that.setData({
  382. couponList: res.data.page.list,
  383. qrCode:res.data.qrCode,
  384. })
  385. })
  386. .catch(err => {
  387. wx.showToast({
  388. title: err.errMsg,
  389. icon: 'none',
  390. duration: 2000,
  391. mask: false
  392. });
  393. })
  394. },
  395. /**
  396. * 获取多商铺列表
  397. */
  398. shopList: function(e) {
  399. wx.navigateTo({
  400. url: `/pages/index/merchantList/index?id=${e.currentTarget.dataset.id}`
  401. })
  402. },
  403. // onShareAppMessage: function(res) {
  404. // var that = this;
  405. // var shareObj = {
  406. // title: that.data.data.title,
  407. // path: `/pages/index/index?couponChannelId=${that.data.couponChannelId}`,
  408. // success: function(res) {
  409. // if (res.errMsg == 'shareAppMessage:ok') {}
  410. // },
  411. // fail: function(error) {
  412. // if (res.errMsg == 'shareAppMessage:fail cancel') {} else if (res.errMsg == 'shareAppMessage:fail') {}
  413. // }
  414. // };
  415. // // 来自页面内的按钮的转发
  416. // if (res.from === 'button') {
  417. // var eData = res.target.dataset.id;
  418. // shareObj.path = `/pages/index/index?couponChannelId=${eData}`;
  419. // }
  420. // // 返回shareObj
  421. // return shareObj;
  422. // },
  423. })