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

185 lines
4.3 KiB

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