index.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728
  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. const userInfo = {
  366. userType: objectType === 5 ? 2 : 1,
  367. userId: user.id,
  368. thirdName: user.name,
  369. thirdCode: user.thirdCode || user.userAccount || '',
  370. gradeName: '',
  371. classTypeName: '',
  372. className: '',
  373. departmentName: '',
  374. groupName: '',
  375. subjectName: '',
  376. roleName: ''
  377. };
  378. switch (objectType) {
  379. case 1:
  380. userInfo.departmentName = user.departmentName || '';
  381. userInfo.groupName = user.groupName || '';
  382. break;
  383. case 2:
  384. userInfo.subjectName = user.subjectName || '';
  385. userInfo.gradeName = user.gradeName || '';
  386. break;
  387. case 3:
  388. userInfo.gradeName = user.gradeName || '';
  389. userInfo.classTypeName = user.classTypeName || '';
  390. userInfo.className = user.className || '';
  391. break;
  392. case 4:
  393. userInfo.roleName = user.roleName || '';
  394. break;
  395. }
  396. return userInfo;
  397. });
  398. if (userList.length > 0) {
  399. params.userList = userList;
  400. }
  401. const res = await overviewApi.save_notices(params);
  402. if (res.code === 200) {
  403. uni.showToast({ title: '发布成功', icon: 'success' });
  404. setTimeout(() => {
  405. uni.redirectTo({ url: '/pages/notice/overview/index' });
  406. }, 1500);
  407. } else {
  408. uni.showToast({ title: res.message || '发布失败', icon: 'none' });
  409. }
  410. } catch (error) {
  411. console.error('发布失败:', error);
  412. uni.showToast({ title: '发布失败', icon: 'none' });
  413. } finally {
  414. uni.hideLoading();
  415. }
  416. };
  417. </script>
  418. <style lang="scss" scoped>
  419. .page_publish {
  420. min-height: 100vh;
  421. background-color: #FFFFFF;
  422. display: flex;
  423. flex-direction: column;
  424. box-sizing: border-box;
  425. }
  426. .publish_content {
  427. flex: 1;
  428. box-sizing: border-box;
  429. padding-top: 36rpx;
  430. padding-left: 24rpx;
  431. padding-right: 24rpx;
  432. }
  433. .form_item {
  434. background-color: #FFFFFF;
  435. border-radius: 8rpx;
  436. padding-bottom: 24rpx;
  437. margin-bottom: 24rpx;
  438. border-bottom: 2rpx solid #F3F3F3;
  439. &:last-child {
  440. border-bottom: none;
  441. }
  442. .form_label {
  443. font-size: 28rpx;
  444. color: #666666;
  445. font-weight: 400;
  446. white-space: nowrap;
  447. flex-shrink: 0;
  448. margin-right: 16rpx;
  449. &::after {
  450. content: ':';
  451. margin-left: 8rpx;
  452. }
  453. }
  454. // 通知类型
  455. .form_row {
  456. display: flex;
  457. align-items: flex-start;
  458. .form_content {
  459. flex: 1;
  460. }
  461. }
  462. .form_row_type {
  463. align-items: center;
  464. .filter_item {
  465. display: flex;
  466. align-items: center;
  467. justify-content: space-between;
  468. padding: 0 16rpx;
  469. height: 72rpx;
  470. line-height: 72rpx;
  471. background-color: #FFFFFF;
  472. border-radius: 8rpx;
  473. width: 320rpx;
  474. border: 2rpx solid #DCDFE6;
  475. .filter_text {
  476. font-size: 28rpx;
  477. color: #333333;
  478. margin-right: 8rpx;
  479. overflow: hidden;
  480. text-overflow: ellipsis;
  481. white-space: nowrap;
  482. &.placeholder {
  483. color: #909399;
  484. }
  485. }
  486. }
  487. }
  488. .form_input {
  489. font-size: 28rpx;
  490. color: #333333;
  491. padding: 0 24rpx;
  492. background-color: #FFFFFF;
  493. border-radius: 8rpx;
  494. width: 100%;
  495. height: 72rpx;
  496. box-sizing: border-box;
  497. border: 2rpx solid #E9E9E9;
  498. }
  499. .form_select {
  500. display: flex;
  501. align-items: center;
  502. justify-content: space-between;
  503. padding: 16rpx;
  504. background-color: #F5F7FA;
  505. border-radius: 8rpx;
  506. border: 2rpx solid #DCDFE6;
  507. .select_text {
  508. font-size: 28rpx;
  509. color: #333333;
  510. }
  511. .select_right {
  512. .select_arrow {
  513. font-size: 28rpx;
  514. color: #999999;
  515. }
  516. }
  517. }
  518. .textarea_container {
  519. position: relative;
  520. .form_textarea {
  521. width: 100%;
  522. height: 240rpx;
  523. font-size: 28rpx;
  524. color: #333333;
  525. line-height: 1.6;
  526. padding: 14rpx 24rpx 56rpx 24rpx;
  527. background-color: #FFFFFF;
  528. border-radius: 8rpx;
  529. box-sizing: border-box;
  530. border: 2rpx solid #E9E9E9;
  531. }
  532. .textarea_actions {
  533. position: absolute;
  534. bottom: 13rpx;
  535. right: 16rpx;
  536. display: flex;
  537. align-items: center;
  538. gap: 8rpx;
  539. .full_icon {
  540. width: 28rpx;
  541. height: 28rpx;
  542. }
  543. .action_text {
  544. font-weight: 400;
  545. font-size: 28rpx;
  546. color: #2E64FA;
  547. }
  548. }
  549. }
  550. .time_picker {
  551. margin-top: 20rpx;
  552. min-width: 300rpx;
  553. height: 68rpx;
  554. float: right;
  555. :deep(.uni-date-x--border) {
  556. height: 68rpx;
  557. }
  558. :deep(.uni-date-x) {
  559. padding: 0 16rpx;
  560. height: 48rpx;
  561. }
  562. :deep(.uni-date-x--text) {
  563. font-weight: 400;
  564. font-size: 28rpx;
  565. color: #333333;
  566. }
  567. :deep(.uni-date__x-input) {
  568. font-size: 28rpx;
  569. color: #333333;
  570. }
  571. }
  572. .object_row {
  573. display: flex;
  574. align-items: center;
  575. justify-content: space-between;
  576. .object_info {
  577. display: flex;
  578. align-items: center;
  579. gap: 12rpx;
  580. .object_count {
  581. font-size: 28rpx;
  582. color: #2E64FA;
  583. }
  584. }
  585. .object_action {
  586. display: flex;
  587. align-items: center;
  588. gap: 8rpx;
  589. .action_text {
  590. font-size: 28rpx;
  591. color: #2E64FA;
  592. }
  593. .action_arrow {
  594. font-size: 28rpx;
  595. color: #2E64FA;
  596. }
  597. }
  598. }
  599. .object_tip {
  600. font-size: 24rpx;
  601. color: #F56C6C;
  602. margin-top: 12rpx;
  603. display: block;
  604. }
  605. .switch_row {
  606. display: flex;
  607. flex-direction:column;
  608. // gap: 16rpx;
  609. .switch_container {
  610. transform: scale(0.8);
  611. transform-origin: left center;
  612. }
  613. .switch_text {
  614. font-weight: 400;
  615. font-size: 28rpx;
  616. color: #999999;
  617. }
  618. }
  619. .record_tip {
  620. font-weight: 400;
  621. font-size: 28rpx;
  622. color: #999999;
  623. margin-top: 16rpx;
  624. display: block;
  625. }
  626. }
  627. .publish_footer {
  628. padding: 24rpx;
  629. background-color: #FFFFFF;
  630. .confirm_btn {
  631. width: 702rpx;
  632. height: 90rpx;
  633. background: #2E64FA;
  634. border-radius: 8rpx 8rpx 8rpx 8rpx;
  635. text-align: center;
  636. line-height: 90rpx;
  637. .btn_text {
  638. font-size: 32rpx;
  639. font-weight: 500;
  640. color: #FFFFFF;
  641. }
  642. }
  643. }
  644. </style>