大数据平台的小程序版本

index.js 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. // pages/extension/poster/index.js
  2. const app = getApp()
  3. Page({
  4. data: {
  5. swiperImgUrls: [
  6. 'https://www.wenxingshuju.com/poster_h5/poster_invite_1.png','https://www.wenxingshuju.com/poster_h5/poster_invite_2.png','https://www.wenxingshuju.com/poster_h5/poster_invite_3.png',
  7. ],
  8. swiperIndex: 0,
  9. tempFilePath:'',
  10. writePhotosFlag:-1,
  11. invite_code:'',
  12. id:'',
  13. evm_url:''
  14. },
  15. onLoad: function (options) {
  16. this.setData({invite_code:wx.getStorageSync('userInfo').invite_code,id:options.id})
  17. this.getSet()
  18. this.init()
  19. },
  20. init(){
  21. wx.showLoading({ title: '加载中', })
  22. app.func.req('/api/sphUserShare/getCommentQrCode', 'get', {
  23. path:'pages/index/index',
  24. param:{invite:this.data.invite_code,qun_id:this.data.id}
  25. }, (res) => {
  26. wx.hideLoading()
  27. if(res && res.errno == 0){
  28. this.setData({evm_url:app.data.rootDocment+'/'+res.rst})
  29. }else{
  30. wx.showToast({
  31. title: res.err,
  32. icon: 'none',
  33. duration: 2000
  34. })
  35. }
  36. },()=>{
  37. wx.hideLoading()
  38. })
  39. },
  40. swiperChange(e) {
  41. const that = this;
  42. that.setData({
  43. swiperIndex: e.detail.current,
  44. })
  45. },
  46. formSubmit: function (e) {//点击生成海报
  47. var that = this;
  48. wx.showToast({
  49. title: '请稍后...',
  50. icon: 'loading',
  51. duration: 1000
  52. });
  53. setTimeout(function () {
  54. wx.hideToast()
  55. that.createNewImg();
  56. // that.setData({
  57. // maskHidden: true
  58. // });
  59. }, 1000)
  60. },
  61. createNewImg: function () {////将canvas转换为图片保存到本地,然后将图片路径传给image图片的src
  62. var that = this;
  63. var path1 = ''
  64. var path2 = ''
  65. var promise1 = new Promise((resolve, reject) => {
  66. wx.getImageInfo({
  67. src: that.data.swiperImgUrls[that.data.swiperIndex],
  68. success: function (res) {
  69. path1 = res.path
  70. resolve()
  71. }
  72. })
  73. });
  74. var promise2 = new Promise((resolve, reject) => {
  75. wx.getImageInfo({
  76. src: that.data.evm_url,
  77. success: function (res) {
  78. path2 = res.path;
  79. resolve()
  80. }
  81. })
  82. });
  83. Promise.all([promise1,promise2]).then((values)=> {
  84. var context = wx.createCanvasContext('mycanvas');
  85. context.drawImage(path1, 0, 0, 350, 494);
  86. // 绘制图片圆角
  87. var bg_x = 145,bg_y=390,bg_w=60,bg_h=60,bg_r=10
  88. context.save()
  89. context.beginPath()
  90. context.arc(bg_x + bg_r, bg_y + bg_r, bg_r, Math.PI, Math.PI*1.5)
  91. context.arc(bg_x + bg_w - bg_r, bg_y + bg_r, bg_r, Math.PI * 1.5, Math.PI * 2)
  92. context.arc(bg_x + bg_w - bg_r, bg_y + bg_h - bg_r, bg_r, 0, Math.PI * 0.5)
  93. context.arc(bg_x + bg_r, bg_y + bg_h - bg_r, bg_r, Math.PI * 0.5, Math.PI)
  94. context.clip()
  95. context.drawImage(path2, bg_x, bg_y, bg_w, bg_h);
  96. // 恢复之前保存的绘图上下文
  97. context.restore()
  98. context.setStrokeStyle('transparent');
  99. // 绘制文字
  100. context.setFontSize(10);
  101. context.setFillStyle('#B4BDFF');
  102. context.setTextAlign('center');
  103. context.fillText("扫码进入小程序", 175, 470);
  104. context.stroke();
  105. context.draw();
  106. //将生成好的图片保存到本地
  107. setTimeout(function () {
  108. wx.canvasToTempFilePath({
  109. canvasId: 'mycanvas',
  110. success: function (res) {
  111. var tempFilePath = res.tempFilePath;
  112. that.setData({
  113. imagePath: tempFilePath,
  114. });
  115. that.downloadImg()
  116. },
  117. fail: function (res) {
  118. console.log(res);
  119. }
  120. });
  121. }, 200);
  122. });
  123. },
  124. downLoadImgEvent(){
  125. this.getSet(()=>{
  126. if(!this.data.writePhotosFlag){
  127. wx.openSetting()
  128. return
  129. }
  130. var _this = this;
  131. wx.getSetting({
  132. success(res) {
  133. if (!res.authSetting['scope.writePhotosAlbum']) {
  134. _this.setData({writePhotosFlag:false})
  135. wx.authorize({
  136. scope:'scope.writePhotosAlbum',
  137. success() {//授权成功
  138. _this.setData({writePhotosFlag:true})
  139. _this.formSubmit()
  140. },
  141. fail:function(err){
  142. _this.setData({writePhotosFlag:false})
  143. }
  144. })
  145. }else{//已经授权
  146. _this.setData({writePhotosFlag:true})
  147. _this.formSubmit()
  148. }
  149. }
  150. })
  151. })
  152. },
  153. downloadImg(){
  154. var _this = this
  155. var imgSrc = this.data.imagePath
  156. wx.saveImageToPhotosAlbum({
  157. filePath: imgSrc,
  158. success: function (data) {
  159. _this.recordInvite(2)
  160. wx.showToast({
  161. title: '海报已保存到相册',
  162. icon: 'success',
  163. duration: 2000
  164. })
  165. },
  166. fail: function (err) {
  167. },
  168. complete(res){
  169. }
  170. })
  171. },
  172. getSet(cb){
  173. var _this = this;
  174. wx.getSetting({
  175. success(res) {
  176. if(res.authSetting['scope.writePhotosAlbum'] == false){
  177. _this.setData({writePhotosFlag:false})
  178. }
  179. if(res.authSetting['scope.writePhotosAlbum'] == true){
  180. _this.setData({writePhotosFlag:true})
  181. }
  182. cb?cb():''
  183. }
  184. })
  185. },
  186. recordInvite(type){//按钮埋点
  187. app.func.req('/api/praise/recordInvite', 'post', {
  188. sph_praise_id:this.data.id,
  189. type:type
  190. }, (res) => {
  191. if(res && res.errno == 0){
  192. }else{
  193. }
  194. },()=>{
  195. })
  196. },
  197. onShareAppMessage: function (res) {
  198. this.recordInvite(1)
  199. return app.extensionShare(res,this.data.id)
  200. }
  201. })