Money.php 75 KB

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