TransferController.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. namespace addons\HiGo\api\modules\v1\controllers;
  3. use addons\HiGo\common\enums\HiGoEnum;
  4. use addons\HiGo\common\logic\WalletLogic;
  5. use addons\HiGo\common\models\HigoWallet;
  6. use common\enums\StatusEnum;
  7. use common\models\common\CapitalLog;
  8. use common\models\user\User;
  9. use Yii;
  10. use api\controllers\OnAuthController;
  11. use common\helpers\ApiCode;
  12. /**
  13. *
  14. * 嗨呗转让
  15. * Class TransferController
  16. * @package addons\HiGo\api\modules\v1\controllers
  17. */
  18. class TransferController extends OnAuthController
  19. {
  20. public $modelClass = '';
  21. /**
  22. * 不用进行登录验证的方法
  23. *
  24. * 例如: ['index', 'update', 'create', 'view', 'delete']
  25. * 默认全部需要验证
  26. *
  27. * @var array
  28. */
  29. protected $authOptional = [];
  30. /**
  31. * 转让嗨呗
  32. */
  33. public function actionHiBei()
  34. {
  35. try{
  36. $params = Yii::$app->request->post();
  37. $res = Yii::$app->services->validate->validate($params, [
  38. [['mobile', 'transfer_num'], 'required'],
  39. [[ 'transfer_num'], 'number'],
  40. ]);
  41. if ($res === false) throw new \Exception(Yii::$app->services->validate->getErrorMessage());
  42. if($params['transfer_num'] < 0 ) throw new \Exception('转让数量错误');
  43. //查询用户是否存在
  44. $to_user = User::getUser([['=', 'mobile', $params['mobile']], ['=', 'mall_id', $this->mall_id]]);
  45. if(!$to_user) throw new \Exception('目标用户不存在');
  46. if($to_user['id'] == $this->user_id) throw new \Exception('请选择目标用户');
  47. $user_wallet = HigoWallet::getUserWallet($this->mall_id,$this->user_id);
  48. if (!$user_wallet || bccomp($user_wallet['hi_bei'], $params['transfer_num'], 10) < 0) {
  49. throw new \Exception('用户' . WalletLogic::getWalletTypeMsg($this->mall_id, HiGoEnum::WALLET_TYPE_HI_BEI) . '不足');
  50. }
  51. //执行转让
  52. $setting = \Yii::$app->services->setting->addons->get($this->mall_id, HiGoEnum::ADDONS_NAME, 'basic');
  53. $hi_bei_transfer_enable = $setting['hi_bei_transfer_enable'] ?? 0;
  54. if(!$hi_bei_transfer_enable) throw new \Exception('系统不支持转让');
  55. $name = $setting['hi_bei_name'] ?? '嗨贝';
  56. //1. 扣除当前用户 嗨呗数量
  57. $wallet_log = [
  58. 'mall_id' => $this->mall_id,
  59. 'user_id' => $this->user_id,
  60. 'change_type' => CapitalLog::CHANGE_TYPE_SUB,
  61. 'change_num' => $params['transfer_num'] * -1,
  62. 'desc' => '转让'.$name.' 到'.$params['mobile'],
  63. 'source_table' => User::tableName(),
  64. 'source_table_id' => $to_user['id'],
  65. 'from_type' => HiGoEnum::FROM_TYPE_TRANSFER_HI_BEI,
  66. 'wallet_type' => HiGoEnum::WALLET_TYPE_HI_BEI,
  67. 'is_send' => false
  68. ];
  69. $res = WalletLogic::handleInQueue($wallet_log,true);
  70. if(!$res){
  71. throw new \Exception('转让失败:'.WalletLogic::getStaticError());
  72. }
  73. //插入队列处理
  74. $wallet_log['to_user_id'] = $to_user['id'];
  75. $wallet_log['to_user_mobile'] = $params['mobile'];
  76. $wallet_log['log_id'] = $res;
  77. $event = new \addons\HiGo\common\events\TransferEvent($this->mall_id);
  78. \Yii::$app->services->events->dispatch($event, $wallet_log, $event->listeners);
  79. return $this->success();
  80. }catch (\Exception $e){
  81. return $this->error($e->getMessage());
  82. }
  83. }
  84. /**
  85. * 获取用户账户信息
  86. */
  87. public function actionGetUser()
  88. {
  89. try{
  90. $higo_wallet = HigoWallet::getOne([['mall_id' => $this->mall_id, 'user_id' => $this->user_id, 'status' => StatusEnum::ENABLED]], true);
  91. $basic = Yii::$app->services->setting->addons->get($this->mall_id, HiGoEnum::ADDONS_NAME, 'basic');
  92. $hi_bei_transfer_fee = $basic['hi_bei_transfer_fee'] ?? 0;
  93. $data = [
  94. 'hi_bei' => $higo_wallet['hi_bei'] ?? 0,
  95. 'hi_bei_name' => $basic['hi_bei_name'] ?? '嗨贝',
  96. 'hi_value' => $higo_wallet['hi_value'] ?? 0,
  97. 'hi_value_name' => $basic['hi_value_name'] ?? '嗨值',
  98. 'freeze_hi_bei' => $higo_wallet['freeze_hi_bei'] ?? 0,
  99. 'freeze_hi_value' => $higo_wallet['freeze_hi_value'] ?? 0,
  100. 'hi_bei_transfer_fee' => $hi_bei_transfer_fee.'%'
  101. ];
  102. return $this->success('success',$data);
  103. }catch (\Exception $e){
  104. return $this->error($e->getMessage());
  105. }
  106. }
  107. }