Money.php 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913
  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. $userwallet = Db::name('user_wallet')->where('user_id',$this->auth->id)->find();
  870. if ($exchangeMoney > $userwallet["money"]) {
  871. throw new Exception("余额不足!");
  872. }
  873. //
  874. $detail = '余额兑换钻石';
  875. //减去用户余额
  876. $rs_wallet = model('wallet')->lockChangeAccountRemain($this->auth->id,$exchangeMoney,'-',0,$detail,114,'money');
  877. if($rs_wallet['status'] == false){
  878. Db::rollback();
  879. $this->error($rs_wallet['msg']);
  880. }
  881. //增加用户钻石余额
  882. $rs_wallet = model('wallet')->lockChangeAccountRemain($this->auth->id, $scoin, '+', 0, $detail, 14,'jewel');
  883. if($rs_wallet['status'] == false){
  884. Db::rollback();
  885. $this->error($rs_wallet['msg']);
  886. }
  887. // 添加兑换记录
  888. $exchangelogModel = new \app\common\model\UserExchangeLog();
  889. $res2 = $exchangelogModel->addExchangeLog($userId, $scoin, $exchangeMoney);
  890. if ($res2) {
  891. Db::commit();
  892. $this->success("兑换成功!");
  893. } else {
  894. $this->error("操作失败!");
  895. }
  896. } catch (ValidateException $e) {
  897. Db::rollback();
  898. $this->error($e->getMessage());
  899. } catch (PDOException $e) {
  900. Db::rollback();
  901. $this->error($e->getMessage());
  902. } catch (Exception $e) {
  903. Db::rollback();
  904. $this->error($e->getMessage());
  905. }
  906. }
  907. // /**
  908. // * 兑换
  909. // */
  910. // public function exchange() {
  911. // $exchange_id = $this->request->request("exchange_id");// 兑换钻石配置ID
  912. // if(!$exchange_id) {
  913. // $this->error(__('Invalid parameters'));
  914. // }
  915. //
  916. // // 获取兑换配置信息
  917. // $exchangeModel = new \app\common\model\ExchangeConfig();
  918. // $exchangeInfo = $exchangeModel->get($exchange_id);
  919. // if(!$exchangeInfo) $this->error("配置信息未找到");
  920. // $exchangeMoney = $exchangeInfo["money"]/100;
  921. // // 查询钻石余额
  922. // $userModel = new \app\common\model\User();
  923. // $userInfo = $userModel->get($this->auth->id);
  924. // if($exchangeInfo["jewel"] > $userInfo["jewel"]) {
  925. // $this->error("钻石余额不足!",[],100);
  926. // }
  927. //
  928. // Db::startTrans();
  929. // try{
  930. // $moneylogModel = new \app\common\model\UserMoneyLog();
  931. // $jewellogModel = new \app\common\model\UserJewelLog();
  932. // $exchangelogModel = new \app\common\model\UserExchangeLog();
  933. // $detail = "兑换";
  934. // // 添加资金流水记录
  935. // $res1 = $moneylogModel->addRecord($this->auth->id,$exchangeMoney,"-",$userInfo["money"],$detail);
  936. // // 添加钻石流水记录
  937. // $res2 = $jewellogModel->addUserJewelLog($this->auth->id,$exchangeMoney,"+",$userInfo["money"],$detail);
  938. // // 添加兑换记录
  939. // $res3 = $exchangelogModel->addExchangeLog($this->auth->id,$exchangeInfo["jewel"],$exchangeMoney);
  940. // // 减去用户钻石余额
  941. // $res4 = $userModel->where(["id"=>$this->auth->id])->setDec("jewel",$exchangeInfo["jewel"]);
  942. // // 增加用户资金余额
  943. // $res5 = $userModel->where(["id"=>$this->auth->id])->setInc("money",$exchangeMoney);
  944. // if($res1 && $res2 && $res3 && $res4 && $res5) {
  945. // Db::commit();
  946. // $this->success("兑换成功!");
  947. // } else {
  948. // $this->error("操作失败!");
  949. // }
  950. // }catch (ValidateException $e) {
  951. // Db::rollback();
  952. // $this->error($e->getMessage());
  953. // } catch (PDOException $e) {
  954. // Db::rollback();
  955. // $this->error($e->getMessage());
  956. // } catch (Exception $e) {
  957. // Db::rollback();
  958. // $this->error($e->getMessage());
  959. // }
  960. // }
  961. /**
  962. * 提现
  963. */
  964. public function withdrow()
  965. {
  966. $money = $this->request->request("money");// 申请提现的金额
  967. if ($money <= 0) {
  968. $this->error(__('提现金额不合法'));
  969. }
  970. // 判断当前用户是否实名认证
  971. $userAuthInfo = \app\common\model\UserAuth::where(["user_id" => $this->auth->id])->find();
  972. if ($userAuthInfo) {
  973. if ($userAuthInfo->status == 0) {
  974. $this->error("您的实名认证还在这审核中...,请耐心等待!");
  975. } elseif ($userAuthInfo->status == 2) {
  976. $this->error("您的实名认证审核未通过,请重新审核!");
  977. }
  978. } else {
  979. $this->error("请先申请实名认证!");
  980. }
  981. // 查询资金余额
  982. $userModel = new \app\common\model\User();
  983. $userInfo = $userModel->get($this->auth->id);
  984. if ($money > $userInfo["money"]) {
  985. $this->error("资金余额不足!", [], 100);
  986. }
  987. Db::startTrans();
  988. try {
  989. $withdrawallogModel = new \app\common\model\UserWithdrawalLog();
  990. // 减去用户可用资金余额
  991. $res1 = $userModel->where(["id" => $this->auth->id])->setDec("money", $money);
  992. // 增加用户冻结资金余额
  993. $res2 = $userModel->where(["id" => $this->auth->id])->setInc("frozen", $money);
  994. // 新增用户提现记录申请
  995. $data = [];
  996. $data["user_id"] = $this->auth->id;
  997. $data["money"] = $money * 100;
  998. $data["status"] = 0; // 提现状态:-1=审核拒绝,0=待审核,1=审核通过
  999. $data["createtime"] = time();
  1000. $res3 = $withdrawallogModel->insert($data);
  1001. if ($res1 && $res2 && $res3) {
  1002. Db::commit();
  1003. $this->success("提现申请发送成功!");
  1004. } else {
  1005. $this->error("操作失败!");
  1006. }
  1007. } catch (ValidateException $e) {
  1008. Db::rollback();
  1009. $this->error($e->getMessage());
  1010. } catch (PDOException $e) {
  1011. Db::rollback();
  1012. $this->error($e->getMessage());
  1013. } catch (Exception $e) {
  1014. Db::rollback();
  1015. $this->error($e->getMessage());
  1016. }
  1017. }
  1018. /**
  1019. * 获取充值金额配置信息
  1020. */
  1021. public function getRecharConfig()
  1022. {
  1023. $params_from = $this->request->request("params_from",'');// 平台参数
  1024. $where = [];
  1025. if($params_from == 'ios'){
  1026. //$where['money'] = ['<=', 3000];
  1027. }
  1028. $list = \app\common\model\RecharConfig::where($where)->order("weight", "asc")->select();
  1029. if (!$list) $this->error("配置信息为空!");
  1030. foreach ($list as $k => $v) {
  1031. $money = $v["money"] / 100;
  1032. $list[$k]["money"] = $money;
  1033. $list[$k]["desId"] = "banshengvoice" . $money;
  1034. if ($params_from != "ios") $list[$k]["jewel"] = $v["jewel_full"];
  1035. $list[$k]['ratio'] = $list[$k]["jewel"] / $money;
  1036. unset($list[$k]["jewel_full"]);
  1037. }
  1038. $this->success("获取成功!", $list);
  1039. }
  1040. /**
  1041. * 获取声币兑换余额配置信息
  1042. */
  1043. public function getExchangeConfig()
  1044. {
  1045. $list = \app\common\model\ExchangeConfig::field("id,sound_coin,money,updatetime,createtime")->select();
  1046. if (!$list) $this->error("配置信息为空!");
  1047. foreach ($list as $k => $v) $list[$k]["money"] = $v["money"] / 100;
  1048. $this->success("获取成功!", $list);
  1049. }
  1050. /**
  1051. * 获取钻石兑换余额配置信息
  1052. */
  1053. public function getExchmoneyConfig()
  1054. {
  1055. $list = \app\common\model\ExchmoneyConfig::field("id,jewel,money")->select();
  1056. if (!$list) $this->error("配置信息为空!");
  1057. foreach ($list as $k => $v) {
  1058. $list[$k]["id"] = (int)$v["id"];
  1059. $list[$k]["jewel"] = (int)$v["jewel"];
  1060. $list[$k]["money"] = $v["money"] / 100;
  1061. }
  1062. //设置是否使用自定义
  1063. $rechargeDefine = config('site.exchange_define');
  1064. if ($rechargeDefine == 1) {
  1065. $fieldArr = [[
  1066. 'id' => -1,
  1067. 'jewel' => 0,
  1068. 'money' => 0.00,
  1069. ],];
  1070. $list = array_merge($list,$fieldArr);
  1071. }
  1072. $this->success("获取成功!", $list);
  1073. }
  1074. /**
  1075. * 获取声币兑换钻石配置信息
  1076. */
  1077. public function getExchsoundConfig()
  1078. {
  1079. $list = \app\common\model\ExchsoundConfig::field("id,sound_coin,jewel,updatetime,createtime")->select();
  1080. if (!$list) $this->error("配置信息为空!");
  1081. $this->success("获取成功!", $list);
  1082. }
  1083. /**
  1084. * 获取用户充值记录
  1085. */
  1086. public function rechargeRecord()
  1087. {
  1088. $page = $this->request->request('page', 1); // 分页
  1089. $pageNum = $this->request->request('pageNum', 10); // 分页
  1090. // 分页搜索构建
  1091. $pageStart = ($page - 1) * $pageNum;
  1092. $res = \app\common\model\UserRechargeLog::where(["user_id" => $this->auth->id])->limit($pageStart, $pageNum)->order("createtime", "desc")->select();
  1093. if ($res): foreach ($res as $k => &$v) $v["createtime"] = date("Y-m-d H:i:s", $v["createtime"]);endif;
  1094. $this->success("获取成功!", $res);
  1095. }
  1096. /**
  1097. * 获取用户兑换记录
  1098. */
  1099. public function exchangeRecord()
  1100. {
  1101. $page = $this->request->request('page', 1); // 分页
  1102. $pageNum = $this->request->request('pageNum', 10); // 分页
  1103. // 分页搜索构建
  1104. $pageStart = ($page - 1) * $pageNum;
  1105. $res = \app\common\model\UserExchangeLog::field("*,sound_coin as jewel")->where(["user_id" => $this->auth->id])->limit($pageStart, $pageNum)->order("createtime", "desc")->select();
  1106. if ($res): foreach ($res as $k => &$v) $v["createtime"] = date("Y-m-d H:i:s", $v["createtime"]);endif;
  1107. $this->success("获取成功!", $res);
  1108. }
  1109. /**
  1110. * 获取用户声币兑换钻石记录
  1111. */
  1112. public function exchsoundRecord()
  1113. {
  1114. $page = $this->request->request('page', 1); // 分页
  1115. $pageNum = $this->request->request('pageNum', 10); // 分页
  1116. // 分页搜索构建
  1117. $pageStart = ($page - 1) * $pageNum;
  1118. $res = \app\common\model\UserExchsoundLog::where(["user_id" => $this->auth->id])->limit($pageStart, $pageNum)->order("createtime", "desc")->select();
  1119. if ($res): foreach ($res as $k => &$v) $v["createtime"] = date("Y-m-d H:i:s", $v["createtime"]);endif;
  1120. $this->success("获取成功!", $res);
  1121. }
  1122. /**
  1123. * 获取用户提现记录
  1124. */
  1125. public function withdrowRecord()
  1126. {
  1127. $page = $this->request->request('page', 1); // 分页
  1128. $pageNum = $this->request->request('pageNum', 10); // 分页
  1129. // 分页搜索构建
  1130. $pageStart = ($page - 1) * $pageNum;
  1131. $res = \app\common\model\UserWithdrawalLog::where(["user_id" => $this->auth->id])->limit($pageStart, $pageNum)->order("createtime", "desc")->select();
  1132. if ($res) {
  1133. $statusTxt = array("-1" => "审核拒绝", "0" => "待审核", "1" => "审核通过");
  1134. foreach ($res as $k => &$v) {
  1135. $v["status"] = $statusTxt[$v["status"]];
  1136. $v["money"] = $v["money"] > 0 ? $v["money"] / 100 : 0;
  1137. $v["createtime"] = date("Y-m-d H:i:s", $v["createtime"]);
  1138. }
  1139. }
  1140. $this->success("获取成功!", $res);
  1141. }
  1142. /**
  1143. * 获取返利列表
  1144. */
  1145. public function getRebateList()
  1146. {
  1147. $pretype = $this->request->request('pretype', 0); // 下属类型:1=直属,2=下属
  1148. if (!in_array($pretype, [0, 1, 2])) $this->error("参数错误!");
  1149. $page = $this->request->request('page', 1); // 分页
  1150. $pageNum = $this->request->request('pageNum', 10); // 分页
  1151. // 分页搜索构建
  1152. $pageStart = ($page - 1) * $pageNum;
  1153. $where = [];
  1154. $where["a.user_id"] = $this->auth->id;
  1155. $pretype && $where["a.rebate_type"] = $pretype;
  1156. $where["a.is_rebate"] = 1;
  1157. $list = \app\common\model\RebateLog::alias("a")
  1158. ->field("a.*,u.nickname")
  1159. ->join("hx_user u", "u.id = a.by_user_id")
  1160. ->limit($pageStart, $pageNum)
  1161. ->order("createtime", "desc")
  1162. ->where($where)->select();
  1163. if ($list) foreach ($list as $k => &$v) {
  1164. $v["createtime"] = date("Y-m-d H:i:s", $v["createtime"]);
  1165. $v["nickname"] = mb_substr($v["nickname"], 0, 1, "utf-8") . "***";
  1166. }
  1167. $this->success("获取成功!", $list);
  1168. }
  1169. /**
  1170. * 获取剩余返利总额
  1171. */
  1172. public function getRebateMoney()
  1173. {
  1174. $this->success("获取成功!", \app\common\model\User::where(["id" => $this->auth->id])->value("rebate_money"));
  1175. }
  1176. /**
  1177. * 获取返利统计
  1178. */
  1179. public function getRebateCount()
  1180. {
  1181. // 直属推荐收益
  1182. $where = [];
  1183. $where["user_id"] = $this->auth->id;
  1184. $where["rebate_type"] = 1;
  1185. $preRebate = \app\common\model\RebateLog::where($where)->sum("rebate_money");
  1186. // 下属推荐收益
  1187. $where = [];
  1188. $where["user_id"] = $this->auth->id;
  1189. $where["rebate_type"] = 2;
  1190. $prepreRebate = \app\common\model\RebateLog::where($where)->sum("rebate_money");
  1191. // 上周收益总计
  1192. $preweek = $this->firstOfWeek(date("Y-m-d H:i:s",bcsub(time(),604800)));
  1193. $starttime = strtotime($preweek." 00:00:00"); // 上周一时间
  1194. $endtime = bcadd($starttime,604800); // 上周天时间
  1195. $where = [];
  1196. $where["user_id"] = $this->auth->id;
  1197. $where["createtime"] = ["between", "$starttime,$endtime"];
  1198. $weekRebate = \app\common\model\RebateLog::where($where)->sum("rebate_money");
  1199. // 历史收益总计
  1200. $where = [];
  1201. $where["user_id"] = $this->auth->id;
  1202. $where["createtime"] = ["lt", $starttime];
  1203. $allRebate = \app\common\model\RebateLog::where($where)->sum("rebate_money");
  1204. $res = [];
  1205. $res["preRebate"] = number_format($preRebate, 2);
  1206. $res["prepreRebate"] = number_format($prepreRebate, 2);
  1207. $res["weekRebate"] = number_format($weekRebate, 2);
  1208. $res["allRebate"] = number_format($allRebate, 2);
  1209. $this->success("获取成功!", $res);
  1210. }
  1211. /**
  1212. * 返利提现
  1213. */
  1214. public function rebateWithdraw()
  1215. {
  1216. $money = $this->request->request('money'); // 提现金额
  1217. if (!$money) $this->error("参数缺失!");
  1218. if ($money > 50000) $this->error("单笔上限50000元哦,超过50000元请联系客服处理!");
  1219. if ($money % 10 > 0) $this->error("提现金额必须为10的倍数!");
  1220. $user_id = $this->auth->id;
  1221. // 获取用户返利金额
  1222. $rebateMoney = \app\common\model\User::where(['id' => $user_id])->value("rebate_money");
  1223. if ($rebateMoney <= 100) $this->error("返利金额需要大于100元才能提现哦!");
  1224. $rdsMoney = bcsub($rebateMoney, $money);
  1225. if ($rdsMoney < 0) {
  1226. $this->error("余额不足,不能提现!", null, 100);
  1227. }
  1228. // 可以提现
  1229. Db::startTrans();
  1230. try {
  1231. $data = [];
  1232. $data["user_id"] = $user_id;
  1233. $data["money"] = $money;
  1234. $data["before"] = $rebateMoney;
  1235. $data["after"] = $rdsMoney;
  1236. $data["status"] = 1;
  1237. $data["createtime"] = time();
  1238. $res1 = \app\common\model\RebateWithdraw::insert($data);
  1239. $res2 = \app\common\model\User::update(["rebate_money" => $rdsMoney], ["id" => $user_id]);
  1240. if ($res1 && $res2) {
  1241. Db::commit();
  1242. $this->success("提现成功!");
  1243. } else {
  1244. $this->error("网络繁忙,请稍后重试!");
  1245. }
  1246. } catch (ValidateException $e) {
  1247. Db::rollback();
  1248. $this->error($e->getMessage());
  1249. } catch (PDOException $e) {
  1250. Db::rollback();
  1251. $this->error($e->getMessage());
  1252. } catch (Exception $e) {
  1253. Db::rollback();
  1254. $this->error($e->getMessage());
  1255. }
  1256. }
  1257. /**
  1258. * 提现余额记录验证
  1259. */
  1260. public function withdrawVerify($user_id)
  1261. {
  1262. if (!$user_id) return false;
  1263. // 统计用户所有兑换记录和提现记录 是否与余额匹配
  1264. $moneylog = \app\common\model\UserMoneyLog::where(["user_id" => $user_id, "mode" => "+"])->sum('value');
  1265. $withdraw = \app\common\model\UserWithdrawalLog::where(["user_id" => $user_id, "status" => ["in", [0, 1]]])->sum("money");
  1266. $withdraw = bcdiv($withdraw, 100,2);
  1267. $sum = bcsub($moneylog, $withdraw,2);
  1268. $usermoney = \app\common\model\User::where(["id" => $user_id])->value("money");
  1269. if ($usermoney == $sum) {
  1270. return true;
  1271. } else {
  1272. return false;
  1273. }
  1274. }
  1275. /**
  1276. * 获取声币记录
  1277. */
  1278. public function getMySoundCoinRecord()
  1279. {
  1280. $type = $this->request->request('type', 1); // 类型
  1281. $page = $this->request->request('page', 1); // 分页
  1282. $pageNum = $this->request->request('pageNum', 10); // 分页
  1283. // 分页搜索构建
  1284. $pageStart = ($page - 1) * $pageNum;
  1285. $where = [];
  1286. $where["user_id"] = $this->auth->id;
  1287. $where["obj_id"] = ['>', 0];
  1288. if ($type == 1) { //收益记录
  1289. $where["type"] = 1;
  1290. } else {// 提现记录,后台调整划到提现记录
  1291. $where["type"] = ['in', '2,3,10000'];
  1292. }
  1293. $res = \app\common\model\UserSoundcoinLog::field('id,type,obj_id,value,mode,detail,createtime')->where($where)->limit($pageStart, $pageNum)->order("createtime", "desc")->select();
  1294. if ($res) {
  1295. foreach ($res as $k => &$v) {
  1296. $subject = $v['detail'];
  1297. if ($v['type'] == 1) {
  1298. // 查询礼物赠送记录
  1299. $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();
  1300. $subject = $giftInfo['nickname'] . '送你';
  1301. $subject_notes = "{$giftInfo['gift_name']} * {$giftInfo['number']}";
  1302. } elseif ($v['type'] == 2) {
  1303. // 查询礼物赠送记录
  1304. $jewel = UserExchsoundLog::where('id', $v['obj_id'])->value('jewel');
  1305. $subject_notes = "已兑换{$jewel}砖石";
  1306. } elseif ($v['type'] == 3) {
  1307. // 查询礼物赠送记录
  1308. $money = UserExchangeLog::where('id', $v['obj_id'])->value('money');
  1309. $subject_notes = "已兑换{$money}余额";
  1310. } elseif ($v['type'] == 5 || $v['type'] == 10000) {
  1311. $modeText = $v['mode'] == '+' ? '调增' : '调减';
  1312. $subject_notes = "已{$modeText}{$v['value']}声币";
  1313. }
  1314. $v['subject'] = $subject;
  1315. $v['subject_notes'] = $subject_notes;
  1316. $v["createtime"] = date("m月d日 H时i分", $v["createtime"]);
  1317. }
  1318. }
  1319. $this->success("获取成功!", $res);
  1320. }
  1321. /**
  1322. * 获取收益
  1323. * @return void
  1324. */
  1325. public function getMySoundCoinRecordNew()
  1326. {
  1327. try {
  1328. $userId = $this->auth->id;
  1329. $field = 'id,type,detail,mode,value,createtime';
  1330. $where['user_id'] = $userId;
  1331. $where['mode'] = '+';
  1332. //$where['type'] = [101,102,103,105,106];//101=>'获赠礼物',102=>'房间礼物抽成',103=>'邀请推广',105=>'申请提现驳回',106=>'接单收益',
  1333. $result = model('UserMoneyLog')->field($field)->where($where)->autopage()->order('createtime desc')->select();
  1334. if (!empty($result)) {
  1335. $logType = config('wallet.logtype');
  1336. foreach ($result as $key => &$value) {
  1337. $value['type_text'] = isset($logType[$value['type']]) ? $logType[$value['type']] : '';
  1338. $value['createtime'] = !empty($value['createtime']) ? date('Y-m-d H:i',$value['createtime']) : '';
  1339. }
  1340. }
  1341. $this->success('获取成功',$result);
  1342. } catch (Exception $e) {
  1343. $this->error($e->getMessage());
  1344. }
  1345. }
  1346. /**
  1347. * 内购参数验证
  1348. */
  1349. public function applebuy()
  1350. {
  1351. $receiptdata = $this->request->request('receiptdata'); // 验证参数
  1352. $rechar_id = $this->request->request("rechar_id");// 充值金额配置ID
  1353. if (!$receiptdata || !$rechar_id) $this->error("参数缺失!");
  1354. // 获取密钥配置
  1355. $configInfo = config("applebuy_url");
  1356. $applebuy = new applebuy($configInfo);
  1357. $ret = $applebuy->toSendVeify($receiptdata);
  1358. if (!$ret || !isset($ret["status"])) $this->error("网络请求失败,请稍后重试!");
  1359. if ($ret["status"] != 0) $this->error("支付验证失败!");
  1360. // print_r($ret);exit;
  1361. $user_id = $this->auth->id;
  1362. // 获取充值金额配置信息
  1363. $recharMode = new \app\common\model\RecharConfig();
  1364. $recharInfo = $recharMode->where(["id" => $rechar_id])->find();
  1365. if (!$recharInfo) $this->error("金额配置信息未找到!");
  1366. $amount = $recharInfo["money"] / 100;
  1367. $retkey = end($ret["receipt"]["in_app"]);
  1368. if ($retkey["product_id"] != "banshengvoice" . $amount) $this->error("支付验证失败!");
  1369. if (!$amount || $amount < 0) {
  1370. $this->error("支付金额必须大于0");
  1371. }
  1372. if($amount > 30){
  1373. $this->error("充值维护中");
  1374. }
  1375. $time = time();
  1376. $money = (float)bcadd($amount, 0, 2);
  1377. $recharOrderMode = new \app\common\model\RecharOrder();
  1378. $resrepeatid = $retkey["transaction_id"];
  1379. // 查询订单是否重复
  1380. $repeatOrderId = $recharOrderMode->where(["transaction_id" => $resrepeatid])->value("id");
  1381. if ($repeatOrderId > 0) $this->error("请勿重复操作!");
  1382. // 生成订单
  1383. $out_trade_no = date("YmdHis") . rand(100000, 999999);
  1384. $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)");
  1385. if (!$orderid) $this->error("订单创建失败!");
  1386. // 添加充值记录等
  1387. $dores = $this->applePayNotify($out_trade_no, $money);
  1388. if (!$dores) $this->error("状态更新失败,请联系客服!");
  1389. // 首充赠送
  1390. $userInfo = \app\common\model\User::get($user_id);
  1391. if ($userInfo->is_recharge == 0) { // 首充
  1392. Db::startTrans();
  1393. try {
  1394. // 赠送消息尾灯
  1395. $res1 = \app\common\model\AttireBack::addToMyBack(15, $user_id);
  1396. $userInfo->is_recharge = 1;
  1397. $res2 = $userInfo->save();
  1398. if ($res1 && $res2) Db::commit();
  1399. } catch (ValidateException $e) {
  1400. Db::rollback();
  1401. $this->error($e->getMessage());
  1402. }
  1403. }
  1404. //$this->rebate($user_id, $amount);
  1405. // +EXP
  1406. \app\common\model\TaskLog::tofinish($user_id, "5EMwg7la", 1);
  1407. // 查询今日充值记录
  1408. $today = strtotime(date("Y-m-d 00:00:00"));
  1409. $rechargeMoney = \app\common\model\RecharOrder::where(["user_id" => $user_id, "createtime" => ["gt", $today]])->sum("money");
  1410. if ($rechargeMoney >= 100) {
  1411. // +EXP
  1412. \app\common\model\TaskLog::tofinish($user_id, "DaCVgOLu", 1);
  1413. }
  1414. // 查询新人礼包
  1415. //$this->addBagGift($user_id, $amount);
  1416. $this->success("购买成功!");
  1417. }
  1418. /**
  1419. * 内购逻辑
  1420. */
  1421. private function applePayNotify($out_trade_no, $payamount)
  1422. {
  1423. Db::startTrans();
  1424. try {
  1425. $orderModel = new \app\common\model\RecharOrder();
  1426. $userModel = new \app\common\model\User();
  1427. $rechargelogModel = new \app\common\model\UserRechargeLog();
  1428. $orderInfo = $orderModel->where(["order_no" => $out_trade_no])->find();
  1429. $userInfo = $userModel->where(["id" => $orderInfo["user_id"]])->find();
  1430. $userwallet = Db::name('user_wallet')->where('user_id',$orderInfo["user_id"])->find();
  1431. // 修改订单状态
  1432. $res1 = $orderModel->update(["status" => 1, 'platform' => 2], ["order_no" => $out_trade_no]);
  1433. // 修改用户钻石余额
  1434. $res2 = true;
  1435. // 添加钻石流水记录
  1436. $detail = "充值";
  1437. $res3 = true;
  1438. //增加用户钻石余额
  1439. $rs_wallet = model('wallet')->lockChangeAccountRemain($userInfo["id"], $orderInfo["jewel"], "+", 0, $detail, 1,'jewel');
  1440. if($rs_wallet['status'] == false){
  1441. Db::rollback();
  1442. $this->error($rs_wallet['msg']);
  1443. }
  1444. //
  1445. $rechargeCount = $rechargelogModel->where('user_id',$orderInfo["user_id"])->count();
  1446. $isFirstRecharge = $rechargeCount > 0 ? 0 : 1;
  1447. // 添加充值记录
  1448. $res4 = $rechargelogModel->addRecord($userInfo["id"], $orderInfo["jewel"], $payamount, bcadd($userwallet["jewel"], $orderInfo["jewel"], 2), $userwallet["money"], 2, 4, $isFirstRecharge, $userInfo["pre_userid"]);
  1449. if ($res1 && $res2 && $res3 && $res4) {
  1450. // 添加统计
  1451. $time = time();
  1452. (new \app\common\model\User)->execute("update hx_user set chargecount = chargecount + $payamount,chargetime = $time where id = " . $userInfo["id"]);
  1453. Db::commit();
  1454. return true;
  1455. }
  1456. } catch (ValidateException $e) {
  1457. Db::rollback();
  1458. return false;
  1459. } catch (PDOException $e) {
  1460. Db::rollback();
  1461. return false;
  1462. } catch (Exception $e) {
  1463. Db::rollback();
  1464. return false;
  1465. }
  1466. }
  1467. /**
  1468. * 获取我的推荐列表
  1469. */
  1470. public function getMyUsers() {
  1471. $thispage = $this->request->request('thispage',1); // 当前页
  1472. $pageNum = $this->request->request('pageNum', 10); // 分页
  1473. $page = [];
  1474. $page["page"] = $thispage;
  1475. $page["list_rows"] = $pageNum;
  1476. $user_id = $this->auth->id;
  1477. $userList = \app\common\model\User::field("id,nickname,u_id,bindtime")->where(["pre_userid"=>$user_id])->paginate($page);
  1478. if($userList) foreach($userList as $k => $v) $userList[$k]["bindtime"] = date("Y-m-d H:i:s",$v["bindtime"]);
  1479. $this->success("获取成功!",$userList);
  1480. }
  1481. /**
  1482. * 获取下级/下下级充值记录
  1483. */
  1484. public function getRechargeLog() {
  1485. $user_type = $this->request->request('user_type'); // 类型:1=直属,2=下属
  1486. $thispage = $this->request->request('thispage',1); // 当前页
  1487. $user_id = $this->auth->id;
  1488. $users = \app\common\model\User::where(["pre_userid"=>$user_id])->column("id");
  1489. if(!$users) $this->success("获取成功!",$users);
  1490. $ids = $users;
  1491. // 下下级
  1492. if($user_type == 2) {
  1493. $usersusers = [];
  1494. foreach($users as $v) {
  1495. $user_id = $v;
  1496. $userspre = \app\common\model\User::where(["pre_userid"=>$user_id])->column("id");
  1497. if(!$userspre) continue;
  1498. foreach($userspre as $n) {
  1499. $usersusers[] = $n;
  1500. }
  1501. }
  1502. $ids = $usersusers;
  1503. }
  1504. // 上个周一
  1505. $preweek = $this->firstOfWeek(date("Y-m-d H:i:s",bcsub(time(),604800)));
  1506. $starttime = strtotime($preweek." 00:00:00"); // 上周时间
  1507. $page = [];
  1508. $page["page"] = $thispage;
  1509. $page["list_rows"] = 10;
  1510. $where = [];
  1511. $where["a.user_id"] = ["in",$ids];
  1512. $where["a.createtime"] = ["gt",$starttime];
  1513. // 获取充值记录
  1514. $rechargeList = \app\common\model\UserRechargeLog::alias("a")
  1515. ->field("a.id,a.user_id,u.nickname,u.bindtime,a.createtime,a.money")
  1516. ->join("hx_user u","u.id = a.user_id")
  1517. ->where($where)
  1518. ->order("a.createtime","desc")
  1519. ->paginate($page);
  1520. if($rechargeList) foreach($rechargeList as $k => $v) {
  1521. if($v["createtime"] < $v["bindtime"]) {
  1522. unset($rechargeList[$k]);
  1523. continue;
  1524. }
  1525. $rechargeList[$k]["createtime"] = date("Y-m-d H:i:s",$v["createtime"]);
  1526. $rechargeList[$k]["nickname"] = mb_substr($v["nickname"], 0, 1, "utf-8") . "***";
  1527. }
  1528. $this->success("获取成功!",$rechargeList);
  1529. }
  1530. /**
  1531. * 获取上周收益
  1532. */
  1533. public function getLastweekProfit() {
  1534. $user_id = $this->auth->id;
  1535. $result = [];
  1536. // 上个周一
  1537. $preweek = $this->firstOfWeek(date("Y-m-d H:i:s",bcsub(time(),604800)));
  1538. $starttime = strtotime($preweek." 00:00:00"); // 上周一时间
  1539. $endtime = bcadd($starttime,604800); // 上周天时间
  1540. // 直属收益
  1541. $where = [];
  1542. $where["user_id"] = $user_id;
  1543. $where["is_rebate"] = 1;
  1544. $where["rebate_type"] = 1;
  1545. $where["createtime"] = ["between",[$starttime,$endtime]];
  1546. $rebate_rate = \app\common\model\RebateLog::where($where)->value("rebate_rate");
  1547. $rebate_rate = bcmul($rebate_rate,100,2);
  1548. $result["pre"]["money"] = \app\common\model\RebateLog::where($where)->sum("money");
  1549. $result["pre"]["rate"] = (float)$rebate_rate;
  1550. $result["pre"]["rebate_money"] = \app\common\model\RebateLog::where($where)->sum("rebate_money");
  1551. // 下属收益
  1552. $where = [];
  1553. $where["user_id"] = $user_id;
  1554. $where["is_rebate"] = 1;
  1555. $where["rebate_type"] = 2;
  1556. $where["createtime"] = ["between",[$starttime,$endtime]];
  1557. $rebate_rate = \app\common\model\RebateLog::where($where)->value("rebate_rate");
  1558. $rebate_rate = bcmul($rebate_rate,100,2);
  1559. $result["prepre"]["money"] = \app\common\model\RebateLog::where($where)->sum("money");
  1560. $result["prepre"]["rate"] = (float)$rebate_rate;
  1561. $result["prepre"]["rebate_money"] = \app\common\model\RebateLog::where($where)->sum("rebate_money");
  1562. // 上周收益总计
  1563. $result['weekRebate'] = (float)bcadd($result["pre"]["rebate_money"],$result["prepre"]["rebate_money"],2);
  1564. $this->success("获取成功!",$result);
  1565. }
  1566. ////////////////////////////////////
  1567. /**
  1568. * 消息-聊天-更多-转账-钻石
  1569. */
  1570. public function givejeweltouser(){
  1571. $money = intval(input('money',0));
  1572. $user_id = input('user_id',0);
  1573. $pay_password = input('paypassword','');
  1574. //验证用户权限是否能转账
  1575. if($this->auth->power->transfer == 1){
  1576. $this->error('您已经被限制消费-转账');
  1577. }
  1578. //不允许给自己转账
  1579. if ($user_id == $this->auth->id) {
  1580. $this->error('不允许给自己转账');
  1581. }
  1582. //验证被转账用户存在
  1583. $check = Db::name('user')->where('id',$user_id)->find();
  1584. if(empty($check)){
  1585. $this->error('不存在的用户');
  1586. }
  1587. //验证操作密码
  1588. if(!$this->auth->pay_password){
  1589. $this->error('请先设置支付密码');
  1590. }
  1591. if ($this->auth->pay_password != $this->getEncryptPassword($pay_password, $this->auth->pay_salt)) {
  1592. $this->error('支付密码有误');
  1593. }
  1594. Db::startTrans();
  1595. //减少和增加余额
  1596. $rs_wallet = model('wallet')->lockChangeAccountRemain($this->auth->id,$money,'-',0,'私聊转账给:'.$check['nickname'],11,'jewel');
  1597. if($rs_wallet['status'] === false){
  1598. Db::rollback();
  1599. $this->error($rs_wallet['msg']);
  1600. }
  1601. $rs_wallet = model('wallet')->lockChangeAccountRemain($user_id,$money,'+',0,$this->auth->nickname.':私聊到账',12,'jewel');
  1602. if($rs_wallet['status'] === false){
  1603. Db::rollback();
  1604. $this->error($rs_wallet['msg']);
  1605. }
  1606. Db::commit();
  1607. $this->success('转账成功');
  1608. }
  1609. /**
  1610. * 获取密码加密后的字符串
  1611. * @param string $password 密码
  1612. * @param string $salt 密码盐
  1613. * @return string
  1614. */
  1615. private function getEncryptPassword($password, $salt = '')
  1616. {
  1617. return md5(md5($password) . $salt);
  1618. }
  1619. /**
  1620. * 提现测试
  1621. * @return void
  1622. */
  1623. public function transfer()
  1624. {
  1625. try {
  1626. $type = 'alipay';
  1627. $out_trade_no = 'withdraw_order_001';
  1628. $title = 'test_withdraw';
  1629. $money = 0.01;
  1630. $method = 'transfer';
  1631. $notifyurl = request()->root(true) . '/api/notify/' . $type . 'notify';
  1632. $returnurl = request()->root(true) . '/addons/epay/index/' . $type . 'return/out_trade_no/' . $out_trade_no;
  1633. $params = [
  1634. 'type' => $type,
  1635. 'orderid' => $out_trade_no,
  1636. 'title' => $title,
  1637. 'amount' => $money,
  1638. 'method' => $method,
  1639. 'notifyurl' => $notifyurl,
  1640. 'returnurl' => $returnurl,
  1641. ];
  1642. $result = Service::submitOrder($params);
  1643. echo '<pre>';var_dump($result);exit;
  1644. $this->success('操作成功',$result);
  1645. } catch (Exception $e) {
  1646. $this->error($e->getMessage());
  1647. }
  1648. }
  1649. //提现转账(新版2020-01-01)
  1650. public function withdrawTransfer() {
  1651. $info = [
  1652. 'money' => 0.10,
  1653. 'alipay_account' => '13651955934',
  1654. 'name' => '张效彬',
  1655. ];
  1656. $data['out_biz_no'] = getMillisecond() . mt_rand(1, 1000); //商户订单号
  1657. $data['trans_amount'] = $info['money']; //订单总金额,单位为元,精确到小数点后两位
  1658. $data['product_code'] = 'TRANS_ACCOUNT_NO_PWD';//业务产品码,收发现金红包固定为:STD_RED_PACKET;单笔无密转账到支付宝账户固定为:TRANS_ACCOUNT_NO_PWD;单笔无密转账到银行卡固定为:TRANS_BANKCARD_NO_PWD
  1659. $data['biz_scene'] = 'DIRECT_TRANSFER'; //描述特定的业务场景,可传的参数如下:PERSONAL_COLLECTION:C2C现金红包-领红包;DIRECT_TRANSFER:B2C现金红包、单笔无密转账到支付宝/银行卡
  1660. $data['order_title'] = 'GG语音提现'; //转账业务的标题,用于在支付宝用户的账单里显示
  1661. $data['payee_info']['identity'] = $info['alipay_account'];//收款方支付宝id或支付宝账户
  1662. /*if ($info['alipay_type'] == 1) { //支付宝账户
  1663. $data['payee_info']['identity_type'] = 'ALIPAY_LOGON_ID';
  1664. //收款支付宝账号真实姓名, 不为空时支付宝会验证
  1665. $data['payee_info']['name'] = $info['name'];
  1666. } else { //支付宝id
  1667. $data['payee_info']['identity_type'] = 'ALIPAY_USER_ID';
  1668. $data['payee_info']['name'] = '';
  1669. }*/
  1670. //支付宝id
  1671. $data['payee_info']['identity_type'] = 'ALIPAY_LOGON_ID';
  1672. $data['payee_info']['name'] = $info['name'];
  1673. //转账备注(支持200个英文/100个汉字)。当付款方为企业账户,且转账金额达到(大于等于)50000元,remark不能为空。收款方可见,会展示在收款用户的收支详情中。
  1674. $data['remark'] = 'GG语音提现';
  1675. require_once("../extend/AliPay/AliPay.php");
  1676. $alipay =new \AliPay();
  1677. $result =$alipay->AliPayWithdraw($data);
  1678. return $result;
  1679. }
  1680. /**
  1681. * 获取余额相关比例
  1682. * @return void
  1683. */
  1684. public function moneyConfig()
  1685. {
  1686. try {
  1687. $field = 'name,title,value';
  1688. $where['name'] = ['in', ['money_to_jewel','withdrawRate','sign_gift']];
  1689. $intField = ['money_to_jewel','withdrawRate','sign_gift'];
  1690. $config = Db::name('config')->field($field)->where($where)->select();
  1691. $result = [];
  1692. if (!empty($config)) {
  1693. foreach ($config as $key => $value) {
  1694. if (in_array($value['name'],$intField)) {
  1695. $value['value'] = (int)$value['value'];
  1696. }
  1697. $result[$value['name']] = $value;
  1698. }
  1699. }
  1700. $this->success('获取成功',$result);
  1701. } catch (Exception $e) {
  1702. $this->error($e->getMessage());
  1703. }
  1704. }
  1705. }