picTextAdd-Edit.vue 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. <template>
  2. <view class="app-content">
  3. <form @submit="formSubmit">
  4. <view class="module-com">
  5. <view class="editor-container">
  6. <rich-media-editor
  7. ref="richEditor"
  8. v-model="form.imageTextVos"
  9. @change="onContentChange"
  10. @collect-delete="onCollectDelete"
  11. :action="getLoginInfo.imgUploadApi"
  12. :deferDelete="isEdit"
  13. />
  14. </view>
  15. <view class="confirm-btn">
  16. <button class="admin-button-com big" @click="previewContent">预览</button>
  17. <button class="admin-button-com big blue" formType="submit">确认</button>
  18. </view>
  19. </view>
  20. </form>
  21. </view>
  22. </template>
  23. <script>
  24. import TuiListCell from '@/components/plugin/list-cell';
  25. import RichMediaEditor from '@/components/RichMediaEditor/index.vue';
  26. const form = require('@/utils/formValidation.js');
  27. import { getItemDesc, createItemDesc, updateItemDesc } from '@/api/item/picText.js';
  28. export default {
  29. name: 'picTextAdd',
  30. components: {
  31. TuiListCell,
  32. RichMediaEditor
  33. },
  34. data() {
  35. return {
  36. form: {
  37. title: '',
  38. imageTextVos: [] // 图文内容数组
  39. },
  40. isEdit: false,
  41. descId: null, // 图文简介ID
  42. itemId: null, // 花材ID
  43. // 编辑模式下,暂存删除的图片路径(短路径)
  44. delImgQueue: []
  45. };
  46. },
  47. async onLoad(option) {
  48. try {
  49. if (option && option.itemId) {
  50. this.itemId = option.itemId;
  51. const res = await this.getDetDesc();
  52. if (res === 'create') {
  53. this.isEdit = false;
  54. uni.setNavigationBarTitle({
  55. title: '创建简介'
  56. });
  57. } else {
  58. this.isEdit = true;
  59. uni.setNavigationBarTitle({
  60. title: '修改简介'
  61. });
  62. }
  63. } else {
  64. this.$msg('缺少商品ID参数');
  65. uni.navigateBack();
  66. }
  67. } catch (error) {
  68. console.error('页面加载错误:', error);
  69. this.isEdit = false;
  70. uni.setNavigationBarTitle({
  71. title: '创建简介'
  72. });
  73. this.$msg('页面加载失败,请重试');
  74. }
  75. },
  76. methods: {
  77. init() {},
  78. // 获取描述
  79. async getDetDesc() {
  80. try {
  81. const res = await getItemDesc({ itemId: this.itemId });
  82. if (res.code == 0 || this.$util.isEmpty(res.data)) {
  83. return 'create';
  84. }
  85. if(res.code == 1 && res.data == 'not_exist'){
  86. return 'create';
  87. }
  88. this.descId = res.data.id;
  89. // 确保将字符串转换成数组
  90. let content = [];
  91. if (typeof res.data.content === 'string') {
  92. try {
  93. content = JSON.parse(res.data.content);
  94. } catch (e) {
  95. console.error('解析图文内容失败:', e);
  96. content = [];
  97. }
  98. } else if (Array.isArray(res.data.content)) {
  99. content = res.data.content;
  100. }
  101. // 处理内容数据,确保每个项目都有正确的格式
  102. content = content.map((item) => {
  103. return {
  104. type: item.type || 3, // 默认为文本类型
  105. content: item.content || ''
  106. };
  107. });
  108. this.form = {
  109. title: res.data.title || '',
  110. imageTextVos: content
  111. };
  112. // 触发富文本编辑器内容更新
  113. this.$nextTick(() => {
  114. setTimeout(() => {
  115. if (this.$refs.richEditor) {
  116. this.$refs.richEditor.updateContent(this.form.imageTextVos);
  117. }
  118. }, 50);
  119. });
  120. return 'edit';
  121. } catch (error) {
  122. console.error('获取描述失败:', error);
  123. return 'create';
  124. }
  125. },
  126. // 图文内容变化处理
  127. onContentChange(content) {
  128. this.form.imageTextVos = content;
  129. },
  130. // 预览内容
  131. previewContent() {
  132. if (this.form.imageTextVos.length === 0) {
  133. this.form.imageTextVos = [
  134. {
  135. type: 3,
  136. content: '这是一个预览示例,因为当前没有添加任何内容。'
  137. }
  138. ];
  139. }
  140. // 准备预览数据
  141. const previewData = {
  142. title: this.form.title || '未命名图文',
  143. content: this.form.imageTextVos
  144. };
  145. // 通过全局数据传递
  146. const app = getApp();
  147. app.globalData = app.globalData || {};
  148. app.globalData.previewData = previewData;
  149. // 跳转到预览页面
  150. uni.navigateTo({
  151. url: '/admin/picText/preview',
  152. success: (res) => {
  153. console.log('跳转成功:', res);
  154. },
  155. fail: (err) => {
  156. console.error('跳转失败:', err);
  157. this.$msg('预览页面跳转失败');
  158. }
  159. });
  160. },
  161. // 提交
  162. confirmFn() {
  163. uni.showLoading({ mask: true });
  164. let that = this;
  165. let apiCall = this.isEdit ? updateItemDesc : createItemDesc;
  166. // 准备提交参数
  167. let params = {
  168. title: this.form.title,
  169. content: JSON.stringify(this.form.imageTextVos)
  170. };
  171. // 编辑模式需要添加简介ID,创建模式需要关联商品ID
  172. if (this.isEdit) {
  173. params.id = this.descId;
  174. params.itemId = this.option.itemId;
  175. } else {
  176. params.itemId = this.option.itemId;
  177. }
  178. apiCall(params)
  179. .then((res) => {
  180. uni.hideLoading();
  181. if (res.code == 1) {
  182. uni.$emit('refreshListPage');
  183. that.$msg(this.isEdit ? '修改成功' : '添加成功');
  184. // 编辑模式下,如存在待删图片,统一调用批量删除
  185. if (this.isEdit && this.delImgQueue.length > 0) {
  186. const { delImages } = require('@/api/pic-text');
  187. const filePaths = Array.from(new Set(this.delImgQueue))
  188. delImages({ filePaths }).then(() => {
  189. this.delImgQueue = []
  190. }).catch(() => {})
  191. }
  192. uni.navigateBack({
  193. delta: 1
  194. });
  195. } else {
  196. that.$msg(res.message || '操作失败');
  197. }
  198. })
  199. .catch((err) => {
  200. uni.hideLoading();
  201. console.error('提交失败:', err);
  202. that.$msg('提交失败,请稍后重试');
  203. });
  204. },
  205. formSubmit(e) {
  206. let rules = [
  207. //{ name: 'title', rule: ['required', 'notEmpty'], msg: ['请输入标题'] }
  208. //{ name: "imageTextVos", rule: ["notEmpty"], msg: ["请添加图文内容"] }
  209. ];
  210. let formData = e.detail.value;
  211. // 将图文内容添加到表单数据中进行验证
  212. // formData.imageTextVos = this.form.imageTextVos;
  213. let checkRes = form.validation(formData, rules);
  214. // 检查图文内容
  215. if (this.form.imageTextVos.length === 0) {
  216. this.$msg('请添加至少一项图文内容');
  217. return;
  218. }
  219. if (!checkRes) {
  220. this.confirmFn();
  221. } else {
  222. this.$msg(checkRes);
  223. }
  224. },
  225. // 收集编辑模式下的删除图片
  226. onCollectDelete(payload){
  227. if (!this.isEdit) return;
  228. if (!payload || !Array.isArray(payload.filePaths)) return;
  229. payload.filePaths.forEach(p => {
  230. if (p && !this.delImgQueue.includes(p)) this.delImgQueue.push(p)
  231. })
  232. },
  233. }
  234. };
  235. </script>
  236. <style lang="scss" scoped>
  237. .app-content {
  238. min-height: calc(100vh - 20rpx);
  239. padding: 12rpx;
  240. background-color: #f5f5f5;
  241. }
  242. .module-com {
  243. margin-bottom: 20rpx;
  244. }
  245. .confirm-btn {
  246. display: flex;
  247. gap: 40rpx;
  248. margin: 40rpx 30rpx 20rpx;
  249. .admin-button-com {
  250. flex: 1;
  251. }
  252. }
  253. .editor-container {
  254. margin-bottom: 20rpx;
  255. }
  256. </style>