Userwallet.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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['exchange_rate'] = 100; //固定100
  128. $return_data['min'] = config('site.withdraw_min_money');
  129. $return_data['list'] = $list;
  130. $this->success('success',$return_data);
  131. }
  132. //提现
  133. public function withdrawal() {
  134. $id = input('id', 0, 'intval');
  135. $type = input('type', 0, 'intval'); //账户类型:1=支付宝,2=银行卡
  136. $freemoney = input('freemoney', 0, 'intval'); //自定义金额
  137. if (!$id && !$freemoney) {
  138. $this->error('请选择或填写提现金额');
  139. }
  140. if (!in_array($type, [1, 2])) {
  141. $this->error('请选择提现账户~');
  142. }
  143. if ($this->auth->idcard_status != 1) {
  144. $this->error('请先完成实名认证~');
  145. }
  146. if ($id > 0) {
  147. $withdrawal_config = Db::name('withdrawal_config')->find($id);
  148. if (!$withdrawal_config) {
  149. $this->error('提现金额不存在~');
  150. }
  151. if ($withdrawal_config['is_show'] != 1) {
  152. $this->error('提现金额暂未开放~');
  153. }
  154. if ($withdrawal_config['money'] <= 0 || $withdrawal_config['real_money'] <= 0 || $withdrawal_config['money'] < $withdrawal_config['real_money']) {
  155. $this->error('提现金额异常~');
  156. }
  157. //扣除金额
  158. $money = $withdrawal_config['money'];
  159. //实际获得金额
  160. $real_money = $withdrawal_config['real_money'];
  161. }
  162. if ($freemoney > 0) {
  163. //扣除金额
  164. $money = floatval($freemoney);
  165. $withdraw_min_money = config('site.withdraw_min_money');
  166. if($money < $withdraw_min_money){
  167. $this->error('最低提现'.$withdraw_min_money.'积分');
  168. }
  169. //平台手续费
  170. $withdrawal_rate = config('site.withdrawal_rate') >= 0 ? config('site.withdrawal_rate') : 10;
  171. $plat_money = bcdiv(bcmul($money,$withdrawal_rate,2),100,2);
  172. //减去手续费,得实得金额
  173. $real_money = bcdiv(bcsub($money,$plat_money,2),100,2);
  174. }
  175. if ($money <= 0 || $real_money <= 0) {
  176. $this->error('提现金额异常');
  177. }
  178. $check = Db::name('take_cash')->where(['user_id'=>$this->auth->id,'status'=>0])->find();
  179. if($check){
  180. $this->error('您有一笔提现在审核中,待审核通过后再申请提现。');
  181. }
  182. $user_money = model('wallet')->getwallet($this->auth->id,'money');
  183. if($money > $user_money){
  184. $this->error('可提现余额不足');
  185. }
  186. $user_bank_info = Db::name('user_bank')->where(['user_id' => $this->auth->id, 'type' => $type])->find();
  187. if (!$user_bank_info) {
  188. $this->error('请先添加提现方式');
  189. }
  190. $alipay_account = $user_bank_info['banknumber'];
  191. $realname = $user_bank_info['realname'];
  192. $data = [
  193. 'user_id' => $this->auth->id,
  194. 'wallet_id' => $freemoney > 0 ? 0 : $id,
  195. 'number' => $money,
  196. 'money' => $real_money,
  197. 'alipay_account' => $alipay_account,
  198. 'status' => 0,
  199. 'createtime' => time(),
  200. 'updatetime' => time(),
  201. 'type' => $type,
  202. 'realname' => $realname
  203. ];
  204. $msg = '申请成功请等待审核';
  205. //开启事务
  206. Db::startTrans();
  207. $log_id = Db::name('take_cash')->insertGetId($data);
  208. if (!$log_id) {
  209. Db::rollback();
  210. $this->error('您的网络开小差啦~');
  211. }
  212. //扣除money
  213. $rs_wallet = model('Wallet')->lockChangeAccountRemain($this->auth->id,0,'money',-$data['number'],15,'提现(审核中)','take_cash',$log_id);
  214. if($rs_wallet['status']===false)
  215. {
  216. Db::rollback();
  217. $this->error($rs_wallet['msg']);
  218. }
  219. Db::commit();
  220. $this->success($msg);
  221. }
  222. //每日数据
  223. public function todayincome() {
  224. $start = input('time', 0, 'strtotime'); //时间: 2022-09-30
  225. if (!$start) {
  226. $start = strtotime(date('Y-m-d')); //默认今日
  227. }
  228. $end = $start + 86399;
  229. //收益type
  230. $profit_type = [21,22,23,54,58,60,67];
  231. //今日收益
  232. $today_map = [
  233. 'log_type' => ['IN',$profit_type],
  234. 'user_id' => $this->auth->id,
  235. 'createtime' => ['between',[$start,$end]],
  236. ];
  237. $today_profit = Db::name('user_money_log')->where($today_map)->sum('change_value');
  238. //今日视频时长/收益/人数
  239. $map = [
  240. 'to_user_id' => $this->auth->id,
  241. 'createtime' => ['between',[$start,$end]],
  242. ];
  243. $today_video_min = Db::name('user_match_video_log')->where($map)->sum('call_minutes');
  244. $today_video_income = Db::name('user_match_video_log')->where($map)->sum('money');
  245. $today_video_income = $today_video_income ? : 0;
  246. $today_video_user = Db::name('user_match_video_log')->where($map)->column('user_id');
  247. $today_video_user = array_unique($today_video_user);
  248. $today_video_user_num = count($today_video_user);
  249. //今日语音时长/收益/人数
  250. $today_audio_min = Db::name('user_match_audio_log')->where($map)->sum('call_minutes');
  251. $today_audio_income = Db::name('user_match_audio_log')->where($map)->sum('money');
  252. $today_audio_income = $today_audio_income ? : 0;
  253. $today_audio_user = Db::name('user_match_audio_log')->where($map)->column('user_id');
  254. $today_audio_user = array_unique($today_audio_user);
  255. $today_audio_user_num = count($today_audio_user);
  256. //今日时长/收益/人数
  257. $today_time = $today_video_min + $today_audio_min;
  258. $today_time_income = number_format($today_video_income + $today_audio_income, 2, '.', '');
  259. $today_user_num = $today_video_user_num + $today_audio_user_num;
  260. //礼物收益
  261. $gitft_map = [
  262. 'log_type' => ['IN',[54, 58, 60]],
  263. 'user_id' => $this->auth->id,
  264. 'createtime' => ['between',[$start,$end]],
  265. ];
  266. $today_gift_income = Db::name('user_money_log')->where($gitft_map)->sum('change_value');
  267. //私聊时长/收益/人数
  268. $today_chat_min = Db::name('user_match_typing_log')->where($map)->count('id');
  269. $today_chat_income = Db::name('user_match_typing_log')->where($map)->sum('money');
  270. $today_chat_income = $today_chat_income ? : 0;
  271. $today_chat_user = Db::name('user_match_typing_log')->where($map)->column('user_id');
  272. $today_chat_user = array_unique($today_chat_user);
  273. $today_chat_user_num = count($today_chat_user);
  274. //任务收益 客户要求由money改为gold
  275. $today_task_income = Db::name('user_gold_log')->where(['user_id' => $this->auth->id, 'log_type' => 67, 'createtime' => ['between',[$start,$end]]])->sum('change_value');
  276. $today_task_income = $today_task_income ? : 0;
  277. $result = [
  278. 'today_profit' => $today_profit,
  279. 'today_time_income' => $today_time_income,
  280. 'today_gift_income' => $today_gift_income,
  281. 'today_chat_income' => $today_chat_income,
  282. 'today_task_income' => $today_task_income,
  283. 'today_time' => $today_time,
  284. 'today_user_num' => $today_user_num,
  285. 'today_chat_min' => $today_chat_min,
  286. 'today_chat_user_num' => $today_chat_user_num
  287. ];
  288. $this->success('success',$result);
  289. }
  290. //每日数据礼物列表
  291. public function todaygiftlist() {
  292. $start = input('time', 0, 'strtotime'); //时间: 09-30
  293. if (!$start) {
  294. $start = strtotime(date('Y-m-d')); //默认今日
  295. }
  296. $end = $start + 86399;
  297. //今日收益
  298. $today_map = [
  299. 'log_type' => ['IN', [54,/*58,*/60]],
  300. 'user_id' => $this->auth->id,
  301. 'createtime' => ['between',[$start,$end]],
  302. ];
  303. $list = Db::name('user_money_log')->where($today_map)->autopage()->order('id desc')->select();
  304. if (!$list) {
  305. $this->success('success', $list);
  306. }
  307. $mt_gift_user_typing = Db::name('gift_user_typing'); //54
  308. // $mt_gift_user_greet = Db::name('gift_user_greet'); //58
  309. $mt_gift_user_dongtai = Db::name('gift_user_dongtai'); //60
  310. foreach ($list as &$v) {
  311. if ($v['log_type'] == 54) {
  312. $table = $mt_gift_user_typing;
  313. }/* elseif ($v['log_type'] == 58) {
  314. $table = $mt_gift_user_greet;
  315. } */else {
  316. $table = $mt_gift_user_dongtai;
  317. }
  318. $info = $table->where(['id' => $v['table_id']])->find();
  319. $v['gift_name'] = $info['gift_name'];
  320. $v['number'] = $info['number'];
  321. }
  322. $this->success('success', $list);
  323. }
  324. //用户之间钻石转赠
  325. public function jewel_transfer(){
  326. $jewel = input('jewel',0,'intval');
  327. $user_id = input('user_id',0,'intval');
  328. if(!$jewel || !$user_id){
  329. $this->error();
  330. }
  331. if($jewel < 0){
  332. $this->error('输入数量为0');
  333. }
  334. Db::startTrans();
  335. $wallet_rs = model('wallet')->lockChangeAccountRemain($this->auth->id,0,'jewel',-$jewel,36,'钻石转赠');
  336. if($wallet_rs['status'] === false){
  337. Db::rollback();
  338. $this->error($wallet_rs['msg']);
  339. }
  340. $wallet_rs = model('wallet')->lockChangeAccountRemain($user_id,0,'jewel',$jewel,37,'钻石获赠');
  341. if($wallet_rs['status'] === false){
  342. Db::rollback();
  343. $this->error($wallet_rs['msg']);
  344. }
  345. Db::commit();
  346. $this->success();
  347. }
  348. }