Money.php 68 KB

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