123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- <?php
- namespace addons\cms\model;
- use think\Db;
- /**
- * 会员模型
- */
- class User Extends \app\common\model\User
- {
- protected static $config = [];
- protected static $tagCount = 0;
- protected static function init()
- {
- $config = get_addon_config('cms');
- self::$config = $config;
- }
- public function getUrlAttr($value, $data)
- {
- return $this->buildUrl($value, $data);
- }
- public function getFullurlAttr($value, $data)
- {
- return $this->buildUrl($value, $data, true);
- }
- private function buildUrl($value, $data, $domain = false)
- {
- $vars = [
- ':id' => $data['id'],
- ];
- $suffix = static::$config['moduleurlsuffix']['user'] ?? static::$config['urlsuffix'];
- return addon_url('cms/user/index', $vars, $suffix, $domain);
- }
- /**
- * 获取会员列表
- */
- public static function getUserList($params)
- {
- $config = get_addon_config('cms');
- $name = empty($params['name']) ? '' : $params['name'];
- $condition = empty($params['condition']) ? '' : $params['condition'];
- $field = empty($params['field']) ? '*' : $params['field'];
- $row = empty($params['row']) ? 10 : (int)$params['row'];
- $orderby = empty($params['orderby']) ? 'createtime' : $params['orderby'];
- $orderway = empty($params['orderway']) ? 'desc' : strtolower($params['orderway']);
- $limit = empty($params['limit']) ? $row : $params['limit'];
- $cache = !isset($params['cache']) ? $config['cachelifetime'] === 'true' ? true : (int)$config['cachelifetime'] : (int)$params['cache'];
- $imgwidth = empty($params['imgwidth']) ? '' : $params['imgwidth'];
- $imgheight = empty($params['imgheight']) ? '' : $params['imgheight'];
- $orderway = in_array($orderway, ['asc', 'desc']) ? $orderway : 'desc';
- $paginate = !isset($params['paginate']) ? false : $params['paginate'];
- $cache = !$cache ? false : $cache;
- self::$tagCount++;
- $where = [];
- if ($name !== '') {
- $where['name'] = $name;
- }
- $order = $orderby == 'rand' ? Db::raw('rand()') : (preg_match("/\,|\s/", $orderby) ? $orderby : "{$orderby} {$orderway}");
- $userModel = self::where($where)
- ->where($condition)
- ->field($field)
- ->orderRaw($order);
- if ($paginate) {
- $paginateArr = explode(',', $paginate);
- $listRows = is_numeric($paginate) ? $paginate : (is_numeric($paginateArr[0]) ? $paginateArr[0] : $row);
- $config = [];
- $config['var_page'] = isset($paginateArr[2]) ? $paginateArr[2] : 'upage' . self::$tagCount;
- $config['path'] = isset($paginateArr[3]) ? $paginateArr[3] : '';
- $config['fragment'] = isset($paginateArr[4]) ? $paginateArr[4] : '';
- $config['query'] = request()->get();
- $list = $userModel->paginate($listRows, (isset($paginateArr[1]) ? $paginateArr[1] : false), $config);
- } else {
- $list = $userModel->limit($limit)->cache($cache)->select();
- }
- self::render($list, $imgwidth, $imgheight);
- return $list;
- }
- public static function render(&$list, $imgwidth, $imgheight)
- {
- $width = $imgwidth ? 'width="' . $imgwidth . '"' : '';
- $height = $imgheight ? 'height="' . $imgheight . '"' : '';
- foreach ($list as $k => &$v) {
- $v['textlink'] = '<a href="' . $v['url'] . '">' . $v['nickname'] . '</a>';
- $v['imglink'] = '<a href="' . $v['url'] . '"><img src="' . $v['avatar'] . '" border="" ' . $width . ' ' . $height . ' /></a>';
- $v['img'] = '<img src="' . $v['avatar'] . '" border="" ' . $width . ' ' . $height . ' />';
- }
- return $list;
- }
- }
|