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.

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