Money.php 74 KB

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