123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228 |
- <?php
- namespace app\common\model;
- use app\common\service\UserService;
- 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,$money,$mode,$before = 0,$remark='',$logtype,$accountType='money',$extend = [])
- {
- //初始化
- $result = array(
- 'status'=>false,
- 'code' => 0, //报错时候采用
- 'msg'=>'',
- 'log_table' => '',
- 'log_id' => '',
- );
- //限制权限
- /*$power_rs = $this->power_limit_logtype($user_id,$logtype);
- if($power_rs !== true){
- $result['msg'] = $power_rs;
- return $result;
- }*/
- //获取小数点
- $point = $accountType == 'money' ? 2 : 0;
- bcscale($point);
- //钱包名称
- $wallet_name = $this->getwalletname($accountType);
- //数字纠正
- $money = abs($money);
- if($mode == '-'){
- $number = -$money;
- }else{
- $number = $money;
- }
- //检测
- $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['code'] = 10;
- $result['msg'] = $wallet_name.'余额不足!';
- return $result;
- }
- if(0 !== bccomp($number, 0))
- {
- //钱币记录
- $data = array();
- $data['user_id'] = $user_id;
- $data['value'] = $money;
- $data['mode'] = $mode;
- $data['before'] = $wallet[$accountType];
- $data['balance'] = bcadd($wallet[$accountType], $number);
- $data['detail'] = $remark;
- $data['createtime'] = time();
- $data['type'] = $logtype;
- if ($accountType == 'jewel') {
- $data['cityname'] = isset($extend['cityname']) ? $extend['cityname'] : '';
- }
- //新的方式
- $rs1 = Db::name('user_wallet')->where(['user_id'=>$user_id])->update([$accountType => $data['balance']]);
- /////////////
- $log_table = 'user_'.$accountType.'_log';
- $rs2_id = Db::name($log_table)->insertGetId($data);
- if($rs1 === false || $rs2_id === false){
- $result['msg'] = '更新财务记录失败!';
- return $result;
- }
- //用不到了
- /*if($accountType == 'jewel'){
- //$userService = new UserService();
- //$userService->updateGuildMember(['user_id'=>$user_id]);
- //更新放到计划任务里,这里做标记
- $rs3 = Db::name('user_power')->where(['user_id'=>$user_id])->update(['guild_status' => 0]);
- }*/
- if( $rs1 !== false && $rs2_id !== false )
- {
- $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;
- }
- }
- //user_power代表用户权限
- //多个字段限制了用户的消费,当某个字段为1代表为禁止,结合logtype,报错到前台
- /*public function power_limit_logtype($user_id,$logtype){
- $enum = [
- 0 => 'payorder',
- 1 => 'recharge',
- // 3 => 'give_gift',//接口处直接判断
- 5 => 'noble',
- 6 => 'attire',
- 11 => 'transfer',
- 13 => 'raffle',
- 104 => 'withdraw',
- ];
- //不受限制
- if(!isset($enum[$logtype])){
- return true;
- }
- $power = Db::name('user_power')->where('user_id',$user_id)->find();
- //没有权限表,直接报错
- if(empty($power)){
- return '不存在的用户';
- }
- $field = $enum[$logtype];
- //受限制
- if($power[$field] == 1){
- $logtype_name = $this->getlogtype($logtype);
- return '您已被限制:'.$logtype_name.',请联系管理员';
- }
- return true;
- }*/
- }
|