invite.vue 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <template>
  2. <view class="container">
  3. <!-- 邀请信息 -->
  4. <view class="invite-card" v-if="guideInfo">
  5. <view class="guide-avatar">{{ guideInfo.guideName ? guideInfo.guideName[0] : '师' }}</view>
  6. <text class="guide-name">{{ guideInfo.guideName }}</text>
  7. <text class="invite-desc">邀请您绑定家庭关系</text>
  8. <text class="expire-text">邀请有效期至 {{ formatDate(guideInfo.expireTime) }}</text>
  9. </view>
  10. <!-- 绑定表单 -->
  11. <view class="bind-form" v-if="!bound">
  12. <view class="form-item">
  13. <text class="label">选择家庭</text>
  14. <picker :range="families" range-key="name" @change="onFamilyChange">
  15. <view class="picker">
  16. {{ selectedFamily ? selectedFamily.name : '请选择家庭' }}
  17. </view>
  18. </picker>
  19. </view>
  20. <view class="form-item">
  21. <text class="label">服务类型</text>
  22. <picker :range="serviceTypes" range-key="label" @change="onServiceTypeChange">
  23. <view class="picker">
  24. {{ serviceType.label }}
  25. </view>
  26. </picker>
  27. </view>
  28. <view class="form-item">
  29. <text class="label">服务价格</text>
  30. <input class="input" type="digit" v-model="servicePrice" placeholder="请输入服务价格" />
  31. </view>
  32. <button class="btn-confirm" @click="confirmBind">确认绑定</button>
  33. </view>
  34. <!-- 已绑定提示 -->
  35. <view class="bound-card" v-else>
  36. <text class="success-icon">✓</text>
  37. <text class="success-text">绑定成功</text>
  38. <text class="success-desc">您已成功绑定成长规划师</text>
  39. <button class="btn-go" @click="goHome">返回首页</button>
  40. </view>
  41. <!-- 分享邀请 -->
  42. <view class="share-section" v-if="isGuide">
  43. <text class="section-title">邀请家长绑定</text>
  44. <view class="invite-url">
  45. <text selectable>{{ inviteUrl }}</text>
  46. </view>
  47. <button class="btn-copy" @click="copyUrl">复制邀请链接</button>
  48. <button class="btn-qrcode" @click="showQrcode">生成二维码</button>
  49. </view>
  50. <!-- 二维码弹窗 -->
  51. <uni-popup ref="qrPopup" type="center">
  52. <view class="qr-popup">
  53. <text class="qr-title">扫码绑定</text>
  54. <image class="qr-image" :src="qrcodeUrl" mode="widthFix" />
  55. <text class="qr-desc">微信扫一扫绑定</text>
  56. <text class="close-btn" @click="closeQrcode">关闭</text>
  57. </view>
  58. </uni-popup>
  59. </view>
  60. </template>
  61. <script>
  62. import { generateBindInvite, generateQrCodeFromText } from '@/utils/api.js'
  63. import config from '@/config.js'
  64. export default {
  65. data() {
  66. return {
  67. inviteToken: '',
  68. guideId: null,
  69. guideInfo: null,
  70. families: [],
  71. selectedFamily: null,
  72. serviceTypes: [
  73. { label: '咨询', value: 'consulting' },
  74. { label: '月度服务', value: 'monthly' },
  75. { label: '年度服务', value: 'yearly' }
  76. ],
  77. serviceType: { label: '咨询', value: 'consulting' },
  78. servicePrice: 0,
  79. bound: false,
  80. isGuide: false,
  81. inviteUrl: '',
  82. qrcodeUrl: ''
  83. }
  84. },
  85. onLoad(options) {
  86. if (options.token) {
  87. this.inviteToken = options.token
  88. this.guideId = options.guideId
  89. this.loadGuideInfo()
  90. }
  91. // 判断是否为成长规划师
  92. const roles = uni.getStorageSync('roles') || ''
  93. this.isGuide = roles.includes('teacher')
  94. if (this.isGuide) {
  95. this.generateInvite()
  96. }
  97. this.loadFamilies()
  98. },
  99. methods: {
  100. loadGuideInfo() {
  101. // 从服务器获取成长规划师信息
  102. uni.request({
  103. url: `${config.API_BASE_URL}/api/guide/bind/validate`,
  104. data: { token: this.inviteToken },
  105. success: (res) => {
  106. if (res.data.code === 200) {
  107. this.guideInfo = res.data.data
  108. }
  109. }
  110. })
  111. },
  112. loadFamilies() {
  113. // 获取家庭列表
  114. uni.request({
  115. url: `${config.API_BASE_URL}/api/family/user/family-members`,
  116. header: { Authorization: `Bearer ${uni.getStorageSync('token')}` },
  117. success: (res) => {
  118. if (res.data.code === 200) {
  119. this.families = res.data.data || []
  120. }
  121. }
  122. })
  123. },
  124. onFamilyChange(e) {
  125. this.selectedFamily = this.families[e.detail.value]
  126. },
  127. onServiceTypeChange(e) {
  128. this.serviceType = this.serviceTypes[e.detail.value]
  129. },
  130. async confirmBind() {
  131. if (!this.selectedFamily) {
  132. uni.showToast({ title: '请选择家庭', icon: 'none' })
  133. return
  134. }
  135. uni.showLoading({ title: '绑定中...' })
  136. try {
  137. await uni.request({
  138. url: `${config.API_BASE_URL}/api/bind/accept`,
  139. method: 'POST',
  140. header: { Authorization: `Bearer ${uni.getStorageSync('token')}` },
  141. data: {
  142. guideId: this.guideId,
  143. bindToken: this.inviteToken,
  144. familyId: this.selectedFamily.id,
  145. serviceType: this.serviceType.value,
  146. servicePrice: this.servicePrice
  147. }
  148. })
  149. uni.showToast({ title: '绑定成功' })
  150. this.bound = true
  151. } catch (e) {
  152. uni.showToast({ title: '绑定失败', icon: 'none' })
  153. } finally {
  154. uni.hideLoading()
  155. }
  156. },
  157. async generateInvite() {
  158. try {
  159. const res = await generateBindInvite()
  160. this.inviteUrl = res.data.inviteUrl
  161. this.guideInfo = res.data
  162. } catch (e) {
  163. console.error(e)
  164. }
  165. },
  166. copyUrl() {
  167. uni.setClipboardData({ data: this.inviteUrl })
  168. uni.showToast({ title: '已复制' })
  169. },
  170. async showQrcode() {
  171. if (!this.inviteUrl) {
  172. uni.showToast({ title: '请先生成邀请', icon: 'none' })
  173. return
  174. }
  175. try {
  176. const res = await generateQrCodeFromText(this.inviteUrl)
  177. if (res.code === 200 && res.data) {
  178. this.qrcodeUrl = res.data.qrCodeBase64
  179. } else {
  180. uni.showToast({ title: res.message || '生成二维码失败', icon: 'none' })
  181. return
  182. }
  183. } catch (e) {
  184. uni.showToast({ title: '生成二维码失败', icon: 'none' })
  185. return
  186. }
  187. this.$refs.qrPopup.open()
  188. },
  189. closeQrcode() {
  190. this.$refs.qrPopup.close()
  191. },
  192. formatDate(date) {
  193. if (!date) return ''
  194. const d = new Date(date)
  195. return `${d.getMonth() + 1}-${d.getDate()}`
  196. },
  197. goHome() {
  198. uni.navigateTo({ url: '/pages/home-pages/parent-index' })
  199. }
  200. }
  201. }
  202. </script>
  203. <style scoped>
  204. .container { min-height: 100vh; background: #f5f5f5; padding: 30rpx; }
  205. .invite-card, .bound-card {
  206. background: #fff;
  207. border-radius: 16rpx;
  208. padding: 60rpx 30rpx;
  209. text-align: center;
  210. margin-bottom: 30rpx;
  211. }
  212. .guide-avatar {
  213. width: 120rpx;
  214. height: 120rpx;
  215. background: #4CAF50;
  216. border-radius: 50%;
  217. margin: 0 auto 20rpx;
  218. display: flex;
  219. align-items: center;
  220. justify-content: center;
  221. font-size: 48rpx;
  222. color: #fff;
  223. }
  224. .guide-name { font-size: 36rpx; font-weight: bold; display: block; margin-bottom: 10rpx; }
  225. .invite-desc { font-size: 28rpx; color: #666; display: block; margin-bottom: 20rpx; }
  226. .expire-text { font-size: 24rpx; color: #999; }
  227. .bind-form { background: #fff; border-radius: 16rpx; padding: 30rpx; }
  228. .form-item { margin-bottom: 30rpx; }
  229. .label { display: block; font-size: 28rpx; color: #333; margin-bottom: 16rpx; }
  230. .input, .picker { padding: 20rpx; background: #f5f5f5; border-radius: 12rpx; font-size: 28rpx; }
  231. .btn-confirm { background: #4CAF50; color: #fff; border-radius: 40rpx; margin-top: 40rpx; }
  232. .success-icon { font-size: 80rpx; color: #4CAF50; display: block; margin-bottom: 20rpx; }
  233. .success-text { font-size: 36rpx; font-weight: bold; display: block; margin-bottom: 10rpx; }
  234. .success-desc { font-size: 28rpx; color: #666; display: block; margin-bottom: 40rpx; }
  235. .btn-go { background: #4CAF50; color: #fff; border-radius: 40rpx; }
  236. .share-section { background: #fff; border-radius: 16rpx; padding: 30rpx; margin-top: 30rpx; }
  237. .section-title { font-size: 30rpx; font-weight: bold; display: block; margin-bottom: 20rpx; }
  238. .invite-url { background: #f5f5f5; padding: 20rpx; border-radius: 8rpx; margin-bottom: 20rpx; font-size: 24rpx; word-break: break-all; }
  239. .btn-copy, .btn-qrcode { background: #4CAF50; color: #fff; margin-bottom: 20rpx; }
  240. .qr-popup { background: #fff; border-radius: 16rpx; padding: 40rpx; text-align: center; width: 500rpx; }
  241. .qr-title { font-size: 32rpx; font-weight: bold; display: block; margin-bottom: 30rpx; }
  242. .qr-image { width: 300rpx; height: 300rpx; margin-bottom: 20rpx; }
  243. .qr-desc { font-size: 26rpx; color: #666; display: block; margin-bottom: 30rpx; }
  244. .close-btn { font-size: 28rpx; color: #999; }
  245. </style>