Money.php 73 KB

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