Money.php 74 KB

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