爱简收旧衣按件回收前端代码仓库
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.

521 lines
12 KiB

4 weeks ago
3 weeks ago
4 weeks ago
3 weeks ago
3 weeks ago
3 weeks ago
4 weeks ago
3 weeks ago
3 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
3 weeks ago
4 weeks ago
4 weeks ago
3 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
3 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
4 weeks ago
  1. <template>
  2. <view class="promo-modal-page">
  3. <!-- 顶部导航栏 -->
  4. <!-- <view class="nav-bar">
  5. <view class="back" @tap="navigateBack">
  6. <uni-icons type="left" size="20" />
  7. </view>
  8. <text class="title">推广链接</text>
  9. </view> -->
  10. <navbar title="推广链接" leftClick
  11. @leftClick="$utils.navigateBack" />
  12. <!-- 页面内容 -->
  13. <view class="content">
  14. <!-- 用户信息 -->
  15. <!-- <view class="user-info-modal">
  16. <view class="avatar-frame">
  17. <image class="avatar-img" :src="userInfo.headImage || '/static/avatar.png'" mode="aspectFill" />
  18. </view>
  19. <view class="nickname">{{ userInfo.nickName || '用户' }}</view>
  20. </view> -->
  21. <!-- 二维码区 -->
  22. <view class="qrcode-modal-section">
  23. <!-- 加载骨架屏 -->
  24. <view v-if="isLoading" class="qrcode-skeleton">
  25. <view class="skeleton-img"></view>
  26. <view class="skeleton-text"></view>
  27. </view>
  28. <!-- 二维码图片 -->
  29. <image
  30. v-else-if="qrcodeUrl"
  31. class="qrcode-img"
  32. :src="qrcodeUrl"
  33. mode="widthFix"
  34. :show-menu-by-longpress="true"
  35. @error="onImageError"
  36. @load="onImageLoad"
  37. />
  38. <view class="invite-code">邀请码{{inviteCode}}</view>
  39. </view>
  40. <!-- 底部按钮 -->
  41. <view class="bottom-btns-modal">
  42. <button class="btn gray" open-type="share">分享给好友</button>
  43. <button class="btn green" @tap="saveToAlbum">保存到本地</button>
  44. </view>
  45. </view>
  46. </view>
  47. </template>
  48. <script>
  49. import pullRefreshMixin from '@/pages/mixins/pullRefreshMixin.js'
  50. import config from '@/config.js'
  51. import navbar from '@/compoent/base/navbar.vue'
  52. export default {
  53. components : {
  54. navbar
  55. },
  56. mixins: [pullRefreshMixin],
  57. data() {
  58. return {
  59. userInfo: {},
  60. qrcodeUrl: '', // 二维码图片URL
  61. inviteCode: '888888',
  62. isLoading: true, // 加载状态,控制骨架屏显示
  63. retryCount: 0, // 重试次数
  64. maxRetries: 2, // 最大重试次数
  65. }
  66. },
  67. onLoad() {
  68. // 获取用户信息和二维码
  69. this.fetchUserInfo()
  70. this.getQrcode()
  71. },
  72. onUnload() {
  73. // 页面销毁时的清理逻辑
  74. },
  75. // 微信小程序分享配置
  76. // #ifdef MP-WEIXIN
  77. onShareAppMessage() {
  78. return {
  79. title: `${this.userInfo.nickName || '用户'}邀请您一起参与旧衣回收`,
  80. path: `/pages/component/home?shareId=${this.inviteCode}`,
  81. imageUrl: this.qrcodeUrl
  82. }
  83. },
  84. onShareTimeline() {
  85. return {
  86. title: `${this.userInfo.nickName || '用户'}邀请您一起参与旧衣回收,环保从我做起!`,
  87. query: `shareId=${this.inviteCode}`,
  88. imageUrl: this.qrcodeUrl
  89. }
  90. },
  91. // #endif
  92. methods: {
  93. async onRefresh() {
  94. // 重新获取用户信息和二维码
  95. this.retryCount = 0 // 重置重试计数
  96. this.isLoading = true // 重置加载状态,显示骨架屏
  97. this.fetchUserInfo()
  98. this.getQrcode()
  99. uni.stopPullRefresh()
  100. },
  101. navigateBack() {
  102. uni.navigateBack()
  103. },
  104. // 获取用户信息
  105. fetchUserInfo() {
  106. if (uni.getStorageSync('token')) {
  107. this.$api("getUserByToken", {}, (res) => {
  108. if (res.code == 200) {
  109. this.userInfo = res.result
  110. // 更新邀请码
  111. if (res.result.intentioCode) {
  112. this.inviteCode = res.result.intentioCode
  113. }
  114. }
  115. })
  116. }
  117. },
  118. getQrcode() {
  119. console.log('调用后端API生成二维码')
  120. uni.showLoading({
  121. title: '生成二维码中...'
  122. })
  123. this.generateQrcode()
  124. },
  125. // 生成二维码
  126. generateQrcode() {
  127. // 重置重试计数(仅在第一次调用时)
  128. if (this.retryCount === 0) {
  129. this.retryCount = 0
  130. }
  131. // 设置加载状态
  132. this.isLoading = true
  133. const token = uni.getStorageSync('token')
  134. const qrcodeUrl = `${config.baseUrl}/recycle-admin/applet/promotion/getInviteCode?token=${token}`
  135. // console.log('二维码URL:', qrcodeUrl)
  136. // 直接使用网络图片
  137. this.qrcodeUrl = qrcodeUrl
  138. if(this.qrcodeUrl){
  139. this.isLoading = false
  140. }
  141. uni.hideLoading()
  142. },
  143. // 保存到手机相册
  144. saveToAlbum() {
  145. if (!this.qrcodeUrl) {
  146. uni.showToast({
  147. title: '二维码还未加载完成',
  148. icon: 'none'
  149. })
  150. return
  151. }
  152. // 检查是否为iOS设备
  153. const systemInfo = uni.getSystemInfoSync()
  154. const isIOS = systemInfo.platform === 'ios'
  155. // 显示加载提示
  156. uni.showLoading({
  157. title: '保存中...'
  158. })
  159. // 先下载图片到本地
  160. uni.downloadFile({
  161. url: this.qrcodeUrl,
  162. success: res => {
  163. uni.hideLoading()
  164. if (res.statusCode === 200) {
  165. this.saveLocalImage(res.tempFilePath, isIOS)
  166. } else {
  167. uni.showToast({ title: '下载失败', icon: 'none' })
  168. }
  169. },
  170. fail: err => {
  171. uni.hideLoading()
  172. console.log('下载失败:', err)
  173. uni.showToast({ title: '下载失败', icon: 'none' })
  174. }
  175. })
  176. },
  177. // 保存本地图片到相册
  178. saveLocalImage(tempFilePath, isIOS) {
  179. // #ifdef APP-PLUS
  180. // APP端特殊处理
  181. uni.saveImageToPhotosAlbum({
  182. filePath: tempFilePath,
  183. success: () => {
  184. uni.showToast({
  185. title: '保存成功',
  186. icon: 'success'
  187. })
  188. },
  189. fail: (err) => {
  190. console.log('保存失败', err)
  191. // iOS权限处理
  192. if (isIOS || (err.errMsg && err.errMsg.includes('auth deny'))) {
  193. uni.showModal({
  194. title: '权限提示',
  195. content: '需要您授权保存相册权限,请在设置中开启',
  196. confirmText: '去设置',
  197. success: (res) => {
  198. if (res.confirm) {
  199. // #ifdef APP-PLUS
  200. plus.runtime.openURL('app-settings://')
  201. // #endif
  202. // #ifdef MP-WEIXIN
  203. uni.openSetting()
  204. // #endif
  205. }
  206. }
  207. })
  208. } else {
  209. uni.showToast({
  210. title: '保存失败',
  211. icon: 'none'
  212. })
  213. }
  214. }
  215. })
  216. // #endif
  217. // #ifdef MP-WEIXIN
  218. // 微信小程序端处理
  219. uni.getSetting({
  220. success: (res) => {
  221. if (res.authSetting['scope.writePhotosAlbum'] === false) {
  222. // 权限被拒绝,引导用户手动开启
  223. uni.showModal({
  224. title: '权限提示',
  225. content: '需要您授权保存相册权限,请在设置中开启',
  226. confirmText: '去设置',
  227. success: (modalRes) => {
  228. if (modalRes.confirm) {
  229. uni.openSetting()
  230. }
  231. }
  232. })
  233. } else {
  234. // 尝试保存,如果没有权限会自动弹出授权框
  235. uni.saveImageToPhotosAlbum({
  236. filePath: tempFilePath,
  237. success: () => {
  238. uni.showToast({
  239. title: '保存成功',
  240. icon: 'success'
  241. })
  242. },
  243. fail: (err) => {
  244. console.log('保存失败', err)
  245. // iOS特殊处理
  246. if (isIOS) {
  247. uni.showModal({
  248. title: 'iOS权限提示',
  249. content: 'iOS设备需要手动授权相册权限,请在设置中开启',
  250. confirmText: '去设置',
  251. success: (modalRes) => {
  252. if (modalRes.confirm) {
  253. uni.openSetting()
  254. }
  255. }
  256. })
  257. } else {
  258. uni.showToast({
  259. title: '保存失败',
  260. icon: 'none'
  261. })
  262. }
  263. }
  264. })
  265. }
  266. },
  267. fail: () => {
  268. // 直接尝试保存
  269. uni.saveImageToPhotosAlbum({
  270. filePath: tempFilePath,
  271. success: () => {
  272. uni.showToast({
  273. title: '保存成功',
  274. icon: 'success'
  275. })
  276. },
  277. fail: (err) => {
  278. console.log('保存失败', err)
  279. // iOS特殊处理
  280. if (isIOS) {
  281. uni.showModal({
  282. title: 'iOS权限提示',
  283. content: 'iOS设备需要手动授权相册权限,请在设置中开启',
  284. confirmText: '去设置',
  285. success: (modalRes) => {
  286. if (modalRes.confirm) {
  287. uni.openSetting()
  288. }
  289. }
  290. })
  291. } else {
  292. uni.showToast({
  293. title: '保存失败',
  294. icon: 'none'
  295. })
  296. }
  297. }
  298. })
  299. }
  300. })
  301. // #endif
  302. },
  303. // 图片加载成功
  304. onImageLoad() {
  305. console.log('二维码图片加载成功')
  306. this.isLoading = false // 隐藏骨架屏
  307. },
  308. // 图片加载失败
  309. onImageError() {
  310. console.log('二维码图片加载失败,尝试重试')
  311. // 如果还有重试次数,则重试
  312. if (this.retryCount < this.maxRetries) {
  313. this.retryCount++
  314. console.log(`${this.retryCount}次重试加载二维码`)
  315. // 延迟1秒后重试
  316. setTimeout(() => {
  317. this.generateQrcode()
  318. }, 1000)
  319. return
  320. }
  321. // 重试次数用完,显示错误提示并隐藏骨架屏
  322. console.log('重试次数用完,二维码加载失败')
  323. this.isLoading = false // 隐藏骨架屏
  324. uni.showToast({
  325. title: '二维码加载失败',
  326. icon: 'none'
  327. })
  328. }
  329. }
  330. }
  331. </script>
  332. <style lang="scss" scoped>
  333. .promo-modal-page {
  334. min-height: 100vh;
  335. background: #f8f8f8;
  336. // padding-bottom: calc(140rpx + env(safe-area-inset-bottom));
  337. overflow: hidden;
  338. }
  339. .nav-bar {
  340. display: flex;
  341. align-items: center;
  342. height: calc(150rpx + var(--status-bar-height));
  343. padding: 0 32rpx;
  344. padding-top: var(--status-bar-height);
  345. background: #fff;
  346. position: fixed;
  347. top: 0;
  348. left: 0;
  349. right: 0;
  350. z-index: 999;
  351. box-sizing: border-box;
  352. .back {
  353. padding: 20rpx;
  354. margin-left: -20rpx;
  355. }
  356. .title {
  357. flex: 1;
  358. text-align: center;
  359. font-size: 34rpx;
  360. font-weight: 500;
  361. color: #222;
  362. }
  363. }
  364. .content {
  365. // padding: 30rpx 0 0 0;
  366. padding: 20rpx;
  367. // margin-top: calc(150rpx + var(--status-bar-height) + 80rpx);
  368. height: 100%;
  369. display: flex;
  370. flex-direction: column;
  371. align-items: center;
  372. overflow: hidden;
  373. box-sizing: border-box;
  374. .user-info-modal {
  375. display: flex;
  376. flex-direction: column;
  377. align-items: center;
  378. margin-bottom: 48rpx;
  379. .avatar-frame {
  380. width: 104rpx;
  381. height: 104rpx;
  382. border-radius: 10rpx;
  383. overflow: hidden;
  384. background: #f2f2f2;
  385. box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.10);
  386. .avatar-img {
  387. width: 104rpx;
  388. height: 104rpx;
  389. object-fit: cover;
  390. display: block;
  391. }
  392. }
  393. .nickname {
  394. margin-top: 24rpx;
  395. font-size: 32rpx;
  396. font-weight: bold;
  397. color: #222;
  398. text-align: center;
  399. }
  400. }
  401. .qrcode-modal-section {
  402. width: 100%;
  403. display: flex;
  404. flex-direction: column;
  405. align-items: center;
  406. margin-bottom: 48rpx;
  407. .qrcode-skeleton {
  408. width: 100%;
  409. display: flex;
  410. flex-direction: column;
  411. align-items: center;
  412. .skeleton-img {
  413. width: 300rpx;
  414. height: 300rpx;
  415. border-radius: 20rpx;
  416. background: linear-gradient(90deg, #f0f0f0 25%, #e0e0e0 50%, #f0f0f0 75%);
  417. background-size: 200% 100%;
  418. animation: skeleton-loading 1.5s infinite;
  419. margin-bottom: 20rpx;
  420. }
  421. .skeleton-text {
  422. width: 200rpx;
  423. height: 32rpx;
  424. border-radius: 16rpx;
  425. background: linear-gradient(90deg, #f0f0f0 25%, #e0e0e0 50%, #f0f0f0 75%);
  426. background-size: 200% 100%;
  427. animation: skeleton-loading 1.5s infinite;
  428. }
  429. @keyframes skeleton-loading {
  430. 0% {
  431. background-position: 200% 0;
  432. }
  433. 100% {
  434. background-position: -200% 0;
  435. }
  436. }
  437. }
  438. .qrcode-img {
  439. width: 100%;
  440. height: 100%;
  441. background: #fff;
  442. border-radius: 24rpx;
  443. box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.08);
  444. }
  445. .invite-code {
  446. margin-top: 32rpx;
  447. font-size: 30rpx;
  448. color: #222;
  449. font-weight: bold;
  450. text-align: center;
  451. }
  452. }
  453. .bottom-btns-modal {
  454. position: fixed;
  455. left: 0;
  456. right: 0;
  457. bottom: 0;
  458. display: flex;
  459. justify-content: space-between;
  460. padding: 24rpx 32rpx calc(env(safe-area-inset-bottom) + 24rpx) 32rpx;
  461. background: #fff;
  462. z-index: 100;
  463. .btn {
  464. flex: 1;
  465. height: 88rpx;
  466. border-radius: 44rpx;
  467. font-size: 32rpx;
  468. font-weight: bold;
  469. margin: 0 12rpx;
  470. border: none;
  471. display: flex;
  472. align-items: center;
  473. justify-content: center;
  474. &.gray {
  475. background: linear-gradient(90deg, #b2f08d, #39e9d2);
  476. color: #fff;
  477. }
  478. &.green {
  479. background: linear-gradient(90deg, #b2f08d, #39e9d2);
  480. color: #fff;
  481. }
  482. }
  483. }
  484. }
  485. </style>