Explorar o código

Wesmiler 校企小程序 更新 6.13

wesmiler %!s(int64=4) %!d(string=hai) anos
pai
achega
21f48a1095

+ 11 - 0
app/api/controller/Region.php

@@ -47,4 +47,15 @@ class Region extends Controller
         return $this->renderSuccess(compact('list'));
     }
 
+    /**
+     * 获取区域选项
+     * @return \think\response\Json
+     */
+    public function options()
+    {
+        $pid = $this->request->param('pid', 0);
+        $pid = $pid? (int)$pid : 2069;
+        $list = RegionModel::getCacheOptions($pid);
+        return $this->renderSuccess(compact('list'));
+    }
 }

+ 15 - 3
app/api/controller/User.php

@@ -16,7 +16,6 @@ use app\api\model\User as UserModel;
 use app\common\exception\BaseException;
 use app\api\model\UserCoupon as UserCouponModel;
 use app\api\service\User as UserService;
-use think\facade\Cache;
 use think\response\Json;
 
 /**
@@ -33,8 +32,6 @@ class User extends Controller
      */
     public function info(): Json
     {
-
-        Cache::set('test', 3333, 600);
         // 当前用户信息
         $userInfo = UserService::getCurrentLoginUser(true);
         // 获取用户头像
@@ -92,4 +89,19 @@ class User extends Controller
         return $this->renderSuccess('恭喜您,手机号绑定成功');
     }
 
+    public function seniorList()
+    {
+        // 当前用户信息
+        $userInfo = UserService::getCurrentLoginUser(true);
+        $info = isset($userInfo['info'])? $userInfo['info'] : [];
+        $admissionYear = isset($info['admission_year'])? $info['admission_year'] : 0;
+
+        $model = new UserModel;
+        $params = $this->request->param();
+        $params['admission_year'] = isset($params['admission_year'])? $params['admission_year'] : $admissionYear;
+        $params['user_id'] = $userInfo['user_id'];
+        $list = $model->getList($params, 15);
+        return $this->renderSuccess(compact('list'));
+    }
+
 }

+ 24 - 0
app/api/model/Region.php

@@ -13,6 +13,7 @@ declare (strict_types = 1);
 namespace app\api\model;
 
 use app\common\model\Region as RegionModel;
+use think\facade\Cache;
 
 /**
  * 地区模型
@@ -21,5 +22,28 @@ use app\common\model\Region as RegionModel;
  */
 class Region extends RegionModel
 {
+    protected $globalScope = [''];
 
+    /**
+     * @param int $pid
+     * @return Region[]|array|mixed|\think\Collection|null
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\DbException
+     * @throws \think\db\exception\ModelNotFoundException
+     */
+    public static function getCacheOptions(int $pid = 2069)
+    {
+        $datas = Cache::get("caches:region:options_{$pid}");
+        if($datas){
+            return $datas;
+        }
+
+        $datas = self::where(['pid'=> $pid])
+            ->field('id,name,code')
+            ->select();
+        if($datas){
+            Cache::set("caches:region:options_{$pid}", $datas, rand(120, 300));
+        }
+        return $datas;
+    }
 }

+ 76 - 0
app/api/model/User.php

@@ -40,6 +40,82 @@ class User extends UserModel
     ];
 
     /**
+     * 获取列表
+     * @param array $param 查询条件
+     * @param int $listRows 分页数量
+     * @return mixed|\think\model\Collection|\think\Paginator
+     * @throws \think\db\exception\DbException
+     */
+    public function getList(array $param = [], int $listRows = 15)
+    {
+        // 整理查询参数
+        $params = array_merge($param, ['status' => 1]);
+        // 获取商品列表
+        $list = parent::getList($params, $listRows);
+        if ($list->isEmpty()) {
+            return $list;
+        }
+
+        // 整理列表数据并返回
+        return $this->setListDataFromApi($list);
+    }
+
+    /**
+     * 获取专业同类学校列表
+     * @param array $param 查询条件
+     * @param int $listRows 分页数量
+     * @return mixed|\think\model\Collection|\think\Paginator
+     * @throws \think\db\exception\DbException
+     */
+    public function getSpecialityList(int $specialityId, array $param = [], int $listRows = 15)
+    {
+        // 整理查询参数
+        $params = array_merge($param, ['status' => 1,'is_delete'=>0]);
+        // 获取商品列表
+        $list = parent::getList($params, $listRows);
+        if ($list->isEmpty()) {
+            return $list;
+        }
+
+        // 整理列表数据并返回
+        return $this->setListDataFromApi($list);
+    }
+
+    /**
+     * 设置展示的数据 api模块
+     * @param $info
+     * @return mixed
+     */
+    private function setListDataFromApi($info)
+    {
+        return $this->setListData($info, function ($data){
+            $data['avatar'] = isset($data['avatar'])? $data['avatar'] : [];
+            unset($data['avatar']);
+
+            // 整理数据 api模块
+            $this->setDataFromApi($data);
+
+            // 隐藏冗余的字段
+            $hidden = ['user_type','user_login','home_bg','update_time','grade_id','platform','status','country','province','city','address','address_id','balance','points','last_login_time','pay_money','expend_money','last_login_time'];
+            $this->hidden(array_merge($this->hidden, $hidden));
+        });
+    }
+
+    /**
+     * 整理数据 api模块
+     * @param $info
+     * @return mixed
+     */
+    private function setDataFromApi($info)
+    {
+        return $this->setData($info, function ($data) {
+            $data['region_name'] = $data['region_id']? Region::getNameById($data['region_id']):'';
+            // 粉丝数
+            $data['fans_num'] = $data['user_id']? UserFans::getFansNum($data['user_id']): 0;
+        });
+    }
+
+    /**
      * 获取器:隐藏手机号中间四位
      * @param string $value
      * @return string

+ 10 - 0
app/api/model/UserFans.php

@@ -40,4 +40,14 @@ class UserFans extends UserFansModel
     {
         return self::where(['user_id'=> $userId, 'status'=>1])->column('fans_id');
     }
+
+    /**
+     * 获取粉丝数
+     * @param int $userId
+     * @return array
+     */
+    public static function getFansNum(int $userId)
+    {
+        return self::where(['user_id'=> $userId, 'status'=>1])->count('fans_id');
+    }
 }

+ 120 - 0
app/common/model/User.php

@@ -13,10 +13,12 @@ declare (strict_types=1);
 namespace app\common\model;
 
 use cores\BaseModel;
+use think\model\Collection;
 use think\model\relation\HasOne;
 use think\model\relation\HasMany;
 use think\model\relation\BelongsTo;
 use app\common\model\user\PointsLog as PointsLogModel;
+use think\Paginator;
 
 /**
  * 用户模型类
@@ -35,6 +37,124 @@ class User extends BaseModel
     private $gender = [0 => '未知', 1 => '男', 2 => '女'];
 
     /**
+     * 获取列表
+     * @param array $param 查询条件
+     * @param int $listRows 分页数量
+     * @return mixed
+     * @throws \think\db\exception\DbException
+     */
+    public function getList(array $param = [], int $listRows = 15)
+    {
+        // 筛选条件
+        $query = $this->getQueryFilter($param);
+
+        // 排序条件
+        $sort = $this->setQuerySort($param);
+
+        // 执行查询
+        $list = $query->alias($this->name)
+            ->leftJoin('user_info ui','ui.user_id='.$this->name.'.user_id')
+            ->leftJoin('school_speciality sp','sp.speciality_id=ui.speciality')
+            ->leftJoin('source_shools s','s.source_shools_id=ui.school_id')
+            ->field($this->name.'.*,ui.admission_year,s.region_id,s.source_shools_name as school_name,sp.speciality_name')
+            ->order($sort)
+            ->paginate($listRows);
+
+        // 整理列表数据并返回
+        return $list;
+    }
+
+    /**
+     * 设置商品展示的数据
+     * @param Collection|Paginator $list 商品列表
+     * @param callable|null $callback 回调函数
+     * @return mixed
+     */
+    protected function setListData($list, callable $callback = null)
+    {
+        if ($list->isEmpty()) return $list;
+        // 遍历商品列表整理数据
+        foreach ($list as &$item) {
+            $data = $this->setData($item, $callback);
+        }
+        return $list;
+    }
+
+    /**
+     * 整理数据
+     * @param Collection|static $info
+     * @param callable|null $callback
+     * @return mixed
+     */
+    protected function setData($info, callable $callback = null)
+    {
+        // 回调函数
+        is_callable($callback) && call_user_func($callback, $info);
+        return $info->hidden(array_merge($this->hidden, ['albums']));
+    }
+
+    /**
+     * 检索查询条件
+     * @param array $params
+     * @return \think\db\BaseQuery
+     */
+    private function getQueryFilter(array $params)
+    {
+        $filter = [];
+
+        // 实例化新查询对象
+        $query = $this->getNewQuery();
+
+        // 学校层次,类型
+        !empty($params['region_id']) && $filter[] = ['s.region_id', '=', "{$params['region_id']}"];
+
+        // 实例化新查询对象
+        return $query->where($filter)->where(function($query) use ($params){
+            // 关键词
+            if(!empty($params['keyword'])){
+                $query->where('s.source_shools_name','like', "%{$params['keyword']}%")
+                    ->whereOr('sp.speciality_name','like',"%{$params['keyword']}%");
+            }
+
+            $admissionYear = isset($params['admission_year'])? intval($params['admission_year']) : -1;
+            if($admissionYear>0){
+                $query->where('ui.admission_year','<', $admissionYear)->where('ui.admission_year','>', 0);
+            }else if(isset($params['admission_year'])){
+                $query->where('ui.admission_year','>', 0);
+            }
+
+            /*$userId = isset($params['user_id'])? $params['user_id'] : 0;
+            if($userId>0){
+                $query->whereNotIn($this->name.'.user_id', [$userId]);
+            }*/
+
+        });
+    }
+
+
+    /**
+     * 检索排序条件
+     * @param array $param
+     * @return array|string[]
+     */
+    private function setQuerySort(array $param = [])
+    {
+        $params = $this->setQueryDefaultValue($param, [
+            'sortType' => 'all',    // 排序类型
+            $this->name.'.create_time' => false,   // 热门排序 (true高到低 false低到高)
+        ]);
+        // 排序规则
+        $sort = [];
+        if ($params['sortType'] === 'all') {
+            $sort = [$this->name.'.create_time' => 'desc'];
+        } elseif ($params['sortType'] === 'view') {
+            $sort = [$this->name.'.id' => 'desc'];
+        }
+
+        return array_merge($sort, [$this->getPk() => 'desc']);
+    }
+
+    /**
      * 关联用户头像表
      * @return HasOne
      */