Money.php 64 KB

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