card.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  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" @click="shareUserCard">
  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" @click="handleShare">
  116. <uni-icons type="paperplane" size="24" color="#4080FF"></uni-icons>
  117. <!-- <button open-type="share" class="action-text">分享给好友</button> -->
  118. <text class="action-text">分享给好友</text>
  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 cardPopup = ref(null)
  159. const productList = ref([])
  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. // 生成名片快照
  195. const snapshotPath = await generateCardPoster(cardInfo.value)
  196. console.log(snapshotPath, "snapshotPathsnapshotPathsnapshotPath");
  197. // 用属性接收快照路径
  198. cardSnapshot.value = snapshotPath
  199. console.log('名片快照路径:', cardSnapshot.value)
  200. // 保存到相册
  201. await savePosterToAlbum(snapshotPath)
  202. uni.hideLoading()
  203. uni.showToast({
  204. title: '已保存到相册',
  205. icon: 'success'
  206. })
  207. } catch (error) {
  208. console.error('保存失败:', error)
  209. uni.hideLoading()
  210. uni.showToast({
  211. title: '保存失败,请重试',
  212. icon: 'none'
  213. })
  214. }
  215. }
  216. // 显示二维码
  217. const showQRCode = async () => {
  218. // 打开弹窗
  219. qrPopup.value.open()
  220. }
  221. const close = async () => {
  222. cardPopup.value.close()
  223. }
  224. const shareUserCard = async () => {
  225. const snapshotPath = await generateCardPoster(cardInfo.value)
  226. wx.showShareImageMenu({
  227. path: snapshotPath,
  228. success: () => {
  229. console.log('分享菜单已调起');
  230. },
  231. fail: (err) => {
  232. console.error('调起分享菜单失败', err);
  233. }
  234. });
  235. }
  236. const base64ToImage = async (base64Data) => {
  237. return new Promise((resolve, reject) => {
  238. try {
  239. const filePath = `${wx.env.USER_DATA_PATH}/temp_image.png`;
  240. let pureBase64Data = base64Data
  241. if (pureBase64Data.startsWith('data:image')) {
  242. pureBase64Data = pureBase64Data.replace(/^data:image\/\w+;base64,/, '');
  243. }
  244. const fs = wx.getFileSystemManager();
  245. fs.writeFile({
  246. filePath: filePath,
  247. data: pureBase64Data,
  248. encoding: 'base64',
  249. success: (res) => {
  250. console.log("resresweresresresres", res);
  251. resolve(filePath)
  252. },
  253. fail: (err) => {
  254. console.error('写入临时文件失败', err);
  255. }
  256. });
  257. } catch (error) {
  258. console.error('base64 转换异常:', error)
  259. reject(error)
  260. }
  261. })
  262. }
  263. const handleShare = async () => {
  264. const qrFilePath = await base64ToTempFile(qrInfo.value.image)
  265. wx.showShareImageMenu({
  266. path: qrFilePath,
  267. success: () => {
  268. console.log('分享菜单已调起');
  269. },
  270. fail: (err) => {
  271. console.error('调起分享菜单失败', err);
  272. }
  273. });
  274. }
  275. // 保存二维码到相册
  276. const saveQRCode = async () => {
  277. if (!qrInfo.value.image) {
  278. uni.showToast({
  279. title: '二维码图片不存在',
  280. icon: 'none'
  281. })
  282. return
  283. }
  284. try {
  285. // 将 base64 转换为临时文件
  286. const qrFilePath = await base64ToTempFile(qrInfo.value.image)
  287. // 保存到相册
  288. await savePosterToAlbum(qrFilePath)
  289. uni.showToast({
  290. title: '已保存到相册',
  291. icon: 'success'
  292. })
  293. } catch (error) {
  294. console.error('保存失败:', error)
  295. uni.showToast({
  296. title: '保存失败,请重试',
  297. icon: 'none'
  298. })
  299. }
  300. }
  301. const switchTab = (tab) => {
  302. currentTab.value = tab
  303. }
  304. // 将 base64 转换为临时文件路径
  305. const base64ToTempFile = async (base64Data) => {
  306. return new Promise((resolve, reject) => {
  307. try {
  308. // 移除 data:image/png;base64, 前缀(如果有)
  309. const pureBase64 = base64Data.replace(/^data:image\/\w+;base64,/, '')
  310. const fileName = `${Date.now()}_qrcode.png`
  311. const filePath = `${wx.env.USER_DATA_PATH}/${fileName}`
  312. const fs = uni.getFileSystemManager()
  313. fs.writeFile({
  314. filePath: filePath,
  315. data: pureBase64,
  316. encoding: 'base64',
  317. success: () => {
  318. console.log('base64 转文件成功:', filePath)
  319. resolve(filePath)
  320. },
  321. fail: (err) => {
  322. console.error('base64 转文件失败:', err)
  323. reject(err)
  324. }
  325. })
  326. } catch (error) {
  327. console.error('base64 转换异常:', error)
  328. reject(error)
  329. }
  330. })
  331. }
  332. </script>
  333. <style lang="scss" scoped>
  334. .card-container {
  335. background: #f5f6f8;
  336. }
  337. .top-bg {
  338. width: 750rpx;
  339. height: 634rpx;
  340. position: fixed;
  341. top: 0;
  342. left: 0;
  343. z-index: 1;
  344. }
  345. // 顶部背景区域
  346. .header-bg {
  347. background: linear-gradient(135deg, #4A90E2 0%, #6FB3F2 50%, #87CEEB 100%);
  348. padding: 0 40rpx;
  349. padding-top: calc(var(--status-bar-height) + 20rpx);
  350. padding-bottom: 60rpx;
  351. position: relative;
  352. overflow: hidden;
  353. // 背景光效
  354. &::before {
  355. content: '';
  356. position: absolute;
  357. top: -100rpx;
  358. right: -100rpx;
  359. width: 500rpx;
  360. height: 500rpx;
  361. background: radial-gradient(circle, rgba(255, 255, 255, 0.2) 0%, transparent 70%);
  362. border-radius: 50%;
  363. }
  364. // 导航栏
  365. .nav-bar {
  366. display: flex;
  367. align-items: center;
  368. justify-content: space-between;
  369. height: 88rpx;
  370. position: relative;
  371. z-index: 10;
  372. .nav-back {
  373. width: 60rpx;
  374. height: 60rpx;
  375. display: flex;
  376. align-items: center;
  377. justify-content: center;
  378. }
  379. .nav-title {
  380. font-size: 32rpx;
  381. font-weight: 600;
  382. color: #ffffff;
  383. text-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.1);
  384. }
  385. .nav-right {
  386. display: flex;
  387. align-items: center;
  388. .more-btn,
  389. .refresh-btn {
  390. width: 64rpx;
  391. height: 64rpx;
  392. border-radius: 50%;
  393. background: rgba(255, 255, 255, 0.15);
  394. display: flex;
  395. align-items: center;
  396. justify-content: center;
  397. margin-left: 16rpx;
  398. }
  399. }
  400. }
  401. }
  402. .user-card {
  403. position: relative;
  404. z-index: 2;
  405. width: 100%;
  406. height: 400rpx;
  407. box-sizing: border-box;
  408. padding: 80rpx 40rpx 32rpx;
  409. position: relative;
  410. .user-card-bg {
  411. position: absolute;
  412. top: 0;
  413. left: 0;
  414. width: 100%;
  415. height: 100%;
  416. z-index: 0;
  417. }
  418. view,
  419. text {
  420. position: relative;
  421. z-index: 1;
  422. }
  423. .user-header {
  424. .name-row {
  425. display: flex;
  426. align-items: center;
  427. margin-bottom: 16rpx;
  428. .user-name {
  429. font-size: 44rpx;
  430. font-weight: 700;
  431. color: #202020;
  432. line-height: 1.2;
  433. }
  434. .user-role {
  435. margin-left: 16rpx;
  436. padding: 6rpx 16rpx;
  437. background: rgba(68, 110, 255, 0.10);
  438. border-radius: 8rpx;
  439. font-size: 24rpx;
  440. font-weight: 500;
  441. color: #446eff;
  442. }
  443. }
  444. .company-name {
  445. font-size: 28rpx;
  446. font-weight: 500;
  447. color: #666666;
  448. line-height: 1.4;
  449. }
  450. }
  451. .avatar-wrapper {
  452. position: absolute;
  453. top: 32rpx;
  454. right: 40rpx;
  455. width: 140rpx;
  456. height: 140rpx;
  457. border-radius: 50%;
  458. box-shadow: 0rpx 0rpx 4rpx 0rpx rgba(21, 93, 252, 0.20);
  459. .avatar {
  460. border-radius: 50%;
  461. width: 100%;
  462. height: 100%;
  463. }
  464. .badge-icon {
  465. width: 64rpx;
  466. height: 64rpx;
  467. position: absolute;
  468. bottom: 0;
  469. right: 0;
  470. transform: translate(50% 50%);
  471. z-index: 1;
  472. }
  473. }
  474. .user-contact {
  475. margin-top: 32rpx;
  476. .contact-row {
  477. display: flex;
  478. align-items: center;
  479. margin-bottom: 16rpx;
  480. uni-icons {
  481. margin-right: 12rpx;
  482. flex-shrink: 0;
  483. }
  484. .contact-text {
  485. font-size: 26rpx;
  486. color: #666666;
  487. }
  488. }
  489. }
  490. }
  491. /* 用户信息卡片 */
  492. .page-top {
  493. margin: 0 24rpx 24rpx;
  494. .control-card {
  495. background-color: #fff;
  496. border-radius: 0 0 24rpx 24rpx;
  497. position: relative;
  498. z-index: 1;
  499. bottom: 24rpx;
  500. display: flex;
  501. align-items: center;
  502. justify-content: space-around;
  503. padding-top: 48rpx;
  504. padding-bottom: 24rpx;
  505. .item {
  506. display: flex;
  507. flex-direction: column;
  508. align-items: center;
  509. justify-content: center;
  510. gap: 4rpx;
  511. image {
  512. width: 64rpx;
  513. height: 64rpx;
  514. }
  515. .text {
  516. font-size: 26rpx !important;
  517. color: #202020;
  518. }
  519. }
  520. }
  521. }
  522. // 名片内容区域
  523. .card-content {
  524. margin: 0 24rpx;
  525. padding: 0 24rpx;
  526. border-radius: 24rpx;
  527. background: #ffffff;
  528. box-shadow: 0 -2rpx 12rpx rgba(0, 0, 0, 0.04);
  529. position: relative;
  530. bottom: 24rpx;
  531. z-index: 2;
  532. }
  533. // Tab 切换区域
  534. .tab-section {
  535. border-radius: 24rpx 24rpx 0 0;
  536. padding: 0 40rpx;
  537. .tab-wrapper {
  538. display: flex;
  539. border-bottom: 1rpx solid #f0f0f0;
  540. .tab-item {
  541. flex: 1;
  542. display: flex;
  543. flex-direction: column;
  544. align-items: center;
  545. padding: 32rpx 0;
  546. position: relative;
  547. .tab-text {
  548. font-size: 28rpx;
  549. color: #999999;
  550. font-weight: 500;
  551. }
  552. &.active .tab-text {
  553. color: #333333;
  554. font-weight: 600;
  555. }
  556. .tab-indicator {
  557. position: absolute;
  558. bottom: 0;
  559. width: 60rpx;
  560. height: 4rpx;
  561. background: linear-gradient(90deg, #4A90E2 0%, #6FB3F2 100%);
  562. border-radius: 2rpx;
  563. }
  564. }
  565. }
  566. }
  567. // 产品列表
  568. .product-list {
  569. padding: 24rpx 40rpx;
  570. .product-item {
  571. display: flex;
  572. padding: 24rpx 0;
  573. border-bottom: 1rpx solid #f0f0f0;
  574. &:last-child {
  575. border-bottom: none;
  576. }
  577. .product-image {
  578. width: 160rpx;
  579. height: 160rpx;
  580. border-radius: 12rpx;
  581. background: #f5f5f5;
  582. flex-shrink: 0;
  583. margin-right: 24rpx;
  584. }
  585. .product-info {
  586. flex: 1;
  587. display: flex;
  588. flex-direction: column;
  589. justify-content: center;
  590. .product-title {
  591. font-size: 28rpx;
  592. color: #333333;
  593. margin-bottom: 12rpx;
  594. line-height: 1.5;
  595. }
  596. .product-desc {
  597. font-size: 24rpx;
  598. color: #999999;
  599. }
  600. }
  601. }
  602. }
  603. // 企业简介
  604. .company-section {
  605. background: #ffffff;
  606. padding: 40rpx;
  607. .company-content {
  608. .company-text {
  609. display: block;
  610. font-size: 28rpx;
  611. color: #666666;
  612. line-height: 1.8;
  613. margin-bottom: 24rpx;
  614. ::v-deep img {
  615. max-width: 100% !important;
  616. }
  617. &:last-child {
  618. margin-bottom: 0;
  619. }
  620. }
  621. }
  622. }
  623. // 隐藏的 canvas 容器
  624. .hidden-canvas-box {
  625. position: fixed;
  626. left: -9999px;
  627. top: -9999px;
  628. width: 1px;
  629. height: 1px;
  630. overflow: hidden;
  631. }
  632. // 二维码弹窗样式
  633. .qr-popup {
  634. margin: 100rpx auto;
  635. width: 600rpx;
  636. background: #ffffff;
  637. border-radius: 24rpx;
  638. padding: 40rpx;
  639. box-shadow: 0 8rpx 32rpx rgba(0, 0, 0, 0.15);
  640. .qr-header {
  641. text-align: center;
  642. margin-bottom: 40rpx;
  643. .qr-title {
  644. display: block;
  645. font-size: 36rpx;
  646. font-weight: 600;
  647. color: #202020;
  648. margin-bottom: 12rpx;
  649. }
  650. .qr-subtitle {
  651. display: block;
  652. font-size: 26rpx;
  653. color: #999999;
  654. }
  655. }
  656. .qr-content {
  657. display: flex;
  658. justify-content: center;
  659. align-items: center;
  660. min-height: 400rpx;
  661. margin-bottom: 40rpx;
  662. .qr-image {
  663. width: 400rpx;
  664. height: 400rpx;
  665. border-radius: 16rpx;
  666. box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.1);
  667. }
  668. .qr-loading {
  669. display: flex;
  670. justify-content: center;
  671. align-items: center;
  672. }
  673. }
  674. .qr-actions {
  675. display: flex;
  676. justify-content: space-around;
  677. .action-btn {
  678. display: flex;
  679. flex-direction: column;
  680. align-items: center;
  681. gap: 12rpx;
  682. padding: 20rpx 40rpx;
  683. border-radius: 16rpx;
  684. background: rgba(64, 128, 255, 0.08);
  685. .action-text {
  686. &::after {
  687. border: none;
  688. }
  689. line-height: 1;
  690. background-color: transparent;
  691. font-size: 26rpx;
  692. color: #4080FF;
  693. font-weight: 500;
  694. }
  695. }
  696. }
  697. }
  698. .resetButton {
  699. background-color: transparent !important;
  700. border: none !important;
  701. display: inline !important;
  702. &::after {
  703. border: none !important;
  704. }
  705. }
  706. </style>