mkOrderSn($uid); $totalIntegral = $integral + $giveIntegral; $bool = $this->insert([ 'order_id' => $orderSn, 'uid' => $uid, 'recharge_id' => $rechargeId, 'price' => $price, 'integral' => $integral, 'give_integral' => $giveIntegral, 'total_integral' => $totalIntegral, 'pay_type' => $payType, 'paid' => 0, 'pay_time' => 0, 'remark' => $remark, 'admin_id' => $adminId, 'discount_amount' => $discount, 'status' => 0, 'add_time' => time(), 'update_time' => time() ]); return $bool ? $orderSn : null; } /** * 根据订单号获取订单 * @param string $orderSn 订单号 * @return array|null */ public function getByOrderSn($orderSn) { return $this->where('order_id', $orderSn)->find(); } /** * 根据ID获取订单 * @param int $id 订单ID * @return array|null */ public function getById($id) { return $this->where('id', $id)->find(); } /** * 更新订单支付状态 * @param string $orderSn 订单号 * @param string $paySn 支付流水号 * @param string $payJson 支付返回信息 * @return bool */ public function updatePayStatus($orderSn, $paySn, $payJson = '') { return $this->where('order_id', $orderSn)->update([ 'paid' => 1, 'pay_sn' => $paySn, 'pay_json' => $payJson, 'pay_time' => time(), 'status' => 1, 'update_time' => time() ]); } /** * 获取用户充值订单列表 * @param int $uid 用户UID * @param array $where 查询条件 * @param string $field 查询字段 * @param int $page 页码 * @param int $pageCount 每页数量 * @param string $order 排序 * @return array */ public function getUserList($uid, $where = [], $field = '*', $page = 1, $pageCount = 20, $order = 'add_time desc') { $where['uid'] = $uid; $data = $this ->where($where) ->field($field) ->order($order) ->paginate(['list_rows' => $pageCount, 'page' => $page]) ->toArray(); return [$data['total'], $data['data']]; } /** * 获取充值订单列表(后台) * @param array $where 查询条件 * @param string $field 查询字段 * @param int $page 页码 * @param int $pageCount 每页数量 * @param string $order 排序 * @return array */ public function getAdminList($where = [], $field = '*', $page = 1, $pageCount = 20, $order = 'add_time desc') { $data = $this ->alias('ro') ->field($field) ->leftJoin('user u', 'u.uid = ro.uid') ->when(!empty($where), function ($query) use ($where) { if (!empty($where['order_id'])) { $query->where('ro.order_id', $where['order_id']); } if (!empty($where['uid'])) { $query->where('ro.uid', $where['uid']); } if (!empty($where['nickname'])) { $query->whereLike('u.nickname', "%{$where['nickname']}%"); } if (!empty($where['mobile'])) { $query->where('u.mobile', $where['mobile']); } if (!empty($where['pay_type'])) { $query->where('ro.pay_type', $where['pay_type']); } if (!empty($where['paid'])) { $query->where('ro.paid', $where['paid']); } if (!empty($where['status'])) { $query->where('ro.status', $where['status']); } if (!empty($where['time']) && !empty($where['time'][0]) && !empty($where['time'][1])) { $startTime = strtotime($where['time'][0]); $endTime = strtotime($where['time'][1]); $query->whereBetween('ro.add_time', "{$startTime},{$endTime}"); } }) ->order($order) ->paginate(['list_rows' => $pageCount, 'page' => $page]) ->toArray(); return [$data['total'], $data['data']]; } /** * 关闭订单 * @param string $orderSn 订单号 * @return bool */ public function closeOrder($orderSn) { return $this->where('order_id', $orderSn)->update([ 'status' => -1, 'update_time' => time() ]); } /** * 增加用户积分(内部调用) * @param int $uid 用户ID * @param int $totalIntegral 总积分 * @param string $orderSn 订单号 * @param float $price 充值金额 * @param int $orderId 订单ID * @return bool */ private function addUserIntegral($uid, $totalIntegral, $orderSn, $price, $orderId) { try { $userModel = new User(); $user = $userModel->where('uid', $uid)->find(); if ($user) { // 更新用户积分和累计充值金额 $userModel->where('uid', $uid)->inc('score', $totalIntegral)->inc('score_in', $totalIntegral)->update(); // 记录积分明细 $scoreDetail = new UserScoreDetail(); $scoreDetail->incomeScore($uid, $totalIntegral, $orderSn, 'income_score', [ 'o_id' => $orderId ], $orderId); } // 清除用户折扣缓存,以便下次充值生成新的折扣金额 Cache::store('redis')->delete('recharge_discount_' . $uid); return true; } catch (\Exception $e) { // 将错误信息保存到 quanju.txt 文件 @file_put_contents('quanju.txt', $e->getLine() . $e->getMessage() . $e->getFile() . "-增加用户积分报错内容\r\n", 8); return false; } } /** * 充值成功回调处理(内部调用) * @param string $orderSn 订单号 * @param string $paySn 支付流水号 * @param string $payJson 支付返回信息 * @return bool */ public function paySuccess($orderSn, $paySn, $payJson = '') { try { $rechargeOrder = new RechargeOrder(); $orderInfo = $rechargeOrder->getByOrderSn($orderSn); @file_put_contents('quanju.txt', json_encode($orderInfo) . "-充值订单信息\r\n", 8); if (!$orderInfo) { return false; } // 更新订单支付状态 $updateResult = $rechargeOrder->updatePayStatus($orderSn, $paySn, $payJson); @file_put_contents('quanju.txt', json_encode($orderInfo) . "-充值订单信息\r\n", 8); if (!$updateResult) { return false; } // 增加用户积分 $this->addUserIntegral($orderInfo['uid'], $orderInfo['total_integral'], $orderSn, $orderInfo['price'], $orderInfo['id']); return true; } catch (\Exception $e) { // 将错误信息保存到 quanju.txt 文件 // $logMessage = date('Y-m-d H:i:s') . " [paySuccess Error] OrderSn: {$orderSn}, PaySn: {$paySn}, Error: " . $e->getMessage() . "\n"; // file_put_contents('quanju.txt', $logMessage, FILE_APPEND); @file_put_contents('quanju.txt', $e->getLine() . $e->getMessage() . $e->getFile() . "-充值完成报错内容\r\n", 8); return false; } } }