{"id":13671665,"url":"https://github.com/wuyanwen/thinkjsplus","last_synced_at":"2025-07-14T03:31:51.317Z","repository":{"id":95167799,"uuid":"104048100","full_name":"wuyanwen/thinkjsplus","owner":"wuyanwen","description":"thinkjs3.0 从入门到实战","archived":false,"fork":false,"pushed_at":"2017-10-05T13:28:14.000Z","size":17187,"stargazers_count":103,"open_issues_count":2,"forks_count":34,"subscribers_count":6,"default_branch":"master","last_synced_at":"2024-11-11T09:44:05.552Z","etag":null,"topics":["bootstrap","mysql","thinkjs","thinkjs3"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/wuyanwen.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2017-09-19T08:28:10.000Z","updated_at":"2024-04-17T05:51:46.000Z","dependencies_parsed_at":"2023-03-10T08:00:36.496Z","dependency_job_id":null,"html_url":"https://github.com/wuyanwen/thinkjsplus","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wuyanwen%2Fthinkjsplus","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wuyanwen%2Fthinkjsplus/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wuyanwen%2Fthinkjsplus/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wuyanwen%2Fthinkjsplus/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/wuyanwen","download_url":"https://codeload.github.com/wuyanwen/thinkjsplus/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":225944519,"owners_count":17549414,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bootstrap","mysql","thinkjs","thinkjs3"],"created_at":"2024-08-02T09:01:15.846Z","updated_at":"2024-11-22T18:27:12.558Z","avatar_url":"https://github.com/wuyanwen.png","language":"JavaScript","readme":"#### 1.安装 ThinkJS 命令\n``` \nnpm install -g think-cli\n```\n\u003e 可以通过 thinkjs -v 查看 think-cli 的版本号，此版本号非 thinkjs 的版本号\n\n#### 2.创建项目 thinkjsPlus\n```\nthinkjs new  thinkjsPlus\n```\n\u003e 创建项目时可以指定 --mode=module 参数创建多模块项目(thinkjs new  thinkjsPlus --mode=module ),本项目没用此参数.\n#### 3.项目结构\n```\n|--- development.js   //开发环境下的入口文件\n|--- nginx.conf  //nginx 配置文件\n|--- package.json\n|--- pm2.json //pm2 配置文件\n|--- production.js //生产环境下的入口文件\n|--- README.md\n|--- src\n| |--- bootstrap  //启动自动执行目录 \n| | |--- master.js //Master 进程下自动执行\n| | |--- worker.js //Worker 进程下自动执行\n| |--- config  //配置文件目录\n| | |--- adapter.js  // adapter 配置文件 \n| | |--- config.js  // 默认配置文件 \n| | |--- config.production.js  //生产环境下的默认配置文件，和 config.js 合并 \n| | |--- extend.js  //extend 配置文件 \n| | |--- middleware.js //middleware 配置文件 \n| | |--- router.js //自定义路由配置文件\n| |--- controller  //控制器目录 \n| | |--- base.js\n| | |--- index.js\n| |--- logic //logic 目录\n| | |--- index.js\n| |--- model //模型目录\n| | |--- index.js\n|--- view  //模板目录\n| |--- index_index.html\n|--- www\n| |--- static  //静态资源目录\n| | |--- css\n| | |--- img\n| | |--- js\n```\n\n#### 4. 项目服务启动\n\u003e 入口文件是 development.js，启动时直接和一般的node启动一样 node development.js即可。查看入口文件可知启动的时候，实际上是实例化 ThinkJS 里的 Application 类，执行 run 方法。\n\n#### 5.配置数据库\n\u003e 本项目使用mysql数据库,配置文件的位置:src/config/adapter.js\n```\nexports.model = {\n  type: 'mysql',\n  common: {\n    logConnect: isDev,\n    logSql: isDev,\n    logger: msg =\u003e think.logger.info(msg)\n  },\n  mysql: {\n    handle: mysql,\n    database: 'thinkjsplus',\n    prefix: '',\n    encoding: 'utf8',\n    host: '127.0.0.1',\n    port: '3306',\n    user: 'root',\n    password: 'root',\n    dateStrings: true\n  }\n};\n```\n#### 5.1 创建模型文件\n```\nthinkjs model +模型文件名\n```\n\n\n#### 6. 控制器\n\u003e创建控制器的命令是 thinkjs controller +控制器名,我们创建admin后台登录控制器,同时在视图层我们创建admin控制器对应的html页面admin_index.html页面,启动服务,浏览器访问http://localhost:8360/admin就可以访问到admin_index.html的登录页面.\n```\nthinkjs controller admin\n```\n\n```\nconst user = this.model('thinkjsplus_user'); // controller 里实例化模型\nconst data = await user.select();\nthis.assign('title',data);\nreturn this.display();\n```\n\n#### 7. 视图\n\u003ethinkjs3默认模板引擎是 nunjucks,如果在控制器action中这样写\n```\nthis.assign('title',\"测试网页之thinkjsplus!\");\nreturn this.display();\n```\n视图html页面中就可以这样写,拿到title\n```\n{{title}}\n```\n#### 8. 权限控制\n\u003e 权限代码如下:\n```\nmodule.exports = class extends think.Controller {\n  async __before() {\n    if(this.ctx.controller === 'admin' \u0026\u0026 this.ctx.action === 'index'||this.ctx.action === 'login' ){ //如果是admin_index那么久不验证了，直接返回给予登录。 \n        return;   \n    } \n    let userinfo =await this.session('userinfo')\n    if (!think.isEmpty(userinfo)){  \n      this.assign('userinfo',userinfo);  \n    }else{  \n      return this.redirect('/admin/index');  \n    }  \n  }\n```\n#### 9.本项目用到了mysql数据库,CRUD操作具体可以查看项目,本项目具有详细注释\n```\n/**\n   * 保存分类\n   */\n  async saveAction() {\n    let data = this.post();\n    if (think.isEmpty(data.id)) {\n      //保存\n      let res = await this.model(\"thinkjsplus_category\").add(data);\n      if (res) {\n        this.json({\"succ\":true});\n      } else {\n        this.json({\"succ\":false});\n      }\n    } else {\n      //更新\n      let res = await this.model(\"thinkjsplus_category\").update(data);\n      if (res) {\n        this.json({\"succ\":true});\n      } else {\n        this.json({\"succ\":false});\n      }\n    }\n  }\n  /**\n   * 删除分类\n   */\n  async delAction() {\n    let categoryModel = this.model(\"thinkjsplus_category\");\n    let posts = this.post(\"id\");\n    let delNums = categoryModel.where({id: ['IN', posts]}).delete();\n    if(delNums){\n        this.json({\"succ\":true});\n    }else{\n        this.json({\"succ\":false});\n    }\n  }\n  /**\n   * 查看分类\n   */\n  async listAction() {\n    const user = this.model('thinkjsplus_category'); // controller 里实例化模型\n    const data = await user.select();\n    return this.json(data);\n  }\n```\n#### 10.运行本项目\n\u003e 首先安装mysql,之后导入项目sql文件夹,然后在项目根目录下运行 npm install,之后运行 npm start,即可启动本项目\n\n#### 附项目截图\n![image](./images/1.jpg)\n![image](./images/2.jpg)\n![image](./images/3.jpg)\n![image](./images/4.jpg)\n![image](./images/5.jpg)","funding_links":[],"categories":["JavaScript","Projects by ThinkJS"],"sub_categories":["websocket"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwuyanwen%2Fthinkjsplus","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwuyanwen%2Fthinkjsplus","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwuyanwen%2Fthinkjsplus/lists"}