| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475 |
- <template>
- <view class="product-flow">
- <view class="product-flow-head">
- <view class="product-flow-head-left">
- <text class="product-flow-title">🛒 产品多选一</text>
- <text class="product-flow-sub">每类产品按效果评分与价格对比,确认后生成购买任务</text>
- </view>
- <text v-if="cartCount > 0" class="product-cart-badge" @click="goCart">购物车 {{ cartCount }}</text>
- </view>
- <view v-if="loading" class="product-hint">正在加载产品推荐...</view>
- <view v-else-if="loadError" class="product-hint product-hint-error">{{ loadError }}</view>
- <view v-else class="product-groups">
- <view v-for="(g, gi) in groups" :key="gi" class="product-group">
- <view class="product-group-head">
- <text class="product-group-title">{{ g.categoryLabel }}</text>
- <text class="product-group-count">多选一</text>
- </view>
- <view v-for="(o, oi) in g.options" :key="oi" class="product-option"
- :class="{ 'product-option-on': selectedMap[g.category] === oi }"
- @click="selectOption(g.category, oi)">
- <view class="product-option-body">
- <text class="product-option-name">{{ o.name }}</text>
- <view class="product-option-tags">
- <text v-if="o.effectScore !== null && o.effectScore !== undefined" class="product-tag tag-effect">效果 {{ o.effectScore }} 分</text>
- <text v-if="o.priceYuan" class="product-tag tag-price">¥{{ o.priceYuan }}</text>
- <text v-if="!o.inPlatform" class="product-tag tag-external">自选渠道</text>
- </view>
- <text v-if="o.reason" class="product-option-reason">{{ o.reason }}</text>
- </view>
- <view class="product-radio" :class="{ 'product-radio-on': selectedMap[g.category] === oi }"></view>
- </view>
- </view>
- <view v-if="groups.length > 0 && purchaseTasks.length === 0" class="product-actions">
- <button class="product-btn product-btn-primary" :disabled="confirming" @click="confirmSelection">
- {{ confirming ? '生成中...' : '确认选择并生成购买任务' }}
- </button>
- </view>
- </view>
- <!-- 购买任务(R5) -->
- <view v-if="purchaseTasks.length > 0" class="purchase-block">
- <view class="purchase-head">
- <text class="purchase-title">购买任务</text>
- <text class="purchase-sub">本平台商品已加入购物车</text>
- </view>
- <view v-for="(t, ti) in purchaseTasks" :key="ti" class="purchase-item">
- <view class="purchase-item-main">
- <text class="purchase-item-name">{{ t.title }}</text>
- <text class="purchase-item-tip">{{ t.actionType === 'cart' ? '已加入购物车,去结算完成购买' : '线下/其他渠道购买' }}</text>
- </view>
- <text class="purchase-item-status" :class="{ 'purchase-item-status-done': t.status === 'completed' }">
- {{ t.status === 'completed' ? '已完成' : '待购买' }}
- </text>
- </view>
- <view class="purchase-actions">
- <button class="product-btn product-btn-ghost" @click="goCart">去购物车</button>
- <button class="product-btn product-btn-primary" :disabled="completing" @click="completePurchase">
- {{ completing ? '生成中...' : '我已完成购买' }}
- </button>
- </view>
- </view>
- <!-- 执行任务(R6) -->
- <view v-if="executionTasks.length > 0" class="execution-block">
- <view class="execution-head">
- <text class="purchase-title">执行任务(按方案建议)</text>
- <text class="purchase-sub">共 {{ executionTasks.length }} 项,购买任务完成后自动解锁</text>
- </view>
- <view v-for="(t, ti) in executionTasks" :key="ti" class="execution-item">
- <text class="execution-item-name">{{ t.title }}</text>
- <text v-if="t.category" class="execution-item-cat">{{ t.category }}</text>
- </view>
- <view class="execution-tip">可在「首页 - 任务」中查看并打卡</view>
- </view>
- </view>
- </template>
- <script>
- import { getHealthPlanProductOptions, selectHealthPlanProducts, completeHealthPlanPurchase, cartCount as getCartCount } from '@/utils/api.js'
- export default {
- name: 'ProductSelectionFlow',
- props: {
- planId: {
- type: [Number, String],
- default: null
- }
- },
- data: function() {
- return {
- loading: false,
- loadError: '',
- groups: [],
- // category -> 选中的选项下标(避免模板内调用带参方法)
- selectedMap: {},
- confirming: false,
- completing: false,
- purchaseTasks: [],
- executionTasks: [],
- cartCount: 0
- }
- },
- watch: {
- planId: function(val) {
- if (val) {
- this.loadOptions()
- }
- }
- },
- mounted: function() {
- if (this.planId) {
- this.loadOptions()
- }
- this.refreshCartCount()
- },
- methods: {
- loadOptions: function() {
- var self = this
- if (!self.planId || self.loading) return
- self.loading = true
- self.loadError = ''
- getHealthPlanProductOptions({ planId: self.planId }).then(function(res) {
- self.loading = false
- if (res && res.code === 200 && res.data) {
- var groups = res.data instanceof Array ? res.data : []
- self.groups = groups
- self.defaultSelect(groups)
- } else {
- self.loadError = (res && res.message) || '产品推荐加载失败'
- }
- }).catch(function() {
- self.loading = false
- self.loadError = '产品推荐加载失败,请稍后重试'
- })
- },
- defaultSelect: function(groups) {
- var map = {}
- for (var i = 0; i < groups.length; i++) {
- var options = (groups[i] && groups[i].options) || []
- map[groups[i].category] = options.length > 0 ? 0 : -1
- }
- this.selectedMap = map
- },
- // Vue 2 无法侦测对象新增属性,整体替换 selectedMap 保证视图刷新
- selectOption: function(category, index) {
- var map = {}
- for (var key in this.selectedMap) {
- if (Object.prototype.hasOwnProperty.call(this.selectedMap, key)) {
- map[key] = this.selectedMap[key]
- }
- }
- map[category] = index
- this.selectedMap = map
- },
- buildSelections: function() {
- var out = []
- for (var i = 0; i < this.groups.length; i++) {
- var group = this.groups[i]
- var options = (group && group.options) || []
- var idx = this.selectedMap[group.category]
- if (idx === undefined || idx === null || idx < 0 || idx >= options.length) continue
- var option = options[idx]
- out.push({
- category: group.category,
- productId: option.productId,
- name: option.name,
- quantity: option.quantity || 1
- })
- }
- return out
- },
- confirmSelection: function() {
- var self = this
- if (self.confirming) return
- var selections = self.buildSelections()
- if (selections.length === 0) {
- uni.showToast({ title: '请为每类产品选择一项', icon: 'none' })
- return
- }
- self.confirming = true
- selectHealthPlanProducts({ planId: self.planId, selections: selections }).then(function(res) {
- self.confirming = false
- if (res && res.code === 200 && res.data) {
- self.purchaseTasks = res.data.purchaseTasks || []
- var added = res.data.cartAddCount || 0
- self.cartCount = added
- uni.showToast({
- title: added > 0 ? '已加入购物车,请完成购买' : '购买任务已生成',
- icon: 'none',
- duration: 2000
- })
- self.$emit('purchase-created', res.data)
- } else {
- uni.showToast({ title: (res && res.message) || '生成购买任务失败', icon: 'none' })
- }
- }).catch(function() {
- self.confirming = false
- uni.showToast({ title: '请求失败', icon: 'none' })
- })
- },
- completePurchase: function() {
- var self = this
- if (self.completing) return
- self.completing = true
- uni.showLoading({ title: '生成执行任务...' })
- completeHealthPlanPurchase({ planId: self.planId }).then(function(res) {
- uni.hideLoading()
- self.completing = false
- if (res && res.code === 200 && res.data) {
- self.executionTasks = res.data.executionTasks || []
- for (var i = 0; i < self.purchaseTasks.length; i++) {
- self.purchaseTasks[i].status = 'completed'
- }
- uni.showToast({
- title: '执行任务已生成(' + (res.data.executionTaskCount || 0) + ' 项)',
- icon: 'none',
- duration: 2000
- })
- self.$emit('purchase-complete', res.data)
- } else {
- uni.showToast({ title: (res && res.message) || '生成执行任务失败', icon: 'none' })
- }
- }).catch(function() {
- uni.hideLoading()
- self.completing = false
- uni.showToast({ title: '请求失败', icon: 'none' })
- })
- },
- refreshCartCount: function() {
- var self = this
- getCartCount().then(function(res) {
- if (res && res.code === 200) {
- self.cartCount = res.data || 0
- }
- }).catch(function() {})
- },
- goCart: function() {
- uni.navigateTo({ url: '/pages/shop/cart/cart' })
- }
- }
- }
- </script>
- <style scoped lang="scss">
- .product-flow {
- margin-top: 24rpx;
- padding: 24rpx;
- background: #ffffff;
- border-radius: 16rpx;
- }
- .product-flow-head {
- display: flex;
- align-items: flex-start;
- justify-content: space-between;
- }
- .product-flow-head-left {
- display: flex;
- flex-direction: column;
- flex: 1;
- }
- .product-flow-title {
- font-size: 30rpx;
- font-weight: 600;
- color: #1f2937;
- }
- .product-flow-sub {
- margin-top: 6rpx;
- font-size: 22rpx;
- color: #6b7280;
- }
- .product-cart-badge {
- padding: 6rpx 16rpx;
- font-size: 22rpx;
- color: #ffffff;
- background: #f97316;
- border-radius: 999rpx;
- }
- .product-hint {
- padding: 32rpx 0;
- font-size: 24rpx;
- color: #6b7280;
- text-align: center;
- }
- .product-hint-error {
- color: #ef4444;
- }
- .product-groups {
- margin-top: 16rpx;
- }
- .product-group {
- margin-bottom: 24rpx;
- }
- .product-group-head {
- display: flex;
- align-items: center;
- justify-content: space-between;
- margin-bottom: 12rpx;
- }
- .product-group-title {
- font-size: 26rpx;
- font-weight: 600;
- color: #374151;
- }
- .product-group-count {
- font-size: 22rpx;
- color: #9ca3af;
- }
- .product-option {
- display: flex;
- align-items: center;
- justify-content: space-between;
- padding: 20rpx;
- margin-bottom: 12rpx;
- background: #f9fafb;
- border: 2rpx solid #e5e7eb;
- border-radius: 12rpx;
- }
- .product-option-on {
- background: #fff7ed;
- border-color: #f97316;
- }
- .product-option-body {
- display: flex;
- flex-direction: column;
- flex: 1;
- margin-right: 16rpx;
- }
- .product-option-name {
- font-size: 26rpx;
- color: #1f2937;
- }
- .product-option-tags {
- display: flex;
- align-items: center;
- margin-top: 8rpx;
- }
- .product-tag {
- margin-right: 12rpx;
- padding: 4rpx 12rpx;
- font-size: 20rpx;
- border-radius: 6rpx;
- }
- .tag-effect {
- color: #2563eb;
- background: #eff6ff;
- }
- .tag-price {
- color: #dc2626;
- background: #fef2f2;
- }
- .tag-external {
- color: #6b7280;
- background: #f3f4f6;
- }
- .product-option-reason {
- margin-top: 8rpx;
- font-size: 22rpx;
- color: #9ca3af;
- }
- .product-radio {
- width: 32rpx;
- height: 32rpx;
- border: 2rpx solid #d1d5db;
- border-radius: 50%;
- }
- .product-radio-on {
- border-color: #f97316;
- background: #f97316;
- }
- .product-actions {
- display: flex;
- align-items: center;
- margin-top: 8rpx;
- }
- .product-btn {
- flex: 1;
- font-size: 26rpx;
- border-radius: 12rpx;
- }
- .product-btn-primary {
- color: #ffffff;
- background: #f97316;
- }
- .product-btn-ghost {
- color: #f97316;
- background: #fff7ed;
- border: 2rpx solid #f97316;
- }
- .purchase-block {
- margin-top: 24rpx;
- padding-top: 24rpx;
- border-top: 2rpx solid #f3f4f6;
- }
- .purchase-head {
- display: flex;
- align-items: baseline;
- justify-content: space-between;
- }
- .purchase-title {
- font-size: 28rpx;
- font-weight: 600;
- color: #1f2937;
- }
- .purchase-sub {
- font-size: 22rpx;
- color: #9ca3af;
- }
- .purchase-item {
- display: flex;
- align-items: center;
- justify-content: space-between;
- padding: 16rpx 0;
- }
- .purchase-item-main {
- display: flex;
- flex-direction: column;
- flex: 1;
- margin-right: 16rpx;
- }
- .purchase-item-name {
- font-size: 26rpx;
- color: #374151;
- }
- .purchase-item-tip {
- margin-top: 4rpx;
- font-size: 22rpx;
- color: #9ca3af;
- }
- .purchase-item-status {
- font-size: 22rpx;
- color: #f97316;
- }
- .purchase-item-status-done {
- color: #10b981;
- }
- .purchase-actions {
- display: flex;
- align-items: center;
- margin-top: 16rpx;
- }
- .execution-block {
- margin-top: 24rpx;
- padding-top: 24rpx;
- border-top: 2rpx solid #f3f4f6;
- }
- .execution-head {
- display: flex;
- align-items: baseline;
- justify-content: space-between;
- }
- .execution-item {
- display: flex;
- align-items: center;
- justify-content: space-between;
- padding: 14rpx 0;
- }
- .execution-item-name {
- flex: 1;
- font-size: 26rpx;
- color: #374151;
- margin-right: 16rpx;
- }
- .execution-item-cat {
- font-size: 22rpx;
- color: #6b7280;
- }
- .execution-tip {
- margin-top: 12rpx;
- font-size: 22rpx;
- color: #9ca3af;
- }
- </style>
|