picTextAdd.vue 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <template>
  2. <view class="app-content">
  3. <form @submit="formSubmit" @reset="formReset">
  4. <view class="module-com input-line-wrap">
  5. <tui-list-cell class="line-cell" :hover="false">
  6. <view class="tui-title required">标题</view>
  7. <input v-model="form.title" type="text" placeholder-class="phcolor" placeholder-style="color:#CCCCCC" placeholder="请输入" class="tui-input" name="title" />
  8. </tui-list-cell>
  9. <view class="editor-container">
  10. <rich-media-editor
  11. ref="richEditor"
  12. v-model="form.imageTextVos"
  13. @change="onContentChange"
  14. :action="getLoginInfo.imgUploadApi"
  15. />
  16. </view>
  17. <view class="confirm-btn">
  18. <button class="admin-button-com big" @click="previewContent">预览</button>
  19. <button class="admin-button-com big blue" formType="submit">确认</button>
  20. </view>
  21. </view>
  22. </form>
  23. </view>
  24. </template>
  25. <script>
  26. import TuiListCell from "@/components/plugin/list-cell";
  27. import RichMediaEditor from "@/components/RichMediaEditor/index.vue";
  28. const form = require("@/utils/formValidation.js");
  29. import { getPicText, createPicText, updatePicText } from "@/api/pic-text";
  30. export default {
  31. name: "picTextAdd",
  32. components: {
  33. TuiListCell,
  34. RichMediaEditor
  35. },
  36. data() {
  37. return {
  38. form: {
  39. title: '',
  40. imageTextVos: [] // 新增图文内容数组
  41. },
  42. isEdit: false
  43. };
  44. },
  45. onLoad(option) {
  46. if (option.id) {
  47. this.isEdit = true;
  48. // 设置页面标题
  49. uni.setNavigationBarTitle({
  50. title: '编辑图文'
  51. })
  52. this._getDet();
  53. } else {
  54. uni.setNavigationBarTitle({
  55. title: '新增图文'
  56. })
  57. }
  58. },
  59. methods: {
  60. init(){
  61. },
  62. // 获取详情
  63. _getDet() {
  64. getPicText({ id: this.option.id }).then(res => {
  65. if (this.$util.isEmpty(res.data)) return;
  66. // 确保将字符串转换成数组
  67. let content = [];
  68. if (typeof res.data.content === 'string') {
  69. try {
  70. content = JSON.parse(res.data.content);
  71. } catch (e) {
  72. console.error('解析图文内容失败:', e);
  73. content = [];
  74. }
  75. } else if (Array.isArray(res.data.content)) {
  76. content = res.data.content;
  77. }
  78. // 处理内容数据,确保每个项目都有正确的格式
  79. content = content.map(item => {
  80. // 确保每个项目都有type和content属性
  81. return {
  82. type: item.type || 3, // 默认为文本类型
  83. content: item.content || ''
  84. };
  85. });
  86. this.form = {
  87. title: res.data.title || '',
  88. imageTextVos: content
  89. };
  90. // 触发富文本编辑器内容更新
  91. this.$nextTick(() => {
  92. // 确保组件已经更新数据后再触发更新
  93. setTimeout(() => {
  94. if (this.$refs.richEditor) {
  95. console.log('更新编辑器内容');
  96. this.$refs.richEditor.updateContent(this.form.imageTextVos);
  97. } else {
  98. console.error('找不到richEditor引用');
  99. }
  100. }, 100);
  101. });
  102. });
  103. },
  104. // 图文内容变化处理
  105. onContentChange(content) {
  106. this.form.imageTextVos = content;
  107. },
  108. // 预览内容
  109. previewContent() {
  110. console.log('点击了预览按钮');
  111. console.log('当前图文内容:', this.form.imageTextVos);
  112. if (this.form.imageTextVos.length === 0) {
  113. // console.log('内容为空,使用示例数据进行预览');
  114. // 如果没有内容,使用示例数据进行预览
  115. this.form.imageTextVos = [
  116. {
  117. type: 3,
  118. content: "这是一个预览示例,因为当前没有添加任何内容。"
  119. }
  120. ];
  121. }
  122. // 准备预览数据
  123. const previewData = {
  124. title: this.form.title || '未命名图文',
  125. content: this.form.imageTextVos
  126. };
  127. console.log('准备预览数据:', previewData);
  128. // 通过全局数据传递
  129. const app = getApp();
  130. app.globalData = app.globalData || {};
  131. app.globalData.previewData = previewData;
  132. console.log('全局数据已设置:', app.globalData.previewData);
  133. // 跳转到预览页面
  134. uni.navigateTo({
  135. url: './preview',
  136. success: (res) => {
  137. console.log('跳转成功:', res);
  138. },
  139. fail: (err) => {
  140. console.error('跳转失败:', err);
  141. this.$msg('页面跳转失败,请检查路由配置');
  142. }
  143. });
  144. },
  145. // 提交
  146. confirmFn() {
  147. uni.showLoading({mask: true});
  148. let that = this;
  149. let apiCall = this.isEdit ? updatePicText : createPicText;
  150. // 准备提交参数
  151. let params = {
  152. title: this.form.title,
  153. content: JSON.stringify(this.form.imageTextVos),
  154. type: this.option.type
  155. };
  156. // 编辑模式需要添加ID
  157. if (this.isEdit) {
  158. params.id = this.option.id;
  159. }
  160. apiCall(params).then(res => {
  161. uni.hideLoading();
  162. if(res.code == 1){
  163. uni.$emit('refreshListPage');
  164. that.$msg(this.isEdit ? '修改成功' : '添加成功');
  165. uni.navigateBack({
  166. delta: 1
  167. });
  168. } else {
  169. that.$msg(res.message || '操作失败');
  170. }
  171. }).catch(err => {
  172. uni.hideLoading();
  173. console.error('提交失败:', err);
  174. that.$msg('提交失败,请稍后重试');
  175. });
  176. },
  177. formSubmit(e) {
  178. let rules = [
  179. { name: "title", rule: ["required", "notEmpty"], msg: ["请输入标题"] },
  180. //{ name: "imageTextVos", rule: ["notEmpty"], msg: ["请添加图文内容"] }
  181. ];
  182. let formData = e.detail.value;
  183. // 将图文内容添加到表单数据中进行验证
  184. // formData.imageTextVos = this.form.imageTextVos;
  185. let checkRes = form.validation(formData, rules);
  186. // 检查图文内容
  187. if (this.form.imageTextVos.length === 0) {
  188. this.$msg("请添加至少一项图文内容");
  189. return;
  190. }
  191. if (!checkRes) {
  192. this.confirmFn();
  193. } else {
  194. this.$msg(checkRes);
  195. }
  196. },
  197. formReset() {
  198. this.form = {
  199. title: '',
  200. imageTextVos: []
  201. };
  202. }
  203. }
  204. };
  205. </script>
  206. <style lang="scss" scoped>
  207. .app-content {
  208. min-height: calc(100vh - 20upx);
  209. padding-top: 20upx;
  210. padding: 12upx;
  211. background-color: #f5f5f5;
  212. }
  213. .module-com {
  214. margin-bottom: 20upx;
  215. }
  216. .confirm-btn {
  217. display: flex;
  218. gap: 30rpx;
  219. margin: 40rpx 30rpx 20rpx;
  220. .admin-button-com {
  221. flex: 1;
  222. }
  223. }
  224. .editor-container {
  225. margin-bottom: 20rpx;
  226. }
  227. </style>