share.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. <template>
  2. <view class="container">
  3. <view class="header">
  4. <text class="title">邀请分享</text>
  5. <text class="subtitle">邀请家长或新成长规划师加入</text>
  6. </view>
  7. <!-- 邀请码列表 -->
  8. <view class="invite-codes-section">
  9. <view class="section-title">我的邀请码</view>
  10. <view class="invite-codes-list">
  11. <view
  12. class="invite-code-item"
  13. v-for="code in inviteCodes"
  14. :key="code.id"
  15. >
  16. <view class="code-info">
  17. <text class="code-text">{{ code.code }}</text>
  18. <text class="code-status" :class="'status-' + code.status">
  19. {{ getStatusText(code.status) }}
  20. </text>
  21. </view>
  22. <view class="code-stats">
  23. <text class="stat">已使用: {{ code.useCount }}/{{ code.maxUseCount }}</text>
  24. <text class="stat" v-if="code.expireAt">过期: {{ formatDate(code.expireAt) }}</text>
  25. </view>
  26. </view>
  27. </view>
  28. <button class="btn-generate" @click="generateInviteCode">生成新邀请码</button>
  29. </view>
  30. <!-- 分享方式 -->
  31. <view class="share-section">
  32. <view class="section-title">分享方式</view>
  33. <!-- 复制链接 -->
  34. <view class="share-item" @click="copyInviteLink">
  35. <view class="share-icon">🔗</view>
  36. <view class="share-content">
  37. <text class="share-title">复制邀请链接</text>
  38. <text class="share-desc">分享链接给家长或新成长规划师</text>
  39. </view>
  40. <text class="share-arrow">›</text>
  41. </view>
  42. <!-- 生成二维码 -->
  43. <view class="share-item" @click="showQrcode">
  44. <view class="share-icon">📱</view>
  45. <view class="share-content">
  46. <text class="share-title">生成二维码</text>
  47. <text class="share-desc">扫码即可加入</text>
  48. </view>
  49. <text class="share-arrow">›</text>
  50. </view>
  51. <!-- 转发给朋友 -->
  52. <view class="share-item" @click="shareToFriend">
  53. <view class="share-icon">📤</view>
  54. <view class="share-content">
  55. <text class="share-title">转发给朋友</text>
  56. <text class="share-desc">直接分享到微信</text>
  57. </view>
  58. <text class="share-arrow">›</text>
  59. </view>
  60. </view>
  61. <!-- 使用说明 -->
  62. <view class="tips-section">
  63. <view class="section-title">使用说明</view>
  64. <view class="tips-list">
  65. <view class="tip-item">
  66. <text class="tip-icon">💡</text>
  67. <text class="tip-text">家长通过邀请码注册后,会自动关联到您</text>
  68. </view>
  69. <view class="tip-item">
  70. <text class="tip-icon">💡</text>
  71. <text class="tip-text">新成长规划师通过邀请码注册,会成为您的下级</text>
  72. </view>
  73. <view class="tip-item">
  74. <text class="tip-icon">💡</text>
  75. <text class="tip-text">您可以查看所有下级成长规划师的测评报告</text>
  76. </view>
  77. <view class="item">
  78. <text class="tip-icon">💡</text>
  79. <text class="tip-text">邀请码可以设置使用次数和过期时间</text>
  80. </view>
  81. </view>
  82. </view>
  83. <!-- 二维码弹窗 -->
  84. <view class="qrcode-modal" v-if="showQrcodeModal" @click="showQrcodeModal = false">
  85. <view class="modal-content" @click.stop>
  86. <view class="modal-header">
  87. <text class="modal-title">邀请二维码</text>
  88. <text class="modal-close" @click="showQrcodeModal = false">✕</text>
  89. </view>
  90. <view class="qrcode-container">
  91. <canvas canvas-id="qrcodeCanvas" class="qrcode-canvas"></canvas>
  92. </view>
  93. <view class="modal-actions">
  94. <button class="btn-save" @click="saveQrcode">保存图片</button>
  95. <button class="btn-close" @click="showQrcodeModal = false">关闭</button>
  96. </view>
  97. </view>
  98. </view>
  99. </view>
  100. </template>
  101. <script>
  102. import { getInviteCodes, generateInviteCode, generateQrCodeFromText } from '../../../utils/api.js'
  103. import { parseDate } from '../../../utils/format.js'
  104. export default {
  105. data() {
  106. return {
  107. inviteCodes: [],
  108. currentInviteCode: null,
  109. showQrcodeModal: false
  110. }
  111. },
  112. onLoad() {
  113. this.loadInviteCodes()
  114. this._onLoadFired = true
  115. },
  116. onShow() {
  117. if (this._onLoadFired) {
  118. this._onLoadFired = false
  119. } else {
  120. this.loadInviteCodes()
  121. }
  122. },
  123. methods: {
  124. async loadInviteCodes() {
  125. try {
  126. const res = await getInviteCodes()
  127. if (res.code === 200) {
  128. this.inviteCodes = res.data || []
  129. // 如果有可用的邀请码,使用第一个
  130. const activeCode = this.inviteCodes.find(code => code.status === 'active')
  131. if (activeCode) {
  132. this.currentInviteCode = activeCode
  133. }
  134. }
  135. } catch (e) {
  136. console.error('获取邀请码失败', e)
  137. }
  138. },
  139. async generateInviteCode() {
  140. try {
  141. uni.showLoading({ title: '生成中...' })
  142. const res = await generateInviteCode()
  143. uni.hideLoading()
  144. if (res.code === 200) {
  145. this.currentInviteCode = res.data
  146. uni.showToast({
  147. title: '生成成功',
  148. icon: 'success'
  149. })
  150. this.loadInviteCodes()
  151. } else {
  152. uni.showToast({
  153. title: res.message || '生成失败',
  154. icon: 'none'
  155. })
  156. }
  157. } catch (e) {
  158. uni.hideLoading()
  159. uni.showToast({
  160. title: '生成失败',
  161. icon: 'none'
  162. })
  163. }
  164. },
  165. copyInviteLink() {
  166. if (!this.currentInviteCode) {
  167. uni.showToast({
  168. title: '请先生成邀请码',
  169. icon: 'none'
  170. })
  171. return
  172. }
  173. const inviteUrl = `pages/guide/register/index?inviteCode=${this.currentInviteCode.code}`
  174. uni.setClipboardData({
  175. data: inviteUrl,
  176. success: () => {
  177. uni.showToast({
  178. title: '链接已复制',
  179. icon: 'success'
  180. })
  181. }
  182. })
  183. },
  184. showQrcode() {
  185. if (!this.currentInviteCode) {
  186. uni.showToast({
  187. title: '请先生成邀请码',
  188. icon: 'none'
  189. })
  190. return
  191. }
  192. this.showQrcodeModal = true
  193. this.$nextTick(() => {
  194. this.drawQrcode()
  195. })
  196. },
  197. async drawQrcode() {
  198. if (!this.currentInviteCode) return
  199. const code = this.currentInviteCode.code
  200. const pagePath = '/pages/guide/register/index?inviteCode=' + code
  201. // 通过后端生成真实二维码
  202. let qrBase64 = ''
  203. try {
  204. const res = await generateQrCodeFromText(pagePath)
  205. if (res.code === 200 && res.data) {
  206. qrBase64 = res.data.qrCodeBase64
  207. }
  208. } catch (e) {
  209. console.error('生成二维码失败', e)
  210. }
  211. const ctx = uni.createCanvasContext('qrcodeCanvas')
  212. const canvasWidth = 300
  213. const canvasHeight = 300
  214. ctx.setFillStyle('#FFFFFF')
  215. ctx.fillRect(0, 0, canvasWidth, canvasHeight)
  216. if (qrBase64) {
  217. // 将 base64 写入临时文件,再绘制到 Canvas
  218. const rawBase64 = qrBase64.indexOf('base64,') >= 0 ? qrBase64.split('base64,')[1] : qrBase64
  219. const fsm = uni.getFileSystemManager()
  220. const tempPath = wx.env.USER_DATA_PATH + '/qr_invite_' + Date.now() + '.png'
  221. try {
  222. fsm.writeFileSync(tempPath, rawBase64, 'base64')
  223. ctx.drawImage(tempPath, 40, 30, 220, 220)
  224. } catch (e) {
  225. this._drawQrFallback(ctx, canvasWidth, canvasHeight, code)
  226. }
  227. } else {
  228. this._drawQrFallback(ctx, canvasWidth, canvasHeight, code)
  229. }
  230. ctx.draw()
  231. },
  232. _drawQrFallback(ctx, w, h, code) {
  233. ctx.setFillStyle('#CCCCCC')
  234. ctx.fillRect(40, 30, 220, 220)
  235. ctx.setFillStyle('#999999')
  236. ctx.font = '16px sans-serif'
  237. ctx.textAlign = 'center'
  238. ctx.fillText('邀请码: ' + code, w / 2, 150)
  239. },
  240. saveQrcode() {
  241. uni.canvasToTempFilePath({
  242. canvasId: 'qrcodeCanvas',
  243. success: (res) => {
  244. uni.saveImageToPhotosAlbum({
  245. filePath: res.tempFilePath,
  246. success: () => {
  247. uni.showToast({
  248. title: '保存成功',
  249. icon: 'success'
  250. })
  251. },
  252. fail: () => {
  253. uni.showToast({
  254. title: '保存失败',
  255. icon: 'none'
  256. })
  257. }
  258. })
  259. }
  260. })
  261. },
  262. shareToFriend() {
  263. if (!this.currentInviteCode) {
  264. uni.showToast({
  265. title: '请先生成邀请码',
  266. icon: 'none'
  267. })
  268. return
  269. }
  270. const inviteUrl = `pages/guide/register/index?inviteCode=${this.currentInviteCode.code}`
  271. uni.share({
  272. provider: 'weixin',
  273. scene: {
  274. type: 0,
  275. title: '成为成长规划师',
  276. path: inviteUrl
  277. },
  278. success: () => {
  279. uni.showToast({
  280. title: '分享成功',
  281. icon: 'success'
  282. })
  283. },
  284. fail: () => {
  285. uni.showToast({
  286. title: '分享失败',
  287. icon: 'none'
  288. })
  289. }
  290. })
  291. },
  292. getStatusText(status) {
  293. const statusMap = {
  294. 'active': '可用',
  295. 'used': '已用完',
  296. 'expired': '已过期'
  297. }
  298. return statusMap[status] || status
  299. },
  300. formatDate(dateStr) {
  301. if (!dateStr) return ''
  302. const date = parseDate(dateStr)
  303. if (!date) return ''
  304. return `${date.getMonth() + 1}月${date.getDate()}日`
  305. }
  306. }
  307. }
  308. </script>
  309. <style scoped>
  310. .container {
  311. padding: 30rpx;
  312. background: #F8F8F8;
  313. min-height: 100vh;
  314. }
  315. .header {
  316. text-align: center;
  317. padding: 60rpx 0 40rpx;
  318. }
  319. .title {
  320. font-size: 48rpx;
  321. font-weight: bold;
  322. color: #333;
  323. display: block;
  324. margin-bottom: 20rpx;
  325. }
  326. .subtitle {
  327. font-size: 28rpx;
  328. color: #666;
  329. }
  330. .section-title {
  331. font-size: 32rpx;
  332. font-weight: bold;
  333. color: #333;
  334. margin-bottom: 20rpx;
  335. }
  336. .invite-codes-section,
  337. .share-section,
  338. .tips-section {
  339. background: #fff;
  340. border-radius: 20rpx;
  341. padding: 30rpx;
  342. margin-bottom: 30rpx;
  343. }
  344. .invite-codes-list {
  345. margin-bottom: 20rpx;
  346. }
  347. .invite-code-item {
  348. background: #F8F8F8;
  349. border-radius: 10rpx;
  350. padding: 20rpx;
  351. margin-bottom: 20rpx;
  352. }
  353. .code-info {
  354. display: flex;
  355. justify-content: space-between;
  356. align-items: center;
  357. margin-bottom: 10rpx;
  358. }
  359. .code-text {
  360. font-size: 32rpx;
  361. font-weight: bold;
  362. color: #333;
  363. font-family: monospace;
  364. }
  365. .code-status {
  366. font-size: 24rpx;
  367. padding: 4rpx 12rpx;
  368. border-radius: 20rpx;
  369. }
  370. .status-active {
  371. background: #E8F5E9;
  372. color: #4CAF50;
  373. }
  374. .status-used {
  375. background: #FFF3E0;
  376. color: #FF9800;
  377. }
  378. .status-expired {
  379. background: #FFEBEE;
  380. color: #F44336;
  381. }
  382. .code-stats {
  383. display: flex;
  384. justify-content: space-between;
  385. font-size: 24rpx;
  386. color: #999;
  387. }
  388. .stat {
  389. margin-right: 20rpx;
  390. }
  391. .btn-generate {
  392. width: 100%;
  393. height: 80rpx;
  394. background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  395. color: #fff;
  396. border-radius: 40rpx;
  397. font-size: 28rpx;
  398. font-weight: bold;
  399. }
  400. .share-item {
  401. display: flex;
  402. align-items: center;
  403. padding: 30rpx 0;
  404. border-bottom: 1rpx solid #F0F0F0;
  405. }
  406. .share-item:last-child {
  407. border-bottom: none;
  408. }
  409. .share-icon {
  410. font-size: 48rpx;
  411. margin-right: 20rpx;
  412. }
  413. .share-content {
  414. flex: 1;
  415. }
  416. .share-title {
  417. font-size: 30rpx;
  418. color: #333;
  419. display: block;
  420. margin-bottom: 8rpx;
  421. }
  422. .share-desc {
  423. font-size: 24rpx;
  424. color: #999;
  425. }
  426. .share-arrow {
  427. font-size: 40rpx;
  428. color: #CCC;
  429. }
  430. .tips-list {
  431. margin-top: 20rpx;
  432. }
  433. .tip-item {
  434. display: flex;
  435. align-items: flex-start;
  436. margin-bottom: 20rpx;
  437. }
  438. .tip-icon {
  439. font-size: 32rpx;
  440. margin-right: 12rpx;
  441. }
  442. .tip-text {
  443. font-size: 26rpx;
  444. color: #666;
  445. line-height: 1.5;
  446. }
  447. .qrcode-modal {
  448. position: fixed;
  449. top: 0;
  450. left: 0;
  451. right: 0;
  452. bottom: 0;
  453. background: rgba(0, 0, 0, 0.5);
  454. display: flex;
  455. align-items: center;
  456. justify-content: center;
  457. z-index: 999;
  458. }
  459. .modal-content {
  460. background: #fff;
  461. border-radius: 20rpx;
  462. padding: 40rpx;
  463. width: 80%;
  464. max-width: 400rpx;
  465. }
  466. .modal-header {
  467. display: flex;
  468. justify-content: space-between;
  469. align-items: center;
  470. margin-bottom: 30rpx;
  471. }
  472. .modal-title {
  473. font-size: 32rpx;
  474. font-weight: bold;
  475. color: #333;
  476. }
  477. .modal-close {
  478. font-size: 40rpx;
  479. color: #999;
  480. padding: 10rpx;
  481. }
  482. .qrcode-container {
  483. display: flex;
  484. justify-content: center;
  485. margin-bottom: 30rpx;
  486. }
  487. .qrcode-canvas {
  488. width: 300rpx;
  489. height: 300rpx;
  490. border: 1rpx solid #E0E0E0;
  491. border-radius: 10rpx;
  492. }
  493. .modal-actions {
  494. display: flex;
  495. gap: 20rpx;
  496. }
  497. .btn-save,
  498. .btn-close {
  499. flex: 1;
  500. height: 80rpx;
  501. border-radius: 40rpx;
  502. font-size: 28rpx;
  503. font-weight: bold;
  504. }
  505. .btn-save {
  506. background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  507. color: #fff;
  508. }
  509. .btn-close {
  510. background: #F0F0F0;
  511. color: #333;
  512. }
  513. </style>