Money.php 75 KB

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