Userwallet.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use think\Db;
  5. //use app\common\model\wallet;
  6. /**
  7. * 用户钱包
  8. */
  9. class Userwallet extends Api
  10. {
  11. protected $noNeedLogin = [];
  12. protected $noNeedRight = ['*'];
  13. //我的钱包余额
  14. public function my_wallet(){
  15. $wallet = model('wallet')->getwallet($this->auth->id);
  16. $this->success('success',$wallet);
  17. }
  18. //充值记录
  19. public function gold_recharge_log(){
  20. $map = [
  21. 'user_id' => $this->auth->id,
  22. 'log_type'=> 10,
  23. ];
  24. $list = Db::name('user_gold_log')->field('id,change_value,remain,createtime')->where($map)->order('id desc')->autopage()->select();
  25. $this->success('success',$list);
  26. }
  27. //我的收益,三个数据
  28. public function my_income_count(){
  29. //累计收益,不计来源
  30. $map = [
  31. 'user_id' => $this->auth->id,
  32. //'log_type'=> ['IN',[21,22,23]],
  33. ];
  34. $income_sum = Db::name('user_money_log')->where($map)->sum('change_value');
  35. //可提现总收益
  36. $money_remain = model('wallet')->getwallet($this->auth->id,'money');
  37. //今日收益
  38. $start = strtotime(date('Y-m-d'));
  39. $end = $start + 86399;
  40. $map['createtime'] = ['between',[$start,$end]];
  41. $today_income_sum = Db::name('user_money_log')->where($map)->sum('change_value');
  42. $result = [
  43. 'income_sum' => $income_sum,
  44. 'money_remain' => $money_remain,
  45. 'today_income_sum' => $today_income_sum,
  46. ];
  47. $this->success('success',$result);
  48. }
  49. //追加log_text
  50. private function list_appen_logtext($list){
  51. if(!empty($list)){
  52. $conf = config('wallet.logtype');
  53. foreach($list as $key => $val){
  54. $list[$key]['createtime'] = date('Y.m.d H:i');
  55. $list[$key]['log_text'] = isset($conf[$val['log_type']]) ? $conf[$val['log_type']] : '';
  56. }
  57. }
  58. return $list;
  59. }
  60. //互动收益,打视频,语音,文字聊天,聊天送礼物
  61. public function hudong_money(){
  62. $map = [
  63. 'user_id' => $this->auth->id,
  64. 'log_type'=> ['IN',[21,22,23,54]],
  65. ];
  66. $list = Db::name('user_money_log')
  67. ->field('id,log_type,change_value,remain,remark,createtime')
  68. ->where($map)->order('id desc')->autopage()->select();
  69. $list = $this->list_appen_logtext($list);
  70. $this->success('success',$list);
  71. }
  72. //音聊收益,语聊间礼物
  73. public function party_money(){
  74. $map = [
  75. 'user_id' => $this->auth->id,
  76. 'log_type'=> 52,
  77. ];
  78. $list = Db::name('user_money_log')
  79. ->field('id,log_type,change_value,remain,remark,createtime')
  80. ->where($map)->order('id desc')->autopage()->select();
  81. $list = $this->list_appen_logtext($list);
  82. $this->success('success',$list);
  83. }
  84. //直播收益,直播间礼物
  85. public function livebc_money(){
  86. $map = [
  87. 'user_id' => $this->auth->id,
  88. 'log_type'=> 56,
  89. ];
  90. $list = Db::name('user_money_log')
  91. ->field('id,log_type,change_value,remain,remark,createtime')
  92. ->where($map)->order('id desc')->autopage()->select();
  93. $list = $this->list_appen_logtext($list);
  94. $this->success('success',$list);
  95. }
  96. //我的余额日志
  97. public function my_money_log(){
  98. $type = input_post('type',0);
  99. $map = [
  100. 'user_id' => $this->auth->id,
  101. ];
  102. // if($type){
  103. // $map['log_type'] = $type;
  104. // }
  105. $list = Db::name('user_money_log')
  106. ->field('id,log_type,change_value,remain,remark,createtime, remark log_text')
  107. ->where($map)->order('id desc')->autopage()->select();
  108. // $list = $this->list_appen_logtext($list);
  109. if(!empty($list)){
  110. foreach($list as $key => $val){
  111. $list[$key]['createtime'] = date('Y.m.d H:i', $val['createtime']);
  112. }
  113. }
  114. $this->success('success',$list);
  115. }
  116. //金币日志
  117. public function my_gold_log(){
  118. $type = input_post('type',0);
  119. $map = [
  120. 'user_id' => $this->auth->id,
  121. ];
  122. if($type == 19){
  123. $map['log_type'] = $type;
  124. }
  125. $list = Db::name('user_gold_log')->field('id,log_type,change_value,remain,createtime, remark log_text')->where($map)->order('id desc')->autopage()->select();
  126. // $list = $this->list_appen_logtext($list);
  127. if(!empty($list)){
  128. foreach($list as $key => $val){
  129. $list[$key]['createtime'] = date('Y.m.d H:i', $val['createtime']);
  130. }
  131. }
  132. $this->success('success',$list);
  133. }
  134. //提现记录
  135. public function take_cash_log(){
  136. $list = Db::name('take_cash')->field('id, number, status, createtime')->where(['user_id'=>$this->auth->id])->autopage()->order('id desc')->select();
  137. if ($list) {
  138. foreach ($list as &$v) {
  139. $v['title'] = '余额提现';
  140. $v['number'] = '-' . $v['number'];
  141. $v['createtime'] = date('Y.m.d H:i', $v['createtime']);
  142. }
  143. }
  144. $this->success('success',$list);
  145. }
  146. //提现金额配置
  147. public function withdrawal_config() {
  148. $show = config('site.withdrawal_show');
  149. $list = Db::name('withdrawal_config')->where('is_show',1)->order('weight asc,id asc')->select();
  150. if ($list) {
  151. $arr = [
  152. 'id' => -1,
  153. 'money' => 0,
  154. 'real_money' => 0,
  155. 'type' => 0,
  156. 'is_show' => 1,
  157. 'weight' => 1
  158. ];
  159. array_push($list, $arr);
  160. }
  161. $return_data['show'] = $show;
  162. $return_data['withdrawal_rate'] = config('site.withdrawal_rate') > 0 ? config('site.withdrawal_rate') : 10;
  163. $return_data['list'] = $list;
  164. $this->success('success',$return_data);
  165. }
  166. //提现
  167. public function withdrawal() {
  168. $id = input('id', 0, 'intval');
  169. $type = input('type', 0, 'intval'); //账户类型:1=支付宝,2=银行卡
  170. $freemoney = input('freemoney', 0, 'intval'); //自定义金额
  171. if (!$id && !$freemoney) {
  172. $this->error('请选择或填写提现金额');
  173. }
  174. if (!in_array($type, [1, 2])) {
  175. $this->error('请选择提现账户~');
  176. }
  177. if ($this->auth->idcard_status != 1) {
  178. $this->error('请先完成实名认证~');
  179. }
  180. if ($id > 0) {
  181. $withdrawal_config = Db::name('withdrawal_config')->find($id);
  182. if (!$withdrawal_config) {
  183. $this->error('提现金额不存在~');
  184. }
  185. if ($withdrawal_config['is_show'] != 1) {
  186. $this->error('提现金额暂未开放~');
  187. }
  188. if ($withdrawal_config['money'] <= 0 || $withdrawal_config['real_money'] <= 0 || $withdrawal_config['money'] < $withdrawal_config['real_money']) {
  189. $this->error('提现金额异常~');
  190. }
  191. //扣除金额
  192. $money = $withdrawal_config['money'];
  193. //实际获得金额
  194. $real_money = $withdrawal_config['real_money'];
  195. }
  196. if ($freemoney > 0) {
  197. //扣除金额
  198. $money = $freemoney;
  199. //平台手续费
  200. $bili = config('site.withdrawal_rate') >= 0 ? config('site.withdrawal_rate') : 10;
  201. $plat_money = bcdiv(bcmul($money,$bili,2),100,2);
  202. //减去手续费,得实得金额
  203. $real_money = bcsub($money,$plat_money,2);
  204. }
  205. if ($money <= 0 || $real_money <= 0) {
  206. $this->error('提现金额异常');
  207. }
  208. $check = Db::name('take_cash')->where(['user_id'=>$this->auth->id,'status'=>0])->find();
  209. if($check){
  210. $this->error('您有一笔提现在审核中,待审核通过后再申请提现。');
  211. }
  212. $user_money = model('wallet')->getwallet($this->auth->id,'money');
  213. if($money > $user_money){
  214. $this->error('可提现余额不足');
  215. }
  216. $user_bank_info = Db::name('user_bank')->where(['user_id' => $this->auth->id, 'type' => $type])->find();
  217. if (!$user_bank_info) {
  218. $this->error('请先添加提现方式');
  219. }
  220. $alipay_account = $user_bank_info['banknumber'];
  221. $realname = $user_bank_info['realname'];
  222. $data = [
  223. 'user_id' => $this->auth->id,
  224. 'wallet_id' => $freemoney > 0 ? 0 : $id,
  225. 'number' => $money,
  226. 'money' => $real_money,
  227. 'alipay_account' => $alipay_account,
  228. 'status' => 0,
  229. 'createtime' => time(),
  230. 'updatetime' => time(),
  231. 'type' => $type,
  232. 'realname' => $realname
  233. ];
  234. $msg = '申请成功请等待审核';
  235. //开启事务
  236. Db::startTrans();
  237. $log_id = Db::name('take_cash')->insertGetId($data);
  238. if (!$log_id) {
  239. Db::rollback();
  240. $this->error('您的网络开小差啦~');
  241. }
  242. //扣除money
  243. $rs_wallet = model('Wallet')->lockChangeAccountRemain($this->auth->id,0,'money',-$data['number'],15,'提现(审核中)','take_cash',$log_id);
  244. if($rs_wallet['status']===false)
  245. {
  246. Db::rollback();
  247. $this->error($rs_wallet['msg']);
  248. }
  249. Db::commit();
  250. $this->success($msg);
  251. }
  252. //每日数据
  253. public function todayincome() {
  254. $start = input('time', 0, 'strtotime'); //时间: 2022-09-30
  255. if (!$start) {
  256. $start = strtotime(date('Y-m-d')); //默认今日
  257. }
  258. $end = $start + 86399;
  259. //收益type
  260. $profit_type = [21,22,23,54,58,60,67];
  261. //今日收益
  262. $today_map = [
  263. 'log_type' => ['IN',$profit_type],
  264. 'user_id' => $this->auth->id,
  265. 'createtime' => ['between',[$start,$end]],
  266. ];
  267. $today_profit = Db::name('user_money_log')->where($today_map)->sum('change_value');
  268. //今日视频时长/收益/人数
  269. $map = [
  270. 'to_user_id' => $this->auth->id,
  271. 'createtime' => ['between',[$start,$end]],
  272. ];
  273. $today_video_min = Db::name('user_match_video_log')->where($map)->sum('call_minutes');
  274. $today_video_income = Db::name('user_match_video_log')->where($map)->sum('money');
  275. $today_video_income = $today_video_income ? : 0;
  276. $today_video_user = Db::name('user_match_video_log')->where($map)->column('user_id');
  277. $today_video_user = array_unique($today_video_user);
  278. $today_video_user_num = count($today_video_user);
  279. //今日语音时长/收益/人数
  280. $today_audio_min = Db::name('user_match_audio_log')->where($map)->sum('call_minutes');
  281. $today_audio_income = Db::name('user_match_audio_log')->where($map)->sum('money');
  282. $today_audio_income = $today_audio_income ? : 0;
  283. $today_audio_user = Db::name('user_match_audio_log')->where($map)->column('user_id');
  284. $today_audio_user = array_unique($today_audio_user);
  285. $today_audio_user_num = count($today_audio_user);
  286. //今日时长/收益/人数
  287. $today_time = $today_video_min + $today_audio_min;
  288. $today_time_income = number_format($today_video_income + $today_audio_income, 2, '.', '');
  289. $today_user_num = $today_video_user_num + $today_audio_user_num;
  290. //礼物收益
  291. $gitft_map = [
  292. 'log_type' => ['IN',[54, 58, 60]],
  293. 'user_id' => $this->auth->id,
  294. 'createtime' => ['between',[$start,$end]],
  295. ];
  296. $today_gift_income = Db::name('user_money_log')->where($gitft_map)->sum('change_value');
  297. //私聊时长/收益/人数
  298. $today_chat_min = Db::name('user_match_typing_log')->where($map)->count('id');
  299. $today_chat_income = Db::name('user_match_typing_log')->where($map)->sum('money');
  300. $today_chat_income = $today_chat_income ? : 0;
  301. $today_chat_user = Db::name('user_match_typing_log')->where($map)->column('user_id');
  302. $today_chat_user = array_unique($today_chat_user);
  303. $today_chat_user_num = count($today_chat_user);
  304. //任务收益 客户要求由money改为gold
  305. $today_task_income = Db::name('user_gold_log')->where(['user_id' => $this->auth->id, 'log_type' => 67, 'createtime' => ['between',[$start,$end]]])->sum('change_value');
  306. $today_task_income = $today_task_income ? : 0;
  307. $result = [
  308. 'today_profit' => $today_profit,
  309. 'today_time_income' => $today_time_income,
  310. 'today_gift_income' => $today_gift_income,
  311. 'today_chat_income' => $today_chat_income,
  312. 'today_task_income' => $today_task_income,
  313. 'today_time' => $today_time,
  314. 'today_user_num' => $today_user_num,
  315. 'today_chat_min' => $today_chat_min,
  316. 'today_chat_user_num' => $today_chat_user_num
  317. ];
  318. $this->success('success',$result);
  319. }
  320. //每日数据礼物列表
  321. public function todaygiftlist() {
  322. $start = input('time', 0, 'strtotime'); //时间: 09-30
  323. if (!$start) {
  324. $start = strtotime(date('Y-m-d')); //默认今日
  325. }
  326. $end = $start + 86399;
  327. //今日收益
  328. $today_map = [
  329. 'log_type' => ['IN', [54,/*58,*/60]],
  330. 'user_id' => $this->auth->id,
  331. 'createtime' => ['between',[$start,$end]],
  332. ];
  333. $list = Db::name('user_money_log')->where($today_map)->autopage()->order('id desc')->select();
  334. if (!$list) {
  335. $this->success('success', $list);
  336. }
  337. $mt_gift_user_typing = Db::name('gift_user_typing'); //54
  338. // $mt_gift_user_greet = Db::name('gift_user_greet'); //58
  339. $mt_gift_user_dongtai = Db::name('gift_user_dongtai'); //60
  340. foreach ($list as &$v) {
  341. if ($v['log_type'] == 54) {
  342. $table = $mt_gift_user_typing;
  343. }/* elseif ($v['log_type'] == 58) {
  344. $table = $mt_gift_user_greet;
  345. } */else {
  346. $table = $mt_gift_user_dongtai;
  347. }
  348. $info = $table->where(['id' => $v['table_id']])->find();
  349. $v['gift_name'] = $info['gift_name'];
  350. $v['number'] = $info['number'];
  351. }
  352. $this->success('success', $list);
  353. }
  354. }