index.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  1. <template>
  2. <view class="page_publish" >
  3. <commonHeader :title="isEdit ? '编辑任务' : '新建任务'" @back="goBack"></commonHeader>
  4. <scroll-view scroll-y class="publish_content" :style="{ paddingTop: statusBarHeight + 160 + 'rpx', paddingBottom: safeAreaBottom + 'rpx' }">
  5. <view class="form_item">
  6. <view class="form_row form_row_type">
  7. <text class="form_label">任务类型</text>
  8. <view class="form_content">
  9. <picker mode="selector" :range="typeOptions" :value="typeIndex" @change="handleTypeChange">
  10. <view class="filter_item">
  11. <text class="filter_text" :class="{ placeholder: typeIndex === -1 }">{{
  12. formData.noticeType || '请选择类型' }}</text>
  13. <uni-icons type="down" size="16" color="#999999"></uni-icons>
  14. </view>
  15. </picker>
  16. </view>
  17. </view>
  18. </view>
  19. <view class="form_item">
  20. <view class="form_row form_row_type">
  21. <text class="form_label">任务名称</text>
  22. <view class="form_content">
  23. <input type="text" class="form_input" v-model="formData.title" placeholder="请输入任务名称" />
  24. </view>
  25. </view>
  26. </view>
  27. <view class="form_item">
  28. <view class="form_row">
  29. <text class="form_label">任务内容</text>
  30. <view class="form_content">
  31. <view class="textarea_container">
  32. <textarea class="form_textarea" v-model="formData.content" placeholder="请输入任务内容"
  33. :maxlength="-1"></textarea>
  34. </view>
  35. </view>
  36. </view>
  37. </view>
  38. <view class="form_item">
  39. <view class="form_row">
  40. <text class="form_label">发布时间</text>
  41. <view class="form_content">
  42. <custom-radio v-model="publishType" :options="publishOptions" />
  43. <view v-if="publishType === 1" class="time_picker">
  44. <uni-datetime-picker v-model="publishDateTime" type="datetime" :start="startTime"
  45. :end="endTime" return-type="string" />
  46. </view>
  47. </view>
  48. </view>
  49. </view>
  50. <view class="form_item">
  51. <view class="form_row">
  52. <text class="form_label">通知对象</text>
  53. <view class="form_content">
  54. <view class="object_row">
  55. <view class="object_info" @click="goPersonList">
  56. <text class="object_count">{{ objectTypeLabel || '请选择' }}</text>
  57. <text class="object_count">已选{{ selectedCount }}人</text>
  58. </view>
  59. <view class="object_action" @click="selectObject">
  60. <text class="action_text">选择通知对象</text>
  61. <text class="action_arrow">></text>
  62. </view>
  63. </view>
  64. </view>
  65. </view>
  66. </view>
  67. <view class="form_item" v-if="isEdit">
  68. <view class="form_row">
  69. <text class="form_label">任务状态</text>
  70. <view class="form_content">
  71. <view class="switch_row">
  72. <view class="switch_container">
  73. <switch :checked="formData.status" color="#2E64FA" @change="onStatusChange" />
  74. </view>
  75. <text class="switch_text">说明:关闭后通知不对通知对象显示</text>
  76. </view>
  77. </view>
  78. </view>
  79. </view>
  80. <view class="form_item" v-if="isEdit && formData.status">
  81. <view class="form_row">
  82. <text class="form_label">已读记录</text>
  83. <view class="form_content">
  84. <custom-radio v-model="saveRecord" :options="recordOptions" />
  85. <text class="record_tip">说明:{{ saveRecord === 0 ? '已读记录将被保留' : '已读记录将被清空,重新记录' }}</text>
  86. </view>
  87. </view>
  88. </view>
  89. </scroll-view>
  90. <view class="publish_footer" :style="{ paddingBottom: safeAreaBottom + 24 + 'rpx' }">
  91. <view class="confirm_btn" @click="handleSave">
  92. <text class="btn_text">确定</text>
  93. </view>
  94. </view>
  95. </view>
  96. </template>
  97. <script setup>
  98. import { ref, reactive, onMounted } from 'vue';
  99. import { onLoad, onShow } from '@dcloudio/uni-app';
  100. import uniIcons from '@/uni_modules/uni-icons/components/uni-icons/uni-icons.vue';
  101. import uniDatetimePicker from '@/uni_modules/uni-datetime-picker/components/uni-datetime-picker/uni-datetime-picker.vue';
  102. import overviewApi from '@/reqApi/overview.js';
  103. import notification from '@/reqApi/notification.js';
  104. import CustomRadio from '@/components/customRadio.vue';
  105. import CommonHeader from '@/components/commonHeader.vue';
  106. import { useSafeArea } from '@/common/safeArea';
  107. const { statusBarHeight, safeAreaBottom, initSafeArea } = useSafeArea();
  108. const showTypePicker = ref(false);
  109. const publishType = ref(0);
  110. const isEdit = ref(false);
  111. const saveRecord = ref(0);
  112. const typeIndex = ref(-1);
  113. const typeData = ref([]);
  114. const typeOptions = ref([]);
  115. const getCurrentDateTime = () => {
  116. const now = new Date();
  117. const year = now.getFullYear();
  118. const month = String(now.getMonth() + 1).padStart(2, '0');
  119. const day = String(now.getDate()).padStart(2, '0');
  120. const hours = String(now.getHours()).padStart(2, '0');
  121. const minutes = String(now.getMinutes()).padStart(2, '0');
  122. const seconds = String(now.getSeconds()).padStart(2, '0');
  123. return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
  124. };
  125. const publishDateTime = ref(getCurrentDateTime());
  126. const startTime = ref('2024-01-01 00:00:00');
  127. const endTime = ref('2030-12-31 23:59:59');
  128. const publishOptions = [
  129. { label: '立即发布', value: 0 },
  130. { label: '预约发布', value: 1 }
  131. ];
  132. const recordOptions = [
  133. { label: '保存', value: 0 },
  134. { label: '不保存', value: 1 }
  135. ];
  136. const formData = reactive({
  137. title: '',
  138. noticeType: '',
  139. content: '',
  140. status: true
  141. });
  142. const selectedCount = ref(0);
  143. const selectedUsers = ref([]);
  144. const schoolYearId = ref('');
  145. const noticeTypeId = ref('');
  146. const objectTypeLabel = ref('');
  147. const editId = ref('');
  148. const originalNoticeType = ref(0);
  149. const fetchNoticeTypeList = async () => {
  150. try {
  151. const res = await overviewApi.noticeTypeList();
  152. if (res.data && Array.isArray(res.data)) {
  153. typeData.value = res.data.map(item => ({
  154. id: item.id,
  155. name: item.name
  156. }));
  157. typeOptions.value = typeData.value.map(item => item.name);
  158. }
  159. } catch (error) {
  160. console.error('获取任务类型失败:', error);
  161. }
  162. };
  163. onMounted(() => {
  164. initSafeArea();
  165. });
  166. onLoad(async (options) => {
  167. await fetchNoticeTypeList();
  168. if (options && options.id) {
  169. isEdit.value = true;
  170. editId.value = options.id;
  171. fetchNoticeDetail(options.id);
  172. }
  173. });
  174. const fetchNoticeDetail = async (id) => {
  175. try {
  176. const res = await notification.find_collection_task_target_list({ id });
  177. if (res.data) {
  178. const data = res.data;
  179. formData.title = data.taskName || data.noticeName || '';
  180. const content = data.noticeContent || data.content || '';
  181. formData.content = content.replace(/<[^>]+>/g, '');
  182. formData.status = data.isClose !== true;
  183. noticeTypeId.value = data.typeId || data.noticeTypeId || '';
  184. const typeIndexVal = typeData.value.findIndex(item => item.id === (data.typeId || data.noticeTypeId));
  185. typeIndex.value = typeIndexVal >= 0 ? typeIndexVal : -1;
  186. if (typeIndexVal >= 0) {
  187. formData.noticeType = typeData.value[typeIndexVal].name;
  188. }
  189. publishType.value = data.sendTimeType === 2 ? 1 : 0;
  190. publishDateTime.value = data.releaseTime || getCurrentDateTime();
  191. saveRecord.value = data.isSave === true ? 0 : 1;
  192. if (data.noticeType) {
  193. const typeMap = {
  194. 1: '按部门',
  195. 2: '按学科',
  196. 3: '按年级',
  197. 4: '按权限',
  198. 5: '按学生'
  199. };
  200. objectTypeLabel.value = typeMap[data.noticeType] || '';
  201. originalNoticeType.value = data.noticeType;
  202. }
  203. if (data.userList && Array.isArray(data.userList)) {
  204. selectedUsers.value = data.userList.map(user => ({
  205. id: user.userId || user.id,
  206. name: user.thirdName,
  207. userId: user.userId,
  208. thirdCode: user.thirdCode || user.userAccount || user.teacherId || user.studentCode || '',
  209. userAccount: user.userAccount || user.thirdCode,
  210. departmentName: user.departmentName || '',
  211. groupName: user.groupName || '',
  212. subjectName: user.subjectName || '',
  213. gradeName: user.gradeName || '',
  214. classTypeName: user.classTypeName || '',
  215. className: user.className || '',
  216. roleName: user.roleName || ''
  217. }));
  218. selectedCount.value = selectedUsers.value.length;
  219. }
  220. }
  221. } catch (error) {
  222. console.error('获取任务详情失败:', error);
  223. }
  224. };
  225. onShow(() => {
  226. const result = uni.getStorageSync('selectObjectResult');
  227. if (result) {
  228. try {
  229. const data = JSON.parse(result);
  230. if (data) {
  231. objectTypeLabel.value = data.tab;
  232. selectedCount.value = data.count;
  233. selectedUsers.value = data.users;
  234. }
  235. } catch (e) {
  236. console.error('解析选择结果失败:', e);
  237. }
  238. uni.removeStorageSync('selectObjectResult');
  239. }
  240. const personResult = uni.getStorageSync('personListResult');
  241. if (personResult) {
  242. try {
  243. const data = JSON.parse(personResult);
  244. if (data && data.users) {
  245. selectedUsers.value = data.users;
  246. selectedCount.value = data.users.length;
  247. }
  248. } catch (e) {
  249. console.error('解析人员列表结果失败:', e);
  250. }
  251. uni.removeStorageSync('personListResult');
  252. }
  253. });
  254. const handleTypeChange = (e) => {
  255. typeIndex.value = e.detail.value;
  256. const selectedItem = typeData.value[typeIndex.value];
  257. if (selectedItem) {
  258. formData.noticeType = selectedItem.name;
  259. noticeTypeId.value = selectedItem.id;
  260. }
  261. };
  262. const goBack = () => {
  263. uni.navigateBack();
  264. };
  265. const onStatusChange = (e) => {
  266. formData.status = e.detail.value;
  267. if (formData.status) {
  268. saveRecord.value = 0;
  269. }
  270. };
  271. const selectObject = () => {
  272. const lastSelection = {
  273. tab: objectTypeLabel.value,
  274. users: selectedUsers.value,
  275. noticeType: originalNoticeType.value || getNoticeTypeValue(objectTypeLabel.value),
  276. isEdit: isEdit.value
  277. };
  278. uni.setStorageSync('selectObjectLastSelection', JSON.stringify(lastSelection));
  279. uni.navigateTo({ url: '/pages/materialCollection/taskOverview/components/selectObjectNoDedup' });
  280. };
  281. const goPersonList = () => {
  282. if (selectedUsers.value.length === 0) {
  283. return;
  284. }
  285. const data = {
  286. tab: objectTypeLabel.value,
  287. users: selectedUsers.value
  288. };
  289. uni.setStorageSync('personListData', JSON.stringify(data));
  290. uni.navigateTo({ url: '/pages/materialCollection/taskOverview/components/personList' });
  291. };
  292. const getCurrentTime = () => {
  293. const now = new Date();
  294. const year = now.getFullYear();
  295. const month = String(now.getMonth() + 1).padStart(2, '0');
  296. const day = String(now.getDate()).padStart(2, '0');
  297. const hours = String(now.getHours()).padStart(2, '0');
  298. const minutes = String(now.getMinutes()).padStart(2, '0');
  299. const seconds = String(now.getSeconds()).padStart(2, '0');
  300. return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
  301. };
  302. const getNoticeTypeValue = (tab) => {
  303. const map = {
  304. '按部门': 1,
  305. '按学科': 2,
  306. '按年级': 3,
  307. '按权限': 4,
  308. '按学生': 5
  309. };
  310. return map[tab] || 1;
  311. };
  312. const handleSave = async () => {
  313. if (!formData.title) {
  314. uni.showToast({ title: '请输入任务名称', icon: 'none' });
  315. return;
  316. }
  317. if (!formData.content) {
  318. uni.showToast({ title: '请输入任务内容', icon: 'none' });
  319. return;
  320. }
  321. if (!objectTypeLabel.value) {
  322. uni.showToast({ title: '请选择通知对象', icon: 'none' });
  323. return;
  324. }
  325. if (selectedUsers.value.length === 0) {
  326. uni.showToast({ title: '请选择通知人员', icon: 'none' });
  327. return;
  328. }
  329. if (!schoolYearId.value) {
  330. try {
  331. const res = await overviewApi.findSchoolYear();
  332. if (res.data && res.data.length > 0) {
  333. schoolYearId.value = res.data[0].id;
  334. }
  335. } catch (error) {
  336. console.error('获取学年失败:', error);
  337. uni.showToast({ title: '获取学年信息失败', icon: 'none' });
  338. return;
  339. }
  340. }
  341. uni.showLoading({ title: '保存中...' });
  342. try {
  343. const objectType = getNoticeTypeValue(objectTypeLabel.value);
  344. const params = {
  345. typeId: noticeTypeId.value || '',
  346. noticeName: formData.title,
  347. noticeContent: btoa(unescape(encodeURIComponent(formData.content))),
  348. sendTimeType: publishType.value + 1,
  349. releaseTime: publishType.value === 0 ? getCurrentTime() : publishDateTime.value,
  350. noticeType: objectType,
  351. isClose: isEdit.value ? !formData.status : false,
  352. isSave: isEdit.value ? (saveRecord.value === 0) : false,
  353. fileList: []
  354. };
  355. if (isEdit.value && editId.value) {
  356. params.id = editId.value;
  357. }
  358. const userList = selectedUsers.value.map(user => {
  359. return {
  360. userType: objectType === 5 ? 2 : 1,
  361. userId: user.userId || user.id,
  362. thirdName: user.name,
  363. thirdCode: user.thirdCode || '',
  364. gradeName: user.gradeName || '',
  365. classTypeName: user.classTypeName || '',
  366. className: user.className || '',
  367. departmentName: user.departmentName || '',
  368. groupName: user.groupName || '',
  369. subjectName: user.subjectName || '',
  370. roleName: user.roleName || ''
  371. };
  372. });
  373. if (userList.length > 0) {
  374. params.userList = userList;
  375. }
  376. const res = await overviewApi.save_notices(params);
  377. if (res.code === 200) {
  378. uni.showToast({ title: '保存成功', icon: 'success' });
  379. setTimeout(() => {
  380. uni.navigateBack();
  381. }, 1500);
  382. } else {
  383. uni.showToast({ title: res.message || '保存失败', icon: 'none' });
  384. }
  385. } catch (error) {
  386. console.error('保存失败:', error);
  387. uni.showToast({ title: '保存失败', icon: 'none' });
  388. } finally {
  389. uni.hideLoading();
  390. }
  391. };
  392. </script>
  393. <style lang="scss" scoped>
  394. .page_publish {
  395. min-height: 100vh;
  396. background-color: #FFFFFF;
  397. display: flex;
  398. flex-direction: column;
  399. box-sizing: border-box;
  400. }
  401. .publish_content {
  402. flex: 1;
  403. box-sizing: border-box;
  404. padding-top: 36rpx;
  405. padding-left: 24rpx;
  406. padding-right: 24rpx;
  407. }
  408. .form_item {
  409. background-color: #FFFFFF;
  410. border-radius: 8rpx;
  411. padding-bottom: 24rpx;
  412. margin-bottom: 24rpx;
  413. border-bottom: 2rpx solid #F3F3F3;
  414. &:last-child {
  415. border-bottom: none;
  416. }
  417. .form_label {
  418. font-size: 28rpx;
  419. color: #666666;
  420. font-weight: 400;
  421. white-space: nowrap;
  422. flex-shrink: 0;
  423. margin-right: 16rpx;
  424. &::after {
  425. content: ':';
  426. margin-left: 8rpx;
  427. }
  428. }
  429. .form_row {
  430. display: flex;
  431. align-items: flex-start;
  432. .form_content {
  433. flex: 1;
  434. }
  435. }
  436. .form_row_type {
  437. align-items: center;
  438. .filter_item {
  439. display: flex;
  440. align-items: center;
  441. justify-content: space-between;
  442. padding: 0 16rpx;
  443. height: 72rpx;
  444. line-height: 72rpx;
  445. background-color: #FFFFFF;
  446. border-radius: 8rpx;
  447. width: 320rpx;
  448. border: 2rpx solid #DCDFE6;
  449. .filter_text {
  450. font-size: 28rpx;
  451. color: #333333;
  452. margin-right: 8rpx;
  453. overflow: hidden;
  454. text-overflow: ellipsis;
  455. white-space: nowrap;
  456. &.placeholder {
  457. color: #909399;
  458. }
  459. }
  460. }
  461. }
  462. .form_input {
  463. font-size: 28rpx;
  464. color: #333333;
  465. padding: 0 24rpx;
  466. background-color: #FFFFFF;
  467. border-radius: 8rpx;
  468. width: 100%;
  469. height: 72rpx;
  470. box-sizing: border-box;
  471. border: 2rpx solid #E9E9E9;
  472. }
  473. .form_select {
  474. display: flex;
  475. align-items: center;
  476. justify-content: space-between;
  477. padding: 16rpx;
  478. background-color: #F5F7FA;
  479. border-radius: 8rpx;
  480. border: 2rpx solid #DCDFE6;
  481. .select_text {
  482. font-size: 28rpx;
  483. color: #333333;
  484. }
  485. .select_right {
  486. .select_arrow {
  487. font-size: 28rpx;
  488. color: #999999;
  489. }
  490. }
  491. }
  492. .textarea_container {
  493. position: relative;
  494. .form_textarea {
  495. width: 100%;
  496. height: 240rpx;
  497. font-size: 28rpx;
  498. color: #333333;
  499. line-height: 1.6;
  500. padding: 14rpx 24rpx 56rpx 24rpx;
  501. background-color: #FFFFFF;
  502. border-radius: 8rpx;
  503. box-sizing: border-box;
  504. border: 2rpx solid #E9E9E9;
  505. }
  506. .textarea_actions {
  507. position: absolute;
  508. bottom: 13rpx;
  509. right: 16rpx;
  510. display: flex;
  511. align-items: center;
  512. gap: 8rpx;
  513. .full_icon {
  514. width: 28rpx;
  515. height: 28rpx;
  516. }
  517. .action_text {
  518. font-weight: 400;
  519. font-size: 28rpx;
  520. color: #2E64FA;
  521. }
  522. }
  523. }
  524. .time_picker {
  525. margin-top: 20rpx;
  526. min-width: 300rpx;
  527. height: 68rpx;
  528. float: right;
  529. :deep(.uni-date-x--border) {
  530. height: 68rpx;
  531. }
  532. :deep(.uni-date-x) {
  533. padding: 0 16rpx;
  534. height: 48rpx;
  535. }
  536. :deep(.uni-date-x--text) {
  537. font-weight: 400;
  538. font-size: 28rpx;
  539. color: #333333;
  540. }
  541. :deep(.uni-date__x-input) {
  542. font-size: 28rpx;
  543. color: #333333;
  544. }
  545. }
  546. .object_row {
  547. display: flex;
  548. align-items: center;
  549. justify-content: space-between;
  550. .object_info {
  551. display: flex;
  552. align-items: center;
  553. gap: 12rpx;
  554. .object_count {
  555. font-size: 28rpx;
  556. color: #2E64FA;
  557. }
  558. }
  559. .object_action {
  560. display: flex;
  561. align-items: center;
  562. gap: 8rpx;
  563. .action_text {
  564. font-size: 28rpx;
  565. color: #2E64FA;
  566. }
  567. .action_arrow {
  568. font-size: 28rpx;
  569. color: #2E64FA;
  570. }
  571. }
  572. }
  573. .object_tip {
  574. font-size: 24rpx;
  575. color: #F56C6C;
  576. margin-top: 12rpx;
  577. display: block;
  578. }
  579. .switch_row {
  580. display: flex;
  581. flex-direction:column;
  582. .switch_container {
  583. transform: scale(0.8);
  584. transform-origin: left center;
  585. }
  586. .switch_text {
  587. font-weight: 400;
  588. font-size: 28rpx;
  589. color: #999999;
  590. }
  591. }
  592. .record_tip {
  593. font-weight: 400;
  594. font-size: 28rpx;
  595. color: #999999;
  596. margin-top: 16rpx;
  597. display: block;
  598. }
  599. }
  600. .publish_footer {
  601. padding: 24rpx;
  602. background-color: #FFFFFF;
  603. .confirm_btn {
  604. width: 702rpx;
  605. height: 90rpx;
  606. background: #2E64FA;
  607. border-radius: 8rpx 8rpx 8rpx 8rpx;
  608. text-align: center;
  609. line-height: 90rpx;
  610. .btn_text {
  611. font-size: 32rpx;
  612. font-weight: 500;
  613. color: #FFFFFF;
  614. }
  615. }
  616. }
  617. </style>