Apply.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 bool
  36. * @throws \think\exception\DbException
  37. * @throws \think\exception\PDOException
  38. */
  39. public function submit($user, $name, $mobile)
  40. {
  41. // 成为分销商条件
  42. $config = Setting::getItem('condition');
  43. // 数据整理
  44. $data = [
  45. 'user_id' => $user['user_id'],
  46. 'real_name' => trim($name),
  47. 'mobile' => trim($mobile),
  48. 'referee_id' => Referee::getRefereeUserId($user['user_id'], 1),
  49. 'apply_type' => $config['become'],
  50. 'apply_time' => time(),
  51. 'wxapp_id' => self::$wxapp_id,
  52. ];
  53. if ($config['become'] == 10) {
  54. $data['apply_status'] = 10;
  55. } elseif ($config['become'] == 20) {
  56. $data['apply_status'] = 20;
  57. }
  58. return $this->add($user, $data);
  59. }
  60. /**
  61. * 更新分销商申请信息
  62. * @param $user
  63. * @param $data
  64. * @return bool
  65. * @throws \think\exception\DbException
  66. * @throws \think\exception\PDOException
  67. */
  68. private function add($user, $data)
  69. {
  70. // 实例化模型
  71. $model = self::detail(['user_id' => $user['user_id']]) ?: $this;
  72. // 更新记录
  73. $this->startTrans();
  74. try {
  75. // $data['create_time'] = time();
  76. // 保存申请信息
  77. $model->save($data);
  78. // 无需审核,自动通过
  79. if ($data['apply_type'] == 20) {
  80. // 新增分销商用户记录
  81. User::add($user['user_id'], [
  82. 'real_name' => $data['real_name'],
  83. 'mobile' => $data['mobile'],
  84. 'referee_id' => $data['referee_id']
  85. ]);
  86. }
  87. $this->commit();
  88. return true;
  89. } catch (\Exception $e) {
  90. $this->error = $e->getMessage();
  91. $this->rollback();
  92. return false;
  93. }
  94. }
  95. }