User.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. namespace addons\cms\model;
  3. use addons\cms\library\Service;
  4. use think\Db;
  5. /**
  6. * 会员模型
  7. */
  8. class User Extends \app\common\model\User
  9. {
  10. protected static $config = [];
  11. protected static $tagCount = 0;
  12. protected static function init()
  13. {
  14. $config = get_addon_config('cms');
  15. self::$config = $config;
  16. }
  17. public function getUrlAttr($value, $data)
  18. {
  19. return $this->buildUrl($value, $data);
  20. }
  21. public function getFullurlAttr($value, $data)
  22. {
  23. return $this->buildUrl($value, $data, true);
  24. }
  25. private function buildUrl($value, $data, $domain = false)
  26. {
  27. $vars = [
  28. ':id' => $data['id'],
  29. ];
  30. //$suffix = static::$config['moduleurlsuffix']['user'] ?? static::$config['urlsuffix'];
  31. return addon_url('cms/user/index', $vars, false, $domain);
  32. }
  33. /**
  34. * 获取会员列表
  35. */
  36. public static function getUserList($params)
  37. {
  38. $config = get_addon_config('cms');
  39. $name = empty($params['name']) ? '' : $params['name'];
  40. $condition = empty($params['condition']) ? '' : $params['condition'];
  41. $field = empty($params['field']) ? '*' : $params['field'];
  42. $row = empty($params['row']) ? 10 : (int)$params['row'];
  43. $orderby = empty($params['orderby']) ? 'createtime' : $params['orderby'];
  44. $orderway = empty($params['orderway']) ? 'desc' : strtolower($params['orderway']);
  45. $limit = empty($params['limit']) ? $row : $params['limit'];
  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. list($cacheKey, $cacheExpire) = Service::getCacheKeyExpire('arclist', $params);
  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. list($listRows, $simple, $config) = Service::getPaginateParams('upage' . self::$tagCount, $params);
  63. $list = $userModel->paginate($listRows, $simple, $config);
  64. } else {
  65. $list = $userModel->limit($limit)->cache($cacheKey, $cacheExpire)->select();
  66. }
  67. self::render($list, $imgwidth, $imgheight);
  68. return $list;
  69. }
  70. public static function render(&$list, $imgwidth, $imgheight)
  71. {
  72. $width = $imgwidth ? 'width="' . $imgwidth . '"' : '';
  73. $height = $imgheight ? 'height="' . $imgheight . '"' : '';
  74. foreach ($list as $k => &$v) {
  75. $v['textlink'] = '<a href="' . $v['url'] . '">' . $v['nickname'] . '</a>';
  76. $v['imglink'] = '<a href="' . $v['url'] . '"><img src="' . $v['avatar'] . '" ' . $width . ' ' . $height . ' /></a>';
  77. $v['img'] = '<img src="' . $v['avatar'] . '" ' . $width . ' ' . $height . ' />';
  78. }
  79. return $list;
  80. }
  81. }