meal-config.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. <template>
  2. <view class="meal-config-page">
  3. <scroll-view class="content" scroll-y>
  4. <!-- 日期切换:两段式胶囊 tabs -->
  5. <view class="date-tabs">
  6. <view class="tab-item" :class="{'tab-active': dateType === 'weekday'}" @tap="switchDateType('weekday')">
  7. <text class="tab-text">工作日</text>
  8. </view>
  9. <view class="tab-item" :class="{'tab-active': dateType === 'weekend'}" @tap="switchDateType('weekend')">
  10. <text class="tab-text">周末</text>
  11. </view>
  12. </view>
  13. <!-- 添加共餐人入口 -->
  14. <view class="add-member-entry" @tap="goManageMembers">
  15. <text class="add-member-entry-icon">+</text>
  16. <text class="add-member-entry-text">添加共餐人</text>
  17. <text class="add-member-entry-arrow">›</text>
  18. </view>
  19. <!-- 餐次配置 -->
  20. <view class="meal-section" v-for="meal in mealGroups" :key="meal.value">
  21. <view class="meal-header">
  22. <view class="meal-icon-wrap" :class="'meal-icon-' + meal.value"><text class="meal-icon">{{ meal.icon }}</text></view>
  23. <text class="meal-name">{{ meal.label }}</text>
  24. <text class="meal-count" v-if="meal.selectedCount > 0">已选 {{ meal.selectedCount }} 人</text>
  25. </view>
  26. <view class="member-list">
  27. <view class="member-item" v-for="member in meal.members" :key="member.id"
  28. :class="{'member-selected': member.selected}"
  29. @tap="toggleMember(meal.value, member.id)">
  30. <view class="member-check" v-if="member.selected">✓</view>
  31. <text class="member-avatar" :class="member._avatarCls">{{ member.nickname ? member.nickname.substring(0, 1) : '?' }}</text>
  32. <text class="member-name">{{ member.nickname || '未知' }}</text>
  33. </view>
  34. <view class="member-empty" v-if="meal.members.length === 0">
  35. <text class="member-empty-text">暂无可选的共餐人</text>
  36. </view>
  37. </view>
  38. </view>
  39. <!-- 提示条 -->
  40. <view class="tip-card">
  41. <text class="tip-text">💡 提示:选择与您在{{ dateType === 'weekday' ? '工作日' : '周末' }}共餐的家庭成员</text>
  42. </view>
  43. <!-- 保存按钮 -->
  44. <view class="save-area">
  45. <view class="save-btn" @tap="saveConfig">
  46. <text>{{ saving ? '保存中...' : '保存配置' }}</text>
  47. </view>
  48. </view>
  49. </scroll-view>
  50. </view>
  51. </template>
  52. <script>
  53. import config from '@/config.js'
  54. import { getMealConfig, saveMealConfig } from '@/utils/api.js'
  55. export default {
  56. data() {
  57. return {
  58. dateType: 'weekday',
  59. familyMembers: [],
  60. mealTypes: [
  61. { label: '早餐', value: 'breakfast', icon: '🌅' },
  62. { label: '午餐', value: 'lunch', icon: '☀️' },
  63. { label: '晚餐', value: 'dinner', icon: '🌙' }
  64. ],
  65. mealConfigs: {},
  66. saving: false
  67. }
  68. },
  69. computed: {
  70. // 将「餐次 × 成员」组装为可直接渲染的列表,成员带 selected 标记(避免模板内方法调用)
  71. mealGroups: function() {
  72. var self = this
  73. return this.mealTypes.map(function(meal) {
  74. var config = self.mealConfigs[meal.value]
  75. var ids = []
  76. if (config && config.participantMemberIds) {
  77. try {
  78. ids = JSON.parse(config.participantMemberIds)
  79. } catch (e) {
  80. ids = []
  81. }
  82. }
  83. var members = self.familyMembers.map(function(m) {
  84. return {
  85. id: m.id,
  86. nickname: m.nickname,
  87. selected: ids.indexOf(m.id) >= 0,
  88. _avatarCls: m.gender === 'male' ? 'member-avatar-male' : (m.gender === 'female' ? 'member-avatar-female' : 'member-avatar-unknown')
  89. }
  90. })
  91. return {
  92. value: meal.value,
  93. label: meal.label,
  94. icon: meal.icon,
  95. members: members,
  96. selectedCount: ids.length
  97. }
  98. })
  99. }
  100. },
  101. onLoad: function() {
  102. this.loadMembers()
  103. this.loadConfigs()
  104. },
  105. methods: {
  106. goBack: function() {
  107. uni.navigateBack()
  108. },
  109. loadMembers: function() {
  110. var self = this
  111. var token = uni.getStorageSync('token')
  112. if (!token) return
  113. uni.request({
  114. url: this.getApiUrl('/api/family/member/list'),
  115. method: 'POST',
  116. data: {},
  117. header: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + token },
  118. success: function(res) {
  119. if (res.data && res.data.code === 200 && res.data.data) {
  120. self.familyMembers = res.data.data
  121. }
  122. }
  123. })
  124. },
  125. loadConfigs: function() {
  126. var self = this
  127. this.mealTypes.forEach(function(meal) {
  128. getMealConfig({ date_type: self.dateType, meal_type: meal.value }).then(function(config) {
  129. if (config) {
  130. self.mealConfigs[meal.value] = config
  131. }
  132. })
  133. })
  134. },
  135. switchDateType: function(type) {
  136. this.dateType = type
  137. this.loadConfigs()
  138. },
  139. toggleMember: function(mealType, memberId) {
  140. var config = this.mealConfigs[mealType] || {}
  141. var ids = []
  142. if (config.participantMemberIds) {
  143. try {
  144. ids = JSON.parse(config.participantMemberIds)
  145. } catch (e) {}
  146. }
  147. var index = ids.indexOf(memberId)
  148. if (index >= 0) {
  149. ids.splice(index, 1)
  150. } else {
  151. ids.push(memberId)
  152. }
  153. this.mealConfigs[mealType] = Object.assign({}, config, { participantMemberIds: JSON.stringify(ids) })
  154. },
  155. goManageMembers: function() {
  156. uni.navigateTo({ url: '/pages/profile-extra/family-members' })
  157. },
  158. saveConfig: function() {
  159. var self = this
  160. this.saving = true
  161. var promises = this.mealTypes.map(function(meal) {
  162. var config = self.mealConfigs[meal.value] || {}
  163. return saveMealConfig({
  164. date_type: self.dateType,
  165. meal_type: meal.value,
  166. participant_member_ids: JSON.stringify(config.participantMemberIds || '[]')
  167. })
  168. })
  169. Promise.all(promises).then(function() {
  170. self.saving = false
  171. uni.showToast({ title: '保存成功', icon: 'success' })
  172. }).catch(function() {
  173. self.saving = false
  174. uni.showToast({ title: '保存失败', icon: 'none' })
  175. })
  176. },
  177. getApiUrl: function(path) {
  178. return config.api(path)
  179. }
  180. }
  181. }
  182. </script>
  183. <style scoped>
  184. /* ===== 页面令牌(暖灶·家的味道) ===== */
  185. .meal-config-page {
  186. min-height: 100vh;
  187. background: linear-gradient(180deg, #FFF7ED 0%, #FFF3E6 100%);
  188. overflow: hidden;
  189. }
  190. .content {
  191. padding: 30rpx;
  192. }
  193. .date-tabs {
  194. display: flex; background: #FFF1E6; border-radius: 999rpx;
  195. padding: 6rpx; margin-bottom: 30rpx;
  196. }
  197. .tab-item {
  198. flex: 1; text-align: center; padding: 18rpx 0;
  199. border-radius: 999rpx; font-size: 28rpx; color: #C2410C;
  200. }
  201. .tab-active {
  202. background: linear-gradient(135deg, #FF8C42, #FFB366);
  203. color: #FFFFFF; font-weight: 600;
  204. box-shadow: 0 4rpx 12rpx rgba(255, 140, 66, 0.3);
  205. }
  206. .meal-section {
  207. background-color: #FFFFFF;
  208. border-radius: 20rpx;
  209. margin-bottom: 30rpx;
  210. overflow: hidden;
  211. box-shadow: 0 2rpx 12rpx rgba(255, 140, 66, 0.10);
  212. }
  213. .meal-header {
  214. display: flex;
  215. align-items: center;
  216. padding: 24rpx 30rpx;
  217. border-bottom: 1rpx solid #FDE8D0;
  218. }
  219. .meal-icon-wrap { width: 72rpx; height: 72rpx; border-radius: 22rpx; display: flex; align-items: center; justify-content: center; margin-right: 16rpx; }
  220. .meal-icon-breakfast { background: linear-gradient(135deg, #FFB366, #FFD48A); }
  221. .meal-icon-lunch { background: linear-gradient(135deg, #FBBF24, #FCD34D); }
  222. .meal-icon-dinner { background: linear-gradient(135deg, #A78BFA, #C4B5FD); }
  223. .meal-count { font-size: 24rpx; color: #C2410C; }
  224. .meal-name {
  225. font-size: 28rpx;
  226. font-weight: 600;
  227. color: #333;
  228. }
  229. .member-list {
  230. display: flex;
  231. flex-wrap: wrap;
  232. gap: 16rpx;
  233. padding: 24rpx 30rpx;
  234. }
  235. .member-item {
  236. box-sizing: border-box; position: relative;
  237. display: flex; flex-direction: column; align-items: center; justify-content: center;
  238. padding: 16rpx 20rpx; border: 2rpx solid #E0E0E0; border-radius: 20rpx;
  239. background: #FAFAFA; min-width: 132rpx;
  240. transition: all 0.15s ease;
  241. }
  242. .member-selected {
  243. border-color: #FF8C42; border-width: 3rpx;
  244. background: #FFF5EB; box-shadow: 0 0 0 4rpx rgba(255, 140, 66, 0.18);
  245. }
  246. .member-check {
  247. position: absolute;
  248. top: -10rpx;
  249. right: -10rpx;
  250. width: 36rpx;
  251. height: 36rpx;
  252. border-radius: 50%;
  253. background-color: #FF8C42;
  254. color: #FFFFFF;
  255. font-size: 22rpx;
  256. font-weight: 700;
  257. display: flex;
  258. align-items: center;
  259. justify-content: center;
  260. z-index: 2;
  261. }
  262. .member-avatar {
  263. width: 60rpx; height: 60rpx; border-radius: 50%;
  264. color: #FFFFFF; display: flex; align-items: center; justify-content: center;
  265. font-size: 24rpx; font-weight: 600; margin-bottom: 8rpx;
  266. }
  267. .member-avatar-male { background: linear-gradient(135deg, #5B9BD5, #8FC5E8); }
  268. .member-avatar-female { background: linear-gradient(135deg, #F472B6, #F9A8D4); }
  269. .member-avatar-unknown{ background: linear-gradient(135deg, #FF8C42, #FFB366); }
  270. .member-name {
  271. font-size: 22rpx;
  272. color: #666;
  273. max-width: 120rpx;
  274. overflow: hidden;
  275. text-overflow: ellipsis;
  276. white-space: nowrap;
  277. }
  278. .member-selected .member-name {
  279. color: #FF8C42;
  280. font-weight: 600;
  281. }
  282. .member-empty {
  283. width: 100%;
  284. padding: 20rpx 0;
  285. }
  286. .member-empty-text {
  287. font-size: 24rpx;
  288. color: #999;
  289. }
  290. .add-member-entry {
  291. box-sizing: border-box;
  292. display: flex;
  293. align-items: center;
  294. justify-content: center;
  295. background-color: #FFFFFF;
  296. border: 2rpx dashed #FF8C42;
  297. border-radius: 20rpx;
  298. padding: 24rpx 30rpx;
  299. margin-bottom: 30rpx;
  300. }
  301. .add-member-entry-icon {
  302. font-size: 30rpx;
  303. color: #FF8C42;
  304. margin-right: 12rpx;
  305. }
  306. .add-member-entry-text {
  307. font-size: 28rpx;
  308. color: #FF8C42;
  309. font-weight: 600;
  310. }
  311. .add-member-entry-arrow {
  312. font-size: 30rpx;
  313. color: #FF8C42;
  314. margin-left: 8rpx;
  315. }
  316. .tip-card { background: #FFF1E6; border-radius: 16rpx; padding: 24rpx 30rpx; margin-bottom: 30rpx; }
  317. .tip-text { font-size: 24rpx; color: #C2410C; line-height: 1.6; }
  318. .save-area {
  319. padding: 40rpx 30rpx;
  320. }
  321. .save-btn {
  322. background: linear-gradient(135deg, #FF8C42, #FFB366);
  323. color: #FFFFFF; text-align: center; padding: 28rpx 0; border-radius: 999rpx;
  324. font-size: 30rpx; font-weight: 600;
  325. box-shadow: 0 6rpx 20rpx rgba(255, 140, 66, 0.25);
  326. }
  327. </style>