share-poster.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. import Poster from '../../components/wxa-plugin-canvas/poster/poster';
  2. const app = getApp();
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. posterConfig: {},
  9. posterImage: ''
  10. },
  11. onLoad: function () {
  12. var that = this
  13. this.setData({
  14. posterConfig: {
  15. width: 750,
  16. height: 1200,
  17. debug: false,
  18. blocks: [
  19. {
  20. height: 920,
  21. width: 700,
  22. x: 25,
  23. y: 90,
  24. backgroundColor: '#fff',
  25. borderRadius: 34
  26. },
  27. {
  28. width: 134,
  29. height: 134,
  30. x: 308,
  31. y: 23,
  32. borderRadius: 134,
  33. backgroundColor: '#3EA0FF',
  34. opacity: 0.5,
  35. zIndex: 1,
  36. },
  37. ],
  38. texts: [
  39. {
  40. x: 375,
  41. y: 185,
  42. baseLine: 'middle',
  43. text: wx.getStorageSync('USER_INFO').user.nickname,
  44. fontSize: 28,
  45. color: '#171717',
  46. textAlign: 'center',
  47. zIndex: 200
  48. },
  49. {
  50. x: 375,
  51. y: 215,
  52. baseLine: 'top',
  53. text: '邀请你参与抽奖',
  54. fontSize: 38,
  55. color: '#52AAFF',
  56. textAlign: 'center',
  57. zIndex: 200
  58. },
  59. {
  60. x: 60,
  61. y: 660,
  62. fontSize: 38,
  63. baseLine: 'middle',
  64. text: wx.getStorageSync('POSTER_INFO').title,
  65. lineNum: 1,
  66. color: '#333',
  67. zIndex: 200
  68. },
  69. {
  70. x: 60,
  71. y: 705,
  72. baseLine: 'middle',
  73. text: wx.getStorageSync('POSTER_INFO').time,
  74. fontSize: 28,
  75. color: '#888',
  76. zIndex: 200
  77. },
  78. {
  79. x: 280,
  80. y: 845,
  81. baseLine: 'middle',
  82. text: '长按识别小程序码,参与抽奖',
  83. fontSize: 30,
  84. color: '#52AAFF',
  85. zIndex: 200
  86. }
  87. ],
  88. images: [
  89. {
  90. width: 114,
  91. height: 114,
  92. x: 318,
  93. y: 33,
  94. borderRadius: 114,
  95. url: wx.getStorageSync('USER_INFO').user.avatar_url,
  96. zIndex: 2
  97. },
  98. {
  99. width: 700,
  100. height: 325,
  101. x: 25,
  102. y: 280,
  103. url: wx.getStorageSync('POSTER_INFO').imageUrl,
  104. },
  105. {
  106. width: 180,
  107. height: 180,
  108. x: 60,
  109. y: 755,
  110. url: wx.getStorageSync('POSTER_INFO').qrcode,
  111. }
  112. ]
  113. }
  114. })
  115. wx.getSystemInfo({
  116. success: function (res) {
  117. let newConfig = that.data.posterConfig
  118. newConfig.height = res.windowHeight
  119. that.setData({
  120. posterConfig: newConfig
  121. })
  122. },
  123. })
  124. this.onCreatePoster();
  125. },
  126. onPosterSuccess(e) {
  127. const { detail } = e
  128. this.setData({
  129. posterImage: detail
  130. })
  131. },
  132. onCreatePoster: function () {
  133. this.setData({ posterConfig: this.data.posterConfig }, () => {
  134. Poster.create(true);
  135. });
  136. },
  137. savePoster: function () {
  138. var that = this
  139. wx.showLoading({
  140. title: '保存中',
  141. mask: true
  142. })
  143. wx.saveImageToPhotosAlbum({
  144. filePath: that.data.posterImage,
  145. success: function () {
  146. wx.hideLoading()
  147. wx.showModal({
  148. content: '保存成功',
  149. showCancel: false
  150. })
  151. },
  152. fail: function () {
  153. wx.hideLoading()
  154. wx.showModal({
  155. content: '保存失败',
  156. showCancel: false
  157. })
  158. }
  159. })
  160. },
  161. getSaveState: function () {
  162. var that = this
  163. wx.getSetting({
  164. success: function (res) {
  165. if (!res.authSetting['scope.writePhotosAlbum']) {
  166. wx.authorize({
  167. scope: 'scope.writePhotosAlbum',
  168. success: function () {
  169. that.savePoster()
  170. },
  171. fail: function () {
  172. wx.showModal({
  173. title: '获取权限失败',
  174. content: '是否打开权限设置,允许小程序保存图片到您的相册',
  175. success: function (res) {
  176. if (res.confirm) {
  177. wx.openSetting({
  178. success: function (setRes) {
  179. if (setRes.authSetting['scope.writePhotosAlbum']) {
  180. setTimeout(function () {
  181. that.savePoster()
  182. })
  183. }
  184. }
  185. })
  186. } else {
  187. wx.showModal({
  188. content: '请打开图片保存权限,否则将无法保存图片',
  189. showCancel: false
  190. })
  191. }
  192. }
  193. })
  194. }
  195. })
  196. } else {
  197. that.savePoster()
  198. }
  199. }
  200. })
  201. }
  202. })