index.vue 17 KB

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