Apply.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. namespace app\common\model\dealer;
  3. use app\common\model\BaseModel;
  4. /**
  5. * 分销商申请模型
  6. * Class Apply
  7. * @package app\common\model\dealer
  8. */
  9. class Apply extends BaseModel
  10. {
  11. protected $name = 'dealer_apply';
  12. /**
  13. * 获取器:申请时间
  14. * @param $value
  15. * @return false|string
  16. */
  17. public function getApplyTimeAttr($value)
  18. {
  19. return date('Y-m-d H:i:s', $value);
  20. }
  21. /**
  22. * 获取器:审核时间
  23. * @param $value
  24. * @return false|string
  25. */
  26. public function getAuditTimeAttr($value)
  27. {
  28. return $value > 0 ? date('Y-m-d H:i:s', $value) : 0;
  29. }
  30. /**
  31. * 关联用户表
  32. * @return \think\model\relation\BelongsTo
  33. */
  34. public function referee()
  35. {
  36. return $this->belongsTo('app\common\model\User', 'referee_id')
  37. ->field(['user_id', 'nickName']);
  38. }
  39. /**
  40. * 关联会员记录表
  41. * @return \think\model\relation\BelongsTo
  42. */
  43. public function user()
  44. {
  45. $module = self::getCalledModule() ?: 'common';
  46. return $this->belongsTo("app\\{$module}\\model\\User");
  47. }
  48. /**
  49. * 销商申请记录详情
  50. * @param $where
  51. * @return Apply|static
  52. * @throws \think\exception\DbException
  53. */
  54. public static function detail($where)
  55. {
  56. return self::get($where);
  57. }
  58. /**
  59. * 购买指定商品成为分销商
  60. * @param $userId
  61. * @param $goodsIds
  62. * @param $wxappId
  63. * @return bool
  64. * @throws \think\exception\DbException
  65. */
  66. public function becomeDealerUser($userId, $goodsIds, $wxappId)
  67. {
  68. // 验证是否设置
  69. $config = Setting::getItem('condition', $wxappId);
  70. if ($config['become__buy_goods'] != '1' || empty($config['become__buy_goods_ids'])) {
  71. return false;
  72. }
  73. // 判断商品是否在设置范围内
  74. $intersect = array_intersect($goodsIds, $config['become__buy_goods_ids']);
  75. if (empty($intersect)) {
  76. return false;
  77. }
  78. // 新增分销商用户
  79. User::add($userId, [
  80. 'referee_id' => Referee::getRefereeUserId($userId, 1),
  81. 'wxapp_id' => $wxappId,
  82. ]);
  83. return true;
  84. }
  85. }