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

245 lines
6.4 KiB

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