金诚优选前端代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

190 lines
4.3 KiB

  1. <template>
  2. <view id="shareImg" v-if="init">
  3. <view class="img-box">
  4. <image mode="widthFix" :src="info.url" alt></image>
  5. </view>
  6. <view class="btn-box">
  7. <view class="btn" @tap="downImg">保存海报</view>
  8. </view>
  9. <!--用户拒绝下载图片授权弹出-->
  10. <alert :is_refused="is_refused" bind:close="closeAlert"></alert>
  11. </view>
  12. </template>
  13. <script>
  14. import {pageLogin, getUrl,config,is} from '@/common/js/utils.js';
  15. import alert from "@/components/alert/alert";
  16. export default {
  17. data() {
  18. return {
  19. info: '',
  20. id: '',
  21. init: false,
  22. channel: '',
  23. is_refused: false
  24. };
  25. },
  26. onLoad(e) {
  27. this.setData({
  28. id: e.id,
  29. channel: e.channel
  30. });
  31. this.getShearImg();
  32. },
  33. components: {
  34. alert
  35. },
  36. props: {},
  37. methods: {
  38. getShearImg() {
  39. wx.showLoading({
  40. title: '加载中',
  41. mask: true
  42. });
  43. var token = this.$cookieStorage.get('user_token');
  44. var pages;
  45. if (this.channel == 'ec') {
  46. pages = 'pages/coupon/onDetail/onDetail';
  47. } else {
  48. pages = 'pages/coupon/offDetail/offDetail';
  49. }
  50. this.$http.post({
  51. api: 'api/coupon/share/agent/image',
  52. data: {
  53. coupon_id: this.id,
  54. pages: pages
  55. },
  56. header: {
  57. Authorization: token
  58. }
  59. }).then(res => {
  60. if (res.statusCode == 200) {
  61. res = res.data;
  62. if (res.status) {
  63. this.setData({
  64. info: res.data,
  65. init: true
  66. });
  67. } else {
  68. wx.showModal({
  69. content: res.message || '请求失败',
  70. showCancel: false
  71. });
  72. }
  73. } else {
  74. wx.showModal({
  75. content: '请求失败',
  76. showCancel: false
  77. });
  78. }
  79. wx.hideLoading();
  80. }).catch(() => {
  81. wx.hideLoading();
  82. wx.showModal({
  83. content: '请求失败',
  84. showCancel: false
  85. });
  86. });
  87. },
  88. closeAlert() {
  89. this.setData({
  90. is_refused: false
  91. });
  92. },
  93. // 下载图片
  94. downImg() {
  95. if (this.info.url) {
  96. wx.showLoading({
  97. title: '正在下载',
  98. mask: true
  99. });
  100. this.$http.dowloadFile({
  101. api: this.info.url
  102. }).then(res => {
  103. if (res.statusCode == 200) {
  104. wx.getSetting({
  105. success: ret => {
  106. // 如果之前没有授权
  107. if (!ret.authSetting['scope.writePhotosAlbum']) {
  108. wx.authorize({
  109. scope: 'scope.writePhotosAlbum',
  110. success: rej => {
  111. this.saveImg(res.tempFilePath);
  112. },
  113. // 用户拒绝授权
  114. fail: ret => {
  115. this.setData({
  116. is_refused: true
  117. });
  118. wx.hideLoading();
  119. }
  120. });
  121. } else {
  122. this.saveImg(res.tempFilePath);
  123. }
  124. }
  125. });
  126. } else {
  127. wx.hideLoading();
  128. wx.showToast({
  129. title: '下载图片失败',
  130. icon: 'none'
  131. });
  132. }
  133. }, err => {});
  134. }
  135. },
  136. // 保存图片
  137. saveImg(path) {
  138. wx.saveImageToPhotosAlbum({
  139. filePath: path,
  140. success: res => {
  141. wx.hideLoading();
  142. },
  143. fail: rej => {
  144. wx.hideLoading();
  145. wx.showToast({
  146. title: '保存图片失败',
  147. icon: 'none'
  148. });
  149. }
  150. });
  151. },
  152. setData: function (obj) {
  153. let that = this;
  154. let keys = [];
  155. let val, data;
  156. Object.keys(obj).forEach(function (key) {
  157. keys = key.split('.');
  158. val = obj[key];
  159. data = that.$data;
  160. keys.forEach(function (key2, index) {
  161. if (index + 1 == keys.length) {
  162. that.$set(data, key2, val);
  163. } else {
  164. if (!data[key2]) {
  165. that.$set(data, key2, {});
  166. }
  167. }
  168. data = data[key2];
  169. });
  170. });
  171. }
  172. },
  173. computed: {},
  174. watch: {}
  175. };
  176. </script>
  177. <style rel="stylesheet/less" lang="less">
  178. @import "shareImg";
  179. </style>