Money.php 76 KB

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