share.vue 13 KB

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