Apply.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. namespace app\api\model\plus\agent;
  3. use app\api\model\plus\agent\Referee as RefereeModel;
  4. use app\common\model\plus\agent\Apply as ApplyModel;
  5. /**
  6. * 分销商申请模型
  7. */
  8. class Apply extends ApplyModel
  9. {
  10. /**
  11. * 隐藏字段
  12. * @var array
  13. */
  14. protected $hidden = [
  15. 'create_time',
  16. 'update_time',
  17. ];
  18. /**
  19. * 是否为分销商申请中
  20. */
  21. public static function isApplying($user_id)
  22. {
  23. $detail = self::detail(['user_id' => $user_id]);
  24. return $detail ? ((int)$detail['apply_status']['value'] === 10) : false;
  25. }
  26. /**
  27. * 提交申请
  28. */
  29. public function submit($user, $data)
  30. {
  31. // 成为分销商条件
  32. $config = Setting::getItem('condition');
  33. // 如果之前有关联分销商,则继续关联之前的分销商
  34. $has_referee_id = Referee::getRefereeUserId($user['user_id'], 1);
  35. if($has_referee_id > 0){
  36. $referee_id = $has_referee_id;
  37. }else{
  38. $referee_id = $data['referee_id'] > 0 ?$data['referee_id']:0;
  39. }
  40. // 数据整理
  41. $data = [
  42. 'user_id' => $user['user_id'],
  43. 'real_name' => trim($data['name']),
  44. 'mobile' => trim($data['mobile']),
  45. 'referee_id' => $referee_id,
  46. 'apply_type' => $config['become'],
  47. 'apply_time' => time(),
  48. 'app_id' => self::$app_id,
  49. ];
  50. if ($config['become'] == 10) {
  51. $data['apply_status'] = 10;
  52. } elseif ($config['become'] == 20) {
  53. $data['apply_status'] = 20;
  54. }
  55. return $this->add($user, $data);
  56. }
  57. /**
  58. * 更新分销商申请信息
  59. */
  60. private function add($user, $data)
  61. {
  62. // 实例化模型
  63. $model = self::detail(['user_id' => $user['user_id']]) ?: $this;
  64. // 更新记录
  65. $this->startTrans();
  66. try {
  67. // 保存申请信息
  68. $model->save($data);
  69. // 无需审核,自动通过
  70. if ($data['apply_type'] == 20) {
  71. // 新增分销商用户记录
  72. User::add($user['user_id'], [
  73. 'real_name' => $data['real_name'],
  74. 'mobile' => $data['mobile'],
  75. 'referee_id' => $data['referee_id']
  76. ]);
  77. }
  78. // 记录推荐人关系
  79. if ($data['referee_id'] > 0) {
  80. RefereeModel::createRelation($user['user_id'], $data['referee_id']);
  81. }
  82. $this->commit();
  83. return true;
  84. } catch (\Exception $e) {
  85. $this->error = $e->getMessage();
  86. $this->rollback();
  87. return false;
  88. }
  89. }
  90. }