project.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. 'use strict';
  2. /**
  3. * 项目数据模型
  4. *
  5. * @author CaiAoLin
  6. * @date 2017/11/16
  7. * @version
  8. */
  9. const imType = require('../const/tender').imType;
  10. const defaultFunRela = {
  11. banOver: true,
  12. hintOver: true,
  13. imType: imType.zl.value,
  14. needGcl: false,
  15. };
  16. const sjsRelaConst = require('../const/setting').sjsRela;
  17. module.exports = app => {
  18. class Project extends app.BaseService {
  19. /**
  20. * 构造函数
  21. *
  22. * @param {Object} ctx - egg全局变量
  23. * @return {void}
  24. */
  25. constructor(ctx) {
  26. super(ctx);
  27. this.tableName = 'project';
  28. // 状态相关
  29. this.status = {
  30. TRY: 1,
  31. NORMAL: 2,
  32. DISABLE: 3,
  33. };
  34. }
  35. /**
  36. * 数据规则
  37. *
  38. * @param {String} scene - 场景
  39. * @return {Object} - 返回数据规则
  40. */
  41. rule(scene) {
  42. let rule = {};
  43. switch (scene) {
  44. case 'saveInfo':
  45. rule = {
  46. name: { type: 'string', required: true, min: 2 },
  47. };
  48. break;
  49. case 'fun':
  50. rule = {
  51. imType: {type: 'enum', values: [imType.tz.value, imType.zl.value, imType.bb.value, imType.bw.value], required: true},
  52. banOver: {type: 'bool', required: true,},
  53. hintOver: {type: 'bool', required: true,}
  54. };
  55. break;
  56. default:
  57. break;
  58. }
  59. return rule;
  60. }
  61. /**
  62. * 根据项目code获取项目数据
  63. *
  64. * @param {String} code - 项目code
  65. * @return {Object} - 返回项目数据
  66. */
  67. async getProjectByCode(code) {
  68. // 获取项目状态为非禁止的项目
  69. this.initSqlBuilder();
  70. this.sqlBuilder.setAndWhere('code', {
  71. value: this.db.escape(code),
  72. operate: '=',
  73. });
  74. this.sqlBuilder.setAndWhere('status', {
  75. value: this.status.DISABLE,
  76. operate: '<',
  77. });
  78. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
  79. const projectData = await this.db.queryOne(sql, sqlParam);
  80. return projectData;
  81. }
  82. /**
  83. * 根据项目code获取项目数据
  84. *
  85. * @param {int} prjId - id
  86. * @return {Object} - 返回项目数据
  87. */
  88. async getProjectById(prjId) {
  89. // 获取项目状态为非禁止的项目
  90. this.initSqlBuilder();
  91. this.sqlBuilder.setAndWhere('id', {
  92. value: this.db.escape(prjId),
  93. operate: '=',
  94. });
  95. this.sqlBuilder.setAndWhere('status', {
  96. value: this.status.DISABLE,
  97. operate: '<',
  98. });
  99. const [sql, sqlParam] = this.sqlBuilder.build(this.tableName);
  100. const projectData = await this.db.queryOne(sql, sqlParam);
  101. return projectData;
  102. }
  103. /**
  104. * 切换项目
  105. *
  106. * @param {Number} projectId - 项目id
  107. * @return {Boolean} - 返回切换结果
  108. */
  109. async switchProject(projectId) {
  110. // 获取该用户拥有的项目数据
  111. const sessionUser = this.ctx.session.sessionUser;
  112. const projectInfo = await this.ctx.service.projectAccount.getProjectInfoByAccount(sessionUser.account);
  113. let result = false;
  114. // 判断切换的项目是否属于对应用户
  115. if (projectInfo.length < 0) {
  116. return result;
  117. }
  118. let targetProject = {};
  119. for (const tmp of projectInfo) {
  120. if (tmp.id === projectId) {
  121. result = true;
  122. targetProject = tmp;
  123. }
  124. }
  125. // 成功后更改session
  126. if (result) {
  127. this.ctx.session.sessionProject = {
  128. id: targetProject.id,
  129. name: targetProject.name,
  130. userAccount: targetProject.user_account,
  131. };
  132. }
  133. return result;
  134. }
  135. /**
  136. * 功能设置
  137. * @param id
  138. * @returns {Promise<null>}
  139. */
  140. async getFunRela(id) {
  141. const projectData = await this.db.get(this.tableName, { id });
  142. const result = projectData.fun_rela ? JSON.parse(projectData.fun_rela) : {};
  143. this.ctx.helper._.defaults(result, defaultFunRela);
  144. return result;
  145. }
  146. async updateFunRela(id, data) {
  147. const result = await this.db.update(this.tableName, {
  148. id: id, fun_rela: JSON.stringify({
  149. banOver: data.banOver, hintOver: data.hintOver,
  150. imType: data.imType, needGcl: data.needGcl,
  151. }),
  152. });
  153. return result.affectedRows === 1;
  154. }
  155. async getSjsRela(id) {
  156. const projectData = await this.db.get(this.tableName, { id });
  157. const result = projectData.sjs_rela ? JSON.parse(projectData.sjs_rela) : {};
  158. this.ctx.helper._.defaults(result, sjsRelaConst);
  159. return result;
  160. }
  161. async updateSjsRela(id, sub, field, key, value) {
  162. const sjsRela = await this.getSjsRela(id);
  163. if (!sjsRela[sub]) throw '数据异常';
  164. const sjsField = sjsRela[sub].find(x => { return x.field === field; });
  165. if (!sjsField) throw '数据异常';
  166. sjsField[key] = value;
  167. await this.db.update(this.tableName, { id: id, sjs_rela: JSON.stringify(sjsRela) });
  168. return sjsField;
  169. }
  170. async updatePageshow(id) {
  171. const result = await this.db.update(this.tableName, {
  172. id, page_show: JSON.stringify(this.ctx.session.sessionProject.page_show),
  173. });
  174. return result.affectedRows === 1;
  175. }
  176. /**
  177. * 校验项目是否存在(项目管理)
  178. * @param {String} token - token
  179. * @return {Promise} Promise
  180. */
  181. verifyManagementProject(token) {
  182. return new Promise((resolve, reject) => {
  183. this.ctx.curl(`${app.config.managementProxyPath}/api/external/jl/calibration`, {
  184. method: 'POST',
  185. // dateType: 'json',
  186. encoding: 'utf8',
  187. data: {
  188. token,
  189. },
  190. // timeout: 2000,
  191. }).then(({ status, data }) => {
  192. if (status === 200) {
  193. const result = JSON.parse(data.toString()).data;
  194. return resolve(result);
  195. }
  196. return reject(new Error('校验失败'));
  197. }).catch(err => {
  198. console.log(err);
  199. return reject(err);
  200. });
  201. });
  202. }
  203. /**
  204. * 创建项目和账号(项目管理)
  205. * @return {Promise} Promise
  206. */
  207. async addProjectFromManagement() {
  208. const token = this.ctx.helper.createJWT({ account: this.ctx.session.sessionUser.account, code: this.ctx.session.sessionProject.code });
  209. return new Promise((resolve, reject) => {
  210. this.ctx.curl(`${app.config.managementProxyPath}/api/external/jl/project/add`, {
  211. method: 'POST',
  212. encoding: 'utf8',
  213. data: { token },
  214. }).then(({ status, data }) => {
  215. if (status === 200) {
  216. const result = JSON.parse(data.toString());
  217. return resolve(result);
  218. }
  219. return reject(new Error('添加失败'));
  220. }).catch(err => {
  221. console.log(err);
  222. return reject(err);
  223. });
  224. });
  225. }
  226. }
  227. return Project;
  228. };