article-setting.vue 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  1. <template>
  2. <view class="app-page" :class="{ 'is-balck grey-darken-6': isBlackTheme }">
  3. <!-- 文章设置页面 -->
  4. <view class="e-fixed shadow-2"><tm-tabs color="light-blue" :shadow="0" v-model="tab.activeIndex" :list="tab.list" align="center"></tm-tabs></view>
  5. <!-- 占位区域 -->
  6. <view style="width: 100vw;height: 90rpx;"></view>
  7. <view class="pa-24" v-if="loading != 'success'">
  8. <block v-if="loading == 'loading'">
  9. <tm-skeleton model="list"></tm-skeleton>
  10. <tm-skeleton model="list"></tm-skeleton>
  11. <tm-skeleton model="list"></tm-skeleton>
  12. <tm-skeleton model="list"></tm-skeleton>
  13. </block>
  14. <view v-else class="flex flex-center" style="height:60vh;">
  15. <tm-empty color="blue"><tm-button theme="blue" @click="fnGetSettings()">刷新试试</tm-button></tm-empty>
  16. </view>
  17. </view>
  18. <view v-else class="app-page-content ma-24 bg-white round-3 ">
  19. <!-- 表单区域 -->
  20. <tm-form ref="formData">
  21. <!-- 基本设置 -->
  22. <view v-show="tab.activeIndex == 0">
  23. <tm-input name="title" required title="文章标题" placeholder="请输入文章标题..." v-model="article.title"></tm-input>
  24. <tm-input name="slug" required title="文章别名" v-model="article.slug" placeholder="请输入文章别名..."></tm-input>
  25. <tm-pickers
  26. title="选择文章的状态"
  27. btn-color="light-blue"
  28. :default-value.sync="articleStatus.status"
  29. rang-key="name"
  30. :list="articleStatus.list"
  31. @confirm="fnOnArticleStatusConfirm"
  32. >
  33. <tm-input
  34. name="status"
  35. required
  36. title="文章状态"
  37. placeholder="请选择文章状态"
  38. disabled
  39. :value="articleStatus.selectText"
  40. right-icon="icon-angle-right"
  41. ></tm-input>
  42. </tm-pickers>
  43. <tm-input
  44. v-if="articleStatus.selectValue == 'INTIMATE'"
  45. name="password"
  46. :password="true"
  47. required
  48. title="文章密码"
  49. placeholder="请输入密码..."
  50. v-model="article.password"
  51. ></tm-input>
  52. <view v-if="articleStatus.selectValue == 'INTIMATE'" class="pl-32 mt-12 mb-24 input-tips text-grey text-size-s">
  53. 填写提示:文章状态为【私有】的时候需要填写密码。
  54. </view>
  55. <view class="mx-32 my-24 pb-24 border-b-1">
  56. <view class="text-size-m required flex-between">
  57. <text>选择分类</text>
  58. <tm-button size="xs" theme="light-blue" :fab="true" :shadow="0" icon="icon-plus" @click="catePoupShow = true">新增</tm-button>
  59. </view>
  60. <view>
  61. <tm-groupcheckbox name="categoryIds">
  62. <tm-checkbox :dense="true" :name="item.id" v-for="(item, index) in categories" :key="index" v-model="item.checked">
  63. <template v-slot:default="{ checkData }">
  64. <tm-button :flat="true" size="s" :theme="checkData.checked ? 'light-blue' : 'grey-lighten-4'" :plan="true">{{ item.name }}</tm-button>
  65. </template>
  66. </tm-checkbox>
  67. </tm-groupcheckbox>
  68. </view>
  69. </view>
  70. <view class="mx-32 my-24 pb-24">
  71. <view class="text-size-m required flex-between">
  72. <text>选择标签</text>
  73. <tm-button size="xs" theme="light-blue" :fab="true" :shadow="0" icon="icon-plus" @click="tagPoupShow = true">新增</tm-button>
  74. </view>
  75. <view>
  76. <tm-groupcheckbox name="tagIds">
  77. <tm-checkbox :dense="true" :name="item.id" v-for="(item, index) in tags" :key="index" v-model="item.checked">
  78. <template v-slot:default="{ checkData }">
  79. <tm-button :flat="true" size="s" :theme="checkData.checked ? 'light-blue' : 'grey-lighten-4'" :plan="true">{{ item.name }}</tm-button>
  80. </template>
  81. </tm-checkbox>
  82. </tm-groupcheckbox>
  83. </view>
  84. </view>
  85. </view>
  86. <!-- 高级设置 -->
  87. <view v-show="tab.activeIndex == 1" class="pt-6">
  88. <view class="mx-32 my-24 border-b-1 pb-24 flex-between">
  89. <text class="text-size-m ">禁止评论</text>
  90. <tm-switch v-model="article.disallowComment" color="light-blue" :text="['是', '否']"></tm-switch>
  91. </view>
  92. <view class="mx-32 my-24 border-b-1 pb-24 flex-between">
  93. <text class="text-size-m ">是否置顶</text>
  94. <tm-switch v-model="topPriority" color="light-blue" :text="['是', '否']"></tm-switch>
  95. </view>
  96. <tm-pickersDate
  97. @confirm="fnOnConfirmPublishTime"
  98. :default-value="createTime"
  99. :show-detail="{ year: true, month: true, day: true, hour: false, min: false, sec: false }"
  100. >
  101. <tm-input name="createTime" title="发表时间" placeholder="请选择发表时间" disabled :value="createTime" right-icon="icon-angle-right"></tm-input>
  102. </tm-pickersDate>
  103. <tm-input
  104. :vertical="true"
  105. :height="150"
  106. input-type="textarea"
  107. bg-color="grey-lighten-5"
  108. :maxlength="200"
  109. title=" 文章摘要"
  110. placeholder="如不填写,会从文章中自动截取"
  111. v-model="article.summary"
  112. ></tm-input>
  113. <view class="ma-30 mt-12 pb-12">
  114. <view class="mb-12">
  115. <text class="text-size-m">封面图</text>
  116. <text class="text-grey text-size-s">(点击下方图片区域选择封面)</text>
  117. </view>
  118. <image v-if="article.showThumbnail" class="thumbnail round-3" :src="article.showThumbnail" mode="aspectFill" @click="attachmentsSelectShow = true"></image>
  119. <view v-else class="thumbnail round-3 text-grey grey-lighten-5 flex flex-col flex-center " @click="attachmentsSelectShow = true">
  120. <text class="iconfont icon-picture" style="font-size: 46rpx;"></text>
  121. <text class="mt-12 text-size-m">选择文章封面图</text>
  122. </view>
  123. </view>
  124. </view>
  125. <view v-show="tab.activeIndex == 2" class="pt-12">
  126. <tm-input
  127. :vertical="true"
  128. :height="150"
  129. input-type="textarea"
  130. bg-color="grey-lighten-5"
  131. :maxlength="200"
  132. title="自定义关键字"
  133. placeholder="多个关键词以英文逗号隔开,如不填写,将自动使用标签作为关键词"
  134. v-model="article.metaKeywords"
  135. ></tm-input>
  136. <tm-input
  137. :vertical="true"
  138. :height="200"
  139. :borderBottom="false"
  140. input-type="textarea"
  141. bg-color="grey-lighten-5"
  142. :maxlength="200"
  143. title="自定义描述"
  144. placeholder="如不填写,会从文章中自动截取"
  145. v-model="article.metaDescription"
  146. ></tm-input>
  147. </view>
  148. </tm-form>
  149. <!-- 操作区域 -->
  150. <view class="btn-bar flex flex-center bg-white">
  151. <tm-button theme="blue" :shadow="0" block :height="70" @click="fnOnSave(false, false)">立即保存</tm-button>
  152. <tm-button v-if="isEdit" theme="light-blue" :shadow="0" block :height="70" @click="fnOnSave(false, true)">保存并返回</tm-button>
  153. <!-- <block v-if="from == 'edit' && isEdit">
  154. <tm-button
  155. v-if="article.status == 'DRAFT' || article.status == 'INTIMATE' || article.status == 'RECYCLE'"
  156. theme="light-blue"
  157. :shadow="0"
  158. block
  159. :height="60"
  160. size="m"
  161. @click="fnOnChangeStatus('PUBLISHED')"
  162. >
  163. 转为发布
  164. </tm-button>
  165. <tm-button
  166. v-if="article.status == 'PUBLISHED' || article.status == 'RECYCLE'"
  167. theme="red"
  168. :shadow="0"
  169. block
  170. :height="60"
  171. size="m"
  172. @click="fnOnChangeStatus('DRAFT')"
  173. >
  174. 转为草稿
  175. </tm-button>
  176. </block>
  177. <block v-else-if="from == 'edit' && isEdit == false">
  178. <tm-button theme="light-blue" :shadow="0" block :height="60" size="m" @click="fnOnChangeCreateStatus('PUBLISHED')">立即发布</tm-button>
  179. <tm-button theme="red" :shadow="0" block :height="60" size="m" @click="fnOnChangeCreateStatus('DRAFT')">存为草稿</tm-button>
  180. </block>
  181. <block v-else>
  182. <tm-button
  183. v-if="article.status == 'DRAFT' || article.status == 'RECYCLE'"
  184. theme="light-blue"
  185. :shadow="0"
  186. block
  187. :height="60"
  188. size="m"
  189. @click="fnOnChangeStatus('PUBLISHED')"
  190. >
  191. 转为发布
  192. </tm-button>
  193. <tm-button
  194. v-if="article.status == 'PUBLISHED' || article.status == 'RECYCLE'"
  195. theme="red"
  196. :shadow="0"
  197. block
  198. :height="60"
  199. size="m"
  200. @click="fnOnChangeStatus('DRAFT')"
  201. >
  202. 转为草稿
  203. </tm-button>
  204. </block> -->
  205. <tm-button theme="blue-grey" :shadow="0" block :height="70" @click="fnOnBack()">{{ from == 'list' ? '返回列表' : '返回编辑' }}</tm-button>
  206. </view>
  207. <!-- 附件选择文件 -->
  208. <attachment-select selectType="image" v-if="attachmentsSelectShow" @on-select="fnOnAttachmentsSelect" @on-close="attachmentsSelectShow = false"></attachment-select>
  209. <!-- 新增分类 -->
  210. <tm-poup v-model="catePoupShow" position="bottom" @change="fnOnCatePoupChange" height="45vh">
  211. <view class="poup-content">
  212. <view class="poup-head text-align-center text-weight-b text-size-g ma-24">新增分类</view>
  213. <scroll-view class="poup-body pa-24 pt-0" style="height: 28vh;" :scroll-y="true" @touchmove.stop>
  214. <tm-input
  215. required
  216. :adjust-position="true"
  217. :round="3"
  218. :borderBottom="false"
  219. title="名称"
  220. bg-color="grey-lighten-5"
  221. v-model="cateForm.name"
  222. placeholder="请输入分类名称"
  223. ></tm-input>
  224. <view class="pl-32 mb-24 input-tips text-grey text-size-s">填写提示:页面上所显示的名称</view>
  225. <tm-input
  226. :borderBottom="false"
  227. :adjust-position="true"
  228. :round="3"
  229. title="别名"
  230. bg-color="grey-lighten-5"
  231. v-model="cateForm.slug"
  232. placeholder="请输入分类别名"
  233. ></tm-input>
  234. <view class="pl-32 mb-24 input-tips text-grey text-size-s">填写提示:一般为单个分类页面的标识,最好为英文</view>
  235. <!-- <tm-input
  236. :borderBottom="false"
  237. :adjust-position="true"
  238. :round="3"
  239. title="密码"
  240. bg-color="grey-lighten-5"
  241. v-model="cateForm.password"
  242. placeholder="请输入分类密码"
  243. ></tm-input>
  244. <view class="pl-32 mb-24 input-tips text-grey text-size-s">填写提示:分类的访问密码</view>
  245. <tm-input
  246. :borderBottom="false"
  247. :vertical="true"
  248. :adjust-position="true"
  249. inputType="textarea"
  250. :round="3"
  251. title="描述"
  252. :height="160"
  253. bg-color="grey-lighten-5"
  254. v-model="cateForm.description"
  255. placeholder="请输入分类描述"
  256. ></tm-input>
  257. <view class="pl-32 mb-24 input-tips text-grey text-size-s">填写提示:分类的描述信息</view> -->
  258. </scroll-view>
  259. <view class="btn-wrap flex flex-center">
  260. <tm-button size="m" theme="bg-gradient-blue-accent" @click="fnOnCateSave()">保 存</tm-button>
  261. <tm-button size="m" theme="bg-gradient-blue-grey-accent" @click="fnOnCatePoupHide()">关 闭</tm-button>
  262. </view>
  263. </view>
  264. </tm-poup>
  265. <!-- 新增标签 -->
  266. <tm-poup v-model="tagPoupShow" position="bottom" @change="fnOnTagPoupChange" height="45vh">
  267. <view class="poup-content">
  268. <view class="poup-head text-align-center text-weight-b text-size-g ma-24">新增标签</view>
  269. <scroll-view class="poup-body pa-24 pt-0" style="height: 28vh;" :scroll-y="true" @touchmove.stop>
  270. <tm-input
  271. required
  272. :adjust-position="true"
  273. :round="3"
  274. :borderBottom="false"
  275. title="标签名称"
  276. bg-color="grey-lighten-5"
  277. v-model="tagForm.name"
  278. placeholder="请输入标签名称"
  279. ></tm-input>
  280. <view class="pl-32 mb-24 input-tips text-grey text-size-s">填写提示:页面上所显示的名称</view>
  281. <tm-input
  282. :borderBottom="false"
  283. :adjust-position="true"
  284. :round="3"
  285. title="标签别名"
  286. bg-color="grey-lighten-5"
  287. v-model="tagForm.slug"
  288. placeholder="请输入分类别名"
  289. ></tm-input>
  290. <view class="pl-32 mb-24 input-tips text-grey text-size-s">填写提示:一般为单个标签页面的标识,最好为英文</view>
  291. </scroll-view>
  292. <view class="btn-wrap flex flex-center">
  293. <tm-button size="m" theme="bg-gradient-blue-accent" @click="fnOnTagSave()">保 存</tm-button>
  294. <tm-button size="m" theme="bg-gradient-blue-grey-accent" @click="fnOnTagPoupHide()">关 闭</tm-button>
  295. </view>
  296. </view>
  297. </tm-poup>
  298. </view>
  299. </view>
  300. </template>
  301. <script>
  302. import { getAdminAccessToken } from '@/utils/auth.js';
  303. import tmButton from '@/tm-vuetify/components/tm-button/tm-button.vue';
  304. import tmTabs from '@/tm-vuetify/components/tm-tabs/tm-tabs.vue';
  305. import tmForm from '@/tm-vuetify/components/tm-form/tm-form.vue';
  306. import tmGroupcheckbox from '@/tm-vuetify/components/tm-groupcheckbox/tm-groupcheckbox.vue';
  307. import tmCheckbox from '@/tm-vuetify/components/tm-checkbox/tm-checkbox.vue';
  308. import tmPickers from '@/tm-vuetify/components/tm-pickers/tm-pickers.vue';
  309. import tmInput from '@/tm-vuetify/components/tm-input/tm-input.vue';
  310. import tmPickersDate from '@/tm-vuetify/components/tm-pickersDate/tm-pickersDate.vue';
  311. import tmPoup from '@/tm-vuetify/components/tm-poup/tm-poup.vue';
  312. import tmSkeleton from '@/tm-vuetify/components/tm-skeleton/tm-skeleton.vue';
  313. import tmEmpty from '@/tm-vuetify/components/tm-empty/tm-empty.vue';
  314. import tmSwitch from '@/tm-vuetify/components/tm-switch/tm-switch.vue';
  315. import AttachmentSelect from '@/components/attachment-select/attachment-select.vue';
  316. export default {
  317. components: {
  318. tmButton,
  319. tmTabs,
  320. tmForm,
  321. tmGroupcheckbox,
  322. tmCheckbox,
  323. tmPickers,
  324. tmInput,
  325. tmPickersDate,
  326. tmPoup,
  327. tmSkeleton,
  328. tmEmpty,
  329. tmSwitch,
  330. AttachmentSelect
  331. },
  332. data() {
  333. return {
  334. isBlackTheme: false,
  335. loading: 'loading',
  336. uploadAvatarShow: false,
  337. attachmentsSelectShow: false,
  338. postsId: undefined,
  339. postTitle: undefined,
  340. createTime: undefined,
  341. isEdit: true,
  342. from: 'list',
  343. topPriority: false,
  344. sfList: [{ title: '是', checked: null }, { title: '否', checked: null }],
  345. tab: {
  346. activeIndex: 0,
  347. list: ['常规', '高级', 'SEO']
  348. },
  349. article: {
  350. title: '',
  351. slug: '',
  352. status: '',
  353. content: '',
  354. keepRaw: true,
  355. topPriority: 0,
  356. summary: '',
  357. password: '',
  358. originalContent: '',
  359. metaDescription: '',
  360. formatContent: '',
  361. editorType: 'MARKDOWN',
  362. createTime: '',
  363. categoryIds: [],
  364. tagIds: []
  365. },
  366. articleStatus: {
  367. list: [{ name: '发布', value: 'PUBLISHED' }, { name: '私有', value: 'INTIMATE' }, { name: '草稿', value: 'DRAFT' }, { name: '回收站', value: 'RECYCLE' }],
  368. status: ['PUBLISHED'],
  369. selectText: '发布',
  370. selectValue: 'PUBLISHED'
  371. },
  372. // 分类
  373. categories: [],
  374. catePoupShow: false,
  375. cateForm: {
  376. name: undefined,
  377. slug: undefined,
  378. password: undefined,
  379. description: undefined
  380. },
  381. // 标签
  382. tags: [],
  383. tagPoupShow: false,
  384. tagForm: {
  385. name: undefined,
  386. slug: undefined
  387. }
  388. };
  389. },
  390. onLoad(e) {
  391. const postsId = this.$Route.query.postsId;
  392. const postTitle = this.$Route.query.postTitle;
  393. const isEdit = this.$Route.query.isEdit;
  394. const from = this.$Route.query.from;
  395. this.postsId = postsId;
  396. this.postTitle = postTitle || '';
  397. this.isEdit = isEdit == 1 ? true : false;
  398. this.from = from;
  399. this.createTime = uni.$tm.dayjs(new Date().getTime()).format('YYYY-MM-DD HH:mm:ss');
  400. this.fnGetSettings();
  401. },
  402. onPullDownRefresh() {
  403. if (this.isEdit == false) {
  404. uni.stopPullDownRefresh();
  405. return;
  406. }
  407. this.fnGetSettings();
  408. },
  409. methods: {
  410. fnOnCatePoupChange(e) {
  411. if (!e) {
  412. this.fnResetCateForm();
  413. }
  414. },
  415. fnResetCateForm() {
  416. this.cateForm = {
  417. color: undefined,
  418. createTime: undefined,
  419. fullPath: undefined,
  420. id: undefined,
  421. name: '',
  422. slug: undefined,
  423. thumbnail: undefined,
  424. showThumbnail: undefined
  425. };
  426. },
  427. fnOnCatePoupHide() {
  428. this.catePoupShow = false;
  429. this.fnResetCateForm();
  430. },
  431. fnOnCateSave() {
  432. if (this.cateForm.name.trim() == '') {
  433. return uni.$tm.toast('分类名称未填写!');
  434. }
  435. this.$httpApi.admin
  436. .createCategory(this.cateForm)
  437. .then(res => {
  438. if (res.status == 200) {
  439. uni.$tm.toast(`保存成功!`);
  440. this.fnResetCateForm();
  441. // 将数据填充到列表
  442. this.categories.push({
  443. id: res.data.id,
  444. name: res.data.name,
  445. checked: false
  446. });
  447. } else {
  448. uni.$tm.toast('添加成功!');
  449. }
  450. })
  451. .catch(err => {
  452. uni.$tm.toast('添加失败,请重试!');
  453. });
  454. },
  455. fnOnTagPoupChange(e) {
  456. if (!e) {
  457. this.fnResetTagForm();
  458. }
  459. },
  460. fnResetTagForm() {
  461. this.tagForm = {
  462. color: undefined,
  463. createTime: undefined,
  464. fullPath: undefined,
  465. id: undefined,
  466. name: '',
  467. slug: undefined,
  468. thumbnail: undefined,
  469. showThumbnail: undefined
  470. };
  471. },
  472. fnOnTagPoupHide() {
  473. this.tagPoupShow = false;
  474. this.fnResetTagForm();
  475. },
  476. fnOnTagSave() {
  477. if (this.tagForm.name.trim() == '') {
  478. return uni.$tm.toast('标签名称未填写!');
  479. }
  480. this.$httpApi.admin
  481. .createTags(this.tagForm)
  482. .then(res => {
  483. if (res.status == 200) {
  484. uni.$tm.toast(`保存成功!`);
  485. this.fnResetTagForm();
  486. // 将数据填充到列表
  487. this.tags.push({
  488. id: res.data.id,
  489. name: res.data.name,
  490. checked: false
  491. });
  492. } else {
  493. uni.$tm.toast('添加成功!');
  494. }
  495. })
  496. .catch(err => {
  497. uni.$tm.toast('添加失败,请重试!');
  498. });
  499. },
  500. fnOnArticleStatusConfirm(e) {
  501. const status = e[0].data;
  502. this.articleStatus.status = [status.value];
  503. this.articleStatus.selectText = status.name;
  504. this.articleStatus.selectValue = status.value;
  505. },
  506. // 获取文章的设置
  507. fnGetSettings() {
  508. // this.loading = 'loading';
  509. this.loading = 'success';
  510. const _post = this.isEdit ? this.$httpApi.admin.getPostsById(this.postsId) : Promise.resolve();
  511. const _cate = this.$httpApi.admin.getCategoryList({ more: true });
  512. const _tags = this.$httpApi.admin.getTagsList({ more: true });
  513. const _apis = Promise.all([_post, _cate, _tags]);
  514. uni.showLoading({
  515. title: '加载中,请稍等...',
  516. mask: true
  517. });
  518. _apis
  519. .then(res => {
  520. if (this.isEdit) {
  521. const _postRes = res[0];
  522. this.article = _postRes.data;
  523. // 设置文章状态
  524. let _articleStatus = this.articleStatus.list.find(x => x.value == this.article.status);
  525. if (_articleStatus) {
  526. this.articleStatus.status = [_articleStatus.value];
  527. this.articleStatus.selectText = _articleStatus.name;
  528. this.articleStatus.selectValue = _articleStatus.value;
  529. }
  530. this.topPriority = this.article.topPriority == 0 ? false : true;
  531. if (this.article.thumbnail) {
  532. this.$set(this.article, 'showThumbnail', this.$utils.checkThumbnailUrl(this.article.thumbnail));
  533. }
  534. this.createTime = uni.$tm.dayjs(new Date(this.article.createTime).getTime()).format('YYYY-MM-DD HH:mm:ss');
  535. if (this.postTitle) {
  536. this.$set(this.article, 'title', this.postTitle);
  537. }
  538. }
  539. const _cateRes = res[1];
  540. let _tempCategories = _cateRes.data.map((item, index) => {
  541. let _isCateCheck = false;
  542. if (this.isEdit) {
  543. _isCateCheck = this.article.categoryIds.some(x => x == item.id);
  544. }
  545. return {
  546. index: _isCateCheck ? 0 : index + 1,
  547. id: item.id,
  548. name: item.name,
  549. checked: _isCateCheck
  550. };
  551. });
  552. if (this.isEdit) {
  553. this.categories = _tempCategories.sort((a, b) => a.index - b.index);
  554. } else {
  555. this.categories = _tempCategories;
  556. }
  557. const _tagRes = res[2];
  558. let _tempTags = _tagRes.data.map((item, index) => {
  559. let _isTagCheck = false;
  560. if (this.isEdit) {
  561. _isTagCheck = this.article.tagIds.some(x => x == item.id);
  562. }
  563. return {
  564. index: _isTagCheck ? 0 : index + 1,
  565. id: item.id,
  566. name: item.name,
  567. checked: _isTagCheck
  568. };
  569. });
  570. if (this.isEdit) {
  571. this.tags = _tempTags.sort((a, b) => a.index - b.index);
  572. } else {
  573. this.tags = _tempTags;
  574. }
  575. this.loading = 'success';
  576. uni.hideLoading();
  577. })
  578. .catch(err => {
  579. this.$tm.toast('数据加载失败,请重试!');
  580. this.loading = 'error';
  581. })
  582. .finally(() => {
  583. uni.stopPullDownRefresh();
  584. });
  585. },
  586. // 监听附件选择
  587. fnOnAttachmentsSelect(file) {
  588. this.article.thumbnail = file.path;
  589. this.$set(this.article, 'showThumbnail', this.$utils.checkThumbnailUrl(file.path));
  590. this.attachmentsSelectShow = false;
  591. },
  592. fnOnConfirmPublishTime(e) {
  593. this.createTime = `${e.year}-${e.month}-${e.day} 00:00:00`;
  594. this.article.createTime = new Date(`${e.year}-${e.month}-${e.day}`).getTime();
  595. },
  596. // 修改文章的状态
  597. fnOnChangeStatus(status) {
  598. if (this.article.status == 'INTIMATE') {
  599. this.article['password'] = '';
  600. }
  601. this.article.status = status;
  602. this.fnOnSave(true);
  603. },
  604. fnOnChangeCreateStatus(status) {
  605. this.article.status = status;
  606. this.fnOnSave();
  607. },
  608. // 保存文章设置
  609. fnOnSave(isChangeStatus = false, isBack = true) {
  610. // 校验数据
  611. if (!this.article.title) {
  612. return uni.$tm.toast('请输入文章标题!');
  613. }
  614. if (!this.article.slug) {
  615. return uni.$tm.toast('请输入文章别名!');
  616. }
  617. if (this.articleStatus.selectValue == 'INTIMATE' && !this.article.password) {
  618. return uni.$tm.toast('文章私有状态,缺少密码!');
  619. }
  620. uni.showLoading({
  621. title: '保存中...',
  622. mask: true
  623. });
  624. // 设置文章内容
  625. if (this.from == 'edit') {
  626. this.article.content = uni.getStorageSync('posts-content');
  627. this.article.formatContent = uni.getStorageSync('posts-content');
  628. this.article.originalContent = uni.getStorageSync('posts-content-source');
  629. }
  630. if (this.articleStatus.selectValue != 'INTIMATE') {
  631. this.article.password = '';
  632. }
  633. this.article.status = this.articleStatus.selectValue;
  634. this.article.topPriority = this.topPriority ? 1 : 0;
  635. this.article.tagIds = this.tags.filter(item => item.checked == true).map(item => item.id);
  636. this.article.categoryIds = this.categories.filter(item => item.checked == true).map(item => item.id);
  637. if (this.isEdit) {
  638. this.$httpApi.admin
  639. .updatePostsById(this.postsId, this.article)
  640. .then(res => {
  641. if (res.status == 200) {
  642. this.$tm.toast('保存成功!');
  643. uni.setStorageSync('posts-html-edit', '');
  644. uni.setStorageSync('posts-content', '');
  645. uni.setStorageSync('posts-content-source', '');
  646. setTimeout(() => {
  647. if (isBack) {
  648. uni.navigateBack({
  649. delta: 1
  650. });
  651. }
  652. // uni.$emit('refresh-article-list');
  653. }, 1000);
  654. } else {
  655. uni.$tm.toast('保存失败,请重试!');
  656. if (isChangeStatus) {
  657. this.article.status = this.article.status == 'DRAFT' ? 'PUBLISHED' : 'DRAFT';
  658. }
  659. }
  660. })
  661. .catch(err => {
  662. uni.$tm.toast(`保存失败,${err.message}!`);
  663. if (isChangeStatus) {
  664. this.article.status = this.article.status == 'DRAFT' ? 'PUBLISHED' : 'DRAFT';
  665. }
  666. });
  667. } else {
  668. this.$httpApi.admin
  669. .createPosts(this.article)
  670. .then(res => {
  671. if (res.status == 200) {
  672. uni.$tm.toast('发布成功!');
  673. uni.setStorageSync('posts-html-edit', '');
  674. uni.setStorageSync('posts-content', '');
  675. uni.setStorageSync('posts-content-source', '');
  676. setTimeout(() => {
  677. uni.navigateBack({
  678. delta: 2
  679. });
  680. uni.$emit('refresh-article-list');
  681. }, 1000);
  682. } else {
  683. uni.$tm.toast('发布失败,请重试!');
  684. }
  685. })
  686. .catch(err => {
  687. uni.$tm.toast(`发布失败:${err.message}`);
  688. });
  689. }
  690. },
  691. fnOnBack() {
  692. uni.$eShowModal({
  693. title: '提示',
  694. content: '您当前可能有未保存的数据,确定返回吗?',
  695. showCancel: true,
  696. cancelText: '否',
  697. cancelColor: '#999999',
  698. confirmText: '是',
  699. confirmColor: '#03a9f4'
  700. })
  701. .then(res => {
  702. uni.navigateBack();
  703. })
  704. .catch(err => {});
  705. }
  706. }
  707. };
  708. </script>
  709. <style scoped lang="scss">
  710. .app-page {
  711. padding-bottom: 110rpx;
  712. .app-page-content {
  713. min-height: calc(100vh - 340rpx);
  714. box-shadow: 0rpx 0rpx 24rpx rgba(0, 0, 0, 0.03);
  715. }
  716. .btn-bar {
  717. width: 100vw;
  718. position: fixed;
  719. left: 0;
  720. bottom: 0;
  721. box-sizing: border-box;
  722. padding: 24rpx;
  723. gap: 0 24rpx;
  724. box-shadow: 0rpx -6rpx 24rpx rgba(0, 0, 0, 0.03);
  725. }
  726. .thumbnail {
  727. width: 100%;
  728. height: 260rpx;
  729. }
  730. }
  731. .required {
  732. position: relative;
  733. padding-left: 18rpx;
  734. &:before {
  735. content: '*';
  736. position: absolute;
  737. left: -4rpx;
  738. top: 50%;
  739. transform: translateY(-50%);
  740. font-size: 34rpx;
  741. color: rgba(244, 67, 54, 1);
  742. }
  743. }
  744. .poup-content {
  745. overflow: hidden;
  746. }
  747. .poup-body {
  748. height: 60vh;
  749. box-sizing: border-box;
  750. touch-action: none;
  751. }
  752. </style>