share-popup.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. <template>
  2. <view >
  3. <u-popup class="share-popup" v-model="showshare" mode="bottom" border-radius="14" :closeable="true"
  4. :safe-area-inset-bottom="true" :mask-close-able="false">
  5. <view class="flex row-center md bold m-t-30 m-b-30">分享至</view>
  6. <view class="flex row-around share-tab">
  7. <view class="flex-col col--center" @tap="getPoster">
  8. <image mode="widthFix" class="share-icon" src="/static/images/icon_generate_poster.png"></image>
  9. <view class="m-t-10">生成海报</view>
  10. </view>
  11. <!-- #ifdef MP-WEIXIN-->
  12. <button open-type="share" class="flex-col col--center" hover-class="none">
  13. <image class="share-icon" src="/static/images/icon_wechat.png"></image>
  14. <view class="m-t-10">微信好友</view>
  15. </button>
  16. <!-- #endif -->
  17. <!-- #ifdef H5 || APP-PLUS -->
  18. <view oclass="flex-col col--center" @tap="shareWx">
  19. <image class="share-icon" src="/static/images/icon_wechat.png"></image>
  20. <view class="m-t-10">微信好友</view>
  21. </view>
  22. <!-- #endif -->
  23. </view>
  24. <view class="flex row-center bg-body cancel xl" @tap="showshare=false">取消</view>
  25. </u-popup>
  26. <u-popup class="share-poster" v-model="showPoster" mode="center" :closeable="true"
  27. :safe-area-inset-bottom="true">
  28. <!-- #ifndef H5 -->
  29. <image style="width: 640rpx;" mode="widthFix" :src="poster"></image>
  30. <!-- #endif -->
  31. <!-- #ifdef H5 -->
  32. <img style="width: 640rpx;" :src="poster" />
  33. <!-- #endif -->
  34. <button class="flex row-center save-btn" size="lg" @tap="savePoster">
  35. <!-- #ifndef H5 -->
  36. 保存图片到相册
  37. <!-- #endif -->
  38. <!-- #ifdef H5 -->
  39. 长按保存图片到相册
  40. <!-- #endif -->
  41. </button>
  42. </u-popup>
  43. <!-- #ifdef H5 -->
  44. <u-popup :custom-style="{'background': 'none'}" class="share-tips" v-model="showTips" mode="top">
  45. <view style="overflow: hidden;">
  46. <image src="/static/images/share_arrow.png" class="share-arrow" />
  47. <view class="white" style="text-align: center;margin-top: 280rpx;">
  48. <view class="bold lg">立即分享给好友吧</view>
  49. <view class="sm m-t-10">点击屏幕右上角将本页面分享给好友</view>
  50. </view>
  51. </view>
  52. </u-popup>
  53. <!-- #endif -->
  54. <poster v-if="enablePoster" :type="type" :share-id="shareId" :page-path="pagePath" :config="config"
  55. :qrcode="mnpQrcode" :link="getLink" @success="handleSuccess" @fail="handleFail" />
  56. </view>
  57. </template>
  58. <script>
  59. import {
  60. apiMnpQrCode,
  61. apiTtQrCode
  62. } from "@/api/app"
  63. import {
  64. baseURL,
  65. basePath
  66. } from '@/config/app'
  67. import {mapGetters} from 'vuex'
  68. import poster from './poster.vue'
  69. export default {
  70. components: {
  71. poster
  72. },
  73. props: {
  74. value: {
  75. type: Boolean
  76. },
  77. shareId: {
  78. type: [String, Number],
  79. default: ''
  80. },
  81. config: {
  82. type: Object,
  83. default: () => ({})
  84. },
  85. pagePath: {
  86. type: String,
  87. default: ''
  88. },
  89. type: {
  90. type: String,
  91. default: 'goods'
  92. }
  93. },
  94. data() {
  95. return {
  96. poster: "",
  97. enablePoster: false,
  98. showPoster: false,
  99. showTips: false,
  100. mnpQrcode: ''
  101. };
  102. },
  103. computed: {
  104. ...mapGetters(['inviteCode', 'userInfo']),
  105. getLink() {
  106. return `${baseURL}${basePath}/${this.pagePath}?id=${this.shareId}&invite_code=${this.inviteCode}`
  107. },
  108. showshare: {
  109. get() {
  110. return this.value
  111. },
  112. set(val) {
  113. this.$emit('input', val)
  114. }
  115. }
  116. },
  117. watch: {
  118. showPoster(val) {
  119. if (!val) {
  120. this.enablePoster = false
  121. }
  122. }
  123. },
  124. methods: {
  125. async getPoster() {
  126. if (!this.isLogin) return this.$Router.push('/pages/login/login')
  127. uni.showLoading({
  128. title: '正在生成中'
  129. })
  130. // #ifdef MP-WEIXIN
  131. if (!this.mnpQrcode) {
  132. // #ifdef MP-WEIXIN
  133. const res = await this.getMnpQrcode()
  134. // #endif
  135. this.mnpQrcode = res.data.qr_code.replaceAll("\r\n", "")
  136. }
  137. this.enablePoster = true
  138. // #endif
  139. // #ifdef APP-PLUS || H5
  140. this.enablePoster = true
  141. // #endif
  142. },
  143. // 获取商品页面二维码
  144. getMnpQrcode() {
  145. return new Promise((resolve, reject) => {
  146. apiMnpQrCode({
  147. id: this.shareId,
  148. url: this.pagePath,
  149. type: this.type
  150. }).then((res) => {
  151. resolve(res)
  152. }).catch(() => {
  153. reject()
  154. })
  155. })
  156. },
  157. getTtQrCode() {
  158. return new Promise((resolve, reject) => {
  159. const sys = uni.getSystemInfoSync()
  160. apiTtQrCode({
  161. appname: TtAppNameEnum[sys.appName],
  162. id: this.shareId,
  163. page: this.pagePath
  164. }).then((res) => {
  165. resolve(res)
  166. }).catch(() => {
  167. uni.hideLoading()
  168. })
  169. })
  170. },
  171. handleSuccess(val) {
  172. this.poster = val
  173. uni.hideLoading()
  174. this.showPoster = true
  175. this.showshare = false
  176. },
  177. handleFail() {
  178. uni.hideLoading({
  179. success: () => {
  180. this.$toast({
  181. title: '生成失败'
  182. })
  183. }
  184. })
  185. },
  186. shareWx() {
  187. // #ifdef H5
  188. this.showTips = true
  189. this.showshare = false
  190. // #endif
  191. // #ifdef APP-PLUS
  192. uni.share({
  193. provider: "weixin",
  194. scene: "WXSceneSession",
  195. type: 0,
  196. href: this.getLink,
  197. title: this.config.name,
  198. summary: '',
  199. imageUrl: this.config.image,
  200. success: (res) => {
  201. console.log('分享成功');
  202. },
  203. fail: (err) => {
  204. this.$toast({
  205. title: err.errMsg
  206. })
  207. }
  208. });
  209. // #endif
  210. },
  211. async savePoster() {
  212. uni.saveImageToPhotosAlbum({
  213. filePath: this.poster,
  214. success: res => {
  215. this.showPoster = false
  216. this.$toast({
  217. title: '保存成功',
  218. icon: 'success'
  219. });
  220. },
  221. fail: (err) => {
  222. this.$toast({
  223. title: '保存失败'
  224. });
  225. console.log(err)
  226. }
  227. })
  228. }
  229. }
  230. }
  231. </script>
  232. <style lang="scss">
  233. .share-popup {
  234. .share-tab {
  235. margin: 40rpx 0 140rpx;
  236. .share-icon {
  237. width: 100rpx;
  238. height: 100rpx;
  239. }
  240. }
  241. .cancel {
  242. height: 98rpx;
  243. }
  244. }
  245. .share-poster {
  246. .share-img {
  247. width: 640rpx;
  248. border-radius: 12rpx;
  249. }
  250. .save-btn {
  251. background-color: $-color-primary;
  252. color: #fff;
  253. margin-top: 20rpx;
  254. }
  255. }
  256. .share-tips .share-arrow {
  257. width: 140rpx;
  258. height: 250rpx;
  259. float: right;
  260. margin: 15rpx 31rpx 0 0;
  261. }
  262. </style>