invite.vue 8.8 KB

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