四零语境前端代码仓库
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.

220 lines
5.3 KiB

  1. <template>
  2. <view class="share-page">
  3. <!-- <uv-status-bar></uv-status-bar> -->
  4. <!-- 主要内容区域 -->
  5. <view class="content">
  6. <!-- 中间图片 -->
  7. <view class="image-container">
  8. <image
  9. class="share-image"
  10. :src="Qrcode"
  11. mode="aspectFit"
  12. ></image>
  13. </view>
  14. </view>
  15. <!-- 底部固定按钮 -->
  16. <view class="bottom-button-container">
  17. <uv-button :custom-style="{
  18. height: '82rpx',
  19. borderRadius: '198rpx',
  20. background: '#06DADC',
  21. border: '2rpx solid #06DADC',
  22. lineHeight: '82rpx',
  23. fontSize: '36rpx'
  24. }" type="primary" @click="save">保存到相册</uv-button>
  25. <uv-safe-bottom></uv-safe-bottom>
  26. </view>
  27. </view>
  28. </template>
  29. <script>
  30. export default {
  31. data() {
  32. return {
  33. Qrcode: '',
  34. }
  35. },
  36. methods: {
  37. handleShare() {
  38. // 分享逻辑
  39. uni.showToast({
  40. title: '分享功能',
  41. icon: 'none'
  42. })
  43. },
  44. async save() {
  45. try {
  46. // 检查相册权限
  47. const authResult = await this.checkPhotoAlbumAuth();
  48. if (authResult.authSetting['scope.writePhotosAlbum'] === true) {
  49. // 已有权限,直接保存
  50. this.saveToAlbum();
  51. } else if (authResult.authSetting['scope.writePhotosAlbum'] === false) {
  52. // 权限被拒绝,引导用户到设置页面
  53. this.showAuthGuide();
  54. } else {
  55. // 未授权,请求权限
  56. this.requestPhotoAlbumAuth();
  57. }
  58. } catch (error) {
  59. console.error('权限检查失败:', error);
  60. // 如果权限检查失败,直接尝试保存(兼容处理)
  61. this.saveToAlbum();
  62. }
  63. },
  64. // 检查相册权限
  65. checkPhotoAlbumAuth() {
  66. return new Promise((resolve, reject) => {
  67. uni.getSetting({
  68. success: (res) => {
  69. resolve(res);
  70. },
  71. fail: (err) => {
  72. reject(err);
  73. }
  74. });
  75. });
  76. },
  77. // 请求相册权限
  78. requestPhotoAlbumAuth() {
  79. uni.authorize({
  80. scope: 'scope.writePhotosAlbum',
  81. success: () => {
  82. // 权限请求成功,保存图片
  83. this.saveToAlbum();
  84. },
  85. fail: () => {
  86. // 权限请求被拒绝,引导用户到设置页面
  87. this.showAuthGuide();
  88. }
  89. });
  90. },
  91. // 显示权限引导
  92. showAuthGuide() {
  93. uni.showModal({
  94. title: '需要相册权限',
  95. content: '保存图片需要访问您的相册权限,请在设置中开启相册权限后重试',
  96. confirmText: '去设置',
  97. cancelText: '取消',
  98. success: (res) => {
  99. if (res.confirm) {
  100. // 打开设置页面
  101. uni.openSetting({
  102. success: (settingRes) => {
  103. if (settingRes.authSetting['scope.writePhotosAlbum']) {
  104. // 用户在设置页面开启了权限,再次调用保存
  105. this.saveToAlbum();
  106. } else {
  107. uni.showToast({
  108. title: '未开启相册权限',
  109. icon: 'none'
  110. });
  111. }
  112. }
  113. });
  114. }
  115. }
  116. });
  117. },
  118. // 保存图片到相册
  119. saveToAlbum() {
  120. if (!this.Qrcode) {
  121. uni.showToast({
  122. title: '图片还未加载完成',
  123. icon: 'none'
  124. });
  125. return;
  126. }
  127. uni.saveImageToPhotosAlbum({
  128. filePath: this.Qrcode,
  129. success: (res) => {
  130. uni.showToast({
  131. title: '保存成功',
  132. icon: 'success'
  133. });
  134. },
  135. fail: (err) => {
  136. console.error('保存失败:', err);
  137. if (err.errMsg.includes('auth')) {
  138. // 如果是权限问题,再次引导用户
  139. this.showAuthGuide();
  140. } else {
  141. uni.showToast({
  142. title: '保存失败,请重试',
  143. icon: 'none'
  144. });
  145. }
  146. }
  147. });
  148. },
  149. async getQrcode() {
  150. // const res = await this.$api.promotion.qrCode()
  151. // if (res.code === 200) {
  152. uni.getImageInfo({
  153. src: `${this.$config.baseURL}/promotion/qrCode?token=${uni.getStorageSync('token')}`,
  154. success: (image) => {
  155. console.log(image.width);
  156. console.log(image.path);
  157. this.Qrcode = image.path;
  158. console.log(image.height);
  159. }
  160. });
  161. // }
  162. }
  163. },
  164. onShow() {
  165. this.getQrcode()
  166. },
  167. }
  168. </script>
  169. <style lang="scss" scoped>
  170. .share-page {
  171. min-height: 100vh;
  172. background-color: #f5f5f5;
  173. display: flex;
  174. flex-direction: column;
  175. }
  176. .content {
  177. height: 100vh;
  178. flex: 1;
  179. padding-bottom: 200rpx;
  180. display: flex;
  181. align-items: center;
  182. justify-content: center;
  183. .image-container {
  184. // background: red;
  185. display: flex;
  186. // height: 100%;
  187. justify-content: center;
  188. align-items: center;
  189. .share-image {
  190. height: 90vh;
  191. width: 670rpx;
  192. border-radius: 16rpx;
  193. }
  194. }
  195. }
  196. // 底部固定按钮
  197. .bottom-button-container {
  198. position: fixed;
  199. bottom: 0;
  200. left: 0;
  201. right: 0;
  202. padding: 30rpx 40rpx;
  203. background: rgba(255, 255, 255, 0.95);
  204. border-top: 1px solid #F1F1F1;
  205. }
  206. </style>