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

235 lines
5.6 KiB

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