icon.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. 'use strict';
  2. const Service = require('egg').Service;
  3. class IconsService extends Service {
  4. async list(query) {
  5. let code = query.code;
  6. let where = code ? `where (id like '%${code}%' or name like '%${code}%' ) `: "";
  7. let type = query.type;
  8. if( type != undefined ) {
  9. where = where?where + `and (\`type\` like '%${type}$'`: `where \`type\` like '%${type}%'`;
  10. }
  11. let ctx = this.ctx;
  12. let ret = await ctx.pageSelect(query, 'icons', `id, name, type, icon, ${ctx.SqlFmtTime("add_time")}`, where);
  13. return ret;
  14. }
  15. async add( data ) {
  16. let mysql = this.app.mysql;
  17. data.add_time = mysql.literals.now;
  18. let ret = await mysql.insert("icons", data);
  19. return {id: ret.insertId};
  20. }
  21. async edit( data ) {
  22. let id = data.id;
  23. if( !id ) throw "id不为空";
  24. let mysql = this.app.mysql;
  25. let ret = await mysql.update("icons", data, {where:{id: data.id}});
  26. return ret;
  27. }
  28. async delete(data)
  29. {
  30. let mysql = this.app.mysql;
  31. let ret = await mysql.query("delete from icons where id=?",[data.id]);
  32. return ret;
  33. }
  34. //app相关接口
  35. async listInApp(query) {
  36. let type = query.type;
  37. let where = type ? `where \`type\`='${type}' `: "";
  38. let ctx = this.ctx;
  39. if( query.pageNo == '0') query.pageNo =1;
  40. let ret = await ctx.pageSelect(query, 'icons', `id, name, type, icon`, where);
  41. let server = this.app.config.server;
  42. ret.records.forEach(element => {
  43. element.icon = server.publicHost + element.icon;
  44. });
  45. return ret;
  46. }
  47. }
  48. module.exports = IconsService;