add.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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) that.$set(that, `user`, res);
  83. } catch (e) {
  84. uni.showToast({
  85. title: err.errmsg,
  86. icon: 'error',
  87. duration: 2000
  88. });
  89. }
  90. },
  91. async search() {
  92. const that = this;
  93. if (that.id) {
  94. const res = await that.$api(`/article/${that.id}`, 'GET', {})
  95. if (res.errcode == '0') {
  96. that.$set(that, `form`, res.data)
  97. } else {
  98. uni.showToast({
  99. title: res.errmsg,
  100. });
  101. }
  102. }
  103. },
  104. // 类型
  105. typechange(type) {
  106. const that = this;
  107. that.$set(that.form, `type`, type);
  108. },
  109. // 是否使用
  110. is_usechange(is_use) {
  111. const that = this;
  112. that.$set(that.form, `is_use`, is_use);
  113. },
  114. onEditorReady() {
  115. const that = this
  116. uni.createSelectorQuery().select('#editor').context((res) => {
  117. that.editorCtx = res.context
  118. that.editorCtx.setContents({
  119. html: that.form.content,
  120. success: (res) => {
  121. // console.log(res)
  122. },
  123. fail: (res) => {
  124. // console.log(res)
  125. },
  126. })
  127. }).exec()
  128. },
  129. editorChange: function(e) {
  130. const that = this
  131. that.$set(that, `content`, e.detail.html)
  132. },
  133. // 图片上传
  134. uplSuc(e) {
  135. const that = this;
  136. that.$set(that.form, `${e.name}`, [...that.form[e.name], e.data]);
  137. },
  138. // 图片删除
  139. uplDel(e) {
  140. const that = this;
  141. let data = that.form[e.name];
  142. let arr = data.filter((i, index) => index != e.data.index);
  143. that.$set(that.form, `${e.name}`, arr)
  144. },
  145. submit(ref) {
  146. const that = this;
  147. const form = that.form;
  148. that.$refs[ref].validate().then(async data => {
  149. let res;
  150. data.contact = that.user._id
  151. data.contact_name = that.user.name || that.user.nick_name
  152. data.create_time = moment().format('YYYY-MM-DD HH:mm:ss')
  153. data.status = '0'
  154. data.content = that.content
  155. if (form._id) res = await that.$api(`/article/${form._id}`, 'POST', data);
  156. else res = await that.$api(`/article`, 'POST', data);
  157. if (res.errcode == '0') {
  158. uni.showToast({
  159. title: '发布作品成功',
  160. icon: 'none'
  161. })
  162. uni.navigateBack({
  163. delta: 1
  164. })
  165. } else {
  166. uni.showToast({
  167. title: res.errmsg,
  168. icon: 'none'
  169. })
  170. }
  171. }).catch(err => {
  172. console.log('err', err);
  173. })
  174. },
  175. async searchOther() {
  176. const that = this;
  177. let res;
  178. // 查询类型
  179. res = await that.$api(`/dictData`, 'GET', {
  180. type: 'home_tabs',
  181. is_use: '0',
  182. })
  183. if (res.errcode == '0') {
  184. let typeList = []
  185. for (let val of res.data) {
  186. typeList.push({
  187. text: val.label,
  188. value: val.value
  189. })
  190. }
  191. that.$set(that, `typeList`, typeList);
  192. }
  193. // 是否启用
  194. res = await that.$api(`/dictData`, 'GET', {
  195. type: 'is_use',
  196. is_use: '0',
  197. })
  198. if (res.errcode == '0') {
  199. let is_useList = []
  200. for (let val of res.data) {
  201. is_useList.push({
  202. text: val.label,
  203. value: val.value
  204. })
  205. }
  206. that.$set(that, `is_useList`, is_useList);
  207. }
  208. }
  209. }
  210. }
  211. </script>
  212. <style lang="scss" scoped>
  213. .main {
  214. padding: 4vw;
  215. .form-item {
  216. display: flex;
  217. align-items: center;
  218. }
  219. #editor {
  220. width: 100%;
  221. height: 100px;
  222. border: 1px solid #F0F0F0;
  223. }
  224. .button {
  225. background-color: var(--f3CColor);
  226. }
  227. }
  228. </style>