index.vue 17 KB

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