微信小店联盟带货小程序

poster.js 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. Page({
  2. data: {
  3. generating: false, // 是否正在生成海报
  4. currentPoster: null, // 当前选中的海报模板
  5. currentGoods: null, // 当前选中的商品
  6. customText: '', // 自定义文案
  7. showGoodsSelect: false, // 是否显示商品选择
  8. showCustomText: false, // 是否显示自定义文案
  9. posterList: [
  10. {
  11. id: '1',
  12. name: '模板一',
  13. url: '/static/images/poster-1.jpg',
  14. type: 'goods' // 商品海报
  15. },
  16. {
  17. id: '2',
  18. name: '模板二',
  19. url: '/static/images/poster-2.jpg',
  20. type: 'profile' // 个人名片
  21. },
  22. {
  23. id: '3',
  24. name: '模板三',
  25. url: '/static/images/poster-3.jpg',
  26. type: 'custom' // 自定义海报
  27. }
  28. ],
  29. goodsList: [
  30. {
  31. id: '1',
  32. name: '商品名称商品名称商品名称',
  33. price: '99.00',
  34. image: '/static/images/goods-1.jpg'
  35. },
  36. {
  37. id: '2',
  38. name: '商品名称2',
  39. price: '199.00',
  40. image: '/static/images/goods-2.jpg'
  41. },
  42. {
  43. id: '3',
  44. name: '商品名称3',
  45. price: '299.00',
  46. image: '/static/images/goods-3.jpg'
  47. }
  48. ]
  49. },
  50. onLoad() {
  51. // 设置默认选中第一个模板
  52. this.setData({
  53. currentPoster: this.data.posterList[0]
  54. })
  55. this.checkTemplateType(this.data.posterList[0])
  56. },
  57. // 选择海报模板
  58. selectTemplate(e) {
  59. const id = e.currentTarget.dataset.id
  60. const poster = this.data.posterList.find(item => item.id === id)
  61. this.setData({ currentPoster: poster })
  62. this.checkTemplateType(poster)
  63. },
  64. // 检查模板类型,显示对应的选项
  65. checkTemplateType(poster) {
  66. this.setData({
  67. showGoodsSelect: poster.type === 'goods',
  68. showCustomText: poster.type === 'custom'
  69. })
  70. },
  71. // 选择商品
  72. selectGoods(e) {
  73. const id = e.currentTarget.dataset.id
  74. const goods = this.data.goodsList.find(item => item.id === id)
  75. this.setData({ currentGoods: goods })
  76. this.generatePoster()
  77. },
  78. // 文案输入
  79. onTextChange(e) {
  80. this.setData({
  81. customText: e.detail.value
  82. })
  83. this.generatePoster()
  84. },
  85. // 生成海报
  86. generatePoster() {
  87. if (this.data.generating) return
  88. this.setData({ generating: true })
  89. // TODO: 调用后端API生成海报
  90. setTimeout(() => {
  91. this.setData({ generating: false })
  92. }, 1500)
  93. },
  94. // 预览海报
  95. previewPoster() {
  96. if (!this.data.currentPoster) return
  97. wx.previewImage({
  98. urls: [this.data.currentPoster.url]
  99. })
  100. },
  101. // 保存海报
  102. savePoster() {
  103. if (!this.data.currentPoster) {
  104. return wx.showToast({
  105. title: '请先生成海报',
  106. icon: 'none'
  107. })
  108. }
  109. wx.getSetting({
  110. success: (res) => {
  111. if (!res.authSetting['scope.writePhotosAlbum']) {
  112. wx.authorize({
  113. scope: 'scope.writePhotosAlbum',
  114. success: () => {
  115. this.saveImage()
  116. },
  117. fail: () => {
  118. wx.showModal({
  119. title: '提示',
  120. content: '需要您授权保存图片到相册',
  121. success: (res) => {
  122. if (res.confirm) {
  123. wx.openSetting()
  124. }
  125. }
  126. })
  127. }
  128. })
  129. } else {
  130. this.saveImage()
  131. }
  132. }
  133. })
  134. },
  135. // 保存图片到相册
  136. saveImage() {
  137. wx.showLoading({
  138. title: '保存中...'
  139. })
  140. wx.downloadFile({
  141. url: this.data.currentPoster.url,
  142. success: (res) => {
  143. wx.saveImageToPhotosAlbum({
  144. filePath: res.tempFilePath,
  145. success: () => {
  146. wx.showToast({
  147. title: '保存成功',
  148. icon: 'success'
  149. })
  150. },
  151. fail: () => {
  152. wx.showToast({
  153. title: '保存失败',
  154. icon: 'none'
  155. })
  156. }
  157. })
  158. },
  159. fail: () => {
  160. wx.showToast({
  161. title: '图片下载失败',
  162. icon: 'none'
  163. })
  164. },
  165. complete: () => {
  166. wx.hideLoading()
  167. }
  168. })
  169. },
  170. onShareAppMessage() {
  171. return {
  172. title: '邀请你加入分销团队',
  173. path: '/pages/index/index?inviter=' + wx.getStorageSync('distributionId'),
  174. imageUrl: this.data.currentPoster?.url || '/static/images/share.jpg'
  175. }
  176. }
  177. })