card.vue 17 KB

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