Browse Source

Wesmiler 校企小程序 更新 6.16

wesmiler 3 years ago
parent
commit
d3867584d7

+ 28 - 1
app/api/common.php

@@ -47,4 +47,31 @@ if (!function_exists('getPreview')) {
         }
         return $url;
     }
-}
+}
+
+/**
+ * 时间简称
+ * @param $dateTime 时间戳
+ * @return false|string
+ */
+function getTimeText($dateTime)
+{
+    if($dateTime<=0){
+        return false;
+    }
+
+    $curTime = strtotime(date('Y-m-d'));
+    if($dateTime >= time() - 60){
+        return '刚刚';
+    }else if($dateTime >= $curTime){
+        return date('H:i:s', $dateTime);
+    }else if($dateTime >= $curTime - 86400){
+        return '昨天';
+    }else if($dateTime >= $curTime - 86400*2){
+        return '前天';
+    }else if($dateTime >= strtotime(date('Y-01-01'))){
+        return date('m/d', $dateTime);
+    }else{
+        return date('Y/m/d', $dateTime);
+    }
+}

+ 28 - 1
app/api/controller/Imchat.php

@@ -2,6 +2,9 @@
 
 namespace app\api\controller;
 
+use \app\api\model\Imchat as ImchatModel;
+use app\api\service\User as UserService;
+
 /**
  * 聊天消息控制器
  * Class Imchat
@@ -9,8 +12,32 @@ namespace app\api\controller;
  */
 class Imchat extends Controller
 {
-    public function index()
+
+    /**
+     * 历史消息列表
+     * @return \think\response\Json
+     */
+    public function history()
     {
+        // 获取列表数据
+        $model = new ImchatModel;
+        $pageSize = $this->request->param('pageSize', 15);
+        $list = $model->getList($this->request->param(), $pageSize);
+        return $this->renderSuccess(compact('list'));
+    }
 
+    /**
+     * 获取最新消息用户列表
+     * @return \think\response\Json
+     */
+    public function newList()
+    {
+        // 获取列表数据
+        $model = new ImchatModel;
+        $pageSize = $this->request->param('pageSize', 15);
+        $userInfo = UserService::getCurrentLoginUser(true);
+        $userId = isset($userInfo['user_id'])? intval($userInfo['user_id']) : 0;
+        $list = $model->getListByUser($userId, $this->request->param(), $pageSize);
+        return $this->renderSuccess(compact('list'));
     }
 }

+ 18 - 0
app/api/controller/User.php

@@ -13,6 +13,7 @@ declare (strict_types = 1);
 namespace app\api\controller;
 
 use app\api\model\User as UserModel;
+use app\api\model\UserFans;
 use app\common\exception\BaseException;
 use app\api\model\UserCoupon as UserCouponModel;
 use app\api\service\User as UserService;
@@ -141,4 +142,21 @@ class User extends Controller
         return $this->renderSuccess(compact('list'));
     }
 
+    /**
+     * 获取关注用户列表
+     * @return Json
+     * @throws \cores\exception\BaseException
+     * @throws \think\db\exception\DbException
+     */
+    public function followList()
+    {
+        // 当前用户信息
+        $userInfo = UserService::getCurrentLoginUser(true);
+        $userId = isset($userInfo['user_id'])? intval($userInfo['user_id']) : 0;
+
+        $model = new UserFans;
+        $params = $this->request->param();
+        $list = $model->getFollowList($userId, $params, 15);
+        return $this->renderSuccess(compact('list'));
+    }
 }

+ 126 - 0
app/api/model/Imchat.php

@@ -0,0 +1,126 @@
+<?php
+// +----------------------------------------------------------------------
+// | 商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
+// +----------------------------------------------------------------------
+// | Copyright (c) 2017~2021 https://www.thinkphp.com All rights reserved.
+// +----------------------------------------------------------------------
+// | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
+// +----------------------------------------------------------------------
+// | Author: thinkphp <admin@yiovo.com>
+// +----------------------------------------------------------------------
+declare (strict_types = 1);
+
+namespace app\api\model;
+
+use app\common\model\Imchat as ImchatModel;
+
+/**
+ * 聊天消息模型
+ * Class Imchat
+ * @package app\api\model
+ */
+class Imchat extends ImchatModel
+{
+
+    /**
+     * 获取列表
+     * @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, []);
+        // 获取商品列表
+        $list = parent::getList($params, $listRows);
+        if ($list->isEmpty()) {
+            return $list;
+        }
+
+        // 整理列表数据并返回
+        return $this->setListDataFromApi($list);
+    }
+
+    public function getListByUser(int $userId, array $param = [], int $listRows = 15)
+    {
+        $param = array_merge($param, ['user_id'=> $userId,'data_type'=>'msg']);
+        $list =  $this->alias($this->name)
+                ->leftJoin('user u','u.user_id='.$this->name.'.from_user_id')
+                ->leftJoin('user_info ui','ui.user_id='.$this->name.'.from_user_id')
+                ->where([$this->name.'.to_user_id'=> $userId])
+                ->where(function($query) use($param){
+                    $keyword = isset($param['keyword'])? $param['keyword'] : '';
+                    if($keyword){
+                        $query->where('from_user_name','like',"%{$keyword}%");
+                    }
+                })
+                ->field($this->name.".*, u.user_type,ui.admission_year")
+                ->group($this->name.'.from_user_id')
+                ->order($this->name.'.sendtime','desc')
+                ->paginate($listRows);
+
+        return $this->setListDataFromApi($list, $param);
+    }
+
+    /**
+     * 设置展示的数据 api模块
+     * @param $info
+     * @return mixed
+     */
+    private function setListDataFromApi($info, $params=[])
+    {
+        return $this->setListData($info, function ($data) use ($params){
+
+            // 统计最新未读消息
+            $dataType = isset($params['data_type'])? $params['data_type'] : '';
+            $userId = isset($params['user_id'])? $params['user_id'] : 0;
+            $fromUserId = isset($data['from_user_id'])? $data['from_user_id'] : 0;
+            if($dataType == 'msg'){
+                $data['unread_num'] = $userId && $fromUserId? Imchat::getUnreadCount($userId, $fromUserId) : 0;
+            }
+
+            // 整理数据 api模块
+            $this->setDataFromApi($data);
+
+            // 隐藏冗余的字段
+            $this->hidden(array_merge($this->hidden, ['']));
+        });
+    }
+
+    /**
+     * 整理数据 api模块
+     * @param $info
+     * @return mixed
+     */
+    private function setDataFromApi($info)
+    {
+        return $this->setData($info, function ($data) {
+            // 头像
+            $data['to_user_avatar'] = isset($data['to_user_avatar']) && $data['to_user_avatar']? getPreview($data['to_user_avatar']) : '';
+            $data['from_user_avatar'] = isset($data['from_user_avatar']) && $data['from_user_avatar']? getPreview($data['from_user_avatar']) : '';
+
+            //
+            if(isset($data['user_type'])){
+                $userType = isset($data['user_type'])? $data['user_type'] : 0;
+                $admissionYear = isset($data['admission_year'])? $data['admission_year'] : '';
+                $data['user_type_text'] = $userType == 3? '招生老师' : ($admissionYear? $admissionYear.'级' : '学生');
+            }
+
+            $sendTime = $data['sendtime']? strtotime($data['sendtime']) : 0;
+            $data['sendtime_text'] = $sendTime? getTimeText($sendTime) : '';
+
+        });
+    }
+
+    /**
+     * 获取未读消息数量
+     * @param $userId
+     * @return int
+     */
+    public static function getUnreadCount($userId, $fromUserId)
+    {
+        return self::where(['from_user_id'=> $fromUserId,'to_user_id'=> $userId,'is_push'=>1])->count('id');
+    }
+}

+ 11 - 0
app/api/model/School.php

@@ -268,4 +268,15 @@ class School extends SchoolModel
 
         return $datas;
     }
+
+    /**
+     * 获取学校字段信息
+     * @param $schoolId
+     * @param string $field
+     * @return mixed
+     */
+    public static function getSchoolField($schoolId, $field='school_name')
+    {
+        return self::where(['id'=>$schoolId])->value($field);
+    }
 }

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

@@ -138,5 +138,15 @@ class SourceShool extends SourceShoolModel
         return static::get($filter, $with);
     }
 
+    /**
+     * 获取学校字段信息
+     * @param $schoolId
+     * @param string $field
+     * @return mixed
+     */
+    public static function getSchoolField($schoolId, $field='source_shools_name')
+    {
+        return static::where(['source_shools_id'=>$schoolId])->value($field);
+    }
 
 }

+ 1 - 0
app/api/model/UploadFile.php

@@ -53,4 +53,5 @@ class UploadFile extends UploadFileModel
         ]);
     }
 
+
 }

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

@@ -31,6 +31,70 @@ class UserFans extends UserFansModel
         'update_time'
     ];
 
+    public function getFollowList($userId, array $param = [], int $listRows = 15)
+    {
+        $list = $this->alias($this->name)
+            ->leftJoin('user u','u.user_id='.$this->name.'.user_id')
+            ->leftJoin('user_info ui','ui.user_id='.$this->name.'.user_id')
+            ->where([$this->name.'.fans_id'=> $userId, $this->name.'.status'=> 1,'u.status'=>1,'u.is_delete'=>0])
+            ->field($this->name.'.*,u.nick_name,u.user_type,u.avatar_id,ui.admission_year,ui.school_id')
+            ->group($this->name.'.user_id')
+            ->order($this->name.'.update_time desc, '.$this->name.'.id desc')
+            ->paginate($listRows);
+
+        return $this->setListDataFromApi($list, $param);
+    }
+
+    /**
+     * 设置展示的数据 api模块
+     * @param $info
+     * @return mixed
+     */
+    private function setListDataFromApi($info, $params=[])
+    {
+        return $this->setListData($info, function ($data) use ($params){
+
+            // 整理数据 api模块
+            $this->setDataFromApi($data);
+
+            // 隐藏冗余的字段
+            $this->hidden(array_merge($this->hidden, ['status','avatar_id']));
+        });
+    }
+
+    /**
+     * 整理数据 api模块
+     * @param $info
+     * @return mixed
+     */
+    private function setDataFromApi($info)
+    {
+        return $this->setData($info, function ($data) {
+                // 用户类型
+                $userType = isset($data['user_type'])? $data['user_type'] : 0;
+                $schoolId = isset($data['school_id'])? $data['school_id'] : 0;
+                if($userType == 3){
+                    // 招生学校
+                    $data['school_name'] = $schoolId? School::getSchoolField($schoolId) : '';
+                }else{
+                    // 生源学校
+                    $data['school_name'] = $schoolId? SourceShool::getSchoolField($schoolId) : '';
+                }
+
+                // 用户头像
+                $avatarId = isset($data['avatar_id'])? $data['avatar_id'] : 0;
+                if($avatarId){
+                    $avatarData = UploadFile::detail($avatarId);
+                    $data['avatar_url'] = isset($avatarData['preview_url'])? $avatarData['preview_url'] : '';
+                }
+
+                // 用户类型
+                $admissionYear = isset($data['admission_year'])? $data['admission_year'] : '';
+                $data['user_type_text'] = $userType == 3? '招生老师' : ($admissionYear? $admissionYear.'级' : '学生');
+
+        });
+    }
+
     /**
      * 获取粉丝用户ID
      * @param int $userId
@@ -50,4 +114,6 @@ class UserFans extends UserFansModel
     {
         return self::where(['user_id'=> $userId, 'status'=>1])->count('fans_id');
     }
+
+
 }

+ 114 - 0
app/common/model/Imchat.php

@@ -13,6 +13,8 @@ declare (strict_types = 1);
 namespace app\common\model;
 
 use cores\BaseModel;
+use think\model\Collection;
+use think\Paginator;
 
 /**
  * 聊天消息模型
@@ -30,5 +32,117 @@ class Imchat extends BaseModel
     // 定义主键
     protected $pk = 'id';
 
+    /**
+     * 获取列表
+     * @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)
+            ->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, ['']));
+    }
+
+    /**
+     * 检索查询条件
+     * @param array $params
+     * @return \think\db\BaseQuery
+     */
+    private function getQueryFilter(array $params)
+    {
+        $filter = [];
+
+        // 实例化新查询对象
+        $query = $this->getNewQuery();
+
+        // 发送者
+        !empty($params['from_user_id']) && $filter[] = ['from_user_id', '=', "{$params['from_user_id']}"];
+
+        // 接受者
+        !empty($params['to_user_id']) && $filter[] = ['to_user_id', '=', "{$params['to_user_id']}"];
+
+        // 学校
+        !empty($params['school_id']) && $filter[] = ['school_id', '=', "{$params['school_id']}"];
+
+        // 咨询专业
+        !empty($params['speciality_id']) && $filter[] = ['speciality_id', '=', "{$params['speciality_id']}"];
+
+        // 是否已读
+        !empty($params['is_push']) && $filter[] = ['is_push', '=', "{$params['is_push']}"];
+
+        // 实例化新查询对象
+        return $query->where($filter)->where(function($query) use ($params){
+            // 关键词
+            if(!empty($params['keyword'])){
+                $query->where('school_name','like', "%{$params['keyword']}%");
+            }
+
+
+        });
+    }
+
+
+    /**
+     * 检索排序条件
+     * @param array $param
+     * @return array|string[]
+     */
+    private function setQuerySort(array $param = [])
+    {
+        $params = $this->setQueryDefaultValue($param, [
+            'sortType' => 'all',    // 排序类型
+            'sendtime' => false,   // 排序 (true高到低 false低到高)
+        ]);
+        // 排序规则
+        $sort = [];
+        if ($params['sortType'] === 'all') {
+            $sort = ['sendtime' => 'desc'];
+        } elseif ($params['sortType'] === 'view') {
+            $sort = ['id' => 'desc'];
+        }
+
+        return array_merge($sort, [$this->getPk() => 'desc']);
+    }
 
 }

+ 94 - 0
app/common/model/UserFans.php

@@ -13,7 +13,9 @@ declare (strict_types=1);
 namespace app\common\model;
 
 use cores\BaseModel;
+use think\model\Collection;
 use think\model\relation\HasOne;
+use think\Paginator;
 
 /**
  * 用户粉丝模型类
@@ -28,4 +30,96 @@ class UserFans extends BaseModel
     // 定义主键
     protected $pk = 'id';
 
+    /**
+     * 获取列表
+     * @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)
+            ->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, ['']));
+    }
+
+    /**
+     * 检索查询条件
+     * @param array $params
+     * @return \think\db\BaseQuery
+     */
+    private function getQueryFilter(array $params)
+    {
+        $filter = [];
+
+        // 实例化新查询对象
+        $query = $this->getNewQuery();
+
+        // 实例化新查询对象
+        return $query->where($filter)->where(function($query) use ($params){
+
+        });
+    }
+
+
+    /**
+     * 检索排序条件
+     * @param array $param
+     * @return array|string[]
+     */
+    private function setQuerySort(array $param = [])
+    {
+        $params = $this->setQueryDefaultValue($param, [
+            'sortType' => 'all',    // 排序类型
+            'id' => false,   // 排序 (true高到低 false低到高)
+        ]);
+        // 排序规则
+        $sort = [];
+        if ($params['sortType'] === 'all') {
+            $sort = ['id' => 'desc'];
+        } elseif ($params['sortType'] === 'view') {
+            $sort = ['id' => 'desc'];
+        }
+
+        return array_merge($sort, [$this->getPk() => 'desc']);
+    }
 }