index.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <template>
  2. <view>
  3. <view class='poster-poster'>
  4. <view class='tip'><text class='iconfont icon-shuoming'></text>提示:点击图片即可保存至手机相册 </view>
  5. <view class='pictrue'>
  6. <image :src='image' mode="widthFix"></image>
  7. </view>
  8. <!-- #ifdef MP -->
  9. <view class='save-poster' @click="savePosterPath">保存到手机</view>
  10. <!-- #endif -->
  11. </view>
  12. </view>
  13. </template>
  14. <script>
  15. import { getBargainPoster, getCombinationPoster } from '../../../api/activity.js';
  16. export default {
  17. data() {
  18. return {
  19. parameter: {
  20. 'navbar': '1',
  21. 'return': '1',
  22. 'title': '拼团海报',
  23. 'color': true,
  24. 'class': '0'
  25. },
  26. type: 0,
  27. id: 0,
  28. image: '',
  29. from:''
  30. }
  31. },
  32. onLoad(options) {
  33. // #ifdef MP
  34. this.from = 'routine'
  35. // #endif
  36. // #ifdef H5
  37. this.from = 'wechat'
  38. // #endif
  39. var that = this;
  40. if (options.hasOwnProperty('type') && options.hasOwnProperty('id')) {
  41. this.type = options.type
  42. this.id = options.id
  43. if (options.type == 1) {
  44. uni.setNavigationBarTitle({
  45. title: '砍价海报'
  46. })
  47. } else {
  48. uni.setNavigationBarTitle({
  49. title: '拼团海报'
  50. })
  51. }
  52. } else {
  53. return app.Tips({
  54. title: '参数错误',
  55. icon: 'none'
  56. }, {
  57. tab: 3,
  58. url: 1
  59. });
  60. }
  61. },
  62. onShow() {
  63. this.getPosterInfo();
  64. },
  65. methods: {
  66. //替换安全域名
  67. setDomain: function(url) {
  68. url = url ? url.toString() : '';
  69. //本地调试打开,生产请注销
  70. if (url.indexOf("https://") > -1) return url;
  71. else return url.replace('http://', 'https://');
  72. },
  73. downloadFilestoreImage: function(imageUrl) {
  74. let that = this;
  75. uni.downloadFile({
  76. // url: that.setDomain(imageUrl),
  77. url: imageUrl,
  78. success: function(res) {
  79. that.image = res.tempFilePath;
  80. },
  81. fail: function() {
  82. return that.$util.Tips({
  83. title: '下载图片失败'
  84. });
  85. },
  86. });
  87. },
  88. savePosterPath: function() {
  89. let that = this;
  90. uni.getSetting({
  91. success(res) {
  92. if (!res.authSetting['scope.writePhotosAlbum']) {
  93. uni.authorize({
  94. scope: 'scope.writePhotosAlbum',
  95. success() {
  96. uni.saveImageToPhotosAlbum({
  97. filePath: that.image,
  98. success: function(res) {
  99. that.$util.Tips({
  100. title: '保存成功',
  101. icon: 'success'
  102. });
  103. },
  104. fail: function(res) {
  105. that.$util.Tips({
  106. title: '保存失败'
  107. });
  108. }
  109. });
  110. }
  111. });
  112. } else {
  113. uni.saveImageToPhotosAlbum({
  114. filePath: that.image,
  115. success: function(res) {
  116. that.$util.Tips({
  117. title: '保存成功',
  118. icon: 'success'
  119. });
  120. },
  121. fail: function(res) {
  122. that.$util.Tips({
  123. title: '保存失败'
  124. });
  125. }
  126. });
  127. }
  128. }
  129. });
  130. },
  131. getPosterInfo: function() {
  132. var that = this,url = '';
  133. let data = {
  134. id: that.id,
  135. 'from': that.from
  136. };
  137. if (that.type == 1) {
  138. getBargainPoster({
  139. bargainId: that.id,
  140. 'from': that.from
  141. }).then(res => {
  142. that.image = res.data.url;
  143. that.downloadFilestoreImage(res.data.url);
  144. }).catch(err => {
  145. console.log(err)
  146. })
  147. } else {
  148. getCombinationPoster(data).then(res => {
  149. that.image = res.data.url;
  150. that.downloadFilestoreImage(res.data.url);
  151. }).catch(err => {
  152. })
  153. }
  154. },
  155. showImage: function() {
  156. var that = this;
  157. let imgArr = this.image.split(',')
  158. uni.previewImage({
  159. urls: imgArr,
  160. longPressActions: {
  161. itemList: ['发送给朋友', '保存图片', '收藏'],
  162. success: function(data) {
  163. console.log('选中了第' + (data.tapIndex + 1) + '个按钮,第' + (data.index + 1) + '张图片');
  164. },
  165. fail: function(err) {
  166. console.log(err.errMsg);
  167. }
  168. }
  169. });
  170. },
  171. }
  172. }
  173. </script>
  174. <style lang="scss">
  175. page {
  176. background-color: #d22516 !important;
  177. }
  178. .poster-poster .save-poster {
  179. background-color: #FFFFFF;
  180. font-size: :22rpx;
  181. color: #df2d0a;
  182. text-align: center;
  183. height: 66rpx;
  184. line-height: 66rpx;
  185. width: 690rpx;
  186. margin: 20rpx auto;
  187. }
  188. .poster-poster .tip {
  189. height: 80rpx;
  190. font-size: 26rpx;
  191. color: #e8c787;
  192. text-align: center;
  193. line-height: 80rpx;
  194. }
  195. .poster-poster .tip .iconfont {
  196. font-size: 36rpx;
  197. vertical-align: -4rpx;
  198. margin-right: 18rpx;
  199. }
  200. .poster-poster .pictrue {
  201. width: 690rpx;
  202. height: 100%;
  203. margin: 0 auto;
  204. }
  205. .poster-poster .pictrue image {
  206. width: 100%;
  207. height: 100%;
  208. }
  209. </style>