金诚优选前端代码
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.

177 lines
4.1 KiB

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