card.vue 16 KB

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