Apply.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. namespace app\api\model\dealer;
  3. use app\common\model\dealer\Apply as ApplyModel;
  4. /**
  5. * 分销商申请模型
  6. * Class Apply
  7. * @package app\api\model\dealer
  8. */
  9. class Apply extends ApplyModel
  10. {
  11. /**
  12. * 隐藏字段
  13. * @var array
  14. */
  15. protected $hidden = [
  16. 'create_time',
  17. 'update_time',
  18. ];
  19. /**
  20. * 是否为分销商申请中
  21. * @param $user_id
  22. * @return bool
  23. * @throws \think\exception\DbException
  24. */
  25. public static function isApplying($user_id)
  26. {
  27. $detail = self::detail(['user_id' => $user_id]);
  28. return $detail ? ((int)$detail['apply_status'] === 10) : false;
  29. }
  30. /**
  31. * 提交申请
  32. * @param $user
  33. * @param $name
  34. * @param $mobile
  35. * @return mixed
  36. * @throws \think\exception\DbException
  37. */
  38. public function submit($user, $name, $mobile)
  39. {
  40. // 成为分销商条件
  41. $config = Setting::getItem('condition');
  42. // 数据整理
  43. $data = [
  44. 'user_id' => $user['user_id'],
  45. 'real_name' => trim($name),
  46. 'mobile' => trim($mobile),
  47. 'referee_id' => Referee::getRefereeUserId($user['user_id'], 1),
  48. 'apply_type' => $config['become'],
  49. 'apply_time' => time(),
  50. 'wxapp_id' => self::$wxapp_id,
  51. ];
  52. if ($config['become'] == 10) {
  53. $data['apply_status'] = 10;
  54. } elseif ($config['become'] == 20) {
  55. $data['apply_status'] = 20;
  56. }
  57. return $this->add($user, $data);
  58. }
  59. /**
  60. * 更新分销商申请信息
  61. * @param $user
  62. * @param $data
  63. * @return mixed
  64. */
  65. private function add($user, $data)
  66. {
  67. // 更新记录
  68. return $this->transaction(function () use ($user, $data) {
  69. // 实例化模型
  70. $model = self::detail(['user_id' => $user['user_id']]) ?: $this;
  71. // 保存申请信息
  72. $model->save($data);
  73. // 无需审核,自动通过
  74. if ($data['apply_type'] == 20) {
  75. // 新增分销商用户记录
  76. User::add($user['user_id'], [
  77. 'real_name' => $data['real_name'],
  78. 'mobile' => $data['mobile'],
  79. 'referee_id' => $data['referee_id']
  80. ]);
  81. }
  82. return true;
  83. });
  84. }
  85. }