Money.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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=购买会员, 4=充值钻石
  21. $type = $this->request->request("type","wechat");// 充值类型:wechat:微信支付,alipay:支付宝支付
  22. $method = "app";
  23. if(!in_array($purpose,[1,2,3,4]) || !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. if (!$vipConfigInfo) {
  53. $this->error('网络延迟,请稍后再试');
  54. }
  55. $amount = $vipConfigInfo['real_price'];
  56. break;
  57. case 4:
  58. $vip_config_id = $this->request->request("vip_config_id",0);
  59. if(!$vip_config_id) {
  60. $this->error(__('Invalid parameters'));
  61. }
  62. $title = "钻石充值支付";
  63. // 获取会员配置信息
  64. if($vip_config_id <= 0) $this->error(__('Invalid parameters'));
  65. $vipConfigInfo = Db::name('diamond')->where(['id'=>$vip_config_id])->find();
  66. if (!$vipConfigInfo) {
  67. $this->error('网络延迟,请稍后再试');
  68. }
  69. $amount = $vipConfigInfo['price'];
  70. break;
  71. }
  72. $out_trade_no = date("YmdHis").rand(100000,999999);
  73. $notifyurl = request()->root(true) . '/api/notify/notify/paytype/' . $type;
  74. $returnurl = request()->root(true) . '/addons/epay/api/returnx/type/' . $type;
  75. if (!$amount || $amount < 0) {
  76. $this->error("支付金额必须大于0");
  77. }
  78. if (!$type || !in_array($type, ['alipay', 'wechat'])) {
  79. $this->error("支付类型错误");
  80. }
  81. $time = time();
  82. $money = $type == 'alipay' ? (float)bcadd($amount,0,2) : (float)bcadd($amount,0,2);
  83. // 生成订单
  84. $recharOrderMode = new \app\common\model\RecharOrder();
  85. $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)");
  86. if(!$orderid) $this->error("订单创建失败!");
  87. $params = [
  88. 'type' => $type,
  89. 'orderid' => $out_trade_no,
  90. 'title' => $title,
  91. 'amount' => $money,
  92. 'method' => $method,
  93. 'notifyurl' => $notifyurl,
  94. 'returnurl' => $returnurl,
  95. ];
  96. $result = Service::submitOrder($params);
  97. $this->success("参数获取成功!",$result);
  98. }
  99. /**
  100. * 提现
  101. */
  102. public function withdrow() {
  103. $money = $this->request->request("money");// 申请提现的金额
  104. $withdrawMax = config('site.withdrawMax'); // 最高提现金额
  105. $withdrawCount = config('site.withdrawCount'); // 每天提现次数
  106. $withdrawallogModel = new \app\common\model\UserWithdrawLog();
  107. if($money <= 0 || $money > $withdrawMax) {
  108. $this->error(__('提现金额范围:10-'.$withdrawMax));
  109. }
  110. if(intval($money) != $money) {
  111. $this->error(__('提现金额必须整数!'));
  112. }
  113. if($money%10 !== 0) {
  114. $this->error(__('提现金额必须为10的倍数!'));
  115. }
  116. // 获取今天的提现次数
  117. $starttime = strtotime(date("Y-m-d 00:00:00"));
  118. $endtime = strtotime(date("Y-m-d 23:59:59"));
  119. $where = [];
  120. $where["user_id"] = $this->auth->id;
  121. $where["status"] = ['in',[0,1]]; // 提现状态:-1=审核拒绝,0=待审核,1=审核通过
  122. $where["createtime"] = ["between","$starttime,$endtime"];
  123. $withdrawCountInfo = $withdrawallogModel->where($where)->count('id');
  124. if($withdrawCountInfo >= $withdrawCount) {
  125. $this->error(__('今日提现次数已达上线!'));
  126. }
  127. // 判断当前用户是否实名认证
  128. $userAuthInfo = \app\common\model\UserAuth::userIsAuth($this->auth->id);
  129. if($userAuthInfo['status'] == 0) $this->error($userAuthInfo['msg']);
  130. // 查询资金余额
  131. $userModel = new \app\common\model\User();
  132. $userInfo = $userModel->get($this->auth->id);
  133. if($money > $userInfo["money"]) {
  134. $this->error("资金余额不足!");
  135. }
  136. Db::startTrans();
  137. try{
  138. // 减去用户可用资金余额
  139. $res1 = $userModel->where(["id"=>$this->auth->id])->setDec("money",$money);
  140. // 增加用户冻结资金余额
  141. $res2 = $userModel->where(["id"=>$this->auth->id])->setInc("frozen",$money);
  142. // 新增用户提现记录申请
  143. $data = [];
  144. $data["user_id"] = $this->auth->id;
  145. $data["money"] = $money;
  146. $data["status"] = 0; // 提现状态:-1=审核拒绝,0=待审核,1=审核通过
  147. $data["createtime"] = time();
  148. $res3 = $withdrawallogModel->insert($data);
  149. if($res1 && $res2 && $res3) {
  150. Db::commit();
  151. $this->success("提现申请发送成功!");
  152. } else {
  153. $this->error("操作失败!");
  154. }
  155. }catch (ValidateException $e) {
  156. Db::rollback();
  157. $this->error($e->getMessage());
  158. } catch (PDOException $e) {
  159. Db::rollback();
  160. $this->error($e->getMessage());
  161. } catch (Exception $e) {
  162. Db::rollback();
  163. $this->error($e->getMessage());
  164. }
  165. }
  166. /**
  167. * 获取用户提现记录
  168. */
  169. public function withdrowRecord() {
  170. $page = $this->request->request('page',1); // 分页
  171. $pageNum = $this->request->request('pageNum',10); // 分页
  172. // 分页搜索构建
  173. $pageStart = ($page-1)*$pageNum;
  174. $res = \app\common\model\UserWithdrawLog::where(["user_id"=>$this->auth->id])->limit($pageStart,$pageNum)->order("createtime","desc")->select();
  175. if($res) {
  176. $statusTxt = array("-1"=>"审核拒绝","0"=>"待审核","1"=>"审核通过");
  177. foreach($res as $k => &$v) {
  178. $v["status"] = $statusTxt[$v["status"]];
  179. $v["money"] = $v["money"]>0?$v["money"]:0;
  180. $v["createtime"] = date("Y-m-d H:i:s",$v["createtime"]);
  181. }
  182. }
  183. $this->success("获取成功!",$res);
  184. }
  185. /**
  186. * 获取收入明细
  187. */
  188. public function getRebateList() {
  189. $page = $this->request->request('page',1); // 分页
  190. $pageNum = $this->request->request('pageNum',10); // 分页
  191. // 分页搜索构建
  192. $pageStart = ($page-1)*$pageNum;
  193. $res = [];
  194. $res['money_count']= \app\common\model\User::where(["id"=>$this->auth->id])->value("money");
  195. $where = [];
  196. $where["user_id"] = $this->auth->id;
  197. $res['money_log'] = \app\common\model\UserProfitLog::where($where)->limit($pageStart,$pageNum)->order("createtime","desc")->select();
  198. if($res['money_log']) foreach($res['money_log'] as $k => &$v) {
  199. $v["createtime"] = date("Y-m-d H:i:s", $v["createtime"]);
  200. }
  201. $this->success("获取成功!",$res);
  202. }
  203. //获取钻石明细
  204. public function getdiamondlog() {
  205. $page = $this->request->request('page', 1, 'intval'); // 分页
  206. $pageNum = $this->request->request('pageNum', 10, 'intval'); // 分页
  207. $list = Db::name('user_diamond_log')->where(['user_id' => $this->auth->id])->order('id', 'desc')->page($page, $pageNum)->select();
  208. if ($list) {
  209. foreach ($list as &$v) {
  210. $v['createtime'] = date("Y-m-d H:i:s", $v["createtime"]);
  211. }
  212. }
  213. $this->success('获取钻石明细', $list);
  214. }
  215. }