1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- <?php
- namespace addons\shopro\service;
- use think\exception\HttpResponseException;
- use app\admin\model\shopro\user\User as UserModel;
- use app\admin\model\shopro\user\WalletLog;
- use addons\shopro\library\Operator;
- use app\common\model\MoneyLog;
- use app\common\model\ScoreLog;
- class Wallet
- {
-
- public static function change($user, $type, $amount, $event, $ext = [], $memo = '')
- {
-
- if (is_numeric($user)) {
- $user = UserModel::getById($user);
- }
- if (!$user) {
- error_stop('未找到用户');
- }
-
- if ($amount == 0) {
- error_stop('请输入正确的金额');
- }
- if (!in_array($type, ['money', 'score', 'commission'])) {
- error_stop('参数错误');
- }
- $before = $user->$type;
- $after = bcadd((string)$user->$type, (string)$amount, 2);
-
- if ($after < 0 && !in_array($event, ['admin_recharge', 'reward_back'])) {
- $walletTypeText = WalletLog::TYPE_MAP[$type];
- error_stop("可用{$walletTypeText}不足");
- }
- try {
-
- $user->setInc($type, $amount);
-
- $oper = Operator::get();
-
- $walletLog = WalletLog::create([
- 'user_id' => $user->id,
- 'amount' => $amount,
- 'type' => $type,
- 'before' => $before,
- 'after' => $after,
- 'event' => $event,
- 'memo' => $memo,
- 'ext' => $ext,
- 'oper_type' => $oper['type'],
- 'oper_id' => $oper['id']
- ]);
-
-
-
- $data = ['walletLog' => $walletLog, 'type' => $type];
- \think\Hook::listen('user_wallet_change', $data);
- } catch (HttpResponseException $e) {
- $data = $e->getResponse()->getData();
- $message = $data ? ($data['msg'] ?? '') : $e->getMessage();
- error_stop($message);
- } catch (\Exception $e) {
- error_stop('您提交的数据不正确');
- }
- return true;
- }
- }
|