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

231 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. success: (image) => {
  157. this.Qrcode = image.path;
  158. this.$store.commit('setQrcode', this.Qrcode)
  159. this.isLoading = false
  160. },
  161. fail: (err) => {
  162. console.error('获取二维码失败:', err);
  163. this.isLoading = false
  164. }
  165. });
  166. }
  167. },
  168. onLoad() {
  169. if (this.$store.state.Qrcode){
  170. this.Qrcode = this.$store.state.Qrcode
  171. }else {
  172. this.getQrcode()
  173. }
  174. }
  175. }
  176. </script>
  177. <style lang="scss" scoped>
  178. .share-page {
  179. min-height: 100vh;
  180. background-color: #f5f5f5;
  181. display: flex;
  182. flex-direction: column;
  183. }
  184. .content {
  185. height: 100vh;
  186. flex: 1;
  187. padding-bottom: 200rpx;
  188. display: flex;
  189. align-items: center;
  190. justify-content: center;
  191. .image-container {
  192. // background: red;
  193. display: flex;
  194. // height: 100%;
  195. justify-content: center;
  196. align-items: center;
  197. .share-image {
  198. height: 90vh;
  199. width: 670rpx;
  200. border-radius: 16rpx;
  201. }
  202. }
  203. }
  204. // 底部固定按钮
  205. .bottom-button-container {
  206. position: fixed;
  207. bottom: 0;
  208. left: 0;
  209. right: 0;
  210. padding: 30rpx 40rpx;
  211. background: rgba(255, 255, 255, 0.95);
  212. border-top: 1px solid #F1F1F1;
  213. }
  214. </style>