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

179 lines
4.4 KiB

  1. <template>
  2. <view id="shareImg" v-if="init">
  3. <image mode="widthFix" :src="url" alt></image>
  4. <view class="save" @tap="down">
  5. 保存图片
  6. </view>
  7. <view class="tips">
  8. 保存图片并分享到朋友圈让好友为你打Call
  9. </view>
  10. <!--用户拒绝下载图片授权弹出-->
  11. <alert :is_refused="is_refused" @close="closeAlert"></alert>
  12. </view>
  13. </template>
  14. <script>
  15. import {pageLogin, getUrl,config,is} from '@/common/js/utils.js';
  16. import alert from "@/components/alert/alert";
  17. export default {
  18. data() {
  19. return {
  20. url: '',
  21. init: false,
  22. is_refused: false
  23. };
  24. },
  25. onLoad(e) {
  26. this.queryImgUrl(e.task_id, e.type);
  27. },
  28. components: {
  29. alert
  30. },
  31. props: {},
  32. methods: {
  33. closeAlert() {
  34. this.setData({
  35. is_refused: false
  36. });
  37. },
  38. // 获取图片url
  39. queryImgUrl(id, type) {
  40. var token = this.$cookieStorage.get('user_token');
  41. var pages = 'pages/store/shareCall/shareCall';
  42. wx.showLoading({
  43. title: "正在生成图片",
  44. mask: true
  45. });
  46. this.$http.get({
  47. api: 'api/free_event/createShareImage',
  48. header: {
  49. Authorization: token
  50. },
  51. data: {
  52. page: pages,
  53. task_id: id
  54. }
  55. }).then(res => {
  56. if (res.statusCode == 200) {
  57. res = res.data;
  58. if (res.status) {
  59. this.setData({
  60. init: true,
  61. url: res.data.image
  62. });
  63. } else {
  64. wx.showModal({
  65. content: res.message || '请求失败',
  66. showCancel: false
  67. });
  68. }
  69. } else {
  70. wx.showModal({
  71. content: res.message || '请求失败',
  72. showCancel: false
  73. });
  74. }
  75. wx.hideLoading();
  76. }).catch(rej => {
  77. wx.hideLoading();
  78. wx.showModal({
  79. content: '内部错误',
  80. showCancel: false
  81. });
  82. });
  83. },
  84. // 下载图片
  85. down() {
  86. if (this.url) {
  87. wx.showLoading({
  88. title: '正在下载',
  89. mask: true
  90. });
  91. this.$http.dowloadFile({
  92. api: this.url
  93. }).then(res => {
  94. if (res.statusCode == 200) {
  95. wx.getSetting({
  96. success: ret => {
  97. // 如果之前没有授权
  98. if (!ret.authSetting['scope.writePhotosAlbum']) {
  99. wx.authorize({
  100. scope: 'scope.writePhotosAlbum',
  101. success: rej => {
  102. this.saveImg(res.tempFilePath);
  103. },
  104. // 用户拒绝授权
  105. fail: ret => {
  106. this.setData({
  107. is_refused: true
  108. });
  109. wx.hideLoading();
  110. }
  111. });
  112. } else {
  113. this.saveImg(res.tempFilePath);
  114. }
  115. }
  116. });
  117. } else {
  118. wx.hideLoading();
  119. wx.showToast({
  120. title: '下载图片失败',
  121. icon: 'none'
  122. });
  123. }
  124. }, err => {});
  125. }
  126. },
  127. // 保存图片
  128. saveImg(path) {
  129. wx.saveImageToPhotosAlbum({
  130. filePath: path,
  131. success: res => {
  132. wx.hideLoading();
  133. },
  134. fail: rej => {
  135. wx.hideLoading();
  136. wx.showToast({
  137. title: '保存图片失败',
  138. icon: 'none'
  139. });
  140. }
  141. });
  142. },
  143. setData: function (obj) {
  144. let that = this;
  145. let keys = [];
  146. let val, data;
  147. Object.keys(obj).forEach(function (key) {
  148. keys = key.split('.');
  149. val = obj[key];
  150. data = that.$data;
  151. keys.forEach(function (key2, index) {
  152. if (index + 1 == keys.length) {
  153. that.$set(data, key2, val);
  154. } else {
  155. if (!data[key2]) {
  156. that.$set(data, key2, {});
  157. }
  158. }
  159. data = data[key2];
  160. });
  161. });
  162. }
  163. },
  164. computed: {},
  165. watch: {}
  166. };
  167. </script>
  168. <style>
  169. #shareImg{padding:10px 15px}#shareImg image{display:block;width:100%;border-radius:6px;overflow:hidden}#shareImg .save{width:100%;height:44px;line-height:44px;color:#FFF;text-align:center;background:#ff2741;box-shadow:0 4px 8px 0 rgba(155,155,155,.7);border-radius:20px;margin:10px 0}#shareImg .tips{font-size:12px;color:#888;text-align:center}
  170. </style>