Money.php 69 KB

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