'直营收入', self::TYPE_DISTRIBUTION => '分销收入', self::TYPE_ROYALTY => '提成', self::TYPE_ACTIBVE => '活动', self::TYPE_WITHDRAW => '提现', ]; // 状态 const STATUS_ADD = 0; //余额增加 const STATUS_SUB = 1; //余额减少 public static $status = [ self::STATUS_ADD => '余额增加', self::STATUS_SUB => '余额减少', ]; /** * 添加记录并修改用户余额 * * @param array $data 用户相关数据 * - user_id 用户ID * - yj 金额 (总金额,如:订单佣金 + 提成佣金) * - status 状态:0 表示余额增加,1 表示余额减少 * - source 来源,例如:订单、提现等 * - order_no 订单号 * - remark 备注信息 * - order_yj 订单佣金 * * @return array 返回状态、提示信息和相关数据 * - $status bool 操作是否成功 * - $msg string 提示信息 * - $data array 相关数据(例如操作后的余额等) */ public function recharge($data) { $validator = Validator::make($data, [ 'user_id' => 'required', // 用户id 'yj' => 'required|numeric', // 佣金金额 'status' => 'required|in:0,1', // 状态 0余额增加,1余额减少 'type' => 'required|in:'.implode(',',array_keys(self::$type)), ]); if ($validator->fails()) { return [false, $validator->errors()->first(), []]; } DB::beginTransaction(); try { $source = $data['source'] ?? null; $type = $data['type']; $order_no = $data['order_no'] ?? null; $remark = $data['remark'] ?? null; $status = $data['status']; $user_id = $data['user_id']; $yj = (string)$data['yj']; $order_yj = $data['order_yj'] ?? 0; $userInfo = AdminUsers::find($user_id); if (empty($userInfo)) { return [false, "用户不存在", []]; } $user_yj = (string)$userInfo->yj; $al_yj = (string)$userInfo->al_yj; if ($status == 0) { $yu_yj = bcadd($user_yj, $yj, 2); $al_yj = bcadd($al_yj, $yj, 2); $userInfo->update([ 'yj' => $yu_yj, 'al_yj' => $al_yj, ]); } elseif ($status == 1) { $yu_yj = bcsub($user_yj, $yj, 2); if ($yu_yj < 0) { return [false, "余额不足", []]; } $userInfo->update([ 'yj' => $yu_yj, ]); } else { return [false, "修改余额失败", []]; } self::create([ 'user_id' => $user_id, 'order_no' => $order_no, 'balance' => $yj, 'source' => $source, 'type' => $type, 'status' => $status, 'yu_yj' => $yu_yj, 'remark' => $remark, 'order_yj' => $order_yj, ]); DB::commit(); return [true, "success", []]; } catch (\Exception $e) { DB::rollBack(); Log::error($e); return [false, "修改余额失败", []]; } } }