|
- const navigationBarHeight = (getApp().statusBarHeight + 44) + 'px'
- var config = require("../../config/config.js");
- var app = getApp();
- const Http = require("../../utils/HttpBasics");
- const util = require("../../utils/util");
- const imgurl = require("../../utils/imgurl");
- Page({
- /**
- * 页面的初始数据
- */
- data: {
- navigationBarHeight,
- poterbg: imgurl.poterbg.url,
- windowWidth: wx.getSystemInfoSync().windowWidth,
- windowHeight: wx.getSystemInfoSync().screenHeight,
- totalHeight: 0,
- isdisable: true,
- mallname: '', //商场名字
- qrCodeL: "", //小程序码
- couponList: [], //活动劵列表
- canvasScale: 1.0 // 画布放大的倍数,因为如果保存的是一倍的分享图片的话,分享图会有点虚。所以保存的时候,canvasScale设置为2.0,wxss 里面的left: 500%;打开注释。就可保存两倍的分享图
- },
-
- /**
- * 生命周期函数--监听页面加载
- */
- onLoad: function(options) {
- this.setCanvas()
- },
- onShow: function(){
-
- },
-
- //绘制微信头像
- drawAvatarimg(ctx, scale) {
- var that = this;
- //商户详情海报
- console.log(that.data.avatarUrl,"???")
- let src1 = that.data.avatarUrl + `?imageView/2/w/${100}/h/${100}`;
- let src2 = util.getProxyImgUrl(src1);
- wx.getImageInfo({
- src: src2,
- success: function (res) {
- console.log(res.path)
- that.darwAvatarArc(ctx, res.path, 10, 8, 35, 35);
- }
- });
- },
-
-
- setCanvas() {
- // 适配屏幕
- let scale = this.data.windowWidth / 375.0
- console.log(scale)
- this.setData({
- totalHeight: 667 * scale
- })
- // 获取Canvas
- let ctx = wx.createCanvasContext("myCanvas");
- this.getMallInfo(ctx, scale)
- this.getUserInfo(ctx, scale)
- this.drawNormalText(ctx, "适用门店:", 30 * scale, 348 * scale, 16 * scale, "#000", "left", "middle")
- this.drawNormalText(ctx, " 向您推荐", 70 * scale, 25 * scale, 16 * scale, "#848484", "left", "middle", scale);
- ctx.draw(true);
- },
-
- // 绘制只有一行的文字
- 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);
- ctx.draw(true);
- },
-
- //商场信息
- getMallInfo: function (ctx, scale) {
- let that = this;
- // 获取用户信息
- Http.get({
- url: config.api.getMallInfo,
- data: {}
- }).then(res => {
- console.log(res);
- if (res.data.img){
- let src1 = res.data.img + `?imageView/2/w/${100}/h/${100}`;
- }else{
- // let src1 = res.data.img + `?imageView/2/w/${100}/h/${100}`;//默认图片地址
- }
-
- let src2 = util.getProxyImgUrl(src1);
- wx.getImageInfo({
- src: src2,
- success: function (res) {
- ctx.drawImage(res.path, 20 * scale, 60 * scale, (that.data.windowWidth-60) * scale, 200*scale);
- ctx.draw(true);
- }
- });
- that.setData({
- mallname: res.data.name
- });
- that.drawNormalText(ctx, "---" + that.data.mallname + "---", 175 * scale, 25 * scale, 16 * scale, "#000", "left", "middle", scale);
- });
- },
-
-
- //获取头像和昵称
- getUserInfo: function (ctx, scale) {
- let that = this;
-
- // 获取用户信息
- Http.get({
- url: config.api.getScore,
- data: {}
- }).then(res => {
- let nickName = '';
- if (res.data.nickName == undefined || res.data.nickName == undefined) {
- wx.getUserInfo({
- success: function(data) {
- nickName = data.userInfo.nickName;
- that.setData({
- nickName: nickName, //用户名
- avatarUrl: data.userInfo.avatarUrl //用户头像
- });
- this.drawNormalText(ctx, nickName, 48 * scale, 24 * scale, 16 * scale, "#000", "left", "middle");
- that.drawAvatarimg(ctx, scale)
- }
- })
- } else {
- nickName = res.data.nickName;
- that.setData({
- nickName: nickName,
- avatarUrl: res.data.avatarUrl
- });
- this.drawNormalText(ctx, nickName, 48 * scale, 24 * scale, 16 * scale, "#000", "left", "middle");
- that.drawAvatarimg(ctx, scale)
- }
- });
- },
-
-
-
-
-
- //绘制圆角图像
- darwAvatarArc: function(ctx, src, x, y, w, h = w) {
- /*
- ctx: 画布对象
- src: 头像缓存路径
- x: 头像起始位置 横坐标
- y: 头像起始位置 纵坐标
- w: 头像宽度
- h: 头像高度,不传为w
- */
- // 保存绘图上下文。
- ctx.save();
- // 开始创建一个路径。需要调用 fill 或者 stroke 才会使用路径进行填充或描边
- ctx.beginPath();
- // 设创建一个圆可以指定 起始弧度为 0,终止弧度为 2 * Math.PI。
- // 用 stroke 或者 fill 方法来在 canvas 中画弧线。
- ctx.arc(x + w / 2, y + h / 2, w / 2, 0, Math.PI * 2, false);
- /* 从原始画布中剪切任意形状和尺寸。
- 一旦剪切了某个区域,则所有之后的绘图都会被限制在被剪切的区域内(
- 不能访问画布上的其他区域)。可以在使用 clip 方法前通过使用 save 方法对当前画布区域进行保存,并在以后的任意时间通过restore方法对其进行恢复。
- */
- ctx.clip();
- // 画头像
- ctx.drawImage(src, x, y, w, h);
- // 恢复之前保存的绘图上下文。
- ctx.restore();
- // 将之前在绘图上下文中的描述(路径、变形、样式)画到 canvas 中。
- ctx.draw(true);
- },
-
-
- /**
- * 保存图片
- */
- 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);
- }
- });
- }
- });
- }
- }
- });
- }
- });
|