Apply.php 2.5 KB

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