index.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. <template>
  2. <view class="chat-page">
  3. <!-- 付费墙:开关开启且当前用户非会员时显示 -->
  4. <view v-if="gateLocked" class="paywall">
  5. <view class="paywall-header">
  6. <text class="lock-icon">🔒</text>
  7. <text class="title">AI 教练</text>
  8. <text class="subtitle">为会员专享功能,请先开通会员</text>
  9. </view>
  10. <view class="paywall-body">
  11. <text class="desc">开通会员后可无限次使用减脂 AI 教练,获得个性化减脂方案与饮食建议。</text>
  12. <button class="unlock-btn" :loading="paying" @click="openPlans">
  13. {{ paying ? '支付中...' : '👉 开通会员解锁' }}
  14. </button>
  15. </view>
  16. </view>
  17. <!-- 正常对话页 -->
  18. <scroll-view v-else class="chat-body" scroll-y :scroll-top="scrollTop">
  19. <view v-for="(msg, idx) in messages" :key="idx" class="msg-item" :class="msg.role">
  20. <view class="bubble">{{ msg.content }}</view>
  21. </view>
  22. <view v-if="sending" class="loading-msg">
  23. <text class="dot"></text><text class="dot"></text><text class="dot"></text>
  24. </view>
  25. </scroll-view>
  26. <view v-if="!gateLocked" class="chat-input">
  27. <input class="input" v-model="inputText" placeholder="输入您的问题..." @confirm="send" confirm-type="send" :disabled="sending" />
  28. <button class="send-btn" size="mini" @click="send" :loading="sending">发送</button>
  29. </view>
  30. <!-- 会员方案选择弹窗 -->
  31. <view v-if="showPlans" class="modal-mask" @click="showPlans=false">
  32. <view class="modal-content" @click.stop>
  33. <view class="modal-header">
  34. <text class="modal-title">选择会员方案</text>
  35. <text class="modal-close" @click="showPlans=false">✕</text>
  36. </view>
  37. <scroll-view scroll-y class="plans-list">
  38. <view
  39. v-for="(plan, i) in plans"
  40. :key="plan.levelCode + plan.period"
  41. class="plan-item"
  42. :class="{selected: selectedPlanIndex===i}"
  43. @click="selectPlan(i)"
  44. >
  45. <view class="plan-left">
  46. <text class="plan-name">{{ plan.levelName || plan.levelCode }}</text>
  47. <text class="plan-period">{{ plan.period === 'monthly' ? '月付' : plan.period === 'quarterly' ? '季付' : '年付' }}</text>
  48. </view>
  49. <view class="plan-right">
  50. <text v-if="plan.originalPrice" class="plan-original">{{ (plan.originalPrice/100).toFixed(2) }}元</text>
  51. <text class="plan-price">{{ (plan.yearly/100).toFixed(2) }}元</text>
  52. </view>
  53. </view>
  54. </scroll-view>
  55. <button class="pay-btn" :loading="paying" @click="confirmPay">{{ paying ? '支付中...' : '立即开通' }}</button>
  56. </view>
  57. </view>
  58. </view>
  59. </template>
  60. <script>
  61. import { aiCoachSend, getAiCoachGateStatus, getMembershipPlans, createMembershipOrder } from '../../utils/api.js'
  62. export default {
  63. data() {
  64. return {
  65. inputText: '',
  66. messages: [],
  67. scrollTop: 0,
  68. sending: false,
  69. // 付费开关相关
  70. gateLocked: false,
  71. gateInfo: null,
  72. showPlans: false,
  73. plans: [],
  74. selectedPlanIndex: 0,
  75. paying: false
  76. }
  77. },
  78. onLoad() {
  79. this.messages.push({
  80. role: 'assistant',
  81. content: '你好,我是你的减脂AI教练,请告诉我你的目标和当前情况,我会帮你制定合适的减脂计划。'
  82. })
  83. this.loadGate()
  84. },
  85. methods: {
  86. loadGate() {
  87. getAiCoachGateStatus().then(res => {
  88. this.gateInfo = res.data
  89. if (res.data && res.data.enabled && !res.data.canUse) {
  90. this.gateLocked = true
  91. }
  92. }).catch(() => {
  93. // 接口异常默认放行(fail-open),不阻断体验
  94. this.gateLocked = false
  95. })
  96. },
  97. openPlans() {
  98. this.showPlans = true
  99. this.plans = []
  100. this.selectedPlanIndex = 0
  101. this.paying = false
  102. getMembershipPlans().then(res => {
  103. if (res.data && Array.isArray(res.data)) {
  104. // 按 levelCode 分组:每个 levelCode 列出 year/month/quarterly 三个档期
  105. var map = {}
  106. res.data.forEach(p => {
  107. if (!map[p.levelCode]) map[p.levelCode] = {}
  108. map[p.levelCode][p.period || 'yearly'] = p
  109. })
  110. var list = []
  111. Object.keys(map).forEach(code => {
  112. var m = map[code]
  113. var yearly = m.yearly || {}
  114. var monthly = m.monthly || {}
  115. var quarterly = m.quarterly || {}
  116. var defaultPeriod = 'yearly'
  117. ;[{period:'yearly',price:yearly.yearly||0,orig:yearly.originalYearly||0},{period:'monthly',price:monthly.monthly||0,orig:monthly.originalMonthly||0},{period:'quarterly',price:quarterly.quarterly||0,orig:quarterly.originalQuarterly||0}]
  118. .filter(p => p.price > 0)
  119. .forEach(p => list.push(Object.assign({levelCode:code, levelName:m[defaultPeriod].levelName||p.levelName||code}, p)))
  120. })
  121. this.plans = list
  122. if (this.plans.length > 0) this.selectedPlanIndex = 0
  123. }
  124. }).catch(() => {
  125. uni.showToast({ title: '获取方案失败', icon: 'none' })
  126. this.showPlans = false
  127. })
  128. },
  129. selectPlan(idx) {
  130. this.selectedPlanIndex = idx
  131. },
  132. confirmPay() {
  133. if (this.plans.length === 0) return
  134. var plan = this.plans[this.selectedPlanIndex]
  135. if (!plan) return
  136. this.paying = true
  137. var payload = {
  138. levelCode: plan.levelCode,
  139. paymentType: plan.period,
  140. period: plan.period
  141. }
  142. createMembershipOrder(payload).then(res => {
  143. this.handlePayResult(res.data)
  144. }).catch(err => {
  145. this.paying = false
  146. uni.showToast({ title: '创建订单失败', icon: 'none' })
  147. })
  148. },
  149. handlePayResult(data) {
  150. this.paying = false
  151. if (!data) return
  152. if (data.payType === 'none') {
  153. // 试用/直接开通
  154. uni.showToast({ title: '开通成功', icon: 'success' })
  155. this.showPlans = false
  156. this.gateLocked = false
  157. return
  158. }
  159. if (data.payType === 'virtual') {
  160. // 微信虚拟支付(道具直购)
  161. this.paying = true
  162. var signData = JSON.stringify(data.orderInfo || {})
  163. wx.requestVirtualPayment({
  164. signData: signData,
  165. extInfo: data.extInfo || {},
  166. sign: data.sign || '',
  167. signature: data.signature || '',
  168. env: data.env,
  169. success: () => {
  170. this.paying = false
  171. uni.showToast({ title: '开通成功', icon: 'success' })
  172. this.showPlans = false
  173. this.loadGate()
  174. },
  175. fail: (err) => {
  176. this.paying = false
  177. console.error('虚拟支付失败', err)
  178. uni.showModal({ title: '提示', content: '支付失败,是否改用普通微信支付?', showCancel: false, confirmText: '我知道了' })
  179. }
  180. })
  181. return
  182. }
  183. if (data.payType === 'wechat') {
  184. var payParams = data.wechatPayParams
  185. if (!payParams) {
  186. uni.showToast({ title: '支付参数异常', icon: 'none' })
  187. return
  188. }
  189. this.paying = true
  190. uni.requestPayment({
  191. ...payParams,
  192. success: () => {
  193. this.paying = false
  194. uni.showToast({ title: '开通成功', icon: 'success' })
  195. this.showPlans = false
  196. this.loadGate()
  197. },
  198. fail: (err) => {
  199. this.paying = false
  200. console.error('微信支付失败', err)
  201. uni.showModal({ title: '提示', content: '支付未完成,请重新发起开通', showCancel: false, confirmText: '我知道了' })
  202. }
  203. })
  204. return
  205. }
  206. uni.showToast({ title: '未知支付方式', icon: 'none' })
  207. },
  208. scrollToBottom() {
  209. setTimeout(() => {
  210. this.scrollTop = this.messages.length * 1000
  211. }, 100)
  212. },
  213. send() {
  214. if (this.gateLocked) {
  215. uni.showModal({
  216. title: 'AI 教练',
  217. content: 'AI 教练为会员专享功能,请先开通会员',
  218. confirmText: '去开通',
  219. cancelText: '取消',
  220. success: (res) => {
  221. if (res.confirm) this.openPlans()
  222. }
  223. })
  224. return
  225. }
  226. if (!this.inputText.trim() || this.sending) return
  227. var content = this.inputText.trim()
  228. this.messages.push({ role: 'user', content: content })
  229. this.inputText = ''
  230. this.sending = true
  231. this.scrollToBottom()
  232. var payload = {
  233. content: content,
  234. history: this.messages.slice(0, -1).map(m => ({ role: m.role, content: m.content }))
  235. }
  236. aiCoachSend(payload).then(res => {
  237. this.sending = false
  238. if (res.data && res.data.response) {
  239. this.messages.push({ role: 'assistant', content: res.data.response })
  240. } else if (res.data && res.data.reply) {
  241. this.messages.push({ role: 'assistant', content: res.data.reply })
  242. } else if (res.code === 46003) {
  243. // 会员门禁:跳转开通
  244. this.gateLocked = true
  245. uni.showModal({ title: 'AI 教练', content: res.message || 'AI 教练为会员专享功能,请先开通会员', confirmText: '去开通', cancelText: '取消', success: r => { if (r.confirm) this.openPlans() })
  246. } else {
  247. this.messages.push({ role: 'assistant', content: '抱歉,我暂时无法回答这个问题。' })
  248. }
  249. this.scrollToBottom()
  250. }).catch(err => {
  251. this.sending = false
  252. // api.js 非 200 会 showToast + reject;46003 已在前端 catch 处理
  253. if (err && err.code === 46003) {
  254. this.gateLocked = true
  255. }
  256. this.messages.push({ role: 'assistant', content: '网络异常,请稍后重试。' })
  257. this.scrollToBottom()
  258. })
  259. }
  260. }
  261. }
  262. </script>
  263. <style>
  264. .chat-page { display: flex; flex-direction: column; height: 100vh; background: #f8f8f8; }
  265. .chat-body { flex: 1; padding: 30rpx; box-sizing: border-box; }
  266. .msg-item { margin-bottom: 30rpx; display: flex; }
  267. .msg-item.user { justify-content: flex-end; }
  268. .msg-item.assistant { justify-content: flex-start; }
  269. .bubble { max-width: 70%; padding: 20rpx 30rpx; border-radius: 16rpx; word-wrap: break-word; }
  270. .user .bubble { background: #F97316; color: #fff; }
  271. .assistant .bubble { background: #fff; color: #333; }
  272. .chat-input { display: flex; padding: 20rpx 30rpx; background: #fff; border-top: 1rpx solid #eee; align-items: center; }
  273. .input { flex: 1; height: 80rpx; padding: 0 20rpx; background: #f5f5f5; border-radius: 40rpx; margin-right: 20rpx; }
  274. .send-btn { background: #F97316; color: #fff; border-radius: 40rpx; }
  275. .loading-msg { display: flex; justify-content: flex-start; margin-bottom: 30rpx; padding-left: 10rpx; }
  276. .dot { width: 14rpx; height: 14rpx; background: #ccc; border-radius: 50%; margin: 0 8rpx; animation: dot-bounce 1.2s infinite; }
  277. .dot:nth-child(2) { animation-delay: 0.2s; }
  278. .dot:nth-child(3) { animation-delay: 0.4s; }
  279. @keyframes dot-bounce { 0%, 80%, 100% { transform: scale(0.6); opacity: 0.5; } 40% { transform: scale(1); opacity: 1; } }
  280. /* 付费墙 */
  281. .paywall { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; padding: 80rpx 40rpx; box-sizing: border-box; background: linear-gradient(135deg, #FFF7ED 0%, #FFF 60%); }
  282. .paywall-header { text-align: center; margin-bottom: 60rpx; }
  283. .lock-icon { font-size: 100rpx; display: block; margin-bottom: 20rpx; }
  284. .paywall-header .title { font-size: 44rpx; font-weight: 700; color: #111; display: block; }
  285. .paywall-header .subtitle { font-size: 28rpx; color: #F97316; margin-top: 12rpx; display: block; }
  286. .paywall-body { width: 100%; background: #fff; border-radius: 24rpx; padding: 40rpx; box-sizing: border-box; box-shadow: 0 4rpx 24rpx rgba(0,0,0,0.06); }
  287. .paywall-body .desc { font-size: 28rpx; color: #666; line-height: 1.7; display: block; margin-bottom: 40rpx; }
  288. .unlock-btn { width: 100%; background: #F97316; color: #fff; border-radius: 48rpx; height: 96rpx; line-height: 96rpx; font-size: 32rpx; font-weight: 600; }
  289. /* 方案弹窗 */
  290. .modal-mask { position: fixed; top: 0; left: 0; right: 0; bottom: 0; background: rgba(0,0,0,0.45); z-index: 1000; display: flex; align-items: flex-end; }
  291. .modal-content { width: 100%; background: #fff; border-radius: 32rpx 32rpx 0 0; padding: 40rpx; box-sizing: border-box; max-height: 85vh; display: flex; flex-direction: column; }
  292. .modal-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 30rpx; }
  293. .modal-title { font-size: 34rpx; font-weight: 700; color: #111; }
  294. .modal-close { font-size: 36rpx; color: #999; padding: 10rpx; }
  295. .plans-list { flex: 1; overflow-y: auto; margin-bottom: 30rpx; }
  296. .plan-item { display: flex; justify-content: space-between; align-items: center; padding: 30rpx; border: 2rpx solid #f0f0f0; border-radius: 20rpx; margin-bottom: 20rpx; }
  297. .plan-item.selected { border-color: #F97316; background: #FFF7ED; }
  298. .plan-left { display: flex; flex-direction: column; }
  299. .plan-name { font-size: 30rpx; font-weight: 600; color: #111; }
  300. .plan-period { font-size: 24rpx; color: #999; margin-top: 6rpx; }
  301. .plan-right { text-align: right; }
  302. .plan-original { font-size: 24rpx; color: #bbb; text-decoration: line-through; }
  303. .plan-price { font-size: 32rpx; font-weight: 700; color: #F97316; }
  304. .pay-btn { width: 100%; background: #F97316; color: #fff; border-radius: 48rpx; height: 96rpx; line-height: 96rpx; font-size: 32rpx; font-weight: 600; }
  305. </style>