wshare.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. const app=getApp();
  2. let winwidth = 0;
  3. Component({
  4. /**
  5. * 组件的属性列表
  6. */
  7. properties: {
  8. wshareshow: {
  9. type: Boolean,
  10. value:false
  11. },
  12. posterimg:{
  13. type:String,
  14. value:''
  15. },
  16. qrcode:{
  17. type:String,
  18. value:''
  19. },
  20. title:{
  21. type:String,
  22. value:''
  23. },
  24. islist: {
  25. type: Boolean,
  26. value:false
  27. }
  28. },
  29. /**
  30. * 组件的初始数据
  31. */
  32. data: {
  33. imgUrl:app.globalData.imgUrl,
  34. imgflag:false,
  35. cangth: 100,
  36. shareImg:''
  37. },
  38. /**
  39. * 组件的方法列表
  40. */
  41. methods: {
  42. dothis:function(){},
  43. cancletap:function(){
  44. this.setData({
  45. wshareshow:false
  46. })
  47. },
  48. // sharefriendtap:function(){
  49. // var that=this;
  50. // that.triggerEvent("sfriend", '')
  51. // this.setData({
  52. // wshareshow:false
  53. // })
  54. // },
  55. shareurltap:function(){
  56. var that=this;
  57. that.triggerEvent("shareurl", '')
  58. this.setData({
  59. wshareshow:false
  60. })
  61. },
  62. setposter:function(){
  63. var that=this;
  64. wx.getSystemInfo({
  65. success: (res) => {
  66. winwidth = res.windowWidth;
  67. }
  68. })
  69. // that.setData({
  70. // imgflag:true
  71. // })
  72. // setTimeout(function(){
  73. // that.createdCode();
  74. // },500)
  75. that.triggerEvent("poster", '')
  76. this.setData({
  77. wshareshow:false
  78. })
  79. },
  80. sendfriend:function(){
  81. var that=this;
  82. that.triggerEvent("sendfriendcircle", '')
  83. this.setData({
  84. wshareshow:false
  85. })
  86. },
  87. //开始绘图
  88. createdCode: function () {
  89. let that = this;
  90. if(that.data.title.length>26){
  91. that.setData({
  92. title:that.data.title.substring(0,23)+'...'
  93. })
  94. }
  95. var posterimg=that.data.posterimg;
  96. posterimg = posterimg.replace('http://o.nczyzs.com',"https://o.nczyzs.com");
  97. var qrcode=that.data.qrcode;
  98. qrcode = qrcode.replace('http://o.nczyzs.com',"https://o.nczyzs.com");
  99. let topwidth = winwidth * 590 / 750,topheight = '';
  100. // const ctx = wx.createCanvasContext('myCanvas'); //绘图上下文
  101. wx.createSelectorQuery().select('#myCanvas')
  102. .fields({ node: true, size: true })
  103. .exec((rest) => {
  104. console.log(rest)
  105. const canvas = rest[0].node;
  106. const ctx = canvas.getContext('2d')
  107. wx.getImageInfo({
  108. src: posterimg,
  109. success(res) {
  110. wx.getImageInfo({
  111. src: qrcode,
  112. success(rs) {
  113. topheight = topwidth * res.height / res.width;
  114. that.setData({
  115. cangth: topheight + 64
  116. })
  117. ctx.save();
  118. ctx.drawImage(res.path, 0, 0, topwidth, topheight);
  119. ctx.rect(0, topheight, topwidth, 64);
  120. ctx.setFillStyle('#FFFFFF');
  121. ctx.fill();
  122. ctx.restore()
  123. ctx.setFontSize(14);
  124. ctx.fillText("@" + app.globalData.personMsg.nickname, 12, topheight + 24, topwidth - 70);
  125. //
  126. ctx.fillText(that.data.title, 12, topheight + 48, topwidth - 70);
  127. ctx.drawImage(rs.path, topwidth - 52, topheight + 12, 40, 40);
  128. ctx.draw();
  129. wx.hideLoading();
  130. setTimeout(() => {
  131. wx.canvasToTempFilePath({
  132. x: 0,
  133. y: 0,
  134. canvasId: 'myCanvas',
  135. success: function (res) {
  136. let shareImg = res.tempFilePath;
  137. that.setData({
  138. shareImg: shareImg,
  139. })
  140. },
  141. fail: function (res) {}
  142. })
  143. },500)
  144. },fail(res){
  145. console.log(res)
  146. },
  147. })
  148. },fail(res){
  149. console.log(res)
  150. }
  151. })
  152. })
  153. },
  154. saveimgtap: function () {
  155. let that = this;
  156. // 获取用户是否开启用户授权相册
  157. wx.getSetting({
  158. success(res) {
  159. // 如果没有则获取授权
  160. if (!res.authSetting['scope.writePhotosAlbum']) {
  161. wx.authorize({
  162. scope: 'scope.writePhotosAlbum',
  163. success() {
  164. wx.saveImageToPhotosAlbum({
  165. filePath: that.data.shareImg,
  166. success() {
  167. wx.showToast({
  168. title: '保存成功'
  169. })
  170. },
  171. fail() {
  172. wx.showToast({
  173. title: '保存失败',
  174. icon: 'none'
  175. })
  176. }
  177. })
  178. },
  179. fail() {
  180. // 如果用户拒绝过或没有授权,则再次打开授权窗口
  181. //(ps:微信api又改了现在只能通过button才能打开授权设置,以前通过openSet就可打开,下面有打开授权的button弹窗代码)
  182. that.setData({
  183. openSet: true
  184. })
  185. }
  186. })
  187. } else {
  188. // 有则直接保存
  189. wx.saveImageToPhotosAlbum({
  190. filePath: that.data.shareImg,
  191. success() {
  192. wx.showToast({
  193. title: '保存成功'
  194. })
  195. },
  196. fail() {
  197. wx.showToast({
  198. title: '保存失败',
  199. icon: 'none'
  200. })
  201. }
  202. })
  203. }
  204. }
  205. })
  206. },
  207. }
  208. })