article-setting.vue 24 KB

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