customer.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. 'use strict';
  2. /**
  3. * 客户信息数据模型
  4. *
  5. * @author CaiAoLin
  6. * @date 2017/11/14
  7. * @version
  8. */
  9. module.exports = app => {
  10. class Customer extends app.BaseService {
  11. /**
  12. * 构造函数
  13. *
  14. * @param {Object} ctx - egg全局变量
  15. * @return {void}
  16. */
  17. constructor(ctx) {
  18. super(ctx);
  19. this.tableName = 'customer';
  20. }
  21. /**
  22. * 数据规则
  23. *
  24. * @param {String} scene - 场景
  25. * @return {Object} - 返回规则数据
  26. */
  27. rule(scene) {
  28. let rule = {};
  29. switch (scene) {
  30. case 'boot':
  31. rule = {
  32. name: { type: 'string', required: true, min: 2 },
  33. company: { type: 'string', required: true, min: 2 },
  34. province: { type: 'string', required: true },
  35. company_type: { type: 'string', allowEmpty: true },
  36. scale: { type: 'string', allowEmpty: true },
  37. };
  38. break;
  39. default:
  40. break;
  41. }
  42. return rule;
  43. }
  44. /**
  45. * 查询过虑
  46. *
  47. * @param {Object} data - 筛选表单中的get数据
  48. * @return {void}
  49. */
  50. searchFilter(data) {
  51. this.initSqlBuilder();
  52. }
  53. /**
  54. * 新增SSO用户
  55. *
  56. * @param {Object} data - 接口返回的数据
  57. * @return {Number} - 返回新增数据的id
  58. */
  59. async addSSOUser(data) {
  60. let result = 0;
  61. if (Object.keys(data).length <= 0 || data.useremail === undefined) {
  62. return result;
  63. }
  64. // 先查找是否存在
  65. const customerData = await this.db.get(this.tableName, { email: data.useremail });
  66. if (customerData !== null) {
  67. // 存在则直接返回id
  68. return customerData.id;
  69. }
  70. const insertData = {
  71. email: data.useremail,
  72. mobile: data.mobile,
  73. name: data.username,
  74. };
  75. result = await this.db.insert(this.tableName, insertData);
  76. return result.insertId;
  77. }
  78. /**
  79. * 判断是否需要初始化信息
  80. *
  81. * @param {Object} postData - 表单post过来的数据
  82. * @return {Boolean} - 返回是否需要初始化结果
  83. */
  84. async isNeedBoot(postData) {
  85. if (postData.username === undefined) {
  86. return false;
  87. }
  88. const customer = await this.getDataByCondition({ email: postData.username });
  89. if (customer === null) {
  90. return false;
  91. }
  92. return customer.company === '';
  93. }
  94. /**
  95. * 初始化用户信息
  96. *
  97. * @param {Object} postData - 表单post过来的数据
  98. * @return {Boolean} - 返回初始化结果
  99. */
  100. async boot(postData) {
  101. const sessionUser = this.ctx.session.sessionUser;
  102. delete postData._csrf;
  103. const result = await this.update(postData, { email: sessionUser.account });
  104. return result;
  105. }
  106. }
  107. return Customer;
  108. };