Money.php 68 KB

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