1
0

wshare.js 9.0 KB

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