瀏覽代碼

聊天扣钱,半成品

lizhen_gitee 3 年之前
父節點
當前提交
d957982f69
共有 2 個文件被更改,包括 199 次插入0 次删除
  1. 16 0
      application/chat/Events.php
  2. 183 0
      application/common/model/Wallet.php

+ 16 - 0
application/chat/Events.php

@@ -76,6 +76,7 @@ class Events
          "nowtime": "1234567890 ",
          "nowtime": "1234567890 ",
          "token": "edbf8a75-8e36-41f5-989f-7b3d067ccc83"
          "token": "edbf8a75-8e36-41f5-989f-7b3d067ccc83"
     }*/
     }*/
+    //修改用的通话状态
     private static function changestatus($message){
     private static function changestatus($message){
         //dump($message);
         //dump($message);
         if(isset($message['uid']) && isset($message['value'])){
         if(isset($message['uid']) && isset($message['value'])){
@@ -83,6 +84,21 @@ class Events
         }
         }
         return;
         return;
     }
     }
+    //聊天扣钱
+    private static function userchat(){
+        $price = config('site.typing_min_price');
+
+        //扣费
+        Db::startTrans();
+        if(isset($message['uid'])){
+            $rs = model('wallet')->lockChangeAccountRemain($message['uid'],'gold',-$price,13,'','',0);
+            if($rs['status'] === false){
+                Db::rollback();
+            }
+        }
+        Db::commit();
+    }
+
    
    
     /**
     /**
      * 当客户端断开连接时
      * 当客户端断开连接时

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

@@ -0,0 +1,183 @@
+<?php
+namespace app\common\model;
+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));
+                }
+
+
+                /////////////
+                $log_table = 'user_money_log';
+                if($accountType == 'gold'){
+                    $log_table = 'user_gold_log';
+                }
+                $rs2_id = Db::name($log_table)->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;
+            }
+        }
+    }
+
+
+
+
+}