Userwallet.php 15 KB

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