share.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  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 } 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. drawQrcode() {
  192. // TODO: 使用二维码生成库生成二维码
  193. // 这里简化处理,使用 Canvas 绘制占位符
  194. const ctx = uni.createCanvasContext('qrcodeCanvas')
  195. const canvasWidth = 300
  196. const canvasHeight = 300
  197. ctx.setFillStyle('#FFFFFF')
  198. ctx.fillRect(0, 0, canvasWidth, canvasHeight)
  199. ctx.setFillStyle('#333333')
  200. ctx.font = '20px sans-serif'
  201. ctx.textAlign = 'center'
  202. ctx.fillText('邀请码', canvasWidth / 2, 100)
  203. ctx.font = 'bold 40px sans-serif'
  204. ctx.fillText(this.currentInviteCode.code, canvasWidth / 2, 150)
  205. ctx.font = '16px sans-serif'
  206. ctx.fillText('扫码成为成长规划师', canvasWidth / 2, 220)
  207. ctx.draw()
  208. },
  209. saveQrcode() {
  210. uni.canvasToTempFilePath({
  211. canvasId: 'qrcodeCanvas',
  212. success: (res) => {
  213. uni.saveImageToPhotosAlbum({
  214. filePath: res.tempFilePath,
  215. success: () => {
  216. uni.showToast({
  217. title: '保存成功',
  218. icon: 'success'
  219. })
  220. },
  221. fail: () => {
  222. uni.showToast({
  223. title: '保存失败',
  224. icon: 'none'
  225. })
  226. }
  227. })
  228. }
  229. })
  230. },
  231. shareToFriend() {
  232. if (!this.currentInviteCode) {
  233. uni.showToast({
  234. title: '请先生成邀请码',
  235. icon: 'none'
  236. })
  237. return
  238. }
  239. const inviteUrl = `pages/guide/register/index?inviteCode=${this.currentInviteCode.code}`
  240. uni.share({
  241. provider: 'weixin',
  242. scene: {
  243. type: 0,
  244. title: '成为成长规划师',
  245. path: inviteUrl
  246. },
  247. success: () => {
  248. uni.showToast({
  249. title: '分享成功',
  250. icon: 'success'
  251. })
  252. },
  253. fail: () => {
  254. uni.showToast({
  255. title: '分享失败',
  256. icon: 'none'
  257. })
  258. }
  259. })
  260. },
  261. getStatusText(status) {
  262. const statusMap = {
  263. 'active': '可用',
  264. 'used': '已用完',
  265. 'expired': '已过期'
  266. }
  267. return statusMap[status] || status
  268. },
  269. formatDate(dateStr) {
  270. if (!dateStr) return ''
  271. const date = new Date(dateStr)
  272. return `${date.getMonth() + 1}月${date.getDate()}日`
  273. }
  274. }
  275. }
  276. </script>
  277. <style scoped>
  278. .container {
  279. padding: 30rpx;
  280. background: #F8F8F8;
  281. min-height: 100vh;
  282. }
  283. .header {
  284. text-align: center;
  285. padding: 60rpx 0 40rpx;
  286. }
  287. .title {
  288. font-size: 48rpx;
  289. font-weight: bold;
  290. color: #333;
  291. display: block;
  292. margin-bottom: 20rpx;
  293. }
  294. .subtitle {
  295. font-size: 28rpx;
  296. color: #666;
  297. }
  298. .section-title {
  299. font-size: 32rpx;
  300. font-weight: bold;
  301. color: #333;
  302. margin-bottom: 20rpx;
  303. }
  304. .invite-codes-section,
  305. .share-section,
  306. .tips-section {
  307. background: #fff;
  308. border-radius: 20rpx;
  309. padding: 30rpx;
  310. margin-bottom: 30rpx;
  311. }
  312. .invite-codes-list {
  313. margin-bottom: 20rpx;
  314. }
  315. .invite-code-item {
  316. background: #F8F8F8;
  317. border-radius: 10rpx;
  318. padding: 20rpx;
  319. margin-bottom: 20rpx;
  320. }
  321. .code-info {
  322. display: flex;
  323. justify-content: space-between;
  324. align-items: center;
  325. margin-bottom: 10rpx;
  326. }
  327. .code-text {
  328. font-size: 32rpx;
  329. font-weight: bold;
  330. color: #333;
  331. font-family: monospace;
  332. }
  333. .code-status {
  334. font-size: 24rpx;
  335. padding: 4rpx 12rpx;
  336. border-radius: 20rpx;
  337. }
  338. .status-active {
  339. background: #E8F5E9;
  340. color: #4CAF50;
  341. }
  342. .status-used {
  343. background: #FFF3E0;
  344. color: #FF9800;
  345. }
  346. .status-expired {
  347. background: #FFEBEE;
  348. color: #F44336;
  349. }
  350. .code-stats {
  351. display: flex;
  352. justify-content: space-between;
  353. font-size: 24rpx;
  354. color: #999;
  355. }
  356. .stat {
  357. margin-right: 20rpx;
  358. }
  359. .btn-generate {
  360. width: 100%;
  361. height: 80rpx;
  362. background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  363. color: #fff;
  364. border-radius: 40rpx;
  365. font-size: 28rpx;
  366. font-weight: bold;
  367. }
  368. .share-item {
  369. display: flex;
  370. align-items: center;
  371. padding: 30rpx 0;
  372. border-bottom: 1rpx solid #F0F0F0;
  373. }
  374. .share-item:last-child {
  375. border-bottom: none;
  376. }
  377. .share-icon {
  378. font-size: 48rpx;
  379. margin-right: 20rpx;
  380. }
  381. .share-content {
  382. flex: 1;
  383. }
  384. .share-title {
  385. font-size: 30rpx;
  386. color: #333;
  387. display: block;
  388. margin-bottom: 8rpx;
  389. }
  390. .share-desc {
  391. font-size: 24rpx;
  392. color: #999;
  393. }
  394. .share-arrow {
  395. font-size: 40rpx;
  396. color: #CCC;
  397. }
  398. .tips-list {
  399. margin-top: 20rpx;
  400. }
  401. .tip-item {
  402. display: flex;
  403. align-items: flex-start;
  404. margin-bottom: 20rpx;
  405. }
  406. .tip-icon {
  407. font-size: 32rpx;
  408. margin-right: 12rpx;
  409. }
  410. .tip-text {
  411. font-size: 26rpx;
  412. color: #666;
  413. line-height: 1.5;
  414. }
  415. .qrcode-modal {
  416. position: fixed;
  417. top: 0;
  418. left: 0;
  419. right: 0;
  420. bottom: 0;
  421. background: rgba(0, 0, 0, 0.5);
  422. display: flex;
  423. align-items: center;
  424. justify-content: center;
  425. z-index: 999;
  426. }
  427. .modal-content {
  428. background: #fff;
  429. border-radius: 20rpx;
  430. padding: 40rpx;
  431. width: 80%;
  432. max-width: 400rpx;
  433. }
  434. .modal-header {
  435. display: flex;
  436. justify-content: space-between;
  437. align-items: center;
  438. margin-bottom: 30rpx;
  439. }
  440. .modal-title {
  441. font-size: 32rpx;
  442. font-weight: bold;
  443. color: #333;
  444. }
  445. .modal-close {
  446. font-size: 40rpx;
  447. color: #999;
  448. padding: 10rpx;
  449. }
  450. .qrcode-container {
  451. display: flex;
  452. justify-content: center;
  453. margin-bottom: 30rpx;
  454. }
  455. .qrcode-canvas {
  456. width: 300rpx;
  457. height: 300rpx;
  458. border: 1rpx solid #E0E0E0;
  459. border-radius: 10rpx;
  460. }
  461. .modal-actions {
  462. display: flex;
  463. gap: 20rpx;
  464. }
  465. .btn-save,
  466. .btn-close {
  467. flex: 1;
  468. height: 80rpx;
  469. border-radius: 40rpx;
  470. font-size: 28rpx;
  471. font-weight: bold;
  472. }
  473. .btn-save {
  474. background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  475. color: #fff;
  476. }
  477. .btn-close {
  478. background: #F0F0F0;
  479. color: #333;
  480. }
  481. </style>