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

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