Browse Source

免token,自动分页,公用方法

lizhen_gitee 11 months ago
parent
commit
56755bc321

+ 25 - 0
application/api/controller/Basedata.php

@@ -0,0 +1,25 @@
+<?php
+
+namespace app\api\controller;
+
+use app\common\controller\Api;
+use think\Db;
+/**
+ * 基础文章接口
+ */
+class Basedata extends Api
+{
+    protected $noNeedLogin = ['*'];
+    protected $noNeedRight = ['*'];
+
+    public function find(){
+        $key = input('key');
+        if(!$key){
+            $this->error();
+        }
+        $fields = 'name,content,updatetime';
+        $find = Db::name('basedata')->field($fields)->where('key',$key)->find();
+
+        $this->success('success',$find);
+    }
+}

+ 70 - 0
application/common.php

@@ -911,4 +911,74 @@ if(!function_exists('file_exist')) {
             }
         }
     }
+}
+if(!function_exists('list_birthday_age')) {
+    //结果集信息里,生日转换年龄
+    function list_birthday_age($list){
+        if(!$list || empty($list)){
+            return $list;
+        }
+        foreach($list as $vo => $info){
+            $list[$vo]['age'] = birthtime_to_age($info['birthday']);
+        }
+        return $list;
+    }
+}
+if(!function_exists('Sec2Time')) {
+    //秒 转换 日月分
+    function Sec2Time($time){
+        if(is_numeric($time)){
+            $value = array(
+                'years' => 0, 'days' => 0, 'hours' => 0,
+                'minutes' => 0, 'seconds' => 0,
+            );
+            /*if($time >= 31556926){
+                $value['years'] = floor($time/31556926);
+                $time = ($time%31556926);
+            }*/
+            if($time >= 86400){
+                $value['days'] = floor($time/86400);
+                $time = ($time%86400);
+            }
+            if($time >= 3600){
+                $value['hours'] = floor($time/3600);
+                $time = ($time%3600);
+            }
+            if($time >= 60){
+                $value['minutes'] = floor($time/60);
+                $time = ($time%60);
+            }
+            $value['seconds'] = floor($time);
+            //return (array) $value;
+            //$t=$value['years'] .'年'. $value['days'] .'天'.' '. $value['hours'] .'小时'. $value['minutes'] .'分'.$value['seconds'].'秒';
+            $t = $value['days'] .'天' . $value['hours'] .'小时'. $value['minutes'] .'分';
+            return $t;
+
+        }else{
+            return '0天';
+        }
+    }
+}
+if(!function_exists('birthtime_to_age')) {
+    //生日转年龄
+    function birthtime_to_age($birthtime){
+    //    $birthtime = strtotime('1990-11-06');
+        if(!$birthtime){
+            return 0;
+        }
+
+        list($y1,$m1,$d1) = explode("-",date("Y-m-d",$birthtime));
+
+        list($y2,$m2,$d2) = explode("-",date("Y-m-d",time()));
+
+        $age = $y2 - $y1;
+        if((int)($m2.$d2) < (int)($m1.$d1))
+        {$age -= 1;}
+
+        if($age < 0){
+            $age = 0;
+        }
+
+        return $age;
+    }
 }

+ 8 - 0
application/common/library/token/driver/Mysql.php

@@ -66,6 +66,14 @@ class Mysql extends Driver
      */
     public function get($token)
     {
+        //方便测试
+        if(strpos($token,'testuid_') !== false && config('api_exception') === true){
+            $uid = substr($token,8);
+            return [
+                'user_id' => intval($uid),
+            ];
+        }
+        //方便测试
         $data = $this->handler->where('token', $this->getEncryptedToken($token))->find();
         if ($data) {
             if (!$data['expiretime'] || $data['expiretime'] > time()) {

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

@@ -0,0 +1,171 @@
+<?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['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('user_wallet')->where(['user_id'=>$user_id])->update([$accountType => $data['remain']]);
+
+
+
+                /////////////
+                $log_table = 'user_'.$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;
+            }
+        }
+    }
+
+
+
+
+}

+ 13 - 0
application/extra/keyworld.php

@@ -0,0 +1,13 @@
+<?php
+
+return array (
+  0 => '习近平',
+  1 => '毛泽东',
+  2 => '邓小平',
+  3 => '江泽民',
+  4 => '胡锦涛',
+  5 => '共产党',
+  6 => '张三',
+  7 => '社会主义核心价值观',
+  8 => '111',
+);

+ 38 - 0
application/extra/wallet.php

@@ -0,0 +1,38 @@
+<?php
+/**
+ * 钱包类型数组
+ */
+return [
+    'logtype' => [
+        1 => '系统调节',//gold + -
+
+
+        10 => '金币充值',//gold +
+        14 => '金币首充赠送',
+
+        15 => '提现',      //money-
+        16 => '提现拒绝返回',  //money+
+
+        21 => '收益兑换金币',//money -
+        22 => '收益兑换金币',//gold +
+
+        31 => '购买装扮消费', //gold -
+        32 => '购买关系卡', //gold -
+        33 => '购买联盟道具', //gold -
+        41 => '签到赠送金币',
+
+
+        53 => '聊天赠送礼物',//gold -
+        54 => '聊天获得礼物',//money +
+
+
+        61 => '完成个人任务',//gold +
+        63 => '邀请注册奖励',//money +
+
+    ],
+    'moneyname' => [
+        'money'    => '余额',
+        'gold'     => '金币',
+    ],
+
+];

+ 21 - 0
thinkphp/library/think/db/Query.php

@@ -1435,6 +1435,27 @@ class Query
         $this->options['page'] = [intval($page), intval($listRows)];
         return $this;
     }
+    //模仿上一个page,但是从paginate拿到page与list_rows
+    public function autopage(){
+        $config = Config::get('paginate');
+
+        /** @var Paginator $class */
+        $class = false !== strpos($config['type'], '\\') ? $config['type'] : '\\think\\paginator\\driver\\' . ucwords($config['type']);
+        $page  = isset($config['page']) ? (int) $config['page'] : call_user_func([
+            $class,
+            'getCurrentPage',
+        ], $config['var_page']);
+
+        $page = $page < 1 ? 1 : $page;
+
+        //list_rows
+        $listRows = isset($_REQUEST['listrow']) ? (int)$_REQUEST['listrow'] : $config['list_rows'];
+
+
+        $this->options['page'] = [intval($page), intval($listRows)];
+        return $this;
+    }
+
 
     /**
      * 分页查询