作者 郭鑫

傻子上传第一次

正在显示 51 个修改的文件 包含 4611 行增加0 行删除

要显示太多修改。

为保证性能只显示 51 of 51+ 个文件。

  1 +.buildpath
  2 +.DS_Store
  3 +.project
  4 +.settings
  5 +.idea
  6 +.data
  7 +.update
  8 +composer.lock
  9 +/public/api
  10 +/public/assets/dist
  11 +/node_modules
  1 +如何贡献我的源代码
  2 +===
  1 +ThinkCMF遵循LGPL开源协议发布,并提供免费使用。
  1 +ThinkCMF 5.0.180123 正式版
  2 +===============
  3 +
  4 +### 系列讲座
  5 +https://www.thinkcmf.com/college.html
  6 +
  7 +### ThinkCMF5主要特性
  8 +* 基于全新 ThinkPHP5.0开发
  9 +* 更规范的代码,遵循PSR-2命名规范和PSR-4自动加载规范
  10 +* 更规范的数据库设计
  11 +* 前后台完全基于bootstrap3
  12 +* 增加 api 模块(需单独下载)
  13 +* 支持 composer 管理第三方库
  14 +* 核心化:独立核心代码包
  15 +* 应用化:开发者以应用的形式增加项目模模块
  16 +* 插件化:更强的插件机制,开发者以插件形式扩展功能
  17 +* 模板化:模板完全傻瓜式,用户无须改动任何代码即可在后台完成模板设计和配置
  18 +* 增加 URL美化功能,支持别名设置,更简单
  19 +* 独立的回收站功能,可以管理所有应用临时删除的数据
  20 +* 统一的资源管理,相同文件只保存一份
  21 +* 注解式的后台菜单管理功能,方便开发者代码管理后台菜单
  22 +* 文件存储插件化,默认支持七牛文件存储插件
  23 +* 模板制作标签化,内置多个cmf标签,方便小白用户
  24 +* 更人性化的导航标签,可以随意定制 html 结构
  25 +* 后台首页插件化,用户可以定制的网站后台首页
  26 +
  27 +### 环境推荐
  28 +> php5.5+
  29 +
  30 +> mysql 5.6+
  31 +
  32 +> 打开rewrite
  33 +
  34 +
  35 +### 最低环境要求
  36 +> php5.4+
  37 +
  38 +> mysql 5.5+ (mysql5.1安装时选择utf8编码,不支持表情符)
  39 +
  40 +> 打开rewrite
  41 +
  42 +
  43 +### 运行环境配置教程
  44 +https://www.thinkcmf.com/topic/1502.html
  45 +
  46 +
  47 +### 自动安装
  48 +> 之前安装过 cmf5的同学,请手动创建`data/install.lock`文件
  49 +
  50 +代码已经加入自动安装程序,如果你在安装中有任何问题请提交 issue!
  51 +
  52 +1. public目录做为网站根目录,入口文件在 public/index.php
  53 +2. 配置好网站,请访问http://你的域名
  54 +
  55 +enjoy your cmf~!
  56 +
  57 +### 系统更新
  58 +如果您是已经安装过 cmf5的用户,请查看 update 目录下的 sql 升级文件,根据自己的下载的程序版本进行更新
  59 +
  60 +### API开发 (支持app,小程序,web)
  61 +如果你需要 `api` 开发请下载:
  62 +ThinkCMF5 API :https://github.com/thinkcmf/thinkcmfapi
  63 +
  64 +### 完整版目录结构
  65 +```
  66 +thinkcmf 根目录
  67 +├─api api目录(核心版不带)
  68 +├─app 应用目录
  69 +│ ├─portal 门户应用目录
  70 +│ │ ├─config.php 应用配置文件
  71 +│ │ ├─common.php 模块函数文件
  72 +│ │ ├─controller 控制器目录
  73 +│ │ ├─model 模型目录
  74 +│ │ └─ ... 更多类库目录
  75 +│ ├─ ... 更多应用
  76 +│ ├─command.php 命令行工具配置文件
  77 +│ ├─common.php 应用公共(函数)文件
  78 +│ ├─config.php 应用(公共)配置文件
  79 +│ ├─database.php 数据库配置文件
  80 +│ ├─tags.php 应用行为扩展定义文件
  81 +│ └─route.php 路由配置文件
  82 +├─data 数据目录
  83 +│ ├─conf 动态配置目录
  84 +│ ├─runtime 应用的运行时目录(可写)
  85 +│ └─ ... 更多
  86 +├─public WEB 部署目录(对外访问目录)
  87 +│ ├─api api入口目录(核心版不带)
  88 +│ ├─plugins 插件目录
  89 +│ ├─static 静态资源存放目录(css,js,image)
  90 +│ ├─themes 前后台主题目录
  91 +│ │ ├─admin_simpleboot3 后台默认主题
  92 +│ │ └─simpleboot3 前台默认主题
  93 +│ ├─upload 文件上传目录
  94 +│ ├─index.php 入口文件
  95 +│ ├─robots.txt 爬虫协议文件
  96 +│ ├─router.php 快速测试文件
  97 +│ └─.htaccess apache重写文件
  98 +├─simplewind
  99 +│ ├─cmf CMF核心库目录
  100 +│ ├─extend 扩展类库目录
  101 +│ ├─thinkphp thinkphp目录
  102 +│ └─vendor 第三方类库目录(Composer)
  103 +├─composer.json composer 定义文件
  104 +├─LICENSE.txt 授权说明文件
  105 +├─README.md README 文件
  106 +├─think 命令行入口文件
  107 +```
  108 +
  109 +### 开发手册
  110 +http://www.kancloud.cn/thinkcmf/doc
  111 +
  112 +### QQ群:
  113 +`ThinkCMF 官方交流群`:316669417
  114 +
  115 +`ThinkCMF 高级交流群`:100828313 (付费)
  116 +高级群专属权益:
  117 +第一波:两个后台风格(ThinkCMF官网风格后台主题,蓝色风格后台主题)
  118 +第二波:ThinkCMF5完全开发手册离线版(PDF,EPUB,MOBI格式)
  119 +更多专属权益正在路上...
  120 +
  121 +`ThinkCMF 铲屎官交流群`:415136742 (生活娱乐,为有喵的猿人准备)
  122 +
  123 +### 话题专区
  124 +http://www.thinkcmf.com/topic/index/index/cat/11.html
  125 +
  126 +### 反馈问题
  127 +https://github.com/thinkcmf/thinkcmf/issues
  128 +
  129 +### 更新日志
  130 +#### 5.0.180123
  131 +[核心]
  132 +* 增加小程序管理插件
  133 +* 增加插件后台首页左侧菜单显示
  134 +* 增加 themes 根命名空间
  135 +* 增加模板设计图片模板变量取消功能
  136 +* 增加插件自定义处理配置功能
  137 +* 增加插件后台权限管理功能
  138 +* 增加后台模板切换
  139 +* 增加直传云存储功能
  140 +* 增强导航和子导航标签,自定义更随意
  141 +* 增加before_content,fetch_upload_view,log_write_done,switch_admin_theme钩子
  142 +* 增加PluginAdminBaseController基类
  143 +* 增加系统钩子同步
  144 +* 增加插件中可使用$site_info变量
  145 +* 增加 xml 生成函数
  146 +* 增加插件设置上传文件组件
  147 +* 优化数字验证码日志写入增加过期时间配置
  148 +* 优化数字验证码逻辑,增加数字验证码发送图片验证码,【升级时注意界面逻辑】
  149 +* 优化验证码生成功能,可增加验证码插件管理验证码生成
  150 +* 优化钩子插件管理
  151 +* 优化插件注册机制
  152 +* 优化后台首页菜单加载
  153 +* 修复模板管理变量数据为array时删除出错 #392
  154 +* 修复后台管理搜索翻页时条件丢失问题 #366
  155 +* 修复删除第三方用户时报错 #368
  156 +* 修复在使用cdn加速js时后台文章编辑器时无法加载编辑器配置
  157 +* 修复模板设计模板变量file类型不支持上传 #136
  158 +* 修复用户行为周期设置无效 #382
  159 +* 修复个人信息编辑签名验证问题
  160 +* 修复用户生日早于1970年报错
  161 +* 修复地址坐标选择搜索后无法确定坐标问题
  162 +* 优化IE8,9下的兼容问题
  163 +* 优化前台未登录时跳转方式
  164 +
  165 +[安装程序]
  166 +* 增加安装时管理员密码长度限制 #334
  167 +* 增加安装时检查 rewrite设置
  168 +* 增加安装时 innodb 检测
  169 +* 更正PHP版本要求
  170 +
  171 +[门户应用]
  172 +* 优化portal:articles标签可在模板里设置分页参数和样式
  173 +* 优化portal:articles标签所有属性都支持PHP变量
  174 +* 优化标签控制器支持标签名
  175 +* 增加portal:tagArticles标签
  176 +* 取消文章列表用户关联查询
  177 +* 修复文章多分类进文章列表文章重复问题
  178 +
  179 +
  180 +#### 5.0.170927
  181 +[核心]
  182 +* 增加是否开放注册设置
  183 +* 增加已经安装模板文件检测是否已经删除功能
  184 +* 增加模板卸载风险提示
  185 +* 增加钩子同步功能
  186 +* 增加用户操作同步功能 #291
  187 +* 增加网站信息【$site_info】变量,可以在插件中使用 #310
  188 +* 修复添加管理员不能登录 #110
  189 +* 优化 admin.js
  190 +* 优化后台模板设计排版
  191 +* 优化后台加密码设置
  192 +* 返回按钮统一优化
  193 +* 优化 url 美化时规划选择
  194 +* 修复`api`模块缺少函数报错
  195 +* 修复回收站还原提示错误 #111
  196 +* 修复原始网址和显示网址同时有参数的情况下,两个参数值相同的时候不能解析URL
  197 +* 修复模板设计数组编辑功能缺失
  198 +* 修复后台登录在双核浏览器下会使用 IE 内核问题#168
  199 +* 修复模板widget只有数组时,后台设计保存时报错
  200 +* 修复日期选择在windows firefox下报错
  201 +* 修复模板设计数据源页面清空链接错误
  202 +* 修复后台模板设计,json文件中的数组数据,不能正常显示 #222
  203 +* 修复`cmf\lib\Auth\check`方法逻辑问题 #252
  204 +* 修复后台用户登录自动退出后iframe页跳转到首页的问题
  205 +* 修复用户个人资料修改问题
  206 +* 修复绑定手机号和绑定邮箱号惟一性提示信息错
  207 +
  208 +[安装程序]
  209 +* 更改安装时数据库默认为127.0.0.1
  210 +* 优化安装时链接生成
  211 +
  212 +[门户应用]
  213 +* 增加前台文章控制器默认分类指定
  214 +* 增加后台文章列表所在分类列
  215 +* 增加后台文章分类必须指定分类验证
  216 +* 增加 portal:articles 标签 limit可以设置变量
  217 +* 增加模板设计页面数据源
  218 +* 完善 ApiService获取指定分类下的所有子分类方法
  219 +* 增加portal:categories,portal:subCategories,portal:allSubCategories标签
  220 +* 增加文章、页面、分类模板选择时模板文件名称查看
  221 +* 增加文章保存钩子
  222 +* 优化指定分类下所有子分类获取方法
  223 +* 修复文章分类管理中不保存选择的模板 #107
  224 +* 修复面包屑标签 self属性无法识别 false
  225 +* 修复后台编辑文件会覆盖原作者ID #175
  226 +* 修复后台文章保存后排序变化问题
  227 +* 修复添加文章分类时 path 没数据问题
  228 +
  229 +[升级指导]
  230 +https://www.kancloud.cn/thinkcmf/doc/327443
  231 +
  232 +#### 5.0.170607
  233 +[核心]
  234 +* 删除 app/common.php
  235 +* 规范 admin.js frontend.js函数名
  236 +* 更改后台模板设计的模板文件列表排序规则为从小到大排序
  237 +* 增加模板切换钩子,方便开发者实现复杂的模板切换功能
  238 +* 增加插件作者和演示信息
  239 +* 增加数字验证码模板编辑功能
  240 +* 增加模板变量编辑控件color
  241 +* 增加插件配置组件时间,图片,地理位置,颜色
  242 +* 优化模板配置更新
  243 +* 优化文件上传,检查已经上传文件是否存在,不存在重新上传
  244 +* 修复插件增加新配置时报错
  245 +* 修复模板变量 rule 规则存在,但没有规则时模板设计保存会报错
  246 +* 修复后台清除缓存后url生成不美化
  247 +* 修复模板设计一个页面有多个数组编辑问题
  248 +* 修复cdn设置不生效
  249 +* 修复后台菜单添加子菜单不选择上级问题
  250 +* 修复后台可能多个滚动条
  251 +* 修复后台添加、编辑角色一处文字错误
  252 +* 修复插件更新时不更新新增的钩子
  253 +
  254 +[门户应用]
  255 +* 完善前台模板钩子
  256 +* 完善文章标签功能
  257 +* 增加前台模板手机注册关闭开关
  258 +* 优化文章后台文章分类链接生成
  259 +* 修复ff下文章相册图片替换和删除问题
  260 +* 修复文章分类排序功能
  261 +
  262 +#### 5.0.170520
  263 +[核心]
  264 +* 完善插件后台管理
  265 +* 后台登录插件化
  266 +* 后台首页插件化
  267 +* 文件存储插件化
  268 +* 增加 URL 美化功能
  269 +* 增加后台加密码功能
  270 +* 增加用户修改头像
  271 +* 增加插件设置表单验证
  272 +* 增加前台后台通用语言包
  273 +* 增加编辑器里上传文件链接替换
  274 +* 增加应用 command.php 配置文件
  275 +* 增加后台管理员添加编辑用户名,邮箱惟一性验证
  276 +* 优化安装程序
  277 +* 优化上传文件
  278 +* 优化后台首页
  279 +* 优化回收站
  280 +* 优化插件启用禁用
  281 +* 优化小屏下后台首页不兼容问题
  282 +* 优化后台图片查看
  283 +* 修复后台菜单编辑不生效
  284 +* 修复幻灯片添加不显示问题
  285 +* 修复导航数据源数据返回为空时报错
  286 +* 修复 pathinfo 模式下后台本站用户默认头像不显示问题
  287 +* 修复后台 cdn 不能设置
  288 +* 合并asset应用到 user
  289 +
  290 +[门户应用]
  291 +* 增加文章收藏功能
  292 +* 增加文章点赞限制,一个用户只能点赞一次
  293 +* 增加文章分类缩略图
  294 +* 优化文章分类管理删除
  295 +* 优化文章页和页面页内容图片样式问题
  296 +* 修复文章添加编辑默认图片错误
  297 +* 修复分类下没有文章时报错
  298 +* 修复页面模板设置无效
  299 +* 修复页面删除后仍可以访问
  300 +
  301 +#### 5.0.170505
  302 +[核心]
  303 +* 完善用户注册流程
  304 +* 完善插件功能
  305 +* 增加手机验证码发送钩子
  306 +* 增加手机验证码发送演示插件
  307 +* 增加用户邮箱绑定
  308 +* 增加用户手机绑定
  309 +* 增加常用模板钩子
  310 +* 增加模板设计图片上传
  311 +* 增加用户密码修改
  312 +* 增加用户收藏功能
  313 +* 增加导航标签,子导航标签增加 `max-level` 设置
  314 +* 修复邮箱验证码发送
  315 +* 修复windows下获取模板数据时模板文件路径问题
  316 +* 修复单文件,多文件上传
  317 +* 修复后台首页用户昵称一直显示admin
  318 +* 修复 `navigation`,`subNavigation` 标签两个以上不能同时使用问题
  319 +* 修复 console 模式报错
  320 +* 取消前台有错误时界面刷新
  321 +
  322 +[门户应用]
  323 +* 增加文章附件功能
  324 +* 优化文章相册
  325 +
  326 +#### 5.0.170422
  327 +[核心]
  328 +* 完善幻灯片
  329 +* 完善后台控制器方法注释
  330 +* 增加调试模式下实时更新模板配置
  331 +* 增加友情链接图片上传
  332 +* 增加应用公共语言包功能
  333 +* 增加资源管理
  334 +* 增加模板设计数据源层级关系
  335 +* 更新jQuery Form版本
  336 +* 增加后台菜单类型是否有界面区分
  337 +* 增加权限验证时权限规则里没有的规则不用验证
  338 +* 增加前台网站信息获取
  339 +* 优化后台菜单导入
  340 +* 统一排序规则,按从小到大排序
  341 +* 修复后台模板管理点更新提示卸载
  342 +* 修复标签`NavigationMenu`
  343 +* 修复菜单导入时未添加权限规则
  344 +* 修复`navigationFolder`设置多个子菜单后会多循环数据
  345 +* 修复部分代码php5.4下不兼容
  346 +* 修复后台菜单不能添加编辑
  347 +
  348 +[门户应用]
  349 +* 完全独立门户应用
  350 +* 完善后台页面管理
  351 +* 完善面包屑标签`breadcrumb`
  352 +* 完善文章分类管理
  353 +* 完善文章管理
  354 +* 修复文章分类`path`更新
  355 +* 优化文章列表标签`articles`
  356 +* 优化后台文章分类选择
  357 +* 增加前台文章点赞功能
  358 +* 增加前台文章搜索功能
  359 +* 增加文章列表分页总数获取
  360 +
  361 +#### 5.0.170401
  362 +* 完善文件上传
  363 +* 增加回收站功能
  364 +* 完善友情链接
  365 +* 优化网站设置
  366 +* 增加后台登陆验证码
  367 +* 修复后台用户密码修改
  368 +* 修复用户管理-本站用户头像不显示
  369 +* 完善前台用户登录注册
  370 +* 增加后台菜单导入
  371 +* 修复后台菜单列表排序
  372 +* 完善导航
  373 +* 增加插件钩子管理
  374 +* 完善前台模板
  375 +
  376 +
  1 +<?php
  2 +// +----------------------------------------------------------------------
  3 +// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4 +// +----------------------------------------------------------------------
  5 +// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
  6 +// +----------------------------------------------------------------------
  7 +// | Author: Dean <zxxjjforever@163.com>
  8 +// +----------------------------------------------------------------------
  9 +namespace api\admin\controller;
  10 +
  11 +use cmf\controller\RestBaseController;
  12 +use think\Db;
  13 +use think\Validate;
  14 +
  15 +class PublicController extends RestBaseController
  16 +{
  17 +
  18 + // 用户登录 TODO 增加最后登录信息记录,如 ip
  19 + public function login()
  20 + {
  21 + $validate = new Validate([
  22 + 'username' => 'require',
  23 + 'password' => 'require'
  24 + ]);
  25 + $validate->message([
  26 + 'username.require' => '请输入手机号,邮箱或用户名!',
  27 + 'password.require' => '请输入您的密码!'
  28 + ]);
  29 +
  30 + $data = $this->request->param();
  31 + if (!$validate->check($data)) {
  32 + $this->error($validate->getError());
  33 + }
  34 +
  35 + $userQuery = Db::name("user");
  36 + if (Validate::is($data['username'], 'email')) {
  37 + $userQuery = $userQuery->where('user_email', $data['username']);
  38 + } else if (preg_match('/(^(13\d|15[^4\D]|17[013678]|18\d)\d{8})$/', $data['username'])) {
  39 + $userQuery = $userQuery->where('mobile', $data['username']);
  40 + } else {
  41 + $userQuery = $userQuery->where('user_login', $data['username']);
  42 + }
  43 +
  44 + $findUser = $userQuery->find();
  45 +
  46 + if (empty($findUser)) {
  47 + $this->error("用户不存在!");
  48 + } else {
  49 +
  50 + switch ($findUser['user_status']) {
  51 + case 0:
  52 + $this->error('您已被拉黑!');
  53 + case 2:
  54 + $this->error('账户还没有验证成功!');
  55 + }
  56 +
  57 + if (!cmf_compare_password($data['password'], $findUser['user_pass'])) {
  58 + $this->error("密码不正确!");
  59 + }
  60 + }
  61 +
  62 + $allowedDeviceTypes = ['mobile', 'android', 'iphone', 'ipad', 'web', 'pc', 'mac'];
  63 +
  64 + if (empty($data['device_type']) || !in_array($data['device_type'], $allowedDeviceTypes)) {
  65 + $this->error("请求错误,未知设备!");
  66 + }
  67 +
  68 + $userTokenQuery = Db::name("user_token")
  69 + ->where('user_id', $findUser['id'])
  70 + ->where('device_type', $data['device_type']);
  71 + $findUserToken = $userTokenQuery->find();
  72 + $currentTime = time();
  73 + $expireTime = $currentTime + 24 * 3600 * 180;
  74 + $token = md5(uniqid()) . md5(uniqid());
  75 + if (empty($findUserToken)) {
  76 + $result = $userTokenQuery->insert([
  77 + 'token' => $token,
  78 + 'user_id' => $findUser['id'],
  79 + 'expire_time' => $expireTime,
  80 + 'create_time' => $currentTime,
  81 + 'device_type' => $data['device_type']
  82 + ]);
  83 + } else {
  84 + $result = $userTokenQuery
  85 + ->where('user_id', $findUser['id'])
  86 + ->where('device_type', $data['device_type'])
  87 + ->update([
  88 + 'token' => $token,
  89 + 'expire_time' => $expireTime,
  90 + 'create_time' => $currentTime
  91 + ]);
  92 + }
  93 +
  94 +
  95 + if (empty($result)) {
  96 + $this->error("登录失败!");
  97 + }
  98 +
  99 + $this->success("登录成功!", ['token' => $token]);
  100 + }
  101 +
  102 + // 管理员退出
  103 + public function logout()
  104 + {
  105 + $userId = $this->getUserId();
  106 + Db::name('user_token')->where([
  107 + 'token' => $this->token,
  108 + 'user_id' => $userId,
  109 + 'device_type' => $this->deviceType
  110 + ])->update(['token' => '']);
  111 +
  112 + $this->success("退出成功!");
  113 + }
  114 +
  115 +}
  1 +<?php
  2 +// +----------------------------------------------------------------------
  3 +// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4 +// +----------------------------------------------------------------------
  5 +// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
  6 +// +----------------------------------------------------------------------
  7 +// | Author: Dean <zxxjjforever@163.com>
  8 +// +----------------------------------------------------------------------
  9 +
  10 +$apps = cmf_scan_dir(APP_PATH . '*', GLOB_ONLYDIR);
  11 +
  12 +$returnCommands = [];
  13 +
  14 +foreach ($apps as $app) {
  15 + $commandFile = APP_PATH . $app . '/command.php';
  16 +
  17 + if (file_exists($commandFile)) {
  18 + $commands = include $commandFile;
  19 +
  20 + $returnCommands = array_merge($returnCommands, $commands);
  21 + }
  22 +
  23 +}
  24 +
  25 +return $returnCommands;
  1 +<?php
  2 +namespace api\common\exception;
  3 +
  4 +/**
  5 + * Created by PhpStorm.
  6 + * User: Administrator
  7 + * Date: 2017/11/28
  8 + * Time: 15:27
  9 + */
  10 +use Exception;
  11 +use think\exception\Handle;
  12 +use think\exception\HttpException;
  13 +
  14 +class Http extends Handle
  15 +{
  16 + public function render(Exception $e)
  17 + {
  18 + if (APP_DEBUG==true) {
  19 + return parent::render($e);
  20 + } elseif ($e instanceof ValidateException) {
  21 + $msg=$e->getMessage();
  22 + } else {
  23 + $msg= '系统错误!';
  24 + }
  25 + $code=0;
  26 + $httpCode=500;
  27 + $result = [
  28 + 'code' => $code,
  29 + 'msg' => $msg,
  30 + 'data' => [],
  31 + ];
  32 + return json($result, $httpCode);
  33 + }
  34 +}
  1 +<?php
  2 +// +----------------------------------------------------------------------
  3 +// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4 +// +----------------------------------------------------------------------
  5 +// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
  6 +// +----------------------------------------------------------------------
  7 +// | Author: pl125 <xskjs888@163.com>
  8 +// +----------------------------------------------------------------------
  9 +namespace api\common\model;
  10 +
  11 +use think\Model;
  12 +use think\Loader;
  13 +
  14 +class CommonModel extends Model
  15 +{
  16 + // 关联模型过滤
  17 + protected $relationFilter = [];
  18 +
  19 + /**
  20 + * @access public
  21 + * @param array $params 过滤参数
  22 + * @return array|collection 查询结果
  23 + */
  24 + public function getDatas($params = [])
  25 + {
  26 + if (empty($params)) {
  27 + return $this->select();
  28 + }
  29 +
  30 + $this->setCondition($params);
  31 + if (!empty($params['id'])) {
  32 + $datas = $this->find();
  33 + } else {
  34 + $datas = $this->select();
  35 + }
  36 +
  37 + if (!empty($params['relation'])) {
  38 + $allowedRelations = $this->allowedRelations($params['relation']);
  39 + if (!empty($allowedRelations)) {
  40 + if (!empty($params['id'])) {
  41 + if (!empty($datas)) {
  42 + $datas->append($allowedRelations);
  43 + }
  44 + } else {
  45 + if (count($datas) > 0) {
  46 + $datas->load($allowedRelations);
  47 + $datas->append($allowedRelations);
  48 + }
  49 + }
  50 + }
  51 + }
  52 +
  53 + return $datas;
  54 + }
  55 +
  56 + /**
  57 + * @access public
  58 + * @param array $params 过滤参数
  59 + * @return $this
  60 + */
  61 + public function setCondition($params)
  62 + {
  63 + if (empty($params)) {
  64 + return $this;
  65 + }
  66 + if (!empty($params['relation'])) {
  67 + $allowedRelations = $this->allowedRelations($params['relation']);
  68 + if (!empty($allowedRelations)) {
  69 + if (!empty($params['id']) && count($allowedRelations) == 1) {
  70 + $this->paramsFilter($params);
  71 + } else {
  72 + $this->paramsFilter($params);//->with($allowedRelations);
  73 + }
  74 + }
  75 + } else {
  76 + $this->paramsFilter($params);
  77 + }
  78 + return $this;
  79 + }
  80 +
  81 + /**
  82 + * @access public
  83 + * @param array $params 过滤参数
  84 + * @param model $model 关联模型
  85 + * @return model|array $this|链式查询条件数组
  86 + */
  87 + public function paramsFilter($params, $model = null)
  88 + {
  89 + if (!empty($model)) {
  90 + $_this = $model;
  91 + } else {
  92 + $_this = $this;
  93 + }
  94 +
  95 + if (isset($_this->visible)) {
  96 + $whiteParams = $_this->visible;
  97 + }
  98 +
  99 + // 设置field字段过滤
  100 + if (!empty($params['field'])) {
  101 + $filterParams = $this->strToArr($params['field']);
  102 + if (!empty($whiteParams)) {
  103 + $mixedField = array_intersect($filterParams, $whiteParams);
  104 + } else {
  105 + $mixedField = $filterParams;
  106 + }
  107 +
  108 + if (!empty($mixedField)) {
  109 + $_this->field($mixedField);
  110 + }
  111 + }
  112 +
  113 + // 设置id,ids
  114 + if (!empty($params['ids'])) {
  115 + $ids = $this->strToArr($params['ids']);
  116 + foreach ($ids as $key => $value) {
  117 + $ids[$key] = intval($value);
  118 + }
  119 + }
  120 +
  121 + if (!empty($params['id'])) {
  122 + $id = intval($params['id']);
  123 + if (!empty($id)) {
  124 + return $_this->where('id', $id);
  125 + }
  126 + } elseif (!empty($ids)) {
  127 + $_this->where('id', 'in', $ids);
  128 + }
  129 +
  130 + if (!empty($params['where'])) {
  131 + if (empty($model)) {
  132 + $_this->where($params['where']);
  133 + }
  134 + }
  135 +
  136 +
  137 + // 设置分页
  138 + if (!empty($params['page'])) {
  139 + $pageArr = $this->strToArr($params['page']);
  140 + $page = [];
  141 + foreach ($pageArr as $value) {
  142 + $page[] = intval($value);
  143 + }
  144 + if (count($page) == 1) {
  145 + $_this->page($page[0]);
  146 + } elseif (count($page) == 2) {
  147 + $_this->page($page[0], $page[1]);
  148 + }
  149 + } elseif (!empty($params['limit'])) { // 设置limit查询
  150 + $limitArr = $this->strToArr($params['limit']);
  151 + $limit = [];
  152 + foreach ($limitArr as $value) {
  153 + $limit[] = intval($value);
  154 + }
  155 + if (count($limit) == 1) {
  156 + $_this->limit($limit[0]);
  157 + } elseif (count($limit) == 2) {
  158 + $_this->limit($limit[0], $limit[1]);
  159 + }
  160 + } else {
  161 + $_this->limit(10);
  162 + }
  163 +
  164 + //设置排序
  165 + if (!empty($params['order'])) {
  166 + $order = $this->strToArr($params['order']);
  167 + foreach ($order as $key => $value) {
  168 + $upDwn = substr($value, 0, 1);
  169 + $orderType = $upDwn == '-' ? 'desc' : 'asc';
  170 + $orderField = substr($value, 1);
  171 + if (!empty($whiteParams)) {
  172 + if (in_array($orderField, $whiteParams)) {
  173 + $orderWhere[$orderField] = $orderType;
  174 + }
  175 + } else {
  176 + $orderWhere[$orderField] = $orderType;
  177 + }
  178 + }
  179 +
  180 + if (!empty($orderWhere)) {
  181 + $_this->order($orderWhere);
  182 + }
  183 + }
  184 +
  185 + return $_this;
  186 + }
  187 +
  188 + /**
  189 + * 设置链式查询
  190 + * @access public
  191 + * @param array $params 链式查询条件
  192 + * @param model $model 模型
  193 + * @return $this
  194 + */
  195 + public function setParamsQuery($params, $model = null)
  196 + {
  197 + if (!empty($model)) {
  198 + $_this = $model;
  199 + } else {
  200 + $_this = $this;
  201 + }
  202 + $_this->alias('articles');
  203 + if (!empty($params['field'])) {
  204 + $_this->field($params['field']);
  205 + }
  206 + if (!empty($params['ids'])) {
  207 + $_this->where('articles.id', $params['ids'][1], $params['ids'][2]);
  208 + }
  209 + if (!empty($params['limit'])) {
  210 + $_this->limit($params['limit']);
  211 + }
  212 + if (!empty($params['page'])) {
  213 + $_this->page($params['page']);
  214 + }
  215 + if (!empty($params['order'])) {
  216 + $_this->order($params['order']);
  217 + }
  218 + return $_this;
  219 + }
  220 +
  221 + public function allowedRelations($relations)
  222 + {
  223 + if (is_string($relations)) {
  224 + $relations = explode(',', $relations);
  225 + }
  226 +
  227 + if (!is_array($relations)) {
  228 + return false;
  229 + }
  230 +
  231 + return array_intersect($this->relationFilter, $relations);
  232 + }
  233 +
  234 + /**
  235 + * 是否允许关联
  236 + * @access public
  237 + * @param string $relationName 模型关联方法名
  238 + * @return boolean
  239 + */
  240 + public function isWhite($relationName)
  241 + {
  242 + if (!is_string($relationName)) {
  243 + return false;
  244 + }
  245 + $name = Loader::parseName($relationName, 1, false);
  246 + if (in_array($name, $this->relationFilter)) {
  247 + return true;
  248 + } else {
  249 + return false;
  250 + }
  251 + }
  252 +
  253 + /**
  254 + * 懒人函数
  255 + * @access public
  256 + * @param string $value 字符串
  257 + * @return array
  258 + */
  259 + public function strToArr($string)
  260 + {
  261 + return is_string($string) ? explode(',', $string) : $string;
  262 + }
  263 +}
  1 +<?php
  2 +// +----------------------------------------------------------------------
  3 +// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4 +// +----------------------------------------------------------------------
  5 +// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
  6 +// +----------------------------------------------------------------------
  7 +// | Author: Dean <zxxjjforever@163.com>
  8 +// +----------------------------------------------------------------------
  9 +
  10 +return [
  11 + // +----------------------------------------------------------------------
  12 + // | 应用设置
  13 + // +----------------------------------------------------------------------
  14 +
  15 + // 应用命名空间
  16 + 'app_namespace' => 'api',
  17 + // 应用模式状态
  18 + 'app_status' => APP_DEBUG ? 'debug' : 'release',
  19 + // 是否支持多模块
  20 + 'app_multi_module' => true,
  21 + // 入口自动绑定模块
  22 + 'auto_bind_module' => false,
  23 + // 注册的根命名空间
  24 + 'root_namespace' => ['cmf' => CMF_PATH, 'plugins' => PLUGINS_PATH, 'app' => CMF_ROOT . 'app/'],
  25 + // 扩展函数文件
  26 + 'extra_file_list' => [THINK_PATH . 'helper' . EXT, CMF_PATH . 'common' . EXT],
  27 + // 默认输出类型
  28 + 'default_return_type' => 'json',
  29 + // 默认AJAX 数据返回格式,可选json xml ...
  30 + 'default_ajax_return' => 'json',
  31 + // 默认JSONP格式返回的处理方法
  32 + 'default_jsonp_handler' => 'jsonpReturn',
  33 + // 默认JSONP处理方法
  34 + 'var_jsonp_handler' => 'callback',
  35 + // 默认时区
  36 + 'default_timezone' => 'PRC',
  37 + // 是否开启多语言
  38 + 'lang_switch_on' => false,
  39 + // 默认全局过滤方法 用逗号分隔多个
  40 + 'default_filter' => 'htmlspecialchars',
  41 + // 默认语言
  42 + 'default_lang' => 'zh-cn',
  43 + // 应用类库后缀
  44 + 'class_suffix' => true,
  45 + // 控制器类后缀
  46 + 'controller_suffix' => true,
  47 +
  48 + // +----------------------------------------------------------------------
  49 + // | 模块设置
  50 + // +----------------------------------------------------------------------
  51 +
  52 + // 默认模块名
  53 + 'default_module' => 'home',
  54 + // 禁止访问模块
  55 + 'deny_module_list' => ['common'],
  56 + // 默认控制器名
  57 + 'default_controller' => 'Index',
  58 + // 默认操作名
  59 + 'default_action' => 'index',
  60 + // 默认验证器
  61 + 'default_validate' => '',
  62 + // 默认的空控制器名
  63 + 'empty_controller' => 'Error',
  64 + // 自动搜索控制器
  65 + 'controller_auto_search' => false,
  66 +
  67 + // +----------------------------------------------------------------------
  68 + // | URL设置
  69 + // +----------------------------------------------------------------------
  70 +
  71 + 'pathinfo_depr' => '/',
  72 + // URL伪静态后缀
  73 + 'url_html_suffix' => 'html',
  74 + // URL普通方式参数 用于自动生成
  75 + 'url_common_param' => false,
  76 + // URL参数方式 0 按名称成对解析 1 按顺序解析
  77 + 'url_param_type' => 0,
  78 + // 是否开启路由
  79 + 'url_route_on' => true,
  80 + // 路由配置文件(支持配置多个)
  81 + 'route_config_file' => ['route'],
  82 + // 是否强制使用路由
  83 + 'url_route_must' => false,
  84 + // 域名部署
  85 + 'url_domain_deploy' => false,
  86 + // 域名根,如thinkphp.cn
  87 + 'url_domain_root' => '',
  88 + // 是否自动转换URL中的控制器和操作名
  89 + 'url_convert' => true,
  90 + // 默认的访问控制器层
  91 + 'url_controller_layer' => 'controller',
  92 + // 表单请求类型伪装变量
  93 + 'var_method' => '_method',
  94 +
  95 + // +----------------------------------------------------------------------
  96 + // | 模板设置
  97 + // +----------------------------------------------------------------------
  98 +
  99 + 'template' => [
  100 + // 模板引擎类型 支持 php think 支持扩展
  101 + 'type' => 'Think',
  102 + // 视图根目录
  103 + 'view_base' => '',
  104 + // 模板路径
  105 + 'view_path' => '',
  106 + // 模板后缀
  107 + 'view_suffix' => 'html',
  108 + // 模板文件名分隔符
  109 + 'view_depr' => DS,
  110 + // 模板引擎普通标签开始标记
  111 + 'tpl_begin' => '{',
  112 + // 模板引擎普通标签结束标记
  113 + 'tpl_end' => '}',
  114 + // 标签库标签开始标记
  115 + 'taglib_begin' => '<',
  116 + // 标签库标签结束标记
  117 + 'taglib_end' => '>',
  118 + ],
  119 +
  120 + // 视图输出字符串内容替换
  121 + 'view_replace_str' => [],
  122 + // 默认跳转页面对应的模板文件
  123 + 'dispatch_success_tmpl' => THINK_PATH . 'tpl' . DS . 'dispatch_jump.tpl',
  124 + 'dispatch_error_tmpl' => THINK_PATH . 'tpl' . DS . 'dispatch_jump.tpl',
  125 +
  126 + // +----------------------------------------------------------------------
  127 + // | 异常及错误设置
  128 + // +----------------------------------------------------------------------
  129 +
  130 + // 异常页面的模板文件
  131 + 'exception_tmpl' => THINK_PATH . 'tpl' . DS . 'think_exception.tpl',
  132 +
  133 + // 错误显示信息,非调试模式有效
  134 + 'error_message' => '页面错误!请稍后再试~',
  135 + // 显示错误信息
  136 + 'show_error_msg' => false,
  137 + // 异常处理handle类 留空使用 \think\exception\Handle
  138 + 'exception_handle' => '\\api\\common\\exception\\Http',
  139 +
  140 + // +----------------------------------------------------------------------
  141 + // | 日志设置
  142 + // +----------------------------------------------------------------------
  143 +
  144 + 'log' => [
  145 + // 日志记录方式,内置 file socket 支持扩展
  146 + 'type' => 'File',
  147 + // 日志保存目录
  148 + 'path' => LOG_PATH,
  149 + // 日志记录级别
  150 + 'level' => [],
  151 + ],
  152 +
  153 + // +----------------------------------------------------------------------
  154 + // | Trace设置 开启 app_trace 后 有效
  155 + // +----------------------------------------------------------------------
  156 + 'trace' => [
  157 + // 内置Html Console 支持扩展
  158 + 'type' => 'Html',
  159 + ],
  160 +
  161 + // +----------------------------------------------------------------------
  162 + // | 缓存设置
  163 + // +----------------------------------------------------------------------
  164 +
  165 + 'cache' => [
  166 + // 驱动方式
  167 + 'type' => 'File',
  168 + // 缓存保存目录
  169 + 'path' => CACHE_PATH,
  170 + // 缓存前缀
  171 + 'prefix' => '',
  172 + // 缓存有效期 0表示永久缓存
  173 + 'expire' => 0,
  174 + ],
  175 +
  176 + // +----------------------------------------------------------------------
  177 + // | 会话设置
  178 + // +----------------------------------------------------------------------
  179 +
  180 + 'session' => [
  181 + 'id' => '',
  182 + // SESSION_ID的提交变量,解决flash上传跨域
  183 + 'var_session_id' => '',
  184 + // SESSION 前缀
  185 + 'prefix' => 'think',
  186 + // 驱动方式 支持redis memcache memcached
  187 + 'type' => '',
  188 + // 是否自动开启 SESSION
  189 + 'auto_start' => true,
  190 + ],
  191 +
  192 + // +----------------------------------------------------------------------
  193 + // | Cookie设置
  194 + // +----------------------------------------------------------------------
  195 + 'cookie' => [
  196 + // cookie 名称前缀
  197 + 'prefix' => '',
  198 + // cookie 保存时间
  199 + 'expire' => 0,
  200 + // cookie 保存路径
  201 + 'path' => '/',
  202 + // cookie 有效域名
  203 + 'domain' => '',
  204 + // cookie 启用安全传输
  205 + 'secure' => false,
  206 + // httponly设置
  207 + 'httponly' => '',
  208 + // 是否使用 setcookie
  209 + 'setcookie' => true,
  210 + ],
  211 +
  212 + // +----------------------------------------------------------------------
  213 + // | 数据库设置
  214 + // +----------------------------------------------------------------------
  215 +
  216 + 'database' => [
  217 + // 数据库调试模式
  218 + 'debug' => true,
  219 + // 数据集返回类型
  220 + 'resultset_type' => 'collection',
  221 + // 自动写入时间戳字段
  222 + 'auto_timestamp' => false,
  223 + // 时间字段取出后的默认时间格式
  224 + 'datetime_format' => false,
  225 + // 是否需要进行SQL性能分析
  226 + 'sql_explain' => false,
  227 + ],
  228 +
  229 + //分页配置
  230 + 'paginate' => [
  231 + 'type' => 'bootstrap',
  232 + 'var_page' => 'page',
  233 + 'list_rows' => 15,
  234 + ],
  235 + //图片验证码
  236 + 'captcha' => [
  237 + // 验证码字符集合
  238 + 'codeSet' => '2345678abcdefhijkmnpqrstuvwxyzABCDEFGHJKLMNPQRTUVWXY',
  239 + // 验证码字体大小(px)
  240 + 'fontSize' => 25,
  241 + // 是否画混淆曲线
  242 + 'useCurve' => true,
  243 + // 验证码图片高度
  244 + 'imageH' => 30,
  245 + // 验证码图片宽度
  246 + 'imageW' => 100,
  247 + // 验证码位数
  248 + 'length' => 5,
  249 + // 验证成功后是否重置
  250 + 'reset' => true
  251 + ],
  252 +
  253 + // +----------------------------------------------------------------------
  254 + // | CMF 设置
  255 + // +----------------------------------------------------------------------
  256 + 'cmf_theme_path' => 'themes/home/',
  257 + 'cmf_default_theme' => 'simpleboot3',
  258 + 'cmf_admin_theme_path' => 'themes/admin/',
  259 + 'cmf_admin_default_theme' => 'simpleboot3',
  260 +];
  1 +<?php
  2 +// +----------------------------------------------------------------------
  3 +// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4 +// +----------------------------------------------------------------------
  5 +// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
  6 +// +----------------------------------------------------------------------
  7 +// | Author: Dean <zxxjjforever@163.com>
  8 +// +----------------------------------------------------------------------
  9 +
  10 +if(file_exists(ROOT_PATH."data/conf/database.php")){
  11 + $database=include ROOT_PATH."data/conf/database.php";
  12 +}else{
  13 + $database=[];
  14 +}
  15 +
  16 +return $database;
  1 +<?php
  2 +// +----------------------------------------------------------------------
  3 +// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4 +// +----------------------------------------------------------------------
  5 +// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
  6 +// +----------------------------------------------------------------------
  7 +// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8 +// +----------------------------------------------------------------------
  9 +// | Author: Dean <zxxjjforever@163.com>
  10 +// +----------------------------------------------------------------------
  11 +
  12 +return [
  13 + // 应用调试模式
  14 + 'app_debug' => true,
  15 + // 应用Trace
  16 + 'app_trace' => true,
  17 +
  18 +];
  1 +<?php
  2 +// +----------------------------------------------------------------------
  3 +// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4 +// +----------------------------------------------------------------------
  5 +// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
  6 +// +----------------------------------------------------------------------
  7 +// | Author: Dean <zxxjjforever@163.com>
  8 +// +----------------------------------------------------------------------
  9 +namespace api\home\controller;
  10 +
  11 +use think\Db;
  12 +use think\Validate;
  13 +use cmf\controller\RestBaseController;
  14 +
  15 +class IndexController extends RestBaseController
  16 +{
  17 + // api 首页
  18 + public function index()
  19 + {
  20 + $this->success("恭喜您,API访问成功!", [
  21 + 'version' => '1.0.0',
  22 + 'doc' => 'http://www.kancloud.cn/thinkcmf/cmf5api'
  23 + ]);
  24 + }
  25 +
  26 +}
  1 +<?php
  2 +// +----------------------------------------------------------------------
  3 +// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4 +// +----------------------------------------------------------------------
  5 +// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
  6 +// +----------------------------------------------------------------------
  7 +// | Author: Dean <zxxjjforever@163.com>
  8 +// +----------------------------------------------------------------------
  9 +// API 模板文件,可以复制
  10 +namespace api\home\controller;
  11 +
  12 +use cmf\controller\RestBaseController;
  13 +
  14 +class RestController extends RestBaseController
  15 +{
  16 + /**
  17 + * 显示资源列表
  18 + */
  19 + public function index()
  20 + {
  21 + }
  22 +
  23 + /**
  24 + * 保存新建的资源
  25 + */
  26 + public function save()
  27 + {
  28 + }
  29 +
  30 + /**
  31 + * 显示指定的资源
  32 + *
  33 + * @param int $id
  34 + */
  35 + public function read($id)
  36 + {
  37 + }
  38 +
  39 + /**
  40 + * 保存更新的资源
  41 + *
  42 + * @param int $id
  43 + */
  44 + public function update($id)
  45 + {
  46 + }
  47 +
  48 + /**
  49 + * 删除指定资源
  50 + *
  51 + * @param int $id
  52 + */
  53 + public function delete($id)
  54 + {
  55 + }
  56 +
  57 +}
  1 +<?php
  2 +// +----------------------------------------------------------------------
  3 +// | 文件说明:幻灯片
  4 +// +----------------------------------------------------------------------
  5 +// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
  6 +// +----------------------------------------------------------------------
  7 +// | Author: wuwu <15093565100@163.com>
  8 +// +----------------------------------------------------------------------
  9 +// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  10 +// +----------------------------------------------------------------------
  11 +// | Date: 2017-5-25
  12 +// +----------------------------------------------------------------------
  13 +namespace api\home\controller;
  14 +
  15 +use api\home\model\SlideModel;
  16 +use cmf\controller\RestBaseController;
  17 +
  18 +class SlidesController extends RestBaseController
  19 +{
  20 + /**
  21 + * [获取幻灯片]
  22 + * @Author: wuwu<15093565100@163.com>
  23 + * @DateTime: 2017-05-25T20:48:53+0800
  24 + * @since: 1.0
  25 + */
  26 + public function read()
  27 + {
  28 + //slide为空或不存在抛出异常
  29 + $id = $this->request->param('id', 0, 'intval');
  30 + if (empty($id)) {
  31 + $this->error('缺少ID参数');
  32 + }
  33 +
  34 + $map['id'] = $id;
  35 + $obj = new SlideModel();
  36 + $data = $obj->SlideList($map);
  37 +
  38 +
  39 + //剔除分类状态隐藏 剔除分类下显示数据为空
  40 + if (empty($data) || $data['items']->isEmpty()) {
  41 + $this->error('该组幻灯片显示数据为空');
  42 + }
  43 +
  44 + if (isset($this->apiVersion)) {
  45 + $response = $data;
  46 + } else {
  47 + $response = [$data];
  48 + }
  49 +
  50 + $this->success("该组幻灯片获取成功!", $response);
  51 + }
  52 +
  53 +}
  1 +<?php
  2 +// +----------------------------------------------------------------------
  3 +// | 文件说明:用户-幻灯片
  4 +// +----------------------------------------------------------------------
  5 +// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
  6 +// +----------------------------------------------------------------------
  7 +// | Author: wuwu <15093565100@163.com>
  8 +// +----------------------------------------------------------------------
  9 +// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  10 +// +----------------------------------------------------------------------
  11 +// | Date: 2017-5-25
  12 +// +----------------------------------------------------------------------
  13 +
  14 +namespace api\home\model;
  15 +
  16 +use think\Model;
  17 +
  18 +class SlideItemModel extends Model
  19 +{
  20 + /**
  21 + * [base 全局查询范围status=1显示状态]
  22 + * @Author: wuwu<15093565100@163.com>
  23 + * @DateTime: 2017-05-25T21:54:03+0800
  24 + * @since: 1.0
  25 + */
  26 + protected function base($query)
  27 + {
  28 + $query->where('status', 1);
  29 + }
  30 +
  31 + /**
  32 + * image 自动转化
  33 + * @param $value
  34 + * @return array
  35 + */
  36 + public function getImageAttr($value)
  37 + {
  38 + return cmf_get_image_url($value);
  39 + }
  40 +}
  41 +
  1 +<?php
  2 +// +----------------------------------------------------------------------
  3 +// | 文件说明:用户-幻灯片
  4 +// +----------------------------------------------------------------------
  5 +// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
  6 +// +----------------------------------------------------------------------
  7 +// | Author: wuwu <15093565100@163.com>
  8 +// +----------------------------------------------------------------------
  9 +// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  10 +// +----------------------------------------------------------------------
  11 +// | Date: 2017-5-25
  12 +// +----------------------------------------------------------------------
  13 +
  14 +namespace api\home\model;
  15 +
  16 +use think\Model;
  17 +
  18 +class SlideModel extends Model
  19 +{
  20 +
  21 + /**
  22 + * [base 全局查询范围status=1显示状态]
  23 + * @Author: wuwu<15093565100@163.com>
  24 + * @DateTime: 2017-05-25T21:54:03+0800
  25 + * @since: 1.0
  26 + */
  27 + protected function base($query)
  28 + {
  29 + $query->where('status', 1)->where('delete_time', 0);
  30 + }
  31 +
  32 + /**
  33 + * [SlideItemModel 一对一关联模型 关联分类下的幻灯片]
  34 + * @Author: wuwu<15093565100@163.com>
  35 + * @DateTime: 2017-05-25T23:30:27+0800
  36 + * @since: 1.0
  37 + */
  38 + protected function items()
  39 + {
  40 + return $this->hasMany('SlideItemModel')->order('list_order ASC');
  41 + }
  42 +
  43 + /**
  44 + * [SlideList 幻灯片获取]
  45 + * @Author: wuwu<15093565100@163.com>
  46 + * @DateTime: 2017-05-25T20:52:27+0800
  47 + * @since: 1.0
  48 + */
  49 + public function SlideList($map)
  50 + {
  51 + $data = $this->relation('items')->field(true)->where($map)->find();
  52 + return $data;
  53 + }
  54 +
  55 +}
  56 +
  1 +<?php
  2 +
  3 +use think\Route;
  4 +
  5 +Route::resource('home/slides', 'home/Slides');
  1 +<?php
  2 +// +----------------------------------------------------------------------
  3 +// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4 +// +----------------------------------------------------------------------
  5 +// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
  6 +// +----------------------------------------------------------------------
  7 +// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8 +// +----------------------------------------------------------------------
  9 +// | Author: 老猫 <thinkcmf@126.com>
  10 +// +----------------------------------------------------------------------
  11 +namespace api\portal\controller;
  12 +//use cmf\controller\HomeBaseController;
  13 +use app\portal\model\MemberModel;
  14 +use app\portal\model\UserModel;
  15 +use app\portal\service\PostService;
  16 +use think\Db;
  17 +use think\Request;
  18 +use think\Loader;
  19 +/**
  20 + * @title 文章接口
  21 + * @description 接口说明
  22 + * @group 接口分组
  23 + */
  24 +
  25 +class ArticleController extends CommonController
  26 +{
  27 + /**
  28 + * @title 文章列表
  29 + * @description 接口说明
  30 + * @author 开发者
  31 + * @url /api/portal/Article/index
  32 + * @method POST
  33 + * @param name:articleType type:int require:1 default: other: desc:类型
  34 + * @param name:page type:int require:1 default: other: desc:页码
  35 + * @return data:''@
  36 + * @data post_id:文章id
  37 + * @data category_id:类型id
  38 + * @data name:类型名
  39 + * @data post_title:文章标题
  40 + *
  41 + */
  42 + public function index(Request $request)
  43 + {
  44 + $page = $request->param('page');
  45 + $where_connect['j.status'] = 1;
  46 + $where_connect['p.post_status'] = 1;
  47 + $where_connect['j.category_id'] = $_POST['articleType'];
  48 + $article_list = Db::name('PortalCategoryPost')->alias('j')
  49 + ->where($where_connect)
  50 + ->field("j.post_id,j.category_id , c.name , p.post_title")
  51 + ->join("PortalCategory c","c.id = j.category_id")
  52 + ->join("PortalPost p","p.id = j.post_id")
  53 + ->page("$page,5")
  54 + ->select()->toArray();
  55 + if(empty($article_list)){
  56 + $this->apiResponse('1','暂无数据');
  57 + }
  58 + $this->apiResponse('1','成功',$article_list);
  59 + }
  60 +
  61 + /**
  62 + * @title 文章详情
  63 + * @description 接口说明
  64 + * @author 开发者
  65 + * @url /api/portal/Article/index
  66 + * @method POST
  67 + *
  68 + * @param name:post_id type:int require:1 default: other: desc:文章id
  69 + *
  70 + * @return content:''@!
  71 + * @content thumb:文章图片
  72 + * @content title:文章标题
  73 + * @content content:文章内容
  74 + * @content update_time:创建时间
  75 + * @content category_id:类型id
  76 + * @content name:类型名
  77 + * @return hot:''@
  78 + * @hot title:文章标题
  79 + * @hot article_id:文章id
  80 + *
  81 + */
  82 + public function detail(Request $request)
  83 + {
  84 + $where_art['p.id'] = $request->param('post_id');
  85 + $where_art['p.post_status'] = 1;
  86 + $article_list = Db::name('PortalCategoryPost')
  87 + ->alias('j')
  88 + ->where($where_art)
  89 + ->field("p.post_title as title,p.post_content as content,p.more,p.update_time ,
  90 + c.name,c.id as category_id")
  91 + ->join("PortalCategory c","c.id = j.category_id")
  92 + ->join("PortalPost p","p.id = j.post_id")
  93 + ->find();
  94 + if(empty($article_list)){
  95 + $this->apiResponse('1','暂无数据');
  96 + }
  97 +// 处理图片
  98 + $thumb = json_decode($article_list['more'],true);
  99 + $article_list['thumb'] = $thumb['thumbnail'];
  100 + unset($article_list['more']);
  101 +// 处理内容
  102 + $article_list['content'] = htmlspecialchars_decode($article_list['content']);
  103 + $article_list['create_time'] = date('Y-m-d',$article_list['update_time']);
  104 + $final['content'] = $article_list;
  105 +// 热门文章
  106 + $where_hot['p.recommended'] = 1;
  107 + $where_hot['p.post_status'] = 1;
  108 + $hotArticle = Db::name('PortalCategoryPost')
  109 + ->alias('j')
  110 + ->where($where_hot)
  111 + ->field("p.post_title as title,p.id as article_id")
  112 +// ->join("PortalCategory c","c.id = j.category_id")
  113 + ->join("PortalPost p","p.id = j.post_id")
  114 + ->select();
  115 + $final['hot'] = $hotArticle;
  116 + $this->apiResponse('1','成功',$final);
  117 + }
  118 +
  119 +
  120 +
  121 +}
  1 +<?php
  2 +// +----------------------------------------------------------------------
  3 +// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4 +// +----------------------------------------------------------------------
  5 +// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
  6 +// +----------------------------------------------------------------------
  7 +// | Author: pl125 <xskjs888@163.com>
  8 +// +----------------------------------------------------------------------
  9 +
  10 +namespace api\portal\controller;
  11 +
  12 +use cmf\controller\RestBaseController;
  13 +use api\portal\model\PortalPostModel;
  14 +
  15 +class ArticlesController extends RestBaseController
  16 +{
  17 + protected $postModel;
  18 +
  19 + public function __construct(PortalPostModel $postModel)
  20 + {
  21 + parent::__construct();
  22 + $this->postModel = $postModel;
  23 + }
  24 +
  25 + /**
  26 + * 文章列表
  27 + */
  28 + public function index()
  29 + {
  30 + $params = $this->request->get();
  31 + $params['where']['post_type'] = 1;
  32 + $data = $this->postModel->getDatas($params);
  33 +
  34 + if (isset($this->apiVersion)) {
  35 + $response = ['list' => $data,];
  36 + } else {
  37 + $response = $data;
  38 + }
  39 + $this->success('请求成功!', $response);
  40 + }
  41 +
  42 + /**
  43 + * 获取指定的文章
  44 + * @param int $id
  45 + */
  46 + public function read($id)
  47 + {
  48 + if (intval($id) === 0) {
  49 + $this->error('无效的文章id!');
  50 + } else {
  51 + $params = $this->request->get();
  52 + $params['where']['post_type'] = 1;
  53 + $params['id'] = $id;
  54 + $data = $this->postModel->getDatas($params);
  55 + if (empty($data)) {
  56 + $this->error('文章不存在!');
  57 + } else {
  58 + $this->postModel->where('id', $id)->setInc('post_hits');
  59 + $this->success('请求成功!', $data);
  60 + }
  61 +
  62 + }
  63 + }
  64 +
  65 + /**
  66 + * 我的文章列表
  67 + */
  68 + public function my()
  69 + {
  70 + $params = $this->request->get();
  71 + $userId = $this->getUserId();
  72 + $data = $this->postModel->getUserArticles($userId, $params);
  73 + $this->success('请求成功!', $data);
  74 + }
  75 +
  76 + /**
  77 + * 添加文章
  78 + */
  79 + public function save()
  80 + {
  81 + $data = $this->request->post();
  82 + $data['user_id'] = $this->getUserId();
  83 + $result = $this->validate($data, 'Articles.article');
  84 + if ($result !== true) {
  85 + $this->error($result);
  86 + }
  87 +
  88 + if (empty($data['published_time'])) {
  89 + $data['published_time'] = time();
  90 + }
  91 +
  92 + $this->postModel->addArticle($data);
  93 + $this->success('添加成功!');
  94 + }
  95 +
  96 + /**
  97 + * 更新文章
  98 + * @param int $id
  99 + */
  100 + public function update($id)
  101 + {
  102 + $data = $this->request->put();
  103 + $result = $this->validate($data, 'Articles.article');
  104 + if ($result !== true) {
  105 + $this->error($result);
  106 + }
  107 + if (empty($id)) {
  108 + $this->error('无效的文章id');
  109 + }
  110 + $result = $this->postModel->editArticle($data, $id, $this->getUserId());
  111 + if ($result === false) {
  112 + $this->error('编辑失败!');
  113 + } else {
  114 + $this->success('编辑成功!');
  115 + }
  116 + }
  117 +
  118 + /**
  119 + * 删除文章
  120 + * @param int $id
  121 + */
  122 + public function delete($id)
  123 + {
  124 + if (empty($id)) {
  125 + $this->error('无效的文章id');
  126 + }
  127 + $result = $this->postModel->deleteArticle($id, $this->getUserId());
  128 + if ($result == -1) {
  129 + $this->error('文章已删除');
  130 + }
  131 + if ($result) {
  132 + $this->success('删除成功!');
  133 + } else {
  134 + $this->error('删除失败!');
  135 + }
  136 + }
  137 +
  138 + /**
  139 + * 批量删除文章
  140 + */
  141 + public function deletes()
  142 + {
  143 + $ids = $this->request->post('ids/a');
  144 + if (empty($ids)) {
  145 + $this->error('文章id不能为空');
  146 + }
  147 + $result = $this->postModel->deleteArticle($ids, $this->getUserId());
  148 + if ($result == -1) {
  149 + $this->error('文章已删除');
  150 + }
  151 + if ($result) {
  152 + $this->success('删除成功!');
  153 + } else {
  154 + $this->error('删除失败!');
  155 + }
  156 + }
  157 +
  158 + public function search()
  159 + {
  160 + $params = $this->request->get();
  161 + if (!empty($params['keyword'])) {
  162 + $params['where'] = [
  163 + 'post_type' => 1,
  164 + 'post_title|post_keywords|post_excerpt' => ['like', '%' . $params['keyword'] . '%']
  165 + ];
  166 + $data = $this->postModel->getDatas($params);
  167 + $this->success('请求成功!', $data);
  168 + } else {
  169 + $this->error('搜索关键词不能为空!');
  170 + }
  171 +
  172 + }
  173 +
  174 + public function doLike()
  175 + {
  176 + $userId = $this->getUserId();
  177 +
  178 + $articleId = $this->request->param('id', 0, 'intval');
  179 +
  180 + $canLike = cmf_check_user_action(['object' => "posts$articleId", 'user_id' => $userId], 1);
  181 +
  182 + if ($canLike) {
  183 + $this->postModel->where(['id' => $articleId])->setInc('post_like');
  184 +
  185 + $likeCount = $this->postModel->where('id', $articleId)->value('post_like');
  186 + $this->success("赞好啦!", ['post_like' => $likeCount]);
  187 + } else {
  188 + $this->error("您已赞过啦!");
  189 + }
  190 + }
  191 +}
  1 +<?php
  2 +// +----------------------------------------------------------------------
  3 +// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4 +// +----------------------------------------------------------------------
  5 +// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
  6 +// +----------------------------------------------------------------------
  7 +// | Author: pl125 <xskjs888@163.com>
  8 +// +----------------------------------------------------------------------
  9 +
  10 +namespace api\portal\controller;
  11 +
  12 +use cmf\controller\RestBaseController;
  13 +use api\portal\model\PortalCategoryModel;
  14 +
  15 +class CategoriesController extends RestBaseController
  16 +{
  17 + protected $categoryModel;
  18 +
  19 + public function __construct(PortalCategoryModel $categoryModel)
  20 + {
  21 + parent::__construct();
  22 + $this->categoryModel = $categoryModel;
  23 + }
  24 +
  25 + /**
  26 + * 获取分类列表
  27 + */
  28 + public function index()
  29 + {
  30 + $params = $this->request->get();
  31 + $data = $this->categoryModel->getDatas($params);
  32 + $this->success('请求成功!', $data);
  33 + }
  34 +
  35 + /**
  36 + * 显示指定的分类
  37 + * @param int $id
  38 + */
  39 + public function read($id)
  40 + {
  41 + $params = $this->request->get();
  42 + $params['id'] = $id;
  43 + $data = $this->categoryModel->getDatas($params);
  44 + $this->success('请求成功!', $data);
  45 + }
  46 +
  47 + /**
  48 + * 获取指定分类的子分类列表
  49 + * @throws \think\db\exception\DataNotFoundException
  50 + * @throws \think\db\exception\ModelNotFoundException
  51 + * @throws \think\exception\DbException
  52 + */
  53 + public function subCategories()
  54 + {
  55 + $id = $this->request->get('category_id', 0, 'intval');
  56 +
  57 + $categories = $this->categoryModel->where(['parent_id' => $id])->select();
  58 +
  59 + $this->success('请求成功', ['categories' => $categories]);
  60 +
  61 + }
  62 +}
  1 +<?php
  2 +// +----------------------------------------------------------------------
  3 +// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4 +// +----------------------------------------------------------------------
  5 +// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
  6 +// +----------------------------------------------------------------------
  7 +// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8 +// +----------------------------------------------------------------------
  9 +// | Author: 老猫 <thinkcmf@126.com>
  10 +// +----------------------------------------------------------------------
  11 +namespace api\portal\controller;
  12 +
  13 +use cmf\controller\HomeBaseController;
  14 +use think\Db;
  15 +use think\Request;
  16 +use think\Loader;
  17 +use SmsDemo;
  18 +
  19 +class CommonController extends HomeBaseController
  20 +{
  21 + /**
  22 + * API返回信息格式函数 ;0失败,1成功,-1需要登录
  23 + * @param string $code
  24 + * @param string $message
  25 + * @param array $data
  26 + */
  27 + public function apiResponse($code = '0', $message = '',$data = array(),$nums =0){
  28 + header('Access-Control-Allow-Origin: *');
  29 + header('Content-Type:application/json; charset=utf-8');
  30 + $result = array(
  31 + 'code'=>$code,
  32 + 'message'=>$message,
  33 + 'data'=>$data,
  34 + 'nums'=>''.$nums
  35 + );
  36 + die(json_encode($result,JSON_UNESCAPED_UNICODE));
  37 + }
  38 +
  39 +// 加密
  40 + public function md5($number){
  41 + $final = sha1(md5($number));
  42 + return $final;
  43 + }
  44 +
  45 +// 短信验证码
  46 + public function sendLogin($tel,$code,$modelVal){
  47 + require_once VENDOR_PATH."aliyun-dysms-php-sdk/api_demo/SmsDemo.php";
  48 + set_time_limit(0);
  49 + header('Content-Type: text/plain; charset=utf-8');
  50 + $response = SmsDemo::sendSms($tel,$code,$modelVal);
  51 + return $response;
  52 + }
  53 +
  54 +// 用户登录
  55 + public function isLogin(){
  56 + if($_SESSION['user_id']){
  57 + $where_firstLogin['id'] = $_SESSION['user_id'];
  58 + $firstMember = Db::name('Member')->where($where_firstLogin)->find();
  59 + if($firstMember['update_time'] >= (time()+604800) ){
  60 + $this->apiResponse('0','请先登录');
  61 + }
  62 + }else{
  63 + $this->apiResponse('0','请先登录');
  64 + }
  65 + }
  66 +
  67 +}
  1 +<?php
  2 +// +----------------------------------------------------------------------
  3 +// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4 +// +----------------------------------------------------------------------
  5 +// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
  6 +// +----------------------------------------------------------------------
  7 +// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8 +// +----------------------------------------------------------------------
  9 +// | Author: 老猫 <thinkcmf@126.com>
  10 +// +----------------------------------------------------------------------
  11 +namespace api\portal\controller;
  12 +use think\Controller;
  13 +//use api\portal\model\MemberModel;
  14 +//use api\portal\model\UserModel;
  15 +//use api\portal\service\PostService;
  16 +use think\Db;
  17 +use think\Request;
  18 +use think\Loader;
  19 +/**
  20 + * @title 首页接口
  21 + * @description 接口说明
  22 + * @group 接口分组
  23 + */
  24 +
  25 +class IndexController extends CommonController
  26 +{
  27 + /**
  28 + * @title 首页
  29 + * @description 接口说明
  30 + * @author 开发者
  31 + * @url /api/portal/Index/index
  32 + * @method POST
  33 + * @param name:chooseType type:int require:1 default: other: desc:类型:1手机站,2PC站
  34 +
  35 +
  36 + */
  37 +
  38 + public function index(Request $request)
  39 + {
  40 +// 1.首页轮播
  41 + $where_banner['status'] = 1;
  42 + $final['banner'] = Db::name('Banner')->where($where_banner)
  43 + ->field("pic")
  44 + ->order("score desc,create_time desc")->select()->toArray();
  45 +// 2.交易流程
  46 + $where_process['status'] = 1;
  47 + $where_process['type'] = $request->param('type');
  48 + $final['process'] = Db::name('Process')->where($where_process)
  49 + ->field("content")
  50 + ->order('create_time desc')
  51 + ->find();
  52 + $final['process']['content'] = htmlspecialchars_decode($final['process']['content']);
  53 +// 3. 最新成交
  54 +
  55 +
  56 +
  57 +
  58 +
  59 +// 4. 合作企业
  60 + $where_coo['status'] = 1;
  61 + $final['coop'] = Db::name('Cooperation')->where($where_coo)->order("score desc , create_time desc")->field('pic,jump')->select()->toArray();
  62 +// 5. 文章
  63 + $where_cate['c.delete_time'] = 0;
  64 + $where_cate['c.status'] = 1;
  65 + $where_cate['j.status'] = 1;
  66 +// 分类
  67 + $connect = Db::name('PortalCategoryPost')->alias('j')
  68 + ->where($where_cate)
  69 + ->join("PortalCategory c",'c.id = j.category_id')
  70 +// ->join("PortalPost p",'p.id = j.post_id')
  71 +// ->group('j.category_id')
  72 + ->field("j.id as jid ,j.post_id,j.category_id,c.name")
  73 + ->select()->toArray();
  74 + $cate_arr = array();
  75 + $post_arr = array();
  76 + $connect_arr = array();
  77 +// 处理分类id
  78 + foreach ($connect as $conk=>$conv){
  79 + $cate_arr[] = $conv['category_id'];
  80 + $post_arr[] = $conv['post_id'];
  81 + $connect_arr[$conk]['category_id'] = $conv['category_id'];
  82 + $connect_arr[$conk]['post_id'] = $conv['post_id'];
  83 + }
  84 + $cate_arr = array_values(array_unique($cate_arr));
  85 +// 查询分类
  86 + $where_cateNext['id'] = array('in',$cate_arr);
  87 + $cateNext_list = Db::name('PortalCategory')->where($where_cateNext)->field('id,name')->select()->toArray();
  88 +// 查文章
  89 + $where_arr['post_status'] = 1;
  90 + $where_arr['id'] = array('in',$post_arr);
  91 + $artcile = Db::name('PortalPost')
  92 + ->where($where_arr)
  93 + ->field('id,post_title')
  94 + ->order("create_time desc")
  95 + ->select()->toArray();
  96 + $middle =array();
  97 +// 拼数组
  98 +// 循环关联
  99 + foreach ($connect_arr as $conNextk=>$conNextv){
  100 +// 循环类
  101 + foreach ($cateNext_list as $catNextk=>$catNextv){
  102 + $middle[$catNextk]['id'] = $catNextv['id'];
  103 + $middle[$catNextk]['name'] = $catNextv['name'];
  104 +// 循环文章
  105 + foreach ($artcile as $artNextk=>$artNextv){
  106 + if($conNextv['category_id'] == $catNextv['id']){
  107 +// $key = $catNextv['name'];
  108 + if($conNextv['post_id'] == $artNextv['id']){
  109 +// $middle[$key][$artNextk]['title'] = $artNextv['post_title'];
  110 +// $middle[$key][$artNextk]['id'] = $artNextv['id'];
  111 + $middle[$catNextk]['list'][$artNextk]['id'] = $artNextv['id'];
  112 + $middle[$catNextk]['list'][$artNextk]['title'] = $artNextv['post_title'];
  113 + }
  114 + }
  115 + }
  116 + }
  117 + }
  118 + $final['article'] = $middle;
  119 +
  120 + $this->apiResponse('1','成功',$final);
  121 + }
  122 +
  123 +}
  1 +<?php
  2 +// +----------------------------------------------------------------------
  3 +// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4 +// +----------------------------------------------------------------------
  5 +// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
  6 +// +----------------------------------------------------------------------
  7 +// | Author: wuwu <15093565100@163.com>
  8 +// +----------------------------------------------------------------------
  9 +namespace api\portal\controller;
  10 +
  11 +use api\portal\model\PortalPostModel;
  12 +use cmf\controller\RestBaseController;
  13 +use think\Db;
  14 +use think\Request;
  15 +use think\Loader;
  16 +
  17 +/**
  18 + * @title 用户接口
  19 + * @description 接口说明
  20 + * @group 接口分组
  21 + */
  22 +
  23 +class IntermediaryController extends CommonController
  24 +{
  25 + protected $postModel;
  26 + public function __construct(PortalPostModel $postModel)
  27 + {
  28 + parent::__construct();
  29 + $this->postModel = $postModel;
  30 + }
  31 +
  32 + // 登录,注册
  33 +//tel 手机号
  34 +//password 密码
  35 +//sure_password 确认密码
  36 +//name 真实姓名
  37 + /**
  38 + * @title 房产中介注册
  39 + * @description 接口说明
  40 + * @author 开发者
  41 + * @url /api/portal/Intermediary/join
  42 + * @method POST
  43 + * @param name:name type:int require:1 default: other: desc:姓名
  44 + * @param name:tel type:int require:1 default: other: desc:手机号
  45 + * @param name:password type:int require:1 default: other: desc:密码
  46 + */
  47 +
  48 + public function join(Request $request)
  49 + {
  50 + if($request->Post()){
  51 +// 验证
  52 + $validate = Loader::validate('Intermediary');
  53 + if(!$validate->scene('add')->check($_POST)){
  54 + return json(array('code'=>0,'msg'=>$validate->getError()));
  55 + }
  56 +// 密码
  57 + $_POST['password'] = $this->md5($_POST['password']);
  58 +// 确认密码
  59 + if($_POST['password'] != $_POST['sure_password']){
  60 + $this->apiResponse('0','两次密码不一致');
  61 + }
  62 +// 短信验证码(未完)
  63 +
  64 +// 判断手机号是否已注册
  65 + $user = new MemberModel($_POST);
  66 + $where_user['tel'] = $_POST['tel'];
  67 + $is_isset = $user->where($where_user)->find();
  68 + if($is_isset){
  69 + $this->apiResponse('0','您已注册过,请直接登录');
  70 + }
  71 + echo "<pre/>";
  72 + print_r('1111');
  73 + die;
  74 + $str = rand('1000,9999').time().rand('100,999');
  75 + $_POST['token'] = $this->md5($str);
  76 + $add = $user->allowField(true)->save();
  77 + if($add){
  78 + echo "<pre/>";
  79 + print_r('1');
  80 + die;
  81 + }else{
  82 + echo "<pre/>";
  83 + print_r('2');
  84 + die;
  85 + }
  86 +
  87 + }else{
  88 +// 服务协议
  89 +// $service = new PostService();
  90 +// $list = $service->publishedArticle(1,1)->toArray();
  91 + $where_pro['status'] = 1;
  92 + $where_pro['type'] = 2;
  93 + $list = Db::name('Protocol')->where($where_pro)->order("update_time desc")->find();
  94 +// 推荐人(未完)
  95 +
  96 +
  97 + if($list){
  98 + $this->apiResponse('1','成功',$list);
  99 + }else{
  100 + $this->apiResponse('0','暂无内容');
  101 + }
  102 +
  103 + }
  104 + }
  105 +
  106 +
  107 +// 登录
  108 + public function login(Request $request){
  109 + if($request->post()){
  110 +// 登录验证
  111 +// 判空
  112 + $tel = $request->param('tel');
  113 + $password = $request->param('password');
  114 + $true = $request->param('true');
  115 + $token = $request->param('token');
  116 + if(empty($tel)){
  117 + $this->apiResponse('0','手机号不能为空');
  118 + }else if(empty($password)){
  119 + $this->apiResponse('0','密码不能为空');
  120 + }else if(empty($true)){
  121 + $this->apiResponse('0','验证码不能为空');
  122 + }
  123 +// 验证
  124 + $where_member['tel'] = $tel;
  125 + $where_member['password'] = $password;
  126 +// $where_member['token'] = $token;
  127 + $member = Db::name('Member')->where($where_member)->find();
  128 + if($member){
  129 + if($member['token'] != $token){
  130 + $this->apiResponse('0','登录失败');
  131 + }
  132 + if($member['update_time'] >= (time()+604800)){
  133 + $this->apiResponse('0','请重新登录');
  134 + }else{
  135 + $this->apiResponse('1','登录成功');
  136 + }
  137 + }else{
  138 + $this->apiResponse('0','您输入的账号或密码不正确');
  139 + }
  140 +
  141 +
  142 + }else{
  143 +// 返回验证码图片
  144 +
  145 + }
  146 + }
  147 +}
  1 +<?php
  2 +// +----------------------------------------------------------------------
  3 +// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4 +// +----------------------------------------------------------------------
  5 +// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
  6 +// +----------------------------------------------------------------------
  7 +// | Author: wuwu <15093565100@163.com>
  8 +// +----------------------------------------------------------------------
  9 +namespace api\portal\controller;
  10 +
  11 +use api\portal\model\MemberModel;
  12 +use api\portal\model\PortalPostModel;
  13 +use cmf\controller\RestBaseController;
  14 +use think\Db;
  15 +use think\Request;
  16 +use think\Loader;
  17 +use think\Config;
  18 +use think\captcha\Captcha;
  19 +use think\Url;
  20 +use SmsDemo;
  21 +//use think\Route;
  22 +/**
  23 + * @title 用户接口
  24 + * @description 接口说明
  25 + * @group 接口分组
  26 + */
  27 +
  28 +class IntermediaryNeedController extends CommonController
  29 +{
  30 + protected $postModel;
  31 + public function __construct(PortalPostModel $postModel)
  32 + {
  33 + parent::__construct();
  34 + $this->postModel = $postModel;
  35 + }
  36 + /**
  37 + * @title 用户发布需求的保证金
  38 + * @description 接口说明
  39 + * @author 开发者
  40 + * @url /api/portal/UserNeed/payMoney
  41 + * @method POST
  42 + * @param name:city type:int require:1 default: other: desc:地区
  43 + * @param name:quarters type:int require:1 default: other: desc:小区
  44 + * @param name:password type:int require:1 default: other: desc:密码
  45 + */
  46 + public function payMoney(Request $request){
  47 + $data = $request->param();
  48 + if(!$data['city'] || !$data['area'] ){
  49 + $this->apiResponse('0','城市、面积不能为空');
  50 + }
  51 + $city = $data['city'];
  52 + $area = $data['area'];
  53 + $where_money[''] =
  54 + Db::name('MoneyRule')->where()
  55 + }
  56 +
  57 +
  58 + /**
  59 + * @title 用户发布需求
  60 + * @description 接口说明
  61 + * @author 开发者
  62 + * @url /api/portal/UserNeed/send
  63 + * @method POST
  64 + * @param name:city type:int require:1 default: other: desc:地区
  65 + * @param name:quarters type:int require:1 default: other: desc:小区
  66 + * @param name:password type:int require:1 default: other: desc:密码
  67 + */
  68 + public function send(Request $request)
  69 + {
  70 + if($request->Post()){
  71 + $data = $request->param();
  72 +// 判空,处理
  73 +// 加数据库
  74 + }else{
  75 +// 问答内容
  76 + $where_pro['status'] = 1;
  77 + $list = Db::name('NeedAnswer')->where($where_pro)->order("update_time desc")->find();
  78 + if($list){
  79 + $this->apiResponse('1','成功',$list);
  80 + }else{
  81 + $this->apiResponse('0','暂无内容');
  82 + }
  83 +
  84 + }
  85 + }
  86 +
  87 +
  88 +
  89 +
  90 +}
  1 +<?php
  2 +// +----------------------------------------------------------------------
  3 +// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4 +// +----------------------------------------------------------------------
  5 +// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
  6 +// +----------------------------------------------------------------------
  7 +// | Author: wuwu <15093565100@163.com>
  8 +// +----------------------------------------------------------------------
  9 +namespace api\portal\controller;
  10 +
  11 +use api\portal\model\PortalCategoryModel;
  12 +use api\portal\model\PortalPostModel;
  13 +use cmf\controller\RestBaseController;
  14 +
  15 +class ListsController extends RestBaseController
  16 +{
  17 +
  18 + /**
  19 + * [推荐文章列表]
  20 + * @Author: wuwu<15093565100@163.com>
  21 + * @DateTime: 2017-07-17T11:36:51+0800
  22 + * @since: 1.0
  23 + */
  24 + public function recommended()
  25 + {
  26 + $param = $this->request->param();
  27 + $portalPostModel = new PortalPostModel();
  28 +
  29 + $param['where'] = ['recommended' => 1];
  30 +
  31 + $articles = $portalPostModel->getDatas($param);
  32 +
  33 + $this->success('ok', ['list' => $articles]);
  34 + }
  35 +
  36 + /**
  37 + * [getCategoryPostLists 分类文章列表]
  38 + * @Author: wuwu<15093565100@163.com>
  39 + * @DateTime: 2017-07-17T15:22:41+0800
  40 + * @since: 1.0
  41 + */
  42 + public function getCategoryPostLists()
  43 + {
  44 + $categoryId = $this->request->param('category_id', 0, 'intval');
  45 +
  46 +
  47 + $portalCategoryModel = new PortalCategoryModel();
  48 +
  49 + $findCategory = $portalCategoryModel->where('id', $categoryId)->find();
  50 +
  51 + //分类是否存在
  52 + if (empty($findCategory)) {
  53 + $this->error('分类不存在!');
  54 + }
  55 +
  56 + $param = $this->request->param();
  57 +
  58 + $articles = $portalCategoryModel->paramsFilter($param, $findCategory->articles()->alias('post'))->select();
  59 +
  60 + if (!empty($param['relation'])) {
  61 + if (count($articles) > 0) {
  62 + $articles->load('user');
  63 + $articles->append(['user']);
  64 + }
  65 + }
  66 +
  67 + $this->success('ok', ['list' => $articles]);
  68 + }
  69 +
  70 +}
  1 +<?php
  2 +// +----------------------------------------------------------------------
  3 +// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4 +// +----------------------------------------------------------------------
  5 +// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
  6 +// +----------------------------------------------------------------------
  7 +// | Author: pl125 <xskjs888@163.com>
  8 +// +----------------------------------------------------------------------
  9 +
  10 +namespace api\portal\controller;
  11 +
  12 +use cmf\controller\RestBaseController;
  13 +use api\portal\model\PortalPostModel;
  14 +
  15 +class PagesController extends RestBaseController
  16 +{
  17 + protected $postModel;
  18 +
  19 + public function __construct(PortalPostModel $postModel)
  20 + {
  21 + parent::__construct();
  22 + $this->postModel = $postModel;
  23 + }
  24 +
  25 + /**
  26 + * 页面列表
  27 + */
  28 + public function index()
  29 + {
  30 + $params = $this->request->get();
  31 + $params['where']['post_type'] = 2;
  32 + $data = $this->postModel->getDatas($params);
  33 + $this->success('请求成功!', $data);
  34 + }
  35 +
  36 + /**
  37 + * 获取页面
  38 + * @param int $id
  39 + */
  40 + public function read($id)
  41 + {
  42 + $params = $this->request->get();
  43 + $params['where']['post_type'] = 2;
  44 + $params['id'] = $id;
  45 + $data = $this->postModel->getDatas($params);
  46 + $this->success('请求成功!', $data);
  47 + }
  48 +}
  1 +<?php
  2 +// +----------------------------------------------------------------------
  3 +// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4 +// +----------------------------------------------------------------------
  5 +// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
  6 +// +----------------------------------------------------------------------
  7 +// | Author: pl125 <xskjs888@163.com>
  8 +// +----------------------------------------------------------------------
  9 +namespace api\portal\controller;
  10 +
  11 +use api\portal\model\PortalPostModel;
  12 +use cmf\controller\RestBaseController;
  13 +use api\portal\model\PortalTagModel;
  14 +
  15 +class TagsController extends RestBaseController
  16 +{
  17 + protected $tagModel;
  18 +
  19 + public function __construct(PortalTagModel $tagModel)
  20 + {
  21 + parent::__construct();
  22 + $this->tagModel = $tagModel;
  23 + }
  24 +
  25 + /**
  26 + * 获取标签列表
  27 + */
  28 + public function index()
  29 + {
  30 + $params = $this->request->get();
  31 + $data = $this->tagModel->getDatas($params);
  32 + $this->success('请求成功!', $data);
  33 + }
  34 +
  35 + /**
  36 + * 获取热门标签列表
  37 + */
  38 + public function hotTags()
  39 + {
  40 + $params = $this->request->get();
  41 + $params['where']['recommended'] = 1;
  42 + $data = $this->tagModel->getDatas($params);
  43 + $this->success('请求成功!', $data);
  44 + }
  45 +
  46 + /**
  47 + * 获取标签文章列表
  48 + * @param int $id
  49 + */
  50 + public function articles($id)
  51 + {
  52 + if (intval($id) === 0) {
  53 + $this->error('无效的标签id!');
  54 + } else {
  55 + $params = $this->request->param();
  56 + $params['id'] = $id;
  57 + $params['relation'] = 'articles';
  58 + $postModel = new PortalPostModel();
  59 +
  60 + $articles = $postModel->setCondition($params)->alias('a')->join('__PORTAL_TAG_POST__ tp', 'a.id = tp.post_id')
  61 + ->where(['tag_id' => $id])->select();
  62 +
  63 + $this->success('请求成功!', ['articles' => $articles]);
  64 + }
  65 + }
  66 +}
  1 +<?php
  2 +// +----------------------------------------------------------------------
  3 +// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4 +// +----------------------------------------------------------------------
  5 +// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
  6 +// +----------------------------------------------------------------------
  7 +// | Author: pl125 <xskjs888@163.com>
  8 +// +----------------------------------------------------------------------
  9 +namespace api\portal\controller;
  10 +
  11 +use cmf\controller\RestUserBaseController;
  12 +use api\portal\logic\PortalPostModel;
  13 +
  14 +class UserArticlesController extends RestUserBaseController
  15 +{
  16 + protected $postModel;
  17 +
  18 + public function __construct(PortalPostModel $postModel)
  19 + {
  20 + parent::__construct();
  21 + $this->postModel = $postModel;
  22 + }
  23 +
  24 + /**
  25 + * 显示资源列表
  26 + */
  27 + public function index()
  28 + {
  29 + $params = $this->request->get();
  30 + $userId = $this->getUserId();
  31 + $datas = $this->postModel->getUserArticles($userId,$params);
  32 + $this->success('请求成功!', $datas);
  33 + }
  34 +
  35 + /**
  36 + * 保存新建的资源
  37 + */
  38 + public function save()
  39 + {
  40 + $datas = $this->request->post();
  41 + $datas['user_id'] = $this->getUserId();
  42 + $result = $this->validate($datas, 'Articles.article');
  43 + if ($result !== true) {
  44 + $this->error($result);
  45 + }
  46 + if (empty($datas['published_time'])) {
  47 + $datas['published_time'] = time();
  48 + }
  49 + $this->postModel->addArticle($datas);
  50 + $this->success('添加成功!');
  51 + }
  52 +
  53 + /**
  54 + * 显示指定的资源
  55 + *
  56 + * @param int $id
  57 + */
  58 + public function read($id)
  59 + {
  60 + if (empty($id)) {
  61 + $this->error('无效的文章id');
  62 + }
  63 + $params = $this->request->get();
  64 + $params['id'] = $id;
  65 + $userId = $this->getUserId();
  66 + $datas = $this->postModel->getUserArticles($userId,$params);
  67 + $this->success('请求成功!', $datas);
  68 + }
  69 +
  70 + /**
  71 + * 保存更新的资源
  72 + *
  73 + * @param int $id
  74 + */
  75 + public function update($id)
  76 + {
  77 + $data = $this->request->put();
  78 + $result = $this->validate($data, 'Articles.article');
  79 + if ($result !== true) {
  80 + $this->error($result);
  81 + }
  82 + if (empty($id)) {
  83 + $this->error('无效的文章id');
  84 + }
  85 + $result = $this->postModel->editArticle($data,$id,$this->getUserId());
  86 + if ($result === false) {
  87 + $this->error('编辑失败!');
  88 + } else {
  89 + $this->success('编辑成功!');
  90 + }
  91 + }
  92 +
  93 + /**
  94 + * 删除指定资源
  95 + *
  96 + * @param int $id
  97 + */
  98 + public function delete($id)
  99 + {
  100 + if (empty($id)) {
  101 + $this->error('无效的文章id');
  102 + }
  103 + $result = $this->postModel->deleteArticle($id,$this->getUserId());
  104 + if ($result == -1) {
  105 + $this->error('文章已删除');
  106 + }
  107 + if ($result) {
  108 + $this->success('删除成功!');
  109 + } else {
  110 + $this->error('删除失败!');
  111 + }
  112 + }
  113 + /**
  114 + * 批量删除文章
  115 + */
  116 + public function deletes()
  117 + {
  118 + $ids = $this->request->post('ids/a');
  119 + if (empty($ids)) {
  120 + $this->error('文章id不能为空');
  121 + }
  122 + $result = $this->postModel->deleteArticle($ids,$this->getUserId());
  123 + if ($result == -1) {
  124 + $this->error('文章已删除');
  125 + }
  126 + if ($result) {
  127 + $this->success('删除成功!');
  128 + } else {
  129 + $this->error('删除失败!');
  130 + }
  131 + }
  132 +
  133 + /**
  134 + * 我的文章列表
  135 + */
  136 + public function my()
  137 + {
  138 + $params = $this->request->get();
  139 + $userId = $this->getUserId();
  140 + $data = $this->postModel->getUserArticles($userId, $params);
  141 + $this->success('请求成功!', $data);
  142 + }
  143 +}
  1 +<?php
  2 +// +----------------------------------------------------------------------
  3 +// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4 +// +----------------------------------------------------------------------
  5 +// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
  6 +// +----------------------------------------------------------------------
  7 +// | Author: wuwu <15093565100@163.com>
  8 +// +----------------------------------------------------------------------
  9 +namespace api\portal\controller;
  10 +
  11 +use api\portal\model\MemberModel;
  12 +use api\portal\model\PortalPostModel;
  13 +use cmf\controller\RestBaseController;
  14 +use think\Db;
  15 +use think\Request;
  16 +use think\Loader;
  17 +use think\Config;
  18 +use think\captcha\Captcha;
  19 +use think\Url;
  20 +use SmsDemo;
  21 +//use think\Route;
  22 +/**
  23 + * @title 用户接口
  24 + * @description 接口说明
  25 + * @group 接口分组
  26 + */
  27 +
  28 +class UserController extends CommonController
  29 +{
  30 + protected $postModel;
  31 + public function __construct(PortalPostModel $postModel)
  32 + {
  33 + parent::__construct();
  34 + $this->postModel = $postModel;
  35 + }
  36 +
  37 +// 验证码图片
  38 + function getImgUrl($id = "")
  39 + {
  40 + \think\Route::get('captcha/[:id]', "\\think\\captcha\\CaptchaController@index");
  41 + \think\Validate::extend('captcha', function ($value, $id = "") {
  42 + return captcha_check($value, $id, (array)\think\Config::get('captcha'));
  43 + });
  44 + $middle_url = \think\Url::build('/captcha/new' . ($id ? "/{$id}" : ''));
  45 + $rand = str_replace(".","",substr(microtime(true),-5)).rand(1000,9999);
  46 + $imgUrl = $middle_url."&time=".$rand;
  47 + return $imgUrl;
  48 + }
  49 +// 获取短信验证码
  50 + public function getSmsResult(Request $request){
  51 + // 短信验证码
  52 + $tel = $request->param('tel');
  53 + $code = 'SMS_137416617';
  54 + $modelVal = rand(1000,9999);
  55 + $sendResult = $this->sendLogin($tel,$code,$modelVal);
  56 + if(($sendResult->Code) != 'OK'){
  57 + $this->apiResponse('0','注册失败');
  58 + }else{
  59 + $_SESSION('code',$modelVal);
  60 + $_SESSION('tel',$tel);
  61 + }
  62 + }
  63 +
  64 + /**
  65 + * @title 用户注册
  66 + * @description 接口说明
  67 + * @author 开发者
  68 + * @url /api/portal/User/join
  69 + * @method POST
  70 + * @param name:name type:int require:1 default: other: desc:姓名
  71 + * @param name:tel type:int require:1 default: other: desc:手机号
  72 + * @param name:password type:int require:1 default: other: desc:密码
  73 + * @param name:sure_password type:int require:1 default: other: desc:确认密码
  74 + */
  75 + public function join(Request $request)
  76 + {
  77 + if($request->Post()){
  78 +// 验证
  79 + $validate = Loader::validate('User');
  80 + if(!$validate->scene('add')->check($_POST)){
  81 + return json(array('code'=>0,'msg'=>$validate->getError()));
  82 + }
  83 + $data['password'] = $this->md5($_POST['password']);
  84 + $sure_password = $this->md5($_POST['sure_password']);
  85 +// 确认密码
  86 + if($data['password'] != $sure_password){
  87 + $this->apiResponse('0','两次密码不一致');
  88 + }
  89 + // 密码
  90 +
  91 +// 短信验证码
  92 +// $tel = $request->param('tel');
  93 +// $code = 'SMS_137416617';
  94 +// $modelVal = rand(1000,9999);
  95 +// $sendResult = $this->sendLogin($tel,$code,$modelVal);
  96 +// if(($sendResult->Code) != 'OK'){
  97 +// $this->apiResponse('0','注册失败');
  98 +// }
  99 + $data['tel'] = $_POST['tel'];
  100 + $code = $request->param('code');
  101 + if($data['tel'] != $_SESSION['tel']){
  102 + $this->apiResponse('0','验证码错误,请重新获取');
  103 + }else{
  104 + if($code != $_SESSION['code']){
  105 + $this->apiResponse('0','验证码错误,请重新获取');
  106 + }
  107 + }
  108 + unset($_SESSION['code']);
  109 + $data['name'] = $_POST['name'];
  110 +
  111 +// 判断手机号是否已注册
  112 + $user = new MemberModel();
  113 + $where_user['tel'] = $data['tel'];
  114 + $is_isset = $user->where($where_user)->find();
  115 + if($is_isset){
  116 + $this->apiResponse('0','您已注册过,请直接登录');
  117 + }
  118 + $str = rand(1000,9999).time().rand(100,999);
  119 + $data['token'] = $this->md5($str);
  120 + $add = $user->allowField(true)->save($data);
  121 + if($add){
  122 + $this->apiResponse('1','注册成功');
  123 + }else{
  124 + $this->apiResponse('0','注册失败');
  125 + }
  126 +
  127 + }else{
  128 +// 服务协议
  129 +// $service = new PostService();
  130 +// $list = $service->publishedArticle(1,1)->toArray();
  131 + $where_pro['status'] = 1;
  132 + $where_pro['type'] = 1;
  133 + $list = Db::name('Protocol')->where($where_pro)->order("update_time desc")->field("title,content")->find();
  134 +// 推荐人(未完)
  135 +
  136 +
  137 + if($list){
  138 + $this->apiResponse('1','成功',$list);
  139 + }else{
  140 + $this->apiResponse('0','暂无内容');
  141 + }
  142 +
  143 + }
  144 + }
  145 +
  146 +
  147 +// 登录
  148 + public function login(Request $request){
  149 + if($request->post()){
  150 +// 登录验证
  151 +// 判空
  152 + $tel = $request->param('tel');
  153 + $password = $request->param('password');
  154 + $true = $request->param('true');
  155 + $token = $request->param('token');
  156 + if(empty($tel)){
  157 + $this->apiResponse('0','手机号不能为空');
  158 + }else if(empty($password)){
  159 + $this->apiResponse('0','密码不能为空');
  160 + }else if(empty($true)){
  161 + $this->apiResponse('0','验证码不能为空');
  162 + }
  163 +// 验证
  164 + $where_member['tel'] = $tel;
  165 + $where_member['password'] = $password;
  166 +// $where_member['token'] = $token;
  167 + $member = Db::name('Member')->where($where_member)->find();
  168 + if($member){
  169 + if($member['token'] != $token){
  170 + $this->apiResponse('0','登录失败');
  171 + }
  172 + if($member['update_time'] >= (time()+604800)){
  173 + $this->apiResponse('0','请重新登录');
  174 + }else{
  175 + $this->apiResponse('1','登录成功');
  176 + }
  177 + }else{
  178 + $this->apiResponse('0','您输入的账号或密码不正确');
  179 + }
  180 +
  181 +
  182 + }else{
  183 +// 返回验证码图片
  184 +
  185 + }
  186 + }
  187 +
  188 +
  189 +
  190 +// 退出登录
  191 + public function outLogin(Request $request){
  192 +// 重置token
  193 + $str = rand('1000,9999').time().rand('100,999');
  194 + $change['token'] = $this->md5($str);
  195 + $where_member['id'] = $request->param('user_id');
  196 + $where_member['token'] = $request->param('token');
  197 + $model = new MemberModel();
  198 + $member = $model->where($where_member)->find();
  199 + if($member){
  200 + $change['id'] = $request->param('user_id');
  201 + $update = $model->isUpdate(true)->allowField(true)->save($change);
  202 + if($update){
  203 + unset($_SESSION['user_id']);
  204 + $this->apiResponse('1','退出成功');
  205 + }else{
  206 + $this->apiResponse('0','退出失败');
  207 + }
  208 + }else{
  209 + $this->apiResponse('0','用户信息错误');
  210 + }
  211 + }
  212 +// 修改密码
  213 +
  214 +}
  1 +<?php
  2 +// +----------------------------------------------------------------------
  3 +// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4 +// +----------------------------------------------------------------------
  5 +// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
  6 +// +----------------------------------------------------------------------
  7 +// | Author: wuwu <15093565100@163.com>
  8 +// +----------------------------------------------------------------------
  9 +namespace api\portal\controller;
  10 +
  11 +use api\portal\model\MemberModel;
  12 +use api\portal\model\PortalPostModel;
  13 +use cmf\controller\RestBaseController;
  14 +use think\Db;
  15 +use think\Request;
  16 +use think\Loader;
  17 +use think\Config;
  18 +use think\captcha\Captcha;
  19 +use think\Url;
  20 +use SmsDemo;
  21 +//use think\Route;
  22 +/**
  23 + * @title 用户接口
  24 + * @description 接口说明
  25 + * @group 接口分组
  26 + */
  27 +
  28 +class UserNeedController extends CommonController
  29 +{
  30 + protected $postModel;
  31 + public function __construct(PortalPostModel $postModel)
  32 + {
  33 + parent::__construct();
  34 + $this->postModel = $postModel;
  35 + }
  36 + /**
  37 + * @title 用户发布需求的保证金
  38 + * @description 接口说明
  39 + * @author 开发者
  40 + * @url /api/portal/UserNeed/payMoney
  41 + * @method POST
  42 + * @param name:city type:int require:1 default: other: desc:地区
  43 + * @param name:quarters type:int require:1 default: other: desc:小区
  44 + * @param name:password type:int require:1 default: other: desc:密码
  45 + */
  46 + public function payMoney(Request $request){
  47 + $data = $request->param();
  48 + if(!$data['city'] || !$data['area'] ){
  49 + $this->apiResponse('0','城市、面积不能为空');
  50 + }
  51 + $city = $data['city'];
  52 + $area = $data['area'];
  53 +
  54 + }
  55 +
  56 +
  57 + /**
  58 + * @title 用户发布需求
  59 + * @description 接口说明
  60 + * @author 开发者
  61 + * @url /api/portal/UserNeed/send
  62 + * @method POST
  63 + * @param name:city type:int require:1 default: other: desc:地区
  64 + * @param name:quarters type:int require:1 default: other: desc:小区
  65 + * @param name:password type:int require:1 default: other: desc:密码
  66 + */
  67 + public function send(Request $request)
  68 + {
  69 + if($request->Post()){
  70 + $data = $request->param();
  71 +// 判空,处理
  72 +// 加数据库
  73 + }else{
  74 +// 问答内容
  75 + $where_pro['status'] = 1;
  76 + $list = Db::name('NeedAnswer')->where($where_pro)->order("update_time desc")->find();
  77 + if($list){
  78 + $this->apiResponse('1','成功',$list);
  79 + }else{
  80 + $this->apiResponse('0','暂无内容');
  81 + }
  82 +
  83 + }
  84 + }
  85 +
  86 +
  87 +
  88 +
  89 +}
  1 +<?php
  2 +// +----------------------------------------------------------------------
  3 +// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4 +// +----------------------------------------------------------------------
  5 +// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
  6 +// +----------------------------------------------------------------------
  7 +// | Author: pl125 <xskjs888@163.com>
  8 +// +----------------------------------------------------------------------
  9 +
  10 +namespace api\portal\logic;
  11 +
  12 +use api\portal\model\PortalPostModel as PortalPost;
  13 +use think\Db;
  14 +class PortalPostModel extends PortalPost
  15 +{
  16 + /**
  17 + * 获取相关文章
  18 + * @param int|string|array $postIds 文章id
  19 + * @return array
  20 + */
  21 + public function getRelationPosts($postIds)
  22 + {
  23 + $posts = $this->with('articleUser')
  24 + ->field('id,post_title,user_id,is_top,post_hits,post_like,comment_count,more')
  25 + ->whereIn('id', $postIds)
  26 + ->select();
  27 + foreach ($posts as $post) {
  28 + $post->appendRelationAttr('articleUser', 'user_nickname');
  29 + }
  30 + return $posts;
  31 + }
  32 + /**
  33 + * 获取用户文章
  34 + */
  35 + public function getUserArticles($userId, $params)
  36 + {
  37 + $where = [
  38 + 'post_type' => 1,
  39 + 'user_id' => $userId
  40 + ];
  41 + if (!empty($params)) {
  42 + $this->paramsFilter($params);
  43 + }
  44 + return $this->where($where)->select();
  45 + }
  46 +
  47 + /**
  48 + * 会员添加文章
  49 + * @param array $data 文章数据
  50 + * @return $this
  51 + */
  52 + public function addArticle($data)
  53 + {
  54 + //设置图片附件,写入字段过滤
  55 + $dataField = $this->setMoreField($data);
  56 + $data = $dataField[0];
  57 + array_push($dataField[1],'user_id');
  58 + $this->readonly = array_diff(['user_id'],$this->readonly);
  59 + $this->allowField($dataField[1])->data($data, true)->isUpdate(false)->save();
  60 + $categories = $this->strToArr($data['categories']);
  61 + $this->categories()->attach($categories);
  62 + if (!empty($data['post_keywords']) && is_string($data['post_keywords'])) {
  63 + //加入标签
  64 + $data['post_keywords'] = str_replace(',', ',', $data['post_keywords']);
  65 + $keywords = explode(',', $data['post_keywords']);
  66 + $this->addTags($keywords, $this->id);
  67 + }
  68 + return $this;
  69 + }
  70 +
  71 + /**
  72 + * 会员文章编辑
  73 + * @param array $data 文章数据
  74 + * @param int $id 文章id
  75 + * @param int $userId 文章所属用户id [可选]
  76 + * @return boolean 成功 true 失败 false
  77 + */
  78 + public function editArticle($data, $id, $userId = '')
  79 + {
  80 + if (!empty($userId)) {
  81 + $isBelong = $this->isuserPost($id, $userId);
  82 + if ($isBelong === false) {
  83 + return $isBelong;
  84 + }
  85 + }
  86 + //设置图片附件,写入字段过滤
  87 + $dataField = $this->setMoreField($data);
  88 + $data = $dataField[0];
  89 + $data['id'] = $id;
  90 + $this->allowField($dataField[1])->data($data, true)->isUpdate(true)->save();
  91 +
  92 + $categories = $this->strToArr($data['categories']);
  93 + $oldCategoryIds = $this->categories()->column('category_id');
  94 + $sameCategoryIds = array_intersect($categories, $oldCategoryIds);
  95 + $needDeleteCategoryIds = array_diff($oldCategoryIds, $sameCategoryIds);
  96 + $newCategoryIds = array_diff($categories, $sameCategoryIds);
  97 + if (!empty($needDeleteCategoryIds)) {
  98 + $this->categories()->detach($needDeleteCategoryIds);
  99 + }
  100 + if (!empty($newCategoryIds)) {
  101 + $this->categories()->attach(array_values($newCategoryIds));
  102 + }
  103 + if (!isset($data['post_keywords'])) {
  104 + $keywords = [];
  105 + } elseif (is_string($data['post_keywords'])) {
  106 + //加入标签
  107 + $data['post_keywords'] = str_replace(',', ',', $data['post_keywords']);
  108 + $keywords = explode(',', $data['post_keywords']);
  109 + }
  110 + $this->addTags($keywords, $data['id']);
  111 + return $this;
  112 + }
  113 +
  114 + /**
  115 + * 根据文章关键字,增加标签
  116 + * @param array $keywords 文章关键字数组
  117 + * @param int $articleId 文章id
  118 + * @return void
  119 + */
  120 + public function addTags($keywords, $articleId)
  121 + {
  122 + foreach ($keywords as $key => $value) {
  123 + $keywords[$key] = trim($value);
  124 + }
  125 + $continue = true;
  126 + $names = $this->tags()->column('name');
  127 + if (!empty($keywords) || !empty($names)) {
  128 + if (!empty($names)) {
  129 + $sameNames = array_intersect($keywords, $names);
  130 + $keywords = array_diff($keywords, $sameNames);
  131 + $shouldDeleteNames = array_diff($names, $sameNames);
  132 + if (!empty($shouldDeleteNames)) {
  133 + $tagIdNames = $this->tags()
  134 + ->where('name', 'in', $shouldDeleteNames)
  135 + ->column('pivot.id', 'tag_id');
  136 + $tagIds = array_keys($tagIdNames);
  137 + $tagPostIds = array_values($tagIdNames);
  138 + $tagPosts = DB::name('portal_tag_post')->where('tag_id', 'in', $tagIds)
  139 + ->field('id,tag_id,post_id')
  140 + ->select();
  141 + $keepTagIds = [];
  142 + foreach ($tagPosts as $key => $tagPost) {
  143 + if ($articleId != $tagPost['post_id']) {
  144 + array_push($keepTagIds, $tagPost['tag_id']);
  145 + }
  146 + }
  147 + $keepTagIds = array_unique($keepTagIds);
  148 + $shouldDeleteTagIds = array_diff($tagIds, $keepTagIds);
  149 + DB::name('PortalTag')->delete($shouldDeleteTagIds);
  150 + DB::name('PortalTagPost')->delete($tagPostIds);
  151 + }
  152 + } else {
  153 + $tagIdNames = DB::name('portal_tag')->where('name', 'in', $keywords)->column('name', 'id');
  154 + if (!empty($tagIdNames)) {
  155 + $tagIds = array_keys($tagIdNames);
  156 + $this->tags()->attach($tagIds);
  157 + $keywords = array_diff($keywords, array_values($tagIdNames));
  158 + if (empty($keywords)) {
  159 + $continue = false;
  160 + }
  161 + }
  162 + }
  163 + if ($continue) {
  164 + foreach ($keywords as $key => $value) {
  165 + if (!empty($value)) {
  166 + $this->tags()->attach(['name' => $value]);
  167 + }
  168 + }
  169 + }
  170 + }
  171 + }
  172 +
  173 + /**
  174 + * 设置缩略图,图片,附件
  175 + * 懒人方法
  176 + * @param $data 表单数据
  177 + */
  178 + public function setMoreField($data)
  179 + {
  180 + $allowField = [
  181 + 'post_title','post_keywords','post_source',
  182 + 'post_excerpt','post_content','more',
  183 + 'published_time'
  184 + ];
  185 + if (!empty($data['more'])) {
  186 + $data['more'] = $this->setMoreUrl($data['more']);
  187 + }
  188 + if (!empty($data['thumbnail'])) {
  189 + $data['more']['thumbnail'] = cmf_asset_relative_url($data['thumbnail']);
  190 + }
  191 + return [$data,$allowField];
  192 + }
  193 +
  194 + /**
  195 + * 获取图片附件url相对地址
  196 + * 默认上传名字 *_names 地址 *_urls
  197 + * @param $annex 上传附件
  198 + * @return array
  199 + */
  200 + public function setMoreUrl($annex)
  201 + {
  202 + $more = [];
  203 + if (!empty($annex)) {
  204 + foreach ($annex as $key => $value) {
  205 + $nameArr = $key . '_names';
  206 + $urlArr = $key . '_urls';
  207 + if (is_string($value[$nameArr]) && is_string($value[$urlArr])) {
  208 + $more[$key] = [$value[$nameArr], $value[$urlArr]];
  209 + } elseif (!empty($value[$nameArr]) && !empty($value[$urlArr])) {
  210 + $more[$key] = [];
  211 + foreach ($value[$urlArr] as $k => $url) {
  212 + $url = cmf_asset_relative_url($url);
  213 + array_push($more[$key], ['url' => $url, 'name' => $value[$nameArr][$k]]);
  214 + }
  215 + }
  216 + }
  217 + }
  218 + return $more;
  219 + }
  220 +
  221 + /**
  222 + * 删除文章
  223 + * @param $ids int|array 文章id
  224 + * @param int $userId 文章所属用户id [可选]
  225 + * @return bool|int 删除结果 true 成功 false 失败 -1 文章不存在
  226 + */
  227 + public function deleteArticle($ids, $userId)
  228 + {
  229 + $time = time();
  230 + $result = false;
  231 + $where = [];
  232 +
  233 + if (!empty($userId)) {
  234 + if (is_numeric($ids)) {
  235 + $article = $this->find($ids);
  236 + if (!empty($article)) {
  237 + if ($this->isUserPost($ids, $userId) || $userId == 1) {
  238 + $where['id'] = $ids;
  239 + }
  240 + }
  241 + } else {
  242 + $ids = $this->strToArr($ids);
  243 + $articles = $this->where('id', 'in', $ids)->select();
  244 + if (!empty($articles)) {
  245 + $deleteIds = $this->isUserPosts($ids, $userId);
  246 + if (!empty($deleteIds)) {
  247 + $where['id'] = ['in', $deleteIds];
  248 + }
  249 + }
  250 + }
  251 + } else {
  252 + if (is_numeric($ids)) {
  253 + $article = $this->find($ids);
  254 + if (!empty($article)) {
  255 + $where['id'] = $ids;
  256 + }
  257 + } else {
  258 + $ids = $this->strToArr($ids);
  259 + $articles = $this->where('id', 'in', $ids)->select();
  260 + if (!empty($articles)) {
  261 + $where['id'] = ['in', $ids];
  262 + }
  263 + }
  264 + }
  265 + if (empty($article) && empty($articles)) {
  266 + return -1;
  267 + }
  268 + if (!empty($where)) {
  269 + $result = $this->useGlobalScope(false)
  270 + ->where($where)
  271 + ->setField('delete_time', $time);
  272 + }
  273 + if ($result) {
  274 + $data = [
  275 + 'create_time' => $time,
  276 + 'table_name' => 'portal_post'
  277 + ];
  278 + if (!empty($article)) {
  279 + $data['name'] = $article['post_title'];
  280 + $article->recycleBin()->save($data);
  281 + }
  282 +
  283 + if (!empty($articles)) {
  284 + foreach ($articles as $article) {
  285 + $data['name'] = $article['post_title'];
  286 + $article->recycleBin()->save($data);
  287 + }
  288 + }
  289 + }
  290 + return $result;
  291 + }
  292 +
  293 + /**
  294 + * 判断文章所属用户是否为当前用户,超级管理员除外
  295 + * @params int $id 文章id
  296 + * @param int $userId 当前用户id
  297 + * @return boolean 是 true , 否 false
  298 + */
  299 + public function isUserPost($id, $userId)
  300 + {
  301 + $postUserId = $this->useGlobalScope(false)
  302 + ->getFieldById($id, 'user_id');
  303 + if ($postUserId != $userId || $userId != 1) {
  304 + return false;
  305 + } else {
  306 + return true;
  307 + }
  308 + }
  309 +
  310 + /**
  311 + * 过滤属于当前用户的文章,超级管理员除外
  312 + * @params array $ids 文章id的数组
  313 + * @param int $userId 当前用户id
  314 + * @return array 属于当前用户的文章id
  315 + */
  316 + public function isUserPosts($ids, $userId)
  317 + {
  318 + $postIds = $this->useGlobalScope(false)
  319 + ->where('user_id', $userId)
  320 + ->where('id', 'in', $ids)
  321 + ->column('id');
  322 + return array_intersect($ids, $postIds);
  323 + }
  324 +}
  1 +<?php
  2 +// +----------------------------------------------------------------------
  3 +// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4 +// +----------------------------------------------------------------------
  5 +// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
  6 +// +----------------------------------------------------------------------
  7 +// | Author: pl125 <xskjs888@163.com>
  8 +// +----------------------------------------------------------------------
  9 +
  10 +namespace api\portal\model;
  11 +use api\common\model\CommonModel;
  12 +
  13 +class IntermediaryModel extends CommonModel
  14 +{
  15 + //可查询字段
  16 +// protected $visible = [
  17 +// 'articles.id', 'user_nickname', 'avatar', 'signature','user'
  18 +// ];
  19 + //模型关联方法
  20 + protected $relationFilter = ['user'];
  21 +
  22 + /**
  23 + * 基础查询
  24 + */
  25 + protected function base($query)
  26 + {
  27 + $query->alias('user')->where('user.user_status', 1);
  28 + }
  29 +
  30 + /**
  31 + * more 自动转化
  32 + * @param $value
  33 + * @return array
  34 + */
  35 + public function getAvatarAttr($value)
  36 + {
  37 + $value = !empty($value) ? cmf_get_image_url($value) : $value;
  38 + return $value;
  39 + }
  40 +
  41 + /**
  42 + * 关联 user表
  43 + * @return $this
  44 + */
  45 + public function user()
  46 + {
  47 + return $this->belongsTo('UserModel', 'user_id')->setEagerlyType(1);
  48 + }
  49 +}
  1 +<?php
  2 +// +----------------------------------------------------------------------
  3 +// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4 +// +----------------------------------------------------------------------
  5 +// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
  6 +// +----------------------------------------------------------------------
  7 +// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8 +// +----------------------------------------------------------------------
  9 +// | Author: 老猫 <thinkcmf@126.com>
  10 +// +----------------------------------------------------------------------
  11 +namespace api\portal\model;
  12 +
  13 +use think\Model;
  14 +
  15 +class MemberModel extends Model
  16 +{
  17 + protected $autoWriteTimestamp = true;
  18 +// protected $auto = [];
  19 + protected $insert = ['create_time'];
  20 + protected $update = [''];
  21 +
  22 + protected function setCreateTimeAttr()
  23 + {
  24 + return time();
  25 + }
  26 +
  27 +}
  1 +<?php
  2 +// +----------------------------------------------------------------------
  3 +// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4 +// +----------------------------------------------------------------------
  5 +// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
  6 +// +----------------------------------------------------------------------
  7 +// | Author: pl125 <xskjs888@163.com>
  8 +// +----------------------------------------------------------------------
  9 +
  10 +namespace api\portal\model;
  11 +
  12 +use api\common\model\CommonModel;
  13 +
  14 +class PortalCategoryModel extends CommonModel
  15 +{
  16 + //类型转换
  17 + protected $type = [
  18 + 'more' => 'array',
  19 + ];
  20 +
  21 + //可查询字段
  22 + protected $visible = [
  23 + 'id', 'name', 'description', 'post_count',
  24 + 'seo_title', 'seo_keywords', 'seo_description',
  25 + 'more', 'PostIds', 'articles'
  26 + ];
  27 +
  28 + //模型关联方法
  29 + protected $relationFilter = ['articles'];
  30 +
  31 + /**
  32 + * 基础查询
  33 + */
  34 + protected function base($query)
  35 + {
  36 + $query->alias('portal_category')->where('delete_time', 0)
  37 + ->where('portal_category.status', 1);
  38 + }
  39 +
  40 + /**
  41 + * more 自动转化
  42 + * @param $value
  43 + * @return array
  44 + */
  45 + public function getMoreAttr($value)
  46 + {
  47 + $more = json_decode($value, true);
  48 + if (!empty($more['thumbnail'])) {
  49 + $more['thumbnail'] = cmf_get_image_url($more['thumbnail']);
  50 + }
  51 +
  52 + if (!empty($more['photos'])) {
  53 + foreach ($more['photos'] as $key => $value) {
  54 + $more['photos'][$key]['url'] = cmf_get_image_url($value['url']);
  55 + }
  56 + }
  57 + return $more;
  58 + }
  59 +
  60 + /**
  61 + * 关联文章表
  62 + * @return $this
  63 + */
  64 + public function articles()
  65 + {
  66 + return $this->belongsToMany('PortalPostModel', 'portal_category_post', 'post_id', 'category_id');
  67 + }
  68 +
  69 + /**
  70 + * [PostIds 关联]
  71 + * @Author: wuwu<15093565100@163.com>
  72 + * @DateTime: 2017-07-17T15:20:31+0800
  73 + * @since: 1.0
  74 + */
  75 + public function PostIds()
  76 + {
  77 + return self::hasMany('PortalCategoryPostModel', 'category_id', 'id');
  78 + }
  79 +
  80 + /**
  81 + * [categoryPostIds 此类文章id数组]
  82 + * @Author: wuwu<15093565100@163.com>
  83 + * @DateTime: 2017-07-17T15:21:08+0800
  84 + * @since: 1.0
  85 + * @param [type] $category_id [分类ID]
  86 + * @return [type] [文章id数组]
  87 + */
  88 + public static function categoryPostIds($category_id)
  89 + {
  90 + $ids = [];
  91 + $post_ids = self::relation('PostIds')->field(true)->where('id', $category_id)->find();
  92 + foreach ($post_ids['PostIds'] as $key => $id) {
  93 + $ids[] = $id['post_id'];
  94 + }
  95 + $post_ids['PostIds'] = $ids;
  96 + return $post_ids;
  97 + }
  98 +}
  1 +<?php
  2 +// +----------------------------------------------------------------------
  3 +// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4 +// +----------------------------------------------------------------------
  5 +// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
  6 +// +----------------------------------------------------------------------
  7 +// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8 +// +----------------------------------------------------------------------
  9 +// | Author: wuwu <15093565100@163.com>
  10 +// +----------------------------------------------------------------------
  11 +namespace api\portal\model;
  12 +
  13 +use think\Model;
  14 +
  15 +class PortalCategoryPostModel extends Model
  16 +{
  17 + /**
  18 + * 基础查询
  19 + */
  20 + protected function base($query)
  21 + {
  22 + $query->where('status', 1);
  23 + }
  24 +}
  1 +<?php
  2 +// +----------------------------------------------------------------------
  3 +// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4 +// +----------------------------------------------------------------------
  5 +// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
  6 +// +----------------------------------------------------------------------
  7 +// | Author: pl125 <xskjs888@163.com>
  8 +// +----------------------------------------------------------------------
  9 +namespace api\portal\model;
  10 +
  11 +use think\Db;
  12 +use api\common\model\CommonModel;
  13 +
  14 +class PortalPostModel extends CommonModel
  15 +{
  16 + //可查询字段
  17 + protected $visible = [
  18 + 'id', 'articles.id', 'user_id', 'post_id', 'post_type', 'comment_status',
  19 + 'is_top', 'recommended', 'post_hits', 'post_like', 'comment_count',
  20 + 'create_time', 'update_time', 'published_time', 'post_title', 'post_keywords',
  21 + 'post_excerpt', 'post_source', 'post_content', 'more', 'user_nickname',
  22 + 'user', 'category_id'
  23 + ];
  24 +
  25 + //设置只读字段
  26 + protected $readonly = ['user_id'];
  27 + // 开启自动写入时间戳字段
  28 + protected $autoWriteTimestamp = true;
  29 + //类型转换
  30 + protected $type = [
  31 + 'more' => 'array',
  32 + ];
  33 + //模型关联方法
  34 + protected $relationFilter = ['user', 'categories'];
  35 +
  36 + /**
  37 + * 基础查询
  38 + */
  39 + protected function base($query)
  40 + {
  41 + $query->where('delete_time', 0)
  42 + ->where('post_status', 1)
  43 + ->whereTime('published_time', 'between', [1, time()]);
  44 + }
  45 +
  46 + /**
  47 + * 关联 user表
  48 + * @return $this
  49 + */
  50 + public function user()
  51 + {
  52 + return $this->belongsTo('api\portal\model\UserModel', 'user_id');
  53 + }
  54 +
  55 + /**
  56 + * 关联 user表
  57 + * @return $this
  58 + */
  59 + public function articleUser()
  60 + {
  61 + return $this->belongsTo('api\portal\model\UserModel', 'user_id')->field('id,user_nickname');
  62 + }
  63 +
  64 + /**
  65 + * 关联分类表
  66 + * @return $this
  67 + */
  68 + public function categories()
  69 + {
  70 + return $this->belongsToMany('api\portal\model\PortalCategoryModel', 'portal_category_post', 'category_id', 'post_id');
  71 + }
  72 +
  73 + /**
  74 + * 关联标签表
  75 + * @return $this
  76 + */
  77 + public function tags()
  78 + {
  79 + return $this->belongsToMany('api\portal\model\PortalTagModel', 'portal_tag_post', 'tag_id', 'post_id');
  80 + }
  81 +
  82 + /**
  83 + * 关联 回收站 表
  84 + */
  85 + public function recycleBin()
  86 + {
  87 + return $this->hasOne('api\portal\model\RecycleBinModel', 'object_id');
  88 + }
  89 +
  90 + /**
  91 + * published_time 自动转化
  92 + * @param $value
  93 + * @return string
  94 + */
  95 + public function getPublishedTimeAttr($value)
  96 + {
  97 + // 兼容老版本 1.0.0的客户端
  98 + $apiVersion = request()->header('XX-Api-Version');
  99 + if (empty($apiVersion)) {
  100 + return date('Y-m-d H:i:s', $value);
  101 + } else {
  102 + return $value;
  103 + }
  104 + }
  105 +
  106 + /**
  107 + * published_time 自动转化
  108 + * @param $value
  109 + * @return int
  110 + */
  111 + public function setPublishedTimeAttr($value)
  112 + {
  113 + if (is_numeric($value)) {
  114 + return $value;
  115 + }
  116 + return strtotime($value);
  117 + }
  118 +
  119 + public function getPostTitleAttr($value)
  120 + {
  121 + return htmlspecialchars_decode($value);
  122 + }
  123 +
  124 + public function getPostExcerptAttr($value)
  125 + {
  126 + return htmlspecialchars_decode($value);
  127 + }
  128 +
  129 + /**
  130 + * post_content 自动转化
  131 + * @param $value
  132 + * @return string
  133 + */
  134 + public function getPostContentAttr($value)
  135 + {
  136 + return cmf_replace_content_file_url(htmlspecialchars_decode($value));
  137 + }
  138 +
  139 + /**
  140 + * post_content 自动转化
  141 + * @param $value
  142 + * @return string
  143 + */
  144 + public function setPostContentAttr($value)
  145 + {
  146 + return htmlspecialchars(cmf_replace_content_file_url(htmlspecialchars_decode($value), true));
  147 + }
  148 +
  149 + /**
  150 + * more 自动转化
  151 + * @param $value
  152 + * @return array
  153 + */
  154 + public function getMoreAttr($value)
  155 + {
  156 + $more = json_decode($value, true);
  157 + if (!empty($more['thumbnail'])) {
  158 + $more['thumbnail'] = cmf_get_image_url($more['thumbnail']);
  159 + }
  160 +
  161 + if (!empty($more['photos'])) {
  162 + foreach ($more['photos'] as $key => $value) {
  163 + $more['photos'][$key]['url'] = cmf_get_image_url($value['url']);
  164 + }
  165 + }
  166 +
  167 + if (!empty($more['files'])) {
  168 + foreach ($more['files'] as $key => $value) {
  169 + $more['files'][$key]['url'] = cmf_get_file_download_url($value['url']);
  170 + }
  171 + }
  172 + return $more;
  173 + }
  174 +
  175 + /**
  176 + * 获取用户文章
  177 + */
  178 + public function getUserArticles($userId, $params)
  179 + {
  180 + $where = [
  181 + 'post_type' => 1,
  182 + 'user_id' => $userId
  183 + ];
  184 +
  185 + $params['where'] = $where;
  186 +
  187 + return $this->getDatas($params);;
  188 + }
  189 +
  190 + /**
  191 + * 会员添加文章
  192 + * @param array $data 文章数据
  193 + * @return $this
  194 + */
  195 + public function addArticle($data)
  196 + {
  197 + if (!empty($data['more'])) {
  198 + $data['more'] = $this->setMoreUrl($data['more']);
  199 + }
  200 + if (!empty($data['thumbnail'])) {
  201 + $data['more']['thumbnail'] = cmf_asset_relative_url($data['thumbnail']);
  202 + }
  203 + $this->allowField(true)->data($data, true)->isUpdate(false)->save();
  204 + $categories = $this->strToArr($data['categories']);
  205 + $this->categories()->attach($categories);
  206 + if (!empty($data['post_keywords']) && is_string($data['post_keywords'])) {
  207 + //加入标签
  208 + $data['post_keywords'] = str_replace(',', ',', $data['post_keywords']);
  209 + $keywords = explode(',', $data['post_keywords']);
  210 + $this->addTags($keywords, $this->id);
  211 + }
  212 + return $this;
  213 + }
  214 +
  215 + /**
  216 + * 会员文章编辑
  217 + * @param array $data 文章数据
  218 + * @param int $id 文章id
  219 + * @param int $userId 文章所属用户id [可选]
  220 + * @return boolean 成功 true 失败 false
  221 + */
  222 + public function editArticle($data, $id, $userId = '')
  223 + {
  224 + if (!empty($userId)) {
  225 + $isBelong = $this->isuserPost($id, $userId);
  226 + if ($isBelong === false) {
  227 + return $isBelong;
  228 + }
  229 + }
  230 + if (!empty($data['more'])) {
  231 + $data['more'] = $this->setMoreUrl($data['more']);
  232 + }
  233 + if (!empty($data['thumbnail'])) {
  234 + $data['more']['thumbnail'] = cmf_asset_relative_url($data['thumbnail']);
  235 + }
  236 + $data['id'] = $id;
  237 + $data['post_status'] = empty($data['post_status']) ? 0 : 1;
  238 + $data['is_top'] = empty($data['is_top']) ? 0 : 1;
  239 + $data['recommended'] = empty($data['recommended']) ? 0 : 1;
  240 + $this->allowField(true)->data($data, true)->isUpdate(true)->save();
  241 +
  242 + $categories = $this->strToArr($data['categories']);
  243 + $oldCategoryIds = $this->categories()->column('category_id');
  244 + $sameCategoryIds = array_intersect($categories, $oldCategoryIds);
  245 + $needDeleteCategoryIds = array_diff($oldCategoryIds, $sameCategoryIds);
  246 + $newCategoryIds = array_diff($categories, $sameCategoryIds);
  247 + if (!empty($needDeleteCategoryIds)) {
  248 + $this->categories()->detach($needDeleteCategoryIds);
  249 + }
  250 +
  251 + if (!empty($newCategoryIds)) {
  252 + $this->categories()->attach(array_values($newCategoryIds));
  253 + }
  254 +
  255 + $keywords = [];
  256 +
  257 + if (!empty($data['post_keywords'])) {
  258 + if (is_string($data['post_keywords'])) {
  259 + //加入标签
  260 + $data['post_keywords'] = str_replace(',', ',', $data['post_keywords']);
  261 + $keywords = explode(',', $data['post_keywords']);
  262 + }
  263 + }
  264 +
  265 + $this->addTags($keywords, $data['id']);
  266 +
  267 + return $this;
  268 + }
  269 +
  270 + /**
  271 + * 根据文章关键字,增加标签
  272 + * @param array $keywords 文章关键字数组
  273 + * @param int $articleId 文章id
  274 + * @return void
  275 + */
  276 + public function addTags($keywords, $articleId)
  277 + {
  278 + foreach ($keywords as $key => $value) {
  279 + $keywords[$key] = trim($value);
  280 + }
  281 + $continue = true;
  282 + $names = $this->tags()->column('name');
  283 + if (!empty($keywords) || !empty($names)) {
  284 + if (!empty($names)) {
  285 + $sameNames = array_intersect($keywords, $names);
  286 + $keywords = array_diff($keywords, $sameNames);
  287 + $shouldDeleteNames = array_diff($names, $sameNames);
  288 + if (!empty($shouldDeleteNames)) {
  289 + $tagIdNames = $this->tags()
  290 + ->where('name', 'in', $shouldDeleteNames)
  291 + ->column('pivot.id', 'tag_id');
  292 + $tagIds = array_keys($tagIdNames);
  293 + $tagPostIds = array_values($tagIdNames);
  294 + $tagPosts = DB::name('portal_tag_post')->where('tag_id', 'in', $tagIds)
  295 + ->field('id,tag_id,post_id')
  296 + ->select();
  297 + $keepTagIds = [];
  298 + foreach ($tagPosts as $key => $tagPost) {
  299 + if ($articleId != $tagPost['post_id']) {
  300 + array_push($keepTagIds, $tagPost['tag_id']);
  301 + }
  302 + }
  303 + $keepTagIds = array_unique($keepTagIds);
  304 + $shouldDeleteTagIds = array_diff($tagIds, $keepTagIds);
  305 + DB::name('PortalTag')->delete($shouldDeleteTagIds);
  306 + DB::name('PortalTagPost')->delete($tagPostIds);
  307 + }
  308 + } else {
  309 + $tagIdNames = DB::name('portal_tag')->where('name', 'in', $keywords)->column('name', 'id');
  310 + if (!empty($tagIdNames)) {
  311 + $tagIds = array_keys($tagIdNames);
  312 + $this->tags()->attach($tagIds);
  313 + $keywords = array_diff($keywords, array_values($tagIdNames));
  314 + if (empty($keywords)) {
  315 + $continue = false;
  316 + }
  317 + }
  318 + }
  319 + if ($continue) {
  320 + foreach ($keywords as $key => $value) {
  321 + if (!empty($value)) {
  322 + $this->tags()->attach(['name' => $value]);
  323 + }
  324 + }
  325 + }
  326 + }
  327 + }
  328 +
  329 + /**
  330 + * 获取图片附件url相对地址
  331 + * 默认上传名字 *_names 地址 *_urls
  332 + * @param $annex 上传附件
  333 + * @return array
  334 + */
  335 + public function setMoreUrl($annex)
  336 + {
  337 + $more = [];
  338 + if (!empty($annex)) {
  339 + foreach ($annex as $key => $value) {
  340 + $nameArr = $key . '_names';
  341 + $urlArr = $key . '_urls';
  342 + if (is_string($value[$nameArr]) && is_string($value[$urlArr])) {
  343 + $more[$key] = [$value[$nameArr], $value[$urlArr]];
  344 + } elseif (!empty($value[$nameArr]) && !empty($value[$urlArr])) {
  345 + $more[$key] = [];
  346 + foreach ($value[$urlArr] as $k => $url) {
  347 + $url = cmf_asset_relative_url($url);
  348 + array_push($more[$key], ['url' => $url, 'name' => $value[$nameArr][$k]]);
  349 + }
  350 + }
  351 + }
  352 + }
  353 + return $more;
  354 + }
  355 +
  356 + /**
  357 + * 删除文章
  358 + * @param $ids int|array 文章id
  359 + * @param int $userId 文章所属用户id [可选]
  360 + * @return bool|int 删除结果 true 成功 false 失败 -1 文章不存在
  361 + */
  362 + public function deleteArticle($ids, $userId = '')
  363 + {
  364 + $time = time();
  365 + $result = false;
  366 + $where = [];
  367 +
  368 + if (!empty($userId)) {
  369 + if (is_numeric($ids)) {
  370 + $article = $this->find($ids);
  371 + if (!empty($article)) {
  372 + if ($this->isUserPost($ids, $userId) || $userId == 1) {
  373 + $where['id'] = $ids;
  374 + }
  375 + }
  376 + } else {
  377 + $ids = $this->strToArr($ids);
  378 + $articles = $this->where('id', 'in', $ids)->select();
  379 + if (!empty($articles)) {
  380 + $deleteIds = $this->isUserPosts($ids, $userId);
  381 + if (!empty($deleteIds)) {
  382 + $where['id'] = ['in', $deleteIds];
  383 + }
  384 + }
  385 + }
  386 + } else {
  387 + if (is_numeric($ids)) {
  388 + $article = $this->find($ids);
  389 + if (!empty($article)) {
  390 + $where['id'] = $ids;
  391 + }
  392 + } else {
  393 + $ids = $this->strToArr($ids);
  394 + $articles = $this->where('id', 'in', $ids)->select();
  395 + if (!empty($articles)) {
  396 + $where['id'] = ['in', $ids];
  397 + }
  398 + }
  399 + }
  400 + if (empty($article) && empty($articles)) {
  401 + return -1;
  402 + }
  403 + if (!empty($where)) {
  404 + $result = $this->useGlobalScope(false)
  405 + ->where($where)
  406 + ->setField('delete_time', $time);
  407 + }
  408 + if ($result) {
  409 + $data = [
  410 + 'create_time' => $time,
  411 + 'table_name' => 'portal_post'
  412 + ];
  413 + if (!empty($article)) {
  414 + $data['name'] = $article['post_title'];
  415 + $article->recycleBin()->save($data);
  416 + }
  417 +
  418 + if (!empty($articles)) {
  419 + foreach ($articles as $article) {
  420 + $data['name'] = $article['post_title'];
  421 + $article->recycleBin()->save($data);
  422 + }
  423 + }
  424 + }
  425 + return $result;
  426 + }
  427 +
  428 + /**
  429 + * 判断文章所属用户是否为当前用户,超级管理员除外
  430 + * @params int $id 文章id
  431 + * @param int $userId 当前用户id
  432 + * @return boolean 是 true , 否 false
  433 + */
  434 + public function isUserPost($id, $userId)
  435 + {
  436 + $postUserId = $this->useGlobalScope(false)
  437 + ->getFieldById($id, 'user_id');
  438 + if ($postUserId == $userId || $userId == 1) {
  439 + return true;
  440 + } else {
  441 + return false;
  442 + }
  443 + }
  444 +
  445 + /**
  446 + * 过滤属于当前用户的文章,超级管理员除外
  447 + * @params array $ids 文章id的数组
  448 + * @param int $userId 当前用户id
  449 + * @return array 属于当前用户的文章id
  450 + */
  451 + public function isUserPosts($ids, $userId)
  452 + {
  453 + $postIds = $this->useGlobalScope(false)
  454 + ->where('user_id', $userId)
  455 + ->where('id', 'in', $ids)
  456 + ->column('id');
  457 + return array_intersect($ids, $postIds);
  458 + }
  459 +}
  1 +<?php
  2 +// +----------------------------------------------------------------------
  3 +// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4 +// +----------------------------------------------------------------------
  5 +// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
  6 +// +----------------------------------------------------------------------
  7 +// | Author: pl125 <xskjs888@163.com>
  8 +// +----------------------------------------------------------------------
  9 +
  10 +namespace api\portal\model;
  11 +
  12 +use api\common\model\CommonModel;
  13 +class PortalTagModel extends CommonModel
  14 +{
  15 + //可查询字段
  16 + protected $visible = [
  17 + 'id','articles.id','recommended', 'post_count', 'name','articles'
  18 + ];
  19 + //模型关联方法
  20 + protected $relationFilter = ['articles'];
  21 +
  22 + /**
  23 + * 基础查询
  24 + */
  25 + protected function base($query)
  26 + {
  27 + $query->alias('post_tag')->where('post_tag.status', 1);
  28 + }
  29 + /**
  30 + * 关联 文章表
  31 + * @return $this
  32 + */
  33 + public function articles()
  34 + {
  35 + return $this->belongsToMany('PortalPostModel','portal_tag_post','post_id','tag_id');
  36 + }
  37 +}
  1 +<?php
  2 +// +----------------------------------------------------------------------
  3 +// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4 +// +----------------------------------------------------------------------
  5 +// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
  6 +// +----------------------------------------------------------------------
  7 +// | Author: pl125 <xskjs888@163.com>
  8 +// +----------------------------------------------------------------------
  9 +
  10 +namespace api\portal\model;
  11 +
  12 +use think\Model;
  13 +
  14 +class PortalTagPostModel extends Model
  15 +{
  16 + /**
  17 + * 获取指定id相关的文章id数组
  18 + * @param $post_id 文章id
  19 + * @return array 相关的文章id
  20 + */
  21 + function getRelationPostIds($post_id)
  22 + {
  23 + $tagIds = $this->where('post_id', $post_id)
  24 + ->column('tag_id');
  25 + $postIds = $this->whereIn('tag_id', $tagIds)
  26 + ->column('post_id');
  27 + return array_unique($postIds);
  28 + }
  29 +}
  1 +<?php
  2 +// +----------------------------------------------------------------------
  3 +// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4 +// +----------------------------------------------------------------------
  5 +// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
  6 +// +----------------------------------------------------------------------
  7 +// | Author: pl125 <xskjs888@163.com>
  8 +// +----------------------------------------------------------------------
  9 +namespace api\portal\model;
  10 +
  11 +use think\Model;
  12 +
  13 +class RecycleBinModel extends Model
  14 +{
  15 +
  16 +}
  1 +<?php
  2 +// +----------------------------------------------------------------------
  3 +// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4 +// +----------------------------------------------------------------------
  5 +// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
  6 +// +----------------------------------------------------------------------
  7 +// | Author: pl125 <xskjs888@163.com>
  8 +// +----------------------------------------------------------------------
  9 +
  10 +namespace api\portal\model;
  11 +use api\common\model\CommonModel;
  12 +
  13 +class UserModel extends CommonModel
  14 +{
  15 + //可查询字段
  16 +// protected $visible = [
  17 +// 'articles.id', 'user_nickname', 'avatar', 'signature','user'
  18 +// ];
  19 + //模型关联方法
  20 + protected $relationFilter = ['user'];
  21 +
  22 + /**
  23 + * 基础查询
  24 + */
  25 + protected function base($query)
  26 + {
  27 + $query->alias('user')->where('user.user_status', 1);
  28 + }
  29 +
  30 + /**
  31 + * more 自动转化
  32 + * @param $value
  33 + * @return array
  34 + */
  35 + public function getAvatarAttr($value)
  36 + {
  37 + $value = !empty($value) ? cmf_get_image_url($value) : $value;
  38 + return $value;
  39 + }
  40 +
  41 + /**
  42 + * 关联 user表
  43 + * @return $this
  44 + */
  45 + public function user()
  46 + {
  47 + return $this->belongsTo('UserModel', 'user_id')->setEagerlyType(1);
  48 + }
  49 +}
  1 +<?php
  2 +
  3 +use think\Route;
  4 +
  5 +Route::resource('portal/categories', 'portal/Categories');
  6 +Route::get('portal/categories/subCategories', 'portal/Categories/subCategories');
  7 +Route::resource('portal/articles', 'portal/Articles');
  8 +Route::resource('portal/pages', 'portal/Pages');
  9 +Route::resource('portal/userArticles', 'portal/UserArticles');
  10 +
  11 +Route::get('portal/search', 'portal/Articles/search');
  12 +Route::get('portal/articles/my', 'portal/Articles/my');
  13 +Route::post('portal/articles/doLike', 'portal/Articles/doLike');
  14 +Route::get('portal/tags/:id/articles', 'portal/Tags/articles');
  15 +Route::get('portal/tags', 'portal/Tags/index');
  16 +
  17 +Route::post('portal/userArticles/deletes', 'portal/UserArticles/deletes');
  1 +<?php
  2 +// +----------------------------------------------------------------------
  3 +// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4 +// +----------------------------------------------------------------------
  5 +// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
  6 +// +----------------------------------------------------------------------
  7 +// | Author: pl125 <xskjs888@163.com>
  8 +// +----------------------------------------------------------------------
  9 +namespace api\portal\service;
  10 +
  11 +use api\portal\model\PortalPostModel as PortalPost;
  12 +use api\portal\model\PortalCategoryModel as PortalCategory;
  13 +
  14 +class PortalPostModel extends PortalPost
  15 +{
  16 + protected $name = "portal_post";
  17 +
  18 + /**
  19 + * [recommendedList 推荐列表]
  20 + * @Author: wuwu<15093565100@163.com>
  21 + * @DateTime: 2017-07-17T11:06:47+0800
  22 + * @since: 1.0
  23 + * @param integer $next_id [最后索引值]
  24 + * @param integer $num [一页多少条 默认10]
  25 + * @return [type] [数据]
  26 + */
  27 + public static function recommendedList($next_id = 0, $num = 10)
  28 + {
  29 + $limit = "{$next_id},{$num}";
  30 + $field = 'id,recommended,user_id,post_like,post_hits,comment_count,create_time,update_time,published_time,post_title,post_excerpt,more';
  31 + $list = self::with('user')->field($field)->where('recommended', 1)->order('published_time DESC')->limit($limit)->select();
  32 + return $list;
  33 + }
  34 +
  35 + /**
  36 + * [categoryPostList 分类文章列表]
  37 + * @Author: wuwu<15093565100@163.com>
  38 + * @DateTime: 2017-07-17T15:16:26+0800
  39 + * @since: 1.0
  40 + * @param [type] $category_id [分类ID]
  41 + * @param integer $next_id [limit索引]
  42 + * @param integer $num [limit每页数量]
  43 + * @return [type] [description]
  44 + */
  45 + public static function categoryPostList($category_id, $next_id = 0, $num = 10)
  46 + {
  47 + $limit = "{$next_id},{$num}";
  48 + $Postlist = PortalCategory::categoryPostIds($category_id);
  49 + $field = 'id,recommended,user_id,post_like,post_hits,comment_count,create_time,update_time,published_time,post_title,post_excerpt,more';
  50 + $list = self::with('user')->field($field)->whereIn('id', $Postlist['PostIds'])->order('published_time DESC')->limit($limit)->select()->toJson();
  51 + return $list;
  52 + }
  53 +}
  1 +<?php
  2 +// +----------------------------------------------------------------------
  3 +// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4 +// +----------------------------------------------------------------------
  5 +// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
  6 +// +----------------------------------------------------------------------
  7 +// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8 +// +----------------------------------------------------------------------
  9 +// | Author: 小夏 < 449134904@qq.com>
  10 +// +----------------------------------------------------------------------
  11 +namespace api\portal\validate;
  12 +
  13 +use think\Validate;
  14 +
  15 +class ArticlesValidate extends Validate
  16 +{
  17 + protected $rule = [
  18 + 'post_title' => 'require',
  19 + 'post_content' => 'require',
  20 + 'categories' => 'require'
  21 + ];
  22 + protected $message = [
  23 + 'post_title.require' => '文章标题不能为空',
  24 + 'post_content.require' => '内容不能为空',
  25 + 'categories.require' => '文章分类不能为空'
  26 + ];
  27 +
  28 + protected $scene = [
  29 + 'article' => [ 'post_title' , 'post_content' , 'categories' ],
  30 + 'page' => ['post_title']
  31 + ];
  32 +}
  1 +<?php
  2 +// +----------------------------------------------------------------------
  3 +// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4 +// +----------------------------------------------------------------------
  5 +// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
  6 +// +----------------------------------------------------------------------
  7 +// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8 +// +----------------------------------------------------------------------
  9 +// | Author: 小夏 < 449134904@qq.com>
  10 +// +----------------------------------------------------------------------
  11 +namespace api\portal\validate;
  12 +
  13 +use think\Validate;
  14 +
  15 +class IntermediaryValidate extends Validate
  16 +{
  17 + protected $rule = [
  18 + 'name' => ['require','length:2,4','chs'],
  19 + 'password' => ['require','length:6,16'],
  20 + 'tel' => ['regex'=>'/13[123569]{1}\d{8}|15[1235689]\d{8}|188\d{8}/','require'],
  21 + ];
  22 + protected $message = [
  23 + 'name.require' => '姓名不能为空!',
  24 + 'name.length' => '姓名为2-4个汉字!',
  25 + 'name.chs' => '姓名只能是汉字!',
  26 + 'tel.require' => '手机号不能为空!',
  27 + 'tel.regex' => '手机号格式不正确!',
  28 + 'password.require' => '密码不能为空!',
  29 + 'password.length' => '密码长度为6-16位!',
  30 +
  31 + ];
  32 +
  33 + protected $scene = [
  34 + 'add' => ['name','tel','password'],
  35 + 'edit' => ['tel','password','name'],
  36 + ];
  37 +
  38 +}
  1 +<?php
  2 +// +----------------------------------------------------------------------
  3 +// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4 +// +----------------------------------------------------------------------
  5 +// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
  6 +// +----------------------------------------------------------------------
  7 +// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8 +// +----------------------------------------------------------------------
  9 +// | Author: 小夏 < 449134904@qq.com>
  10 +// +----------------------------------------------------------------------
  11 +namespace api\portal\validate;
  12 +
  13 +use think\Validate;
  14 +
  15 +class UserValidate extends Validate
  16 +{
  17 + protected $rule = [
  18 + 'name' => ['require','length:2,4','chs'],
  19 + 'password' => ['require','length:6,16'],
  20 + 'tel' => ['regex'=>'/13[123569]{1}\d{8}|15[1235689]\d{8}|18\d{9}/','require'],
  21 + 'sure_password' => 'require'
  22 + ];
  23 + protected $message = [
  24 + 'name.require' => '姓名不能为空!',
  25 + 'name.length' => '姓名为2-4个汉字!',
  26 + 'name.chs' => '姓名只能是汉字!',
  27 + 'tel.require' => '手机号不能为空!',
  28 + 'tel.regex' => '手机号格式不正确!',
  29 + 'password.require' => '密码不能为空!',
  30 + 'password.length' => '密码长度为6-16位!',
  31 + 'sure_password.require' => '确认密码不能为空!',
  32 +
  33 + ];
  34 +
  35 + protected $scene = [
  36 + 'add' => ['name','tel','password','sure_password'],
  37 + 'edit' => ['tel','password','name','sure_password'],
  38 + ];
  39 +
  40 +}
  1 +<?php
  2 +// +----------------------------------------------------------------------
  3 +// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4 +// +----------------------------------------------------------------------
  5 +// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
  6 +// +----------------------------------------------------------------------
  7 +// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8 +// +----------------------------------------------------------------------
  9 +// | Author: Dean <zxxjjforever@163.com>
  10 +// +----------------------------------------------------------------------
  11 +
  12 +return [
  13 + // 应用调试模式
  14 + 'app_debug' => false,
  15 + // 应用Trace
  16 + 'app_trace' => false,
  17 +
  18 +];
  1 +<?php
  2 +// +----------------------------------------------------------------------
  3 +// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4 +// +----------------------------------------------------------------------
  5 +// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
  6 +// +----------------------------------------------------------------------
  7 +// | Author: Dean <zxxjjforever@163.com>
  8 +// +----------------------------------------------------------------------
  9 +$apps = cmf_scan_dir(APP_PATH . '*', GLOB_ONLYDIR);
  10 +
  11 +foreach ($apps as $app) {
  12 + $routeFile = APP_PATH . $app . '/route.php';
  13 +
  14 + if (file_exists($routeFile)) {
  15 + include_once $routeFile;
  16 + }
  17 +
  18 +}
  19 +
  20 +
  21 +return [
  22 +];
  1 +<?php
  2 +// +----------------------------------------------------------------------
  3 +// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4 +// +----------------------------------------------------------------------
  5 +// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
  6 +// +----------------------------------------------------------------------
  7 +// | Author: Dean <zxxjjforever@163.com>
  8 +// +----------------------------------------------------------------------
  9 +
  10 +// 应用行为扩展定义文件
  11 +return [
  12 + // 应用初始化
  13 + 'app_init' => [
  14 + 'cmf\\behavior\\InitHookBehavior',
  15 + ],
  16 + // 应用开始
  17 + 'app_begin' => [
  18 + 'cmf\\behavior\\LangBehavior',
  19 + ],
  20 + // 模块初始化
  21 + 'module_init' => [],
  22 + // 操作开始执行
  23 + 'action_begin' => [],
  24 + // 视图内容过滤
  25 + 'view_filter' => [],
  26 + // 日志写入
  27 + 'log_write' => [],
  28 + // 应用结束
  29 + 'app_end' => [],
  30 +];
  1 +<?php
  2 +// +----------------------------------------------------------------------
  3 +// | 文件说明:评论
  4 +// +----------------------------------------------------------------------
  5 +// | Copyright (c) 2017 http://www.thinkcmf.com All rights reserved.
  6 +// +----------------------------------------------------------------------
  7 +// | Author: wuwu <15093565100@163.com>
  8 +// +----------------------------------------------------------------------
  9 +// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  10 +// +----------------------------------------------------------------------
  11 +// | Date: 2017-7-26
  12 +// +----------------------------------------------------------------------
  13 +namespace api\user\controller;
  14 +
  15 +use api\user\model\CommentModel as Comment;
  16 +use api\user\model\UserModel as User;
  17 +use cmf\controller\RestUserBaseController;
  18 +
  19 +class CommentsController extends RestUserBaseController
  20 +{
  21 +
  22 + /**
  23 + * [getUserComments 获取用户评论]
  24 + * @Author: wuwu<15093565100@163.com>
  25 + * @DateTime: 2017-05-25T20:48:53+0800
  26 + * @since: 1.0
  27 + * @return [array_json] [获取Comment]
  28 + */
  29 + public function getUserComments()
  30 + {
  31 + $input = $this->request->param();
  32 +
  33 + $comment = new Comment();
  34 + $map['where']['user_id'] = $this->getUserId();
  35 + $map['order'] = '-create_time';
  36 + $map['relation'] = 'user,to_user';
  37 + if (!empty($input['page'])) {
  38 + $map['page'] = $input['page'];
  39 + }
  40 + //处理不同的情况
  41 + $data = $comment->getDatas($map);
  42 + $this->success('请求成功', $data);
  43 +
  44 + }
  45 +
  46 + /**
  47 + * [getComments 获取评论]
  48 + * @Author: wuwu<15093565100@163.com>
  49 + * @DateTime: 2017-05-25T20:48:53+0800
  50 + * @since: 1.0
  51 + * @return [array_json] [获取Comment]
  52 + */
  53 + public function getComments()
  54 + {
  55 + $input = $this->request->param();
  56 + $id = $this->request->has('object_id') ? $input['object_id'] : $this->error('id参数不存在');
  57 + $table = $this->request->has('table_name') ? $input['table_name'] : $this->error('table参数不存在');
  58 + $comment = new Comment();
  59 + $map['where'] = [
  60 + 'object_id' => $id,
  61 + 'table_name' => $table
  62 + ];
  63 + $map['relation'] = 'user,to_user';
  64 +
  65 + if (!empty($input['page'])) {
  66 + $map['page'] = $input['page'];
  67 + }
  68 +
  69 + $data = $comment->getDatas($map);
  70 + //数据是否存在
  71 + if ($data->isEmpty()) {
  72 + $this->error('评论数据为空');
  73 + } else {
  74 + $this->success('评论获取成功!', $data);
  75 + }
  76 + }
  77 +
  78 + /**
  79 + * [delComments 删除评论]
  80 + * @Author: wuwu<15093565100@163.com>
  81 + * @DateTime: 2017-08-11T22:08:56+0800
  82 + * @since: 1.0
  83 + * @return
  84 + */
  85 + public function delComments()
  86 + {
  87 + $input = $this->request->param();
  88 + $id = $this->request->has('id') ? intval($input['id']) : $this->error('id参数不存在');
  89 + $userId = $this->getUserId();
  90 + Comment::destroy(['id' => $id, 'user_id' => $userId]);
  91 +
  92 + $this->success('删除成功');
  93 + }
  94 +
  95 + /**
  96 + * [setComments 添加评论]
  97 + * @Author: wuwu<15093565100@163.com>
  98 + * @DateTime: 2017-08-16T01:07:44+0800
  99 + * @since: 1.0
  100 + */
  101 + public function setComments()
  102 + {
  103 + $data = $this->_setComments();
  104 + if ($res = Comment::setComment($data)) {
  105 + $this->success('评论成功', $res);
  106 + } else {
  107 + $this->error('评论失败');
  108 + }
  109 + }
  110 +
  111 + /**
  112 + * [_setComments 评论数据组织]
  113 + * @Author: wuwu<15093565100@163.com>
  114 + * @DateTime: 2017-08-16T01:00:02+0800
  115 + * @since: 1.0
  116 + */
  117 + protected function _setComments()
  118 + {
  119 + $input = $this->request->param();
  120 + $data['object_id'] = $this->request->has('object_id') ? $input['object_id'] : $this->error('object_id参数不存在');
  121 + $data['table_name'] = $this->request->has('table_name') ? $input['table_name'] : $this->error('table_name参数不存在');
  122 + $data['url'] = $this->request->has('url') ? $input['url'] : $this->error('url参数不存在');
  123 + $data['content'] = $this->request->has('content') ? $input['content'] : $this->error('内容不为空');
  124 + $data['parent_id'] = $this->request->has('parent_id') ? $input['parent_id'] : 0;
  125 + $result = $this->validate($data,
  126 + [
  127 + 'object_id' => 'require|number',
  128 + 'content' => 'require',
  129 + ]);
  130 + if (true !== $result) {
  131 + // 验证失败 输出错误信息
  132 + $this->error($result);
  133 + }
  134 + $data['delete_time'] = 0;
  135 + $data['create_time'] = time();
  136 + if ($data['parent_id']) {
  137 + $res = Comment::field(['parent_id', 'path', 'user_id'])->find($data['parent_id']);
  138 + if ($res) {
  139 + $data['path'] = $res['path'] . $data['parent_id'] . ',';
  140 + $data['to_user_id'] = $res['user_id'];
  141 + } else {
  142 + $this->error('回复的评论不存在');
  143 + }
  144 + } else {
  145 + $data['path'] = '0,';
  146 + }
  147 + $data['user_id'] = $this->getUserId();
  148 + $userData = User::field(true)->find($data['user_id']);
  149 + if (!$userData) {
  150 + $this->error('评论用户不存在');
  151 + }
  152 +
  153 + $data['full_name'] = $userData['user_nickname'];
  154 + $data['email'] = $userData['user_email'];
  155 + return $data;
  156 + }
  157 +}
  1 +<?php
  2 +// +----------------------------------------------------------------------
  3 +// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4 +// +----------------------------------------------------------------------
  5 +// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
  6 +// +----------------------------------------------------------------------
  7 +// | Author: pl125 <xskjs888@163.com>
  8 +// +----------------------------------------------------------------------
  9 +
  10 +namespace api\user\controller;
  11 +
  12 +use api\user\model\UserFavoriteModel;
  13 +use cmf\controller\RestUserBaseController;
  14 +
  15 +class FavoritesController extends RestUserBaseController
  16 +{
  17 + protected $userFavoriteModel;
  18 +
  19 + public function __construct(UserFavoriteModel $userFavoriteModel)
  20 + {
  21 + parent::__construct();
  22 + $this->userFavoriteModel = $userFavoriteModel;
  23 + }
  24 +
  25 + /**
  26 + * 显示收藏列表
  27 + */
  28 + public function getFavorites()
  29 + {
  30 + $userId = $this->getUserId();
  31 +
  32 + $param = $this->request->param();
  33 + $param['where'] = [
  34 + 'user_id' => $userId
  35 + ];
  36 + $param['order'] = '-create_time';
  37 +
  38 + $favoriteData = $this->userFavoriteModel->getDatas($param);
  39 + $this->success('请求成功', $favoriteData);
  40 + }
  41 +
  42 + /**
  43 + * [setFavorites 添加收藏]
  44 + * @Author: wuwu<15093565100@163.com>
  45 + * @DateTime: 2017-08-03T09:03:40+0800
  46 + * @since: 1.0
  47 + */
  48 + public function setFavorites()
  49 + {
  50 + $input = $this->request->param();
  51 +
  52 + //组装数据
  53 + $data = $this->_FavoritesObject($input['title'], $input['url'], $input['description'], $input['table_name'], $input['object_id']);
  54 + if (!$data) {
  55 + $this->error('收藏失败');
  56 + }
  57 + if ($this->userFavoriteModel->where(['user_id' => $this->getUserId(), 'object_id' => $input['object_id']])->where('table_name', $input['table_name'])->count() > 0) {
  58 + $this->error('已收藏');
  59 + }
  60 + if ($this->userFavoriteModel->setFavorite($data)) {
  61 + $this->success('收藏成功');
  62 + } else {
  63 + $this->error('收藏失败');
  64 + }
  65 +
  66 + }
  67 +
  68 + /**
  69 + * [_FavoritesObject 收藏数据组装]
  70 + * @Author: wuwu<15093565100@163.com>
  71 + * @DateTime: 2017-08-03T09:39:06+0800
  72 + * @since: 1.0
  73 + * @return [type] [description]
  74 + */
  75 + protected function _FavoritesObject($title, $url, $description, $table_name, $object_id)
  76 + {
  77 + $data['user_id'] = $this->getUserId();
  78 + $data['create_time'] = THINK_START_TIME;
  79 +
  80 + if (empty($title)) {
  81 + return false;
  82 + } else if (empty($url)) {
  83 + return false;
  84 + } elseif (empty($table_name)) {
  85 + return false;
  86 + } elseif (empty($object_id)) {
  87 + return false;
  88 + }
  89 + $data['title'] = $title;
  90 + $data['url'] = htmlspecialchars_decode($url);
  91 + $data['description'] = $description;
  92 + $data['table_name'] = $table_name;
  93 + $data['object_id'] = $object_id;
  94 + return $data;
  95 + }
  96 +
  97 + /**
  98 + * [unsetFavorites 取消收藏]
  99 + * @Author: wuwu<15093565100@163.com>
  100 + * @DateTime: 2017-08-03T09:04:31+0800
  101 + * @since: 1.0
  102 + * @return [type] [description]
  103 + */
  104 + public function unsetFavorites()
  105 + {
  106 + $id = $this->request->param('id', 0, 'intval');
  107 + $userId = $this->getUserId();
  108 +
  109 + $count = $this->userFavoriteModel->where(['id' => $id, 'user_id' => $userId])->count();
  110 +
  111 + if ($count == 0) {
  112 + $this->error('收藏不存在,无法取消');
  113 + }
  114 +
  115 + $this->userFavoriteModel->where(['id' => $id])->delete();
  116 +
  117 + $this->success('取消成功');
  118 +
  119 + }
  120 +}
  1 +<?php
  2 +// +----------------------------------------------------------------------
  3 +// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4 +// +----------------------------------------------------------------------
  5 +// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
  6 +// +----------------------------------------------------------------------
  7 +// | Author: Dean <zxxjjforever@163.com>
  8 +// +----------------------------------------------------------------------
  9 +namespace api\user\controller;
  10 +
  11 +use cmf\controller\RestUserBaseController;
  12 +use think\Db;
  13 +use think\Validate;
  14 +
  15 +class ProfileController extends RestUserBaseController
  16 +{
  17 + // 用户密码修改
  18 + public function changePassword()
  19 + {
  20 + $validate = new Validate([
  21 + 'old_password' => 'require',
  22 + 'password' => 'require',
  23 + 'confirm_password' => 'require|confirm:password'
  24 + ]);
  25 +
  26 + $validate->message([
  27 + 'old_password.require' => '请输入您的旧密码!',
  28 + 'password.require' => '请输入您的新密码!',
  29 + 'confirm_password.require' => '请输入确认密码!',
  30 + 'confirm_password.confirm' => '两次输入的密码不一致!'
  31 + ]);
  32 +
  33 + $data = $this->request->param();
  34 + if (!$validate->check($data)) {
  35 + $this->error($validate->getError());
  36 + }
  37 +
  38 + $userId = $this->getUserId();
  39 + $userPassword = Db::name("user")->where('id', $userId)->value('user_pass');
  40 +
  41 + if (!cmf_compare_password($data['old_password'], $userPassword)) {
  42 + $this->error('旧密码不正确!');
  43 + }
  44 +
  45 + Db::name("user")->where('id', $userId)->update(['user_pass' => cmf_password($data['password'])]);
  46 +
  47 + $this->success("密码修改成功!");
  48 +
  49 + }
  50 +
  51 + // 用户绑定邮箱
  52 + public function bindingEmail()
  53 + {
  54 + $validate = new Validate([
  55 + 'email' => 'require|email|unique:user,user_email',
  56 + 'verification_code' => 'require'
  57 + ]);
  58 +
  59 + $validate->message([
  60 + 'email.require' => '请输入您的邮箱!',
  61 + 'email.email' => '请输入正确的邮箱格式!',
  62 + 'email.unique' => '正确账号已存在!',
  63 + 'verification_code.require' => '请输入数字验证码!'
  64 + ]);
  65 +
  66 + $data = $this->request->param();
  67 + if (!$validate->check($data)) {
  68 + $this->error($validate->getError());
  69 + }
  70 +
  71 + $userId = $this->getUserId();
  72 + $userEmail = Db::name("user")->where('id', $userId)->value('user_email');
  73 +
  74 + if (!empty($userEmail)) {
  75 + $this->error("您已经绑定邮箱!");
  76 + }
  77 +
  78 + $errMsg = cmf_check_verification_code($data['email'], $data['verification_code']);
  79 + if (!empty($errMsg)) {
  80 + $this->error($errMsg);
  81 + }
  82 +
  83 + Db::name("user")->where('id', $userId)->update(['user_email' => $data['email']]);
  84 +
  85 + $this->success("绑定成功!");
  86 + }
  87 +
  88 + // 用户绑定手机号
  89 + public function bindingMobile()
  90 + {
  91 + $validate = new Validate([
  92 + 'mobile' => 'require|unique:user,mobile',
  93 + 'verification_code' => 'require'
  94 + ]);
  95 +
  96 + $validate->message([
  97 + 'mobile.require' => '请输入您的手机号!',
  98 + 'mobile.unique' => '手机号已经存在!',
  99 + 'verification_code.require' => '请输入数字验证码!'
  100 + ]);
  101 +
  102 + $data = $this->request->param();
  103 + if (!$validate->check($data)) {
  104 + $this->error($validate->getError());
  105 + }
  106 +
  107 + if (!preg_match('/(^(13\d|15[^4\D]|17[013678]|18\d)\d{8})$/', $data['mobile'])) {
  108 + $this->error("请输入正确的手机格式!");
  109 + }
  110 +
  111 +
  112 + $userId = $this->getUserId();
  113 + $mobile = Db::name("user")->where('id', $userId)->value('mobile');
  114 +
  115 + if (!empty($mobile)) {
  116 + $this->error("您已经绑定手机!");
  117 + }
  118 +
  119 + $errMsg = cmf_check_verification_code($data['mobile'], $data['verification_code']);
  120 + if (!empty($errMsg)) {
  121 + $this->error($errMsg);
  122 + }
  123 +
  124 + Db::name("user")->where('id', $userId)->update(['mobile' => $data['mobile']]);
  125 +
  126 + $this->success("绑定成功!");
  127 + }
  128 +
  129 + /**
  130 + * 用户基本信息获取及修改
  131 + * @param 请求为GET 获取信息
  132 + * @param [string] $[field] [要获取的一个或多个字段名] 可选
  133 + * @return 带参数,返回某个或多个字段信息。不带参数,返回所有信息
  134 + * @param 请求为POST 修改信息
  135 + */
  136 + public function userInfo($field = '')
  137 + {
  138 + //判断请求为GET,获取信息
  139 + if ($this->request->isGet()) {
  140 + $userId = $this->getUserId();
  141 + $fieldStr = 'user_type,user_login,mobile,user_email,user_nickname,avatar,signature,user_url,sex,birthday,score,coin,user_status,user_activation_key,create_time,last_login_time,last_login_ip';
  142 + if (empty($field)) {
  143 + $userData = Db::name("user")->field($fieldStr)->find($userId);
  144 + } else {
  145 + $fieldArr = explode(',', $fieldStr);
  146 + $postFieldArr = explode(',', $field);
  147 + $mixedField = array_intersect($fieldArr, $postFieldArr);
  148 + if (empty($mixedField)) {
  149 + $this->error('您查询的信息不存在!');
  150 + }
  151 + if (count($mixedField) > 1) {
  152 + $fieldStr = implode(',', $mixedField);
  153 + $userData = Db::name("user")->field($fieldStr)->find($userId);
  154 + } else {
  155 + $userData = Db::name("user")->where('id', $userId)->value($mixedField);
  156 + }
  157 + }
  158 + $this->success('获取成功!', $userData);
  159 + }
  160 + //判断请求为POST,修改信息
  161 + if ($this->request->isPost()) {
  162 + $userId = $this->getUserId();
  163 + $fieldStr = 'user_nickname,avatar,signature,user_url,sex,birthday';
  164 + $data = $this->request->post();
  165 + if (empty($data)) {
  166 + $this->error('修改失败,提交表单为空!');
  167 + }
  168 +
  169 + if (!empty($data['birthday'])) {
  170 + $data['birthday'] = strtotime($data['birthday']);
  171 + }
  172 +
  173 + $upData = Db::name("user")->where('id', $userId)->field($fieldStr)->update($data);
  174 + if ($upData !== false) {
  175 + $this->success('修改成功!');
  176 + } else {
  177 + $this->error('修改失败!');
  178 + }
  179 + }
  180 + }
  181 +
  182 +}