customer.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. const updateData = {
  69. id: customerData.id,
  70. email: data.useremail,
  71. mobile: data.mobile,
  72. name: data.username,
  73. };
  74. await this.db.update(this.tableName, updateData);
  75. return customerData.id;
  76. }
  77. const insertData = {
  78. email: data.useremail,
  79. mobile: data.mobile,
  80. name: data.username,
  81. };
  82. result = await this.db.insert(this.tableName, insertData);
  83. return result.insertId;
  84. }
  85. /**
  86. * 判断是否需要初始化信息
  87. *
  88. * @param {Object} postData - 表单post过来的数据
  89. * @return {Boolean} - 返回是否需要初始化结果
  90. */
  91. async isNeedBoot(postData) {
  92. if (postData.username === undefined) {
  93. return false;
  94. }
  95. const customer = await this.getDataByCondition({ email: postData.username });
  96. if (customer === null) {
  97. return false;
  98. }
  99. return customer.company === '';
  100. }
  101. /**
  102. * 初始化用户信息
  103. *
  104. * @param {Object} postData - 表单post过来的数据
  105. * @return {Boolean} - 返回初始化结果
  106. */
  107. async boot(postData) {
  108. const sessionUser = this.ctx.session.sessionUser;
  109. delete postData._csrf_j;
  110. const result = await this.update(postData, { email: sessionUser.account });
  111. return result;
  112. }
  113. }
  114. return Customer;
  115. };