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

363 lines
8.2 KiB

1 month ago
3 weeks ago
1 month ago
3 weeks ago
3 weeks ago
3 weeks ago
1 month ago
3 weeks ago
3 weeks ago
1 month ago
1 month ago
3 weeks ago
1 month ago
1 month ago
3 weeks ago
1 month ago
1 month ago
3 weeks ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
3 weeks ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month 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. <image
  24. class="qrcode-img"
  25. :src="qrcodeUrl"
  26. mode="widthFix"
  27. :show-menu-by-longpress="true"
  28. @error="onImageError"
  29. @load="onImageLoad"
  30. />
  31. <view class="invite-code">邀请码{{inviteCode}}</view>
  32. </view>
  33. <!-- 底部按钮 -->
  34. <view class="bottom-btns-modal">
  35. <button class="btn gray" open-type="share">分享给好友</button>
  36. <button class="btn green" @tap="saveToAlbum">保存到本地</button>
  37. </view>
  38. </view>
  39. </view>
  40. </template>
  41. <script>
  42. import pullRefreshMixin from '@/pages/mixins/pullRefreshMixin.js'
  43. import config from '@/config.js'
  44. import navbar from '@/compoent/base/navbar.vue'
  45. export default {
  46. components : {
  47. navbar
  48. },
  49. mixins: [pullRefreshMixin],
  50. data() {
  51. return {
  52. userInfo: {},
  53. qrcodeUrl: '/static/qrcode.png', // 展示用的图片URL
  54. qrcodeLocalPath: '', // 下载用的本地文件路径
  55. inviteCode: '888888',
  56. }
  57. },
  58. onLoad() {
  59. // 获取用户信息和二维码
  60. this.fetchUserInfo()
  61. this.getQrcode()
  62. },
  63. // 微信小程序分享配置
  64. // #ifdef MP-WEIXIN
  65. onShareAppMessage() {
  66. return {
  67. title: `${this.userInfo.nickName || '用户'}邀请您一起参与旧衣回收`,
  68. path: `/pages/component/home?shareId=${this.inviteCode}`,
  69. imageUrl: this.qrcodeUrl || '/static/share-default.png'
  70. }
  71. },
  72. onShareTimeline() {
  73. return {
  74. title: `${this.userInfo.nickName || '用户'}邀请您一起参与旧衣回收,环保从我做起!`,
  75. query: `shareId=${this.inviteCode}`,
  76. imageUrl: this.qrcodeUrl || '/static/share-default.png'
  77. }
  78. },
  79. // #endif
  80. methods: {
  81. async onRefresh() {
  82. // 模拟刷新数据
  83. await new Promise(resolve => setTimeout(resolve, 1000))
  84. uni.stopPullRefresh()
  85. },
  86. navigateBack() {
  87. uni.navigateBack()
  88. },
  89. // 获取用户信息
  90. fetchUserInfo() {
  91. if (uni.getStorageSync('token')) {
  92. this.$api("getUserByToken", {}, (res) => {
  93. if (res.code == 200) {
  94. this.userInfo = res.result
  95. // 更新邀请码
  96. if (res.result.intentioCode) {
  97. this.inviteCode = res.result.intentioCode
  98. }
  99. }
  100. })
  101. }
  102. },
  103. getQrcode() {
  104. // 获取全局二维码路径
  105. if (getApp().globalData.qr_path && getApp().globalData.qr_path.startsWith('wxfile://')) {
  106. console.log('获取到二维码路径', getApp().globalData.qr_path)
  107. this.qrcodeLocalPath = getApp().globalData.qr_path
  108. this.qrcodeUrl = getApp().globalData.qr_path
  109. } else {
  110. console.log('全局二维码路径不存在,调用后端API生成')
  111. uni.showLoading({
  112. title: '生成二维码中...'
  113. })
  114. this.generateQrcode()
  115. }
  116. },
  117. // 只保留后端API生成二维码
  118. generateQrcode() {
  119. const token = uni.getStorageSync('token')
  120. const qrcodeUrl = `${config.baseUrl}/recycle-admin/applet/promotion/getInviteCode?token=${token}`
  121. console.log('二维码URL:', qrcodeUrl)
  122. // 展示用网络图片
  123. this.qrcodeUrl = qrcodeUrl
  124. // 下载到本地用于保存
  125. uni.downloadFile({
  126. url: qrcodeUrl,
  127. success: res => {
  128. console.log('下载成功:', res)
  129. if (res.statusCode === 200) {
  130. // 使用微信小程序推荐的临时文件路径格式
  131. const tempFilePath = res.tempFilePath
  132. getApp().globalData.qr_path = tempFilePath
  133. this.qrcodeLocalPath = tempFilePath
  134. console.log('本地二维码路径:', tempFilePath)
  135. } else {
  136. console.log('下载失败,状态码:', res.statusCode)
  137. uni.showToast({ title: '二维码下载失败', icon: 'none' })
  138. }
  139. },
  140. fail: err => {
  141. console.log('下载失败:', err)
  142. uni.showToast({ title: '二维码下载失败', icon: 'none' })
  143. },
  144. complete() {
  145. uni.hideLoading()
  146. }
  147. })
  148. },
  149. // 保存到手机相册
  150. saveToAlbum() {
  151. if (!this.qrcodeLocalPath) {
  152. uni.showToast({
  153. title: '二维码还未下载完成,请稍后再试',
  154. icon: 'none'
  155. })
  156. return
  157. }
  158. // 使用新的权限检查方式
  159. uni.getSetting({
  160. success: (res) => {
  161. if (res.authSetting['scope.writePhotosAlbum'] === false) {
  162. // 权限被拒绝,引导用户手动开启
  163. uni.showModal({
  164. title: '提示',
  165. content: '需要您授权保存相册权限,请在设置中开启',
  166. showCancel: false,
  167. success: () => {
  168. uni.openSetting()
  169. }
  170. })
  171. } else {
  172. // 尝试保存,如果没有权限会自动弹出授权框
  173. this.doSaveImage()
  174. }
  175. },
  176. fail: () => {
  177. // 直接尝试保存
  178. this.doSaveImage()
  179. }
  180. })
  181. },
  182. // 执行保存图片
  183. doSaveImage() {
  184. uni.saveImageToPhotosAlbum({
  185. filePath: this.qrcodeLocalPath,
  186. success: () => {
  187. uni.showToast({
  188. title: '保存成功',
  189. icon: 'success'
  190. })
  191. },
  192. fail: (err) => {
  193. console.log('保存失败', err)
  194. uni.showToast({
  195. title: '保存失败',
  196. icon: 'none'
  197. })
  198. }
  199. })
  200. },
  201. // 图片加载成功
  202. onImageLoad() {
  203. console.log('二维码图片加载成功')
  204. },
  205. // 图片加载失败
  206. onImageError() {
  207. console.log('二维码图片加载失败,使用默认图片')
  208. this.qrcodeUrl = '/static/qrcode.png'
  209. this.qrcodeLocalPath = ''
  210. }
  211. }
  212. }
  213. </script>
  214. <style lang="scss" scoped>
  215. .promo-modal-page {
  216. min-height: 100vh;
  217. background: #f8f8f8;
  218. // padding-bottom: calc(140rpx + env(safe-area-inset-bottom));
  219. overflow: hidden;
  220. }
  221. .nav-bar {
  222. display: flex;
  223. align-items: center;
  224. height: calc(150rpx + var(--status-bar-height));
  225. padding: 0 32rpx;
  226. padding-top: var(--status-bar-height);
  227. background: #fff;
  228. position: fixed;
  229. top: 0;
  230. left: 0;
  231. right: 0;
  232. z-index: 999;
  233. box-sizing: border-box;
  234. .back {
  235. padding: 20rpx;
  236. margin-left: -20rpx;
  237. }
  238. .title {
  239. flex: 1;
  240. text-align: center;
  241. font-size: 34rpx;
  242. font-weight: 500;
  243. color: #222;
  244. }
  245. }
  246. .content {
  247. // padding: 30rpx 0 0 0;
  248. padding: 20rpx;
  249. // margin-top: calc(150rpx + var(--status-bar-height) + 80rpx);
  250. height: 100%;
  251. display: flex;
  252. flex-direction: column;
  253. align-items: center;
  254. overflow: hidden;
  255. box-sizing: border-box;
  256. .user-info-modal {
  257. display: flex;
  258. flex-direction: column;
  259. align-items: center;
  260. margin-bottom: 48rpx;
  261. .avatar-frame {
  262. width: 104rpx;
  263. height: 104rpx;
  264. border-radius: 10rpx;
  265. overflow: hidden;
  266. background: #f2f2f2;
  267. box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.10);
  268. .avatar-img {
  269. width: 104rpx;
  270. height: 104rpx;
  271. object-fit: cover;
  272. display: block;
  273. }
  274. }
  275. .nickname {
  276. margin-top: 24rpx;
  277. font-size: 32rpx;
  278. font-weight: bold;
  279. color: #222;
  280. text-align: center;
  281. }
  282. }
  283. .qrcode-modal-section {
  284. width: 100%;
  285. display: flex;
  286. flex-direction: column;
  287. align-items: center;
  288. margin-bottom: 48rpx;
  289. .qrcode-img {
  290. width: 100%;
  291. height: 100%;
  292. background: #fff;
  293. border-radius: 24rpx;
  294. box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.08);
  295. }
  296. .invite-code {
  297. margin-top: 32rpx;
  298. font-size: 30rpx;
  299. color: #222;
  300. font-weight: bold;
  301. text-align: center;
  302. }
  303. }
  304. .bottom-btns-modal {
  305. position: fixed;
  306. left: 0;
  307. right: 0;
  308. bottom: 0;
  309. display: flex;
  310. justify-content: space-between;
  311. padding: 24rpx 32rpx calc(env(safe-area-inset-bottom) + 24rpx) 32rpx;
  312. background: #fff;
  313. z-index: 100;
  314. .btn {
  315. flex: 1;
  316. height: 88rpx;
  317. border-radius: 44rpx;
  318. font-size: 32rpx;
  319. font-weight: bold;
  320. margin: 0 12rpx;
  321. border: none;
  322. display: flex;
  323. align-items: center;
  324. justify-content: center;
  325. &.gray {
  326. background: linear-gradient(90deg, #b2f08d, #39e9d2);
  327. color: #fff;
  328. }
  329. &.green {
  330. background: linear-gradient(90deg, #b2f08d, #39e9d2);
  331. color: #fff;
  332. }
  333. }
  334. }
  335. }
  336. </style>