TaskBelongingPicker.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. <template>
  2. <!-- 任务归属选择组件 -->
  3. <view>
  4. <!-- 触发区域 -->
  5. <slot name="trigger" :openPicker="openPicker" :value="displayValue">
  6. <view class="picker-trigger" @click="openPicker">
  7. <view class="trigger-label">{{ label }}</view>
  8. <view class="trigger-value" :class="{ placeholder: !displayValue }">
  9. {{ displayValue || placeholder }}
  10. </view>
  11. <image class="trigger-icon" src="/static/image/icon_more.png"></image>
  12. </view>
  13. </slot>
  14. <!-- 选择器弹出层 -->
  15. <u-popup :show="visible" mode="bottom" closeable round="20" @close="closePicker" :safeAreaInsetBottom="true"
  16. :zIndex="1001">
  17. <view class="task-belonging-picker">
  18. <!-- 标题栏 -->
  19. <view class="picker-header">
  20. <view class="picker-title">{{ title }}</view>
  21. <view class="picker-close" @click="closePicker">
  22. <text class="close-icon">×</text>
  23. </view>
  24. </view>
  25. <!-- 选项卡区域 -->
  26. <view class="tab-container">
  27. <scroll-view class="tab-scroll" scroll-x :scroll-left="scrollLeft">
  28. <view class="tab-list">
  29. <view v-for="(tab, index) in tabs" :key="index" class="tab-item"
  30. :class="{ active: currentTab === index }" @click="switchTab(index)">
  31. <text class="tab-text">{{ getTabTitle(tab, index) }}</text>
  32. <view v-if="currentTab === index" class="tab-indicator"></view>
  33. </view>
  34. </view>
  35. </scroll-view>
  36. </view>
  37. <!-- 列表区域 -->
  38. <scroll-view class="list-container" scroll-y :scroll-top="scrollTop">
  39. <view class="option-list">
  40. <view v-for="(item, index) in currentOptions" :key="item.id || index" class="option-item"
  41. :class="{
  42. selected: isOptionSelected(item),
  43. disabled: item.disabled
  44. }" @click="!item.disabled && selectOption(item)">
  45. <view class="option-content">
  46. <text class="option-name">{{ item.name }}</text>
  47. <view v-if="hasChildren(item)" class="option-arrow">
  48. <text class="arrow-icon">›</text>
  49. </view>
  50. </view>
  51. <view v-if="isOptionSelected(item)" class="option-selected">
  52. <text class="selected-icon">✓</text>
  53. </view>
  54. </view>
  55. </view>
  56. <!-- 空状态 -->
  57. <view v-if="currentOptions.length === 0" class="empty-state">
  58. <image class="empty-icon" src="/static/image/empty.png"></image>
  59. <text class="empty-text">暂无数据</text>
  60. </view>
  61. </scroll-view>
  62. <!-- 确定按钮 -->
  63. <view class="picker-footer">
  64. <view class="confirm-button" @click="confirmSelection">
  65. {{ confirmText }}
  66. </view>
  67. </view>
  68. </view>
  69. </u-popup>
  70. </view>
  71. </template>
  72. <script>
  73. export default {
  74. name: 'TaskBelongingPicker',
  75. props: {
  76. // 是否显示弹出层
  77. show: {
  78. type: Boolean,
  79. default: false
  80. },
  81. // 标题
  82. title: {
  83. type: String,
  84. default: '任务归属'
  85. },
  86. // 标签文本
  87. label: {
  88. type: String,
  89. default: '任务归属'
  90. },
  91. // 占位符
  92. placeholder: {
  93. type: String,
  94. default: '请选择任务归属'
  95. },
  96. // 确认按钮文本
  97. confirmText: {
  98. type: String,
  99. default: '确定'
  100. },
  101. // 是否必填
  102. required: {
  103. type: Boolean,
  104. default: true
  105. },
  106. // 初始选中的值
  107. value: {
  108. type: [Array, String],
  109. default: () => []
  110. },
  111. // 数据源
  112. data: {
  113. type: Array,
  114. default: () => []
  115. },
  116. // 最大层级
  117. maxLevel: {
  118. type: Number,
  119. default: 7
  120. },
  121. // 字段映射
  122. fieldNames: {
  123. type: Object,
  124. default: () => ({
  125. label: 'name',
  126. value: 'id',
  127. children: 'children'
  128. })
  129. }
  130. },
  131. data() {
  132. return {
  133. visible: false,
  134. currentTab: 0,
  135. selectedPath: [], // 选中的路径
  136. tabs: [], // 选项卡数据
  137. scrollLeft: 0,
  138. scrollTop: 0,
  139. tempSelectedPath: [] // 临时选择的路径(用于取消时恢复)
  140. }
  141. },
  142. computed: {
  143. // 显示的值
  144. displayValue() {
  145. if (typeof this.value === 'string') {
  146. return this.value
  147. }
  148. if (Array.isArray(this.value) && this.value.length > 0) {
  149. return this.value.map(item => item[this.fieldNames.label]).join(' / ')
  150. }
  151. return ''
  152. },
  153. // 当前选项卡对应的选项列表
  154. currentOptions() {
  155. if (this.selectedPath.length === 0) {
  156. return this.data
  157. }
  158. // 根据当前选中的路径获取子级选项
  159. let current = this.data
  160. for (let i = 0; i < this.currentTab; i++) {
  161. if (this.selectedPath[i] && current) {
  162. const selectedItem = this.selectedPath[i]
  163. const childrenKey = this.fieldNames.children
  164. current = selectedItem[childrenKey] || []
  165. }
  166. }
  167. return current || []
  168. }
  169. },
  170. watch: {
  171. show: {
  172. immediate: true,
  173. handler(newVal) {
  174. this.visible = newVal
  175. if (newVal) {
  176. this.initPicker()
  177. }
  178. }
  179. },
  180. value: {
  181. deep: true,
  182. handler(newVal) {
  183. this.initSelectedPath(newVal)
  184. }
  185. },
  186. data: {
  187. deep: true,
  188. handler() {
  189. if (this.visible) {
  190. this.initTabs()
  191. }
  192. }
  193. }
  194. },
  195. created() {
  196. this.initSelectedPath(this.value)
  197. },
  198. methods: {
  199. // 初始化选择器
  200. initPicker() {
  201. this.initTabs()
  202. this.initSelectedPath(this.value)
  203. this.tempSelectedPath = [...this.selectedPath]
  204. this.currentTab = Math.max(this.selectedPath.length - 1, 0)
  205. this.scrollToTab(this.currentTab)
  206. },
  207. // 初始化选项卡
  208. initTabs() {
  209. const defaultTabs = [
  210. '中药事业部',
  211. '事业1组',
  212. '学术研究部',
  213. '湖南省药学服务公司',
  214. '中药事业部',
  215. '中药事业部',
  216. '中药事业部'
  217. ]
  218. this.tabs = []
  219. for (let i = 0; i < this.maxLevel; i++) {
  220. this.tabs.push({
  221. title: defaultTabs[i] || `层级${i + 1}`,
  222. level: i
  223. })
  224. }
  225. },
  226. // 初始化选中的路径
  227. initSelectedPath(value) {
  228. if (typeof value === 'string') {
  229. // 如果是字符串,解析成数组
  230. this.selectedPath = []
  231. } else if (Array.isArray(value)) {
  232. this.selectedPath = [...value]
  233. } else {
  234. this.selectedPath = []
  235. }
  236. },
  237. // 获取选项卡标题
  238. getTabTitle(tab, index) {
  239. if (this.selectedPath[index]) {
  240. return this.selectedPath[index][this.fieldNames.label]
  241. }
  242. return tab.title
  243. },
  244. // 打开选择器
  245. openPicker() {
  246. this.visible = true
  247. this.$emit('open')
  248. this.$nextTick(() => {
  249. this.initPicker()
  250. })
  251. },
  252. // 关闭选择器
  253. closePicker() {
  254. this.visible = false
  255. this.selectedPath = [...this.tempSelectedPath]
  256. this.$emit('close')
  257. this.$emit('update:show', false)
  258. },
  259. // 切换选项卡
  260. switchTab(index) {
  261. if (index < 0 || index >= this.tabs.length) return
  262. this.currentTab = index
  263. this.scrollToTab(index)
  264. this.scrollTop = 0
  265. },
  266. // 滚动到指定选项卡
  267. scrollToTab(index) {
  268. this.$nextTick(() => {
  269. // 简单计算滚动位置,实际可以更精确
  270. this.scrollLeft = index * 100
  271. })
  272. },
  273. // 判断是否有子级
  274. hasChildren(item) {
  275. const childrenKey = this.fieldNames.children
  276. return item[childrenKey] && item[childrenKey].length > 0
  277. },
  278. // 判断选项是否被选中
  279. isOptionSelected(item) {
  280. const currentSelected = this.selectedPath[this.currentTab]
  281. if (!currentSelected) return false
  282. const valueKey = this.fieldNames.value
  283. return currentSelected[valueKey] === item[valueKey]
  284. },
  285. // 选择选项
  286. selectOption(item) {
  287. const valueKey = this.fieldNames.value
  288. const labelKey = this.fieldNames.label
  289. const childrenKey = this.fieldNames.children
  290. // 更新当前级别的选择
  291. this.selectedPath[this.currentTab] = {
  292. [valueKey]: item[valueKey],
  293. [labelKey]: item[labelKey]
  294. }
  295. // 复制子级数据(如果有)
  296. if (item[childrenKey]) {
  297. Object.assign(this.selectedPath[this.currentTab], {
  298. [childrenKey]: item[childrenKey]
  299. })
  300. }
  301. // 清空后续级别的选择
  302. for (let i = this.currentTab + 1; i < this.selectedPath.length; i++) {
  303. this.selectedPath[i] = null
  304. }
  305. // 如果有子级且未达到最大层级,自动切换到下一级
  306. if (this.hasChildren(item) && this.currentTab < this.tabs.length - 1) {
  307. setTimeout(() => {
  308. this.currentTab++
  309. this.scrollToTab(this.currentTab)
  310. }, 150)
  311. }
  312. // 触发选择事件
  313. this.$emit('select', {
  314. level: this.currentTab,
  315. item: item,
  316. path: this.getCurrentPath()
  317. })
  318. },
  319. // 获取当前选择的完整路径
  320. getCurrentPath() {
  321. return this.selectedPath.filter(item => item !== null)
  322. },
  323. // 确认选择
  324. confirmSelection() {
  325. const selectedPath = this.getCurrentPath()
  326. if (this.required && selectedPath.length === 0) {
  327. uni.showToast({
  328. title: '请选择任务归属',
  329. icon: 'none'
  330. })
  331. return
  332. }
  333. // 保存临时路径
  334. this.tempSelectedPath = [...selectedPath]
  335. // 构建显示文本
  336. const displayText = selectedPath
  337. .map(item => item[this.fieldNames.label])
  338. .join(' / ')
  339. this.visible = false
  340. // 触发确认事件
  341. this.$emit('confirm', {
  342. value: selectedPath,
  343. displayText: displayText
  344. })
  345. // 更新外部数据
  346. this.$emit('input', selectedPath)
  347. this.$emit('update:show', false)
  348. }
  349. }
  350. }
  351. </script>
  352. <style lang="scss" scoped>
  353. /* 触发区域样式 */
  354. .picker-trigger {
  355. display: flex;
  356. align-items: center;
  357. padding: 30rpx 0;
  358. border-bottom: 1px solid #EBEDF0;
  359. .trigger-label {
  360. font-size: 28rpx;
  361. color: #333;
  362. flex-shrink: 0;
  363. }
  364. .trigger-value {
  365. flex: 1;
  366. text-align: right;
  367. font-size: 28rpx;
  368. color: #333;
  369. padding-right: 16rpx;
  370. &.placeholder {
  371. color: #C8C9CC;
  372. }
  373. }
  374. .trigger-icon {
  375. width: 36rpx;
  376. height: 36rpx;
  377. }
  378. }
  379. /* 选择器弹出层样式 */
  380. .task-belonging-picker {
  381. height: 70vh;
  382. display: flex;
  383. flex-direction: column;
  384. background: #fff;
  385. }
  386. /* 头部样式 */
  387. .picker-header {
  388. display: flex;
  389. align-items: center;
  390. justify-content: space-between;
  391. padding: 32rpx 32rpx 24rpx;
  392. border-bottom: 1px solid #f0f0f0;
  393. }
  394. .picker-title {
  395. font-size: 32rpx;
  396. font-weight: 500;
  397. color: #333;
  398. }
  399. .picker-close {
  400. width: 48rpx;
  401. height: 48rpx;
  402. display: flex;
  403. align-items: center;
  404. justify-content: center;
  405. }
  406. .close-icon {
  407. font-size: 40rpx;
  408. color: #999;
  409. line-height: 1;
  410. }
  411. /* 选项卡样式 */
  412. .tab-container {
  413. border-bottom: 1px solid #f0f0f0;
  414. background: #fff;
  415. }
  416. .tab-scroll {
  417. width: 100%;
  418. white-space: nowrap;
  419. }
  420. .tab-list {
  421. display: inline-flex;
  422. padding: 0 32rpx;
  423. }
  424. .tab-item {
  425. position: relative;
  426. padding: 24rpx 0;
  427. margin-right: 40rpx;
  428. flex-shrink: 0;
  429. &:last-child {
  430. margin-right: 0;
  431. }
  432. &.active {
  433. .tab-text {
  434. color: #576B95;
  435. font-weight: 500;
  436. }
  437. }
  438. }
  439. .tab-text {
  440. font-size: 28rpx;
  441. color: #666;
  442. line-height: 40rpx;
  443. white-space: nowrap;
  444. }
  445. .tab-indicator {
  446. position: absolute;
  447. bottom: 0;
  448. left: 0;
  449. right: 0;
  450. height: 4rpx;
  451. background: #576B95;
  452. border-radius: 2rpx;
  453. }
  454. /* 列表区域样式 */
  455. .list-container {
  456. flex: 1;
  457. padding: 0 32rpx;
  458. }
  459. .option-list {
  460. padding: 24rpx 0;
  461. }
  462. .option-item {
  463. display: flex;
  464. align-items: center;
  465. justify-content: space-between;
  466. padding: 24rpx 0;
  467. border-bottom: 1px solid #f5f5f5;
  468. &:last-child {
  469. border-bottom: none;
  470. }
  471. &.selected {
  472. .option-name {
  473. color: #576B95;
  474. }
  475. }
  476. &.disabled {
  477. opacity: 0.5;
  478. pointer-events: none;
  479. }
  480. }
  481. .option-content {
  482. flex: 1;
  483. display: flex;
  484. align-items: center;
  485. justify-content: space-between;
  486. }
  487. .option-name {
  488. font-size: 28rpx;
  489. color: #333;
  490. }
  491. .option-arrow {
  492. margin-left: 16rpx;
  493. }
  494. .arrow-icon {
  495. font-size: 36rpx;
  496. color: #999;
  497. }
  498. .option-selected {
  499. margin-left: 16rpx;
  500. }
  501. .selected-icon {
  502. font-size: 32rpx;
  503. color: #576B95;
  504. font-weight: bold;
  505. }
  506. /* 空状态样式 */
  507. .empty-state {
  508. display: flex;
  509. flex-direction: column;
  510. align-items: center;
  511. justify-content: center;
  512. padding: 100rpx 0;
  513. .empty-icon {
  514. width: 200rpx;
  515. height: 200rpx;
  516. margin-bottom: 32rpx;
  517. opacity: 0.5;
  518. }
  519. .empty-text {
  520. font-size: 28rpx;
  521. color: #999;
  522. }
  523. }
  524. /* 底部按钮样式 */
  525. .picker-footer {
  526. padding: 24rpx 32rpx;
  527. border-top: 1px solid #f0f0f0;
  528. background: #fff;
  529. }
  530. .confirm-button {
  531. height: 80rpx;
  532. line-height: 80rpx;
  533. text-align: center;
  534. background: #576B95;
  535. border-radius: 40rpx;
  536. font-size: 28rpx;
  537. color: #fff;
  538. font-weight: 500;
  539. }
  540. </style>