transfer-create.vue 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. <template>
  2. <view class="page-no-tab">
  3. <view class="form-intro">
  4. <ModuleIcon :icon="meta.icon" :color="meta.color" soft />
  5. <view>
  6. <text class="intro-title">{{ meta.title }}</text>
  7. <text class="intro-sub">{{ meta.subtitle }}</text>
  8. </view>
  9. </view>
  10. <view class="steps">
  11. <view class="step active"><view>1</view><text>填写申请</text></view>
  12. <view class="step-line"></view>
  13. <view class="step"><view>2</view><text>主管审批</text></view>
  14. <view class="step-line"></view>
  15. <view class="step"><view>3</view><text>完成</text></view>
  16. </view>
  17. <view class="form-card card section">
  18. <view class="field"><text class="label">申请人</text>
  19. <view class="readonly">
  20. <view class="avatar small-avatar">{{ currentUser.initials || '—' }}</view>
  21. <text>{{ currentUser.name }} · {{ currentUser.department }}</text>
  22. </view>
  23. </view>
  24. <view class="divider"></view>
  25. <view class="field"><text class="label">当前岗位</text>
  26. <text class="value">{{ currentUser.position || '—' }}</text>
  27. </view>
  28. <view class="divider"></view>
  29. <view class="field press" @click="chooseChangeType"><text class="label required">异动类型</text>
  30. <text :class="form.changeType ? 'value' : 'placeholder'">{{ changeTypeText }}</text>
  31. <text class="arrow">›</text>
  32. </view>
  33. <view class="divider"></view>
  34. <view class="field press" @click="chooseTargetDept"><text class="label">目标部门</text>
  35. <text :class="form.targetDeptName ? 'value' : 'placeholder'">{{ form.targetDeptName || '请选择' }}</text>
  36. <text class="arrow">›</text>
  37. </view>
  38. <view class="divider"></view>
  39. <view class="field press" @click="chooseTargetPosition"><text class="label required">目标岗位</text>
  40. <text :class="form.targetPositionName ? 'value' : 'placeholder'">{{ form.targetPositionName || '请选择' }}</text>
  41. <text class="arrow">›</text>
  42. </view>
  43. <template v-if="form.targetLevel">
  44. <view class="divider"></view>
  45. <view class="field"><text class="label">目标职级</text><text class="value">{{ form.targetLevel }}</text></view>
  46. </template>
  47. <view class="divider"></view>
  48. <view class="field press"><text class="label required">生效日期</text>
  49. <picker mode="date" :value="form.effectiveDate" :start="todayIso" @change="onEffectiveDateChange">
  50. <text :class="form.effectiveDate ? 'value' : 'placeholder'">{{ form.effectiveDate || '请选择' }}</text>
  51. </picker>
  52. <text class="arrow">›</text>
  53. </view>
  54. </view>
  55. <view class="section-title"><text>申请说明</text></view>
  56. <view class="form-card card section">
  57. <view class="field textarea-field"><text class="label required">调岗事由</text>
  58. <textarea v-model="form.reason" maxlength="200" placeholder="请说明调岗原因与工作安排" />
  59. <text class="counter">{{ form.reason.length }} / 200</text>
  60. </view>
  61. </view>
  62. <view class="form-card card section">
  63. <view class="field textarea-field"><text class="label">备注</text>
  64. <textarea v-model="form.remark" maxlength="200" placeholder="补充说明(选填)" />
  65. <text class="counter">{{ form.remark.length }} / 200</text>
  66. </view>
  67. </view>
  68. <view class="footer-tip muted">
  69. <text>{{ footerTip }}</text>
  70. </view>
  71. <view class="bottom-action">
  72. <view class="draft-btn press" @click="onSaveDraft">保存草稿</view>
  73. <view class="submit-btn press" @click="onSubmit">提交审批</view>
  74. </view>
  75. </view>
  76. </template>
  77. <script setup>
  78. import { computed, reactive, ref } from 'vue'
  79. import { onLoad } from '@dcloudio/uni-app'
  80. import ModuleIcon from '@/components/ModuleIcon.vue'
  81. import { useAuthGuard } from '@/hooks/useAuthGuard'
  82. import { useCurrentUser } from '@/hooks/useCurrentUser'
  83. import { applicationTypes } from '@/data/mock'
  84. import { listOrganizations } from '@/api/organization'
  85. import { pagePosition } from '@/api/position'
  86. import { buildOrganizationTree } from '@/utils/organization-tree'
  87. import { addApplication } from '@/utils/storage'
  88. import { getServerConfig } from '@/utils/auth'
  89. import { back } from '@/utils/router'
  90. import {
  91. ESS_CHANGE_TYPE_OPTIONS,
  92. canEditUserChange,
  93. changeTypeLabel,
  94. loadTransferDetail,
  95. submitTransferApplication,
  96. } from '@/api/userChange'
  97. useAuthGuard()
  98. const currentUser = useCurrentUser()
  99. const meta = computed(() => applicationTypes.transfer || { title: '调岗申请', subtitle: '', icon: '岗', color: '#9270ca' })
  100. const today = new Date()
  101. const todayIso = `${today.getFullYear()}-${String(today.getMonth() + 1).padStart(2, '0')}-${String(today.getDate()).padStart(2, '0')}`
  102. const form = reactive({
  103. id: null,
  104. userId: null,
  105. name: '',
  106. employeeNo: '',
  107. dept: '',
  108. position: '',
  109. level: '',
  110. changeType: 'POSITION_TRANSFER',
  111. targetDeptId: null,
  112. targetDeptName: '',
  113. targetPositionId: null,
  114. targetPositionName: '',
  115. targetLevel: '',
  116. effectiveDate: '',
  117. reason: '',
  118. remark: '',
  119. })
  120. const submitting = ref(false)
  121. const deptOptions = ref([])
  122. const positionOptions = ref([])
  123. const positionLoading = ref(false)
  124. const changeTypeText = computed(() => (form.changeType ? changeTypeLabel(form.changeType) : '请选择'))
  125. const summaryText = computed(() => {
  126. const parts = [changeTypeLabel(form.changeType)]
  127. if (form.targetDeptName) parts.push(form.targetDeptName)
  128. if (form.targetPositionName) parts.push(form.targetPositionName)
  129. if (form.effectiveDate) parts.push(`生效 ${form.effectiveDate}`)
  130. return parts.join(' · ')
  131. })
  132. const footerTip = computed(() =>
  133. getServerConfig().mode === 'server'
  134. ? '提交后由 BPM 流程驱动审批,可在「调岗申请」中查看审批状态。'
  135. : '演示模式:申请保存在本机,提交后可在「调岗申请」与「我的申请」中查看进度。',
  136. )
  137. // 法人节点只作为路径前缀,不作为可选的调动目标
  138. const LEGAL_TYPES = new Set(['10', '20', 'group', 'company'])
  139. function flattenDepartments(nodes = []) {
  140. const result = []
  141. const walk = (items, parentPath) => {
  142. items.forEach((node) => {
  143. const path = parentPath ? `${parentPath} / ${node.name}` : node.name
  144. if (!LEGAL_TYPES.has(String(node.type))) result.push({ id: node.id, name: node.name, path })
  145. walk(node.children || [], path)
  146. })
  147. }
  148. walk(nodes, '')
  149. return result
  150. }
  151. async function loadDepartments() {
  152. try {
  153. const list = await listOrganizations()
  154. deptOptions.value = flattenDepartments(buildOrganizationTree(list))
  155. } catch (error) {
  156. deptOptions.value = []
  157. }
  158. }
  159. async function loadPositions() {
  160. positionLoading.value = true
  161. try {
  162. const data = await pagePosition({ pageNum: 1, size: 50 })
  163. positionOptions.value = (data?.list || []).map((p) => ({
  164. id: p.id,
  165. name: p.positionName,
  166. deptId: p.deptId,
  167. deptName: p.deptName,
  168. level: p.positionLevel || '',
  169. }))
  170. } catch (error) {
  171. positionOptions.value = []
  172. } finally {
  173. positionLoading.value = false
  174. }
  175. }
  176. function syncOrigin() {
  177. const user = currentUser || {}
  178. form.userId = form.userId || user.id || null
  179. form.name = form.name || user.name || ''
  180. form.employeeNo = form.employeeNo || user.id || ''
  181. form.dept = form.dept || user.department || ''
  182. form.position = form.position || user.position || ''
  183. form.level = form.level || user.level || ''
  184. }
  185. function chooseChangeType() {
  186. uni.showActionSheet({
  187. itemList: ESS_CHANGE_TYPE_OPTIONS.map((item) => item.label),
  188. success: (r) => {
  189. const picked = ESS_CHANGE_TYPE_OPTIONS[r.tapIndex]
  190. if (picked) form.changeType = picked.value
  191. },
  192. })
  193. }
  194. function chooseTargetDept() {
  195. if (!deptOptions.value.length) {
  196. uni.showToast({ title: '部门数据加载中,请稍后重试', icon: 'none' })
  197. loadDepartments()
  198. return
  199. }
  200. uni.showActionSheet({
  201. itemList: deptOptions.value.map((item) => item.path),
  202. success: (r) => {
  203. const dept = deptOptions.value[r.tapIndex]
  204. if (!dept) return
  205. form.targetDeptId = dept.id
  206. form.targetDeptName = dept.name
  207. },
  208. })
  209. }
  210. async function chooseTargetPosition() {
  211. if (positionLoading.value) return
  212. if (!positionOptions.value.length) {
  213. await loadPositions()
  214. }
  215. if (!positionOptions.value.length) {
  216. uni.showToast({ title: '暂无岗位数据', icon: 'none' })
  217. return
  218. }
  219. uni.showActionSheet({
  220. itemList: positionOptions.value.map((item) => (item.deptName ? `${item.name}(${item.deptName})` : item.name)),
  221. success: (r) => {
  222. const position = positionOptions.value[r.tapIndex]
  223. if (!position) return
  224. form.targetPositionId = position.id
  225. form.targetPositionName = position.name
  226. // 目标岗位自带岗位层级时同步展示;与当前职级一致则不产生 GRADE 变动
  227. form.targetLevel = position.level || ''
  228. // 岗位归属部门即调动目标部门,避免部门与岗位不一致
  229. if (position.deptId != null && position.deptName) {
  230. form.targetDeptId = position.deptId
  231. form.targetDeptName = position.deptName
  232. }
  233. },
  234. })
  235. }
  236. function onEffectiveDateChange(e) {
  237. form.effectiveDate = e.detail.value
  238. }
  239. function validate() {
  240. if (!form.changeType) return '请选择异动类型'
  241. if (['DEPARTMENT_TRANSFER', 'COMPOSITE_CHANGE'].includes(form.changeType) && !form.targetDeptName) return '请选择目标部门'
  242. if (!form.targetPositionName) return '请选择目标岗位'
  243. if (!form.effectiveDate) return '请选择生效日期'
  244. if (!String(form.reason || '').trim()) return '请填写调岗事由'
  245. return null
  246. }
  247. async function save(submit) {
  248. const error = validate()
  249. if (error) {
  250. uni.showToast({ title: error, icon: 'none' })
  251. return
  252. }
  253. if (submitting.value) return
  254. submitting.value = true
  255. uni.showLoading({ title: submit ? '提交中...' : '保存中...' })
  256. try {
  257. syncOrigin()
  258. const id = await submitTransferApplication({
  259. form: { ...form, name: form.name || currentUser.name },
  260. submit,
  261. existingId: form.id,
  262. })
  263. form.id = id
  264. if (submit) {
  265. addApplication({
  266. type: meta.value.title,
  267. summary: summaryText.value,
  268. reason: form.reason,
  269. color: meta.value.color,
  270. metadata: {
  271. changeId: id,
  272. changeType: form.changeType,
  273. targetPositionName: form.targetPositionName,
  274. effectiveDate: form.effectiveDate,
  275. source: getServerConfig().mode === 'server' ? 'server' : 'demo',
  276. },
  277. })
  278. }
  279. uni.hideLoading()
  280. uni.showModal({
  281. title: submit ? '提交成功' : '已保存草稿',
  282. content: submit
  283. ? '调岗申请已提交审批,可在「调岗申请」或「我的申请」中查看进度。'
  284. : '草稿已保存,可在「调岗申请」列表中继续提交或编辑。',
  285. showCancel: false,
  286. success: () => uni.redirectTo({ url: '/pages/manage/transfer-list' }),
  287. })
  288. } catch (e) {
  289. uni.hideLoading()
  290. uni.showToast({ title: e.message || '操作失败', icon: 'none' })
  291. } finally {
  292. submitting.value = false
  293. }
  294. }
  295. function onSaveDraft() {
  296. save(false)
  297. }
  298. function onSubmit() {
  299. save(true)
  300. }
  301. async function hydrateForm(id) {
  302. uni.showLoading({ title: '加载中...' })
  303. try {
  304. const row = await loadTransferDetail(id)
  305. if (!row) throw new Error('未找到该调岗单')
  306. if (!canEditUserChange(row)) throw new Error('当前状态不可编辑')
  307. Object.assign(form, {
  308. id: row.id,
  309. userId: row.userId,
  310. name: row.name,
  311. employeeNo: row.employeeNo,
  312. dept: row.dept,
  313. position: row.position,
  314. level: row.level,
  315. changeType: row.changeType || 'POSITION_TRANSFER',
  316. targetDeptId: row.targetDeptId ?? null,
  317. targetDeptName: row.targetDept,
  318. targetPositionId: row.targetPositionId ?? null,
  319. targetPositionName: row.targetPosition,
  320. targetLevel: row.targetLevel,
  321. effectiveDate: row.effectiveDate,
  322. reason: row.reason,
  323. remark: row.remark,
  324. })
  325. } catch (e) {
  326. uni.hideLoading()
  327. uni.showToast({ title: e.message || '加载失败', icon: 'none' })
  328. setTimeout(() => back('/pages/manage/transfer-list'), 900)
  329. return
  330. }
  331. uni.hideLoading()
  332. }
  333. onLoad((options) => {
  334. uni.setNavigationBarTitle({ title: '调岗申请' })
  335. loadDepartments()
  336. loadPositions()
  337. const id = options?.id || ''
  338. if (id) {
  339. hydrateForm(id)
  340. return
  341. }
  342. syncOrigin()
  343. })
  344. </script>
  345. <style scoped lang="scss">
  346. .page-no-tab {
  347. padding-bottom: 150rpx;
  348. }
  349. .form-intro {
  350. padding: 30rpx 30rpx 22rpx;
  351. display: flex;
  352. align-items: center;
  353. background: #fff;
  354. }
  355. .form-intro > view:last-child {
  356. margin-left: 22rpx;
  357. }
  358. .intro-title {
  359. display: block;
  360. font-size: 32rpx;
  361. font-weight: 600;
  362. }
  363. .intro-sub {
  364. display: block;
  365. margin-top: 9rpx;
  366. color: #8b939f;
  367. font-size: 23rpx;
  368. }
  369. .steps {
  370. height: 128rpx;
  371. padding: 18rpx 70rpx 12rpx;
  372. display: flex;
  373. align-items: flex-start;
  374. background: #fff;
  375. border-top: 1rpx solid #f2f3f5;
  376. }
  377. .step {
  378. width: 92rpx;
  379. text-align: center;
  380. color: #a0a7b0;
  381. font-size: 20rpx;
  382. }
  383. .step > view {
  384. width: 42rpx;
  385. height: 42rpx;
  386. margin: 0 auto 8rpx;
  387. border-radius: 50%;
  388. background: #e8eaed;
  389. color: #8b939f;
  390. line-height: 42rpx;
  391. }
  392. .step.active {
  393. color: #1677ff;
  394. }
  395. .step.active > view {
  396. background: #1677ff;
  397. color: #fff;
  398. }
  399. .step-line {
  400. flex: 1;
  401. height: 2rpx;
  402. margin-top: 20rpx;
  403. background: #e1e4e8;
  404. }
  405. .form-card {
  406. overflow: hidden;
  407. }
  408. .field {
  409. position: relative;
  410. min-height: 108rpx;
  411. padding: 28rpx;
  412. display: flex;
  413. align-items: center;
  414. }
  415. .label {
  416. width: 180rpx;
  417. flex-shrink: 0;
  418. font-size: 28rpx;
  419. }
  420. .required::before {
  421. content: '*';
  422. color: #ee4d4d;
  423. margin-right: 6rpx;
  424. }
  425. .readonly {
  426. flex: 1;
  427. display: flex;
  428. align-items: center;
  429. justify-content: flex-end;
  430. font-size: 26rpx;
  431. }
  432. .small-avatar {
  433. width: 52rpx;
  434. height: 52rpx;
  435. margin-right: 14rpx;
  436. font-size: 22rpx;
  437. }
  438. .value {
  439. color: #1f2329;
  440. font-size: 27rpx;
  441. }
  442. .placeholder {
  443. color: #b2b7be;
  444. font-size: 27rpx;
  445. }
  446. .arrow {
  447. margin-left: 13rpx;
  448. color: #c4c8ce;
  449. font-size: 38rpx;
  450. }
  451. .textarea-field {
  452. display: block;
  453. min-height: 240rpx;
  454. }
  455. .textarea-field .label {
  456. display: block;
  457. width: auto;
  458. margin-bottom: 20rpx;
  459. }
  460. .textarea-field textarea {
  461. width: 100%;
  462. height: 130rpx;
  463. padding: 18rpx;
  464. border-radius: 12rpx;
  465. background: #f6f7f8;
  466. font-size: 27rpx;
  467. }
  468. .counter {
  469. position: absolute;
  470. right: 45rpx;
  471. bottom: 38rpx;
  472. color: #b2b7be;
  473. font-size: 21rpx;
  474. }
  475. .footer-tip {
  476. padding: 8rpx 34rpx 0;
  477. font-size: 22rpx;
  478. line-height: 34rpx;
  479. }
  480. .bottom-action {
  481. position: fixed;
  482. z-index: 10;
  483. left: 0;
  484. right: 0;
  485. bottom: 0;
  486. height: 120rpx;
  487. padding: 16rpx 24rpx;
  488. display: flex;
  489. gap: 18rpx;
  490. background: #fff;
  491. border-top: 1rpx solid #e8e9eb;
  492. }
  493. .draft-btn,
  494. .submit-btn {
  495. height: 84rpx;
  496. border-radius: 12rpx;
  497. display: flex;
  498. align-items: center;
  499. justify-content: center;
  500. font-size: 30rpx;
  501. }
  502. .draft-btn {
  503. width: 210rpx;
  504. border: 1rpx solid #d7dce2;
  505. color: #4b5563;
  506. }
  507. .submit-btn {
  508. flex: 1;
  509. background: #1677ff;
  510. color: #fff;
  511. }
  512. @media (min-width: 800px) {
  513. .bottom-action {
  514. max-width: 520px;
  515. left: 50%;
  516. transform: translateX(-50%);
  517. }
  518. }
  519. </style>