ValetOrderValidate.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. namespace common\logic\order\validate;
  3. use addons\ValetOrder\common\enums\ValetOrderEnum;
  4. use addons\ValetOrder\common\service\SettingService;
  5. use common\enums\AddonsEnum;
  6. use common\enums\StatusEnum;
  7. use common\helpers\sms\Sms;
  8. use common\logic\order\BaseOrderForm;
  9. use common\models\mall\MallAddons;
  10. use common\models\user\User;
  11. use common\models\user\UserPartitionRelationship;
  12. class ValetOrderValidate implements OrderValidateInterface
  13. {
  14. /**
  15. * @param BaseOrderForm $form
  16. * @return void
  17. * @throws \Exception
  18. */
  19. public function valid(BaseOrderForm $form)
  20. {
  21. try {
  22. // 检测是否有安装积分拓客应用
  23. if (MallAddons::isInstall($form->mall_id, AddonsEnum::ADDONS_NAME_VALET_ORDER)) {
  24. $config = \Yii::$app->services->setting->addons->get($form->mall_id, AddonsEnum::ADDONS_NAME_VALET_ORDER, 'basic');
  25. if (!empty($config['is_enable']) && !empty($config['is_enable_sms_code']) && !empty($form->data['extra_info']['valet_order']['mobile'])) {
  26. if (empty($form->data['captcha'])) throw new \Exception('短信验证码不能为空');
  27. if (YII_ENV == 'prod') {
  28. // 短信验证码
  29. if (!Sms::checkValidateCode($form->data['extra_info']['valet_order']['mobile'],
  30. $form->data['captcha'], ValetOrderEnum::SMS_TYPE))
  31. throw new \Exception('验证码不正确');
  32. }
  33. }
  34. // TODO 这个参数不为空,就是代客下单?
  35. // 如果后台开启了限制同个团队购买,并且这个用户绑定了上级(没有绑定的则会绑定到自身,则不需要验证)
  36. if (!empty($config['is_enable']) && !empty($form->data['extra_info']['valet_order']['mobile'])) {
  37. // 判断这个会员的等级是否能代下单
  38. if (!(new SettingService())->getIsShowValetOrderBtn($form->mall_id, $form->user->level, $config)) {
  39. throw new \Exception('当前用户不允许代下单');
  40. }
  41. // 验证新会员手机号不能填自己
  42. if ($form->data['extra_info']['valet_order']['mobile'] == $form->user->mobile) {
  43. throw new \Exception('不允许为自己代下单');
  44. }
  45. // 两个手机号相同
  46. if ($form->data['extra_info']['valet_order']['mobile'] == ($form->data['extra_info']['valet_order']['parent'] ?? '')) {
  47. throw new \Exception('新会员手机号和直属老师手机号不能相同');
  48. }
  49. // 代客下单不能下自提订单
  50. if (isset($form->data['shipping_type']) && $form->data['shipping_type'] == 2) {
  51. throw new \Exception('站点自提不支持代客下单');
  52. }
  53. if (!empty($config['is_limit_same_team'])) {
  54. // 根据手机号查询出用户id
  55. $userInfoByMobile = User::find()
  56. ->where([
  57. 'mobile' => $form->data['extra_info']['valet_order']['mobile'],
  58. 'mall_id' => $form->mall_id,
  59. 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]
  60. ])
  61. ->select('id, parent_id')
  62. ->one();
  63. $parent = $form->data['extra_info']['valet_order']['parent'] ?? '';
  64. // 因为是限制了同一团队,所以手机号只能填帮代下单的手机号,只能成为他的下级
  65. if ($parent && $form->user->mobile != $parent) {
  66. throw new \Exception('直属老师手机号只能是下单用户的手机号!');
  67. }
  68. if ($userInfoByMobile) {
  69. switch ($config['is_limit_same_team']) {
  70. case 1: // 同团队
  71. if (
  72. $userInfoByMobile->parent_id &&
  73. !UserPartitionRelationship::isMyTeam($form->user_id, $userInfoByMobile->id)
  74. ) {
  75. throw new \Exception('代客下单用户不在您的团队,不允许下单');
  76. }
  77. break;
  78. case 2: // 仅限下级
  79. if ($userInfoByMobile->parent_id) {
  80. if ($userInfoByMobile->parent_id != $form->user_id) {
  81. throw new \Exception('只允许给直推下级代下单');
  82. }
  83. } else {
  84. if (UserPartitionRelationship::isMyTeam($form->user_id, $userInfoByMobile->id)) {
  85. throw new \Exception('只允许给直推下级代下单');
  86. }
  87. }
  88. break;
  89. }
  90. }
  91. }
  92. }
  93. }
  94. } catch (\Exception $e) {
  95. throw new \Exception($e->getMessage());
  96. }
  97. }
  98. }