RechargeOrder.php 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. <?php
  2. declare (strict_types=1);
  3. namespace app\model\api;
  4. use library\basic\BaseModel;
  5. use think\facade\Cache;
  6. use think\Model;
  7. /**
  8. * @mixin \think\Model
  9. */
  10. class RechargeOrder extends BaseModel
  11. {
  12. /**
  13. * 生成充值订单号
  14. * @param int $uid 用户UID
  15. * @return string
  16. */
  17. public function mkOrderSn($uid)
  18. {
  19. return 'RO' . time() . rand(1000, 9000) . $uid;
  20. }
  21. /**
  22. * 创建充值订单
  23. * @param int $uid 用户UID
  24. * @param int $rechargeId 充值配置ID(0为自定义充值)
  25. * @param float $price 充值金额
  26. * @param int $integral 获得积分
  27. * @param int $giveIntegral 赠送积分
  28. * @param string $payType 支付方式:wxpay/alipay/system
  29. * @param int $adminId 管理员ID(后台充值)
  30. * @param string $remark 备注
  31. * @param float $discount 折扣金额
  32. * @return string|null 订单号
  33. */
  34. public function createOrder($uid, $rechargeId, $price, $integral, $giveIntegral, $payType = 'wxpay', $adminId = 0, $remark = '', $discount = 0)
  35. {
  36. try {
  37. $orderSn = $this->mkOrderSn($uid);
  38. $totalIntegral = $integral + $giveIntegral;
  39. $bool = $this->insert([
  40. 'order_id' => $orderSn,
  41. 'uid' => $uid,
  42. 'recharge_id' => $rechargeId,
  43. 'price' => $price,
  44. 'integral' => $integral,
  45. 'give_integral' => $giveIntegral,
  46. 'total_integral' => $totalIntegral,
  47. 'pay_type' => $payType,
  48. 'paid' => 0,
  49. 'pay_time' => 0,
  50. 'remark' => $remark,
  51. 'admin_id' => $adminId,
  52. 'discount_amount' => $discount,
  53. 'status' => 0,
  54. 'add_time' => time(),
  55. 'update_time' => time()
  56. ]);
  57. return $bool;
  58. } catch (\Exception $e) {
  59. @file_put_contents('quanju.txt', date('Y-m-d H:i:s') . ' [createOrder Error] ' . $e->getMessage() . ' File: ' . $e->getFile() . ' Line: ' . $e->getLine() . "创建支付订单报错\r\n", 8);
  60. return null;
  61. }
  62. }
  63. /**
  64. * 根据订单号获取订单
  65. * @param string $orderSn 订单号
  66. * @return array|null
  67. */
  68. public function getByOrderSn($orderSn)
  69. {
  70. return $this->where('order_id', $orderSn)->find();
  71. }
  72. /**
  73. * 根据ID获取订单
  74. * @param int $id 订单ID
  75. * @return array|null
  76. */
  77. public function getById($id)
  78. {
  79. return $this->where('id', $id)->find();
  80. }
  81. /**
  82. * 更新订单支付状态
  83. * @param string $orderSn 订单号
  84. * @param string $paySn 支付流水号
  85. * @param string $payJson 支付返回信息
  86. * @return bool
  87. */
  88. public function updatePayStatus($orderSn, $paySn, $payJson = '')
  89. {
  90. return $this->where('order_id', $orderSn)->update([
  91. 'paid' => 1,
  92. 'pay_sn' => $paySn,
  93. 'pay_json' => $payJson,
  94. 'pay_time' => time(),
  95. 'status' => 1,
  96. 'update_time' => time()
  97. ]);
  98. }
  99. /**
  100. * 获取用户充值订单列表
  101. * @param int $uid 用户UID
  102. * @param array $where 查询条件
  103. * @param string $field 查询字段
  104. * @param int $page 页码
  105. * @param int $pageCount 每页数量
  106. * @param string $order 排序
  107. * @return array
  108. */
  109. public function getUserList($uid, $where = [], $field = '*', $page = 1, $pageCount = 20, $order = 'add_time desc')
  110. {
  111. $where['uid'] = $uid;
  112. $data = $this
  113. ->where($where)
  114. ->field($field)
  115. ->order($order)
  116. ->paginate(['list_rows' => $pageCount, 'page' => $page])
  117. ->toArray();
  118. return [$data['total'], $data['data']];
  119. }
  120. /**
  121. * 获取充值订单列表(后台)
  122. * @param array $where 查询条件
  123. * @param string $field 查询字段
  124. * @param int $page 页码
  125. * @param int $pageCount 每页数量
  126. * @param string $order 排序
  127. * @return array
  128. */
  129. public function getAdminList($where = [], $field = '*', $page = 1, $pageCount = 20, $order = 'add_time desc')
  130. {
  131. $data = $this
  132. ->alias('ro')
  133. ->field($field)
  134. ->leftJoin('user u', 'u.uid = ro.uid')
  135. ->when(!empty($where), function ($query) use ($where) {
  136. if (!empty($where['order_id'])) {
  137. $query->where('ro.order_id', $where['order_id']);
  138. }
  139. if (!empty($where['uid'])) {
  140. $query->where('ro.uid', $where['uid']);
  141. }
  142. if (!empty($where['nickname'])) {
  143. $query->whereLike('u.nickname', "%{$where['nickname']}%");
  144. }
  145. if (!empty($where['mobile'])) {
  146. $query->where('u.mobile', $where['mobile']);
  147. }
  148. if (!empty($where['pay_type'])) {
  149. $query->where('ro.pay_type', $where['pay_type']);
  150. }
  151. if (!empty($where['paid'])) {
  152. $query->where('ro.paid', $where['paid']);
  153. }
  154. if (!empty($where['status'])) {
  155. $query->where('ro.status', $where['status']);
  156. }
  157. if (!empty($where['time']) && !empty($where['time'][0]) && !empty($where['time'][1])) {
  158. $startTime = strtotime($where['time'][0]);
  159. $endTime = strtotime($where['time'][1]);
  160. $query->whereBetween('ro.add_time', "{$startTime},{$endTime}");
  161. }
  162. })
  163. ->order($order)
  164. ->paginate(['list_rows' => $pageCount, 'page' => $page])
  165. ->toArray();
  166. return [$data['total'], $data['data']];
  167. }
  168. /**
  169. * 关闭订单
  170. * @param string $orderSn 订单号
  171. * @return bool
  172. */
  173. public function closeOrder($orderSn)
  174. {
  175. return $this->where('order_id', $orderSn)->update([
  176. 'status' => -1,
  177. 'update_time' => time()
  178. ]);
  179. }
  180. /**
  181. * 增加用户积分(内部调用)
  182. * @param int $uid 用户ID
  183. * @param int $totalIntegral 总积分
  184. * @param string $orderSn 订单号
  185. * @param float $price 充值金额
  186. * @param int $orderId 订单ID
  187. * @return bool
  188. */
  189. private function addUserIntegral($uid, $totalIntegral, $orderSn, $price, $orderId)
  190. {
  191. try {
  192. $userModel = new User();
  193. $user = $userModel->where('uid', $uid)->find();
  194. if ($user) {
  195. // 更新用户积分和累计充值金额
  196. // $res=$userModel->where('uid', $uid)->inc('score', $totalIntegral)->inc('score_in', $totalIntegral)->update();
  197. // @file_put_contents('quanju.txt', $res. "-增加积分怎么样\r\n", 8);
  198. // 记录积分明细
  199. $scoreDetail = new UserScoreDetail();
  200. $scoreDetail->incomeScore($uid, $totalIntegral, $orderSn, 'income_score', [
  201. 'o_id' => $orderId
  202. ], $orderId,'充值积分',"充值".$price."元获得".$totalIntegral."积分");
  203. }
  204. // 清除用户折扣缓存,以便下次充值生成新的折扣金额
  205. Cache::store('redis')->delete('recharge_discount_' . $uid);
  206. return true;
  207. } catch (\Exception $e) {
  208. // 将错误信息保存到 quanju.txt 文件
  209. @file_put_contents('quanju.txt', $e->getLine() . $e->getMessage() . $e->getFile() . "-增加用户积分报错内容\r\n", 8);
  210. return false;
  211. }
  212. }
  213. /**
  214. * 充值成功回调处理(内部调用)
  215. * @param string $orderSn 订单号
  216. * @param string $paySn 支付流水号
  217. * @param string $payJson 支付返回信息
  218. * @return bool
  219. */
  220. public function paySuccess($orderSn, $paySn, $payJson = '')
  221. {
  222. try {
  223. @file_put_contents('quanju.txt', $orderSn . "-orderSn\r\n", 8);
  224. @file_put_contents('quanju.txt', $paySn . "-paySn\r\n", 8);
  225. $rechargeOrder = new RechargeOrder();
  226. $order_id=(new PayTrade)->where('pay_no', $orderSn)->value('order_id');
  227. $orderInfo = $rechargeOrder->getByOrderSn($order_id);
  228. @file_put_contents('quanju.txt', json_encode($orderInfo) . "-充值订单信息\r\n", 8);
  229. if (!$orderInfo) {
  230. return false;
  231. }
  232. // 更新订单支付状态
  233. $updateResult = $rechargeOrder->updatePayStatus($order_id, $paySn, $payJson);
  234. @file_put_contents('quanju.txt', json_encode($updateResult) . "-更新订单支付状态\r\n", 8);
  235. if (!$updateResult) {
  236. return false;
  237. }
  238. // 增加用户积分
  239. $this->addUserIntegral($orderInfo['uid'], $orderInfo['total_integral'], $orderSn, $orderInfo['price'], $orderInfo['id']);
  240. return true;
  241. } catch (\Exception $e) {
  242. // 将错误信息保存到 quanju.txt 文件
  243. // $logMessage = date('Y-m-d H:i:s') . " [paySuccess Error] OrderSn: {$orderSn}, PaySn: {$paySn}, Error: " . $e->getMessage() . "\n";
  244. // file_put_contents('quanju.txt', $logMessage, FILE_APPEND);
  245. @file_put_contents('quanju.txt', $e->getLine() . $e->getMessage() . $e->getFile() . "-充值完成报错内容\r\n", 8);
  246. return false;
  247. }
  248. }
  249. }