Member.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\model\api;
  4. use Closure;
  5. use Firebase\JWT\JWT;
  6. use library\basic\BaseModel;
  7. use library\traits\JwtAuthModelTrait;
  8. use library\traits\ModelTrait;
  9. use think\facade\Cache;
  10. use think\facade\Env;
  11. use think\Model;
  12. /**
  13. * @mixin \think\Model
  14. */
  15. class Member extends BaseModel
  16. {
  17. use ModelTrait;
  18. use JwtAuthModelTrait;
  19. /**
  20. * 获取列表数据
  21. * @param $page
  22. * @param $where
  23. * @param $pageCount
  24. * @param $desc
  25. */
  26. public function getList($page,$where = [],$pageCount = 20,$filed = '*',$desc = ''){
  27. $data = $this
  28. ->field("*,(SELECT count(*) from table_order where uid = u.uid and status > 0) as order_count,
  29. (select name from table_member_level where id = u.levelid) as lavel_name
  30. ")
  31. ->alias("u")
  32. ->when(!empty($where),function ($query) use($where){
  33. foreach ($where as $k=>$v) {
  34. if($v instanceof Closure) {
  35. $v($query);
  36. } else {
  37. if(is_array($v)) {
  38. //whereLike
  39. if($v[1] == 'whereLike') {
  40. if(!empty($v[0]))
  41. $query->whereLike($k,$v[0]);
  42. continue;
  43. }
  44. $bool = false;
  45. eval('$bool = '.$v[1].'($v[0]);');
  46. if($bool) {
  47. $query->where($k,$v[0]);
  48. }
  49. } else {
  50. $query->where($k,$v);
  51. }
  52. }
  53. }
  54. })
  55. ->order($desc)
  56. ->paginate(['list_rows'=>$pageCount,'page'=>$page])
  57. ->toArray();
  58. return [$data['total'],$data['data']];
  59. }
  60. /**
  61. * 获取列表数据
  62. * @param $page
  63. * @param $where
  64. * @param $pageCount
  65. * @param $desc
  66. */
  67. public function getItem($uid){
  68. $data = $this
  69. ->field("*,(SELECT count(*) from table_order where uid = u.uid and status > 0) as order_count,
  70. (select name from table_member_level where id = u.levelid) as lavel_name
  71. ")
  72. ->alias("u")
  73. ->where('uid',$uid)
  74. ->toArray();
  75. return $data;
  76. }
  77. public static function login($userName,$passWord) {
  78. $info = self::where('mobile',$userName)->find();
  79. if(empty($info)) return self::setErrorInfo('请输入正确的账号和密码');
  80. if (!$info['status']) return self::setErrorInfo('账户被停用了!');
  81. if($info['password'] != md5($passWord)) {
  82. return self::setErrorInfo('请输入正确的密码');
  83. }
  84. return $info;
  85. }
  86. /**
  87. * 生成token存放令牌 | 存放redis
  88. * @param SystemAdmin $info
  89. * @param $secret_key
  90. * @return array|bool
  91. */
  92. public static function createToken(Member $info,$secret_key)
  93. {
  94. $params['exp'] = 30;
  95. $params['time'] = time();
  96. $params['token'] = base64_encode(crypto_encrypt($info->getData('uid'),$secret_key));
  97. $params['uid'] = $info->getData('uid');
  98. $token = JWT::encode($params, Env::get('app.app_key', 'default'));
  99. return $token;
  100. }
  101. }