Browse Source

医生的钱包,提现,钱包操作方法

lizhen_gitee 11 months ago
parent
commit
54e685cbc4

+ 173 - 0
application/api/controller/doctor/Takecash.php

@@ -0,0 +1,173 @@
+<?php
+
+namespace app\api\controller\doctor;
+
+use app\common\controller\Apic;
+use think\Db;
+/**
+ * 提现
+ */
+class Takecash extends Apic
+{
+    protected $noNeedLogin = [];
+    protected $noNeedRight = ['*'];
+
+    //提现
+    public function take_cash(){
+        $freemoney = input('freemoney',0);
+        $type = input('type',1);
+
+        if(!$freemoney){
+            $this->error('请填写金额');
+        }
+
+        if (!in_array($type,[1,2,3])) {
+            $this->error('未知的提现类型');
+        }
+
+        if($this->auth->idcard_status != 1){
+            $this->error('请先完成实名认证');
+        }
+
+        //赋值money
+        /*if($rc_id){
+            $recharge_config = Db::name('take_cash_config')->where('id',$rc_id)->find();
+            $money = $recharge_config['money'] ?: 0;
+        }*/
+
+        //自由输入覆盖
+        if(!empty($freemoney)){
+            $rc_id = 0;
+            $money = floatval($freemoney);
+        }
+
+        //
+        if($money<=0)
+        {
+            $this->error('金额必须大于0');
+        }
+
+        $min = config('site.min_takecash_money');
+        $max = config('site.max_takecash_money');
+        if($money < $min){
+            $this->error('提现金额不能小于'.$min);
+        }
+        if($money > $max){
+            $this->error('提现金额不能大于'.$max);
+        }
+
+        $check = Db::name('doctor_take_cash')->where(['doctor_id'=>$this->auth->id,'status'=>0])->find();
+        if($check){
+            $this->error('您已经申请了提现,请等待审核');
+        }
+
+        $user_money = model('walletdoctor')->getwallet($this->auth->id,'money');
+        if($money > $user_money){
+            $this->error('提现金额不能大于可提现余额');
+        }
+
+        if($type == 1){
+            $table_name = 'doctor_alipay';
+            $account_json = Db::name($table_name)->where('doctor_id',$this->auth->id)->find();
+            if(empty($account_json)){
+                $this->error('未绑定对应的提现账号');
+            }
+        }elseif($type == 2){
+            $table_name = 'doctor_bank';
+            $account_json = Db::name($table_name)->where('doctor_id',$this->auth->id)->find();
+            if(empty($account_json)){
+                $this->error('未绑定对应的提现账号');
+            }
+        }elseif($type == 3){
+            //微信支付
+            $table_name = 'doctor_wechat';
+            $account_json = Db::name($table_name)->where('doctor_id',$this->auth->id)->find();
+            if(empty($account_json)){
+                $this->error('未绑定对应的提现账号');
+            }
+        }
+
+        //平台手续费
+        $plat_bilv = config('site.takecash_plat_bili');
+        $plat_money = bcdiv(bcmul($money,$plat_bilv,2),100,2);
+
+        //减去手续费,得实得金额
+        $get_money = bcsub($money,$plat_money,2);
+
+        $data = [
+            'doctor_id' => $this->auth->id,
+            'money' => $money,
+            'plat_bilv'  => $plat_bilv,
+            'plat_money' => $plat_money,
+            'get_money'  => $get_money,
+            'type' => $type,
+            'acount_json' => json_encode($account_json),
+            'createtime' => time(),
+            'updatetime' => time(),
+            'status' => 0,
+        ];
+        Db::startTrans();
+
+        $log_id = Db::name('doctor_take_cash')->insertGetId($data);
+        if(!$log_id){
+            Db::rollback();
+            $this->error('提现失败');
+        }
+
+        //扣除money
+        $rs_wallet = model('Walletdoctor')->lockChangeAccountRemain($this->auth->id,'money',-$money,121,'提现(审核中)','doctor_take_cash',$log_id);
+        if($rs_wallet['status']===false)
+        {
+            Db::rollback();
+            $this->error($rs_wallet['msg']);
+        }
+
+        Db::commit();
+        $this->success('申请成功请等待审核');
+    }
+
+    //提现记录
+    public function take_cash_log(){
+        $list = Db::name('doctor_take_cash')->field('id,money,get_money,type,createtime,status')->where(['doctor_id'=>$this->auth->id])->autopage()->select();
+        foreach($list as $key => &$val){
+            $val['remark'] = '';
+
+            if($val['type'] == 1){
+                $val['remark'] = '支付宝提现';
+            }elseif($val['type'] == 2){
+                $val['remark'] = '银行卡提现';
+            }else{
+                $val['remark'] = '微信提现';
+            }
+        }
+
+        $this->success('success',$list);
+    }
+
+   //////////////////////////////////////////////
+
+    //提现配置
+    public function take_cash_config(){
+        $config = Db::name('take_cash_config')->order('weigh asc,id asc')->select();
+
+        $plat_bilv = config('site.takecash_plat_bili');
+        foreach($config as $key => &$val){
+            $val['get_money'] = bcdiv(bcmul($val['money'],(100-$plat_bilv),2),100,2);
+        }
+
+        $data = [
+            'config' => $config,
+            'wallet' => model('wallet')->getwallet($this->auth->id),
+            'min' => config('site.min_takecash_money'),
+            'max' => config('site.max_takecash_money'),
+            'plat_bilv' => $plat_bilv,
+            'user_bank' => Db::name('user_bank')->where('user_id',$this->auth->id)->find(),
+            'user_alipay' => Db::name('user_alipay')->where('user_id',$this->auth->id)->find(),
+            'remark' => config('site.take_cash_rule'),
+        ];
+
+        $this->success('success',$data);
+    }
+
+
+}

+ 63 - 0
application/api/controller/doctor/Userwallet.php

@@ -0,0 +1,63 @@
+<?php
+
+namespace app\api\controller\doctor;
+
+use app\common\controller\Apic;
+use think\Db;
+//use app\common\model\wallet;
+/**
+ * 用户钱包
+ */
+class Userwallet extends Apic
+{
+    protected $noNeedLogin = [];
+    protected $noNeedRight = ['*'];
+
+    //我的钱包余额
+    public function my_wallet(){
+        $wallet = Db::name('doctor_wallet')->where(['doctor_id' => $this->auth->id])->find();
+
+        $wallet['user_alipay'] = Db::name('doctor_alipay')->where(['doctor_id' => $this->auth->id])->find();
+        $wallet['user_bank']   = Db::name('doctor_bank')->where(['doctor_id' => $this->auth->id])->find();
+        $wallet['user_wechat'] = Db::name('doctor_wechat')->where(['doctor_id' => $this->auth->id])->find();
+        $this->success('success',$wallet);
+    }
+
+    //我的余额日志
+    public function my_money_log(){
+        $type = input('type',0);
+
+        $map = [
+           'doctor_id' => $this->auth->id,
+        ];
+
+        if($type == 1){
+            $map['change_value'] = ['gt',0];
+        }
+        if($type == 2){
+            $map['change_value'] = ['lt',0];
+        }
+
+        $list = Db::name('doctor_money_log')
+            ->field('id,log_type,before,change_value,remain,remark,createtime')
+            ->where($map)->order('id desc')->autopage()->select();
+        $list = $this->list_appen_logtext($list);
+
+        $this->success('success',$list);
+    }
+
+
+    //追加log_text
+    private function list_appen_logtext($list){
+        if(!empty($list)){
+            $conf = config('wallet.logtype');
+            $conf_en = config('wallet.logtype_en');
+            foreach($list as $key => $val){
+                $list[$key]['log_text'] = isset($conf[$val['log_type']]) ? $conf[$val['log_type']] : '';
+                $list[$key]['log_text_en'] = isset($conf_en[$val['log_type']]) ? $conf_en[$val['log_type']] : '';
+            }
+        }
+        return $list;
+    }
+
+}

+ 171 - 0
application/common/model/Walletdoctor.php

@@ -0,0 +1,171 @@
+<?php
+namespace app\common\model;
+use think\Model;
+use think\Db;
+/**
+ * 货币模型
+ */
+class Walletdoctor 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('doctor_wallet')->lock(true)->where(['doctor_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('doctor_wallet')->lock(true)->where(['doctor_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['doctor_id'] = $user_id;
+                $data['log_type'] = $logtype;
+//                $data['money_type'] = $accountType;
+                $data['before'] = $wallet[$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();
+
+                //新的方式
+                $rs1 = Db::name('doctor_wallet')->where(['doctor_id'=>$user_id])->update([$accountType => $data['remain']]);
+
+
+
+                /////////////
+                $log_table = 'doctor_'.$accountType.'_log';
+
+                $rs2_id = Db::name($log_table)->insertGetId($data);
+
+                if($rs1 === false || $rs2_id === false){
+                    $result['msg'] = '更新财务记录失败!';
+                    return $result;
+                }
+
+                if( $rs1 !== false && $rs2_id !== false )
+                {
+                    $result['status'] = true;
+                    $result['msg'] = '账户余额已更新!';
+                    $result['log_table'] = $log_table;
+                    $result['log_id'] = $rs2_id;
+
+                    return $result;
+                }
+                else
+                {
+                    $result['msg'] = '更新财务记录失败!';
+                    return $result;
+                }
+            } else {
+                $result['msg'] = '金额不足0.01';
+                return $result;
+            }
+        }
+    }
+
+
+
+
+}