C端小程序
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

252 wiersze
7.7 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. navigationBarHeight,
  13. poterbg: imgurl.poterbg.url,
  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, 8, 35, 35);
  43. }
  44. });
  45. },
  46. setCanvas() {
  47. // 适配屏幕
  48. let scale = this.data.windowWidth / 375.0
  49. console.log(scale)
  50. this.setData({
  51. totalHeight: 667 * scale
  52. })
  53. // 获取Canvas
  54. let ctx = wx.createCanvasContext("myCanvas");
  55. this.getMallInfo(ctx, scale)
  56. this.getUserInfo(ctx, scale)
  57. this.drawNormalText(ctx, "适用门店:", 30 * scale, 348 * scale, 16 * scale, "#000", "left", "middle")
  58. this.drawNormalText(ctx, " 向您推荐", 70 * scale, 25 * scale, 16 * scale, "#848484", "left", "middle", scale);
  59. ctx.draw(true);
  60. },
  61. // 绘制只有一行的文字
  62. drawNormalText(ctx, str, x, y, font, style, align, baseLine) {
  63. ctx.setFontSize(font);
  64. ctx.setFillStyle(style);
  65. ctx.setTextAlign(align);
  66. ctx.setTextBaseline(baseLine);
  67. ctx.fillText(str, x, y);
  68. ctx.draw(true);
  69. },
  70. //商场信息
  71. getMallInfo: function (ctx, scale) {
  72. let that = this;
  73. // 获取用户信息
  74. Http.get({
  75. url: config.api.getMallInfo,
  76. data: {}
  77. }).then(res => {
  78. console.log(res);
  79. if (res.data.img){
  80. let src1 = res.data.img + `?imageView/2/w/${100}/h/${100}`;
  81. }else{
  82. // let src1 = res.data.img + `?imageView/2/w/${100}/h/${100}`;//默认图片地址
  83. }
  84. let src2 = util.getProxyImgUrl(src1);
  85. wx.getImageInfo({
  86. src: src2,
  87. success: function (res) {
  88. ctx.drawImage(res.path, 20 * scale, 60 * scale, (that.data.windowWidth-60) * scale, 200*scale);
  89. ctx.draw(true);
  90. }
  91. });
  92. that.setData({
  93. mallname: res.data.name
  94. });
  95. that.drawNormalText(ctx, "---" + that.data.mallname + "---", 175 * scale, 25 * scale, 16 * scale, "#000", "left", "middle", scale);
  96. });
  97. },
  98. //获取头像和昵称
  99. getUserInfo: function (ctx, scale) {
  100. let that = this;
  101. // 获取用户信息
  102. Http.get({
  103. url: config.api.getScore,
  104. data: {}
  105. }).then(res => {
  106. let nickName = '';
  107. if (res.data.nickName == undefined || res.data.nickName == undefined) {
  108. wx.getUserInfo({
  109. success: function(data) {
  110. nickName = data.userInfo.nickName;
  111. that.setData({
  112. nickName: nickName, //用户名
  113. avatarUrl: data.userInfo.avatarUrl //用户头像
  114. });
  115. this.drawNormalText(ctx, nickName, 48 * scale, 24 * scale, 16 * scale, "#000", "left", "middle");
  116. that.drawAvatarimg(ctx, scale)
  117. }
  118. })
  119. } else {
  120. nickName = res.data.nickName;
  121. that.setData({
  122. nickName: nickName,
  123. avatarUrl: res.data.avatarUrl
  124. });
  125. this.drawNormalText(ctx, nickName, 48 * scale, 24 * scale, 16 * scale, "#000", "left", "middle");
  126. that.drawAvatarimg(ctx, scale)
  127. }
  128. });
  129. },
  130. //绘制圆角图像
  131. darwAvatarArc: function(ctx, src, x, y, w, h = w) {
  132. /*
  133. ctx: 画布对象
  134. src: 头像缓存路径
  135. x: 头像起始位置 横坐标
  136. y: 头像起始位置 纵坐标
  137. w: 头像宽度
  138. h: 头像高度,不传为w
  139. */
  140. // 保存绘图上下文。
  141. ctx.save();
  142. // 开始创建一个路径。需要调用 fill 或者 stroke 才会使用路径进行填充或描边
  143. ctx.beginPath();
  144. // 设创建一个圆可以指定 起始弧度为 0,终止弧度为 2 * Math.PI。
  145. // 用 stroke 或者 fill 方法来在 canvas 中画弧线。
  146. ctx.arc(x + w / 2, y + h / 2, w / 2, 0, Math.PI * 2, false);
  147. /* 从原始画布中剪切任意形状和尺寸。
  148. 一旦剪切了某个区域,则所有之后的绘图都会被限制在被剪切的区域内(
  149. 不能访问画布上的其他区域)。可以在使用 clip 方法前通过使用 save 方法对当前画布区域进行保存,并在以后的任意时间通过restore方法对其进行恢复。
  150. */
  151. ctx.clip();
  152. // 画头像
  153. ctx.drawImage(src, x, y, w, h);
  154. // 恢复之前保存的绘图上下文。
  155. ctx.restore();
  156. // 将之前在绘图上下文中的描述(路径、变形、样式)画到 canvas 中。
  157. ctx.draw(true);
  158. },
  159. /**
  160. * 保存图片
  161. */
  162. saveImage() {
  163. let that = this;
  164. wx.canvasToTempFilePath({
  165. x: 0,
  166. y: 0,
  167. width: this.data.windowWidth * this.data.canvasScale,
  168. height: this.data.totalHeight * this.data.canvasScale,
  169. canvasId: "myCanvas",
  170. success: function(res) {
  171. that.saveImageToPhotos(res.tempFilePath);
  172. },
  173. fail: function(res) {
  174. wx.showToast({
  175. title: "图片生成失败",
  176. icon: "none",
  177. duration: 2000
  178. });
  179. }
  180. });
  181. },
  182. //保存到相册
  183. saveImageToPhotos: function(tempFilePath) {
  184. wx.saveImageToPhotosAlbum({
  185. filePath: tempFilePath,
  186. success(result) {
  187. wx.showToast({
  188. title: "保存成功,从相册中分享到朋友圈吧",
  189. icon: "none",
  190. duration: 4000
  191. });
  192. },
  193. fail: function(err) {
  194. if (
  195. err.errMsg === "saveImageToPhotosAlbum:fail:auth denied" ||
  196. err.errMsg === "saveImageToPhotosAlbum:fail auth deny"
  197. ) {
  198. // 这边微信做过调整,必须要在按钮中触发,因此需要在弹框回调中进行调用
  199. wx.showModal({
  200. title: "提示",
  201. content: "需要您授权保存相册",
  202. showCancel: false,
  203. success: modalSuccess => {
  204. wx.openSetting({
  205. success(settingdata) {
  206. console.log("settingdata", settingdata);
  207. if (settingdata.authSetting["scope.writePhotosAlbum"]) {
  208. wx.showModal({
  209. title: "提示",
  210. content: "获取权限成功,再次点击图片即可保存",
  211. showCancel: false
  212. });
  213. } else {
  214. wx.showModal({
  215. title: "提示",
  216. content: "获取权限失败,将无法保存到相册哦~",
  217. showCancel: false
  218. });
  219. }
  220. },
  221. fail(failData) {
  222. console.log("failData", failData);
  223. },
  224. complete(finishData) {
  225. console.log("finishData", finishData);
  226. }
  227. });
  228. }
  229. });
  230. }
  231. }
  232. });
  233. }
  234. });