Userwallet.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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 my_money_log(){
  62. $type = input_post('type',0);
  63. $map = [
  64. 'user_id' => $this->auth->id,
  65. ];
  66. // if($type){
  67. // $map['log_type'] = $type;
  68. // }
  69. $list = Db::name('user_money_log')
  70. ->field('id,log_type,change_value,remain,remark,createtime, remark log_text')
  71. ->where($map)->order('id desc')->autopage()->select();
  72. // $list = $this->list_appen_logtext($list);
  73. if(!empty($list)){
  74. foreach($list as $key => $val){
  75. $list[$key]['createtime'] = date('Y.m.d H:i', $val['createtime']);
  76. }
  77. }
  78. $this->success('success',$list);
  79. }
  80. //金币日志
  81. public function my_gold_log(){
  82. $type = input_post('type',0);
  83. $map = [
  84. 'user_id' => $this->auth->id,
  85. ];
  86. if($type == 19){
  87. $map['log_type'] = $type;
  88. }
  89. $list = Db::name('user_gold_log')->field('id,log_type,change_value,remain,createtime, remark log_text')->where($map)->order('id desc')->autopage()->select();
  90. // $list = $this->list_appen_logtext($list);
  91. if(!empty($list)){
  92. foreach($list as $key => $val){
  93. $list[$key]['createtime'] = date('Y.m.d H:i', $val['createtime']);
  94. }
  95. }
  96. $this->success('success',$list);
  97. }
  98. //提现记录
  99. public function take_cash_log(){
  100. $list = Db::name('take_cash')->field('id, number, status, createtime')->where(['user_id'=>$this->auth->id])->autopage()->order('id desc')->select();
  101. if ($list) {
  102. foreach ($list as &$v) {
  103. $v['title'] = '余额提现';
  104. $v['number'] = '-' . $v['number'];
  105. $v['createtime'] = date('Y.m.d H:i', $v['createtime']);
  106. }
  107. }
  108. $this->success('success',$list);
  109. }
  110. //提现金额配置
  111. public function withdrawal_config() {
  112. $show = config('site.withdrawal_show');
  113. $list = Db::name('withdrawal_config')->where('is_show',1)->order('weight asc,id asc')->select();
  114. if ($list) {
  115. $arr = [
  116. 'id' => -1,
  117. 'money' => 0,
  118. 'real_money' => 0,
  119. 'type' => 0,
  120. 'is_show' => 1,
  121. 'weight' => 1
  122. ];
  123. array_push($list, $arr);
  124. }
  125. $return_data['show'] = $show;
  126. $return_data['withdrawal_rate'] = config('site.withdrawal_rate') > 0 ? config('site.withdrawal_rate') : 10;
  127. $return_data['list'] = $list;
  128. $this->success('success',$return_data);
  129. }
  130. //提现
  131. public function withdrawal() {
  132. $id = input('id', 0, 'intval');
  133. $type = input('type', 0, 'intval'); //账户类型:1=支付宝,2=银行卡
  134. $freemoney = input('freemoney', 0, 'intval'); //自定义金额
  135. if (!$id && !$freemoney) {
  136. $this->error('请选择或填写提现金额');
  137. }
  138. if (!in_array($type, [1, 2])) {
  139. $this->error('请选择提现账户~');
  140. }
  141. if ($this->auth->idcard_status != 1) {
  142. $this->error('请先完成实名认证~');
  143. }
  144. if ($id > 0) {
  145. $withdrawal_config = Db::name('withdrawal_config')->find($id);
  146. if (!$withdrawal_config) {
  147. $this->error('提现金额不存在~');
  148. }
  149. if ($withdrawal_config['is_show'] != 1) {
  150. $this->error('提现金额暂未开放~');
  151. }
  152. if ($withdrawal_config['money'] <= 0 || $withdrawal_config['real_money'] <= 0 || $withdrawal_config['money'] < $withdrawal_config['real_money']) {
  153. $this->error('提现金额异常~');
  154. }
  155. //扣除金额
  156. $money = $withdrawal_config['money'];
  157. //实际获得金额
  158. $real_money = $withdrawal_config['real_money'];
  159. }
  160. if ($freemoney > 0) {
  161. //扣除金额
  162. $money = $freemoney;
  163. //平台手续费
  164. $bili = config('site.withdrawal_rate') >= 0 ? config('site.withdrawal_rate') : 10;
  165. $plat_money = bcdiv(bcmul($money,$bili,2),100,2);
  166. //减去手续费,得实得金额
  167. $real_money = bcsub($money,$plat_money,2);
  168. }
  169. if ($money <= 0 || $real_money <= 0) {
  170. $this->error('提现金额异常');
  171. }
  172. $check = Db::name('take_cash')->where(['user_id'=>$this->auth->id,'status'=>0])->find();
  173. if($check){
  174. $this->error('您有一笔提现在审核中,待审核通过后再申请提现。');
  175. }
  176. $user_money = model('wallet')->getwallet($this->auth->id,'money');
  177. if($money > $user_money){
  178. $this->error('可提现余额不足');
  179. }
  180. $user_bank_info = Db::name('user_bank')->where(['user_id' => $this->auth->id, 'type' => $type])->find();
  181. if (!$user_bank_info) {
  182. $this->error('请先添加提现方式');
  183. }
  184. $alipay_account = $user_bank_info['banknumber'];
  185. $realname = $user_bank_info['realname'];
  186. $data = [
  187. 'user_id' => $this->auth->id,
  188. 'wallet_id' => $freemoney > 0 ? 0 : $id,
  189. 'number' => $money,
  190. 'money' => $real_money,
  191. 'alipay_account' => $alipay_account,
  192. 'status' => 0,
  193. 'createtime' => time(),
  194. 'updatetime' => time(),
  195. 'type' => $type,
  196. 'realname' => $realname
  197. ];
  198. $msg = '申请成功请等待审核';
  199. //开启事务
  200. Db::startTrans();
  201. $log_id = Db::name('take_cash')->insertGetId($data);
  202. if (!$log_id) {
  203. Db::rollback();
  204. $this->error('您的网络开小差啦~');
  205. }
  206. //扣除money
  207. $rs_wallet = model('Wallet')->lockChangeAccountRemain($this->auth->id,0,'money',-$data['number'],15,'提现(审核中)','take_cash',$log_id);
  208. if($rs_wallet['status']===false)
  209. {
  210. Db::rollback();
  211. $this->error($rs_wallet['msg']);
  212. }
  213. Db::commit();
  214. $this->success($msg);
  215. }
  216. //每日数据
  217. public function todayincome() {
  218. $start = input('time', 0, 'strtotime'); //时间: 2022-09-30
  219. if (!$start) {
  220. $start = strtotime(date('Y-m-d')); //默认今日
  221. }
  222. $end = $start + 86399;
  223. //收益type
  224. $profit_type = [21,22,23,54,58,60,67];
  225. //今日收益
  226. $today_map = [
  227. 'log_type' => ['IN',$profit_type],
  228. 'user_id' => $this->auth->id,
  229. 'createtime' => ['between',[$start,$end]],
  230. ];
  231. $today_profit = Db::name('user_money_log')->where($today_map)->sum('change_value');
  232. //今日视频时长/收益/人数
  233. $map = [
  234. 'to_user_id' => $this->auth->id,
  235. 'createtime' => ['between',[$start,$end]],
  236. ];
  237. $today_video_min = Db::name('user_match_video_log')->where($map)->sum('call_minutes');
  238. $today_video_income = Db::name('user_match_video_log')->where($map)->sum('money');
  239. $today_video_income = $today_video_income ? : 0;
  240. $today_video_user = Db::name('user_match_video_log')->where($map)->column('user_id');
  241. $today_video_user = array_unique($today_video_user);
  242. $today_video_user_num = count($today_video_user);
  243. //今日语音时长/收益/人数
  244. $today_audio_min = Db::name('user_match_audio_log')->where($map)->sum('call_minutes');
  245. $today_audio_income = Db::name('user_match_audio_log')->where($map)->sum('money');
  246. $today_audio_income = $today_audio_income ? : 0;
  247. $today_audio_user = Db::name('user_match_audio_log')->where($map)->column('user_id');
  248. $today_audio_user = array_unique($today_audio_user);
  249. $today_audio_user_num = count($today_audio_user);
  250. //今日时长/收益/人数
  251. $today_time = $today_video_min + $today_audio_min;
  252. $today_time_income = number_format($today_video_income + $today_audio_income, 2, '.', '');
  253. $today_user_num = $today_video_user_num + $today_audio_user_num;
  254. //礼物收益
  255. $gitft_map = [
  256. 'log_type' => ['IN',[54, 58, 60]],
  257. 'user_id' => $this->auth->id,
  258. 'createtime' => ['between',[$start,$end]],
  259. ];
  260. $today_gift_income = Db::name('user_money_log')->where($gitft_map)->sum('change_value');
  261. //私聊时长/收益/人数
  262. $today_chat_min = Db::name('user_match_typing_log')->where($map)->count('id');
  263. $today_chat_income = Db::name('user_match_typing_log')->where($map)->sum('money');
  264. $today_chat_income = $today_chat_income ? : 0;
  265. $today_chat_user = Db::name('user_match_typing_log')->where($map)->column('user_id');
  266. $today_chat_user = array_unique($today_chat_user);
  267. $today_chat_user_num = count($today_chat_user);
  268. //任务收益 客户要求由money改为gold
  269. $today_task_income = Db::name('user_gold_log')->where(['user_id' => $this->auth->id, 'log_type' => 67, 'createtime' => ['between',[$start,$end]]])->sum('change_value');
  270. $today_task_income = $today_task_income ? : 0;
  271. $result = [
  272. 'today_profit' => $today_profit,
  273. 'today_time_income' => $today_time_income,
  274. 'today_gift_income' => $today_gift_income,
  275. 'today_chat_income' => $today_chat_income,
  276. 'today_task_income' => $today_task_income,
  277. 'today_time' => $today_time,
  278. 'today_user_num' => $today_user_num,
  279. 'today_chat_min' => $today_chat_min,
  280. 'today_chat_user_num' => $today_chat_user_num
  281. ];
  282. $this->success('success',$result);
  283. }
  284. //每日数据礼物列表
  285. public function todaygiftlist() {
  286. $start = input('time', 0, 'strtotime'); //时间: 09-30
  287. if (!$start) {
  288. $start = strtotime(date('Y-m-d')); //默认今日
  289. }
  290. $end = $start + 86399;
  291. //今日收益
  292. $today_map = [
  293. 'log_type' => ['IN', [54,/*58,*/60]],
  294. 'user_id' => $this->auth->id,
  295. 'createtime' => ['between',[$start,$end]],
  296. ];
  297. $list = Db::name('user_money_log')->where($today_map)->autopage()->order('id desc')->select();
  298. if (!$list) {
  299. $this->success('success', $list);
  300. }
  301. $mt_gift_user_typing = Db::name('gift_user_typing'); //54
  302. // $mt_gift_user_greet = Db::name('gift_user_greet'); //58
  303. $mt_gift_user_dongtai = Db::name('gift_user_dongtai'); //60
  304. foreach ($list as &$v) {
  305. if ($v['log_type'] == 54) {
  306. $table = $mt_gift_user_typing;
  307. }/* elseif ($v['log_type'] == 58) {
  308. $table = $mt_gift_user_greet;
  309. } */else {
  310. $table = $mt_gift_user_dongtai;
  311. }
  312. $info = $table->where(['id' => $v['table_id']])->find();
  313. $v['gift_name'] = $info['gift_name'];
  314. $v['number'] = $info['number'];
  315. }
  316. $this->success('success', $list);
  317. }
  318. }