Money.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. namespace app\api\controller;
  3. use addons\epay\library\Service;
  4. use think\Db;
  5. /**
  6. * 钱包接口
  7. */
  8. class Money extends Common
  9. {
  10. protected $noNeedLogin = [];
  11. protected $noNeedRight = '*';
  12. public function _initialize()
  13. {
  14. parent::_initialize();
  15. }
  16. /**
  17. * 充值
  18. */
  19. public function recharge() {
  20. $purpose = $this->request->request("purpose");// 充值用途 1=认证,2=有眼缘,3=购买会员
  21. $type = $this->request->request("type","wechat");// 充值类型:wechat:微信支付,alipay:支付宝支付
  22. $method = "app";
  23. if(!in_array($purpose,[1,2,3]) || !in_array($type,['wechat','alipay'])) {
  24. $this->error(__('Invalid parameters'));
  25. }
  26. if($type == 'wechat') $this->error(__('微信支付暂未开通!请选择支付宝支付!'));
  27. $user_id = $this->auth->id;
  28. $fate_user_id = 0;
  29. $vip_config_id = 0;
  30. switch ($purpose) {
  31. case 1:
  32. $title = "真人认证支付";
  33. $amount = config("site.auth");
  34. break;
  35. case 2:
  36. $fate_user_id = $this->request->request("fate_user_id",0); // 有眼缘用户ID
  37. if(!$fate_user_id) {
  38. $this->error(__('Invalid parameters'));
  39. }
  40. $title = "有眼缘支付";
  41. $amount = config("site.fate");
  42. break;
  43. case 3:
  44. $vip_config_id = $this->request->request("vip_config_id",0);
  45. if(!$vip_config_id) {
  46. $this->error(__('Invalid parameters'));
  47. }
  48. $title = "会员开通支付";
  49. // 获取会员配置信息
  50. if($vip_config_id <= 0) $this->error(__('Invalid parameters'));
  51. $vipConfigInfo = \app\admin\model\vip\Config::where(['id'=>$vip_config_id])->find();
  52. $amount = $vipConfigInfo['real_price'];
  53. break;
  54. }
  55. $out_trade_no = date("YmdHis").rand(100000,999999);
  56. $notifyurl = request()->root(true) . '/api/notify/notify/paytype/' . $type;
  57. $returnurl = request()->root(true) . '/addons/epay/api/returnx/type/' . $type;
  58. if (!$amount || $amount < 0) {
  59. $this->error("支付金额必须大于0");
  60. }
  61. if (!$type || !in_array($type, ['alipay', 'wechat'])) {
  62. $this->error("支付类型错误");
  63. }
  64. $time = time();
  65. $money = $type == 'alipay' ? (float)bcadd($amount,0,2) : (float)bcmul($amount,100,2);
  66. // 生成订单
  67. $recharOrderMode = new \app\common\model\RecharOrder();
  68. $orderid = $recharOrderMode->execute("INSERT INTO `hx_rechar_order` (`user_id` , `order_no` , `vip_config_id`, `fate_user_id`, `money` ,`purpose`, `pay_type`, `createtime`) VALUES ($user_id , '$out_trade_no' , $vip_config_id, $fate_user_id, $money ,$purpose, '$type', $time)");
  69. if(!$orderid) $this->error("订单创建失败!");
  70. $params = [
  71. 'type' => $type,
  72. 'orderid' => $out_trade_no,
  73. 'title' => $title,
  74. 'amount' => $money,
  75. 'method' => $method,
  76. 'notifyurl' => $notifyurl,
  77. 'returnurl' => $returnurl,
  78. ];
  79. $result = Service::submitOrder($params);
  80. $this->success("参数获取成功!",$result);
  81. }
  82. /**
  83. * 提现
  84. */
  85. public function withdrow() {
  86. $money = $this->request->request("money");// 申请提现的金额
  87. $withdrawMax = config('site.withdrawMax'); // 最高提现金额
  88. $withdrawCount = config('site.withdrawCount'); // 每天提现次数
  89. $withdrawallogModel = new \app\common\model\UserWithdrawLog();
  90. if($money <= 0 || $money > $withdrawMax) {
  91. $this->error(__('提现金额范围:10-'.$withdrawMax));
  92. }
  93. if(intval($money) != $money) {
  94. $this->error(__('提现金额必须整数!'));
  95. }
  96. if($money%10 !== 0) {
  97. $this->error(__('提现金额必须为10的倍数!'));
  98. }
  99. // 获取今天的提现次数
  100. $starttime = strtotime(date("Y-m-d 00:00:00"));
  101. $endtime = strtotime(date("Y-m-d 23:59:59"));
  102. $where = [];
  103. $where["user_id"] = $this->auth->id;
  104. $where["status"] = ['in',[0,1]]; // 提现状态:-1=审核拒绝,0=待审核,1=审核通过
  105. $where["createtime"] = ["between","$starttime,$endtime"];
  106. $withdrawCountInfo = $withdrawallogModel->where($where)->count('id');
  107. if($withdrawCountInfo >= $withdrawCount) {
  108. $this->error(__('今日提现次数已达上线!'));
  109. }
  110. // 判断当前用户是否实名认证
  111. $userAuthInfo = \app\common\model\UserAuth::userIsAuth($this->auth->id);
  112. if($userAuthInfo['status'] == 0) $this->error($userAuthInfo['msg']);
  113. // 查询资金余额
  114. $userModel = new \app\common\model\User();
  115. $userInfo = $userModel->get($this->auth->id);
  116. if($money > $userInfo["money"]) {
  117. $this->error("资金余额不足!");
  118. }
  119. Db::startTrans();
  120. try{
  121. // 减去用户可用资金余额
  122. $res1 = $userModel->where(["id"=>$this->auth->id])->setDec("money",$money);
  123. // 增加用户冻结资金余额
  124. $res2 = $userModel->where(["id"=>$this->auth->id])->setInc("frozen",$money);
  125. // 新增用户提现记录申请
  126. $data = [];
  127. $data["user_id"] = $this->auth->id;
  128. $data["money"] = $money;
  129. $data["status"] = 0; // 提现状态:-1=审核拒绝,0=待审核,1=审核通过
  130. $data["createtime"] = time();
  131. $res3 = $withdrawallogModel->insert($data);
  132. if($res1 && $res2 && $res3) {
  133. Db::commit();
  134. $this->success("提现申请发送成功!");
  135. } else {
  136. $this->error("操作失败!");
  137. }
  138. }catch (ValidateException $e) {
  139. Db::rollback();
  140. $this->error($e->getMessage());
  141. } catch (PDOException $e) {
  142. Db::rollback();
  143. $this->error($e->getMessage());
  144. } catch (Exception $e) {
  145. Db::rollback();
  146. $this->error($e->getMessage());
  147. }
  148. }
  149. /**
  150. * 获取用户提现记录
  151. */
  152. public function withdrowRecord() {
  153. $page = $this->request->request('page',1); // 分页
  154. $pageNum = $this->request->request('pageNum',10); // 分页
  155. // 分页搜索构建
  156. $pageStart = ($page-1)*$pageNum;
  157. $res = \app\common\model\UserWithdrawLog::where(["user_id"=>$this->auth->id])->limit($pageStart,$pageNum)->order("createtime","desc")->select();
  158. if($res) {
  159. $statusTxt = array("-1"=>"审核拒绝","0"=>"待审核","1"=>"审核通过");
  160. foreach($res as $k => &$v) {
  161. $v["status"] = $statusTxt[$v["status"]];
  162. $v["money"] = $v["money"]>0?$v["money"]:0;
  163. $v["createtime"] = date("Y-m-d H:i:s",$v["createtime"]);
  164. }
  165. }
  166. $this->success("获取成功!",$res);
  167. }
  168. /**
  169. * 获取收入明细
  170. */
  171. public function getRebateList() {
  172. $page = $this->request->request('page',1); // 分页
  173. $pageNum = $this->request->request('pageNum',10); // 分页
  174. // 分页搜索构建
  175. $pageStart = ($page-1)*$pageNum;
  176. $res = [];
  177. $res['money_count']= \app\common\model\User::where(["id"=>$this->auth->id])->value("money");
  178. $where = [];
  179. $where["user_id"] = $this->auth->id;
  180. $res['money_log'] = \app\common\model\UserProfitLog::where($where)->limit($pageStart,$pageNum)->order("createtime","desc")->select();
  181. if($res['money_log']) foreach($res['money_log'] as $k => &$v) {
  182. $v["createtime"] = date("Y-m-d H:i:s", $v["createtime"]);
  183. }
  184. $this->success("获取成功!",$res);
  185. }
  186. }