Money.php 74 KB

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