Money.php 76 KB

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