C端小程序
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

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