TransferForm.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. <?php
  2. namespace common\models\forms;
  3. use common\helpers\Cache;
  4. use common\helpers\Date;
  5. use common\components\Model;
  6. use common\helpers\Form;
  7. use common\helpers\LoggerTool;
  8. use common\helpers\snowflake\SnowFake;
  9. use common\helpers\Tool;
  10. use common\helpers\user\Balance;
  11. use common\helpers\user\Cash;
  12. use common\helpers\user\Info;
  13. use common\libs\logging\operate\UserOperate;
  14. use common\models\Article;
  15. use common\models\ArticleCategory;
  16. use common\models\DealType;
  17. use common\models\DecOrder;
  18. use common\models\Order;
  19. use common\models\PerfPeriod;
  20. use common\models\Period;
  21. use common\models\Transfer;
  22. use common\models\User;
  23. use common\models\UserBind;
  24. use common\models\UserBonus;
  25. use common\models\UserInfo;
  26. use common\models\UserRelation;
  27. use common\models\UserSystem;
  28. use yii\base\Exception;
  29. use yii\helpers\Json;
  30. /**
  31. * Login form
  32. */
  33. class TransferForm extends Model {
  34. const allowTransferType = [1, 2, 3, 4];
  35. const BONUS_TO_BALANCE = 1;
  36. const BONUS_TO_BONUS = 2;
  37. const BALANCE_TO_BALANCE = 3;
  38. const POINT_TO_BALANCE = 4;
  39. public $toUserName;
  40. public $toRealName;
  41. public $amount;
  42. public $payPassword;
  43. public $type;
  44. public $remark;
  45. public $transferCode;
  46. private $_fromUserInfo;
  47. private $_toUserInfo;
  48. private $_transferProp;
  49. private $_transferConfig;
  50. private $_fee = 0;
  51. public function init() {
  52. parent::init();
  53. $this->userOperateLogger = new UserOperate([
  54. 'fetchClass' => Transfer::class,
  55. ]);
  56. }
  57. /**
  58. * @inheritdoc
  59. */
  60. public function rules() {
  61. return [
  62. [['toUserName', 'toRealName', 'amount', 'type', 'remark'], 'trim'],
  63. [['toUserName', 'toRealName', 'amount', 'type', 'payPassword', 'transferCode'], 'required'],
  64. [['toUserName'], 'exist', 'targetClass' => UserInfo::class, 'targetAttribute' => 'USER_NAME'],
  65. [['type'], 'isType'],
  66. [['toUserName'], 'initUser'],
  67. [['amount'], 'price'],
  68. [['amount'], 'isAmount'],
  69. [['transferCode'], 'validateCode'],
  70. [['payPassword'], 'validatePassword'],
  71. ];
  72. }
  73. /**
  74. * 指定校验场景
  75. * @return array
  76. */
  77. public function scenarios() {
  78. $parentScenarios = parent::scenarios();
  79. $customScenarios = [
  80. 'transfer' => ['type', 'toUserName', 'toRealName', 'amount', 'remark', 'payPassword', 'transferCode'],
  81. ];
  82. return array_merge($parentScenarios, $customScenarios);
  83. }
  84. public function attributeLabels() {
  85. return [
  86. 'toUserName' => '转入会员编号',
  87. 'amount' => '金额',
  88. 'payPassword' => '支付密码',
  89. 'transferCode' => '转账校验码',
  90. ];
  91. }
  92. /**
  93. * 初始化转入转出用户信息
  94. * @param $attribute
  95. * @return null
  96. * @throws Exception
  97. */
  98. public function initUser($attribute) {
  99. // 转账记录
  100. if(!$toUserId = Info::getUserIdByUserName($this->toUserName)){
  101. $this->addError($attribute, '转入会员不存在');
  102. return null;
  103. }
  104. // if(Info::getUserRealNameByUserId($toUserId)!=$this->toRealName){
  105. // $this->addError($attribute, '转入会员会员姓名不正确');
  106. // return null;
  107. // }
  108. $this->_toUserInfo = Info::baseInfo($toUserId);
  109. $fromUserId = \Yii::$app->user->id;
  110. $fromUser = Info::baseInfo($fromUserId);
  111. if (!$fromUser) {
  112. $this->addError($attribute, '转出会员不存在');
  113. return null;
  114. }
  115. // 转账是否已开启
  116. $transferSwitch = Cache::getSystemConfig()['transferSwitch'] ?? '';
  117. LoggerTool::info('transferSwitch . ' . json_encode($transferSwitch));
  118. if ($transferSwitch != 1) {
  119. // 转账未开启,判断是否报单中心. 报单中心可开启转账
  120. $isDec = \frontendApi\modules\v1\models\User::getEnCodeInfo($fromUserId)['IS_DEC'];
  121. if ($isDec != 1) {
  122. throw new \Exception('转账功能已关闭');
  123. return null;
  124. }
  125. }
  126. // 转账条件判断
  127. $orderAmount = Order::find()->where('USER_ID=:USER_ID', [':USER_ID' => $fromUserId])->SUM('ORDER_AMOUNT');
  128. $recNum = intval(DecOrder::find()->where('REC_USER_ID=:REC_USER_ID', [':REC_USER_ID' => $fromUserId])->count());
  129. //$recNum = UserRelation::firstFloorChildNum($fromUserId);
  130. // if ($orderAmount < 300 && $recNum==0) {
  131. // $this->addError($attribute, '消费未满300元或未推荐新人,暂不能转账');
  132. // return null;
  133. // }
  134. $this->_fromUserInfo = $fromUser;
  135. //是否同体系转账
  136. // if($this->_transferConfig['isSystem']==1){
  137. // if($this->_toUserInfo['SYSTEM_ID']!=$this->_fromUserInfo['SYSTEM_ID']){
  138. // $this->addError($attribute, '只能同一体系间转账');
  139. // return null;
  140. // }
  141. // }
  142. //是否点位绑定转账
  143. // if ($this->_transferConfig['isBind'] == 1) {
  144. // if (!UserBind::sameBind($this->_toUserInfo['ID'], $this->_fromUserInfo['ID'])) {
  145. // $this->addError($attribute, '只能点位绑定在一起的会员之间转账');
  146. // return null;
  147. // }
  148. // }
  149. //奖金转奖金限同一身份证
  150. /*if ($this->type == self::BONUS_TO_BONUS) {
  151. $toIdCard = User::findOneAsArray('ID=:ID', [':ID' => $toUser['USER_ID']], 'ID_CARD');
  152. $fromIdCard = User::findOneAsArray('ID=:ID', [':ID' => $fromUser['USER_ID']], 'ID_CARD');
  153. if ($toIdCard['ID_CARD'] && ($toIdCard['ID_CARD'] != $fromIdCard['ID_CARD'])) {
  154. $this->addError($attribute, '奖金转奖金只能在同一身份证下进行');
  155. return null;
  156. }
  157. }*/
  158. //奖金转现金限一周一次
  159. /*if ($this->type == self::BONUS_TO_BALANCE) {
  160. if (Transfer::hasThisWeekTransfer(\Yii::$app->user->id)) {
  161. $this->addError($attribute, '转账失败,每周只可以转账一次');
  162. return null;
  163. }
  164. }*/
  165. }
  166. /**
  167. * 校验申请金额是否小于当前余额并符合配置中的设置
  168. * @param $attribute
  169. * @return null
  170. */
  171. public function isAmount($attribute) {
  172. if(!$this->_fromUserInfo){
  173. $this->addError($attribute, '会员信息未验证通过');
  174. return null;
  175. }
  176. if ($this->amount <= 0) {
  177. $this->addError($attribute, '转账金额必须大于0');
  178. }
  179. if ((int)$this->amount!=$this->amount) {
  180. $this->addError('scenario', '转账金额必须是整数');
  181. }
  182. $minAmount = $this->_transferConfig['outMin'];
  183. if ($this->amount < $minAmount) {
  184. $this->addError($attribute, '转账金额低于转账下限');
  185. }
  186. $maxAmount = $this->_transferConfig['outMax'];
  187. if ($maxAmount && $this->amount > $maxAmount) {
  188. $this->addError($attribute, '转账金额高于转账上限');
  189. }
  190. //周转账上限
  191. if($this->_transferConfig['weekMax']!=0){
  192. if($this->amount+Transfer::weekTransfer($this->_fromUserInfo['ID'])>$this->_transferConfig['weekMax']){
  193. $this->addError($attribute, '超出周转账上限');
  194. }
  195. }
  196. //月转账上限
  197. if($this->_transferConfig['monthMax']!=0){
  198. if($this->amount+Transfer::monthTransfer($this->_fromUserInfo['ID'])>$this->_transferConfig['weekMax']){
  199. $this->addError($attribute, '超出月转账上限');
  200. }
  201. }
  202. // 获取当前用户的可用金额
  203. if( $this->type == self::BALANCE_TO_BALANCE ) {
  204. $haveBalance = Cash::getAvailableBalance($this->_fromUserInfo['ID']);
  205. }else if ($this->type == self::POINT_TO_BALANCE) {
  206. $haveBalance = Balance::getBalanceReconsumePoints($this->_fromUserInfo['ID']);
  207. } else {
  208. $haveBalance = Balance::getAvailableBalance($this->_fromUserInfo['ID']);
  209. }
  210. if ($this->amount > $haveBalance) {
  211. $this->addError($attribute, '转账金额必须小于自己的可用余额');
  212. }
  213. // 转账比例
  214. if ($this->type == self::BONUS_TO_BALANCE) {
  215. $isCanTransferProp = Cache::getSystemConfig()['isCanTransferProp']['VALUE'];
  216. if ($isCanTransferProp == 1) {
  217. $maxAmount = Tool::formatPrice($this->_fromUserInfo['TRANSFER_PROP'] * Balance::getAvailableBalance($this->_fromUserInfo['ID']) * 0.01);
  218. if ($this->amount > $maxAmount) {
  219. $this->addError($attribute, '转账金额超出转账比例限额');
  220. }
  221. }
  222. }
  223. //手续费
  224. if($this->_transferConfig['fee']!=0){
  225. $fee = Tool::formatPrice($this->amount * ($this->_transferConfig['fee']) * 0.01);
  226. if($this->_transferConfig['feeMin']!=0&&$fee<$this->_transferConfig['feeMin']){
  227. $fee = $this->_transferConfig['feeMin'];
  228. }
  229. if($this->_transferConfig['feeMax']!=0&&$fee>$this->_transferConfig['feeMax']){
  230. $fee = $this->_transferConfig['feeMax'];
  231. }
  232. $this->_fee = $fee;
  233. }
  234. }
  235. /**
  236. * 转账类型判断
  237. * @param $attribute
  238. */
  239. public function isType($attribute) {
  240. if (!in_array($this->type, self::allowTransferType)) {
  241. $this->addError($attribute, '不允许的转账类型');
  242. }
  243. //获取转账参数
  244. if(!$this->_transferConfig = Json::decode(Cache::getSystemConfig()['allowWallet']['VALUE'])[$this->type-1]){
  245. $this->addError($attribute, '错误的转账类型');
  246. }
  247. if(!$this->_transferConfig['isOpen']){
  248. $this->addError($attribute, '不可用的转账类型');
  249. }
  250. }
  251. /**
  252. * 校验支付密码
  253. * @param $attribute
  254. * @param $params
  255. */
  256. public function validatePassword($attribute, $params) {
  257. $uid = \Yii::$app->user->id;
  258. if(!$this->toUserName || !$this->toRealName || $this->amount == 0){
  259. $this->addError($attribute, '转账失败');
  260. }else if(!$this->_fromUserInfo){
  261. LoggerTool::info('transfer error, '.$uid.' '. $this->amount.' '. $this->toUserName);
  262. $this->addError($attribute, '转账时发生错误');
  263. }else if (!User::validatePayPassword($this->_fromUserInfo['ID'], $this->payPassword)) {
  264. $this->addError($attribute, '支付密码不正确');
  265. }
  266. }
  267. public function validateCode($attribute, $params) {
  268. $uid = \Yii::$app->user->id;
  269. $redisCode = \Yii::$app->redis->getset('transferCode_'.$uid,'');
  270. \Yii::$app->redis->del('transferCode_'.$uid);
  271. if ($this->transferCode!=$redisCode) {
  272. $this->addError($attribute, '转账校验失败'.$redisCode);
  273. }
  274. }
  275. /**
  276. * 生成随机数
  277. * @param $length
  278. * @param int $numeric
  279. * @return string
  280. */
  281. public function random($length, $numeric = 0) {
  282. $seed = base_convert(md5(microtime() . $_SERVER['DOCUMENT_ROOT']), 16, $numeric ? 10 : 35);
  283. $seed = $numeric ? (str_replace('0', '', $seed) . '012340567890') : ($seed . 'zZ' . strtoupper($seed));
  284. $hash = '';
  285. $max = strlen($seed) - 1;
  286. for ($i = 0; $i < $length; $i++) {
  287. $hash .= $seed[mt_rand(0, $max)];
  288. }
  289. return $hash;
  290. }
  291. /**
  292. * 转账
  293. * @return Transfer|null
  294. * @throws \Exception
  295. */
  296. public function transfer() {
  297. if (!$this->validate()) {
  298. return null;
  299. }
  300. $fromData = Balance::getLogData($this->_fromUserInfo['ID']);
  301. $toData = Balance::getLogData($this->_toUserInfo['ID']);
  302. if ($this->_fromUserInfo['ID']==$this->_toUserInfo['ID'] && $this->_transferConfig['out']==$this->_transferConfig['in']){
  303. throw new \Exception('不能给同一账户转账');
  304. }
  305. $this->userOperateLogger->saveBeforeContent=array_merge($fromData,$toData);
  306. $db = \Yii::$app->db;
  307. $transaction = $db->beginTransaction();
  308. try {
  309. $amount = Tool::formatPrice($this->amount - $this->_fee);
  310. // 转账记录
  311. $period = Period::instance();
  312. $model = new Transfer();
  313. $model->ID = SnowFake::instance()->generateId();
  314. $model->TRANSFER_SN = Transfer::generateSN();
  315. $model->OUT_UID = $this->_fromUserInfo['ID'];
  316. $model->LAST_OUT_USER_NAME = $this->_fromUserInfo['USER_NAME'];
  317. $model->LAST_OUT_REAL_NAME = $this->_fromUserInfo['REAL_NAME'];
  318. $model->LAST_OUT_DEC_LV = $this->_fromUserInfo['DEC_LV'];
  319. $model->LAST_OUT_DEC_ROLE_ID = $this->_fromUserInfo['DEC_ROLE_ID'];
  320. $model->OUT_WALLET = $this->_transferConfig['out'];
  321. $model->LAST_OUT_SYSTEM_ID = $this->_fromUserInfo['SYSTEM_ID'] ? $this->_fromUserInfo['SYSTEM_ID'] : '';
  322. $model->IN_UID = $this->_toUserInfo['ID'];
  323. $model->LAST_IN_USER_NAME = $this->_toUserInfo['USER_NAME'];
  324. $model->LAST_IN_REAL_NAME = $this->_toUserInfo['REAL_NAME'];
  325. $model->LAST_IN_DEC_LV = $this->_toUserInfo['DEC_LV'];
  326. $model->IN_WALLET = $this->_transferConfig['in'];
  327. $model->LAST_IN_SYSTEM_ID = $this->_toUserInfo['SYSTEM_ID'] ? $this->_toUserInfo['SYSTEM_ID'] : '';
  328. $model->ORI_AMOUNT = $this->amount;
  329. $model->FEE = $this->_fee;
  330. $model->AMOUNT = $amount;
  331. $model->REMARK = $this->remark;
  332. $model->PERIOD_NUM = $period->getNowPeriodNum();
  333. $model->CALC_MONTH = $period->getNowYearMonth();
  334. $model->CREATED_AT = Date::nowTime();
  335. if (!$model->save()) {
  336. throw new Exception(Form::formatErrorsForApi($model->getErrors()));
  337. }
  338. if ($this->type == self::BONUS_TO_BALANCE) {
  339. // 会员金额减少
  340. Balance::changeUserBonus($this->_fromUserInfo['ID'], 'bonus', -abs($this->amount), ['TRANSFER_SN' => $model->TRANSFER_SN, 'DEAL_TYPE_ID' => DealType::TRANSFER_OUT, 'REMARK' => 'To:' . $this->_toUserInfo['USER_NAME'] . ',' . $this->remark]);
  341. Cash::changeUserCash($this->_toUserInfo['ID'], 'CASH', abs($amount), ['TRANSFER_SN' => $model->TRANSFER_SN,'DEAL_TYPE_ID' => DealType::TRANSFER_IN, 'REMARK' => 'From:' . $this->_fromUserInfo['USER_NAME'] . ',' . $this->remark]);
  342. } elseif($this->type == self::BONUS_TO_BONUS) {
  343. // 会员金额减少
  344. Balance::changeUserBonus($this->_fromUserInfo['ID'], 'bonus', -abs($this->amount), ['TRANSFER_SN' => $model->TRANSFER_SN, 'DEAL_TYPE_ID' => DealType::TRANSFER_OUT, 'REMARK' => 'To:' . $this->_toUserInfo['USER_NAME'] . ',' . $this->remark]);
  345. Balance::changeUserBonus($this->_toUserInfo['ID'], 'bonus', abs($amount), ['TRANSFER_SN' => $model->TRANSFER_SN, 'DEAL_TYPE_ID' => DealType::TRANSFER_IN, 'REMARK' => 'From:' . $this->_fromUserInfo['USER_NAME'] . ',' . $this->remark]);
  346. }else if ($this->type == self::BALANCE_TO_BALANCE) {
  347. // 会员金额减少
  348. Cash::changeUserCash($this->_fromUserInfo['ID'], 'CASH', -abs($this->amount), ['TRANSFER_SN' => $model->TRANSFER_SN,'DEAL_TYPE_ID' => DealType::TRANSFER_OUT, 'REMARK' => 'To:' . $this->_toUserInfo['USER_NAME'] . ',' . $this->remark]);
  349. Cash::changeUserCash($this->_toUserInfo['ID'], 'CASH', abs($amount), ['TRANSFER_SN' => $model->TRANSFER_SN,'DEAL_TYPE_ID' => DealType::TRANSFER_IN, 'REMARK' => 'From:' . $this->_fromUserInfo['USER_NAME'] . ',' . $this->remark]);
  350. } else if ($this->type == self::POINT_TO_BALANCE) {
  351. // 复消积分转余额
  352. // 减少复消积分
  353. Balance::changeUserBonus(
  354. $this->_fromUserInfo['ID'],
  355. 'reconsume_points',
  356. -abs($this->amount),
  357. [
  358. 'TRANSFER_SN' => $model->TRANSFER_SN,
  359. 'DEAL_TYPE_ID' => DealType::TRANSFER_OUT,
  360. 'REMARK' => 'To:' . $this->_toUserInfo['USER_NAME'] . ',' . $this->remark
  361. ]
  362. );
  363. // 增加余额
  364. Cash::changeUserCash(
  365. $this->_toUserInfo['ID'],
  366. 'CASH', abs($amount),
  367. [
  368. 'TRANSFER_SN' => $model->TRANSFER_SN,
  369. 'DEAL_TYPE_ID' => DealType::TRANSFER_IN,
  370. 'REMARK' => 'From:' . $this->_fromUserInfo['USER_NAME'] . ',' . $this->remark
  371. ]
  372. );
  373. } else {
  374. throw new \Exception('错误的交易类型');
  375. }
  376. $transaction->commit();
  377. } catch (Exception $e) {
  378. $transaction->rollBack();
  379. $this->addError('edit', $e->getMessage());
  380. return null;
  381. }
  382. $fromData = Balance::getLogData($this->_fromUserInfo['ID']);
  383. $toData = Balance::getLogData($this->_toUserInfo['ID']);
  384. $this->userOperateLogger->saveAfterContent=array_merge($fromData,$toData);
  385. unset($fromData,$toData);
  386. $this->userOperateLogger->clean()->save([
  387. 'optType' => '会员转账',
  388. 'userId' => \Yii::$app->user->id,
  389. 'userName' => Info::getUserNameByUserId(\Yii::$app->user->id),
  390. 'remark' => $this->remark,
  391. ]);
  392. return $model;
  393. }
  394. }