75 lines
		
	
	
		
			2.3 KiB
		
	
	
	
		
			PHP
		
	
	
			
		
		
	
	
			75 lines
		
	
	
		
			2.3 KiB
		
	
	
	
		
			PHP
		
	
	
<?php
 | 
						|
 | 
						|
namespace app\traits\account;
 | 
						|
 | 
						|
use app\model\AccountLevel;
 | 
						|
use think\Collection;
 | 
						|
use think\db\exception\DataNotFoundException;
 | 
						|
use think\db\exception\DbException;
 | 
						|
use think\db\exception\ModelNotFoundException;
 | 
						|
 | 
						|
trait AccountLevelTrait
 | 
						|
{
 | 
						|
    /**
 | 
						|
     * 获取用户当前等级
 | 
						|
     * @param $coinTotal int 最大孔雀币数量
 | 
						|
     * @return array
 | 
						|
     */
 | 
						|
    public function getUserLevel($coinTotal)
 | 
						|
    {
 | 
						|
        $level = AccountLevel::where("value", "<=", $coinTotal)
 | 
						|
            ->field("name,rights,content,value")
 | 
						|
            ->withAttr("rights", function ($value) {
 | 
						|
                if (empty($value)) return [];
 | 
						|
                return explode(",", $value);
 | 
						|
            })->order(["value" => "desc", "id" => "desc"])->findOrEmpty()->toArray();
 | 
						|
        if (empty($level)) {
 | 
						|
            return ["name" => "普通会员", "rights" => [], "content" => "", "value" => 0];
 | 
						|
        }
 | 
						|
        return $level;
 | 
						|
    }
 | 
						|
 | 
						|
    //获取下一个等级
 | 
						|
    public function getUserNextLevel($levelValue)
 | 
						|
    {
 | 
						|
        return AccountLevel::where("value", ">", $levelValue)->field("name,rights,content,value")->order(["value" => "asc", "id" => "desc"])->findOrEmpty()->toArray();
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * 获取用户当前等级
 | 
						|
     * @param $coinTotal int 最大孔雀币数量
 | 
						|
     * @param $levelList AccountLevel[]|array|Collection 会员等级列表 getLevelList返回结果
 | 
						|
     * @return array|mixed
 | 
						|
     */
 | 
						|
    public function getCurrentLevel(int $coinTotal, $levelList)
 | 
						|
    {
 | 
						|
        $res = ["name" => "普通会员", "rights" => [], "content" => "", "value" => 0];
 | 
						|
        if ($coinTotal == 0 | !$levelList || empty($levelList) || $levelList->isEmpty()) {
 | 
						|
            return  $res;
 | 
						|
        }
 | 
						|
        $info = $levelList->where('value', "<=", $coinTotal)->shift();
 | 
						|
        if (!$info) {
 | 
						|
            return $res;
 | 
						|
        }
 | 
						|
 | 
						|
        return $info;
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * 获取会员等级列表
 | 
						|
     *
 | 
						|
     * @return AccountLevel[]|array|Collection
 | 
						|
     * @throws DataNotFoundException
 | 
						|
     * @throws DbException
 | 
						|
     * @throws ModelNotFoundException
 | 
						|
     */
 | 
						|
    public function getLevelList()
 | 
						|
    {
 | 
						|
        return AccountLevel::field("name,rights,content,value")
 | 
						|
            ->withAttr("rights", function ($value) {
 | 
						|
                if (empty($value)) return [];
 | 
						|
                return explode(",", $value);
 | 
						|
            })->order(["value" => "desc", "id" => "desc"])->select();
 | 
						|
    }
 | 
						|
 | 
						|
} |