User.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace addons\cms\model;
  3. use think\Db;
  4. /**
  5. * 会员模型
  6. */
  7. class User Extends \app\common\model\User
  8. {
  9. protected static $config = [];
  10. protected static $tagCount = 0;
  11. protected static function init()
  12. {
  13. $config = get_addon_config('cms');
  14. self::$config = $config;
  15. }
  16. public function getUrlAttr($value, $data)
  17. {
  18. return $this->buildUrl($value, $data);
  19. }
  20. public function getFullurlAttr($value, $data)
  21. {
  22. return $this->buildUrl($value, $data, true);
  23. }
  24. private function buildUrl($value, $data, $domain = false)
  25. {
  26. $vars = [
  27. ':id' => $data['id'],
  28. ];
  29. $suffix = static::$config['moduleurlsuffix']['user'] ?? static::$config['urlsuffix'];
  30. return addon_url('cms/user/index', $vars, $suffix, $domain);
  31. }
  32. /**
  33. * 获取会员列表
  34. */
  35. public static function getUserList($params)
  36. {
  37. $config = get_addon_config('cms');
  38. $name = empty($params['name']) ? '' : $params['name'];
  39. $condition = empty($params['condition']) ? '' : $params['condition'];
  40. $field = empty($params['field']) ? '*' : $params['field'];
  41. $row = empty($params['row']) ? 10 : (int)$params['row'];
  42. $orderby = empty($params['orderby']) ? 'createtime' : $params['orderby'];
  43. $orderway = empty($params['orderway']) ? 'desc' : strtolower($params['orderway']);
  44. $limit = empty($params['limit']) ? $row : $params['limit'];
  45. $cache = !isset($params['cache']) ? $config['cachelifetime'] === 'true' ? true : (int)$config['cachelifetime'] : (int)$params['cache'];
  46. $imgwidth = empty($params['imgwidth']) ? '' : $params['imgwidth'];
  47. $imgheight = empty($params['imgheight']) ? '' : $params['imgheight'];
  48. $orderway = in_array($orderway, ['asc', 'desc']) ? $orderway : 'desc';
  49. $paginate = !isset($params['paginate']) ? false : $params['paginate'];
  50. $cache = !$cache ? false : $cache;
  51. self::$tagCount++;
  52. $where = [];
  53. if ($name !== '') {
  54. $where['name'] = $name;
  55. }
  56. $order = $orderby == 'rand' ? Db::raw('rand()') : (preg_match("/\,|\s/", $orderby) ? $orderby : "{$orderby} {$orderway}");
  57. $userModel = self::where($where)
  58. ->where($condition)
  59. ->field($field)
  60. ->orderRaw($order);
  61. if ($paginate) {
  62. $paginateArr = explode(',', $paginate);
  63. $listRows = is_numeric($paginate) ? $paginate : (is_numeric($paginateArr[0]) ? $paginateArr[0] : $row);
  64. $config = [];
  65. $config['var_page'] = isset($paginateArr[2]) ? $paginateArr[2] : 'upage' . self::$tagCount;
  66. $config['path'] = isset($paginateArr[3]) ? $paginateArr[3] : '';
  67. $config['fragment'] = isset($paginateArr[4]) ? $paginateArr[4] : '';
  68. $config['query'] = request()->get();
  69. $list = $userModel->paginate($listRows, (isset($paginateArr[1]) ? $paginateArr[1] : false), $config);
  70. } else {
  71. $list = $userModel->limit($limit)->cache($cache)->select();
  72. }
  73. self::render($list, $imgwidth, $imgheight);
  74. return $list;
  75. }
  76. public static function render(&$list, $imgwidth, $imgheight)
  77. {
  78. $width = $imgwidth ? 'width="' . $imgwidth . '"' : '';
  79. $height = $imgheight ? 'height="' . $imgheight . '"' : '';
  80. foreach ($list as $k => &$v) {
  81. $v['textlink'] = '<a href="' . $v['url'] . '">' . $v['nickname'] . '</a>';
  82. $v['imglink'] = '<a href="' . $v['url'] . '"><img src="' . $v['avatar'] . '" border="" ' . $width . ' ' . $height . ' /></a>';
  83. $v['img'] = '<img src="' . $v['avatar'] . '" border="" ' . $width . ' ' . $height . ' />';
  84. }
  85. return $list;
  86. }
  87. }