invite.vue 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. export default {
  64. data() {
  65. return {
  66. inviteToken: '',
  67. guideId: null,
  68. guideInfo: null,
  69. families: [],
  70. selectedFamily: null,
  71. serviceTypes: [
  72. { label: '咨询', value: 'consulting' },
  73. { label: '月度服务', value: 'monthly' },
  74. { label: '年度服务', value: 'yearly' }
  75. ],
  76. serviceType: { label: '咨询', value: 'consulting' },
  77. servicePrice: 0,
  78. bound: false,
  79. isGuide: false,
  80. inviteUrl: '',
  81. qrcodeUrl: ''
  82. }
  83. },
  84. onLoad(options) {
  85. if (options.token) {
  86. this.inviteToken = options.token
  87. this.guideId = options.guideId
  88. this.loadGuideInfo()
  89. }
  90. // 判断是否为成长规划师
  91. const roles = uni.getStorageSync('roles') || ''
  92. this.isGuide = roles.includes('teacher')
  93. if (this.isGuide) {
  94. this.generateInvite()
  95. }
  96. this.loadFamilies()
  97. },
  98. methods: {
  99. loadGuideInfo() {
  100. // 从服务器获取成长规划师信息
  101. uni.request({
  102. url: `${getApp().globalData.baseUrl}/api/guide/bind/validate`,
  103. data: { token: this.inviteToken },
  104. success: (res) => {
  105. if (res.data.code === 200) {
  106. this.guideInfo = res.data.data
  107. }
  108. }
  109. })
  110. },
  111. loadFamilies() {
  112. // 获取家庭列表
  113. uni.request({
  114. url: `${getApp().globalData.baseUrl}/api/family/user/family-members`,
  115. header: { Authorization: `Bearer ${uni.getStorageSync('token')}` },
  116. success: (res) => {
  117. if (res.data.code === 200) {
  118. this.families = res.data.data || []
  119. }
  120. }
  121. })
  122. },
  123. onFamilyChange(e) {
  124. this.selectedFamily = this.families[e.detail.value]
  125. },
  126. onServiceTypeChange(e) {
  127. this.serviceType = this.serviceTypes[e.detail.value]
  128. },
  129. async confirmBind() {
  130. if (!this.selectedFamily) {
  131. uni.showToast({ title: '请选择家庭', icon: 'none' })
  132. return
  133. }
  134. uni.showLoading({ title: '绑定中...' })
  135. try {
  136. await uni.request({
  137. url: `${getApp().globalData.baseUrl}/api/bind/accept`,
  138. method: 'POST',
  139. header: { Authorization: `Bearer ${uni.getStorageSync('token')}` },
  140. data: {
  141. guideId: this.guideId,
  142. bindToken: this.inviteToken,
  143. familyId: this.selectedFamily.id,
  144. serviceType: this.serviceType.value,
  145. servicePrice: this.servicePrice
  146. }
  147. })
  148. uni.showToast({ title: '绑定成功' })
  149. this.bound = true
  150. } catch (e) {
  151. uni.showToast({ title: '绑定失败', icon: 'none' })
  152. } finally {
  153. uni.hideLoading()
  154. }
  155. },
  156. async generateInvite() {
  157. try {
  158. const res = await generateBindInvite()
  159. this.inviteUrl = res.data.inviteUrl
  160. this.guideInfo = res.data
  161. } catch (e) {
  162. console.error(e)
  163. }
  164. },
  165. copyUrl() {
  166. uni.setClipboardData({ data: this.inviteUrl })
  167. uni.showToast({ title: '已复制' })
  168. },
  169. async showQrcode() {
  170. if (!this.inviteUrl) {
  171. uni.showToast({ title: '请先生成邀请', icon: 'none' })
  172. return
  173. }
  174. try {
  175. const res = await generateQrCodeFromText(this.inviteUrl)
  176. if (res.code === 200 && res.data) {
  177. this.qrcodeUrl = res.data.qrCodeBase64
  178. } else {
  179. uni.showToast({ title: res.message || '生成二维码失败', icon: 'none' })
  180. return
  181. }
  182. } catch (e) {
  183. uni.showToast({ title: '生成二维码失败', icon: 'none' })
  184. return
  185. }
  186. this.$refs.qrPopup.open()
  187. },
  188. closeQrcode() {
  189. this.$refs.qrPopup.close()
  190. },
  191. formatDate(date) {
  192. if (!date) return ''
  193. const d = new Date(date)
  194. return `${d.getMonth() + 1}-${d.getDate()}`
  195. },
  196. goHome() {
  197. uni.navigateTo({ url: '/pages/home-pages/parent-index' })
  198. }
  199. }
  200. }
  201. </script>
  202. <style scoped>
  203. .container { min-height: 100vh; background: #f5f5f5; padding: 30rpx; }
  204. .invite-card, .bound-card {
  205. background: #fff;
  206. border-radius: 16rpx;
  207. padding: 60rpx 30rpx;
  208. text-align: center;
  209. margin-bottom: 30rpx;
  210. }
  211. .guide-avatar {
  212. width: 120rpx;
  213. height: 120rpx;
  214. background: #4CAF50;
  215. border-radius: 50%;
  216. margin: 0 auto 20rpx;
  217. display: flex;
  218. align-items: center;
  219. justify-content: center;
  220. font-size: 48rpx;
  221. color: #fff;
  222. }
  223. .guide-name { font-size: 36rpx; font-weight: bold; display: block; margin-bottom: 10rpx; }
  224. .invite-desc { font-size: 28rpx; color: #666; display: block; margin-bottom: 20rpx; }
  225. .expire-text { font-size: 24rpx; color: #999; }
  226. .bind-form { background: #fff; border-radius: 16rpx; padding: 30rpx; }
  227. .form-item { margin-bottom: 30rpx; }
  228. .label { display: block; font-size: 28rpx; color: #333; margin-bottom: 16rpx; }
  229. .input, .picker { padding: 20rpx; background: #f5f5f5; border-radius: 12rpx; font-size: 28rpx; }
  230. .btn-confirm { background: #4CAF50; color: #fff; border-radius: 40rpx; margin-top: 40rpx; }
  231. .success-icon { font-size: 80rpx; color: #4CAF50; display: block; margin-bottom: 20rpx; }
  232. .success-text { font-size: 36rpx; font-weight: bold; display: block; margin-bottom: 10rpx; }
  233. .success-desc { font-size: 28rpx; color: #666; display: block; margin-bottom: 40rpx; }
  234. .btn-go { background: #4CAF50; color: #fff; border-radius: 40rpx; }
  235. .share-section { background: #fff; border-radius: 16rpx; padding: 30rpx; margin-top: 30rpx; }
  236. .section-title { font-size: 30rpx; font-weight: bold; display: block; margin-bottom: 20rpx; }
  237. .invite-url { background: #f5f5f5; padding: 20rpx; border-radius: 8rpx; margin-bottom: 20rpx; font-size: 24rpx; word-break: break-all; }
  238. .btn-copy, .btn-qrcode { background: #4CAF50; color: #fff; margin-bottom: 20rpx; }
  239. .qr-popup { background: #fff; border-radius: 16rpx; padding: 40rpx; text-align: center; width: 500rpx; }
  240. .qr-title { font-size: 32rpx; font-weight: bold; display: block; margin-bottom: 30rpx; }
  241. .qr-image { width: 300rpx; height: 300rpx; margin-bottom: 20rpx; }
  242. .qr-desc { font-size: 26rpx; color: #666; display: block; margin-bottom: 30rpx; }
  243. .close-btn { font-size: 28rpx; color: #999; }
  244. </style>