Money.php 74 KB

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