card.vue 16 KB

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