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

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