share.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  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. },
  115. onShow() {
  116. this.loadInviteCodes()
  117. },
  118. methods: {
  119. async loadInviteCodes() {
  120. try {
  121. const res = await getInviteCodes()
  122. if (res.code === 200) {
  123. this.inviteCodes = res.data || []
  124. // 如果有可用的邀请码,使用第一个
  125. const activeCode = this.inviteCodes.find(code => code.status === 'active')
  126. if (activeCode) {
  127. this.currentInviteCode = activeCode
  128. }
  129. }
  130. } catch (e) {
  131. console.error('获取邀请码失败', e)
  132. }
  133. },
  134. async generateInviteCode() {
  135. try {
  136. uni.showLoading({ title: '生成中...' })
  137. const res = await generateInviteCode()
  138. uni.hideLoading()
  139. if (res.code === 200) {
  140. this.currentInviteCode = res.data
  141. uni.showToast({
  142. title: '生成成功',
  143. icon: 'success'
  144. })
  145. this.loadInviteCodes()
  146. } else {
  147. uni.showToast({
  148. title: res.message || '生成失败',
  149. icon: 'none'
  150. })
  151. }
  152. } catch (e) {
  153. uni.hideLoading()
  154. uni.showToast({
  155. title: '生成失败',
  156. icon: 'none'
  157. })
  158. }
  159. },
  160. copyInviteLink() {
  161. if (!this.currentInviteCode) {
  162. uni.showToast({
  163. title: '请先生成邀请码',
  164. icon: 'none'
  165. })
  166. return
  167. }
  168. const inviteUrl = `pages/guide/register/index?inviteCode=${this.currentInviteCode.code}`
  169. uni.setClipboardData({
  170. data: inviteUrl,
  171. success: () => {
  172. uni.showToast({
  173. title: '链接已复制',
  174. icon: 'success'
  175. })
  176. }
  177. })
  178. },
  179. showQrcode() {
  180. if (!this.currentInviteCode) {
  181. uni.showToast({
  182. title: '请先生成邀请码',
  183. icon: 'none'
  184. })
  185. return
  186. }
  187. this.showQrcodeModal = true
  188. this.$nextTick(() => {
  189. this.drawQrcode()
  190. })
  191. },
  192. async drawQrcode() {
  193. if (!this.currentInviteCode) return
  194. const code = this.currentInviteCode.code
  195. const pagePath = '/pages/guide/register/index?inviteCode=' + code
  196. // 通过后端生成真实二维码
  197. let qrBase64 = ''
  198. try {
  199. const res = await generateQrCodeFromText(pagePath)
  200. if (res.code === 200 && res.data) {
  201. qrBase64 = res.data.qrCodeBase64
  202. }
  203. } catch (e) {
  204. console.error('生成二维码失败', e)
  205. }
  206. const ctx = uni.createCanvasContext('qrcodeCanvas')
  207. const canvasWidth = 300
  208. const canvasHeight = 300
  209. ctx.setFillStyle('#FFFFFF')
  210. ctx.fillRect(0, 0, canvasWidth, canvasHeight)
  211. if (qrBase64) {
  212. // 将 base64 写入临时文件,再绘制到 Canvas
  213. const rawBase64 = qrBase64.indexOf('base64,') >= 0 ? qrBase64.split('base64,')[1] : qrBase64
  214. const fsm = uni.getFileSystemManager()
  215. const tempPath = wx.env.USER_DATA_PATH + '/qr_invite_' + Date.now() + '.png'
  216. try {
  217. fsm.writeFileSync(tempPath, rawBase64, 'base64')
  218. ctx.drawImage(tempPath, 40, 30, 220, 220)
  219. } catch (e) {
  220. this._drawQrFallback(ctx, canvasWidth, canvasHeight, code)
  221. }
  222. } else {
  223. this._drawQrFallback(ctx, canvasWidth, canvasHeight, code)
  224. }
  225. ctx.draw()
  226. },
  227. _drawQrFallback(ctx, w, h, code) {
  228. ctx.setFillStyle('#CCCCCC')
  229. ctx.fillRect(40, 30, 220, 220)
  230. ctx.setFillStyle('#999999')
  231. ctx.font = '16px sans-serif'
  232. ctx.textAlign = 'center'
  233. ctx.fillText('邀请码: ' + code, w / 2, 150)
  234. },
  235. saveQrcode() {
  236. uni.canvasToTempFilePath({
  237. canvasId: 'qrcodeCanvas',
  238. success: (res) => {
  239. uni.saveImageToPhotosAlbum({
  240. filePath: res.tempFilePath,
  241. success: () => {
  242. uni.showToast({
  243. title: '保存成功',
  244. icon: 'success'
  245. })
  246. },
  247. fail: () => {
  248. uni.showToast({
  249. title: '保存失败',
  250. icon: 'none'
  251. })
  252. }
  253. })
  254. }
  255. })
  256. },
  257. shareToFriend() {
  258. if (!this.currentInviteCode) {
  259. uni.showToast({
  260. title: '请先生成邀请码',
  261. icon: 'none'
  262. })
  263. return
  264. }
  265. const inviteUrl = `pages/guide/register/index?inviteCode=${this.currentInviteCode.code}`
  266. uni.share({
  267. provider: 'weixin',
  268. scene: {
  269. type: 0,
  270. title: '成为成长规划师',
  271. path: inviteUrl
  272. },
  273. success: () => {
  274. uni.showToast({
  275. title: '分享成功',
  276. icon: 'success'
  277. })
  278. },
  279. fail: () => {
  280. uni.showToast({
  281. title: '分享失败',
  282. icon: 'none'
  283. })
  284. }
  285. })
  286. },
  287. getStatusText(status) {
  288. const statusMap = {
  289. 'active': '可用',
  290. 'used': '已用完',
  291. 'expired': '已过期'
  292. }
  293. return statusMap[status] || status
  294. },
  295. formatDate(dateStr) {
  296. if (!dateStr) return ''
  297. const date = parseDate(dateStr)
  298. if (!date) return ''
  299. return `${date.getMonth() + 1}月${date.getDate()}日`
  300. }
  301. }
  302. }
  303. </script>
  304. <style scoped>
  305. .container {
  306. padding: 30rpx;
  307. background: #F8F8F8;
  308. min-height: 100vh;
  309. }
  310. .header {
  311. text-align: center;
  312. padding: 60rpx 0 40rpx;
  313. }
  314. .title {
  315. font-size: 48rpx;
  316. font-weight: bold;
  317. color: #333;
  318. display: block;
  319. margin-bottom: 20rpx;
  320. }
  321. .subtitle {
  322. font-size: 28rpx;
  323. color: #666;
  324. }
  325. .section-title {
  326. font-size: 32rpx;
  327. font-weight: bold;
  328. color: #333;
  329. margin-bottom: 20rpx;
  330. }
  331. .invite-codes-section,
  332. .share-section,
  333. .tips-section {
  334. background: #fff;
  335. border-radius: 20rpx;
  336. padding: 30rpx;
  337. margin-bottom: 30rpx;
  338. }
  339. .invite-codes-list {
  340. margin-bottom: 20rpx;
  341. }
  342. .invite-code-item {
  343. background: #F8F8F8;
  344. border-radius: 10rpx;
  345. padding: 20rpx;
  346. margin-bottom: 20rpx;
  347. }
  348. .code-info {
  349. display: flex;
  350. justify-content: space-between;
  351. align-items: center;
  352. margin-bottom: 10rpx;
  353. }
  354. .code-text {
  355. font-size: 32rpx;
  356. font-weight: bold;
  357. color: #333;
  358. font-family: monospace;
  359. }
  360. .code-status {
  361. font-size: 24rpx;
  362. padding: 4rpx 12rpx;
  363. border-radius: 20rpx;
  364. }
  365. .status-active {
  366. background: #E8F5E9;
  367. color: #4CAF50;
  368. }
  369. .status-used {
  370. background: #FFF3E0;
  371. color: #FF9800;
  372. }
  373. .status-expired {
  374. background: #FFEBEE;
  375. color: #F44336;
  376. }
  377. .code-stats {
  378. display: flex;
  379. justify-content: space-between;
  380. font-size: 24rpx;
  381. color: #999;
  382. }
  383. .stat {
  384. margin-right: 20rpx;
  385. }
  386. .btn-generate {
  387. width: 100%;
  388. height: 80rpx;
  389. background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  390. color: #fff;
  391. border-radius: 40rpx;
  392. font-size: 28rpx;
  393. font-weight: bold;
  394. }
  395. .share-item {
  396. display: flex;
  397. align-items: center;
  398. padding: 30rpx 0;
  399. border-bottom: 1rpx solid #F0F0F0;
  400. }
  401. .share-item:last-child {
  402. border-bottom: none;
  403. }
  404. .share-icon {
  405. font-size: 48rpx;
  406. margin-right: 20rpx;
  407. }
  408. .share-content {
  409. flex: 1;
  410. }
  411. .share-title {
  412. font-size: 30rpx;
  413. color: #333;
  414. display: block;
  415. margin-bottom: 8rpx;
  416. }
  417. .share-desc {
  418. font-size: 24rpx;
  419. color: #999;
  420. }
  421. .share-arrow {
  422. font-size: 40rpx;
  423. color: #CCC;
  424. }
  425. .tips-list {
  426. margin-top: 20rpx;
  427. }
  428. .tip-item {
  429. display: flex;
  430. align-items: flex-start;
  431. margin-bottom: 20rpx;
  432. }
  433. .tip-icon {
  434. font-size: 32rpx;
  435. margin-right: 12rpx;
  436. }
  437. .tip-text {
  438. font-size: 26rpx;
  439. color: #666;
  440. line-height: 1.5;
  441. }
  442. .qrcode-modal {
  443. position: fixed;
  444. top: 0;
  445. left: 0;
  446. right: 0;
  447. bottom: 0;
  448. background: rgba(0, 0, 0, 0.5);
  449. display: flex;
  450. align-items: center;
  451. justify-content: center;
  452. z-index: 999;
  453. }
  454. .modal-content {
  455. background: #fff;
  456. border-radius: 20rpx;
  457. padding: 40rpx;
  458. width: 80%;
  459. max-width: 400rpx;
  460. }
  461. .modal-header {
  462. display: flex;
  463. justify-content: space-between;
  464. align-items: center;
  465. margin-bottom: 30rpx;
  466. }
  467. .modal-title {
  468. font-size: 32rpx;
  469. font-weight: bold;
  470. color: #333;
  471. }
  472. .modal-close {
  473. font-size: 40rpx;
  474. color: #999;
  475. padding: 10rpx;
  476. }
  477. .qrcode-container {
  478. display: flex;
  479. justify-content: center;
  480. margin-bottom: 30rpx;
  481. }
  482. .qrcode-canvas {
  483. width: 300rpx;
  484. height: 300rpx;
  485. border: 1rpx solid #E0E0E0;
  486. border-radius: 10rpx;
  487. }
  488. .modal-actions {
  489. display: flex;
  490. gap: 20rpx;
  491. }
  492. .btn-save,
  493. .btn-close {
  494. flex: 1;
  495. height: 80rpx;
  496. border-radius: 40rpx;
  497. font-size: 28rpx;
  498. font-weight: bold;
  499. }
  500. .btn-save {
  501. background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  502. color: #fff;
  503. }
  504. .btn-close {
  505. background: #F0F0F0;
  506. color: #333;
  507. }
  508. </style>