Money.php 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624
  1. <?php
  2. namespace app\api\controller;
  3. use addons\epay\library\Service;
  4. use app\api\controller\Common;
  5. use app\common\model\GiftUserParty;
  6. use app\common\model\UserExchangeLog;
  7. use app\common\model\UserExchsoundLog;
  8. use applebuy\applebuy;
  9. use fast\Random;
  10. use app\common\model\RecharOrder;
  11. use kjpay\kjpay;
  12. use kjwithdraw\kjwithdraw;
  13. use think\Request;
  14. use think\Db;
  15. use app\common\library\Sms as Smslib;
  16. /**
  17. * 钱包接口
  18. */
  19. class Money extends Common
  20. {
  21. protected $noNeedLogin = ['getRecharConfig', 'getRateMoney', 'getExchangeConfig', 'getExchsoundConfig', 'rebateDuty', 'kjRecharge', 'changeWithDrawStatus'];
  22. protected $noNeedRight = '*';
  23. public function _initialize()
  24. {
  25. parent::_initialize();
  26. }
  27. /**
  28. * 获取用户财富信息
  29. */
  30. public function getUserMoney()
  31. {
  32. $this->success("获取成功!", \app\common\model\User::field("money,rebate_money,score,jewel,sound_coin,hammer")->find($this->auth->id));
  33. }
  34. /**
  35. * 充值
  36. */
  37. public function recharge()
  38. {
  39. $rechar_id = $this->request->request("rechar_id");// 充值金额配置ID
  40. $type = $this->request->request("type", "wechat");// 充值类型:wechat:微信支付,alipay:支付宝支付
  41. $method = $this->request->request("method", "miniapp");// 支付方式:app:app支付,miniapp:小程序支付
  42. $openid = $this->request->request("openid", "");// 非小程序支付无需传值
  43. $params_from = $this->request->request("params_from");// 平台参数
  44. if (!$rechar_id) {
  45. $this->error(__('Invalid parameters'));
  46. }
  47. $user_id = $this->auth->id;
  48. // 获取充值金额配置信息
  49. $recharMode = new \app\common\model\RecharConfig();
  50. $recharInfo = $recharMode->where(["id" => $rechar_id])->find();
  51. if (!$recharInfo) $this->error("金额配置信息未找到!");
  52. $out_trade_no = date("YmdHis") . rand(100000, 999999);
  53. $title = "充值钻石";
  54. $amount = $recharInfo["money"] / 100;
  55. if ($params_from == "ios") {
  56. $jewel = $recharInfo['jewel'];
  57. } else {
  58. $jewel = $recharInfo['jewel_full'];
  59. }
  60. $notifyurl = request()->root(true) . '/api/notify/' . $type . 'notify';
  61. $returnurl = request()->root(true) . '/addons/epay/index/' . $type . 'return/out_trade_no/' . $out_trade_no;
  62. if (!$amount || $amount < 0) {
  63. $this->error("支付金额必须大于0");
  64. }
  65. if (!$type || !in_array($type, ['alipay', 'wechat'])) {
  66. $this->error("支付类型错误");
  67. }
  68. $time = time();
  69. $money = (float)bcadd($amount, 0, 2);
  70. // 生成订单
  71. $recharOrderMode = new \app\common\model\RecharOrder();
  72. // $data = [];
  73. // $data["user_id"] = $user_id;
  74. // $data["order_no"] = $out_trade_no;
  75. // $data["money"] = $money;
  76. // $data["jewel"] = $recharInfo["jewel"];
  77. // $data["createtime"] = time();
  78. $orderid = $recharOrderMode->execute("INSERT INTO `hx_rechar_order` (`user_id` , `order_no` , `money` , `jewel` , `createtime`) VALUES ($user_id , $out_trade_no , $money , " . $jewel . " , $time)");
  79. if (!$orderid) $this->error("订单创建失败!");
  80. $params = [
  81. 'type' => $type,
  82. 'orderid' => $out_trade_no,
  83. 'title' => $title,
  84. 'amount' => $money,
  85. 'method' => $method,
  86. 'openid' => $openid,
  87. 'notifyurl' => $notifyurl,
  88. 'returnurl' => $returnurl,
  89. ];
  90. $result = Service::submitOrder($params);
  91. // 首充赠送
  92. $userInfo = \app\common\model\User::get($user_id);
  93. if ($userInfo->is_recharge == 0) { // 首充
  94. Db::startTrans();
  95. try {
  96. // 赠送消息尾灯
  97. $res1 = \app\common\model\AttireBack::addToMyBack(15, $user_id);
  98. $userInfo->is_recharge = 1;
  99. $res2 = $userInfo->save();
  100. if ($res1 && $res2) Db::commit();
  101. } catch (ValidateException $e) {
  102. Db::rollback();
  103. $this->error($e->getMessage());
  104. }
  105. }
  106. $this->rebate($user_id, $amount);
  107. // +EXP
  108. \app\common\model\TaskLog::tofinish($user_id, "5EMwg7la", 1);
  109. // 查询今日充值记录
  110. $today = strtotime(date("Y-m-d 00:00:00"));
  111. $rechargeMoney = \app\common\model\RecharOrder::where(["user_id" => $user_id, "createtime" => ["gt", $today]])->sum("money");
  112. if ($rechargeMoney >= 100) {
  113. // +EXP
  114. \app\common\model\TaskLog::tofinish($user_id, "DaCVgOLu", 1);
  115. }
  116. // 查询新人礼包
  117. $this->addBagGift($user_id, $amount);
  118. if ($method == "app") {
  119. $this->success("", json_decode($result, true));
  120. } else {
  121. $this->success("", $result);
  122. }
  123. }
  124. /**
  125. * 聚合支付充值
  126. */
  127. public function kjRecharge()
  128. {
  129. $rechar_id = $this->request->request("rechar_id");// 充值金额配置ID
  130. $u_id = $this->request->request("u_id");// 用户ID
  131. $type = $this->request->request("type", "wechat");// 充值类型:wechat:微信支付,alipay:支付宝支付
  132. $method = $this->request->request("method", "miniapp");// 支付方式:app:app支付,miniapp:小程序支付,public:公众号支付
  133. $openid = $this->request->request("openid", "");// 非小程序支付无需传值
  134. $params_from = $this->request->request("params_from");// 平台参数
  135. $custom_money = $this->request->request("custom_money", 0, "intval");// 自定义充值金额
  136. if (!$rechar_id && !$custom_money) {
  137. $this->error(__('Invalid parameters'));
  138. }
  139. if ($custom_money > 0) {
  140. $amount = $custom_money;
  141. $jewel = $custom_money * 100;
  142. if ($amount < 10) $this->error('支付金额必须大于10');
  143. if ($amount > 30000) $this->error('支付金额超出单笔交易限额');
  144. } else {
  145. // 获取充值金额配置信息
  146. $recharMode = new \app\common\model\RecharConfig();
  147. $recharInfo = $recharMode->where(["id" => $rechar_id])->find();
  148. if (!$recharInfo) $this->error("金额配置信息未找到!");
  149. $amount = $recharInfo["money"] / 100;
  150. if (!$amount || $amount < 0) {
  151. $this->error("支付金额必须大于0");
  152. }
  153. if ($params_from == "ios") {
  154. $jewel = $recharInfo['jewel'];
  155. } else {
  156. $jewel = $recharInfo['jewel_full'];
  157. }
  158. }
  159. $platformArr = ["android" => 1, "ios" => 2, "wxweb" => 3];
  160. $platform = isset($platformArr[$params_from]) ? $platformArr[$params_from] : 1;
  161. if ($method == "public") {
  162. if (!$u_id) $this->error(__('Invalid parameters'));
  163. $user_id = \app\common\model\User::where(["u_id" => $u_id])->value("id");
  164. } else {
  165. $user_id = $this->auth->id;
  166. }
  167. if (!$user_id) $this->error("数据缺失,请重新登录!");
  168. $out_trade_no = date("YmdHis") . rand(100000, 999999);
  169. $title = "充值钻石";
  170. $content = "充值钻石";
  171. $request = Request::instance();
  172. $user_ip = $request->ip();
  173. $notifyurl = request()->root(true) . '/api/notify/wechatkjnotify';
  174. $returnurl = "https://voicew.bansheng-live.com/pages/index/index";
  175. $time = time();
  176. $money = (float)bcadd($amount, 0, 2);
  177. // 生成订单
  178. $recharOrderMode = new \app\common\model\RecharOrder();
  179. $orderid = $recharOrderMode->execute("INSERT INTO `hx_rechar_order` (`user_id` , `order_no` , `money` , `platform`, `jewel` , `createtime`) VALUES ($user_id , $out_trade_no , $money , $platform, " . $jewel . " , $time)");
  180. if (!$orderid) $this->error("订单创建失败!");
  181. // 获取公共参数
  182. $config = config("kjPay");
  183. //商户号
  184. $merchant_no = $config["merchant_no"];
  185. //密钥
  186. $key = $config["key"];
  187. // 实例化支付类
  188. $kjpay = new kjpay();
  189. // 公众号分账参数
  190. $publicreceivers = [];
  191. $publicamount = $money - $money * 0.005;
  192. $publicreceivers[] = [
  193. "member_id" => $config["receivers_member_id"],
  194. "amount" => floor($publicamount * 100) / 100,
  195. ];
  196. // app分账参数
  197. $appreceivers = [];
  198. $appamount = $money * (1 - 0.006) * 0.3 - $money * 0.002;
  199. // $appreceivers[] = [
  200. // "type" => "PERSONAL_OPENID",
  201. // "account" => "oSepO0jXj8CLlEsb9v4Gw9sbzQqU",
  202. // "name" => "石国华",
  203. // "amount" => floor($appamount*100)/100,
  204. // "description" => "分账",
  205. // ];
  206. $appreceivers[] = [
  207. "type" => "PERSONAL_OPENID",
  208. "account" => "oSepO0nhtkSxiCoAhZFlnydDEzoA",
  209. "name" => "石国华",
  210. "amount" => floor($appamount * 100) / 100,
  211. "description" => "分账",
  212. ];
  213. //请求的参数(注:不同支付接口请求参数略有不同,具体请参照接口文档)
  214. $param = array(
  215. 'merchant_no' => $merchant_no,
  216. 'merchant_order_no' => $out_trade_no,
  217. //异步通知的地址
  218. 'notify_url' => $notifyurl,
  219. 'start_time' => date('YmdHis'),
  220. 'trade_amount' => $money,
  221. 'goods_name' => $title,
  222. 'goods_desc' => $content,
  223. 'user_ip' => $user_ip,
  224. 'sign_type' => 1
  225. );
  226. if ($method == "public") {
  227. $param['open_id'] = $openid;
  228. //同步跳转的地址
  229. $param['return_url'] = $returnurl;
  230. }
  231. if ($method == "public" || $method == "miniapp") {
  232. $param['receivers'] = json_encode($publicreceivers);
  233. }
  234. if ($method == "app") {
  235. if ($type == "alipay") {
  236. // $param['receivers'] = json_encode($publicreceivers);
  237. } else {
  238. $param['receivers'] = json_encode($appreceivers);
  239. }
  240. }
  241. //签名
  242. $sign = $kjpay->local_sign($param, $key);
  243. $param['sign'] = $sign;
  244. $typeArr = ["wechat" => "wechar", "alipay" => "alipay"];
  245. if ($type == "wechat" && $method == "app") {
  246. $method = "app_wechat";
  247. } elseif ($type == "alipay" && $method == "app") {
  248. $method = "app_alipay";
  249. }
  250. $res = $kjpay->getdata("http://" . $config["url"] . "/" . $typeArr[$type] . "/" . $config["pay_params"][$method], $param);
  251. $list = json_decode($res, true);
  252. if ($list["status"] != 1) $this->error($list['data']['error']);
  253. //对响应结果进行验签,
  254. if ($kjpay->local_sign($list['data'], $key) == $list['data']['sign']) {
  255. $this->success("支付验证通过!", $list["data"]);
  256. } else {
  257. $this->error("支付验证失败!");
  258. }
  259. }
  260. /**
  261. * 聚合支付提现
  262. */
  263. public function kjout()
  264. {
  265. $this->error(__('功能暂时关闭!'));
  266. // 接口防并发
  267. if (!$this->apiLimit(1, 2000)) {
  268. $this->error(__('Operation frequently'));
  269. }
  270. // 获取用户信息
  271. $user_id = $this->auth->id;
  272. $money = $this->request->request("money");// 申请提现的金额
  273. if ($money < 30) {
  274. $this->error(__('提现金额至少30元'));
  275. }
  276. if (bcsub($money, intval($money), 2) > 0) {
  277. $this->error(__('提现金额必须为整数'));
  278. }
  279. // if (!$this->withdrawVerify($user_id)) {
  280. // $this->error(__('账户验证失败!请联系管理员!'));
  281. // }
  282. // 判断当前用户是否实名认证
  283. $userAuthInfo = \app\common\model\UserAuth::where(["user_id" => $user_id])->find();
  284. if ($userAuthInfo) {
  285. if ($userAuthInfo->status == 0) {
  286. $this->error("您的实名认证还在这审核中...,请耐心等待!");
  287. } elseif ($userAuthInfo->status == 2) {
  288. $this->error("您的实名认证审核未通过,请重新审核!");
  289. }
  290. } else {
  291. $this->error("请先申请实名认证!");
  292. }
  293. // 查询资金余额
  294. $userModel = new \app\common\model\User();
  295. $userInfo = $userModel->where('id',$user_id)->lock(true)->find();
  296. if (bcsub($money, $userInfo["money"], 2) > 0) {
  297. $this->error("资金余额不足!", [], 100);
  298. }
  299. // 增加提现收益记录
  300. $withdrawRate = (100 - config("site.withdrawRate")) / 100;
  301. $promoney = bcmul($money, $withdrawRate, 2);
  302. $redmoney = bcsub($money, $promoney, 2);
  303. // 查询银行卡信息
  304. $bankInfo = \app\common\model\UserBank::where(["user_id" => $user_id])->find();
  305. if (!$bankInfo) $this->error("请先绑定银行卡!");
  306. $bankno = $bankInfo->bank_no;
  307. $bankname = $bankInfo->bank_name;
  308. $username = $bankInfo->realname;
  309. $mobile = $bankInfo->mobile;
  310. // 获取公共参数
  311. $config = config("kjPay");
  312. //商户号
  313. $merchant_no = $config["merchant_no"];
  314. //密钥
  315. $key = $config["key"];
  316. $out_trade_no = date("YmdHis") . rand(100000, 999999);
  317. // 实例化支付类
  318. $kjwithdraw = new kjwithdraw();
  319. //请求的参数(注:不同支付接口请求参数略有不同,具体请参照接口文档)
  320. $param = array(
  321. 'merchant_no' => $merchant_no, // 商户号
  322. 'merchant_order_no' => $out_trade_no,// 商户订单号
  323. 'payment_days' => date("Y-m-d", strtotime("-1 day")), // 账期
  324. // 'payment_days' => "2021-04-19", // 账期
  325. 'pay_channel' => "ali_wap_pay", // 支付通道
  326. 'trade_amount' => $redmoney, // 提现金额 单位为元,精确到小数点后两位,取值范围[0.01,100000000]
  327. 'bank_no' => $bankno, // 银行卡号
  328. 'bank_acct_name' => $username, // 银行账户名称
  329. 'bank_mobile' => $mobile, // 银行预留手机号
  330. 'account_attr' => "0", // 账户类型 0-对私,默认0
  331. 'bank_name' => $bankname, //银行名称
  332. 'remarks' => "伴声公众号提现", // 提现备注
  333. 'sign_type' => 1
  334. );
  335. //签名
  336. $sign = $kjwithdraw->sign($param, $key);
  337. $param['sign'] = $sign;
  338. $res = $kjwithdraw->getdata($config["cashOutForSelf"]["url"], $param);
  339. // $this->error($res);exit;
  340. $list = json_decode($res, true);
  341. // 提现申请成功 添加申请记录
  342. if (isset($list["status"])) {
  343. if ($list["status"] == 1) {
  344. Db::startTrans();
  345. try {
  346. $withdrawallogModel = new \app\common\model\UserWithdrawalLog();
  347. // 减去用户可用资金余额
  348. $res1 = $userModel->where(["id" => $user_id])->setDec("money", $money);
  349. // 增加用户冻结资金余额
  350. $res2 = $userModel->where(["id" => $user_id])->setInc("frozen", $money);
  351. // 新增用户提现记录申请
  352. $data = [];
  353. $data["user_id"] = $user_id;
  354. $data["money"] = $money * 100;
  355. $data["out_trade_no"] = $out_trade_no;
  356. $data["status"] = 0; // 提现状态:-1=审核拒绝,0=待审核,1=审核通过
  357. $data["createtime"] = time();
  358. $res3 = $withdrawallogModel->insert($data);
  359. if ($res1 && $res2 && $res3) {
  360. Db::commit();
  361. $this->success("提现申请发送成功!");
  362. } else {
  363. $this->error("操作失败!");
  364. }
  365. } catch (ValidateException $e) {
  366. Db::rollback();
  367. $this->error($e->getMessage());
  368. } catch (PDOException $e) {
  369. Db::rollback();
  370. $this->error($e->getMessage());
  371. } catch (Exception $e) {
  372. Db::rollback();
  373. $this->error($e->getMessage());
  374. }
  375. $this->success("恭喜,提现成功!请留意预留银行账户资金变动!");
  376. } else {
  377. $this->error("商户账户异常,请联系管理员!");
  378. }
  379. } else {
  380. $this->error("提现失败,请联系客服处理!");
  381. }
  382. }
  383. /**
  384. * 获取用户比例提现金额
  385. */
  386. public function getRateMoney()
  387. {
  388. $money = $this->request->request("money");// 申请提现的金额
  389. if ($money <= 0) {
  390. $this->error(__('提现金额不合法'));
  391. }
  392. // 增加提现收益记录
  393. $money = bcsub($money, 0, 2);
  394. $rate = 100 - config("site.withdrawRate");
  395. $withdrawRate = $rate / 100;
  396. $promoney = bcmul($money, $withdrawRate, 2);
  397. $redmoney = bcsub($money, $promoney, 2);
  398. $res = [];
  399. $res["rate"] = $rate . "%";
  400. $res["money"] = $money;
  401. $res["promoney"] = $promoney;
  402. $res["redmoney"] = $redmoney;
  403. $this->success("获取成功!", $res);
  404. }
  405. public function aa()
  406. {
  407. $this->addBagGift(86, 100);
  408. }
  409. /**
  410. * 更新新人礼包
  411. */
  412. private function addBagGift($user_id, $amount)
  413. {
  414. if (!$user_id || !$amount) return false;
  415. // 先看该用户是否有新人礼包特权
  416. $have = \app\common\model\NewBagHave::where(["user_id" => $user_id])->find();
  417. // print_r($have);exit;
  418. if ($have && time() - $have->createtime <= 7 * 86400) {
  419. $where = [];
  420. $where["value"] = ["elt", $amount];
  421. $bagInfo = \app\common\model\NewBag::where($where)->order("value", "desc")->select();
  422. if (!$bagInfo) return false;
  423. $res1 = false;
  424. foreach ($bagInfo as $k => $v) {
  425. // 查询完成情况
  426. $bagfinish = \app\common\model\NewBagFinish::where(["user_id" => $user_id, "bag_id" => $v["id"]])->find();
  427. if (!$bagfinish || $bagfinish->status <= 0) {
  428. if ($bagfinish) {
  429. $bagfinish->status = 1;
  430. $bagfinish->updatetime = time();
  431. $res1 = $bagfinish->save();
  432. } else {
  433. $res1 = \app\common\model\NewBagFinish::insert(["user_id" => $user_id, "bag_id" => $v["id"], "status" => 1, "updatetime" => time()]);
  434. }
  435. break;
  436. }
  437. }
  438. return $res1;
  439. }
  440. }
  441. /**
  442. * 更新提现状态 定时任务
  443. */
  444. public function changeWithDrawStatus()
  445. {
  446. // 获取公共参数
  447. $config = config("kjPay");
  448. //商户号
  449. $merchant_no = $config["merchant_no"];
  450. //密钥
  451. $key = $config["key"];
  452. // 查询 有哪些 提现未审核的提现记录
  453. $log = \app\common\model\UserWithdrawalLog::where(["status" => 0])->order("doit", "asc")->find();
  454. if (!$log) exit;
  455. $log->doit = $log->doit + 1;
  456. $log->save();
  457. // 实例化支付类
  458. $kjwithdraw = new kjwithdraw();
  459. //请求的参数(注:不同支付接口请求参数略有不同,具体请参照接口文档)
  460. $param = array(
  461. 'merchant_no' => $merchant_no, // 商户号
  462. 'merchant_order_no' => $log->out_trade_no,// 商户订单号
  463. 'sign_type' => 1
  464. );
  465. $money = $log->money / 100;
  466. // 增加提现收益记录
  467. $withdrawRate = (100 - config("site.withdrawRate")) / 100;
  468. $promoney = bcmul($money, $withdrawRate, 2) - 1;
  469. // $redmoney = bcsub($money,$promoney,2);
  470. // 查询资金余额
  471. $userModel = new \app\common\model\User();
  472. $userInfo = $userModel->get($log->user_id);
  473. if ($money > $userInfo["frozen"]) {
  474. exit;
  475. }
  476. //签名
  477. $sign = $kjwithdraw->sign($param, $key);
  478. $param['sign'] = $sign;
  479. $res = $kjwithdraw->getdata($config["cashOutForSelf"]["check_url"], $param);
  480. $list = json_decode($res, true);
  481. // 查询成功
  482. if (isset($list["status"])) {
  483. if ($list["status"] == 1) {
  484. Db::startTrans();
  485. try {
  486. $userModel = new \app\common\model\User();
  487. $userInfo = $userModel->where(["id" => $log->user_id])->find();
  488. if ($list["data"]["status"] == 4) {// 更新状态为审核通过
  489. // 减去用户冻结资金余额
  490. $res1 = (new \app\common\model\User())->execute("update hx_user set frozen = frozen - $money where id = " . $log->user_id);
  491. $time = time();
  492. $res2 = (new \app\common\model\UserWithdrawalProfitLog())->execute("INSERT INTO `hx_user_withdrawal_profit_log`(`u_id`, `nickname`, `plat_value`, `createtime`) VALUES ($userInfo->u_id, '$userInfo->nickname', $promoney, $time)");
  493. // 更改用户提现记录状态
  494. $res3 = \app\common\model\UserWithdrawalLog::update(["status" => 1, "updatetime" => time()], ["out_trade_no" => $log->out_trade_no]);
  495. if ($res1 && $res2 && $res3) {
  496. Smslib::send($userInfo->mobile, $money, "drowsuccess");
  497. Db::commit();
  498. }
  499. } elseif ($list["data"]["status"] == 5) { // 更新状态为审核拒绝
  500. // 增加用户资金余额
  501. $res1 = (new \app\common\model\User())->execute("update hx_user set money = money + $money where id = " . $log->user_id);
  502. // 减去用户冻结资金余额
  503. $res2 = (new \app\common\model\User())->execute("update hx_user set frozen = frozen - $money where id = " . $log->user_id);
  504. // 更改用户提现记录状态
  505. $res3 = \app\common\model\UserWithdrawalLog::update(["status" => -1, "updatetime" => time()], ["out_trade_no" => $log->out_trade_no]);
  506. if ($res1 && $res2 && $res3) {
  507. Smslib::send($userInfo->mobile, $money, "drowFalse");
  508. Db::commit();
  509. }
  510. }
  511. } catch (ValidateException $e) {
  512. Db::rollback();
  513. }
  514. }
  515. }
  516. }
  517. /**
  518. * 充值返利 定时任务
  519. */
  520. public function rebateDuty()
  521. {
  522. // 获取本周所有充值记录
  523. // 上个周一
  524. $preweek = $this->firstOfWeek(date("Y-m-d H:i:s",bcsub(time(),604800)));
  525. $weekstart = strtotime($preweek." 00:00:00"); // 上周一时间
  526. $weekend = bcadd($weekstart,604800); // 上周天时间
  527. $where = [];
  528. $where["createtime"] = ["between", "$weekstart,$weekend"];
  529. $where["status"] = 1;
  530. $rechargeOrder = \app\common\model\RecharOrder::field("id,sum(money) as money,user_id")->where($where)->group("user_id")->select();
  531. $usercharge = [];
  532. // 获取上级用户的所有下级用户
  533. $preuser = [];
  534. if ($rechargeOrder) foreach ($rechargeOrder as $k => $v) {
  535. // 上级用户
  536. $pre_userid = \app\common\model\User::where(["id"=>$v["user_id"]])->value("pre_userid");
  537. if($pre_userid > 0) {
  538. // 所有下级用户
  539. $userids = \app\common\model\User::where(["pre_userid"=>$pre_userid])->column("id");
  540. $where = [];
  541. $where["createtime"] = ["between", "$weekstart,$weekend"];
  542. $where["status"] = 1;
  543. $where["user_id"] = ["in",$userids];
  544. $preuser[$v["user_id"]] = \app\common\model\RecharOrder::where($where)->sum("money");
  545. }
  546. }
  547. if ($rechargeOrder) foreach ($rechargeOrder as $k => $v) {
  548. $user_id = $v["user_id"];
  549. $money = isset($preuser[$v["user_id"]])?$preuser[$v["user_id"]]:0;
  550. // 获取配置信息
  551. $config = \app\common\model\RebateConfig::where(["recharge" => ["lt", $money]])->order("recharge", "desc")->select();
  552. if (!$config) continue;
  553. $config = $config[0];
  554. // 找到当前用户的上级和上上级
  555. $userInfo = \app\common\model\User::where(["id" => $user_id])->find();
  556. if ($userInfo->pre_userid > 0) {
  557. $usercharge[1][$user_id] = $config["preget"];
  558. $preUserInfo = \app\common\model\User::where(["id" => $userInfo->pre_userid])->find();// 上一级用户
  559. if ($preUserInfo->pre_userid > 0) {
  560. $usercharge[2][$user_id] = $config["prepreget"];
  561. }
  562. }
  563. }
  564. // 获取返利列表信息
  565. $where = [];
  566. $where["createtime"] = ["between", "$weekstart,$weekend"];
  567. $where["is_rebate"] = 0;
  568. $RebateLog = \app\common\model\RebateLog::where($where)->select();
  569. $week = intval(date('W', time()));
  570. $year = intval(date('Y', time()));
  571. $time = $year . $week;
  572. $rank = time() . rand(1000, 999);
  573. if ($RebateLog && $usercharge) foreach ($RebateLog as $k => $v) {
  574. if (isset($usercharge[$v["rebate_type"]][$v["by_user_id"]]) && $usercharge[$v["rebate_type"]][$v["by_user_id"]] > 0) {
  575. $rate = bcdiv($usercharge[$v["rebate_type"]][$v["by_user_id"]], 1000, 5);
  576. $rebatemoney = $v["money"] * $rate;
  577. (new \app\common\model\RebateLog)->execute("update hx_rebate_log set rebate_rate = $rate,is_rebate = 1, rebate_money = $rebatemoney where id = " . $v['id']);
  578. $uInfo = \app\common\model\User::where(["id" => $v["user_id"], "finish_rebate" => $time])->find();
  579. if ($uInfo) {
  580. if ($rank == $uInfo->finish_rank) {
  581. (new \app\common\model\User)->execute("update hx_user set rebate_money = rebate_money + $rebatemoney,rebatecount = rebatecount + $rebatemoney,finish_rebate = $time, finish_rank = $rank where id = " . $v["user_id"]);
  582. }
  583. } else {
  584. (new \app\common\model\User)->execute("update hx_user set rebate_money = rebate_money + $rebatemoney,rebatecount = rebatecount + $rebatemoney,finish_rebate = $time, finish_rank = $rank where id = " . $v["user_id"]);
  585. }
  586. }
  587. }
  588. }
  589. /**
  590. * 充值返利
  591. */
  592. private function rebate($user_id, $money)
  593. {
  594. // // 获取配置信息
  595. // $config = \app\common\model\RebateConfig::where(["recharge"=>$money])->find();
  596. // if(!$config) return true;
  597. // 找到当前用户的上级和上上级
  598. $userInfo = \app\common\model\User::get($user_id);
  599. if ($userInfo->pre_userid > 0) {
  600. $preUserInfo = \app\common\model\User::where(["id" => $userInfo->pre_userid])->find();// 上一级用户
  601. $this->addRebateMoney($preUserInfo->id, $user_id, $money, 1);
  602. if ($preUserInfo->pre_userid > 0) {
  603. $this->addRebateMoney($preUserInfo->pre_userid, $user_id, $money, 2);
  604. } else {
  605. return true;
  606. }
  607. } else {
  608. return true;
  609. }
  610. }
  611. /**
  612. * 添加返利金额
  613. */
  614. private function addRebateMoney($preUserId, $user_id, $money, $type)
  615. {
  616. if ($money <= 0) return false;
  617. Db::startTrans();
  618. try {
  619. // 添加返利记录表
  620. $data = [];
  621. $data["user_id"] = $user_id;
  622. $data["by_user_id"] = $preUserId;
  623. $data["money"] = $money;
  624. $data["rebate_money"] = 0.00;
  625. $data["rebate_type"] = $type;
  626. $data["createtime"] = time();
  627. $res1 = \app\common\model\RebateLog::insertGetId($data);
  628. // $res2 = (new \app\common\model\RebateLog)->execute("update hx_rebate_log set rebate_money = $rebatemoney where id = $res1");
  629. if ($res1) {
  630. Db::commit();
  631. }
  632. } catch (ValidateException $e) {
  633. Db::rollback();
  634. $this->error($e->getMessage());
  635. } catch (PDOException $e) {
  636. Db::rollback();
  637. $this->error($e->getMessage());
  638. } catch (Exception $e) {
  639. Db::rollback();
  640. $this->error($e->getMessage());
  641. }
  642. }
  643. /**
  644. * 声币兑换钻石
  645. */
  646. public function exchangeSoundcoin()
  647. {
  648. // 接口防并发
  649. if (!$this->apiLimit(1, 1000)) {
  650. $this->error(__('Operation frequently'));
  651. }
  652. $exchange_id = $this->request->request("exchsound_id");// 兑换声币配置ID
  653. $sound_coin = $this->request->request("sound_coin");// 输入具体声币数量
  654. if (!$sound_coin && !$exchange_id) {
  655. $this->error(__('Invalid parameters'));
  656. }
  657. if ($sound_coin > 0) {
  658. $scoin = $sound_coin;
  659. // 获取配置信息
  660. $conf = config("site.soundcoinTojewel");
  661. $exchangeMoney = round($sound_coin * ($conf / 100));
  662. if ($exchangeMoney < 1) $this->error(__('声币兑换数量太小啦!'));
  663. } else {
  664. // 获取兑换配置信息
  665. $exchangeModel = new \app\common\model\ExchsoundConfig();
  666. $exchangeInfo = $exchangeModel->get($exchange_id);
  667. if (!$exchangeInfo) $this->error("配置信息未找到");
  668. $exchangeMoney = $exchangeInfo["jewel"];
  669. $scoin = $exchangeInfo["sound_coin"];
  670. }
  671. Db::startTrans();
  672. try {
  673. // 查询声币余额
  674. $userModel = new \app\common\model\User();
  675. $userInfo = $userModel->lock('lock in share mode')->find($this->auth->id);
  676. if ($scoin > $userInfo["sound_coin"]) {
  677. $this->error("声币余额不足!", [], 100);
  678. }
  679. $soundlogModel = new \app\common\model\UserSoundcoinLog();
  680. $jewellogModel = new \app\common\model\UserJewelLog();
  681. $exchexsoundlogModel = new \app\common\model\UserExchsoundLog();
  682. $detail = "声币兑换钻石";
  683. // 添加钻石流水记录
  684. $res1 = $jewellogModel->addUserJewelLog($this->auth->id, $exchangeMoney, "+", $userInfo["jewel"], $detail, 4);
  685. // 添加兑换记录
  686. $res2 = $exchexsoundlogModel->addExchsoundLog($this->auth->id, $scoin, $exchangeMoney);
  687. // 添加声币流水记录
  688. $res3 = $soundlogModel->addUserSoundcoinLog($this->auth->id, $scoin, "-", $userInfo["sound_coin"], $detail, 2, $res2);
  689. // 减去用户声币余额
  690. $res4 = $userModel->where(["id" => $this->auth->id])->setDec("sound_coin", $scoin);
  691. // 增加用户钻石余额
  692. $res5 = $userModel->where(["id" => $this->auth->id])->setInc("jewel", $exchangeMoney);
  693. if ($res1 && $res2 && $res3 && $res4 && $res5) {
  694. Db::commit();
  695. $this->success("兑换成功!");
  696. } else {
  697. $this->error("操作失败!");
  698. }
  699. } catch (ValidateException $e) {
  700. Db::rollback();
  701. $this->error($e->getMessage());
  702. } catch (PDOException $e) {
  703. Db::rollback();
  704. $this->error($e->getMessage());
  705. } catch (Exception $e) {
  706. Db::rollback();
  707. $this->error($e->getMessage());
  708. }
  709. }
  710. /**
  711. * 声币兑换余额
  712. */
  713. public function exchange()
  714. {
  715. $this->error('接口作废');
  716. exit;//作废
  717. // 接口防并发
  718. if (!$this->apiLimit(1, 1000)) {
  719. $this->error(__('Operation frequently'));
  720. }
  721. $exchange_id = $this->request->request("exchange_id");// 兑换声币配置ID
  722. $sound_coin = $this->request->request("sound_coin");// 输入具体声币数量
  723. if (!$sound_coin && !$exchange_id) {
  724. $this->error(__('Invalid parameters'));
  725. }
  726. if ($sound_coin > 0) {
  727. $scoin = $sound_coin;
  728. // 获取配置信息
  729. $conf = config("site.soundcoinTomoney");
  730. $exchangeMoney = round(($sound_coin * ($conf / 100)) / 100, 2);
  731. if ($exchangeMoney < 0.01) $this->error(__('声币兑换数量太小啦!'));
  732. } else {
  733. // 获取兑换配置信息
  734. $exchangeModel = new \app\common\model\ExchangeConfig();
  735. $exchangeInfo = $exchangeModel->get($exchange_id);
  736. if (!$exchangeInfo) $this->error("配置信息未找到");
  737. $exchangeMoney = $exchangeInfo["money"] / 100;
  738. $scoin = $exchangeInfo["sound_coin"];
  739. }
  740. Db::startTrans();
  741. try {
  742. // 查询声币余额
  743. $userModel = new \app\common\model\User();
  744. $userInfo = $userModel->lock('lock in share mode')->find($this->auth->id);
  745. if ($scoin > $userInfo["sound_coin"]) {
  746. $this->error("声币余额不足!", [], 100);
  747. }
  748. $moneylogModel = new \app\common\model\UserMoneyLog();
  749. $soundlogModel = new \app\common\model\UserSoundcoinLog();
  750. $exchangelogModel = new \app\common\model\UserExchangeLog();
  751. $detail = "声币兑换余额";
  752. // 添加资金流水记录
  753. $res1 = $moneylogModel->addRecord($this->auth->id, $exchangeMoney, "+", $userInfo["money"], $detail);
  754. // 添加兑换记录
  755. $res2 = $exchangelogModel->addExchangeLog($this->auth->id, $scoin, $exchangeMoney);
  756. // 添加声币流水记录
  757. $res3 = $soundlogModel->addUserSoundcoinLog($this->auth->id, $scoin, "-", $userInfo["sound_coin"], $detail, 3, $res2);
  758. // 减去用户声币余额
  759. $res4 = $userModel->where(["id" => $this->auth->id])->setDec("sound_coin", $scoin);
  760. // 增加用户资金余额
  761. $res5 = $userModel->where(["id" => $this->auth->id])->setInc("money", $exchangeMoney);
  762. if ($res1 && $res2 && $res3 && $res4 && $res5) {
  763. Db::commit();
  764. $this->success("兑换成功!");
  765. } else {
  766. $this->error("操作失败!");
  767. }
  768. } catch (ValidateException $e) {
  769. Db::rollback();
  770. $this->error($e->getMessage());
  771. } catch (PDOException $e) {
  772. Db::rollback();
  773. $this->error($e->getMessage());
  774. } catch (Exception $e) {
  775. Db::rollback();
  776. $this->error($e->getMessage());
  777. }
  778. }
  779. // /**
  780. // * 兑换
  781. // */
  782. // public function exchange() {
  783. // $exchange_id = $this->request->request("exchange_id");// 兑换钻石配置ID
  784. // if(!$exchange_id) {
  785. // $this->error(__('Invalid parameters'));
  786. // }
  787. //
  788. // // 获取兑换配置信息
  789. // $exchangeModel = new \app\common\model\ExchangeConfig();
  790. // $exchangeInfo = $exchangeModel->get($exchange_id);
  791. // if(!$exchangeInfo) $this->error("配置信息未找到");
  792. // $exchangeMoney = $exchangeInfo["money"]/100;
  793. // // 查询钻石余额
  794. // $userModel = new \app\common\model\User();
  795. // $userInfo = $userModel->get($this->auth->id);
  796. // if($exchangeInfo["jewel"] > $userInfo["jewel"]) {
  797. // $this->error("钻石余额不足!",[],100);
  798. // }
  799. //
  800. // Db::startTrans();
  801. // try{
  802. // $moneylogModel = new \app\common\model\UserMoneyLog();
  803. // $jewellogModel = new \app\common\model\UserJewelLog();
  804. // $exchangelogModel = new \app\common\model\UserExchangeLog();
  805. // $detail = "兑换";
  806. // // 添加资金流水记录
  807. // $res1 = $moneylogModel->addRecord($this->auth->id,$exchangeMoney,"-",$userInfo["money"],$detail);
  808. // // 添加钻石流水记录
  809. // $res2 = $jewellogModel->addUserJewelLog($this->auth->id,$exchangeMoney,"+",$userInfo["money"],$detail);
  810. // // 添加兑换记录
  811. // $res3 = $exchangelogModel->addExchangeLog($this->auth->id,$exchangeInfo["jewel"],$exchangeMoney);
  812. // // 减去用户钻石余额
  813. // $res4 = $userModel->where(["id"=>$this->auth->id])->setDec("jewel",$exchangeInfo["jewel"]);
  814. // // 增加用户资金余额
  815. // $res5 = $userModel->where(["id"=>$this->auth->id])->setInc("money",$exchangeMoney);
  816. // if($res1 && $res2 && $res3 && $res4 && $res5) {
  817. // Db::commit();
  818. // $this->success("兑换成功!");
  819. // } else {
  820. // $this->error("操作失败!");
  821. // }
  822. // }catch (ValidateException $e) {
  823. // Db::rollback();
  824. // $this->error($e->getMessage());
  825. // } catch (PDOException $e) {
  826. // Db::rollback();
  827. // $this->error($e->getMessage());
  828. // } catch (Exception $e) {
  829. // Db::rollback();
  830. // $this->error($e->getMessage());
  831. // }
  832. // }
  833. /**
  834. * 提现
  835. */
  836. public function withdrow()
  837. {
  838. $money = $this->request->request("money");// 申请提现的金额
  839. if ($money <= 0) {
  840. $this->error(__('提现金额不合法'));
  841. }
  842. // 判断当前用户是否实名认证
  843. $userAuthInfo = \app\common\model\UserAuth::where(["user_id" => $this->auth->id])->find();
  844. if ($userAuthInfo) {
  845. if ($userAuthInfo->status == 0) {
  846. $this->error("您的实名认证还在这审核中...,请耐心等待!");
  847. } elseif ($userAuthInfo->status == 2) {
  848. $this->error("您的实名认证审核未通过,请重新审核!");
  849. }
  850. } else {
  851. $this->error("请先申请实名认证!");
  852. }
  853. // 查询资金余额
  854. $userModel = new \app\common\model\User();
  855. $userInfo = $userModel->get($this->auth->id);
  856. if ($money > $userInfo["money"]) {
  857. $this->error("资金余额不足!", [], 100);
  858. }
  859. Db::startTrans();
  860. try {
  861. $withdrawallogModel = new \app\common\model\UserWithdrawalLog();
  862. // 减去用户可用资金余额
  863. $res1 = $userModel->where(["id" => $this->auth->id])->setDec("money", $money);
  864. // 增加用户冻结资金余额
  865. $res2 = $userModel->where(["id" => $this->auth->id])->setInc("frozen", $money);
  866. // 新增用户提现记录申请
  867. $data = [];
  868. $data["user_id"] = $this->auth->id;
  869. $data["money"] = $money * 100;
  870. $data["status"] = 0; // 提现状态:-1=审核拒绝,0=待审核,1=审核通过
  871. $data["createtime"] = time();
  872. $res3 = $withdrawallogModel->insert($data);
  873. if ($res1 && $res2 && $res3) {
  874. Db::commit();
  875. $this->success("提现申请发送成功!");
  876. } else {
  877. $this->error("操作失败!");
  878. }
  879. } catch (ValidateException $e) {
  880. Db::rollback();
  881. $this->error($e->getMessage());
  882. } catch (PDOException $e) {
  883. Db::rollback();
  884. $this->error($e->getMessage());
  885. } catch (Exception $e) {
  886. Db::rollback();
  887. $this->error($e->getMessage());
  888. }
  889. }
  890. /**
  891. * 获取充值金额配置信息
  892. */
  893. public function getRecharConfig()
  894. {
  895. $params_from = $this->request->request("params_from",'');// 平台参数
  896. $where = [];
  897. if($params_from == 'ios'){
  898. $where['money'] = ['<=', 3000];
  899. }
  900. $list = \app\common\model\RecharConfig::where($where)->order("weight", "asc")->select();
  901. if (!$list) $this->error("配置信息为空!");
  902. foreach ($list as $k => $v) {
  903. $money = $v["money"] / 100;
  904. $list[$k]["money"] = $money;
  905. $list[$k]["desId"] = "banshengvoice" . $money;
  906. if ($params_from != "ios") $list[$k]["jewel"] = $v["jewel_full"];
  907. $list[$k]['ratio'] = $list[$k]["jewel"] / $money;
  908. unset($list[$k]["jewel_full"]);
  909. }
  910. $this->success("获取成功!", $list);
  911. }
  912. /**
  913. * 获取声币兑换余额配置信息
  914. */
  915. public function getExchangeConfig()
  916. {
  917. $list = \app\common\model\ExchangeConfig::field("id,sound_coin,money,updatetime,createtime")->select();
  918. if (!$list) $this->error("配置信息为空!");
  919. foreach ($list as $k => $v) $list[$k]["money"] = $v["money"] / 100;
  920. $this->success("获取成功!", $list);
  921. }
  922. /**
  923. * 获取声币兑换钻石配置信息
  924. */
  925. public function getExchsoundConfig()
  926. {
  927. $list = \app\common\model\ExchsoundConfig::field("id,sound_coin,jewel,updatetime,createtime")->select();
  928. if (!$list) $this->error("配置信息为空!");
  929. $this->success("获取成功!", $list);
  930. }
  931. /**
  932. * 获取用户充值记录
  933. */
  934. public function rechargeRecord()
  935. {
  936. $page = $this->request->request('page', 1); // 分页
  937. $pageNum = $this->request->request('pageNum', 10); // 分页
  938. // 分页搜索构建
  939. $pageStart = ($page - 1) * $pageNum;
  940. $res = \app\common\model\UserRechargeLog::where(["user_id" => $this->auth->id])->limit($pageStart, $pageNum)->order("createtime", "desc")->select();
  941. if ($res): foreach ($res as $k => &$v) $v["createtime"] = date("Y-m-d H:i:s", $v["createtime"]);endif;
  942. $this->success("获取成功!", $res);
  943. }
  944. /**
  945. * 获取用户兑换记录
  946. */
  947. public function exchangeRecord()
  948. {
  949. $page = $this->request->request('page', 1); // 分页
  950. $pageNum = $this->request->request('pageNum', 10); // 分页
  951. // 分页搜索构建
  952. $pageStart = ($page - 1) * $pageNum;
  953. $res = \app\common\model\UserExchangeLog::field("*,sound_coin as jewel")->where(["user_id" => $this->auth->id])->limit($pageStart, $pageNum)->order("createtime", "desc")->select();
  954. if ($res): foreach ($res as $k => &$v) $v["createtime"] = date("Y-m-d H:i:s", $v["createtime"]);endif;
  955. $this->success("获取成功!", $res);
  956. }
  957. /**
  958. * 获取用户声币兑换钻石记录
  959. */
  960. public function exchsoundRecord()
  961. {
  962. $page = $this->request->request('page', 1); // 分页
  963. $pageNum = $this->request->request('pageNum', 10); // 分页
  964. // 分页搜索构建
  965. $pageStart = ($page - 1) * $pageNum;
  966. $res = \app\common\model\UserExchsoundLog::where(["user_id" => $this->auth->id])->limit($pageStart, $pageNum)->order("createtime", "desc")->select();
  967. if ($res): foreach ($res as $k => &$v) $v["createtime"] = date("Y-m-d H:i:s", $v["createtime"]);endif;
  968. $this->success("获取成功!", $res);
  969. }
  970. /**
  971. * 获取用户提现记录
  972. */
  973. public function withdrowRecord()
  974. {
  975. $page = $this->request->request('page', 1); // 分页
  976. $pageNum = $this->request->request('pageNum', 10); // 分页
  977. // 分页搜索构建
  978. $pageStart = ($page - 1) * $pageNum;
  979. $res = \app\common\model\UserWithdrawalLog::where(["user_id" => $this->auth->id])->limit($pageStart, $pageNum)->order("createtime", "desc")->select();
  980. if ($res) {
  981. $statusTxt = array("-1" => "审核拒绝", "0" => "待审核", "1" => "审核通过");
  982. foreach ($res as $k => &$v) {
  983. $v["status"] = $statusTxt[$v["status"]];
  984. $v["money"] = $v["money"] > 0 ? $v["money"] / 100 : 0;
  985. $v["createtime"] = date("Y-m-d H:i:s", $v["createtime"]);
  986. }
  987. }
  988. $this->success("获取成功!", $res);
  989. }
  990. /**
  991. * 获取返利列表
  992. */
  993. public function getRebateList()
  994. {
  995. $pretype = $this->request->request('pretype', 0); // 下属类型:1=直属,2=下属
  996. if (!in_array($pretype, [0, 1, 2])) $this->error("参数错误!");
  997. $page = $this->request->request('page', 1); // 分页
  998. $pageNum = $this->request->request('pageNum', 10); // 分页
  999. // 分页搜索构建
  1000. $pageStart = ($page - 1) * $pageNum;
  1001. $where = [];
  1002. $where["a.user_id"] = $this->auth->id;
  1003. $pretype && $where["a.rebate_type"] = $pretype;
  1004. $where["a.is_rebate"] = 1;
  1005. $list = \app\common\model\RebateLog::alias("a")
  1006. ->field("a.*,u.nickname")
  1007. ->join("hx_user u", "u.id = a.by_user_id")
  1008. ->limit($pageStart, $pageNum)
  1009. ->order("createtime", "desc")
  1010. ->where($where)->select();
  1011. if ($list) foreach ($list as $k => &$v) {
  1012. $v["createtime"] = date("Y-m-d H:i:s", $v["createtime"]);
  1013. $v["nickname"] = mb_substr($v["nickname"], 0, 1, "utf-8") . "***";
  1014. }
  1015. $this->success("获取成功!", $list);
  1016. }
  1017. /**
  1018. * 获取剩余返利总额
  1019. */
  1020. public function getRebateMoney()
  1021. {
  1022. $this->success("获取成功!", \app\common\model\User::where(["id" => $this->auth->id])->value("rebate_money"));
  1023. }
  1024. /**
  1025. * 获取返利统计
  1026. */
  1027. public function getRebateCount()
  1028. {
  1029. // 直属推荐收益
  1030. $where = [];
  1031. $where["user_id"] = $this->auth->id;
  1032. $where["rebate_type"] = 1;
  1033. $preRebate = \app\common\model\RebateLog::where($where)->sum("rebate_money");
  1034. // 下属推荐收益
  1035. $where = [];
  1036. $where["user_id"] = $this->auth->id;
  1037. $where["rebate_type"] = 2;
  1038. $prepreRebate = \app\common\model\RebateLog::where($where)->sum("rebate_money");
  1039. // 上周收益总计
  1040. $preweek = $this->firstOfWeek(date("Y-m-d H:i:s",bcsub(time(),604800)));
  1041. $starttime = strtotime($preweek." 00:00:00"); // 上周一时间
  1042. $endtime = bcadd($starttime,604800); // 上周天时间
  1043. $where = [];
  1044. $where["user_id"] = $this->auth->id;
  1045. $where["createtime"] = ["between", "$starttime,$endtime"];
  1046. $weekRebate = \app\common\model\RebateLog::where($where)->sum("rebate_money");
  1047. // 历史收益总计
  1048. $where = [];
  1049. $where["user_id"] = $this->auth->id;
  1050. $where["createtime"] = ["lt", $starttime];
  1051. $allRebate = \app\common\model\RebateLog::where($where)->sum("rebate_money");
  1052. $res = [];
  1053. $res["preRebate"] = number_format($preRebate, 2);
  1054. $res["prepreRebate"] = number_format($prepreRebate, 2);
  1055. $res["weekRebate"] = number_format($weekRebate, 2);
  1056. $res["allRebate"] = number_format($allRebate, 2);
  1057. $this->success("获取成功!", $res);
  1058. }
  1059. /**
  1060. * 返利提现
  1061. */
  1062. public function rebateWithdraw()
  1063. {
  1064. $money = $this->request->request('money'); // 提现金额
  1065. if (!$money) $this->error("参数缺失!");
  1066. if ($money > 50000) $this->error("单笔上限50000元哦,超过50000元请联系客服处理!");
  1067. if ($money % 10 > 0) $this->error("提现金额必须为10的倍数!");
  1068. $user_id = $this->auth->id;
  1069. // 获取用户返利金额
  1070. $rebateMoney = \app\common\model\User::where(['id' => $user_id])->value("rebate_money");
  1071. if ($rebateMoney <= 100) $this->error("返利金额需要大于100元才能提现哦!");
  1072. $rdsMoney = bcsub($rebateMoney, $money);
  1073. if ($rdsMoney < 0) {
  1074. $this->error("余额不足,不能提现!", null, 100);
  1075. }
  1076. // 可以提现
  1077. Db::startTrans();
  1078. try {
  1079. $data = [];
  1080. $data["user_id"] = $user_id;
  1081. $data["money"] = $money;
  1082. $data["before"] = $rebateMoney;
  1083. $data["after"] = $rdsMoney;
  1084. $data["status"] = 1;
  1085. $data["createtime"] = time();
  1086. $res1 = \app\common\model\RebateWithdraw::insert($data);
  1087. $res2 = \app\common\model\User::update(["rebate_money" => $rdsMoney], ["id" => $user_id]);
  1088. if ($res1 && $res2) {
  1089. Db::commit();
  1090. $this->success("提现成功!");
  1091. } else {
  1092. $this->error("网络繁忙,请稍后重试!");
  1093. }
  1094. } catch (ValidateException $e) {
  1095. Db::rollback();
  1096. $this->error($e->getMessage());
  1097. } catch (PDOException $e) {
  1098. Db::rollback();
  1099. $this->error($e->getMessage());
  1100. } catch (Exception $e) {
  1101. Db::rollback();
  1102. $this->error($e->getMessage());
  1103. }
  1104. }
  1105. /**
  1106. * 提现余额记录验证
  1107. */
  1108. public function withdrawVerify($user_id)
  1109. {
  1110. if (!$user_id) return false;
  1111. // 统计用户所有兑换记录和提现记录 是否与余额匹配
  1112. $moneylog = \app\common\model\UserMoneyLog::where(["user_id" => $user_id, "mode" => "+"])->sum('value');
  1113. $withdraw = \app\common\model\UserWithdrawalLog::where(["user_id" => $user_id, "status" => ["in", [0, 1]]])->sum("money");
  1114. $withdraw = bcdiv($withdraw, 100,2);
  1115. $sum = bcsub($moneylog, $withdraw,2);
  1116. $usermoney = \app\common\model\User::where(["id" => $user_id])->value("money");
  1117. if ($usermoney == $sum) {
  1118. return true;
  1119. } else {
  1120. return false;
  1121. }
  1122. }
  1123. /**
  1124. * 获取声币记录
  1125. */
  1126. public function getMySoundCoinRecord()
  1127. {
  1128. $type = $this->request->request('type', 1); // 类型
  1129. $page = $this->request->request('page', 1); // 分页
  1130. $pageNum = $this->request->request('pageNum', 10); // 分页
  1131. // 分页搜索构建
  1132. $pageStart = ($page - 1) * $pageNum;
  1133. $where = [];
  1134. $where["user_id"] = $this->auth->id;
  1135. $where["obj_id"] = ['>', 0];
  1136. if ($type == 1) { //收益记录
  1137. $where["type"] = 1;
  1138. } else {// 提现记录,后台调整划到提现记录
  1139. $where["type"] = ['in', '2,3,10000'];
  1140. }
  1141. $res = \app\common\model\UserSoundcoinLog::field('id,type,obj_id,value,mode,detail,createtime')->where($where)->limit($pageStart, $pageNum)->order("createtime", "desc")->select();
  1142. if ($res) {
  1143. foreach ($res as $k => &$v) {
  1144. $subject = $v['detail'];
  1145. if ($v['type'] == 1) {
  1146. // 查询礼物赠送记录
  1147. $giftInfo = GiftUserParty::alias('a')->join('user u', 'a.user_id = u.id')->where('a.id', $v['obj_id'])->field('user_id,nickname,gift_id,gift_name,number')->find();
  1148. $subject = $giftInfo['nickname'] . '送你';
  1149. $subject_notes = "{$giftInfo['gift_name']} * {$giftInfo['number']}";
  1150. } elseif ($v['type'] == 2) {
  1151. // 查询礼物赠送记录
  1152. $jewel = UserExchsoundLog::where('id', $v['obj_id'])->value('jewel');
  1153. $subject_notes = "已兑换{$jewel}砖石";
  1154. } elseif ($v['type'] == 3) {
  1155. // 查询礼物赠送记录
  1156. $money = UserExchangeLog::where('id', $v['obj_id'])->value('money');
  1157. $subject_notes = "已兑换{$money}余额";
  1158. } elseif ($v['type'] == 5 || $v['type'] == 10000) {
  1159. $modeText = $v['mode'] == '+' ? '调增' : '调减';
  1160. $subject_notes = "已{$modeText}{$v['value']}声币";
  1161. }
  1162. $v['subject'] = $subject;
  1163. $v['subject_notes'] = $subject_notes;
  1164. $v["createtime"] = date("m月d日 H时i分", $v["createtime"]);
  1165. }
  1166. }
  1167. $this->success("获取成功!", $res);
  1168. }
  1169. /**
  1170. * 内购参数验证
  1171. */
  1172. public function applebuy()
  1173. {
  1174. $receiptdata = $this->request->request('receiptdata'); // 验证参数
  1175. $rechar_id = $this->request->request("rechar_id");// 充值金额配置ID
  1176. if (!$receiptdata || !$rechar_id) $this->error("参数缺失!");
  1177. // 获取密钥配置
  1178. $configInfo = config("applebuy_url");
  1179. $applebuy = new applebuy($configInfo);
  1180. $ret = $applebuy->toSendVeify($receiptdata);
  1181. if (!$ret || !isset($ret["status"])) $this->error("网络请求失败,请稍后重试!");
  1182. if ($ret["status"] != 0) $this->error("支付验证失败!");
  1183. // print_r($ret);exit;
  1184. $user_id = $this->auth->id;
  1185. // 获取充值金额配置信息
  1186. $recharMode = new \app\common\model\RecharConfig();
  1187. $recharInfo = $recharMode->where(["id" => $rechar_id])->find();
  1188. if (!$recharInfo) $this->error("金额配置信息未找到!");
  1189. $amount = $recharInfo["money"] / 100;
  1190. $retkey = end($ret["receipt"]["in_app"]);
  1191. if ($retkey["product_id"] != "banshengvoice" . $amount) $this->error("支付验证失败!");
  1192. if (!$amount || $amount < 0) {
  1193. $this->error("支付金额必须大于0");
  1194. }
  1195. if($amount > 30){
  1196. $this->error("充值维护中");
  1197. }
  1198. $time = time();
  1199. $money = (float)bcadd($amount, 0, 2);
  1200. $recharOrderMode = new \app\common\model\RecharOrder();
  1201. $resrepeatid = $retkey["transaction_id"];
  1202. // 查询订单是否重复
  1203. $repeatOrderId = $recharOrderMode->where(["transaction_id" => $resrepeatid])->value("id");
  1204. if ($repeatOrderId > 0) $this->error("请勿重复操作!");
  1205. // 生成订单
  1206. $out_trade_no = date("YmdHis") . rand(100000, 999999);
  1207. $orderid = $recharOrderMode->execute("INSERT INTO `hx_rechar_order` (`user_id` , `order_no` , `money` , `jewel` , `transaction_id`, `createtime`) VALUES ($user_id , $out_trade_no , $money , " . $recharInfo['jewel'] . ", $resrepeatid, $time)");
  1208. if (!$orderid) $this->error("订单创建失败!");
  1209. // 添加充值记录等
  1210. $dores = $this->applePayNotify($out_trade_no, $money);
  1211. if (!$dores) $this->error("状态更新失败,请联系客服!");
  1212. // 首充赠送
  1213. $userInfo = \app\common\model\User::get($user_id);
  1214. if ($userInfo->is_recharge == 0) { // 首充
  1215. Db::startTrans();
  1216. try {
  1217. // 赠送消息尾灯
  1218. $res1 = \app\common\model\AttireBack::addToMyBack(15, $user_id);
  1219. $userInfo->is_recharge = 1;
  1220. $res2 = $userInfo->save();
  1221. if ($res1 && $res2) Db::commit();
  1222. } catch (ValidateException $e) {
  1223. Db::rollback();
  1224. $this->error($e->getMessage());
  1225. }
  1226. }
  1227. $this->rebate($user_id, $amount);
  1228. // +EXP
  1229. \app\common\model\TaskLog::tofinish($user_id, "5EMwg7la", 1);
  1230. // 查询今日充值记录
  1231. $today = strtotime(date("Y-m-d 00:00:00"));
  1232. $rechargeMoney = \app\common\model\RecharOrder::where(["user_id" => $user_id, "createtime" => ["gt", $today]])->sum("money");
  1233. if ($rechargeMoney >= 100) {
  1234. // +EXP
  1235. \app\common\model\TaskLog::tofinish($user_id, "DaCVgOLu", 1);
  1236. }
  1237. // 查询新人礼包
  1238. $this->addBagGift($user_id, $amount);
  1239. $this->success("购买成功!");
  1240. }
  1241. /**
  1242. * 内购逻辑
  1243. */
  1244. private function applePayNotify($out_trade_no, $payamount)
  1245. {
  1246. Db::startTrans();
  1247. try {
  1248. $orderModel = new \app\common\model\RecharOrder();
  1249. $userModel = new \app\common\model\User();
  1250. $jewellogModel = new \app\common\model\UserJewelLog();
  1251. $rechargelogModel = new \app\common\model\UserRechargeLog();
  1252. $orderInfo = $orderModel->where(["order_no" => $out_trade_no])->find();
  1253. $userInfo = $userModel->where(["id" => $orderInfo["user_id"]])->find();
  1254. // 修改订单状态
  1255. $res1 = $orderModel->update(["status" => 1, 'platform' => 2], ["order_no" => $out_trade_no]);
  1256. // 修改用户钻石余额
  1257. $res2 = $userModel->where(["id" => $orderInfo["user_id"]])->setInc("jewel", $orderInfo["jewel"]);
  1258. // 添加钻石流水记录
  1259. $detail = "充值";
  1260. $res3 = $jewellogModel->addUserJewelLog($userInfo["id"], $orderInfo["jewel"], "+", $userInfo["jewel"], $detail, 1);
  1261. $rechargeCount = $rechargelogModel->where('user_id',$orderInfo["user_id"])->count();
  1262. $isFirstRecharge = $rechargeCount > 0 ? 0 : 1;
  1263. // 添加充值记录
  1264. $res4 = $rechargelogModel->addRecord($userInfo["id"], $orderInfo["jewel"], $payamount, bcadd($userInfo["jewel"], $orderInfo["jewel"], 2), $userInfo["money"], 2, 4, $isFirstRecharge, $userInfo["pre_userid"]);
  1265. if ($res1 && $res2 && $res3 && $res4) {
  1266. // 添加统计
  1267. $time = time();
  1268. (new \app\common\model\User)->execute("update hx_user set chargecount = chargecount + $payamount,chargetime = $time where id = " . $userInfo["id"]);
  1269. Db::commit();
  1270. return true;
  1271. }
  1272. } catch (ValidateException $e) {
  1273. Db::rollback();
  1274. return false;
  1275. } catch (PDOException $e) {
  1276. Db::rollback();
  1277. return false;
  1278. } catch (Exception $e) {
  1279. Db::rollback();
  1280. return false;
  1281. }
  1282. }
  1283. /**
  1284. * 获取我的推荐列表
  1285. */
  1286. public function getMyUsers() {
  1287. $thispage = $this->request->request('thispage',1); // 当前页
  1288. $pageNum = $this->request->request('pageNum', 10); // 分页
  1289. $page = [];
  1290. $page["page"] = $thispage;
  1291. $page["list_rows"] = $pageNum;
  1292. $user_id = $this->auth->id;
  1293. $userList = \app\common\model\User::field("id,nickname,u_id,bindtime")->where(["pre_userid"=>$user_id])->paginate($page);
  1294. if($userList) foreach($userList as $k => $v) $userList[$k]["bindtime"] = date("Y-m-d H:i:s",$v["bindtime"]);
  1295. $this->success("获取成功!",$userList);
  1296. }
  1297. /**
  1298. * 获取下级/下下级充值记录
  1299. */
  1300. public function getRechargeLog() {
  1301. $user_type = $this->request->request('user_type'); // 类型:1=直属,2=下属
  1302. $thispage = $this->request->request('thispage',1); // 当前页
  1303. $user_id = $this->auth->id;
  1304. $users = \app\common\model\User::where(["pre_userid"=>$user_id])->column("id");
  1305. if(!$users) $this->success("获取成功!",$users);
  1306. $ids = $users;
  1307. // 下下级
  1308. if($user_type == 2) {
  1309. $usersusers = [];
  1310. foreach($users as $v) {
  1311. $user_id = $v;
  1312. $userspre = \app\common\model\User::where(["pre_userid"=>$user_id])->column("id");
  1313. if(!$userspre) continue;
  1314. foreach($userspre as $n) {
  1315. $usersusers[] = $n;
  1316. }
  1317. }
  1318. $ids = $usersusers;
  1319. }
  1320. // 上个周一
  1321. $preweek = $this->firstOfWeek(date("Y-m-d H:i:s",bcsub(time(),604800)));
  1322. $starttime = strtotime($preweek." 00:00:00"); // 上周时间
  1323. $page = [];
  1324. $page["page"] = $thispage;
  1325. $page["list_rows"] = 10;
  1326. $where = [];
  1327. $where["a.user_id"] = ["in",$ids];
  1328. $where["a.createtime"] = ["gt",$starttime];
  1329. // 获取充值记录
  1330. $rechargeList = \app\common\model\UserRechargeLog::alias("a")
  1331. ->field("a.id,a.user_id,u.nickname,u.bindtime,a.createtime,a.money")
  1332. ->join("hx_user u","u.id = a.user_id")
  1333. ->where($where)
  1334. ->order("a.createtime","desc")
  1335. ->paginate($page);
  1336. if($rechargeList) foreach($rechargeList as $k => $v) {
  1337. if($v["createtime"] < $v["bindtime"]) {
  1338. unset($rechargeList[$k]);
  1339. continue;
  1340. }
  1341. $rechargeList[$k]["createtime"] = date("Y-m-d H:i:s",$v["createtime"]);
  1342. $rechargeList[$k]["nickname"] = mb_substr($v["nickname"], 0, 1, "utf-8") . "***";
  1343. }
  1344. $this->success("获取成功!",$rechargeList);
  1345. }
  1346. /**
  1347. * 获取上周收益
  1348. */
  1349. public function getLastweekProfit() {
  1350. $user_id = $this->auth->id;
  1351. $result = [];
  1352. // 上个周一
  1353. $preweek = $this->firstOfWeek(date("Y-m-d H:i:s",bcsub(time(),604800)));
  1354. $starttime = strtotime($preweek." 00:00:00"); // 上周一时间
  1355. $endtime = bcadd($starttime,604800); // 上周天时间
  1356. // 直属收益
  1357. $where = [];
  1358. $where["user_id"] = $user_id;
  1359. $where["is_rebate"] = 1;
  1360. $where["rebate_type"] = 1;
  1361. $where["createtime"] = ["between",[$starttime,$endtime]];
  1362. $rebate_rate = \app\common\model\RebateLog::where($where)->value("rebate_rate");
  1363. $rebate_rate = bcmul($rebate_rate,100,2);
  1364. $result["pre"]["money"] = \app\common\model\RebateLog::where($where)->sum("money");
  1365. $result["pre"]["rate"] = (float)$rebate_rate;
  1366. $result["pre"]["rebate_money"] = \app\common\model\RebateLog::where($where)->sum("rebate_money");
  1367. // 下属收益
  1368. $where = [];
  1369. $where["user_id"] = $user_id;
  1370. $where["is_rebate"] = 1;
  1371. $where["rebate_type"] = 2;
  1372. $where["createtime"] = ["between",[$starttime,$endtime]];
  1373. $rebate_rate = \app\common\model\RebateLog::where($where)->value("rebate_rate");
  1374. $rebate_rate = bcmul($rebate_rate,100,2);
  1375. $result["prepre"]["money"] = \app\common\model\RebateLog::where($where)->sum("money");
  1376. $result["prepre"]["rate"] = (float)$rebate_rate;
  1377. $result["prepre"]["rebate_money"] = \app\common\model\RebateLog::where($where)->sum("rebate_money");
  1378. // 上周收益总计
  1379. $result['weekRebate'] = (float)bcadd($result["pre"]["rebate_money"],$result["prepre"]["rebate_money"],2);
  1380. $this->success("获取成功!",$result);
  1381. }
  1382. ////////////////////////////////////
  1383. /**
  1384. * 消息-聊天-更多-转账-钻石
  1385. */
  1386. public function givejeweltouser(){
  1387. $money = intval(input('money',0));
  1388. $user_id = input('user_id',0);
  1389. $pay_password = input('paypassword','');
  1390. //验证用户权限是否能转账
  1391. if($this->auth->power->transfer == 1){
  1392. $this->error('您已经被限制消费-转账');
  1393. }
  1394. //验证被转账用户存在
  1395. $check = Db::name('user')->where('id',$user_id)->find();
  1396. if(empty($check)){
  1397. $this->error('不存在的用户');
  1398. }
  1399. //验证操作密码
  1400. if(!$this->auth->pay_password){
  1401. $this->error('请先设置支付密码');
  1402. }
  1403. if ($this->auth->pay_password != $this->getEncryptPassword($pay_password, $this->auth->pay_salt)) {
  1404. $this->error('支付密码有误');
  1405. }
  1406. Db::startTrans();
  1407. //减少和增加余额
  1408. $rs_wallet = model('wallet')->lockChangeAccountRemain($this->auth->id,$money,'-',0,'私聊转账给:'.$check['nickname'],11,'jewel');
  1409. if($rs_wallet['status'] === false){
  1410. Db::rollback();
  1411. $this->error($rs_wallet['msg']);
  1412. }
  1413. $rs_wallet = model('wallet')->lockChangeAccountRemain($user_id,$money,'+',0,$this->auth->nickname.':私聊到账',12,'jewel');
  1414. if($rs_wallet['status'] === false){
  1415. Db::rollback();
  1416. $this->error($rs_wallet['msg']);
  1417. }
  1418. Db::commit();
  1419. $this->success('转账成功');
  1420. }
  1421. /**
  1422. * 获取密码加密后的字符串
  1423. * @param string $password 密码
  1424. * @param string $salt 密码盐
  1425. * @return string
  1426. */
  1427. private function getEncryptPassword($password, $salt = '')
  1428. {
  1429. return md5(md5($password) . $salt);
  1430. }
  1431. }