C端小程序
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

330 строки
10 KiB

  1. const navigationBarHeight = (getApp().statusBarHeight + 44) + 'px'
  2. var config = require("../../config/config.js");
  3. var app = getApp();
  4. const Http = require("../../utils/HttpBasics");
  5. const util = require("../../utils/util");
  6. const imgurl = require("../../utils/imgurl");
  7. Page({
  8. /**
  9. * 页面的初始数据
  10. */
  11. data: {
  12. shareDefaultTp: imgurl.shareDefaultTp.url,
  13. navigationBarHeight,
  14. windowWidth: wx.getSystemInfoSync().windowWidth,
  15. windowHeight: wx.getSystemInfoSync().screenHeight,
  16. totalHeight: 0,
  17. isdisable: true,
  18. mallname: '', //商场名字
  19. qrCodeL: "", //小程序码
  20. couponList: [], //活动劵列表
  21. canvasScale: 1.0 // 画布放大的倍数,因为如果保存的是一倍的分享图片的话,分享图会有点虚。所以保存的时候,canvasScale设置为2.0,wxss 里面的left: 500%;打开注释。就可保存两倍的分享图
  22. },
  23. /**
  24. * 生命周期函数--监听页面加载
  25. */
  26. onLoad: function(options) {
  27. this.setCanvas()
  28. },
  29. onShow: function() {
  30. },
  31. //绘制微信头像
  32. drawAvatarimg(ctx, scale) {
  33. var that = this;
  34. //商户详情海报
  35. console.log(that.data.avatarUrl, "???")
  36. let src1 = that.data.avatarUrl + `?imageView/2/w/${100}/h/${100}`;
  37. let src2 = util.getProxyImgUrl(src1);
  38. wx.getImageInfo({
  39. src: src2,
  40. success: function(res) {
  41. console.log(res.path)
  42. that.darwAvatarArc(ctx, res.path, 10, 18, 35, 35);
  43. }
  44. });
  45. },
  46. setCanvas() {
  47. wx.showLoading({
  48. title: "生成中..."
  49. });
  50. var tha
  51. // 适配屏幕
  52. let scale = this.data.windowWidth / 375.0
  53. console.log(scale)
  54. this.setData({
  55. totalHeight: 667 * scale
  56. })
  57. // 获取Canvas
  58. let ctx = wx.createCanvasContext("myCanvas");
  59. this.getQrcodeFx(ctx, scale)
  60. this.getMallInfo(ctx, scale)
  61. this.getUserInfo(ctx, scale)
  62. ctx.setFillStyle("#fff");
  63. ctx.fillRect(0, 0, this.data.windowWidth, this.data.windowHeight);
  64. this.drawNormalText(ctx, " 向您推荐", 140 * scale, 32 * scale, 16 * scale, "#848484", "left", "middle", scale);
  65. this.drawNormalText(ctx, "长按二维码识别", 180 * scale, 360 * scale, 16 * scale, "#000", "left", "middle")
  66. this.drawNormalText(ctx, "尽享更多优惠活动!", 170 * scale, 390 * scale, 16 * scale, "#000", "left", "middle")
  67. ctx.draw(true);
  68. this.setData({
  69. isdisable: false,
  70. })
  71. setTimeout(function() {
  72. wx.hideLoading()
  73. }, 2000)
  74. },
  75. // 绘制只有一行的文字
  76. drawNormalText(ctx, str, x, y, font, style, align, baseLine) {
  77. ctx.setFontSize(font);
  78. ctx.setFillStyle(style);
  79. ctx.setTextAlign(align);
  80. ctx.setTextBaseline(baseLine);
  81. ctx.fillText(str, x, y);
  82. ctx.draw(true);
  83. },
  84. //商场信息
  85. getMallInfo: function(ctx, scale) {
  86. let that = this;
  87. // 获取用户信息
  88. Http.get({
  89. url: config.api.getMallInfo,
  90. data: {}
  91. }).then(res => {
  92. console.log(res);
  93. let src1 = ""
  94. if (res.data.img) {
  95. src1 = res.data.img + `?imageView/2/w/${100}/h/${100}`;
  96. } else {
  97. src1 = that.data.shareDefaultTp + `?imageView/2/w/${100}/h/${100}`; //默认图片地址
  98. }
  99. let src2 = util.getProxyImgUrl(src1);
  100. wx.getImageInfo({
  101. src: src2,
  102. success: function(res) {
  103. ctx.drawImage(res.path, 20 * scale, 80 * scale, (that.data.windowWidth - 60) * scale, 200 * scale);
  104. ctx.draw(true);
  105. }
  106. });
  107. that.setData({
  108. mallname: res.data.name
  109. });
  110. if (res.data.addr){
  111. that.canvasTextAutoLine('详细地址:' + res.data.addr, ctx, 20 * scale, 480 * scale, 14 * scale, "#888", "left", "middle", 26 * scale, 330 * scale, 2)
  112. }
  113. if (res.data.servicePhone){
  114. that.drawNormalText(ctx, '客服电话:' + res.data.servicePhone, 20 * scale, 540 * scale, 16 * scale, "#888", "left", "middle", scale);
  115. }
  116. that.drawNormalText(ctx, "---" + that.data.mallname + "---", 90 * scale, 62 * scale, 16 * scale, "#000", "left", "middle", scale);
  117. });
  118. },
  119. //获取分享小程序二维码
  120. getQrcodeFx(ctx, scale) {
  121. let that = this
  122. let coImageWidth = parseInt(120 * scale);
  123. let coImageHeight = parseInt(120 * scale);
  124. Http.get({
  125. url: config.api.getQrcodeFx,
  126. }).then(res => {
  127. console.log(res.data)
  128. let src1 = res.data + `?imageView/2/w/${100}/h/${100}`;
  129. let src2 = util.getProxyImgUrl(src1);
  130. wx.getImageInfo({
  131. src: src2,
  132. success: function(res) {
  133. console.log(res.path)
  134. that.darwAvatarArc(ctx, res.path, 30 * scale, 310 * scale, coImageWidth, coImageHeight, scale);
  135. }
  136. });
  137. })
  138. },
  139. //获取头像和昵称
  140. getUserInfo: function(ctx, scale) {
  141. let that = this;
  142. // 获取用户信息
  143. Http.get({
  144. url: config.api.getScore,
  145. data: {}
  146. }).then(res => {
  147. let nickName = '';
  148. if (res.data.nickName == undefined || res.data.nickName == undefined) {
  149. wx.getUserInfo({
  150. success: function(data) {
  151. nickName = data.userInfo.nickName;
  152. that.setData({
  153. nickName: nickName, //用户名
  154. avatarUrl: data.userInfo.avatarUrl //用户头像
  155. });
  156. console.log(data.userInfo.avatarUrl, "????--**")
  157. this.drawNormalText(ctx, nickName, 50 * scale, 34 * scale, 16 * scale, "#000", "left", "middle");
  158. that.drawAvatarimg(ctx, scale)
  159. }
  160. })
  161. } else {
  162. nickName = res.data.nickName;
  163. that.setData({
  164. nickName: nickName,
  165. avatarUrl: res.data.avatarUrl
  166. });
  167. this.drawNormalText(ctx, nickName, 48 * scale, 34 * scale, 16 * scale, "#000", "left", "middle");
  168. that.drawAvatarimg(ctx, scale)
  169. }
  170. });
  171. },
  172. canvasTextAutoLine: function(str, ctx, initX, initY, font, style, align, baseLine, lineHeight, maxWidth, row = 1) {
  173. var lineWidth = 0;
  174. var lastSubStrIndex = 0;
  175. var currentRow = 1;
  176. for (let i = 0; i < str.length; i++) {
  177. lineWidth += ctx.measureText(str[i]).width;
  178. if (lineWidth > maxWidth) {
  179. currentRow++;
  180. let newStr = str.substring(lastSubStrIndex, i);
  181. if (currentRow > row && str.length > i) {
  182. newStr = str.substring(lastSubStrIndex, i - 2) + "...";
  183. }
  184. ctx.setFontSize(font);
  185. ctx.setFillStyle(style);
  186. ctx.setTextAlign(align);
  187. ctx.setTextBaseline(baseLine);
  188. ctx.fillText(newStr, initX, initY);
  189. initY += lineHeight;
  190. lineWidth = 0;
  191. lastSubStrIndex = i;
  192. if (currentRow > row) {
  193. break;
  194. }
  195. }
  196. if (i == str.length - 1) {
  197. ctx.setFontSize(font);
  198. ctx.setFillStyle(style);
  199. ctx.setTextAlign(align);
  200. ctx.setTextBaseline(baseLine);
  201. ctx.fillText(str.substring(lastSubStrIndex, i + 1), initX, initY);
  202. }
  203. }
  204. },
  205. //绘制圆角图像
  206. darwAvatarArc: function(ctx, src, x, y, w, h = w) {
  207. /*
  208. ctx: 画布对象
  209. src: 头像缓存路径
  210. x: 头像起始位置 横坐标
  211. y: 头像起始位置 纵坐标
  212. w: 头像宽度
  213. h: 头像高度,不传为w
  214. */
  215. // 保存绘图上下文。
  216. ctx.save();
  217. // 开始创建一个路径。需要调用 fill 或者 stroke 才会使用路径进行填充或描边
  218. ctx.beginPath();
  219. // 设创建一个圆可以指定 起始弧度为 0,终止弧度为 2 * Math.PI。
  220. // 用 stroke 或者 fill 方法来在 canvas 中画弧线。
  221. ctx.arc(x + w / 2, y + h / 2, w / 2, 0, Math.PI * 2, false);
  222. /* 从原始画布中剪切任意形状和尺寸。
  223. 一旦剪切了某个区域,则所有之后的绘图都会被限制在被剪切的区域内(
  224. 不能访问画布上的其他区域)。可以在使用 clip 方法前通过使用 save 方法对当前画布区域进行保存,并在以后的任意时间通过restore方法对其进行恢复。
  225. */
  226. ctx.clip();
  227. // 画头像
  228. ctx.drawImage(src, x, y, w, h);
  229. // 恢复之前保存的绘图上下文。
  230. ctx.restore();
  231. // 将之前在绘图上下文中的描述(路径、变形、样式)画到 canvas 中。
  232. ctx.draw(true);
  233. },
  234. /**
  235. * 保存图片
  236. */
  237. saveImage() {
  238. let that = this;
  239. wx.canvasToTempFilePath({
  240. x: 0,
  241. y: 0,
  242. width: this.data.windowWidth * this.data.canvasScale,
  243. height: this.data.totalHeight * this.data.canvasScale,
  244. canvasId: "myCanvas",
  245. success: function(res) {
  246. that.saveImageToPhotos(res.tempFilePath);
  247. },
  248. fail: function(res) {
  249. wx.showToast({
  250. title: "图片生成失败",
  251. icon: "none",
  252. duration: 2000
  253. });
  254. }
  255. });
  256. },
  257. //保存到相册
  258. saveImageToPhotos: function(tempFilePath) {
  259. wx.saveImageToPhotosAlbum({
  260. filePath: tempFilePath,
  261. success(result) {
  262. wx.showToast({
  263. title: "保存成功,从相册中分享到朋友圈吧",
  264. icon: "none",
  265. duration: 4000
  266. });
  267. },
  268. fail: function(err) {
  269. if (
  270. err.errMsg === "saveImageToPhotosAlbum:fail:auth denied" ||
  271. err.errMsg === "saveImageToPhotosAlbum:fail auth deny"
  272. ) {
  273. // 这边微信做过调整,必须要在按钮中触发,因此需要在弹框回调中进行调用
  274. wx.showModal({
  275. title: "提示",
  276. content: "需要您授权保存相册",
  277. showCancel: false,
  278. success: modalSuccess => {
  279. wx.openSetting({
  280. success(settingdata) {
  281. console.log("settingdata", settingdata);
  282. if (settingdata.authSetting["scope.writePhotosAlbum"]) {
  283. wx.showModal({
  284. title: "提示",
  285. content: "获取权限成功,再次点击图片即可保存",
  286. showCancel: false
  287. });
  288. } else {
  289. wx.showModal({
  290. title: "提示",
  291. content: "获取权限失败,将无法保存到相册哦~",
  292. showCancel: false
  293. });
  294. }
  295. },
  296. fail(failData) {
  297. console.log("failData", failData);
  298. },
  299. complete(finishData) {
  300. console.log("finishData", finishData);
  301. }
  302. });
  303. }
  304. });
  305. }
  306. }
  307. });
  308. }
  309. });