Browse Source

feat:添加新闻资讯管理部分代码

xzh 4 năm trước cách đây
mục cha
commit
da6592c57c

Những thai đổi đã bị hủy bỏ vì nó quá lớn
+ 173 - 0
common/data/news.js


+ 4 - 0
pc/build/webpack.dev.conf.js

@@ -49,6 +49,10 @@ const devWebpackConfig = merge(baseWebpackConfig, {
       '/api': {
         target: 'https://test.4dkankan.com',
         changeOrigin: true,
+      },
+      '/node-upload': {
+        target: 'http://vrhouse2.4dkankan.com',
+        changeOrigin: true,
       }
     },
     setup: wepackDevServer

+ 9 - 3
pc/build/wepack.dev.server.js

@@ -8,7 +8,6 @@ module.exports = function(app, server) {
 
     app.get('/dev/lang/:id', function(req, res) {
         let filePath = path.join(langDir, req.params.id + '/modules')
-        console.log(filePath)
         const files = fs.readdirSync(filePath)
         let data = {}
         files.forEach(item => {
@@ -20,9 +19,16 @@ module.exports = function(app, server) {
     app.post('/dev/lang/:id', async function(req, res) {
       await Object.keys(req.body).forEach(async item => {
         await fs.writeFile('./src/lang/' + req.params.id + `/modules/${item}.js`, 'module.exports = ' + JSON.stringify(req.body[item], null, 4), err => {
-          
+          res.json({ ok: true });
         })
       })
-      res.json({ ok: true });
+     
     });
+
+    app.post('/dev/news/update', async function(req, res) {
+      const data = req.body.data
+      await fs.writeFile('./../common/data/news.js', `${JSON.stringify(data, null, 4)}`, err => {
+        res.json({ ok: true });
+      })
+    })
 }

+ 33 - 30
pc/src/components/dev/components/News.vue

@@ -18,14 +18,19 @@
                     >{{item.text}}</option>
                 </select> -->
             </span>
-            <button >保存</button>
+            <button @click="onSave">保存</button>
         </template>
         <template slot="content">
             <ul class="menus">
                 <li v-for="(item,key) in News" :key="key">
                   <h1>第{{key + 1}}篇</h1>
+                    <input type="file" @change="handleImgChange(key, $event)" />
                     <img :src="item.img" />
                     <div>
+                      <p>排序</p>
+                      <input v-model="item.sort" type="number" />
+                    </div>
+                    <div>
                       <p>标题</p>
                       <input v-model="item.title" type="text" />
                     </div>
@@ -75,24 +80,8 @@ export default {
         }
     },
     created() {
-        this.load();
     },
     methods: {
-        async load(curr) {
-            const result = await http.get("../dev/lang/" + (curr || this.curr));
-            if (result.data.ok) {
-                if (this.lang == null) {
-                    this.lang = result.data.data;
-                } else {
-                    this.lang = deepExtend(this.lang, result.data.data);
-                }
-            } else {
-                if (this.curr != "zh") {
-                    this.lang = null;
-                    this.load("zh");
-                }
-            }
-        },
         isChildren(key, value) {
             return key !== "__name" && typeof value === "string";
         },
@@ -109,19 +98,33 @@ export default {
             }
         },
         onSave() {
-            // http.post(`../dev/lang/${this.curr}`, this.lang)
-            // .then(response => {
-            //     if (response.data.ok) {
-            //         this.$alert('保存成功', {
-            //             icon: 'success'
-            //         });
-            //     } else {
-            //         this.$alert('保存失败');
-            //     }
-            // })
-            // .catch(err => {
-            //     this.$alert('保存失败');
-            // });
+            http.post(`../dev/news/update`, { data: this.News, lang: 'zh'})
+            .then(response => {
+                if (response.data.ok) {
+                    this.$alert('保存成功', {
+                        icon: 'success'
+                    });
+                } else {
+                    this.$alert('保存失败');
+                }
+            })
+            .catch(err => {
+                this.$alert('保存失败');
+            });
+        },
+        handleImgChange (index, e) {
+            const file = e.target.files[0]
+            let formdata = new FormData()
+            formdata.append('file', file)
+            http.post(`../node-upload/uploadfile`, formdata)
+            .then(res => {
+                const url = res.data.data.url
+                this.News[index].img = url
+            })
+            .catch(err => {
+                console.error(err)
+                this.$alert('图片上传失败')
+            });
         }
     }
 };

+ 1 - 1
pc/src/util/http.js

@@ -19,7 +19,7 @@ axios.defaults.retryDelay = 1000
 
 // 拦截请求,做登陆,或head处理
 axios.interceptors.request.use(function (config) {
-  if (config.method === 'post') {
+  if (config.method === 'post' && Object.prototype.toString.call(config.data) !== '[object FormData]') {
     config.data = {
       ...config.data
     }