敢为人鲜小程序前端代码仓库
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.

240 lines
6.4 KiB

  1. <template>
  2. <!-- 导航栏 -->
  3. <view class="share-page">
  4. <navbar title="推广链接" leftClick @leftClick="$utils.navigateBack" bgColor="#019245" color="#fff" />
  5. <!-- 名片展示区域 -->
  6. <view class="card-container">
  7. <!-- 用户头像 - 放在卡片容器外使其一半在卡片之上 -->
  8. <view class="avatar-container">
  9. <image :src="shareData.userInfo.avatar" class="avatar" mode="aspectFill" />
  10. </view>
  11. <view class="share-card">
  12. <!-- 用户信息 -->
  13. <view class="user-info">
  14. <view class="nickname">{{ shareData.userInfo.nickname }} {{ shareData.userInfo.role }}</view>
  15. </view>
  16. <!-- 小程序码 -->
  17. <view class="qrcode-container">
  18. <image :src="shareData.qrCodeImage" class="qrcode-image" mode="aspectFit" />
  19. </view>
  20. <!-- 邀请码 -->
  21. <view class="invite-code">
  22. 邀请码{{ shareData.inviteCode }}
  23. </view>
  24. </view>
  25. </view>
  26. <!-- 底部按钮区域 -->
  27. <view class="action-buttons">
  28. <button class="action-btn share-btn" @click="shareToFriend">
  29. 分享给好友
  30. </button>
  31. <button class="action-btn save-btn" @click="saveToLocal">
  32. 保存到本地
  33. </button>
  34. </view>
  35. </view>
  36. </template>
  37. <script>
  38. import navbar from '@/components/base/navbar.vue'
  39. import { shareData } from '@/static/js/mockShare.js'
  40. export default {
  41. components: {
  42. navbar
  43. },
  44. data() {
  45. return {
  46. shareData: null
  47. }
  48. },
  49. onLoad() {
  50. this.shareData = shareData
  51. },
  52. methods: {
  53. // 分享给好友 - 微信小程序特有功能
  54. shareToFriend() {
  55. // 触发按钮形式的分享,提示用户使用右上角的分享
  56. uni.showModal({
  57. title: '分享提示',
  58. content: '点击右上角"..."按钮,选择"转发"即可分享给好友',
  59. confirmText: '我知道了',
  60. confirmColor: '#019245',
  61. showCancel: false
  62. })
  63. },
  64. // 保存到本地
  65. saveToLocal() {
  66. // 微信小程序保存图片需要用户授权
  67. try{
  68. this.$authorize('scope.writePhotosAlbum')
  69. this.saveImage()
  70. } catch (error) {
  71. }
  72. },
  73. // 保存图片的具体实现
  74. saveImage() {
  75. uni.showLoading({
  76. title: '保存中...'
  77. })
  78. // 直接保存小程序码图片
  79. uni.getImageInfo({
  80. // 获取图片信息
  81. src: this.shareData.qrCodeImage, // 图片的本地路径
  82. success: (res) => {
  83. uni.saveImageToPhotosAlbum({
  84. // 保存图片到系统相册
  85. filePath: res.path, // 返回的图片
  86. success: () => {
  87. uni.hideLoading()
  88. uni.showToast({
  89. title: '已保存到相册',
  90. icon: 'success'
  91. })
  92. },
  93. fail: () => {
  94. uni.hideLoading()
  95. uni.showToast({
  96. title: '保存失败',
  97. icon: 'error'
  98. })
  99. }
  100. })
  101. },
  102. fail: () => {
  103. uni.hideLoading()
  104. uni.showToast({
  105. title: '图片获取失败',
  106. icon: 'exception'
  107. })
  108. }
  109. })
  110. }
  111. },
  112. // 微信小程序的分享功能
  113. onShareAppMessage() {
  114. return {
  115. title: `邀请您使用敢为人鲜小程序,邀请码: ${this.shareData.inviteCode}`,
  116. path: `/pages/index/index?inviteCode=${this.shareData.inviteCode}`,
  117. imageUrl: this.shareData.qrCodeImage,
  118. success: () => {
  119. uni.showToast({
  120. title: '分享成功',
  121. icon: 'success'
  122. })
  123. },
  124. fail: () => {
  125. uni.showToast({
  126. title: '分享失败',
  127. icon: 'fail'
  128. })
  129. }
  130. }
  131. }
  132. }
  133. </script>
  134. <style lang="scss" scoped>
  135. .share-page {
  136. min-height: 100vh;
  137. background-color: $uni-color;
  138. display: flex;
  139. flex-direction: column;
  140. // padding-top: 30rpx;
  141. }
  142. .card-container {
  143. position: relative;
  144. display: flex;
  145. justify-content: center;
  146. margin: auto;
  147. // margin-bottom: 30rpx;
  148. width: 96%;
  149. padding-top: 80rpx;
  150. /* 为头像腾出空间 */
  151. }
  152. .avatar-container {
  153. position: absolute;
  154. top: 0;
  155. left: 50%;
  156. transform: translateX(-50%);
  157. z-index: 10;
  158. .avatar {
  159. width: 140rpx;
  160. height: 140rpx;
  161. border-radius: 50%;
  162. // border: 4rpx solid #fff;
  163. }
  164. }
  165. .share-card {
  166. width: 100%;
  167. /* 卡片占屏幕宽度的80% */
  168. background-color: #fff;
  169. border-radius: 16rpx;
  170. padding: 80rpx 20rpx 40rpx;
  171. /* 顶部padding增加,给头像留空间 */
  172. text-align: center;
  173. }
  174. .user-info {
  175. margin-bottom: 20rpx;
  176. .nickname {
  177. font-size: 26rpx;
  178. font-weight: bold;
  179. color: #333;
  180. }
  181. }
  182. .qrcode-container {
  183. width: 100%;
  184. display: flex;
  185. justify-content: center;
  186. margin: 20rpx 0;
  187. .qrcode-image {
  188. width: 440rpx;
  189. /* 稍微缩小二维码图片 */
  190. height: 440rpx;
  191. }
  192. }
  193. .invite-code {
  194. font-size: 26rpx;
  195. color: #333;
  196. margin: 20rpx 0;
  197. font-weight: 500;
  198. }
  199. .action-buttons {
  200. width: 100%;
  201. padding: 0 30rpx;
  202. display: flex;
  203. justify-content: space-between;
  204. margin-top: auto;
  205. margin-bottom: 60rpx;
  206. box-sizing: border-box;
  207. .action-btn {
  208. width: 45%;
  209. height: 110rpx;
  210. line-height: 110rpx;
  211. background-color: #fff;
  212. color: $uni-color;
  213. font-size: 32rpx;
  214. border-radius: 15rpx;
  215. border: none;
  216. }
  217. }
  218. </style>