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.

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