Order.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. namespace app\api\controller;
  3. use think\Db;
  4. use think\Request;
  5. /**
  6. * 充值订单管理 API
  7. *
  8. * 提供订单列表、详情、状态查询(创建订单走 index 模块 POST user/buy)。
  9. * 所有接口均需用户登录(Cookie/Session 认证)。
  10. */
  11. class Order extends Base
  12. {
  13. use PublicApi;
  14. public function __construct()
  15. {
  16. parent::__construct();
  17. $this->check_config();
  18. }
  19. /**
  20. * 辅助:检查登录
  21. */
  22. private function _checkLogin()
  23. {
  24. $check = model('User')->checkLogin();
  25. if ($check['code'] > 1) {
  26. return ['ok' => false, 'user_id' => 0, 'user' => null,
  27. 'response' => json(['code' => 1401, 'msg' => '未登录,请先登录'])];
  28. }
  29. $uid = intval($check['info']['user_id']);
  30. $user = Db::name('User')->where('user_id', $uid)->find();
  31. if (!$user) {
  32. return ['ok' => false, 'user_id' => 0, 'user' => null,
  33. 'response' => json(['code' => 1002, 'msg' => '用户不存在'])];
  34. }
  35. return ['ok' => true, 'user_id' => $uid, 'user' => $user, 'response' => null];
  36. }
  37. /**
  38. * 创建充值订单
  39. * POST /api.php/order/create
  40. *
  41. * @param price float 必填,充值金额(单位:元)
  42. * @return JSON {code:1, msg:'订单创建成功', info:{order_code, order_price, order_points, order_time}}
  43. */
  44. public function create(Request $request)
  45. {
  46. $auth = $this->_checkLogin();
  47. if (!$auth['ok']) return $auth['response'];
  48. $param = $request->param();
  49. $validate = validate($request->controller());
  50. if (!$validate->scene($request->action())->check($param)) {
  51. return json(['code' => 1001, 'msg' => '参数错误: ' . $validate->getError()]);
  52. }
  53. $price = floatval($param['price'] ?? 0);
  54. $pay_config = config('maccms.pay');
  55. if (!empty($pay_config['min']) && $price < $pay_config['min']) {
  56. return json(['code' => 1002, 'msg' => '最小充值金额不能低于' . $pay_config['min'] . '元']);
  57. }
  58. $data = [];
  59. $data['user_id'] = $auth['user_id'];
  60. $data['order_code'] = 'PAY' . mac_get_uniqid_code();
  61. $data['order_price'] = $price;
  62. $data['order_time'] = time();
  63. $data['order_points'] = intval(($pay_config['scale'] ?? 1) * $price);
  64. $res = model('Order')->saveData($data);
  65. if ($res['code'] > 1) {
  66. return json($res);
  67. }
  68. return json([
  69. 'code' => 1,
  70. 'msg' => '订单创建成功',
  71. 'info' => [
  72. 'order_code' => $data['order_code'],
  73. 'order_price' => $data['order_price'],
  74. 'order_points' => $data['order_points'],
  75. 'order_time' => $data['order_time'],
  76. ],
  77. ]);
  78. }
  79. /**
  80. * 获取用户订单列表
  81. * GET /api.php/order/get_list?page=1&limit=20&status=
  82. *
  83. * @param page int 可选,页码,默认1
  84. * @param limit int 可选,每页条数,默认20,最大100
  85. * @param status int 可选,订单状态筛选(0=未支付,1=已支付)
  86. * @return JSON {code:1, msg:'获取成功', info:{page, pagecount, limit, total, list:[...]}}
  87. */
  88. public function get_list(Request $request)
  89. {
  90. $auth = $this->_checkLogin();
  91. if (!$auth['ok']) return $auth['response'];
  92. $param = $request->param();
  93. $validate = validate($request->controller());
  94. if (!$validate->scene($request->action())->check($param)) {
  95. return json(['code' => 1001, 'msg' => '参数错误: ' . $validate->getError()]);
  96. }
  97. $page = max(1, intval($param['page'] ?? 1));
  98. $limit = max(1, min(100, intval($param['limit'] ?? 20)));
  99. $where = [];
  100. $where['o.user_id'] = $auth['user_id'];
  101. if (isset($param['status']) && $param['status'] !== '') {
  102. $where['order_status'] = intval($param['status']);
  103. }
  104. $order = 'o.order_id desc';
  105. $res = model('Order')->listData($where, $order, $page, $limit);
  106. return json([
  107. 'code' => 1,
  108. 'msg' => '获取成功',
  109. 'info' => $res,
  110. ]);
  111. }
  112. /**
  113. * 获取订单详情
  114. * GET /api.php/order/get_detail?order_id=1 或 ?order_code=PAYxxx
  115. *
  116. * @param order_id int 可选,订单ID(与 order_code 二选一)
  117. * @param order_code string 可选,订单号
  118. * @return JSON {code:1, msg:'获取成功', info:{...}}
  119. */
  120. public function get_detail(Request $request)
  121. {
  122. $auth = $this->_checkLogin();
  123. if (!$auth['ok']) return $auth['response'];
  124. $param = $request->param();
  125. $validate = validate($request->controller());
  126. if (!$validate->scene($request->action())->check($param)) {
  127. return json(['code' => 1001, 'msg' => '参数错误: ' . $validate->getError()]);
  128. }
  129. $where = [];
  130. $where['user_id'] = $auth['user_id'];
  131. if (!empty($param['order_id'])) {
  132. $where['order_id'] = intval($param['order_id']);
  133. } elseif (!empty($param['order_code'])) {
  134. $where['order_code'] = htmlspecialchars(urldecode(trim($param['order_code'])));
  135. } else {
  136. return json(['code' => 1001, 'msg' => '参数错误: order_id 或 order_code 必须']);
  137. }
  138. $res = model('Order')->infoData($where);
  139. return json($res);
  140. }
  141. /**
  142. * 查询订单支付状态
  143. * GET /api.php/order/check_status?order_code=PAYxxx
  144. *
  145. * @param order_code string 必填,订单号
  146. * @return JSON {code:1, msg:'...', info:{order_code, order_status, order_status_text, order_pay_type, order_pay_time}}
  147. */
  148. public function check_status(Request $request)
  149. {
  150. $auth = $this->_checkLogin();
  151. if (!$auth['ok']) return $auth['response'];
  152. $param = $request->param();
  153. $validate = validate($request->controller());
  154. if (!$validate->scene($request->action())->check($param)) {
  155. return json(['code' => 1001, 'msg' => '参数错误: ' . $validate->getError()]);
  156. }
  157. $order_code = htmlspecialchars(urldecode(trim($param['order_code'] ?? '')));
  158. if (empty($order_code)) {
  159. return json(['code' => 1001, 'msg' => '参数错误: order_code 必须']);
  160. }
  161. $where = [];
  162. $where['order_code'] = $order_code;
  163. $where['user_id'] = $auth['user_id'];
  164. $res = model('Order')->infoData($where);
  165. if ($res['code'] > 1) {
  166. return json($res);
  167. }
  168. $info = $res['info'];
  169. $status_text = $info['order_status'] == 1 ? '已支付' : '未支付';
  170. return json([
  171. 'code' => 1,
  172. 'msg' => '获取成功',
  173. 'info' => [
  174. 'order_code' => $info['order_code'],
  175. 'order_price' => $info['order_price'],
  176. 'order_points' => $info['order_points'],
  177. 'order_status' => intval($info['order_status']),
  178. 'order_status_text' => $status_text,
  179. 'order_pay_type' => $info['order_pay_type'] ?? '',
  180. 'order_pay_time' => $info['order_pay_time'] ?? 0,
  181. 'order_time' => $info['order_time'],
  182. ],
  183. ]);
  184. }
  185. }