Money.php 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565
  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. // 接口防并发
  716. if (!$this->apiLimit(1, 1000)) {
  717. $this->error(__('Operation frequently'));
  718. }
  719. $exchange_id = $this->request->request("exchange_id");// 兑换声币配置ID
  720. $sound_coin = $this->request->request("sound_coin");// 输入具体声币数量
  721. if (!$sound_coin && !$exchange_id) {
  722. $this->error(__('Invalid parameters'));
  723. }
  724. if ($sound_coin > 0) {
  725. $scoin = $sound_coin;
  726. // 获取配置信息
  727. $conf = config("site.soundcoinTomoney");
  728. $exchangeMoney = round(($sound_coin * ($conf / 100)) / 100, 2);
  729. if ($exchangeMoney < 0.01) $this->error(__('声币兑换数量太小啦!'));
  730. } else {
  731. // 获取兑换配置信息
  732. $exchangeModel = new \app\common\model\ExchangeConfig();
  733. $exchangeInfo = $exchangeModel->get($exchange_id);
  734. if (!$exchangeInfo) $this->error("配置信息未找到");
  735. $exchangeMoney = $exchangeInfo["money"] / 100;
  736. $scoin = $exchangeInfo["sound_coin"];
  737. }
  738. Db::startTrans();
  739. try {
  740. // 查询声币余额
  741. $userModel = new \app\common\model\User();
  742. $userInfo = $userModel->lock('lock in share mode')->find($this->auth->id);
  743. if ($scoin > $userInfo["sound_coin"]) {
  744. $this->error("声币余额不足!", [], 100);
  745. }
  746. $moneylogModel = new \app\common\model\UserMoneyLog();
  747. $soundlogModel = new \app\common\model\UserSoundcoinLog();
  748. $exchangelogModel = new \app\common\model\UserExchangeLog();
  749. $detail = "声币兑换余额";
  750. // 添加资金流水记录
  751. $res1 = $moneylogModel->addRecord($this->auth->id, $exchangeMoney, "+", $userInfo["money"], $detail);
  752. // 添加兑换记录
  753. $res2 = $exchangelogModel->addExchangeLog($this->auth->id, $scoin, $exchangeMoney);
  754. // 添加声币流水记录
  755. $res3 = $soundlogModel->addUserSoundcoinLog($this->auth->id, $scoin, "-", $userInfo["sound_coin"], $detail, 3, $res2);
  756. // 减去用户声币余额
  757. $res4 = $userModel->where(["id" => $this->auth->id])->setDec("sound_coin", $scoin);
  758. // 增加用户资金余额
  759. $res5 = $userModel->where(["id" => $this->auth->id])->setInc("money", $exchangeMoney);
  760. if ($res1 && $res2 && $res3 && $res4 && $res5) {
  761. Db::commit();
  762. $this->success("兑换成功!");
  763. } else {
  764. $this->error("操作失败!");
  765. }
  766. } catch (ValidateException $e) {
  767. Db::rollback();
  768. $this->error($e->getMessage());
  769. } catch (PDOException $e) {
  770. Db::rollback();
  771. $this->error($e->getMessage());
  772. } catch (Exception $e) {
  773. Db::rollback();
  774. $this->error($e->getMessage());
  775. }
  776. }
  777. // /**
  778. // * 兑换
  779. // */
  780. // public function exchange() {
  781. // $exchange_id = $this->request->request("exchange_id");// 兑换钻石配置ID
  782. // if(!$exchange_id) {
  783. // $this->error(__('Invalid parameters'));
  784. // }
  785. //
  786. // // 获取兑换配置信息
  787. // $exchangeModel = new \app\common\model\ExchangeConfig();
  788. // $exchangeInfo = $exchangeModel->get($exchange_id);
  789. // if(!$exchangeInfo) $this->error("配置信息未找到");
  790. // $exchangeMoney = $exchangeInfo["money"]/100;
  791. // // 查询钻石余额
  792. // $userModel = new \app\common\model\User();
  793. // $userInfo = $userModel->get($this->auth->id);
  794. // if($exchangeInfo["jewel"] > $userInfo["jewel"]) {
  795. // $this->error("钻石余额不足!",[],100);
  796. // }
  797. //
  798. // Db::startTrans();
  799. // try{
  800. // $moneylogModel = new \app\common\model\UserMoneyLog();
  801. // $jewellogModel = new \app\common\model\UserJewelLog();
  802. // $exchangelogModel = new \app\common\model\UserExchangeLog();
  803. // $detail = "兑换";
  804. // // 添加资金流水记录
  805. // $res1 = $moneylogModel->addRecord($this->auth->id,$exchangeMoney,"-",$userInfo["money"],$detail);
  806. // // 添加钻石流水记录
  807. // $res2 = $jewellogModel->addUserJewelLog($this->auth->id,$exchangeMoney,"+",$userInfo["money"],$detail);
  808. // // 添加兑换记录
  809. // $res3 = $exchangelogModel->addExchangeLog($this->auth->id,$exchangeInfo["jewel"],$exchangeMoney);
  810. // // 减去用户钻石余额
  811. // $res4 = $userModel->where(["id"=>$this->auth->id])->setDec("jewel",$exchangeInfo["jewel"]);
  812. // // 增加用户资金余额
  813. // $res5 = $userModel->where(["id"=>$this->auth->id])->setInc("money",$exchangeMoney);
  814. // if($res1 && $res2 && $res3 && $res4 && $res5) {
  815. // Db::commit();
  816. // $this->success("兑换成功!");
  817. // } else {
  818. // $this->error("操作失败!");
  819. // }
  820. // }catch (ValidateException $e) {
  821. // Db::rollback();
  822. // $this->error($e->getMessage());
  823. // } catch (PDOException $e) {
  824. // Db::rollback();
  825. // $this->error($e->getMessage());
  826. // } catch (Exception $e) {
  827. // Db::rollback();
  828. // $this->error($e->getMessage());
  829. // }
  830. // }
  831. /**
  832. * 提现
  833. */
  834. public function withdrow()
  835. {
  836. $money = $this->request->request("money");// 申请提现的金额
  837. if ($money <= 0) {
  838. $this->error(__('提现金额不合法'));
  839. }
  840. // 判断当前用户是否实名认证
  841. $userAuthInfo = \app\common\model\UserAuth::where(["user_id" => $this->auth->id])->find();
  842. if ($userAuthInfo) {
  843. if ($userAuthInfo->status == 0) {
  844. $this->error("您的实名认证还在这审核中...,请耐心等待!");
  845. } elseif ($userAuthInfo->status == 2) {
  846. $this->error("您的实名认证审核未通过,请重新审核!");
  847. }
  848. } else {
  849. $this->error("请先申请实名认证!");
  850. }
  851. // 查询资金余额
  852. $userModel = new \app\common\model\User();
  853. $userInfo = $userModel->get($this->auth->id);
  854. if ($money > $userInfo["money"]) {
  855. $this->error("资金余额不足!", [], 100);
  856. }
  857. Db::startTrans();
  858. try {
  859. $withdrawallogModel = new \app\common\model\UserWithdrawalLog();
  860. // 减去用户可用资金余额
  861. $res1 = $userModel->where(["id" => $this->auth->id])->setDec("money", $money);
  862. // 增加用户冻结资金余额
  863. $res2 = $userModel->where(["id" => $this->auth->id])->setInc("frozen", $money);
  864. // 新增用户提现记录申请
  865. $data = [];
  866. $data["user_id"] = $this->auth->id;
  867. $data["money"] = $money * 100;
  868. $data["status"] = 0; // 提现状态:-1=审核拒绝,0=待审核,1=审核通过
  869. $data["createtime"] = time();
  870. $res3 = $withdrawallogModel->insert($data);
  871. if ($res1 && $res2 && $res3) {
  872. Db::commit();
  873. $this->success("提现申请发送成功!");
  874. } else {
  875. $this->error("操作失败!");
  876. }
  877. } catch (ValidateException $e) {
  878. Db::rollback();
  879. $this->error($e->getMessage());
  880. } catch (PDOException $e) {
  881. Db::rollback();
  882. $this->error($e->getMessage());
  883. } catch (Exception $e) {
  884. Db::rollback();
  885. $this->error($e->getMessage());
  886. }
  887. }
  888. /**
  889. * 获取充值金额配置信息
  890. */
  891. public function getRecharConfig()
  892. {
  893. $params_from = $this->request->request("params_from",'');// 平台参数
  894. $where = [];
  895. if($params_from == 'ios'){
  896. $where['money'] = ['<=', 3000];
  897. }
  898. $list = \app\common\model\RecharConfig::where($where)->order("weight", "asc")->select();
  899. if (!$list) $this->error("配置信息为空!");
  900. foreach ($list as $k => $v) {
  901. $money = $v["money"] / 100;
  902. $list[$k]["money"] = $money;
  903. $list[$k]["desId"] = "banshengvoice" . $money;
  904. if ($params_from != "ios") $list[$k]["jewel"] = $v["jewel_full"];
  905. $list[$k]['ratio'] = $list[$k]["jewel"] / $money;
  906. unset($list[$k]["jewel_full"]);
  907. }
  908. $this->success("获取成功!", $list);
  909. }
  910. /**
  911. * 获取声币兑换余额配置信息
  912. */
  913. public function getExchangeConfig()
  914. {
  915. $list = \app\common\model\ExchangeConfig::field("id,sound_coin,money,updatetime,createtime")->select();
  916. if (!$list) $this->error("配置信息为空!");
  917. foreach ($list as $k => $v) $list[$k]["money"] = $v["money"] / 100;
  918. $this->success("获取成功!", $list);
  919. }
  920. /**
  921. * 获取声币兑换钻石配置信息
  922. */
  923. public function getExchsoundConfig()
  924. {
  925. $list = \app\common\model\ExchsoundConfig::field("id,sound_coin,jewel,updatetime,createtime")->select();
  926. if (!$list) $this->error("配置信息为空!");
  927. $this->success("获取成功!", $list);
  928. }
  929. /**
  930. * 获取用户充值记录
  931. */
  932. public function rechargeRecord()
  933. {
  934. $page = $this->request->request('page', 1); // 分页
  935. $pageNum = $this->request->request('pageNum', 10); // 分页
  936. // 分页搜索构建
  937. $pageStart = ($page - 1) * $pageNum;
  938. $res = \app\common\model\UserRechargeLog::where(["user_id" => $this->auth->id])->limit($pageStart, $pageNum)->order("createtime", "desc")->select();
  939. if ($res): foreach ($res as $k => &$v) $v["createtime"] = date("Y-m-d H:i:s", $v["createtime"]);endif;
  940. $this->success("获取成功!", $res);
  941. }
  942. /**
  943. * 获取用户兑换记录
  944. */
  945. public function exchangeRecord()
  946. {
  947. $page = $this->request->request('page', 1); // 分页
  948. $pageNum = $this->request->request('pageNum', 10); // 分页
  949. // 分页搜索构建
  950. $pageStart = ($page - 1) * $pageNum;
  951. $res = \app\common\model\UserExchangeLog::field("*,sound_coin as jewel")->where(["user_id" => $this->auth->id])->limit($pageStart, $pageNum)->order("createtime", "desc")->select();
  952. if ($res): foreach ($res as $k => &$v) $v["createtime"] = date("Y-m-d H:i:s", $v["createtime"]);endif;
  953. $this->success("获取成功!", $res);
  954. }
  955. /**
  956. * 获取用户声币兑换钻石记录
  957. */
  958. public function exchsoundRecord()
  959. {
  960. $page = $this->request->request('page', 1); // 分页
  961. $pageNum = $this->request->request('pageNum', 10); // 分页
  962. // 分页搜索构建
  963. $pageStart = ($page - 1) * $pageNum;
  964. $res = \app\common\model\UserExchsoundLog::where(["user_id" => $this->auth->id])->limit($pageStart, $pageNum)->order("createtime", "desc")->select();
  965. if ($res): foreach ($res as $k => &$v) $v["createtime"] = date("Y-m-d H:i:s", $v["createtime"]);endif;
  966. $this->success("获取成功!", $res);
  967. }
  968. /**
  969. * 获取用户提现记录
  970. */
  971. public function withdrowRecord()
  972. {
  973. $page = $this->request->request('page', 1); // 分页
  974. $pageNum = $this->request->request('pageNum', 10); // 分页
  975. // 分页搜索构建
  976. $pageStart = ($page - 1) * $pageNum;
  977. $res = \app\common\model\UserWithdrawalLog::where(["user_id" => $this->auth->id])->limit($pageStart, $pageNum)->order("createtime", "desc")->select();
  978. if ($res) {
  979. $statusTxt = array("-1" => "审核拒绝", "0" => "待审核", "1" => "审核通过");
  980. foreach ($res as $k => &$v) {
  981. $v["status"] = $statusTxt[$v["status"]];
  982. $v["money"] = $v["money"] > 0 ? $v["money"] / 100 : 0;
  983. $v["createtime"] = date("Y-m-d H:i:s", $v["createtime"]);
  984. }
  985. }
  986. $this->success("获取成功!", $res);
  987. }
  988. /**
  989. * 获取返利列表
  990. */
  991. public function getRebateList()
  992. {
  993. $pretype = $this->request->request('pretype', 0); // 下属类型:1=直属,2=下属
  994. if (!in_array($pretype, [0, 1, 2])) $this->error("参数错误!");
  995. $page = $this->request->request('page', 1); // 分页
  996. $pageNum = $this->request->request('pageNum', 10); // 分页
  997. // 分页搜索构建
  998. $pageStart = ($page - 1) * $pageNum;
  999. $where = [];
  1000. $where["a.user_id"] = $this->auth->id;
  1001. $pretype && $where["a.rebate_type"] = $pretype;
  1002. $where["a.is_rebate"] = 1;
  1003. $list = \app\common\model\RebateLog::alias("a")
  1004. ->field("a.*,u.nickname")
  1005. ->join("hx_user u", "u.id = a.by_user_id")
  1006. ->limit($pageStart, $pageNum)
  1007. ->order("createtime", "desc")
  1008. ->where($where)->select();
  1009. if ($list) foreach ($list as $k => &$v) {
  1010. $v["createtime"] = date("Y-m-d H:i:s", $v["createtime"]);
  1011. $v["nickname"] = mb_substr($v["nickname"], 0, 1, "utf-8") . "***";
  1012. }
  1013. $this->success("获取成功!", $list);
  1014. }
  1015. /**
  1016. * 获取剩余返利总额
  1017. */
  1018. public function getRebateMoney()
  1019. {
  1020. $this->success("获取成功!", \app\common\model\User::where(["id" => $this->auth->id])->value("rebate_money"));
  1021. }
  1022. /**
  1023. * 获取返利统计
  1024. */
  1025. public function getRebateCount()
  1026. {
  1027. // 直属推荐收益
  1028. $where = [];
  1029. $where["user_id"] = $this->auth->id;
  1030. $where["rebate_type"] = 1;
  1031. $preRebate = \app\common\model\RebateLog::where($where)->sum("rebate_money");
  1032. // 下属推荐收益
  1033. $where = [];
  1034. $where["user_id"] = $this->auth->id;
  1035. $where["rebate_type"] = 2;
  1036. $prepreRebate = \app\common\model\RebateLog::where($where)->sum("rebate_money");
  1037. // 上周收益总计
  1038. $preweek = $this->firstOfWeek(date("Y-m-d H:i:s",bcsub(time(),604800)));
  1039. $starttime = strtotime($preweek." 00:00:00"); // 上周一时间
  1040. $endtime = bcadd($starttime,604800); // 上周天时间
  1041. $where = [];
  1042. $where["user_id"] = $this->auth->id;
  1043. $where["createtime"] = ["between", "$starttime,$endtime"];
  1044. $weekRebate = \app\common\model\RebateLog::where($where)->sum("rebate_money");
  1045. // 历史收益总计
  1046. $where = [];
  1047. $where["user_id"] = $this->auth->id;
  1048. $where["createtime"] = ["lt", $starttime];
  1049. $allRebate = \app\common\model\RebateLog::where($where)->sum("rebate_money");
  1050. $res = [];
  1051. $res["preRebate"] = number_format($preRebate, 2);
  1052. $res["prepreRebate"] = number_format($prepreRebate, 2);
  1053. $res["weekRebate"] = number_format($weekRebate, 2);
  1054. $res["allRebate"] = number_format($allRebate, 2);
  1055. $this->success("获取成功!", $res);
  1056. }
  1057. /**
  1058. * 返利提现
  1059. */
  1060. public function rebateWithdraw()
  1061. {
  1062. $money = $this->request->request('money'); // 提现金额
  1063. if (!$money) $this->error("参数缺失!");
  1064. if ($money > 50000) $this->error("单笔上限50000元哦,超过50000元请联系客服处理!");
  1065. if ($money % 10 > 0) $this->error("提现金额必须为10的倍数!");
  1066. $user_id = $this->auth->id;
  1067. // 获取用户返利金额
  1068. $rebateMoney = \app\common\model\User::where(['id' => $user_id])->value("rebate_money");
  1069. if ($rebateMoney <= 100) $this->error("返利金额需要大于100元才能提现哦!");
  1070. $rdsMoney = bcsub($rebateMoney, $money);
  1071. if ($rdsMoney < 0) {
  1072. $this->error("余额不足,不能提现!", null, 100);
  1073. }
  1074. // 可以提现
  1075. Db::startTrans();
  1076. try {
  1077. $data = [];
  1078. $data["user_id"] = $user_id;
  1079. $data["money"] = $money;
  1080. $data["before"] = $rebateMoney;
  1081. $data["after"] = $rdsMoney;
  1082. $data["status"] = 1;
  1083. $data["createtime"] = time();
  1084. $res1 = \app\common\model\RebateWithdraw::insert($data);
  1085. $res2 = \app\common\model\User::update(["rebate_money" => $rdsMoney], ["id" => $user_id]);
  1086. if ($res1 && $res2) {
  1087. Db::commit();
  1088. $this->success("提现成功!");
  1089. } else {
  1090. $this->error("网络繁忙,请稍后重试!");
  1091. }
  1092. } catch (ValidateException $e) {
  1093. Db::rollback();
  1094. $this->error($e->getMessage());
  1095. } catch (PDOException $e) {
  1096. Db::rollback();
  1097. $this->error($e->getMessage());
  1098. } catch (Exception $e) {
  1099. Db::rollback();
  1100. $this->error($e->getMessage());
  1101. }
  1102. }
  1103. /**
  1104. * 提现余额记录验证
  1105. */
  1106. public function withdrawVerify($user_id)
  1107. {
  1108. if (!$user_id) return false;
  1109. // 统计用户所有兑换记录和提现记录 是否与余额匹配
  1110. $moneylog = \app\common\model\UserMoneyLog::where(["user_id" => $user_id, "mode" => "+"])->sum("money");
  1111. $withdraw = \app\common\model\UserWithdrawalLog::where(["user_id" => $user_id, "status" => ["in", [0, 1]]])->sum("money");
  1112. $withdraw = bcdiv($withdraw, 100,2);
  1113. $sum = bcsub($moneylog, $withdraw,2);
  1114. $usermoney = \app\common\model\User::where(["id" => $user_id])->value("money");
  1115. if ($usermoney == $sum) {
  1116. return true;
  1117. } else {
  1118. return false;
  1119. }
  1120. }
  1121. /**
  1122. * 获取声币记录
  1123. */
  1124. public function getMySoundCoinRecord()
  1125. {
  1126. $type = $this->request->request('type', 1); // 类型
  1127. $page = $this->request->request('page', 1); // 分页
  1128. $pageNum = $this->request->request('pageNum', 10); // 分页
  1129. // 分页搜索构建
  1130. $pageStart = ($page - 1) * $pageNum;
  1131. $where = [];
  1132. $where["user_id"] = $this->auth->id;
  1133. $where["obj_id"] = ['>', 0];
  1134. if ($type == 1) { //收益记录
  1135. $where["type"] = 1;
  1136. } else {// 提现记录,后台调整划到提现记录
  1137. $where["type"] = ['in', '2,3,10000'];
  1138. }
  1139. $res = \app\common\model\UserSoundcoinLog::field('id,type,obj_id,value,mode,detail,createtime')->where($where)->limit($pageStart, $pageNum)->order("createtime", "desc")->select();
  1140. if ($res) {
  1141. foreach ($res as $k => &$v) {
  1142. $subject = $v['detail'];
  1143. if ($v['type'] == 1) {
  1144. // 查询礼物赠送记录
  1145. $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();
  1146. $subject = $giftInfo['nickname'] . '送你';
  1147. $subject_notes = "{$giftInfo['gift_name']} * {$giftInfo['number']}";
  1148. } elseif ($v['type'] == 2) {
  1149. // 查询礼物赠送记录
  1150. $jewel = UserExchsoundLog::where('id', $v['obj_id'])->value('jewel');
  1151. $subject_notes = "已兑换{$jewel}砖石";
  1152. } elseif ($v['type'] == 3) {
  1153. // 查询礼物赠送记录
  1154. $money = UserExchangeLog::where('id', $v['obj_id'])->value('money');
  1155. $subject_notes = "已兑换{$money}余额";
  1156. } elseif ($v['type'] == 5 || $v['type'] == 10000) {
  1157. $modeText = $v['mode'] == '+' ? '调增' : '调减';
  1158. $subject_notes = "已{$modeText}{$v['value']}声币";
  1159. }
  1160. $v['subject'] = $subject;
  1161. $v['subject_notes'] = $subject_notes;
  1162. $v["createtime"] = date("m月d日 H时i分", $v["createtime"]);
  1163. }
  1164. }
  1165. $this->success("获取成功!", $res);
  1166. }
  1167. /**
  1168. * 内购参数验证
  1169. */
  1170. public function applebuy()
  1171. {
  1172. $receiptdata = $this->request->request('receiptdata'); // 验证参数
  1173. $rechar_id = $this->request->request("rechar_id");// 充值金额配置ID
  1174. if (!$receiptdata || !$rechar_id) $this->error("参数缺失!");
  1175. // 获取密钥配置
  1176. $configInfo = config("applebuy_url");
  1177. $applebuy = new applebuy($configInfo);
  1178. $ret = $applebuy->toSendVeify($receiptdata);
  1179. if (!$ret || !isset($ret["status"])) $this->error("网络请求失败,请稍后重试!");
  1180. if ($ret["status"] != 0) $this->error("支付验证失败!");
  1181. // print_r($ret);exit;
  1182. $user_id = $this->auth->id;
  1183. // 获取充值金额配置信息
  1184. $recharMode = new \app\common\model\RecharConfig();
  1185. $recharInfo = $recharMode->where(["id" => $rechar_id])->find();
  1186. if (!$recharInfo) $this->error("金额配置信息未找到!");
  1187. $amount = $recharInfo["money"] / 100;
  1188. $retkey = end($ret["receipt"]["in_app"]);
  1189. if ($retkey["product_id"] != "banshengvoice" . $amount) $this->error("支付验证失败!");
  1190. if (!$amount || $amount < 0) {
  1191. $this->error("支付金额必须大于0");
  1192. }
  1193. if($amount > 30){
  1194. $this->error("充值维护中");
  1195. }
  1196. $time = time();
  1197. $money = (float)bcadd($amount, 0, 2);
  1198. $recharOrderMode = new \app\common\model\RecharOrder();
  1199. $resrepeatid = $retkey["transaction_id"];
  1200. // 查询订单是否重复
  1201. $repeatOrderId = $recharOrderMode->where(["transaction_id" => $resrepeatid])->value("id");
  1202. if ($repeatOrderId > 0) $this->error("请勿重复操作!");
  1203. // 生成订单
  1204. $out_trade_no = date("YmdHis") . rand(100000, 999999);
  1205. $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)");
  1206. if (!$orderid) $this->error("订单创建失败!");
  1207. // 添加充值记录等
  1208. $dores = $this->applePayNotify($out_trade_no, $money);
  1209. if (!$dores) $this->error("状态更新失败,请联系客服!");
  1210. // 首充赠送
  1211. $userInfo = \app\common\model\User::get($user_id);
  1212. if ($userInfo->is_recharge == 0) { // 首充
  1213. Db::startTrans();
  1214. try {
  1215. // 赠送消息尾灯
  1216. $res1 = \app\common\model\AttireBack::addToMyBack(15, $user_id);
  1217. $userInfo->is_recharge = 1;
  1218. $res2 = $userInfo->save();
  1219. if ($res1 && $res2) Db::commit();
  1220. } catch (ValidateException $e) {
  1221. Db::rollback();
  1222. $this->error($e->getMessage());
  1223. }
  1224. }
  1225. $this->rebate($user_id, $amount);
  1226. // +EXP
  1227. \app\common\model\TaskLog::tofinish($user_id, "5EMwg7la", 1);
  1228. // 查询今日充值记录
  1229. $today = strtotime(date("Y-m-d 00:00:00"));
  1230. $rechargeMoney = \app\common\model\RecharOrder::where(["user_id" => $user_id, "createtime" => ["gt", $today]])->sum("money");
  1231. if ($rechargeMoney >= 100) {
  1232. // +EXP
  1233. \app\common\model\TaskLog::tofinish($user_id, "DaCVgOLu", 1);
  1234. }
  1235. // 查询新人礼包
  1236. $this->addBagGift($user_id, $amount);
  1237. $this->success("购买成功!");
  1238. }
  1239. /**
  1240. * 内购逻辑
  1241. */
  1242. private function applePayNotify($out_trade_no, $payamount)
  1243. {
  1244. Db::startTrans();
  1245. try {
  1246. $orderModel = new \app\common\model\RecharOrder();
  1247. $userModel = new \app\common\model\User();
  1248. $jewellogModel = new \app\common\model\UserJewelLog();
  1249. $rechargelogModel = new \app\common\model\UserRechargeLog();
  1250. $orderInfo = $orderModel->where(["order_no" => $out_trade_no])->find();
  1251. $userInfo = $userModel->where(["id" => $orderInfo["user_id"]])->find();
  1252. // 修改订单状态
  1253. $res1 = $orderModel->update(["status" => 1, 'platform' => 2], ["order_no" => $out_trade_no]);
  1254. // 修改用户钻石余额
  1255. $res2 = $userModel->where(["id" => $orderInfo["user_id"]])->setInc("jewel", $orderInfo["jewel"]);
  1256. // 添加钻石流水记录
  1257. $detail = "充值";
  1258. $res3 = $jewellogModel->addUserJewelLog($userInfo["id"], $orderInfo["jewel"], "+", $userInfo["jewel"], $detail, 1);
  1259. $rechargeCount = $rechargelogModel->where('user_id',$orderInfo["user_id"])->count();
  1260. $isFirstRecharge = $rechargeCount > 0 ? 0 : 1;
  1261. // 添加充值记录
  1262. $res4 = $rechargelogModel->addRecord($userInfo["id"], $orderInfo["jewel"], $payamount, bcadd($userInfo["jewel"], $orderInfo["jewel"], 2), $userInfo["money"], 2, 4, $isFirstRecharge, $userInfo["pre_userid"]);
  1263. if ($res1 && $res2 && $res3 && $res4) {
  1264. // 添加统计
  1265. $time = time();
  1266. (new \app\common\model\User)->execute("update hx_user set chargecount = chargecount + $payamount,chargetime = $time where id = " . $userInfo["id"]);
  1267. Db::commit();
  1268. return true;
  1269. }
  1270. } catch (ValidateException $e) {
  1271. Db::rollback();
  1272. return false;
  1273. } catch (PDOException $e) {
  1274. Db::rollback();
  1275. return false;
  1276. } catch (Exception $e) {
  1277. Db::rollback();
  1278. return false;
  1279. }
  1280. }
  1281. /**
  1282. * 获取我的推荐列表
  1283. */
  1284. public function getMyUsers() {
  1285. $thispage = $this->request->request('thispage',1); // 当前页
  1286. $pageNum = $this->request->request('pageNum', 10); // 分页
  1287. $page = [];
  1288. $page["page"] = $thispage;
  1289. $page["list_rows"] = $pageNum;
  1290. $user_id = $this->auth->id;
  1291. $userList = \app\common\model\User::field("id,nickname,u_id,bindtime")->where(["pre_userid"=>$user_id])->paginate($page);
  1292. if($userList) foreach($userList as $k => $v) $userList[$k]["bindtime"] = date("Y-m-d H:i:s",$v["bindtime"]);
  1293. $this->success("获取成功!",$userList);
  1294. }
  1295. /**
  1296. * 获取下级/下下级充值记录
  1297. */
  1298. public function getRechargeLog() {
  1299. $user_type = $this->request->request('user_type'); // 类型:1=直属,2=下属
  1300. $thispage = $this->request->request('thispage',1); // 当前页
  1301. $user_id = $this->auth->id;
  1302. $users = \app\common\model\User::where(["pre_userid"=>$user_id])->column("id");
  1303. if(!$users) $this->success("获取成功!",$users);
  1304. $ids = $users;
  1305. // 下下级
  1306. if($user_type == 2) {
  1307. $usersusers = [];
  1308. foreach($users as $v) {
  1309. $user_id = $v;
  1310. $userspre = \app\common\model\User::where(["pre_userid"=>$user_id])->column("id");
  1311. if(!$userspre) continue;
  1312. foreach($userspre as $n) {
  1313. $usersusers[] = $n;
  1314. }
  1315. }
  1316. $ids = $usersusers;
  1317. }
  1318. // 上个周一
  1319. $preweek = $this->firstOfWeek(date("Y-m-d H:i:s",bcsub(time(),604800)));
  1320. $starttime = strtotime($preweek." 00:00:00"); // 上周时间
  1321. $page = [];
  1322. $page["page"] = $thispage;
  1323. $page["list_rows"] = 10;
  1324. $where = [];
  1325. $where["a.user_id"] = ["in",$ids];
  1326. $where["a.createtime"] = ["gt",$starttime];
  1327. // 获取充值记录
  1328. $rechargeList = \app\common\model\UserRechargeLog::alias("a")
  1329. ->field("a.id,a.user_id,u.nickname,u.bindtime,a.createtime,a.money")
  1330. ->join("hx_user u","u.id = a.user_id")
  1331. ->where($where)
  1332. ->order("a.createtime","desc")
  1333. ->paginate($page);
  1334. if($rechargeList) foreach($rechargeList as $k => $v) {
  1335. if($v["createtime"] < $v["bindtime"]) {
  1336. unset($rechargeList[$k]);
  1337. continue;
  1338. }
  1339. $rechargeList[$k]["createtime"] = date("Y-m-d H:i:s",$v["createtime"]);
  1340. $rechargeList[$k]["nickname"] = mb_substr($v["nickname"], 0, 1, "utf-8") . "***";
  1341. }
  1342. $this->success("获取成功!",$rechargeList);
  1343. }
  1344. /**
  1345. * 获取上周收益
  1346. */
  1347. public function getLastweekProfit() {
  1348. $user_id = $this->auth->id;
  1349. $result = [];
  1350. // 上个周一
  1351. $preweek = $this->firstOfWeek(date("Y-m-d H:i:s",bcsub(time(),604800)));
  1352. $starttime = strtotime($preweek." 00:00:00"); // 上周一时间
  1353. $endtime = bcadd($starttime,604800); // 上周天时间
  1354. // 直属收益
  1355. $where = [];
  1356. $where["user_id"] = $user_id;
  1357. $where["is_rebate"] = 1;
  1358. $where["rebate_type"] = 1;
  1359. $where["createtime"] = ["between",[$starttime,$endtime]];
  1360. $rebate_rate = \app\common\model\RebateLog::where($where)->value("rebate_rate");
  1361. $rebate_rate = bcmul($rebate_rate,100,2);
  1362. $result["pre"]["money"] = \app\common\model\RebateLog::where($where)->sum("money");
  1363. $result["pre"]["rate"] = (float)$rebate_rate;
  1364. $result["pre"]["rebate_money"] = \app\common\model\RebateLog::where($where)->sum("rebate_money");
  1365. // 下属收益
  1366. $where = [];
  1367. $where["user_id"] = $user_id;
  1368. $where["is_rebate"] = 1;
  1369. $where["rebate_type"] = 2;
  1370. $where["createtime"] = ["between",[$starttime,$endtime]];
  1371. $rebate_rate = \app\common\model\RebateLog::where($where)->value("rebate_rate");
  1372. $rebate_rate = bcmul($rebate_rate,100,2);
  1373. $result["prepre"]["money"] = \app\common\model\RebateLog::where($where)->sum("money");
  1374. $result["prepre"]["rate"] = (float)$rebate_rate;
  1375. $result["prepre"]["rebate_money"] = \app\common\model\RebateLog::where($where)->sum("rebate_money");
  1376. // 上周收益总计
  1377. $result['weekRebate'] = (float)bcadd($result["pre"]["rebate_money"],$result["prepre"]["rebate_money"],2);
  1378. $this->success("获取成功!",$result);
  1379. }
  1380. }