123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234 |
- <template>
- <view class="main">
- <uni-forms class="form" ref="form" :rules="rules" :modelValue="form" label-width='90'>
- <uni-forms-item label="标题" required name="title">
- <uni-easyinput type="textarea" v-model="form.title" placeholder="请输入您的标题" />
- </uni-forms-item>
- <uni-forms-item label="类型" required name="type">
- <uni-data-select v-model="form.type" :localdata="typeList" @change="typechange">
- </uni-data-select>
- </uni-forms-item>
- <uni-forms-item label="内容" required name="content">
- <editor style="width: 100%; padding: 10upx;" id="editor" :placeholder="placeholder"
- @input="editorChange" @ready="onEditorReady">
- </editor>
- </uni-forms-item>
- <uni-forms-item label="封面图片" required name="file">
- <upload class='upload' :list="form.file" name="file" :count="6" @uplSuc="uplSuc" @uplDel="uplDel">
- </upload>
- </uni-forms-item>
- <uni-forms-item label="是否启用" name="is_use">
- <uni-data-select v-model="form.is_use" :localdata="is_useList" @change="is_usechange">
- </uni-data-select>
- </uni-forms-item>
- </uni-forms>
- <button class="button" type="primary" @click="submit('form')">提交</button>
- </view>
- </template>
- <script>
- import moment from 'moment';
- import upload from '../../components/upload/index.vue';
- export default {
- components: {
- upload
- },
- data() {
- return {
- id: '',
- user: {},
- form: {
- file: []
- },
- // 校验规则
- rules: {
- title: {
- rules: [{
- required: true,
- errorMessage: '请输入标题'
- }]
- },
- type: {
- rules: [{
- required: true,
- errorMessage: '请输入类型'
- }]
- },
- file: {
- rules: [{
- required: true,
- errorMessage: '请上传封面图片'
- }]
- },
- },
- placeholder: '请输入内容',
- content: '',
- // 字典表
- typeList: [],
- is_useList: []
- }
- },
- onLoad: async function(e) {
- const that = this;
- that.$set(that, `id`, e && e.id || '');
- that.searchToken();
- await that.searchOther();
- that.search();
- },
- methods: {
- searchToken() {
- const that = this;
- try {
- const res = uni.getStorageSync('token');
- if (res) that.$set(that, `user`, res);
- } catch (e) {
- uni.showToast({
- title: err.errmsg,
- icon: 'error',
- duration: 2000
- });
- }
- },
- async search() {
- const that = this;
- if (that.id) {
- const res = await that.$api(`/article/${that.id}`, 'GET', {})
- if (res.errcode == '0') {
- that.$set(that, `form`, res.data)
- } else {
- uni.showToast({
- title: res.errmsg,
- });
- }
- }
- },
- // 类型
- typechange(type) {
- const that = this;
- that.$set(that.form, `type`, type);
- },
- // 是否使用
- is_usechange(is_use) {
- const that = this;
- that.$set(that.form, `is_use`, is_use);
- },
- onEditorReady() {
- const that = this
- uni.createSelectorQuery().select('#editor').context((res) => {
- that.editorCtx = res.context
- that.editorCtx.setContents({
- html: that.form.content,
- success: (res) => {
- // console.log(res)
- },
- fail: (res) => {
- // console.log(res)
- },
- })
- }).exec()
- },
- editorChange: function(e) {
- const that = this
- that.$set(that, `content`, e.detail.html)
- },
- // 图片上传
- uplSuc(e) {
- const that = this;
- that.$set(that.form, `${e.name}`, [...that.form[e.name], e.data]);
- },
- // 图片删除
- uplDel(e) {
- const that = this;
- let data = that.form[e.name];
- let arr = data.filter((i, index) => index != e.data.index);
- that.$set(that.form, `${e.name}`, arr)
- },
- submit(ref) {
- const that = this;
- const form = that.form;
- that.$refs[ref].validate().then(async data => {
- let res;
- data.contact = that.user._id
- data.contact_name = that.user.name || that.user.nick_name
- data.create_time = moment().format('YYYY-MM-DD HH:mm:ss')
- data.status = '0'
- data.content = that.content
- if (form._id) res = await that.$api(`/article/${form._id}`, 'POST', data);
- else res = await that.$api(`/article`, 'POST', data);
- if (res.errcode == '0') {
- uni.showToast({
- title: '发布作品成功',
- icon: 'none'
- })
- uni.navigateBack({
- delta: 1
- })
- } else {
- uni.showToast({
- title: res.errmsg,
- icon: 'none'
- })
- }
- }).catch(err => {
- console.log('err', err);
- })
- },
- async searchOther() {
- const that = this;
- let res;
- // 查询类型
- res = await that.$api(`/dictData`, 'GET', {
- type: 'home_tabs',
- is_use: '0',
- })
- if (res.errcode == '0') {
- let typeList = []
- for (let val of res.data) {
- typeList.push({
- text: val.label,
- value: val.value
- })
- }
- that.$set(that, `typeList`, typeList);
- }
- // 是否启用
- res = await that.$api(`/dictData`, 'GET', {
- type: 'is_use',
- is_use: '0',
- })
- if (res.errcode == '0') {
- let is_useList = []
- for (let val of res.data) {
- is_useList.push({
- text: val.label,
- value: val.value
- })
- }
- that.$set(that, `is_useList`, is_useList);
- }
- }
- }
- }
- </script>
- <style lang="scss" scoped>
- .main {
- padding: 4vw;
- .form-item {
- display: flex;
- align-items: center;
- }
- #editor {
- width: 100%;
- height: 100px;
- border: 1px solid #F0F0F0;
- }
- .button {
- background-color: var(--f3CColor);
- }
- }
- </style>
|