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

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