gaoshuaixing пре 4 година
родитељ
комит
263d7c85b5

+ 1 - 1
README.md

@@ -64,7 +64,7 @@
 2. 安装,node推荐v14.16.0
     ```
     # 提升安装速度,使用国内镜像;
-    npm config set registry https://registry.npm.taobao.org
+    npm config set registry https://registry.npmmirror.com
     # 进入目录 ./electron-egg/
     npm install
     ```

+ 30 - 0
app/controller/v1/example.js

@@ -124,6 +124,36 @@ class ExampleController extends BaseController {
     self.sendSuccess(data);
   }
 
+  async dbOperation() {
+    const self = this;
+    const { ctx, service } = this;
+    const paramsObj = ctx.request.body;
+    const data = {
+      action: paramsObj.action,
+      result: null,
+      all_list: []
+    };
+    
+    switch (paramsObj.action) {
+      case 'add' :
+        data.result = await service.storage.addTestData(paramsObj.info);;
+        break;
+      case 'del' :
+        data.result = await service.storage.delTestData(paramsObj.delete_name);;
+        break;
+      case 'update' :
+        data.result = await service.storage.updateTestData(paramsObj.update_name, paramsObj.update_age);
+        break;
+      case 'get' :
+        data.result = await service.storage.getTestData(paramsObj.search_age);
+        break;
+    }
+
+    data.all_list = await service.storage.getAllTestData();
+
+    self.sendSuccess(data);
+  }
+
   async addTestData() {
     const self = this;
     const { service } = this;

+ 2 - 0
app/router/example.js

@@ -41,4 +41,6 @@ module.exports = app => {
   router.post('/api/v1/example/messageShowConfirm', controller.v1.example.messageShowConfirm);
   // upload chrome extension
   router.post('/api/v1/example/uploadExtension', controller.v1.example.uploadExtension);
+  // db operation
+  router.post('/api/v1/example/dbOperation', controller.v1.example.dbOperation);
 };

+ 35 - 5
app/service/storage.js

@@ -95,8 +95,8 @@ class StorageService extends BaseService {
     const key = storageKey.TEST_DATA;
     const data = this.instance()
     .get(key)
-    .find({name: name})
-    .assign({ age: age})
+    .find({name: name}) // 修改找到的第一个数据,貌似无法批量修改 todo
+    .assign({age: age})
     .write();
 
     return data;
@@ -105,15 +105,45 @@ class StorageService extends BaseService {
   /*
    * 查 Test data
    */
-  async getTestData(name = '') {
+  async getTestData(age = 0) {
     const key = storageKey.TEST_DATA;
-    const data = this.instance()
+    let data = this.instance()
     .get(key)
-    .find({name: name})
+    //.find({age: age}) 查找单个
+    .filter(function(o) {
+      let isHas = true;
+      isHas = age === o.age ? true : false;
+      return isHas;
+    })
+    //.orderBy(['age'], ['name']) 排序
+    //.slice(0, 10) 分页
     .value();
 
+    if (_.isEmpty(data)) {
+      data = []
+    }
+
     return data;
   }
+
+  /*
+   * all Test data
+   */
+    async getAllTestData() {
+      const key = storageKey.TEST_DATA;
+      if (!this.instance().has(key).value()) {
+        this.instance().set(key, []).write();
+      }
+      let data = this.instance()
+      .get(key)
+      .value();
+  
+      if (_.isEmpty(data)) {
+        data = []
+      }
+  
+      return data;
+    }
 }
 
 module.exports = StorageService;

+ 1 - 0
frontend/src/api/main.js

@@ -13,6 +13,7 @@ const mainApi = {
   selectFileDir: '/api/v1/example/selectFileDir',
   messageShow: '/api/v1/example/messageShow',
   messageShowConfirm: '/api/v1/example/messageShowConfirm',
+  dbOperation: '/api/v1/example/dbOperation',
   testElectronApi: '/api/v1/example/testElectronApi',
 }
 

+ 5 - 0
frontend/src/config/router.config.js

@@ -29,6 +29,11 @@ export const constantRouterMap = [
             component: () => import('@/views/demo/socket/Index')
           },
           {
+            path: '/demo/db/index',
+            name: 'DemoDBIndex',
+            component: () => import('@/views/demo/db/Index')
+          },
+          {
             path: '/demo/windowview/index',
             name: 'DemoWindowViewIndex',
             component: () => import('@/views/demo/windowview/Index')

+ 6 - 0
frontend/src/layouts/DemoMenu.vue

@@ -36,6 +36,12 @@ export default {
           pageName: 'DemoSocketIndex',
           params: {}
         },
+        'menu_301' : {
+          icon: 'profile',
+          title: '数据库',
+          pageName: 'DemoDBIndex',
+          params: {}
+        },
         'menu_400' : {
           icon: 'profile',
           title: '视图',

+ 223 - 0
frontend/src/views/demo/db/Index.vue

@@ -0,0 +1,223 @@
+<template>
+  <div id="app-demo-db">
+    <div class="one-block-1">
+      <span>
+        1. 本地数据库
+      </span>
+    </div>  
+    <div class="one-block-2">
+      <a-row>
+        <a-col :span="8">
+          • LowDB本地JSON数据库
+        </a-col>
+        <a-col :span="8">
+          • 可使用lodash语法
+        </a-col>
+        <a-col :span="8">
+          • 数据文件db.json在日志同级目录
+        </a-col>
+      </a-row>
+    </div>
+    <div class="one-block-1">
+      <span>
+        2. 测试数据
+      </span>
+    </div>  
+    <div class="one-block-2">
+      <a-row>
+        <a-col :span="24">
+          {{ all_list }}
+        </a-col>
+      </a-row>
+    </div>    
+    <div class="one-block-1">
+      <span>
+        3. 添加数据
+      </span>
+    </div>  
+    <div class="one-block-2">
+      <a-row>
+        <a-col :span="6">
+          <a-input v-model="name" :value="name" addon-before="姓名" />
+        </a-col>
+        <a-col :span="3">
+        </a-col>
+        <a-col :span="6">
+          <a-input v-model="age" :value="age" addon-before="年龄" />
+        </a-col>
+        <a-col :span="3">
+        </a-col>
+        <a-col :span="6">
+          <a-button @click="dbOperation('add')">
+            添加
+          </a-button>
+        </a-col>
+      </a-row>
+    </div>
+    <div class="one-block-1">
+      <span>
+        4. 获取数据
+      </span>
+    </div>  
+    <div class="one-block-2">
+      <a-row>
+        <a-col :span="6">
+          <a-input v-model="search_age" :value="search_age" addon-before="年龄" />
+        </a-col>
+        <a-col :span="3">
+        </a-col>
+        <a-col :span="6">
+        </a-col>
+        <a-col :span="3">
+        </a-col>
+        <a-col :span="6">
+          <a-button @click="dbOperation('get')">
+            查找
+          </a-button>
+        </a-col>
+      </a-row>
+      <a-row>
+        <a-col :span="24">
+          {{ userList }}
+        </a-col>
+      </a-row>
+    </div>
+    <div class="one-block-1">
+      <span>
+        5. 修改数据
+      </span>
+    </div>  
+    <div class="one-block-2">
+      <a-row>
+        <a-col :span="6">
+          <a-input v-model="update_name" :value="update_name" addon-before="姓名" />
+        </a-col>
+        <a-col :span="3">
+        </a-col>
+        <a-col :span="6">
+          <a-input v-model="update_age" :value="update_age" addon-before="年龄" />
+        </a-col>
+        <a-col :span="3">
+        </a-col>
+        <a-col :span="6">
+          <a-button @click="dbOperation('update')">
+            更新
+          </a-button>
+        </a-col>
+      </a-row>
+    </div>
+    <div class="one-block-1">
+      <span>
+        6. 删除数据
+      </span>
+    </div>  
+    <div class="one-block-2">
+      <a-row>
+        <a-col :span="6">
+          <a-input v-model="delete_name" :value="delete_name" addon-before="姓名" />
+        </a-col>
+        <a-col :span="3">
+        </a-col>
+        <a-col :span="6">
+        </a-col>
+        <a-col :span="3">
+        </a-col>
+        <a-col :span="6">
+          <a-button @click="dbOperation('del')">
+            删除
+          </a-button>
+        </a-col>
+      </a-row>
+    </div>       
+  </div>
+</template>
+<script>
+import { localApi } from '@/api/main'
+
+export default {
+  data() {
+    return {
+      name: '张三',
+      age: 10,
+      userList: ['空'],
+      search_age: 10,
+      update_name: '张三',
+      update_age: 21,
+      delete_name: '张三',
+      all_list: ['空']
+    };
+  },
+  mounted () {
+    this.getAllTestData();
+  },
+  methods: {
+    getAllTestData () {
+      const self = this;
+      const params = {
+        action: 'all',
+      }
+      localApi('dbOperation', params).then(res => {
+        if (res.code !== 0) {
+          return false
+        }
+        if (res.data.all_list.length == 0) {
+          return false;
+        }
+        self.all_list = res.data.all_list;
+      }).catch(err => {
+        console.log('err:', err)
+      })
+    },
+    dbOperation (ac) {
+      const self = this;
+      const params = {
+        action: ac,
+        info: {
+          name: this.name,
+          age: parseInt(this.age)
+        },
+        search_age: parseInt(this.search_age),
+        update_name: this.update_name,
+        update_age: parseInt(this.update_age),
+        delete_name: this.delete_name,
+      }
+      if (ac == 'add' && this.name.length == 0) {
+        self.$message.error(`请填写数据`);
+      }
+      localApi('dbOperation', params).then(res => {
+        if (res.code !== 0) {
+          return false
+        }
+        if (ac == 'get') {
+          if (res.data.result.length == 0) {
+            self.$message.error(`没有数据`);
+            return;
+          }
+          self.userList = res.data.result;
+        }
+        if (res.data.all_list.length == 0) {
+          return;
+        }
+        self.all_list = res.data.all_list;
+        self.$message.success(`success`);
+      }).catch(err => {
+        console.log('err:', err)
+      })
+    },
+  }
+};
+</script>
+<style lang="less" scoped>
+#app-demo-db {
+  padding: 0px 10px;
+  text-align: left;
+  width: 100%;
+  .one-block-1 {
+    font-size: 16px;
+    padding-top: 10px;
+  }
+  .one-block-2 {
+    padding-top: 10px;
+  }
+}
+</style>

+ 3 - 3
package.json

@@ -1,6 +1,6 @@
 {
   "name": "electron-egg",
-  "version": "1.15.0",
+  "version": "1.16.0",
   "description": "A fast, desktop software development framework",
   "main": "main.js",
   "scripts": {
@@ -33,7 +33,7 @@
       "to": "extraResources"
     },
     "electronDownload": {
-      "mirror": "https://npm.taobao.org/mirrors/electron/"
+      "mirror": "https://npmmirror.com/mirrors/electron/"
     },
     "nsis": {
       "oneClick": false,
@@ -92,7 +92,7 @@
     "egg-bin": "^4.12.3",
     "egg-ci": "^1.11.0",
     "egg-mock": "^3.21.0",
-    "electron": "^12.0.10",
+    "electron": "^12.2.3",
     "electron-builder": "22.10.4",
     "eslint": "^5.13.0",
     "eslint-config-egg": "^7.1.0",

+ 5 - 0
update.md

@@ -1,3 +1,8 @@
+## 1.16.0
+1. 增加lowdb数据库实例代码
+2. 更新npm源
+3. 更新electron版本
+
 ## 1.15.0
 1. 增加chrome扩展程序(重点)
 2. 增加web(html)内容嵌入