123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- <?php
- declare (strict_types = 1);
- namespace app\model\api;
- use Closure;
- use Firebase\JWT\JWT;
- use library\basic\BaseModel;
- use library\traits\JwtAuthModelTrait;
- use library\traits\ModelTrait;
- use think\facade\Cache;
- use think\facade\Env;
- use think\Model;
- /**
- * @mixin \think\Model
- */
- class Member extends BaseModel
- {
- use ModelTrait;
- use JwtAuthModelTrait;
- /**
- * 获取列表数据
- * @param $page
- * @param $where
- * @param $pageCount
- * @param $desc
- */
- public function getList($page,$where = [],$pageCount = 20,$filed = '*',$desc = ''){
- $data = $this
- ->field("*,(SELECT count(*) from table_order where uid = u.uid and status > 0) as order_count,
- (select name from table_member_level where id = u.levelid) as lavel_name
- ")
- ->alias("u")
- ->when(!empty($where),function ($query) use($where){
- foreach ($where as $k=>$v) {
- if($v instanceof Closure) {
- $v($query);
- } else {
- if(is_array($v)) {
- //whereLike
- if($v[1] == 'whereLike') {
- if(!empty($v[0]))
- $query->whereLike($k,$v[0]);
- continue;
- }
- $bool = false;
- eval('$bool = '.$v[1].'($v[0]);');
- if($bool) {
- $query->where($k,$v[0]);
- }
- } else {
- $query->where($k,$v);
- }
- }
- }
- })
- ->order($desc)
- ->paginate(['list_rows'=>$pageCount,'page'=>$page])
- ->toArray();
- return [$data['total'],$data['data']];
- }
- /**
- * 获取列表数据
- * @param $page
- * @param $where
- * @param $pageCount
- * @param $desc
- */
- public function getItem($uid){
- $data = $this
- ->field("*,(SELECT count(*) from table_order where uid = u.uid and status > 0) as order_count,
- (select name from table_member_level where id = u.levelid) as lavel_name
- ")
- ->alias("u")
- ->where('uid',$uid)
- ->toArray();
- return $data;
- }
- public static function login($userName,$passWord) {
- $info = self::where('mobile',$userName)->find();
- if(empty($info)) return self::setErrorInfo('请输入正确的账号和密码');
- if (!$info['status']) return self::setErrorInfo('账户被停用了!');
- if($info['password'] != md5($passWord)) {
- return self::setErrorInfo('请输入正确的密码');
- }
- return $info;
- }
- /**
- * 生成token存放令牌 | 存放redis
- * @param SystemAdmin $info
- * @param $secret_key
- * @return array|bool
- */
- public static function createToken(Member $info,$secret_key)
- {
- $params['exp'] = 30;
- $params['time'] = time();
- $params['token'] = base64_encode(crypto_encrypt($info->getData('uid'),$secret_key));
- $params['uid'] = $info->getData('uid');
- $token = JWT::encode($params, Env::get('app.app_key', 'default'));
- return $token;
- }
- }
|