index.vue 16 KB

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