|
- const Http = require("../../../../utils/HttpBasics");
- const imgurl = require("../../../../utils/imgurl");
- const config = require("../../../../config/config");
- const extConfig = wx.getExtConfigSync ? wx.getExtConfigSync() : {}
- let imgOrgUrl = extConfig.attr.imgOrgUrl;
- let imgOrgUrl1 = extConfig.attr.imgOrgUrl1;
- let imgNewUrl = extConfig.attr.imgNewUrl;
- let imgNewUrl1 = extConfig.attr.imgNewUrl;
- let app = getApp();
- Page({
-
- /**
- * 页面的初始数据
- */
- data: {
- poterbg: imgurl.poterbg.url,
- teljpgUrl: imgurl.teljpg.url,
- share01: imgurl.share01.url,
- actUrl: imgurl.act.url,
- poster: './../../../../assets/images/picture.png',
- page: 1,
- imglist: null,
- shopVoList: [],
- couponList: [], //活动劵列表
- qrCodeL:'',//小程序码
- currentTab: 0,
- isshare: false,
- showpost: false,
- imgHeight: 0,
- id: null,
- windowWidth: wx.getSystemInfoSync().windowWidth,
- windowHeight: wx.getSystemInfoSync().screenHeight,
- totalHeight: 0,
- canvasScale: 1.0, // 画布放大的倍数,因为如果保存的是一倍的分享图片的话,分享图会有点虚。所以保存的时候,canvasScale设置为2.0,wxss 里面的left: 500%;打开注释。就可保存两倍的分享图
- },
- /**
- * 分享海报的生成
- */
- //关闭海报
- closePoste: function() {
- this.setData({
- showpost: false
- })
- },
-
- /**
- * 生命周期函数--监听页面加载
- */
- onLoad: function(options) {
- let that = this;
- if (options && options.id) {
- this.setData({
- id: options.id
- });
- that.getList(options.id);
- that.getCouponList(options.id);
- this.setData({
- currentTab: 0
- })
- }
- this.getUserInfo()
- },
- /**
- * 绘制分享海报
- */
- begainDrawShareImage() {
-
- wx.showLoading({
- title: '生成中...',
- })
-
- var that = this
- // 适配屏幕
- let scale = this.data.windowWidth / 375.0
-
- this.setData({
- totalHeight: 767 * scale
- })
- // 获取Canvas
- let ctx = wx.createCanvasContext('myCanvas')
- // 获取宽高
- let wW = that.data.windowWidth;
- let wH = that.data.windowHeight;
- // 放大 因为不放大的话,生成的分享图会模糊。暂时先注释
- ctx.scale(this.data.canvasScale, this.data.canvasScale)
-
- // 绘制背景图
- let bgimg1 = this.data.poterbg + `?imageView/2/w/${wW}/h/${wH}`
- let bgimg2 = bgimg1.replace(imgOrgUrl1, imgNewUrl)
- ctx.drawImage(bgimg2, 0, 0, wW, wH);
- //绘制头像
- let avatar1 = this.data.avatarUrl + `?imageView/2/w/${wW}/h/${wH}`
- let avatar2 = avatar1.replace(imgOrgUrl, imgNewUrl)
- ctx.drawImage(avatar2, 15, 10, 30*scale,30*scale);
- // 首先要绘制顶部的背景图片,因为它在最底层,然后才能绘制其他内容
- let topImageWidth = parseInt(315 * scale) // 因为小数有时候会请求不到图片,所以转成int
- let topImageHeight = parseInt(200 * scale)
- let src1 = this.data.data.merchantImgUrl + `?imageView/2/w/${topImageWidth}/h/${topImageHeight}`
- let src2 = src1.replace(imgOrgUrl, imgNewUrl)
- wx.getImageInfo({
- src: src2,
- success: function(res) {
- // 绘制白色背景
- ctx.setFillStyle('#fff')
- ctx.fillRect(25,45, topImageWidth+10, topImageHeight+10)
- ctx.draw()
- ctx.drawImage(res.path, 30,50, topImageWidth, topImageHeight)
- that.drawOtherContent(ctx, scale)
- that.drawOtherImage(ctx, scale)
- }
- })
- },
- //获取头像和昵称
- getUserInfo: function () {
- let that = this;
- // 获取用户信息
- Http.get({
- url: config.api.getScore,
- data: {}
- })
- .then(res => {
- console.log(res)
- that.setData({
- nickName: res.data.nickName,
- avatarUrl: res.data.avatarUrl
- })
- })
- },
- // 绘制除了图片之外的剩余内容
- drawOtherContent(ctx, scale) {
- //昵称
- this.drawNormalText(ctx, this.data.nickName+" 向您推荐", 50 * scale, 25 * scale, 16 * scale, '#fff', 'left', 'middle', scale);
- //店铺名
- this.drawNormalText(ctx, this.data.data.merchantName, 180 * scale, 280 * scale, 30 * scale, '#fff', 'center', 'middle', scale);
- //店铺电话
- // this.drawNormalText(ctx, "商铺电话:"+this.data.data.merchantLinkPhone, 100 * scale, 405 * scale, 16 * scale, '#000', 'left', 'middle', scale);
- if (this.data.couponList.length>0) {
- for (let i = 0; i < this.data.couponList.length; i++) {
- // 第一个商品信息
- this.drawNormalText(ctx, this.substrTile(this.data.couponList[1].title), 40 * scale, 396 * scale, 14 * scale, '#000', 'left', 'middle', scale);
- this.drawNormalText(ctx, "价格:"+this.data.couponList[1].salePrice, 40 * scale, 416 * scale, 14 * scale, '#FF3232', 'left', 'middle', scale);
- //第二个商品信息
- this.drawNormalText(ctx, this.substrTile(this.data.couponList[2].title), 155 * scale, 396 * scale, 14 * scale, '#000', 'left', 'middle', scale);
- this.drawNormalText(ctx, "价格:"+this.data.couponList[2].salePrice, 155 * scale, 416 * scale, 14 * scale, '#FF3232', 'left', 'middle', scale);
- //第三个商品信息
- this.drawNormalText(ctx, this.substrTile(this.data.couponList[3].title), 270 * scale, 396 * scale, 14 * scale, '#000', 'left', 'middle', scale);
- this.drawNormalText(ctx, "价格:"+this.data.couponList[3].salePrice, 270 * scale, 416 * scale, 14 * scale, '#FF3232', 'left', 'middle', scale);
- }
-
- }
- this.drawNormalText(ctx, "长按二维码识别小程序进店逛逛", 85 * scale, 570 * scale, 16 * scale, '#000', 'left', 'middle', scale);
- ctx.draw(true)
-
- },
- //截取商品名
- substrTile(str){
- return str.substr(0,5)+'...'
- },
- // 绘制活动图片
- drawOtherImage(ctx, scale) {
- var that = this
- // 如果该商户有活动商品
- if (that.data.couponList.length>0) {
- let cotentImageWidth = parseInt(80 * scale)
- let cotentImageHeight = parseInt(80 * scale)
- for (let i = 0; i < that.data.couponList.length; i++) {
- let imageItem = that.data.couponList[i].coverImg
- let src1 = imageItem + `?imageView/2/w/${cotentImageWidth}/h/${cotentImageHeight}`
-
- let src2 = src1.replace(imgOrgUrl, imgNewUrl)
- wx.getImageInfo({
- src: src2,
- success: function(res) {
- // ctx.setFillStyle('rgba(255,255,255,0.3)')
- // ctx.fillRect(25,300, 300*scale, 200*scale)
- // ctx.draw(true)
- ctx.drawImage(res.path, 45 * scale + i * 110 * scale, 300 * scale, cotentImageWidth, cotentImageHeight)
- }
- })
- }
-
- }
- if (this.data.qrCode) {
- let coImageWidth = parseInt(120 * scale)
- let coImageHeight = parseInt(120 * scale)
- let src1 = this.data.qrCode + `?imageView/2/w/${coImageWidth}/h/${coImageHeight}`
- let src2 = src1.replace(imgOrgUrl, imgNewUrl)
- wx.getImageInfo({
- src: src2,
- success: function(res) {
- ctx.drawImage(res.path, 115 * scale, 425 * scale, coImageWidth, coImageHeight)
- ctx.draw(true)
- }
- })
- }
-
- wx.hideLoading()
- },
- // 绘制只有一行的文字
- drawNormalText(ctx, str, x, y, font, style, align, baseLine) {
- ctx.setFontSize(font);
- ctx.setFillStyle(style);
- ctx.setTextAlign(align);
- ctx.setTextBaseline(baseLine);
- ctx.fillText(str, x, y);
- },
-
-
- /*
- * 绘制多行文本,自动换行,超出添加...
- *
- str:要绘制的字符串
- canvas:canvas对象
- initX:绘制字符串起始x坐标
- initY:绘制字符串起始y坐标
- lineHeight:字行高,自己定义个值即可
- maxWidth: 文本最大宽度
- row: 最大行数
- */
- canvasTextAutoLine: function(str, ctx, initX, initY, lineHeight, maxWidth, row = 1) {
- var lineWidth = 0;
- var lastSubStrIndex = 0;
- var currentRow = 1;
- for (let i = 0; i < str.length; i++) {
- lineWidth += ctx.measureText(str[i]).width;
- if (lineWidth > maxWidth) {
- currentRow++;
- let newStr = str.substring(lastSubStrIndex, i)
- if (currentRow > row && str.length > i) {
- newStr = str.substring(lastSubStrIndex, i - 2) + '...'
- }
- ctx.fillText(newStr, initX, initY);
- initY += lineHeight;
- lineWidth = 0;
- lastSubStrIndex = i;
-
- if (currentRow > row) {
- break;
- }
- }
- if (i == str.length - 1) {
- ctx.fillText(str.substring(lastSubStrIndex, i + 1), initX, initY);
- }
- }
- },
- /**
- * 图片预览方法
- * 此处注意的一点就是,调用 "wx.previewImage"时,第二个参数要求为数组形式哦
- * 当然,做过图片上传功能的应该会注意到,如果涉及到多张图片预览,图片链接数组集合即为参数 urls!
- */
- previewImage: function() {
- wx.canvasToTempFilePath({
- x: 0,
- y: 0,
- width: this.data.windowWidth * this.data.canvasScale,
- height: this.data.totalHeight * this.data.canvasScale,
- canvasId: 'myCanvas',
- success: function (res) {
- var current = res.tempFilePath;
- wx.previewImage({
- current: current,
- urls: [current]
- })
- },
- fail: function (res) {
- wx.showToast({
- title: '图片生成失败',
- icon: 'none',
- duration: 2000
- })
- }
- })
-
- },
- // 保存图片
- saveImage(){
- let that = this
- wx.canvasToTempFilePath({
- x: 0,
- y: 0,
- width: this.data.windowWidth * this.data.canvasScale,
- height: this.data.totalHeight * this.data.canvasScale,
- canvasId: 'myCanvas',
- success: function (res) {
- that.saveImageToPhotos(res.tempFilePath);
- },
- fail: function (res) {
- wx.showToast({
- title: '图片生成失败',
- icon: 'none',
- duration: 2000
- })
- }
- })
- },
- saveImageToPhotos: function (tempFilePath) {
- wx.saveImageToPhotosAlbum({
- filePath: tempFilePath,
- success(result) {
- wx.showToast({
- title: '保存成功,从相册中分享到朋友圈吧',
- icon: 'none',
- duration: 4000
- })
- },
- fail: function (err) {
- if (err.errMsg === "saveImageToPhotosAlbum:fail:auth denied" || err.errMsg === "saveImageToPhotosAlbum:fail auth deny") {
- // 这边微信做过调整,必须要在按钮中触发,因此需要在弹框回调中进行调用
- wx.showModal({
- title: '提示',
- content: '需要您授权保存相册',
- showCancel: false,
- success:modalSuccess=>{
- wx.openSetting({
- success(settingdata) {
- console.log("settingdata", settingdata)
- if (settingdata.authSetting['scope.writePhotosAlbum']) {
- wx.showModal({
- title: '提示',
- content: '获取权限成功,再次点击图片即可保存',
- showCancel: false,
- })
- } else {
- wx.showModal({
- title: '提示',
- content: '获取权限失败,将无法保存到相册哦~',
- showCancel: false,
- })
- }
- },
- fail(failData) {
- console.log("failData",failData)
- },
- complete(finishData) {
- console.log("finishData", finishData)
- }
- })
- }
- })
- }
- },
- })
- },
- /**
- * 拨打电话
- */
- phone: function(e) {
- let that = this;
- wx.makePhoneCall({
- phoneNumber: e.target.dataset.merchantlinkphone
- });
- },
- /**
- * 显示分享弹框
- */
- showshare: function() {
- this.setData({
- isshare: true,
- })
- },
- /**
- * 隐藏分享弹框
- */
- hidemodal: function() {
- this.setData({
- isshare: false,
- })
- },
- //滑动切换
- swiperTabView: function(e) {
- this.setData({
- currentTab: e.detail.current
- });
- },
- /**
- * 隐藏分享海报
- */
- hideposter: function() {
- this.setData({
- showpost: false,
- })
- },
- /**
- * 显示分享海报
- */
- showPoster: function() {
- this.setData({
- showpost: true,
- })
- this.begainDrawShareImage();
- },
- //点击切换
- clickTab: function(e) {
- if (this.data.currentTab === e.target.dataset.current) {
- return false;
- } else {
- this.setData({
- currentTab: e.target.dataset.current
- })
- }
- },
- /**
- * 获取商户详情
- */
- getList: function(id) {
- let that = this;
- let data;
- data = {
- pageNum: that.data.page,
- pageSize: 15,
- id: id
- }
- Http.get({
- url: config.api.merchantList,
- data: data
- }).then(res => {
- let imgList = [];
- imgList.push(res.data.list[0].merchantImgUrl)
- that.setData({
- data: res.data.list[0],
- shopVoList: res.data.list[0].shopVoList,
- imglist: res.data.list[0].coverPicture == '[]' ? imgList:JSON.parse(res.data.list[0].coverPicture),
- })
- })
- .catch(err => {
- wx.showToast({
- title: err.errMsg,
- icon: 'none',
- duration: 2000,
- mask: false
- });
- })
- },
- /**
- * 获取商户活动信息 券类型(1.满减券,2.代金券,3.团购券,4.礼品券,5.停车券,6.多商户券,8.砍价券,9.团购券,50.积分券,51.积分停车券 100.消费卡)
- * 投放频道:(1.列表, 2.限时抢购, 3. banner图 4. 游戏 5.卡频道 6.砍价频道 7.拼团频道 8专题)
- */
- getCouponList: function(id) {
- let that = this;
- let data;
- data = {
- status: 0,
- merchantId: id,
- pageNum: that.data.page,
- pageSize: 15,
- }
- Http.get({
- url: config.api.listByMerchant,
- data: data
- }).then(res => {
- that.setData({
- couponList: res.data.page.list,
- qrCode:res.data.qrCode,
- })
- })
- .catch(err => {
- wx.showToast({
- title: err.errMsg,
- icon: 'none',
- duration: 2000,
- mask: false
- });
- })
- },
- /**
- * 获取多商铺列表
- */
- shopList: function(e) {
- wx.navigateTo({
- url: `/pages/index/merchantList/index?id=${e.currentTarget.dataset.id}`
- })
- },
- // onShareAppMessage: function(res) {
- // var that = this;
- // var shareObj = {
- // title: that.data.data.title,
- // path: `/pages/index/index?couponChannelId=${that.data.couponChannelId}`,
- // success: function(res) {
- // if (res.errMsg == 'shareAppMessage:ok') {}
- // },
- // fail: function(error) {
- // if (res.errMsg == 'shareAppMessage:fail cancel') {} else if (res.errMsg == 'shareAppMessage:fail') {}
- // }
- // };
- // // 来自页面内的按钮的转发
- // if (res.from === 'button') {
- // var eData = res.target.dataset.id;
- // shareObj.path = `/pages/index/index?couponChannelId=${eData}`;
- // }
- // // 返回shareObj
- // return shareObj;
- // },
- })
|