Money.php 63 KB

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