add.vue 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <template>
  2. <view class="main">
  3. <uni-forms class="form" ref="form" :rules="rules" :modelValue="form" label-width='90'>
  4. <uni-forms-item label="标题" required name="title">
  5. <uni-easyinput type="textarea" v-model="form.title" placeholder="请输入您的标题" />
  6. </uni-forms-item>
  7. <uni-forms-item label="类型" required name="type">
  8. <uni-data-select v-model="form.type" :localdata="typeList" @change="typechange">
  9. </uni-data-select>
  10. </uni-forms-item>
  11. <uni-forms-item label="内容" required name="content">
  12. <editor style="width: 100%; padding: 10upx;" id="editor" :placeholder="placeholder"
  13. @input="editorChange" @ready="onEditorReady">
  14. </editor>
  15. </uni-forms-item>
  16. <uni-forms-item label="封面图片" required name="file">
  17. <upload class='upload' :list="form.file" name="file" :count="6" @uplSuc="uplSuc" @uplDel="uplDel">
  18. </upload>
  19. </uni-forms-item>
  20. <uni-forms-item label="是否启用" name="is_use">
  21. <uni-data-select v-model="form.is_use" :localdata="is_useList" @change="is_usechange">
  22. </uni-data-select>
  23. </uni-forms-item>
  24. </uni-forms>
  25. <button class="button" type="primary" @click="submit('form')">提交</button>
  26. </view>
  27. </template>
  28. <script>
  29. import moment from 'moment';
  30. import upload from '../../components/upload/index.vue';
  31. export default {
  32. components: {
  33. upload
  34. },
  35. data() {
  36. return {
  37. id: '',
  38. user: {},
  39. form: {
  40. file: []
  41. },
  42. // 校验规则
  43. rules: {
  44. title: {
  45. rules: [{
  46. required: true,
  47. errorMessage: '请输入标题'
  48. }]
  49. },
  50. type: {
  51. rules: [{
  52. required: true,
  53. errorMessage: '请输入类型'
  54. }]
  55. },
  56. file: {
  57. rules: [{
  58. required: true,
  59. errorMessage: '请上传封面图片'
  60. }]
  61. },
  62. },
  63. placeholder: '请输入内容',
  64. content: '',
  65. // 字典表
  66. typeList: [],
  67. is_useList: []
  68. }
  69. },
  70. onLoad: async function(e) {
  71. const that = this;
  72. that.$set(that, `id`, e && e.id || '');
  73. that.searchToken();
  74. await that.searchOther();
  75. that.search();
  76. },
  77. methods: {
  78. searchToken() {
  79. const that = this;
  80. try {
  81. const res = uni.getStorageSync('token');
  82. if (res) {
  83. that.$set(that, `user`, res);
  84. }
  85. } catch (e) {
  86. uni.showToast({
  87. title: err.errmsg,
  88. icon: 'error',
  89. duration: 2000
  90. });
  91. }
  92. },
  93. async search() {
  94. const that = this;
  95. if (that.id) {
  96. const res = await that.$api(`/article/${that.id}`, 'GET', {})
  97. if (res.errcode == '0') {
  98. that.$set(that, `form`, res.data)
  99. } else {
  100. uni.showToast({
  101. title: res.errmsg,
  102. });
  103. }
  104. }
  105. },
  106. // 类型
  107. typechange(type) {
  108. const that = this;
  109. that.$set(that.form, `type`, type);
  110. },
  111. // 是否使用
  112. is_usechange(is_use) {
  113. const that = this;
  114. that.$set(that.form, `is_use`, is_use);
  115. },
  116. onEditorReady() {
  117. const that = this
  118. uni.createSelectorQuery().select('#editor').context((res) => {
  119. that.editorCtx = res.context
  120. that.editorCtx.setContents({
  121. html: that.form.content,
  122. success: (res) => {
  123. // console.log(res)
  124. },
  125. fail: (res) => {
  126. // console.log(res)
  127. },
  128. })
  129. }).exec()
  130. },
  131. editorChange: function(e) {
  132. const that = this
  133. that.$set(that, `content`, e.detail.html)
  134. },
  135. // 图片上传
  136. uplSuc(e) {
  137. const that = this;
  138. that.$set(that.form, `${e.name}`, [...that.form[e.name], e.data]);
  139. },
  140. // 图片删除
  141. uplDel(e) {
  142. const that = this;
  143. let data = that.form[e.name];
  144. let arr = data.filter((i, index) => index != e.data.index);
  145. that.$set(that.form, `${e.name}`, arr)
  146. },
  147. submit(ref) {
  148. const that = this;
  149. const form = that.form;
  150. that.$refs[ref].validate().then(async data => {
  151. let res;
  152. data.contact = that.user._id
  153. data.contact_name = that.user.name || that.user.nick_name
  154. data.create_time = moment().format('YYYY-MM-DD HH:mm:ss')
  155. data.status = '0'
  156. data.content = that.content
  157. if (form._id) res = await that.$api(`/article/${form._id}`, 'POST', data);
  158. else res = await that.$api(`/article`, 'POST', data);
  159. if (res.errcode == '0') {
  160. uni.showToast({
  161. title: '发布作品成功',
  162. icon: 'none'
  163. })
  164. uni.navigateBack({
  165. delta: 1
  166. })
  167. } else {
  168. uni.showToast({
  169. title: res.errmsg,
  170. icon: 'none'
  171. })
  172. }
  173. }).catch(err => {
  174. console.log('err', err);
  175. })
  176. },
  177. async searchOther() {
  178. const that = this;
  179. let res;
  180. // 查询类型
  181. res = await that.$api(`/dictData`, 'GET', {
  182. type: 'home_tabs',
  183. is_use: '0',
  184. })
  185. if (res.errcode == '0') {
  186. let typeList = []
  187. for (let val of res.data) {
  188. typeList.push({
  189. text: val.label,
  190. value: val.value
  191. })
  192. }
  193. that.$set(that, `typeList`, typeList);
  194. }
  195. // 是否启用
  196. res = await that.$api(`/dictData`, 'GET', {
  197. type: 'is_use',
  198. is_use: '0',
  199. })
  200. if (res.errcode == '0') {
  201. let is_useList = []
  202. for (let val of res.data) {
  203. is_useList.push({
  204. text: val.label,
  205. value: val.value
  206. })
  207. }
  208. that.$set(that, `is_useList`, is_useList);
  209. }
  210. }
  211. }
  212. }
  213. </script>
  214. <style lang="scss" scoped>
  215. .main {
  216. padding: 4vw;
  217. .form-item {
  218. display: flex;
  219. align-items: center;
  220. }
  221. #editor {
  222. width: 100%;
  223. height: 100px;
  224. border: 1px solid #F0F0F0;
  225. }
  226. .button {
  227. background-color: var(--f3CColor);
  228. }
  229. }
  230. </style>