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

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