ProductSelectionFlow.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. <template>
  2. <view class="product-flow">
  3. <view class="product-flow-head">
  4. <view class="product-flow-head-left">
  5. <text class="product-flow-title">🛒 产品多选一</text>
  6. <text class="product-flow-sub">每类产品按效果评分与价格对比,确认后生成购买任务</text>
  7. </view>
  8. <text v-if="cartCount > 0" class="product-cart-badge" @click="goCart">购物车 {{ cartCount }}</text>
  9. </view>
  10. <view v-if="loading" class="product-hint">正在加载产品推荐...</view>
  11. <view v-else-if="loadError" class="product-hint product-hint-error">{{ loadError }}</view>
  12. <view v-else class="product-groups">
  13. <view v-for="(g, gi) in groups" :key="gi" class="product-group">
  14. <view class="product-group-head">
  15. <text class="product-group-title">{{ g.categoryLabel }}</text>
  16. <text class="product-group-count">多选一</text>
  17. </view>
  18. <view v-for="(o, oi) in g.options" :key="oi" class="product-option"
  19. :class="{ 'product-option-on': selectedMap[g.category] === oi }"
  20. @click="selectOption(g.category, oi)">
  21. <view class="product-option-body">
  22. <text class="product-option-name">{{ o.name }}</text>
  23. <view class="product-option-tags">
  24. <text v-if="o.effectScore !== null && o.effectScore !== undefined" class="product-tag tag-effect">效果 {{ o.effectScore }} 分</text>
  25. <text v-if="o.priceYuan" class="product-tag tag-price">¥{{ o.priceYuan }}</text>
  26. <text v-if="!o.inPlatform" class="product-tag tag-external">自选渠道</text>
  27. </view>
  28. <text v-if="o.reason" class="product-option-reason">{{ o.reason }}</text>
  29. </view>
  30. <view class="product-radio" :class="{ 'product-radio-on': selectedMap[g.category] === oi }"></view>
  31. </view>
  32. </view>
  33. <view v-if="groups.length > 0 && purchaseTasks.length === 0" class="product-actions">
  34. <button class="product-btn product-btn-primary" :disabled="confirming" @click="confirmSelection">
  35. {{ confirming ? '生成中...' : '确认选择并生成购买任务' }}
  36. </button>
  37. </view>
  38. </view>
  39. <!-- 购买任务(R5) -->
  40. <view v-if="purchaseTasks.length > 0" class="purchase-block">
  41. <view class="purchase-head">
  42. <text class="purchase-title">购买任务</text>
  43. <text class="purchase-sub">本平台商品已加入购物车</text>
  44. </view>
  45. <view v-for="(t, ti) in purchaseTasks" :key="ti" class="purchase-item">
  46. <view class="purchase-item-main">
  47. <text class="purchase-item-name">{{ t.title }}</text>
  48. <text class="purchase-item-tip">{{ t.actionType === 'cart' ? '已加入购物车,去结算完成购买' : '线下/其他渠道购买' }}</text>
  49. </view>
  50. <text class="purchase-item-status" :class="{ 'purchase-item-status-done': t.status === 'completed' }">
  51. {{ t.status === 'completed' ? '已完成' : '待购买' }}
  52. </text>
  53. </view>
  54. <view class="purchase-actions">
  55. <button class="product-btn product-btn-ghost" @click="goCart">去购物车</button>
  56. <button class="product-btn product-btn-primary" :disabled="completing" @click="completePurchase">
  57. {{ completing ? '生成中...' : '我已完成购买' }}
  58. </button>
  59. </view>
  60. </view>
  61. <!-- 执行任务(R6) -->
  62. <view v-if="executionTasks.length > 0" class="execution-block">
  63. <view class="execution-head">
  64. <text class="purchase-title">执行任务(按方案建议)</text>
  65. <text class="purchase-sub">共 {{ executionTasks.length }} 项,购买任务完成后自动解锁</text>
  66. </view>
  67. <view v-for="(t, ti) in executionTasks" :key="ti" class="execution-item">
  68. <text class="execution-item-name">{{ t.title }}</text>
  69. <text v-if="t.category" class="execution-item-cat">{{ t.category }}</text>
  70. </view>
  71. <view class="execution-tip">可在「首页 - 任务」中查看并打卡</view>
  72. </view>
  73. </view>
  74. </template>
  75. <script>
  76. import { getHealthPlanProductOptions, selectHealthPlanProducts, completeHealthPlanPurchase, cartCount as getCartCount } from '@/utils/api.js'
  77. export default {
  78. name: 'ProductSelectionFlow',
  79. props: {
  80. planId: {
  81. type: [Number, String],
  82. default: null
  83. }
  84. },
  85. data: function() {
  86. return {
  87. loading: false,
  88. loadError: '',
  89. groups: [],
  90. // category -> 选中的选项下标(避免模板内调用带参方法)
  91. selectedMap: {},
  92. confirming: false,
  93. completing: false,
  94. purchaseTasks: [],
  95. executionTasks: [],
  96. cartCount: 0
  97. }
  98. },
  99. watch: {
  100. planId: function(val) {
  101. if (val) {
  102. this.loadOptions()
  103. }
  104. }
  105. },
  106. mounted: function() {
  107. if (this.planId) {
  108. this.loadOptions()
  109. }
  110. this.refreshCartCount()
  111. },
  112. methods: {
  113. loadOptions: function() {
  114. var self = this
  115. if (!self.planId || self.loading) return
  116. self.loading = true
  117. self.loadError = ''
  118. getHealthPlanProductOptions({ planId: self.planId }).then(function(res) {
  119. self.loading = false
  120. if (res && res.code === 200 && res.data) {
  121. var groups = res.data instanceof Array ? res.data : []
  122. self.groups = groups
  123. self.defaultSelect(groups)
  124. } else {
  125. self.loadError = (res && res.message) || '产品推荐加载失败'
  126. }
  127. }).catch(function() {
  128. self.loading = false
  129. self.loadError = '产品推荐加载失败,请稍后重试'
  130. })
  131. },
  132. defaultSelect: function(groups) {
  133. var map = {}
  134. for (var i = 0; i < groups.length; i++) {
  135. var options = (groups[i] && groups[i].options) || []
  136. map[groups[i].category] = options.length > 0 ? 0 : -1
  137. }
  138. this.selectedMap = map
  139. },
  140. // Vue 2 无法侦测对象新增属性,整体替换 selectedMap 保证视图刷新
  141. selectOption: function(category, index) {
  142. var map = {}
  143. for (var key in this.selectedMap) {
  144. if (Object.prototype.hasOwnProperty.call(this.selectedMap, key)) {
  145. map[key] = this.selectedMap[key]
  146. }
  147. }
  148. map[category] = index
  149. this.selectedMap = map
  150. },
  151. buildSelections: function() {
  152. var out = []
  153. for (var i = 0; i < this.groups.length; i++) {
  154. var group = this.groups[i]
  155. var options = (group && group.options) || []
  156. var idx = this.selectedMap[group.category]
  157. if (idx === undefined || idx === null || idx < 0 || idx >= options.length) continue
  158. var option = options[idx]
  159. out.push({
  160. category: group.category,
  161. productId: option.productId,
  162. name: option.name,
  163. quantity: option.quantity || 1
  164. })
  165. }
  166. return out
  167. },
  168. confirmSelection: function() {
  169. var self = this
  170. if (self.confirming) return
  171. var selections = self.buildSelections()
  172. if (selections.length === 0) {
  173. uni.showToast({ title: '请为每类产品选择一项', icon: 'none' })
  174. return
  175. }
  176. self.confirming = true
  177. selectHealthPlanProducts({ planId: self.planId, selections: selections }).then(function(res) {
  178. self.confirming = false
  179. if (res && res.code === 200 && res.data) {
  180. self.purchaseTasks = res.data.purchaseTasks || []
  181. var added = res.data.cartAddCount || 0
  182. self.cartCount = added
  183. uni.showToast({
  184. title: added > 0 ? '已加入购物车,请完成购买' : '购买任务已生成',
  185. icon: 'none',
  186. duration: 2000
  187. })
  188. self.$emit('purchase-created', res.data)
  189. } else {
  190. uni.showToast({ title: (res && res.message) || '生成购买任务失败', icon: 'none' })
  191. }
  192. }).catch(function() {
  193. self.confirming = false
  194. uni.showToast({ title: '请求失败', icon: 'none' })
  195. })
  196. },
  197. completePurchase: function() {
  198. var self = this
  199. if (self.completing) return
  200. self.completing = true
  201. uni.showLoading({ title: '生成执行任务...' })
  202. completeHealthPlanPurchase({ planId: self.planId }).then(function(res) {
  203. uni.hideLoading()
  204. self.completing = false
  205. if (res && res.code === 200 && res.data) {
  206. self.executionTasks = res.data.executionTasks || []
  207. for (var i = 0; i < self.purchaseTasks.length; i++) {
  208. self.purchaseTasks[i].status = 'completed'
  209. }
  210. uni.showToast({
  211. title: '执行任务已生成(' + (res.data.executionTaskCount || 0) + ' 项)',
  212. icon: 'none',
  213. duration: 2000
  214. })
  215. self.$emit('purchase-complete', res.data)
  216. } else {
  217. uni.showToast({ title: (res && res.message) || '生成执行任务失败', icon: 'none' })
  218. }
  219. }).catch(function() {
  220. uni.hideLoading()
  221. self.completing = false
  222. uni.showToast({ title: '请求失败', icon: 'none' })
  223. })
  224. },
  225. refreshCartCount: function() {
  226. var self = this
  227. getCartCount().then(function(res) {
  228. if (res && res.code === 200) {
  229. self.cartCount = res.data || 0
  230. }
  231. }).catch(function() {})
  232. },
  233. goCart: function() {
  234. uni.navigateTo({ url: '/pages/shop/cart/cart' })
  235. }
  236. }
  237. }
  238. </script>
  239. <style scoped lang="scss">
  240. .product-flow {
  241. margin-top: 24rpx;
  242. padding: 24rpx;
  243. background: #ffffff;
  244. border-radius: 16rpx;
  245. }
  246. .product-flow-head {
  247. display: flex;
  248. align-items: flex-start;
  249. justify-content: space-between;
  250. }
  251. .product-flow-head-left {
  252. display: flex;
  253. flex-direction: column;
  254. flex: 1;
  255. }
  256. .product-flow-title {
  257. font-size: 30rpx;
  258. font-weight: 600;
  259. color: #1f2937;
  260. }
  261. .product-flow-sub {
  262. margin-top: 6rpx;
  263. font-size: 22rpx;
  264. color: #6b7280;
  265. }
  266. .product-cart-badge {
  267. padding: 6rpx 16rpx;
  268. font-size: 22rpx;
  269. color: #ffffff;
  270. background: #f97316;
  271. border-radius: 999rpx;
  272. }
  273. .product-hint {
  274. padding: 32rpx 0;
  275. font-size: 24rpx;
  276. color: #6b7280;
  277. text-align: center;
  278. }
  279. .product-hint-error {
  280. color: #ef4444;
  281. }
  282. .product-groups {
  283. margin-top: 16rpx;
  284. }
  285. .product-group {
  286. margin-bottom: 24rpx;
  287. }
  288. .product-group-head {
  289. display: flex;
  290. align-items: center;
  291. justify-content: space-between;
  292. margin-bottom: 12rpx;
  293. }
  294. .product-group-title {
  295. font-size: 26rpx;
  296. font-weight: 600;
  297. color: #374151;
  298. }
  299. .product-group-count {
  300. font-size: 22rpx;
  301. color: #9ca3af;
  302. }
  303. .product-option {
  304. display: flex;
  305. align-items: center;
  306. justify-content: space-between;
  307. padding: 20rpx;
  308. margin-bottom: 12rpx;
  309. background: #f9fafb;
  310. border: 2rpx solid #e5e7eb;
  311. border-radius: 12rpx;
  312. }
  313. .product-option-on {
  314. background: #fff7ed;
  315. border-color: #f97316;
  316. }
  317. .product-option-body {
  318. display: flex;
  319. flex-direction: column;
  320. flex: 1;
  321. margin-right: 16rpx;
  322. }
  323. .product-option-name {
  324. font-size: 26rpx;
  325. color: #1f2937;
  326. }
  327. .product-option-tags {
  328. display: flex;
  329. align-items: center;
  330. margin-top: 8rpx;
  331. }
  332. .product-tag {
  333. margin-right: 12rpx;
  334. padding: 4rpx 12rpx;
  335. font-size: 20rpx;
  336. border-radius: 6rpx;
  337. }
  338. .tag-effect {
  339. color: #2563eb;
  340. background: #eff6ff;
  341. }
  342. .tag-price {
  343. color: #dc2626;
  344. background: #fef2f2;
  345. }
  346. .tag-external {
  347. color: #6b7280;
  348. background: #f3f4f6;
  349. }
  350. .product-option-reason {
  351. margin-top: 8rpx;
  352. font-size: 22rpx;
  353. color: #9ca3af;
  354. }
  355. .product-radio {
  356. width: 32rpx;
  357. height: 32rpx;
  358. border: 2rpx solid #d1d5db;
  359. border-radius: 50%;
  360. }
  361. .product-radio-on {
  362. border-color: #f97316;
  363. background: #f97316;
  364. }
  365. .product-actions {
  366. display: flex;
  367. align-items: center;
  368. margin-top: 8rpx;
  369. }
  370. .product-btn {
  371. flex: 1;
  372. font-size: 26rpx;
  373. border-radius: 12rpx;
  374. }
  375. .product-btn-primary {
  376. color: #ffffff;
  377. background: #f97316;
  378. }
  379. .product-btn-ghost {
  380. color: #f97316;
  381. background: #fff7ed;
  382. border: 2rpx solid #f97316;
  383. }
  384. .purchase-block {
  385. margin-top: 24rpx;
  386. padding-top: 24rpx;
  387. border-top: 2rpx solid #f3f4f6;
  388. }
  389. .purchase-head {
  390. display: flex;
  391. align-items: baseline;
  392. justify-content: space-between;
  393. }
  394. .purchase-title {
  395. font-size: 28rpx;
  396. font-weight: 600;
  397. color: #1f2937;
  398. }
  399. .purchase-sub {
  400. font-size: 22rpx;
  401. color: #9ca3af;
  402. }
  403. .purchase-item {
  404. display: flex;
  405. align-items: center;
  406. justify-content: space-between;
  407. padding: 16rpx 0;
  408. }
  409. .purchase-item-main {
  410. display: flex;
  411. flex-direction: column;
  412. flex: 1;
  413. margin-right: 16rpx;
  414. }
  415. .purchase-item-name {
  416. font-size: 26rpx;
  417. color: #374151;
  418. }
  419. .purchase-item-tip {
  420. margin-top: 4rpx;
  421. font-size: 22rpx;
  422. color: #9ca3af;
  423. }
  424. .purchase-item-status {
  425. font-size: 22rpx;
  426. color: #f97316;
  427. }
  428. .purchase-item-status-done {
  429. color: #10b981;
  430. }
  431. .purchase-actions {
  432. display: flex;
  433. align-items: center;
  434. margin-top: 16rpx;
  435. }
  436. .execution-block {
  437. margin-top: 24rpx;
  438. padding-top: 24rpx;
  439. border-top: 2rpx solid #f3f4f6;
  440. }
  441. .execution-head {
  442. display: flex;
  443. align-items: baseline;
  444. justify-content: space-between;
  445. }
  446. .execution-item {
  447. display: flex;
  448. align-items: center;
  449. justify-content: space-between;
  450. padding: 14rpx 0;
  451. }
  452. .execution-item-name {
  453. flex: 1;
  454. font-size: 26rpx;
  455. color: #374151;
  456. margin-right: 16rpx;
  457. }
  458. .execution-item-cat {
  459. font-size: 22rpx;
  460. color: #6b7280;
  461. }
  462. .execution-tip {
  463. margin-top: 12rpx;
  464. font-size: 22rpx;
  465. color: #9ca3af;
  466. }
  467. </style>