Browse Source

视频通话判断余额,每分钟扣一次费,钱包操作方法

lizhen_gitee 3 years ago
parent
commit
a556d5ca32

+ 6 - 2
application/api/controller/Topichub.php

@@ -9,7 +9,7 @@ use think\Db;
  */
 class Topichub extends Api
 {
-    protected $noNeedLogin = ['lists','info'];
+    protected $noNeedLogin = [];
     protected $noNeedRight = ['*'];
 
 
@@ -28,12 +28,16 @@ class Topichub extends Api
 
         //圈子成员
         $user_list = Db::name('topic_user')->alias('tp')
-            ->field('tp.user_id,user.avatar,user.nickname,user.gender,user.longitude,user.latitude')
+            ->field('tp.user_id,user.avatar,user.nickname,user.gender,user.cityname,user.longitude,user.latitude')
             ->join('user','tp.user_id = user.id','LEFT')
             ->where('topic_id',$id)->autopage()->select();
         $user_list = list_domain_image($user_list,['avatar']);
 
         $info['userlist'] = $user_list;
+
+        //我是否已加入
+        $isjoin = Db::name('topic_user')->where(['topic_id'=>$id,'user_id'=>$this->auth->id])->find();
+        $info['isjoin'] = $isjoin ? 1 : 0;
         $this->success('success',$info);
     }
 

+ 4 - 0
application/api/controller/User.php

@@ -281,6 +281,10 @@ class User extends Api
         $this->success();
     }
 
+    public function set_status_switch(){
+
+    }
+
     /*
      * 修改用户的坐标
      * */

+ 77 - 1
application/api/controller/Usercenter.php

@@ -4,7 +4,7 @@ namespace app\api\controller;
 
 use app\common\controller\Api;
 use think\Db;
-
+use app\common\model\wallet;
 /**
  * 会员中心
  */
@@ -113,8 +113,84 @@ class Usercenter extends Api
 
     }
 
+    //视频通话每分钟调用一次
+    public function video_onemin(){
+        $to_user_id = input_post('to_user_id');
+
+        //先检查今天免费的一分钟
+        $start = strtotime(date('Y-m-d'));
+        $end   = $start + 86399;
+
+        $map = [
+            'user_id' => $this->auth->id,
+            'createtime' => ['between',[$start,$end]],
+            'price' => 0,
+        ];
+
+        $check = Db::name('user_video_log')->where($map)->find();
+
+        //设置价格
+        $price = config('site.video_min_price');
+        $price = empty($check) ? 0 : $price;
+
+
+        Db::startTrans();
+
+        //记录日志
+        $data = [
+            'user_id' => $this->auth->id,
+            'price'   => $price,
+            'createtime' => time(),
+            'to_user_id' => $to_user_id,
+        ];
+
+        $log_id = Db::name('user_video_log')->insertGetId($data);
+        if(!$log_id){
+            Db::rollback();
+            $this->error('扣费失败');
+        }
+
+        //扣费
+        if(!empty($check) && $log_id){
+            $rs = model('wallet')->lockChangeAccountRemain($this->auth->id,'gold',-$price,11,'','user_video_log',$log_id);
+            if($rs['status'] === false){
+                Db::rollback();
+                $this->error($rs['msg']);
+            }
+        }
+
+        Db::commit();
+        $this->success('success');
+    }
+
     //视频匹配
     public function getvideouser(){
+
+        //判断资格
+        $start = strtotime(date('Y-m-d'));
+        $end   = $start + 86399;
+
+        $map = [
+            'user_id' => $this->auth->id,
+            'createtime' => ['between',[$start,$end]],
+            'price' => 0,
+        ];
+
+        $check = Db::name('user_video_log')->where($map)->find();
+
+        //已经用掉免费的了,判断金额
+        if($check){
+            $price = config('site.video_min_price');
+            $gold = model('wallet')->getWallet($this->auth->id,'gold');
+            $moneyname = model('wallet')->getwalletname('gold');
+
+            if($gold < $price){
+                $this->error('您的'.$moneyname.'已经不足,请充值');
+            }
+        }
+        
+
+        //给出备选用户
         $map = [
             'status' =>1,
             //'gender' => $this->auth->gender == 1 ? 0 : 1,

+ 180 - 0
application/common/model/Wallet.php

@@ -0,0 +1,180 @@
+<?php
+namespace app\common\model;
+use com\Pay\Pay;
+use think\Model;
+use think\Db;
+/**
+ * 货币模型
+ */
+class Wallet extends Model
+{
+    /**
+     * 获取交易类型配置
+     * @return mixed
+     */
+    public function getlogtype($type = '')
+    {
+        $conf = config('wallet.logtype');
+        if($type){
+            return $conf[$type] ?: $type;
+        }
+        return $conf;
+    }
+
+    //获取钱包名称
+    public function getwalletname($name = ''){
+        $conf = config('wallet.moneyname');
+        if($name){
+            return $conf[$name] ?: $name;
+        }
+        return $conf;
+    }
+
+    /**
+     * 获取钱包余额
+     * @param string $username 用户编号
+     * @param string $wallet_name 指定钱包名
+     * @return array|float
+     */
+    public function getWallet($user_id = '', $wallet_name = '')
+    {
+        //所有钱包余额
+        $wallet = Db::name('user_wallet')->lock(true)->where(['user_id' => $user_id])->find();
+        if(!$wallet) {
+            abort(500,'钱包余额获取失败');
+        }
+
+        if($wallet_name) { //返回指定钱包
+            return isset($wallet[$wallet_name]) ? $wallet[$wallet_name] : 0;
+        } else { //返回所有钱包
+            return $wallet;
+        }
+    }
+
+
+    /**
+     *
+     * @param floatval $number 金额(正数进账,负数出账)
+     * @param $accountType 货币类型,money,score
+     * @param $logtype 日志的类型
+     * @param $remark  备注
+     * @param $user_id  用户id
+     * @param $table  来源表
+     * @param $data_id  表id
+     * @param $isAdmin 是否是管理员处理
+     * @return array
+     * @return array[status]
+     * @return array[msg]
+     * @return array[log_table]
+     * @return array[log_id]
+     */
+    public function lockChangeAccountRemain($user_id,$accountType='money',$number,$logtype='',$remark='',$table='',$table_id=0,$isAdmin=false)
+    {
+        //初始化
+        $result = array(
+            'status'=>false,
+            'msg'=>'',
+            'log_table' => '',
+            'log_id' => '',
+        );
+
+        //获取小数点
+        $point = $accountType == 'money' ? 2 : 0;
+        bcscale($point);
+
+        //钱包名称
+        $wallet_name = $this->getwalletname($accountType);
+
+        //检测
+        $number = floatval( $number );
+        if( $number == 0 )
+        {
+            $result['msg'] = '交易金额:0';
+            return $result;
+        }
+        if(0 === bccomp($number, 0)){
+            $result['msg'] = '交易金额:0';
+            return $result;
+        }
+
+
+        //检测
+        $wallet = Db::name('user_wallet')->lock(true)->where(['user_id'=>$user_id])->find();
+        if(!$wallet)
+        {
+            $result['msg'] = '不存在的用户';
+            return $result;
+        }
+
+        if(bccomp(bcadd($wallet[$accountType], $number), 0) === -1)
+        {
+            $result['msg'] = $wallet_name.'余额不足!';
+            return $result;
+        }
+        else
+        {
+            if(0 !== bccomp($number, 0))
+            {
+
+                //钱币记录
+                $data = array();
+                $data['user_id'] = $user_id;
+                $data['log_type'] = $logtype;
+                $data['money_type'] = $accountType;
+                $data['change_value'] = $number;
+                $data['remain'] = bcadd($wallet[$accountType], $number);
+                $data['table'] = $table;
+                $data['table_id'] = $table_id;
+                $data['remark'] = $remark;
+                $data['createtime'] = time();
+                $data['updatetime'] = time();
+
+                //新的方式
+                if(bccomp($number, 0) === 1){
+                    $rs1 = Db::name('user_wallet')->where(['user_id'=>$user_id])->setInc($accountType,$number);
+                }else{
+                    $rs1 = Db::name('user_wallet')->where(['user_id'=>$user_id])->setDec($accountType,abs($number));
+                }
+
+
+                /////////////
+                $rs2_id = Db::name('user_wallet_log')->insertGetId($data);
+                if( $rs1 && $rs2_id )
+                {
+                    //金额变动
+                    /*$params = [
+                        'msg' => '您的钱包['.$wallet_name.']有变化,请注意查看',
+                        'description' => '您的钱包['.$wallet_name.']有变化:'.$number.'剩余:'.$data['remain'],
+                        'content' => '您的钱包['.$wallet_name.']有变化:'.$number.',剩余:'.$data['remain'].',备注:'.$remark,
+                        'user_id' => $user_id,
+                        'status' => 1,
+                        'comefrom' => '系统',
+                        'type' => '钱包',
+                        'createtime' => time(),
+                        'important' => 3,
+                    ];
+                    Db::name('system_msg')->insertGetId($params);*/
+
+                    $result['status'] = true;
+                    $result['msg'] = '账户余额已更新!';
+                    $result['log_table'] = $accountType.'_log';
+                    $result['log_id'] = $rs2_id;
+
+                    return $result;
+                }
+                else
+                {
+                    $result['msg'] = '更新财务记录失败!';
+                    return $result;
+                }
+            } else {
+                $result['msg'] = '金额不足0.01';
+                return $result;
+            }
+        }
+    }
+
+
+
+
+}

+ 16 - 11
application/extra/site.php

@@ -15,18 +15,19 @@ return array (
   'fixedpage' => 'dashboard',
   'categorytype' => 
   array (
-    'default' => 'Default',
-    'page' => 'Page',
-    'article' => 'Article',
+    'default' => '默认',
+    'page' => '单页',
+    'article' => '文章',
     'test' => 'Test',
   ),
   'configgroup' => 
   array (
-    'basic' => 'Basic',
-    'email' => 'Email',
-    'dictionary' => 'Dictionary',
-    'user' => 'User',
-    'example' => 'Example',
+    'basic' => '基础配置',
+    'email' => '邮件配置',
+    'dictionary' => '字典配置',
+    'user' => '会员配置',
+    'example' => '示例分组',
+    'website' => '金钱变量',
   ),
   'mail_type' => '1',
   'mail_smtp_host' => 'smtp.qq.com',
@@ -37,13 +38,17 @@ return array (
   'mail_from' => '10000@qq.com',
   'attachmentcategory' => 
   array (
-    'category1' => 'Category1',
-    'category2' => 'Category2',
-    'custom' => 'Custom',
+    'category1' => '分类一',
+    'category2' => '分类二',
+    'custom' => '自定义',
   ),
   'domain_name' => 'http://mita.com',
   'android_update_num' => '1',
   'android_update_version' => '1.0',
   'ios_update_num' => '1',
   'ios_update_version' => '1.0',
+  'money_to_gold' => '10',
+  'video_min_price' => '26',
+  'audio_min_price' => '13',
+  'typing_min_price' => '6',
 );

+ 6 - 17
application/extra/wallet.php

@@ -5,26 +5,15 @@
 return [
     'logtype' => [
         1 => '系统调节',
-        2 => '陪玩下单',
-        3 => '陪玩超时退款',
-        4 => '礼物打赏',
-        5 => '陪玩接单',
-        6 => '礼物收赏',
-        7 => '礼物打赏厅主收益',
-        8 => '购买守护',
-        9 => '余额充值',
-        10 => '陪玩订单取消退款',
-        11 => '兑换消耗余额',
-        12 => '兑换增加巧鱼币',
-        13 => '提现',
-        14 => '推广用户充值返利',
-        15 => '提现驳回',
-        16 => '开宝箱',
-        117 => '巧鱼币充值',
+
+
+        11 => '视频通话',
+        12 => '语音通话',
+        13 => '文字聊天',
     ],
     'moneyname' => [
         'money'    => '余额',
-        'score'    => '金币',
+        'gold'     => '金币',
     ],
 
 ];