Money.php 75 KB

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