card.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  1. <template>
  2. <view class="card-container">
  3. <!-- 顶部背景 -->
  4. <image class="top-bg" src="/static/image/home/top-bg.png" />
  5. <NavBar title="我的名片" color="#FFFFFF" :fixed="true" :bg="'transparent'"></NavBar>
  6. <!-- 名片卡片 -->
  7. <view class="page-top">
  8. <view class="user-card">
  9. <!-- 背景图 -->
  10. <image class="user-card-bg" src="/static/image/home/usecard-bg.png" />
  11. <!-- 左上:姓名 + 职位 -->
  12. <view class="user-header">
  13. <view class="name-row">
  14. <text class="user-name">{{ cardInfo.nickName || '用户' }}</text>
  15. <text class="user-role">{{ cardInfo.postName || '职位' }}</text>
  16. </view>
  17. <text class="company-name">{{ cardInfo.companyName || '公司名称' }}</text>
  18. </view>
  19. <!-- 右上:头像 -->
  20. <view class="avatar-wrapper">
  21. <image class="avatar" :src="cardInfo.avatar || '/static/image/public/avatar-default.png'" />
  22. <image class="badge-icon" src="/static/image/public/badge-icon.png" />
  23. </view>
  24. <!-- 左下:联系方式 -->
  25. <view class="user-contact">
  26. <view class="contact-row" @click="makeCall">
  27. <uni-icons type="phone" :size="18" color="#666666"></uni-icons>
  28. <text class="contact-text">{{ cardInfo.phonenumber || '暂无电话' }}</text>
  29. </view>
  30. <view class="contact-row">
  31. <uni-icons type="email" :size="18" color="#666666"></uni-icons>
  32. <text class="contact-text">{{ cardInfo.email || '暂无邮箱' }}</text>
  33. </view>
  34. <view class="contact-row">
  35. <uni-icons type="location" :size="18" color="#666666"></uni-icons>
  36. <text class="contact-text">{{ cardInfo.companyAddress || '暂无地址' }}</text>
  37. </view>
  38. </view>
  39. </view>
  40. <view class="control-card">
  41. <view class="item" @click="shareCard">
  42. <image src="/static/image/public/card-sharing.png"></image>
  43. <text>分享名片</text>
  44. </view>
  45. <view class="item" @click="saveCard">
  46. <image src="/static/image/public/card-save.png"></image>
  47. <text>保存名片</text>
  48. </view>
  49. <view class="item" @click="showQRCode">
  50. <image src="/static/image/public/card-qr.png"></image>
  51. <text>名片码</text>
  52. </view>
  53. </view>
  54. </view>
  55. <view class="card-content">
  56. <!-- Tab 切换 -->
  57. <view class="tab-section">
  58. <view class="tab-wrapper">
  59. <view class="tab-item" :class="{ active: currentTab === 'products' }"
  60. @click="switchTab('products')">
  61. <text class="tab-text">产品列表</text>
  62. <view class="tab-indicator" v-if="currentTab === 'products'"></view>
  63. </view>
  64. <view class="tab-item" :class="{ active: currentTab === 'company' }" @click="switchTab('company')">
  65. <text class="tab-text">企业简介</text>
  66. <view class="tab-indicator" v-if="currentTab === 'company'"></view>
  67. </view>
  68. </view>
  69. </view>
  70. <!-- 产品列表 -->
  71. <view class="product-list" v-if="currentTab === 'products'">
  72. <view class="product-item" v-for="(item, index) in productList" :key="index">
  73. <image class="product-image" :src="item.image" mode="aspectFill" />
  74. <view class="product-info">
  75. <text class="product-title">{{ item.title }}</text>
  76. <text class="product-desc">{{ item.description }}</text>
  77. </view>
  78. </view>
  79. </view>
  80. <!-- 企业简介 -->
  81. <view class="company-section" v-if="currentTab === 'company'">
  82. <view class="company-content">
  83. <text class="company-text">
  84. <view v-html="companyInfo.introduce" v-if="companyInfo.introduce"></view>
  85. </text>
  86. </view>
  87. </view>
  88. </view>
  89. <!-- 隐藏的 Canvas 用于生成名片快照 -->
  90. <canvas id="posterCanvas" type="2d"
  91. style="position: fixed; left: -9999px; top: -9999px; width: 750px; height: 800px;"></canvas>
  92. <!-- 隐藏的 Canvas 用于生成二维码海报 -->
  93. <view class="hidden-canvas-box">
  94. <canvas id="qrPosterCanvas" type="2d" style="width: 600px; height: 700px;"></canvas>
  95. </view>
  96. <!-- 二维码弹窗 -->
  97. <uni-popup ref="qrPopup" type="center">
  98. <view class="qr-popup">
  99. <view class="qr-header">
  100. <text class="qr-title">名片二维码</text>
  101. <text class="qr-subtitle">扫一扫查看我的名片信息</text>
  102. </view>
  103. <view class="qr-content">
  104. <image v-if="qrInfo && qrInfo.image"
  105. :src="qrInfo.image.startsWith('data:') ? qrInfo.image : 'data:image/png;base64,' + qrInfo.image"
  106. class="qr-image" mode="aspectFit" />
  107. <view v-else class="qr-loading">
  108. <text>加载中...</text>
  109. </view>
  110. </view>
  111. <view class="qr-actions">
  112. <view class="action-btn" @click="saveQRCode">
  113. <uni-icons type="download" size="24" color="#4080FF"></uni-icons>
  114. <text class="action-text">保存到相册</text>
  115. </view>
  116. <view class="action-btn" @click="shareQRCode">
  117. <uni-icons type="paperplane" size="24" color="#4080FF"></uni-icons>
  118. <button open-type="share" class="action-text">分享给好友</button>
  119. </view>
  120. </view>
  121. </view>
  122. </uni-popup>
  123. </view>
  124. </template>
  125. <script setup>
  126. import {
  127. ref,
  128. onMounted
  129. } from 'vue'
  130. import {
  131. onShareAppMessage
  132. } from '@dcloudio/uni-app';
  133. import NavBar from '@/components/nav-bar/index.vue'
  134. import {
  135. useUserStore
  136. } from '@/store/modules/user.js'
  137. import {
  138. storeToRefs
  139. } from 'pinia'
  140. import {
  141. generateCardPoster,
  142. savePosterToAlbum,
  143. } from '@/utils/poster.js'
  144. // 使用 Pinia 管理用户状态
  145. const userStore = useUserStore()
  146. const {
  147. cardInfo,
  148. companyInfo,
  149. qrInfo,
  150. } = storeToRefs(userStore)
  151. const currentTab = ref('products')
  152. // 名片快照图片路径
  153. const cardSnapshot = ref('')
  154. // 二维码海报图片路径
  155. const qrCodePoster = ref('')
  156. // 二维码弹窗引用
  157. const qrPopup = ref(null)
  158. const productList = ref([ ])
  159. onShareAppMessage(() => {
  160. return {
  161. userName: '小程序',
  162. path: 'pages/index/index',
  163. title: '布尔销销乐',
  164. imagePath: 'data:image/png;base64,' + qrInfo.value.image,
  165. };
  166. });
  167. onMounted(async () => {
  168. // 如果没有数据,重新获取
  169. if (!cardInfo.value.nickName) {
  170. await userStore.queryCardInfo()
  171. }
  172. if (!companyInfo.value.name) {
  173. await userStore.queryCompanyInfo()
  174. }
  175. })
  176. const makeCall = () => {
  177. if (cardInfo.value.phonenumber) {
  178. uni.makePhoneCall({
  179. phoneNumber: cardInfo.value.phonenumber
  180. })
  181. } else {
  182. uni.showToast({
  183. title: '暂无电话号码',
  184. icon: 'none'
  185. })
  186. }
  187. }
  188. const shareCard = () => {
  189. uni.showShareMenu({
  190. withShareTicket: true
  191. })
  192. uni.showToast({
  193. title: '分享名片',
  194. icon: 'none'
  195. })
  196. }
  197. // 生成名片快照并保存
  198. const saveCard = async () => {
  199. uni.showLoading({
  200. title: '生成快照中...',
  201. mask: true
  202. })
  203. try {
  204. // 打印 cardInfo 详细信息
  205. console.log('[saveCard] cardInfo:', JSON.stringify(cardInfo.value, null, 2))
  206. console.log('[saveCard] avatar:', cardInfo.value.avatar)
  207. // 生成名片快照
  208. const snapshotPath = await generateCardPoster(cardInfo.value)
  209. // 用属性接收快照路径
  210. cardSnapshot.value = snapshotPath
  211. console.log('名片快照路径:', cardSnapshot.value)
  212. // 保存到相册
  213. await savePosterToAlbum(snapshotPath)
  214. uni.hideLoading()
  215. uni.showToast({
  216. title: '已保存到相册',
  217. icon: 'success'
  218. })
  219. } catch (error) {
  220. console.error('保存失败:', error)
  221. uni.hideLoading()
  222. uni.showToast({
  223. title: '保存失败,请重试',
  224. icon: 'none'
  225. })
  226. }
  227. }
  228. // 显示二维码
  229. const showQRCode = async () => {
  230. // 打开弹窗
  231. qrPopup.value.open()
  232. }
  233. // 保存二维码到相册
  234. const saveQRCode = async () => {
  235. if (!qrInfo.value.image) {
  236. uni.showToast({
  237. title: '二维码图片不存在',
  238. icon: 'none'
  239. })
  240. return
  241. }
  242. try {
  243. // 将 base64 转换为临时文件
  244. const qrFilePath = await base64ToTempFile(qrInfo.value.image)
  245. // 保存到相册
  246. await savePosterToAlbum(qrFilePath)
  247. uni.showToast({
  248. title: '已保存到相册',
  249. icon: 'success'
  250. })
  251. } catch (error) {
  252. console.error('保存失败:', error)
  253. uni.showToast({
  254. title: '保存失败,请重试',
  255. icon: 'none'
  256. })
  257. }
  258. }
  259. // 分享二维码给好友
  260. const shareQRCode = async () => {
  261. if (!qrInfo.value.image) {
  262. uni.showToast({
  263. title: '二维码图片不存在',
  264. icon: 'none'
  265. })
  266. return
  267. }
  268. try {
  269. // 将 base64 转换为临时文件
  270. const qrFilePath = await base64ToTempFile(qrInfo.value.image)
  271. // #ifdef MP-WEIXIN
  272. // #endif
  273. // #ifndef MP-WEIXIN
  274. await shareImageToWeChat(qrFilePath)
  275. uni.showToast({
  276. title: '分享成功',
  277. icon: 'success'
  278. })
  279. // #endif
  280. } catch (error) {
  281. console.error('分享失败:', error)
  282. uni.showToast({
  283. title: '分享失败,请重试',
  284. icon: 'none'
  285. })
  286. }
  287. }
  288. const switchTab = (tab) => {
  289. currentTab.value = tab
  290. }
  291. // 将 base64 转换为临时文件路径
  292. const base64ToTempFile = async (base64Data) => {
  293. return new Promise((resolve, reject) => {
  294. try {
  295. // 移除 data:image/png;base64, 前缀(如果有)
  296. const pureBase64 = base64Data.replace(/^data:image\/\w+;base64,/, '')
  297. const fileName = `${Date.now()}_qrcode.png`
  298. const filePath = `${wx.env.USER_DATA_PATH}/${fileName}`
  299. const fs = uni.getFileSystemManager()
  300. fs.writeFile({
  301. filePath: filePath,
  302. data: pureBase64,
  303. encoding: 'base64',
  304. success: () => {
  305. console.log('base64 转文件成功:', filePath)
  306. resolve(filePath)
  307. },
  308. fail: (err) => {
  309. console.error('base64 转文件失败:', err)
  310. reject(err)
  311. }
  312. })
  313. } catch (error) {
  314. console.error('base64 转换异常:', error)
  315. reject(error)
  316. }
  317. })
  318. }
  319. </script>
  320. <style lang="scss" scoped>
  321. .card-container {
  322. background: #f5f6f8;
  323. }
  324. .top-bg {
  325. width: 750rpx;
  326. height: 634rpx;
  327. position: fixed;
  328. top: 0;
  329. left: 0;
  330. z-index: 1;
  331. }
  332. // 顶部背景区域
  333. .header-bg {
  334. background: linear-gradient(135deg, #4A90E2 0%, #6FB3F2 50%, #87CEEB 100%);
  335. padding: 0 40rpx;
  336. padding-top: calc(var(--status-bar-height) + 20rpx);
  337. padding-bottom: 60rpx;
  338. position: relative;
  339. overflow: hidden;
  340. // 背景光效
  341. &::before {
  342. content: '';
  343. position: absolute;
  344. top: -100rpx;
  345. right: -100rpx;
  346. width: 500rpx;
  347. height: 500rpx;
  348. background: radial-gradient(circle, rgba(255, 255, 255, 0.2) 0%, transparent 70%);
  349. border-radius: 50%;
  350. }
  351. // 导航栏
  352. .nav-bar {
  353. display: flex;
  354. align-items: center;
  355. justify-content: space-between;
  356. height: 88rpx;
  357. position: relative;
  358. z-index: 10;
  359. .nav-back {
  360. width: 60rpx;
  361. height: 60rpx;
  362. display: flex;
  363. align-items: center;
  364. justify-content: center;
  365. }
  366. .nav-title {
  367. font-size: 32rpx;
  368. font-weight: 600;
  369. color: #ffffff;
  370. text-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.1);
  371. }
  372. .nav-right {
  373. display: flex;
  374. align-items: center;
  375. .more-btn,
  376. .refresh-btn {
  377. width: 64rpx;
  378. height: 64rpx;
  379. border-radius: 50%;
  380. background: rgba(255, 255, 255, 0.15);
  381. display: flex;
  382. align-items: center;
  383. justify-content: center;
  384. margin-left: 16rpx;
  385. }
  386. }
  387. }
  388. }
  389. /* 用户信息卡片 */
  390. .page-top {
  391. margin: 0 24rpx 24rpx;
  392. .user-card {
  393. position: relative;
  394. z-index: 2;
  395. width: 100%;
  396. height: 400rpx;
  397. box-sizing: border-box;
  398. padding: 80rpx 40rpx 32rpx;
  399. position: relative;
  400. .user-card-bg {
  401. position: absolute;
  402. top: 0;
  403. left: 0;
  404. width: 100%;
  405. height: 100%;
  406. z-index: 0;
  407. }
  408. view,
  409. text {
  410. position: relative;
  411. z-index: 1;
  412. }
  413. .user-header {
  414. .name-row {
  415. display: flex;
  416. align-items: center;
  417. margin-bottom: 16rpx;
  418. .user-name {
  419. font-size: 44rpx;
  420. font-weight: 700;
  421. color: #202020;
  422. line-height: 1.2;
  423. }
  424. .user-role {
  425. margin-left: 16rpx;
  426. padding: 6rpx 16rpx;
  427. background: rgba(68, 110, 255, 0.10);
  428. border-radius: 8rpx;
  429. font-size: 24rpx;
  430. font-weight: 500;
  431. color: #446eff;
  432. }
  433. }
  434. .company-name {
  435. font-size: 28rpx;
  436. font-weight: 500;
  437. color: #666666;
  438. line-height: 1.4;
  439. }
  440. }
  441. .avatar-wrapper {
  442. position: absolute;
  443. top: 32rpx;
  444. right: 40rpx;
  445. width: 140rpx;
  446. height: 140rpx;
  447. border-radius: 50%;
  448. box-shadow: 0rpx 0rpx 4rpx 0rpx rgba(21, 93, 252, 0.20);
  449. .avatar {
  450. width: 100%;
  451. height: 100%;
  452. }
  453. .badge-icon {
  454. width: 64rpx;
  455. height: 64rpx;
  456. position: absolute;
  457. bottom: 0;
  458. right: 0;
  459. transform: translate(50% 50%);
  460. z-index: 1;
  461. }
  462. }
  463. .user-contact {
  464. margin-top: 32rpx;
  465. .contact-row {
  466. display: flex;
  467. align-items: center;
  468. margin-bottom: 16rpx;
  469. uni-icons {
  470. margin-right: 12rpx;
  471. flex-shrink: 0;
  472. }
  473. .contact-text {
  474. font-size: 26rpx;
  475. color: #666666;
  476. }
  477. }
  478. }
  479. }
  480. .control-card {
  481. background-color: #fff;
  482. border-radius: 0 0 24rpx 24rpx;
  483. position: relative;
  484. z-index: 1;
  485. bottom: 24rpx;
  486. display: flex;
  487. align-items: center;
  488. justify-content: space-around;
  489. padding-top: 48rpx;
  490. padding-bottom: 24rpx;
  491. .item {
  492. display: flex;
  493. flex-direction: column;
  494. align-items: center;
  495. justify-content: center;
  496. gap: 4rpx;
  497. image {
  498. width: 64rpx;
  499. height: 64rpx;
  500. }
  501. text {
  502. font-size: 26rpx;
  503. color: #202020;
  504. }
  505. }
  506. }
  507. }
  508. // 名片内容区域
  509. .card-content {
  510. margin: 0 24rpx;
  511. padding: 0 24rpx;
  512. border-radius: 24rpx;
  513. background: #ffffff;
  514. box-shadow: 0 -2rpx 12rpx rgba(0, 0, 0, 0.04);
  515. position: relative;
  516. bottom: 24rpx;
  517. z-index: 2;
  518. }
  519. // Tab 切换区域
  520. .tab-section {
  521. border-radius: 24rpx 24rpx 0 0;
  522. padding: 0 40rpx;
  523. .tab-wrapper {
  524. display: flex;
  525. border-bottom: 1rpx solid #f0f0f0;
  526. .tab-item {
  527. flex: 1;
  528. display: flex;
  529. flex-direction: column;
  530. align-items: center;
  531. padding: 32rpx 0;
  532. position: relative;
  533. .tab-text {
  534. font-size: 28rpx;
  535. color: #999999;
  536. font-weight: 500;
  537. }
  538. &.active .tab-text {
  539. color: #333333;
  540. font-weight: 600;
  541. }
  542. .tab-indicator {
  543. position: absolute;
  544. bottom: 0;
  545. width: 60rpx;
  546. height: 4rpx;
  547. background: linear-gradient(90deg, #4A90E2 0%, #6FB3F2 100%);
  548. border-radius: 2rpx;
  549. }
  550. }
  551. }
  552. }
  553. // 产品列表
  554. .product-list {
  555. padding: 24rpx 40rpx;
  556. .product-item {
  557. display: flex;
  558. padding: 24rpx 0;
  559. border-bottom: 1rpx solid #f0f0f0;
  560. &:last-child {
  561. border-bottom: none;
  562. }
  563. .product-image {
  564. width: 160rpx;
  565. height: 160rpx;
  566. border-radius: 12rpx;
  567. background: #f5f5f5;
  568. flex-shrink: 0;
  569. margin-right: 24rpx;
  570. }
  571. .product-info {
  572. flex: 1;
  573. display: flex;
  574. flex-direction: column;
  575. justify-content: center;
  576. .product-title {
  577. font-size: 28rpx;
  578. color: #333333;
  579. margin-bottom: 12rpx;
  580. line-height: 1.5;
  581. }
  582. .product-desc {
  583. font-size: 24rpx;
  584. color: #999999;
  585. }
  586. }
  587. }
  588. }
  589. // 企业简介
  590. .company-section {
  591. background: #ffffff;
  592. padding: 40rpx;
  593. .company-content {
  594. .company-text {
  595. display: block;
  596. font-size: 28rpx;
  597. color: #666666;
  598. line-height: 1.8;
  599. margin-bottom: 24rpx;
  600. ::v-deep img {
  601. max-width: 100% !important;
  602. }
  603. &:last-child {
  604. margin-bottom: 0;
  605. }
  606. }
  607. }
  608. }
  609. // 隐藏的 canvas 容器
  610. .hidden-canvas-box {
  611. position: fixed;
  612. left: -9999px;
  613. top: -9999px;
  614. width: 1px;
  615. height: 1px;
  616. overflow: hidden;
  617. }
  618. // 二维码弹窗样式
  619. .qr-popup {
  620. width: 600rpx;
  621. background: #ffffff;
  622. border-radius: 24rpx;
  623. padding: 40rpx;
  624. box-shadow: 0 8rpx 32rpx rgba(0, 0, 0, 0.15);
  625. .qr-header {
  626. text-align: center;
  627. margin-bottom: 40rpx;
  628. .qr-title {
  629. display: block;
  630. font-size: 36rpx;
  631. font-weight: 600;
  632. color: #202020;
  633. margin-bottom: 12rpx;
  634. }
  635. .qr-subtitle {
  636. display: block;
  637. font-size: 26rpx;
  638. color: #999999;
  639. }
  640. }
  641. .qr-content {
  642. display: flex;
  643. justify-content: center;
  644. align-items: center;
  645. min-height: 400rpx;
  646. margin-bottom: 40rpx;
  647. .qr-image {
  648. width: 400rpx;
  649. height: 400rpx;
  650. border-radius: 16rpx;
  651. box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.1);
  652. }
  653. .qr-loading {
  654. display: flex;
  655. justify-content: center;
  656. align-items: center;
  657. }
  658. }
  659. .qr-actions {
  660. display: flex;
  661. justify-content: space-around;
  662. .action-btn {
  663. display: flex;
  664. flex-direction: column;
  665. align-items: center;
  666. gap: 12rpx;
  667. padding: 20rpx 40rpx;
  668. border-radius: 16rpx;
  669. background: rgba(64, 128, 255, 0.08);
  670. .action-text {
  671. &::after{
  672. border: none;
  673. }
  674. line-height: 1;
  675. background-color: transparent;
  676. font-size: 26rpx;
  677. color: #4080FF;
  678. font-weight: 500;
  679. }
  680. }
  681. }
  682. }
  683. </style>