index.vue 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <template>
  2. <view class="sharesheet" :class="{ show: modelValue }">
  3. <view class="mask-class sharesheet__mask" @click="onMaskClick"></view>
  4. <view class="sharesheet__container">
  5. <!-- 分享选项列表 -->
  6. <view class="sharesheet__list">
  7. <!-- 选项按钮: 发送给朋友(仅支持小程序) -->
  8. <!-- #ifdef MP -->
  9. <button class="share-item btn-normal" open-type="share" @click="handleCancel()">
  10. <view class="item-image" :style="{ backgroundColor: '#44DB74' }">
  11. <text class="iconfont icon-weixin"></text>
  12. </view>
  13. <view class="item-name">
  14. <text>发送给朋友</text>
  15. </view>
  16. </button>
  17. <!-- #endif -->
  18. <view class="share-item" @click="handleCopyLink()">
  19. <view class="item-image" :style="{ backgroundColor: '#38beec' }">
  20. <text class="iconfont icon-link"></text>
  21. </view>
  22. <view class="item-name">
  23. <text>复制链接</text>
  24. </view>
  25. </view>
  26. <!-- <view class="share-item">
  27. <view class="item-image" :style="{ backgroundColor: '#FE8A4F' }">
  28. <text class="iconfont icon-weibo"></text>
  29. </view>
  30. <view class="item-name">
  31. <text>新浪微博</text>
  32. </view>
  33. </view> -->
  34. <!-- <view class="share-item">
  35. <view class="item-image" :style="{ backgroundColor: '#56C0F2' }">
  36. <text class="iconfont icon-qq"></text>
  37. </view>
  38. <view class="item-name">
  39. <text>QQ好友</text>
  40. </view>
  41. </view> -->
  42. <!-- <view class="share-item">
  43. <view class="item-image" :style="{ backgroundColor: '#FFBB0D' }">
  44. <text class="iconfont icon-qzone"></text>
  45. </view>
  46. <view class="item-name">
  47. <text>QQ空间</text>
  48. </view>
  49. </view> -->
  50. </view>
  51. <!-- 取消按钮 -->
  52. <view v-if="cancelText" class="sharesheet__footer" @click="handleCancel()">
  53. <view class="btn-cancel">{{ cancelText }}</view>
  54. </view>
  55. </view>
  56. </view>
  57. </template>
  58. <!-- 参考的uniapp文档 -->
  59. <!-- https://uniapp.dcloud.io/component/button?id=button -->
  60. <!-- https://uniapp.dcloud.io/api/plugins/share -->
  61. <script>
  62. import Config from '@/core/config'
  63. import { getCurrentPage, buildUrL } from '@/core/app'
  64. import { inArray } from '@/utils/util'
  65. import StoreModel from '@/common/model/Store'
  66. export default {
  67. name: 'ShareSheet',
  68. components: {},
  69. emits: ['update:modelValue'],
  70. props: {
  71. // 控制组件显示隐藏
  72. modelValue: {
  73. Type: Boolean,
  74. default: false
  75. },
  76. // 点击遮罩层取消
  77. cancelWithMask: {
  78. type: Boolean,
  79. default: true
  80. },
  81. // 分享链接的标题
  82. shareTitle: {
  83. type: String,
  84. default: '商品分享'
  85. },
  86. // 分享链接的封面图
  87. shareImageUrl: {
  88. type: String,
  89. default: ''
  90. },
  91. // 取消按钮文字
  92. cancelText: {
  93. type: String,
  94. default: '关闭'
  95. }
  96. },
  97. data() {
  98. return {
  99. }
  100. },
  101. // 初始化方法
  102. created() {
  103. this.initSharesheet()
  104. },
  105. methods: {
  106. // 初始化选择项
  107. initSharesheet() {},
  108. // 点击遮罩层(关闭菜单)
  109. onMaskClick() {
  110. if (this.cancelWithMask) {
  111. this.handleCancel()
  112. }
  113. },
  114. // 获取分享链接 (H5外链)
  115. getShareUrl() {
  116. const { path, query } = getCurrentPage()
  117. return new Promise((resolve, reject) => {
  118. // 获取h5站点地址
  119. StoreModel.h5Url().then(baseUrl => {
  120. // 生成完整的分享链接
  121. const shareUrl = buildUrL(baseUrl, path, query)
  122. resolve(shareUrl)
  123. })
  124. })
  125. },
  126. // 复制商品链接
  127. handleCopyLink() {
  128. const app = this
  129. app.getShareUrl().then(shareUrl => {
  130. // 复制到剪贴板
  131. uni.setClipboardData({
  132. data: shareUrl,
  133. success: () => app.$toast('链接复制成功,快去发送给朋友吧~'),
  134. fail: ({ errMsg }) => app.$toast('复制失败 ' + errMsg),
  135. complete: () => app.handleCancel()
  136. })
  137. })
  138. },
  139. // 关闭菜单
  140. handleCancel() {
  141. this.$emit('update:modelValue', false)
  142. }
  143. }
  144. }
  145. </script>
  146. <style lang="scss" scoped>
  147. .sharesheet {
  148. background-color: #f8f8f8;
  149. font-size: 28rpx;
  150. }
  151. .sharesheet__mask {
  152. position: fixed;
  153. top: 0;
  154. left: var(--window-left);
  155. right: var(--window-right);
  156. bottom: var(--window-bottom);
  157. z-index: 12;
  158. background: rgba(0, 0, 0, 0.7);
  159. display: none;
  160. }
  161. .sharesheet__container {
  162. position: fixed;
  163. left: var(--window-left);
  164. right: var(--window-right);
  165. bottom: var(--window-bottom);
  166. background: #ffffff;
  167. transform: translate3d(0, 50%, 0);
  168. transform-origin: center;
  169. transition: all 0.2s ease;
  170. z-index: 13;
  171. opacity: 0;
  172. visibility: hidden;
  173. border-top-left-radius: 26rpx;
  174. border-top-right-radius: 26rpx;
  175. padding: 50rpx 30rpx 0 30rpx;
  176. // 设置ios刘海屏底部横线安全区域
  177. padding-bottom: calc(constant(safe-area-inset-bottom) + 30rpx);
  178. padding-bottom: calc(env(safe-area-inset-bottom) + 30rpx);
  179. }
  180. .sharesheet__list {
  181. display: flex;
  182. flex-wrap: wrap;
  183. justify-content: flex-start;
  184. margin-bottom: -35rpx;
  185. .share-item {
  186. flex: 0 0 25%;
  187. margin-bottom: 40rpx;
  188. .item-name,
  189. .item-image {
  190. width: 140rpx;
  191. margin: 0 auto;
  192. }
  193. .item-image {
  194. display: flex;
  195. justify-content: center;
  196. align-items: center;
  197. width: 86rpx;
  198. height: 86rpx;
  199. border-radius: 50%;
  200. color: #fff;
  201. font-size: 38rpx;
  202. }
  203. .item-name {
  204. margin-top: 12rpx;
  205. text-align: center;
  206. font-size: 26rpx;
  207. }
  208. }
  209. }
  210. .sharesheet__footer {
  211. background: #fff;
  212. margin-top: 40rpx;
  213. .btn-cancel {
  214. font-size: 28rpx;
  215. text-align: center;
  216. }
  217. }
  218. // 显示状态
  219. .show {
  220. .sharesheet__mask {
  221. display: block;
  222. }
  223. .sharesheet__container {
  224. opacity: 1;
  225. -webkit-transform: translate3d(0, 0, 0);
  226. transform: translate3d(0, 0, 0);
  227. visibility: visible;
  228. }
  229. }
  230. </style>