Cash.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. namespace app\api\controller;
  3. use think\Db;
  4. use think\Request;
  5. /**
  6. * 提现管理 API
  7. *
  8. * 提供用户积分提现的申请、列表查询、删除等功能。
  9. * 所有接口均需用户登录(Cookie/Session 认证)。
  10. */
  11. class Cash 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. * GET /api.php/cash/get_list?page=1&limit=20
  40. *
  41. * @param page int 可选,页码,默认1
  42. * @param limit int 可选,每页条数,默认20,最大100
  43. * @param status int 可选,提现状态筛选(0=待审核,1=已审核)
  44. * @return JSON {code:1, msg:'获取成功', info:{page, pagecount, limit, total, list:[...]}}
  45. */
  46. public function get_list(Request $request)
  47. {
  48. $auth = $this->_checkLogin();
  49. if (!$auth['ok']) return $auth['response'];
  50. $param = $request->param();
  51. $validate = validate($request->controller());
  52. if (!$validate->scene($request->action())->check($param)) {
  53. return json(['code' => 1001, 'msg' => '参数错误: ' . $validate->getError()]);
  54. }
  55. $page = max(1, intval($param['page'] ?? 1));
  56. $limit = max(1, min(100, intval($param['limit'] ?? 20)));
  57. $where = ['user_id' => $auth['user_id']];
  58. if (isset($param['status']) && $param['status'] !== '') {
  59. $where['cash_status'] = intval($param['status']);
  60. }
  61. $order = 'cash_id desc';
  62. $res = model('Cash')->listData($where, $order, $page, $limit);
  63. return json([
  64. 'code' => 1,
  65. 'msg' => '获取成功',
  66. 'info' => $res,
  67. ]);
  68. }
  69. /**
  70. * 获取提现详情
  71. * GET /api.php/cash/get_detail?cash_id=1
  72. *
  73. * @param cash_id int 必填,提现记录ID
  74. * @return JSON {code:1, msg:'获取成功', info:{...}}
  75. */
  76. public function get_detail(Request $request)
  77. {
  78. $auth = $this->_checkLogin();
  79. if (!$auth['ok']) return $auth['response'];
  80. $param = $request->param();
  81. $validate = validate($request->controller());
  82. if (!$validate->scene($request->action())->check($param)) {
  83. return json(['code' => 1001, 'msg' => '参数错误: ' . $validate->getError()]);
  84. }
  85. $cash_id = intval($param['cash_id'] ?? 0);
  86. $where = [
  87. 'cash_id' => $cash_id,
  88. 'user_id' => $auth['user_id'],
  89. ];
  90. $res = model('Cash')->infoData($where);
  91. return json($res);
  92. }
  93. /**
  94. * 提交提现申请
  95. * POST /api.php/cash/create
  96. *
  97. * @param cash_money float 必填,提现金额(单位:元)
  98. * @param cash_bank_name string 必填,银行名称
  99. * @param cash_bank_no string 必填,银行账号
  100. * @param cash_payee_name string 必填,收款人姓名
  101. * @return JSON {code:1, msg:'保存成功'}
  102. *
  103. * 说明:
  104. * - 提现需后台开启提现功能(cash_status=1)
  105. * - 提现金额不能低于后台设置的最小提现金额(cash_min)
  106. * - 提现所需积分 = 提现金额 × 提现兑换比例(cash_ratio)
  107. * - 提现后对应积分会冻结,待管理员审核后正式扣除
  108. */
  109. public function create(Request $request)
  110. {
  111. $auth = $this->_checkLogin();
  112. if (!$auth['ok']) return $auth['response'];
  113. $param = $request->param();
  114. $validate = validate($request->controller());
  115. if (!$validate->scene($request->action())->check($param)) {
  116. return json(['code' => 1001, 'msg' => '参数错误: ' . $validate->getError()]);
  117. }
  118. $param['user_id'] = $auth['user_id'];
  119. $res = model('Cash')->saveData($param);
  120. return json($res);
  121. }
  122. /**
  123. * 删除提现记录
  124. * POST /api.php/cash/del
  125. *
  126. * @param ids string 可选,提现记录ID列表,逗号分隔(与 all 二选一)
  127. * @param all string 可选,传 "1" 表示删除全部
  128. * @return JSON {code:1, msg:'删除成功'}
  129. *
  130. * 说明:
  131. * - 仅能删除当前登录用户的提现记录
  132. * - 未审核的提现记录删除后,冻结积分会自动恢复
  133. */
  134. public function del(Request $request)
  135. {
  136. $auth = $this->_checkLogin();
  137. if (!$auth['ok']) return $auth['response'];
  138. $param = $request->param();
  139. $validate = validate($request->controller());
  140. if (!$validate->scene($request->action())->check($param)) {
  141. return json(['code' => 1001, 'msg' => '参数错误: ' . $validate->getError()]);
  142. }
  143. $ids = htmlspecialchars(urldecode(trim($param['ids'] ?? '')));
  144. $all = $param['all'] ?? '';
  145. if (empty($ids) && $all != '1') {
  146. return json(['code' => 1001, 'msg' => '参数错误: ids 或 all=1 必须']);
  147. }
  148. $where = ['user_id' => $auth['user_id']];
  149. if ($all != '1') {
  150. $arr = array_filter(array_map('intval', explode(',', $ids)));
  151. if (empty($arr)) {
  152. return json(['code' => 1001, 'msg' => '参数错误: ids 格式不正确']);
  153. }
  154. $where['cash_id'] = ['in', implode(',', $arr)];
  155. }
  156. $res = model('Cash')->delData($where);
  157. return json($res);
  158. }
  159. /**
  160. * 获取提现配置信息
  161. * GET /api.php/cash/get_config
  162. *
  163. * @return JSON {code:1, msg:'获取成功', info:{cash_status, cash_min, cash_ratio}}
  164. *
  165. * 说明:
  166. * - cash_status: 提现功能开关(0=关闭, 1=开启)
  167. * - cash_min: 最小提现金额(单位:元)
  168. * - cash_ratio: 兑换比例(1元 = 多少积分)
  169. */
  170. public function get_config(Request $request)
  171. {
  172. $user_config = $GLOBALS['config']['user'] ?? [];
  173. return json([
  174. 'code' => 1,
  175. 'msg' => '获取成功',
  176. 'info' => [
  177. 'cash_status' => intval($user_config['cash_status'] ?? 0),
  178. 'cash_min' => floatval($user_config['cash_min'] ?? 0),
  179. 'cash_ratio' => intval($user_config['cash_ratio'] ?? 1),
  180. ],
  181. ]);
  182. }
  183. }