Money.php 75 KB

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