作者 anyv
1 个管道 的构建 失败 耗费 0 秒

澳德

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

要显示太多修改。

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

  1 +.buildpath
  2 +.DS_Store
  3 +.project
  4 +.settings
  5 +.idea
  6 +composer.lock
  1 +# 定义 stages
  2 +stages:
  3 + - pull
  4 +
  5 +# 定义 job
  6 +job1:
  7 + stage: pull
  8 + script:
  9 + - cd /alidata/www/w/aode
  10 + - git pull
  1 +如何贡献我的源代码
  2 +===
  1 +bronet遵循LGPL开源协议发布,并提供免费使用。
  1 +ThinkBRO 1.0
  2 +===============
  3 +以客户为中心 以奋斗者为本
  4 +
  5 +### 环境推荐
  6 +> php5.5+
  7 +
  8 +> mysql 5.6+
  9 +
  10 +> 打开rewrite
  11 +
  12 +
  13 +### 最低环境要求
  14 +> php5.4+
  15 +
  16 +> mysql 5.5+ (mysql5.1安装时选择utf8编码,不支持表情符)
  17 +
  18 +> 打开rewrite
  19 +
  20 +
  21 +### 自动安装
  22 +> 之前安装过的同学,请手动创建`data/install.lock`文件
  23 +
  24 +1. public目录做为网站根目录,入口文件在 public/index.php
  25 +2. 配置好网站,请访问http://你的域名
  26 +
  27 +### 完整版目录结构
  28 +```
  29 +bronet 根目录
  30 +├─api api目录(核心版不带)
  31 +├─app 应用目录
  32 +│ ├─portal 门户应用目录
  33 +│ │ ├─config.php 应用配置文件
  34 +│ │ ├─common.php 模块函数文件
  35 +│ │ ├─controller 控制器目录
  36 +│ │ ├─model 模型目录
  37 +│ │ └─ ... 更多类库目录
  38 +│ ├─extra 微信配置目录
  39 +│ │ ├─wechat.php 微信配置文件
  40 +│ ├─ ... 更多应用
  41 +│ ├─command.php 命令行工具配置文件
  42 +│ ├─common.php 应用公共(函数)文件
  43 +│ ├─config.php 应用(公共)配置文件
  44 +│ ├─database.php 数据库配置文件
  45 +│ ├─tags.php 应用行为扩展定义文件
  46 +│ └─route.php 路由配置文件
  47 +├─data 数据目录
  48 +│ ├─conf 动态配置目录
  49 +│ ├─runtime 应用的运行时目录(可写)
  50 +│ └─ ... 更多
  51 +├─public WEB 部署目录(对外访问目录)
  52 +│ ├─api api入口目录(核心版不带)
  53 +│ ├─plugins 插件目录
  54 +│ ├─static 静态资源存放目录(css,js,image)
  55 +│ ├─themes 前后台主题目录
  56 +│ │ ├─admin_simpleboot3 后台默认主题
  57 +│ │ └─simpleboot3 前台默认主题
  58 +│ ├─upload 文件上传目录
  59 +│ ├─index.php 入口文件
  60 +│ ├─robots.txt 爬虫协议文件
  61 +│ ├─router.php 快速测试文件
  62 +│ └─.htaccess apache重写文件
  63 +├─simplewind
  64 +│ ├─cmf CMF核心库目录
  65 +│ ├─extend 扩展类库目录
  66 +│ ├─thinkphp thinkphp目录
  67 +│ └─vendor 第三方类库目录(Composer)
  68 +├─composer.json composer 定义文件
  69 +├─LICENSE.txt 授权说明文件
  70 +├─README.md README 文件
  71 +├─think 命令行入口文件
  72 +```
  73 +
  74 +### 开发手册
  75 +http://www.kancloud.cn/thinkcmf/doc
  76 +
  77 +
  78 +### 贴别注意
  79 +
  80 +>本环境下已经引入微信SDK(EasyWechat)
  81 +
  82 +EasyWechat 使用手册:https://www.easywechat.com/docs/3.x
  83 +
  84 +微信开发所需要的功能一般情况都涵盖了,以下是封装的公众号开发常用的功能
  85 +
  86 +1. 微信公众号授权已经封装好,只要调用WeChatBaseController下的checkWeChatUserLogin()即可
  87 +
  88 +2. 微信授权回调地址 /user/index/callback
  89 +
  90 +3. 微信服务器配置接口 /Portal/Wechat/index
  91 +
  92 +4. 在user/PayController下整理了微信支付,支付回调,退款,企业支付,红包,订单查询等DEMO
  93 +
  94 +5. 小程序登录注册 Api目录下 /Wxapp/Public/login 需要先用code换取sessionKey和openid
  95 +
  96 +### 更多内容
  97 +
  98 +> 关于更多微信相关操作多多查看使用手册
  99 +
  100 +> 各位同学可参考使用,更多内容还再持续更新中...
  101 +
  102 +> 各位同学有什么好的想法、意见请及时反馈!
  103 +
  1 +<?php
  2 +// +----------------------------------------------------------------------
  3 +// | bronet [ 以客户为中心 以奋斗者为本 ]
  4 +// +----------------------------------------------------------------------
  5 +// | Copyright (c) 2013-2017 http://www.bronet.cn All rights reserved.
  6 +// +----------------------------------------------------------------------
  7 +namespace api\admin\controller;
  8 +
  9 +use cmf\controller\RestBaseController;
  10 +use think\Db;
  11 +use think\Validate;
  12 +
  13 +class PublicController extends RestBaseController
  14 +{
  15 +
  16 + // 用户登录 TODO 增加最后登录信息记录,如 ip
  17 + public function login()
  18 + {
  19 + $validate = new Validate([
  20 + 'username' => 'require',
  21 + 'password' => 'require'
  22 + ]);
  23 + $validate->message([
  24 + 'username.require' => '请输入手机号,邮箱或用户名!',
  25 + 'password.require' => '请输入您的密码!'
  26 + ]);
  27 +
  28 + $data = $this->request->param();
  29 + if (!$validate->check($data)) {
  30 + $this->error($validate->getError());
  31 + }
  32 +
  33 + $userQuery = Db::name("user");
  34 + if (Validate::is($data['username'], 'email')) {
  35 + $userQuery = $userQuery->where('user_email', $data['username']);
  36 + } else if (preg_match('/(^(13\d|15[^4\D]|17[13678]|18\d)\d{8}|170[^346\D]\d{7})$/', $data['username'])) {
  37 + $userQuery = $userQuery->where('mobile', $data['username']);
  38 + } else {
  39 + $userQuery = $userQuery->where('user_login', $data['username']);
  40 + }
  41 +
  42 + $findUser = $userQuery->find();
  43 +
  44 + if (empty($findUser)) {
  45 + $this->error("用户不存在!");
  46 + } else {
  47 +
  48 + switch ($findUser['user_status']) {
  49 + case 0:
  50 + $this->error('您已被拉黑!');
  51 + case 2:
  52 + $this->error('账户还没有验证成功!');
  53 + }
  54 +
  55 + if (!cmf_compare_password($data['password'], $findUser['user_pass'])) {
  56 + $this->error("密码不正确!");
  57 + }
  58 + }
  59 +
  60 + $allowedDeviceTypes = ['mobile', 'android', 'iphone', 'ipad', 'web', 'pc', 'mac'];
  61 +
  62 + if (empty($data['device_type']) || !in_array($data['device_type'], $allowedDeviceTypes)) {
  63 + $this->error("请求错误,未知设备!");
  64 + }
  65 +
  66 + $userTokenQuery = Db::name("user_token")
  67 + ->where('user_id', $findUser['id'])
  68 + ->where('device_type', $data['device_type']);
  69 + $findUserToken = $userTokenQuery->find();
  70 + $currentTime = time();
  71 + $expireTime = $currentTime + 24 * 3600 * 180;
  72 + $token = md5(uniqid()) . md5(uniqid());
  73 + if (empty($findUserToken)) {
  74 + $result = $userTokenQuery->insert([
  75 + 'token' => $token,
  76 + 'user_id' => $findUser['id'],
  77 + 'expire_time' => $expireTime,
  78 + 'create_time' => $currentTime,
  79 + 'device_type' => $data['device_type']
  80 + ]);
  81 + } else {
  82 + $result = $userTokenQuery
  83 + ->where('user_id', $findUser['id'])
  84 + ->where('device_type', $data['device_type'])
  85 + ->update([
  86 + 'token' => $token,
  87 + 'expire_time' => $expireTime,
  88 + 'create_time' => $currentTime
  89 + ]);
  90 + }
  91 +
  92 +
  93 + if (empty($result)) {
  94 + $this->error("登录失败!");
  95 + }
  96 +
  97 + $this->success("登录成功!", ['token' => $token]);
  98 + }
  99 +
  100 + // 管理员退出
  101 + public function logout()
  102 + {
  103 + $userId = $this->getUserId();
  104 + Db::name('user_token')->where([
  105 + 'token' => $this->token,
  106 + 'user_id' => $userId,
  107 + 'device_type' => $this->deviceType
  108 + ])->update(['token' => '']);
  109 +
  110 + $this->success("退出成功!");
  111 +
  112 + $this->success("退出成功!");
  113 + }
  114 +
  115 +}
  1 +<?php
  2 +// +----------------------------------------------------------------------
  3 +// | bronet [ 以客户为中心 以奋斗者为本 ]
  4 +// +----------------------------------------------------------------------
  5 +// | Copyright (c) 2013-2017 http://www.bronet.cn All rights reserved.
  6 +// +----------------------------------------------------------------------
  7 +
  8 +$apps = cmf_scan_dir(APP_PATH . '*', GLOB_ONLYDIR);
  9 +
  10 +$returnCommands = [];
  11 +
  12 +foreach ($apps as $app) {
  13 + $commandFile = APP_PATH . $app . '/command.php';
  14 +
  15 + if (file_exists($commandFile)) {
  16 + $commands = include $commandFile;
  17 +
  18 + $returnCommands = array_merge($returnCommands, $commands);
  19 + }
  20 +
  21 +}
  22 +
  23 +return $returnCommands;
  1 +<?php
  2 +// +----------------------------------------------------------------------
  3 +// | bronet [ 以客户为中心 以奋斗者为本 ]
  4 +// +----------------------------------------------------------------------
  5 +// | Copyright (c) 2013-2017 http://www.bronet.cn All rights reserved.
  6 +// +----------------------------------------------------------------------
  7 +namespace api\common\model;
  8 +
  9 +use think\Model;
  10 +use think\Loader;
  11 +
  12 +class CommonModel extends Model
  13 +{
  14 + // 关联模型过滤
  15 + protected $relationFilter = [];
  16 +
  17 + /**
  18 + * @access public
  19 + * @param array $params 过滤参数
  20 + * @return array|collection 查询结果
  21 + */
  22 + public function getDatas($params = [])
  23 + {
  24 + if (empty($params)) {
  25 + return $this->select();
  26 + }
  27 +
  28 + $this->setCondition($params);
  29 + if (!empty($params['id'])) {
  30 + $datas = $this->find();
  31 + } else {
  32 + $datas = $this->select();
  33 + }
  34 +
  35 + if (!empty($params['relation'])) {
  36 + $allowedRelations = $this->allowedRelations($params['relation']);
  37 + if (!empty($allowedRelations)) {
  38 + if (!empty($params['id'])) {
  39 + if (!empty($datas)) {
  40 + $datas->append($allowedRelations);
  41 + }
  42 + } else {
  43 + if (count($datas) > 0) {
  44 + $datas->load($allowedRelations);
  45 + $datas->append($allowedRelations);
  46 + }
  47 + }
  48 + }
  49 + }
  50 +
  51 + return $datas;
  52 + }
  53 +
  54 + /**
  55 + * @access public
  56 + * @param array $params 过滤参数
  57 + * @return $this
  58 + */
  59 + public function setCondition($params)
  60 + {
  61 + if (empty($params)) {
  62 + return $this;
  63 + }
  64 + if (!empty($params['relation'])) {
  65 + $allowedRelations = $this->allowedRelations($params['relation']);
  66 + if (!empty($allowedRelations)) {
  67 + if (!empty($params['id']) && count($allowedRelations) == 1) {
  68 + $this->paramsFilter($params);
  69 + } else {
  70 + $this->paramsFilter($params);//->with($allowedRelations);
  71 + }
  72 + }
  73 + } else {
  74 + $this->paramsFilter($params);
  75 + }
  76 + return $this;
  77 + }
  78 +
  79 + /**
  80 + * @access public
  81 + * @param array $params 过滤参数
  82 + * @param model $model 关联模型
  83 + * @return model|array $this|链式查询条件数组
  84 + */
  85 + public function paramsFilter($params, $model = null)
  86 + {
  87 + if (!empty($model)) {
  88 + $_this = $model;
  89 + } else {
  90 + $_this = $this;
  91 + }
  92 +
  93 + if (isset($_this->visible)) {
  94 + $whiteParams = $_this->visible;
  95 + }
  96 +
  97 + // 设置field字段过滤
  98 + if (!empty($params['field'])) {
  99 + $filterParams = $this->strToArr($params['field']);
  100 + if (!empty($whiteParams)) {
  101 + $mixedField = array_intersect($filterParams, $whiteParams);
  102 + } else {
  103 + $mixedField = $filterParams;
  104 + }
  105 +
  106 + if (!empty($mixedField)) {
  107 + $_this->field($mixedField);
  108 + }
  109 + }
  110 +
  111 + // 设置id,ids
  112 + if (!empty($params['ids'])) {
  113 + $ids = $this->strToArr($params['ids']);
  114 + foreach ($ids as $key => $value) {
  115 + $ids[$key] = intval($value);
  116 + }
  117 + }
  118 +
  119 + if (!empty($params['id'])) {
  120 + $id = intval($params['id']);
  121 + if (!empty($id)) {
  122 + return $_this->where('id', $id);
  123 + }
  124 + } elseif (!empty($ids)) {
  125 + $_this->where('id', 'in', $ids);
  126 + }
  127 +
  128 + if (!empty($params['where'])) {
  129 + if (empty($model)) {
  130 + $_this->where($params['where']);
  131 + }
  132 + }
  133 +
  134 +
  135 + // 设置分页
  136 + if (!empty($params['page'])) {
  137 + $pageArr = $this->strToArr($params['page']);
  138 + $page = [];
  139 + foreach ($pageArr as $value) {
  140 + $page[] = intval($value);
  141 + }
  142 + if (count($page) == 1) {
  143 + $_this->page($page[0]);
  144 + } elseif (count($page) == 2) {
  145 + $_this->page($page[0], $page[1]);
  146 + }
  147 + } elseif (!empty($params['limit'])) { // 设置limit查询
  148 + $limitArr = $this->strToArr($params['limit']);
  149 + $limit = [];
  150 + foreach ($limitArr as $value) {
  151 + $limit[] = intval($value);
  152 + }
  153 + if (count($limit) == 1) {
  154 + $_this->limit($limit[0]);
  155 + } elseif (count($limit) == 2) {
  156 + $_this->limit($limit[0], $limit[1]);
  157 + }
  158 + } else {
  159 + $_this->limit(10);
  160 + }
  161 +
  162 + //设置排序
  163 + if (!empty($params['order'])) {
  164 + $order = $this->strToArr($params['order']);
  165 + foreach ($order as $key => $value) {
  166 + $upDwn = substr($value, 0, 1);
  167 + $orderType = $upDwn == '-' ? 'desc' : 'asc';
  168 + $orderField = substr($value, 1);
  169 + if (!empty($whiteParams)) {
  170 + if (in_array($orderField, $whiteParams)) {
  171 + $orderWhere[$orderField] = $orderType;
  172 + }
  173 + } else {
  174 + $orderWhere[$orderField] = $orderType;
  175 + }
  176 + }
  177 +
  178 + if (!empty($orderWhere)) {
  179 + $_this->order($orderWhere);
  180 + }
  181 + }
  182 +
  183 + return $_this;
  184 + }
  185 +
  186 + /**
  187 + * 设置链式查询
  188 + * @access public
  189 + * @param array $params 链式查询条件
  190 + * @param model $model 模型
  191 + * @return $this
  192 + */
  193 + public function setParamsQuery($params, $model = null)
  194 + {
  195 + if (!empty($model)) {
  196 + $_this = $model;
  197 + } else {
  198 + $_this = $this;
  199 + }
  200 + $_this->alias('articles');
  201 + if (!empty($params['field'])) {
  202 + $_this->field($params['field']);
  203 + }
  204 + if (!empty($params['ids'])) {
  205 + $_this->where('articles.id', $params['ids'][1], $params['ids'][2]);
  206 + }
  207 + if (!empty($params['limit'])) {
  208 + $_this->limit($params['limit']);
  209 + }
  210 + if (!empty($params['page'])) {
  211 + $_this->page($params['page']);
  212 + }
  213 + if (!empty($params['order'])) {
  214 + $_this->order($params['order']);
  215 + }
  216 + return $_this;
  217 + }
  218 +
  219 + public function allowedRelations($relations)
  220 + {
  221 + if (is_string($relations)) {
  222 + $relations = explode(',', $relations);
  223 + }
  224 +
  225 + if (!is_array($relations)) {
  226 + return false;
  227 + }
  228 +
  229 + return array_intersect($this->relationFilter, $relations);
  230 + }
  231 +
  232 + /**
  233 + * 是否允许关联
  234 + * @access public
  235 + * @param string $relationName 模型关联方法名
  236 + * @return boolean
  237 + */
  238 + public function isWhite($relationName)
  239 + {
  240 + if (!is_string($relationName)) {
  241 + return false;
  242 + }
  243 + $name = Loader::parseName($relationName, 1, false);
  244 + if (in_array($name, $this->relationFilter)) {
  245 + return true;
  246 + } else {
  247 + return false;
  248 + }
  249 + }
  250 +
  251 + /**
  252 + * 懒人函数
  253 + * @access public
  254 + * @param string $value 字符串
  255 + * @return array
  256 + */
  257 + public function strToArr($string)
  258 + {
  259 + return is_string($string) ? explode(',', $string) : $string;
  260 + }
  261 +}
  1 +<?php
  2 +// +----------------------------------------------------------------------
  3 +// | bronet [ 以客户为中心 以奋斗者为本 ]
  4 +// +----------------------------------------------------------------------
  5 +// | Copyright (c) 2013-2017 http://www.bronet.cn All rights reserved.
  6 +// +----------------------------------------------------------------------
  7 +
  8 +return [
  9 + // +----------------------------------------------------------------------
  10 + // | 应用设置
  11 + // +----------------------------------------------------------------------
  12 +
  13 + // 应用命名空间
  14 + 'app_namespace' => 'api',
  15 + // 应用模式状态
  16 + 'app_status' => APP_DEBUG ? 'debug' : 'release',
  17 + // 是否支持多模块
  18 + 'app_multi_module' => true,
  19 + // 入口自动绑定模块
  20 + 'auto_bind_module' => false,
  21 + // 注册的根命名空间
  22 + 'root_namespace' => ['cmf' => CMF_PATH, 'plugins' => PLUGINS_PATH, 'app' => CMF_PATH . 'app/'],
  23 + // 扩展函数文件
  24 + 'extra_file_list' => [THINK_PATH . 'helper' . EXT, CMF_PATH . 'common' . EXT],
  25 + // 默认输出类型
  26 + 'default_return_type' => 'json',
  27 + // 默认AJAX 数据返回格式,可选json xml ...
  28 + 'default_ajax_return' => 'json',
  29 + // 默认JSONP格式返回的处理方法
  30 + 'default_jsonp_handler' => 'jsonpReturn',
  31 + // 默认JSONP处理方法
  32 + 'var_jsonp_handler' => 'callback',
  33 + // 默认时区
  34 + 'default_timezone' => 'PRC',
  35 + // 是否开启多语言
  36 + 'lang_switch_on' => false,
  37 + // 默认全局过滤方法 用逗号分隔多个
  38 + 'default_filter' => 'htmlspecialchars',
  39 + // 默认语言
  40 + 'default_lang' => 'zh-cn',
  41 + // 应用类库后缀
  42 + 'class_suffix' => true,
  43 + // 控制器类后缀
  44 + 'controller_suffix' => true,
  45 +
  46 + // +----------------------------------------------------------------------
  47 + // | 模块设置
  48 + // +----------------------------------------------------------------------
  49 +
  50 + // 默认模块名
  51 + 'default_module' => 'home',
  52 + // 禁止访问模块
  53 + 'deny_module_list' => ['common'],
  54 + // 默认控制器名
  55 + 'default_controller' => 'Index',
  56 + // 默认操作名
  57 + 'default_action' => 'index',
  58 + // 默认验证器
  59 + 'default_validate' => '',
  60 + // 默认的空控制器名
  61 + 'empty_controller' => 'Error',
  62 + // 自动搜索控制器
  63 + 'controller_auto_search' => false,
  64 +
  65 + // +----------------------------------------------------------------------
  66 + // | URL设置
  67 + // +----------------------------------------------------------------------
  68 +
  69 + 'pathinfo_depr' => '/',
  70 + // URL伪静态后缀
  71 + 'url_html_suffix' => 'html',
  72 + // URL普通方式参数 用于自动生成
  73 + 'url_common_param' => false,
  74 + // URL参数方式 0 按名称成对解析 1 按顺序解析
  75 + 'url_param_type' => 0,
  76 + // 是否开启路由
  77 + 'url_route_on' => true,
  78 + // 路由配置文件(支持配置多个)
  79 + 'route_config_file' => ['route'],
  80 + // 是否强制使用路由
  81 + 'url_route_must' => false,
  82 + // 域名部署
  83 + 'url_domain_deploy' => false,
  84 + // 域名根,如thinkphp.cn
  85 + 'url_domain_root' => '',
  86 + // 是否自动转换URL中的控制器和操作名
  87 + 'url_convert' => true,
  88 + // 默认的访问控制器层
  89 + 'url_controller_layer' => 'controller',
  90 + // 表单请求类型伪装变量
  91 + 'var_method' => '_method',
  92 +
  93 + // +----------------------------------------------------------------------
  94 + // | 模板设置
  95 + // +----------------------------------------------------------------------
  96 +
  97 + 'template' => [
  98 + // 模板引擎类型 支持 php think 支持扩展
  99 + 'type' => 'Think',
  100 + // 视图根目录
  101 + 'view_base' => '',
  102 + // 模板路径
  103 + 'view_path' => '',
  104 + // 模板后缀
  105 + 'view_suffix' => 'html',
  106 + // 模板文件名分隔符
  107 + 'view_depr' => DS,
  108 + // 模板引擎普通标签开始标记
  109 + 'tpl_begin' => '{',
  110 + // 模板引擎普通标签结束标记
  111 + 'tpl_end' => '}',
  112 + // 标签库标签开始标记
  113 + 'taglib_begin' => '<',
  114 + // 标签库标签结束标记
  115 + 'taglib_end' => '>',
  116 + ],
  117 +
  118 + // 视图输出字符串内容替换
  119 + 'view_replace_str' => [],
  120 + // 默认跳转页面对应的模板文件
  121 + 'dispatch_success_tmpl' => THINK_PATH . 'tpl' . DS . 'dispatch_jump.tpl',
  122 + 'dispatch_error_tmpl' => THINK_PATH . 'tpl' . DS . 'dispatch_jump.tpl',
  123 +
  124 + // +----------------------------------------------------------------------
  125 + // | 异常及错误设置
  126 + // +----------------------------------------------------------------------
  127 +
  128 + // 异常页面的模板文件
  129 + 'exception_tmpl' => THINK_PATH . 'tpl' . DS . 'think_exception.tpl',
  130 +
  131 + // 错误显示信息,非调试模式有效
  132 + 'error_message' => '页面错误!请稍后再试~',
  133 + // 显示错误信息
  134 + 'show_error_msg' => false,
  135 + // 异常处理handle类 留空使用 \think\exception\Handle
  136 + 'exception_handle' => '',
  137 +
  138 + // +----------------------------------------------------------------------
  139 + // | 日志设置
  140 + // +----------------------------------------------------------------------
  141 +
  142 + 'log' => [
  143 + // 日志记录方式,内置 file socket 支持扩展
  144 + 'type' => 'File',
  145 + // 日志保存目录
  146 + 'path' => LOG_PATH,
  147 + // 日志记录级别
  148 + 'level' => [],
  149 + ],
  150 +
  151 + // +----------------------------------------------------------------------
  152 + // | Trace设置 开启 app_trace 后 有效
  153 + // +----------------------------------------------------------------------
  154 + 'trace' => [
  155 + // 内置Html Console 支持扩展
  156 + 'type' => 'Html',
  157 + ],
  158 +
  159 + // +----------------------------------------------------------------------
  160 + // | 缓存设置
  161 + // +----------------------------------------------------------------------
  162 +
  163 + 'cache' => [
  164 + // 驱动方式
  165 + 'type' => 'File',
  166 + // 缓存保存目录
  167 + 'path' => CACHE_PATH,
  168 + // 缓存前缀
  169 + 'prefix' => '',
  170 + // 缓存有效期 0表示永久缓存
  171 + 'expire' => 0,
  172 + ],
  173 +
  174 + // +----------------------------------------------------------------------
  175 + // | 会话设置
  176 + // +----------------------------------------------------------------------
  177 +
  178 + 'session' => [
  179 + 'id' => '',
  180 + // SESSION_ID的提交变量,解决flash上传跨域
  181 + 'var_session_id' => '',
  182 + // SESSION 前缀
  183 + 'prefix' => 'think',
  184 + // 驱动方式 支持redis memcache memcached
  185 + 'type' => '',
  186 + // 是否自动开启 SESSION
  187 + 'auto_start' => true,
  188 + ],
  189 +
  190 + // +----------------------------------------------------------------------
  191 + // | Cookie设置
  192 + // +----------------------------------------------------------------------
  193 + 'cookie' => [
  194 + // cookie 名称前缀
  195 + 'prefix' => '',
  196 + // cookie 保存时间
  197 + 'expire' => 0,
  198 + // cookie 保存路径
  199 + 'path' => '/',
  200 + // cookie 有效域名
  201 + 'domain' => '',
  202 + // cookie 启用安全传输
  203 + 'secure' => false,
  204 + // httponly设置
  205 + 'httponly' => '',
  206 + // 是否使用 setcookie
  207 + 'setcookie' => true,
  208 + ],
  209 +
  210 + // +----------------------------------------------------------------------
  211 + // | 数据库设置
  212 + // +----------------------------------------------------------------------
  213 +
  214 + 'database' => [
  215 + // 数据库调试模式
  216 + 'debug' => true,
  217 + // 数据集返回类型
  218 + 'resultset_type' => 'collection',
  219 + // 自动写入时间戳字段
  220 + 'auto_timestamp' => false,
  221 + // 时间字段取出后的默认时间格式
  222 + 'datetime_format' => false,
  223 + // 是否需要进行SQL性能分析
  224 + 'sql_explain' => false,
  225 + ],
  226 +
  227 + //分页配置
  228 + 'paginate' => [
  229 + 'type' => 'bootstrap',
  230 + 'var_page' => 'page',
  231 + 'list_rows' => 15,
  232 + ],
  233 + //图片验证码
  234 + 'captcha' => [
  235 + // 验证码字符集合
  236 + 'codeSet' => '2345678abcdefhijkmnpqrstuvwxyzABCDEFGHJKLMNPQRTUVWXY',
  237 + // 验证码字体大小(px)
  238 + 'fontSize' => 25,
  239 + // 是否画混淆曲线
  240 + 'useCurve' => true,
  241 + // 验证码图片高度
  242 + 'imageH' => 30,
  243 + // 验证码图片宽度
  244 + 'imageW' => 100,
  245 + // 验证码位数
  246 + 'length' => 5,
  247 + // 验证成功后是否重置
  248 + 'reset' => true
  249 + ],
  250 +
  251 + // +----------------------------------------------------------------------
  252 + // | CMF 设置
  253 + // +----------------------------------------------------------------------
  254 + 'cmf_theme_path' => 'themes/home/',
  255 + 'cmf_default_theme' => 'simpleboot3',
  256 + 'cmf_admin_theme_path' => 'themes/admin/',
  257 + 'cmf_admin_default_theme' => 'simpleboot3',
  258 +];
  1 +<?php
  2 +// +----------------------------------------------------------------------
  3 +// | bronet [ 以客户为中心 以奋斗者为本 ]
  4 +// +----------------------------------------------------------------------
  5 +// | Copyright (c) 2013-2017 http://www.bronet.cn All rights reserved.
  6 +// +----------------------------------------------------------------------
  7 +
  8 +if(file_exists(ROOT_PATH."data/conf/database.php")){
  9 + $database=include ROOT_PATH."data/conf/database.php";
  10 +}else{
  11 + $database=[];
  12 +}
  13 +
  14 +return $database;
  1 +<?php
  2 +// +----------------------------------------------------------------------
  3 +// | bronet [ 以客户为中心 以奋斗者为本 ]
  4 +// +----------------------------------------------------------------------
  5 +// | Copyright (c) 2013-2017 http://www.bronet.cn All rights reserved.
  6 +// +----------------------------------------------------------------------
  7 +
  8 +return [
  9 + // 应用调试模式
  10 + 'app_debug' => true,
  11 + // 应用Trace
  12 + 'app_trace' => true,
  13 +
  14 +];
  1 +<?php
  2 +namespace api\home\controller;
  3 +
  4 +use cmf\controller\RestBaseController;
  5 +
  6 +/**
  7 + * @title 欢迎页
  8 + * @description 欢迎使用在线接口文档
  9 + */
  10 +class IndexController extends RestBaseController
  11 +{
  12 + /**
  13 + * @title 首页
  14 + * @description 默认访问接口
  15 + * @author Tiger Yang
  16 + * @url /home/index/index
  17 + * @method GET
  18 + *
  19 + * @return version:版本号
  20 + * @return code:错误码
  21 + */
  22 + public function index()
  23 + {
  24 + $data=[
  25 + 'version' => '1.0.0',
  26 + 'code'=>[
  27 + '20000' => '默认成功返回码',
  28 + '40000' => '默认错误返回码',
  29 + '40001' => '未登录或登录失效',
  30 + '40002' => '签名验证失败',
  31 + '40003' => '缺少必要参数或参数格式错误',
  32 + '40004' => '登录失败',
  33 + ]
  34 + ];
  35 + $this->success("恭喜您,API访问成功!", $data);
  36 + }
  37 +
  38 +}
  1 +<?php
  2 +// +----------------------------------------------------------------------
  3 +// | bronet [ 以客户为中心 以奋斗者为本 ]
  4 +// +----------------------------------------------------------------------
  5 +// | Copyright (c) 2013-2017 http://www.bronet.cn All rights reserved.
  6 +// +----------------------------------------------------------------------
  7 +// API 模板文件,可以复制
  8 +namespace api\home\controller;
  9 +
  10 +use cmf\controller\RestBaseController;
  11 +
  12 +class RestController extends RestBaseController
  13 +{
  14 + /**
  15 + * 显示资源列表
  16 + */
  17 + public function index()
  18 + {
  19 + }
  20 +
  21 + /**
  22 + * 保存新建的资源
  23 + */
  24 + public function save()
  25 + {
  26 + }
  27 +
  28 + /**
  29 + * 显示指定的资源
  30 + *
  31 + * @param int $id
  32 + */
  33 + public function read($id)
  34 + {
  35 + }
  36 +
  37 + /**
  38 + * 保存更新的资源
  39 + *
  40 + * @param int $id
  41 + */
  42 + public function update($id)
  43 + {
  44 + }
  45 +
  46 + /**
  47 + * 删除指定资源
  48 + *
  49 + * @param int $id
  50 + */
  51 + public function delete($id)
  52 + {
  53 + }
  54 +
  55 +}
  1 +<?php
  2 +// +----------------------------------------------------------------------
  3 +// | 文件说明:幻灯片
  4 +// +----------------------------------------------------------------------
  5 +// | Copyright (c) 2013-2017 http://www.bronet.cn All rights reserved.
  6 +// +----------------------------------------------------------------------
  7 +// | Date: 2017-5-25
  8 +// +----------------------------------------------------------------------
  9 +namespace api\home\controller;
  10 +
  11 +use api\home\model\SlideModel;
  12 +use cmf\controller\RestBaseController;
  13 +
  14 +class SlidesController extends RestBaseController
  15 +{
  16 + /**
  17 + * [获取幻灯片]
  18 + * @Author: wuwu<15093565100@163.com>
  19 + * @DateTime: 2017-05-25T20:48:53+0800
  20 + * @since: 1.0
  21 + */
  22 + public function read()
  23 + {
  24 + //slide为空或不存在抛出异常
  25 + $id = $this->request->param('id', 0, 'intval');
  26 + if (empty($id)) {
  27 + $this->error('缺少ID参数');
  28 + }
  29 +
  30 + $map['id'] = $id;
  31 + $obj = new SlideModel();
  32 + $data = $obj->SlideList($map);
  33 +
  34 + //剔除分类状态隐藏 剔除分类下显示数据为空
  35 + if ($data->isEmpty() || empty($data->toArray()[0]['items'])) {
  36 + $this->error('该组幻灯片显示数据为空');
  37 + }
  38 +
  39 + $this->success("该组幻灯片获取成功!", $data);
  40 + }
  41 +
  42 +}
  1 +<?php
  2 +// +----------------------------------------------------------------------
  3 +// | 文件说明:用户-幻灯片
  4 +// +----------------------------------------------------------------------
  5 +// | Copyright (c) 2013-2017 http://www.bronet.cn All rights reserved.
  6 +// +----------------------------------------------------------------------
  7 +// | Date: 2017-5-25
  8 +// +----------------------------------------------------------------------
  9 +
  10 +namespace api\home\model;
  11 +
  12 +use think\Model;
  13 +
  14 +class SlideItemModel extends Model
  15 +{
  16 + /**
  17 + * [base 全局查询范围status=1显示状态]
  18 + * @Author: wuwu<15093565100@163.com>
  19 + * @DateTime: 2017-05-25T21:54:03+0800
  20 + * @since: 1.0
  21 + */
  22 + protected function base($query)
  23 + {
  24 + $query->where('status', 1);
  25 + }
  26 +
  27 + /**
  28 + * image 自动转化
  29 + * @param $value
  30 + * @return array
  31 + */
  32 + public function getImageAttr($value)
  33 + {
  34 + return cmf_get_image_url($value);
  35 + }
  36 +}
  37 +
  1 +<?php
  2 +// +----------------------------------------------------------------------
  3 +// | 文件说明:用户-幻灯片
  4 +// +----------------------------------------------------------------------
  5 +// | Copyright (c) 2013-2017 http://www.bronet.cn All rights reserved.
  6 +// +----------------------------------------------------------------------
  7 +// | Date: 2017-5-25
  8 +// +----------------------------------------------------------------------
  9 +
  10 +namespace api\home\model;
  11 +
  12 +use think\Model;
  13 +
  14 +class SlideModel extends Model
  15 +{
  16 +
  17 + /**
  18 + * [base 全局查询范围status=1显示状态]
  19 + * @Author: wuwu<15093565100@163.com>
  20 + * @DateTime: 2017-05-25T21:54:03+0800
  21 + * @since: 1.0
  22 + */
  23 + protected function base($query)
  24 + {
  25 + $query->where('status', 1)->where('delete_time', 0);
  26 + }
  27 +
  28 + /**
  29 + * [SlideItemModel 一对一关联模型 关联分类下的幻灯片]
  30 + * @Author: wuwu<15093565100@163.com>
  31 + * @DateTime: 2017-05-25T23:30:27+0800
  32 + * @since: 1.0
  33 + */
  34 + protected function items()
  35 + {
  36 + return $this->hasMany('SlideItemModel');
  37 + }
  38 +
  39 + /**
  40 + * [SlideList 幻灯片获取]
  41 + * @Author: wuwu<15093565100@163.com>
  42 + * @DateTime: 2017-05-25T20:52:27+0800
  43 + * @since: 1.0
  44 + */
  45 + public function SlideList($map)
  46 + {
  47 + $data = $this->relation('items')->field(true)->where($map)->select();
  48 + return $data;
  49 + }
  50 +
  51 +}
  52 +
  1 +<?php
  2 +
  3 +use think\Route;
  4 +
  5 +Route::resource('home/slides', 'home/Slides');
  1 +<?php
  2 +// +----------------------------------------------------------------------
  3 +// | bronet [ 以客户为中心 以奋斗者为本 ]
  4 +// +----------------------------------------------------------------------
  5 +// | Copyright (c) 2013-2017 http://www.bronet.cn All rights reserved.
  6 +// +----------------------------------------------------------------------
  7 +
  8 +namespace api\portal\controller;
  9 +
  10 +use cmf\controller\RestBaseController;
  11 +use api\portal\model\PortalPostModel;
  12 +use api\portal\model\PortalTagPostModel;
  13 +
  14 +class ArticlesController extends RestBaseController
  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 + $params['where']['post_type'] = 1;
  31 + $data = $this->postModel->getDatas($params);
  32 + $this->success('请求成功!', $data);
  33 + }
  34 +
  35 + /**
  36 + * 获取指定的文章
  37 + * @param int $id
  38 + */
  39 + public function read($id)
  40 + {
  41 + if (intval($id) === 0) {
  42 + $this->error('无效的文章id!');
  43 + } else {
  44 + $params = $this->request->get();
  45 + $params['where']['post_type'] = 1;
  46 + $params['id'] = $id;
  47 + $data = $this->postModel->getDatas($params);
  48 + if (empty($data)) {
  49 + $this->error('文章不存在!');
  50 + } else {
  51 + $this->success('请求成功!', $data);
  52 + }
  53 +
  54 + }
  55 + }
  56 +
  57 + /**
  58 + * 我的文章列表
  59 + */
  60 + public function my()
  61 + {
  62 + $params = $this->request->get();
  63 + $userId = $this->getUserId();
  64 + $data = $this->postModel->getUserArticles($userId, $params);
  65 + $this->success('请求成功!', $data);
  66 + }
  67 +
  68 + /**
  69 + * 添加文章
  70 + */
  71 + public function save()
  72 + {
  73 + $data = $this->request->post();
  74 + $data['user_id'] = $this->getUserId();
  75 + $result = $this->validate($data, 'Articles.article');
  76 + if ($result !== true) {
  77 + $this->error($result);
  78 + }
  79 +
  80 + if (empty($data['published_time'])) {
  81 + $data['published_time'] = time();
  82 + }
  83 +
  84 + $this->postModel->addArticle($data);
  85 + $this->success('添加成功!');
  86 + }
  87 +
  88 + /**
  89 + * 更新文章
  90 + * @param int $id
  91 + */
  92 + public function update($id)
  93 + {
  94 + $data = $this->request->put();
  95 + $result = $this->validate($data, 'Articles.article');
  96 + if ($result !== true) {
  97 + $this->error($result);
  98 + }
  99 + if (empty($id)) {
  100 + $this->error('无效的文章id');
  101 + }
  102 + $result = $this->postModel->editArticle($data, $id, $this->getUserId());
  103 + if ($result === false) {
  104 + $this->error('编辑失败!');
  105 + } else {
  106 + $this->success('编辑成功!');
  107 + }
  108 + }
  109 +
  110 + /**
  111 + * 删除文章
  112 + * @param int $id
  113 + */
  114 + public function delete($id)
  115 + {
  116 + if (empty($id)) {
  117 + $this->error('无效的文章id');
  118 + }
  119 + $result = $this->postModel->deleteArticle($id, $this->getUserId());
  120 + if ($result == -1) {
  121 + $this->error('文章已删除');
  122 + }
  123 + if ($result) {
  124 + $this->success('删除成功!');
  125 + } else {
  126 + $this->error('删除失败!');
  127 + }
  128 + }
  129 +
  130 + /**
  131 + * 批量删除文章
  132 + */
  133 + public function deletes()
  134 + {
  135 + $ids = $this->request->post('ids/a');
  136 + if (empty($ids)) {
  137 + $this->error('文章id不能为空');
  138 + }
  139 + $result = $this->postModel->deleteArticle($ids, $this->getUserId());
  140 + if ($result == -1) {
  141 + $this->error('文章已删除');
  142 + }
  143 + if ($result) {
  144 + $this->success('删除成功!');
  145 + } else {
  146 + $this->error('删除失败!');
  147 + }
  148 + }
  149 +
  150 + public function search()
  151 + {
  152 + $params = $this->request->get();
  153 + if (!empty($params['keyword'])) {
  154 + $params['where'] = [
  155 + 'post_type' => 1,
  156 + 'post_title|post_keywords|post_excerpt' => ['like', '%' . $params['keyword'] . '%']
  157 + ];
  158 + $data = $this->postModel->getDatas($params);
  159 + $this->success('请求成功!', $data);
  160 + } else {
  161 + $this->error('搜索关键词不能为空!');
  162 + }
  163 +
  164 + }
  165 +}
  1 +<?php
  2 +// +----------------------------------------------------------------------
  3 +// | bronet [ 以客户为中心 以奋斗者为本 ]
  4 +// +----------------------------------------------------------------------
  5 +// | Copyright (c) 2013-2017 http://www.bronet.cn All rights reserved.
  6 +// +----------------------------------------------------------------------
  7 +
  8 +namespace api\portal\controller;
  9 +
  10 +use cmf\controller\RestBaseController;
  11 +use api\portal\model\PortalCategoryModel;
  12 +
  13 +class CategoriesController extends RestBaseController
  14 +{
  15 + protected $categoryModel;
  16 +
  17 + public function __construct(PortalCategoryModel $categoryModel)
  18 + {
  19 + parent::__construct();
  20 + $this->categoryModel = $categoryModel;
  21 + }
  22 +
  23 + /**
  24 + * 获取分类列表
  25 + */
  26 + public function index()
  27 + {
  28 + $params = $this->request->get();
  29 + $data = $this->categoryModel->getDatas($params);
  30 + $this->success('请求成功!', $data);
  31 + }
  32 +
  33 + /**
  34 + * 显示指定的分类
  35 + * @param int $id
  36 + */
  37 + public function read($id)
  38 + {
  39 + $params = $this->request->get();
  40 + $params['id'] = $id;
  41 + $data = $this->categoryModel->getDatas($params);
  42 + $this->success('请求成功!', $data);
  43 + }
  44 +}
  1 +<?php
  2 +namespace api\portal\controller;
  3 +use think\Controller;
  4 +
  5 +/**
  6 + * @title 测试demo
  7 + * @description 接口说明
  8 + * @header name:key require:1 default: desc:秘钥(区别设置)
  9 + * @param name:public type:int require:1 default:1 other: desc:公共参数(区别设置)
  10 + */
  11 +class DemoController extends Controller
  12 +{
  13 + /**
  14 + * @title 测试demo接口
  15 + * @description 接口说明
  16 + * @author 开发者
  17 + * @url /portal/demo/index
  18 + * @method GET
  19 + * @module 测试模块
  20 + * @header name:device require:1 default: desc:设备号
  21 + *
  22 + * @param name:id type:int require:1 default:1 other: desc:唯一ID
  23 + *
  24 + * @return name:名称
  25 + * @return mobile:手机号
  26 + * @return list_messages:消息列表@
  27 + * @list_messages message_id:消息ID content:消息内容
  28 + * @return object:对象信息@!
  29 + * @object attribute1:对象属性1 attribute2:对象属性2
  30 + * @return array:数组值#
  31 + * @return list_user:用户列表@
  32 + * @list_user name:名称 mobile:手机号 list_follow:关注列表@
  33 + * @list_follow user_id:用户id name:名称
  34 + */
  35 + public function index()
  36 + {
  37 + //接口代码
  38 + $device = $this->request->header('device');
  39 + echo json_encode(["code"=>200, "message"=>"success", "data"=>['device'=>$device]]);
  40 + }
  41 +
  42 + /**
  43 + * @title 登录接口
  44 + * @description 接口说明
  45 + * @author 开发者
  46 + * @url /api/demo
  47 + * @method GET
  48 + * @module 用户模块
  49 +
  50 + * @param name:name type:int require:1 default:1 other: desc:用户名
  51 + * @param name:pass type:int require:1 default:1 other: desc:密码
  52 + *
  53 + * @return name:名称
  54 + * @return mobile:手机号
  55 + *
  56 + */
  57 + public function login(Request $request)
  58 + {
  59 + //接口代码
  60 + $device = $request->header('device');
  61 + echo json_encode(["code"=>200, "message"=>"success", "data"=>['device'=>$device]]);
  62 + }
  63 +
  64 +}
  1 +<?php
  2 +// +----------------------------------------------------------------------
  3 +// | bronet [ 以客户为中心 以奋斗者为本 ]
  4 +// +----------------------------------------------------------------------
  5 +// | Copyright (c) 2013-2017 http://www.bronet.cn All rights reserved.
  6 +// +----------------------------------------------------------------------
  7 +namespace api\portal\controller;
  8 +
  9 +use api\portal\model\PortalCategoryModel;
  10 +use api\portal\model\PortalPostModel;
  11 +use cmf\controller\RestBaseController;
  12 +
  13 +class ListsController extends RestBaseController
  14 +{
  15 +
  16 + /**
  17 + * [推荐文章列表]
  18 + * @Author: wuwu<15093565100@163.com>
  19 + * @DateTime: 2017-07-17T11:36:51+0800
  20 + * @since: 1.0
  21 + */
  22 + public function recommended()
  23 + {
  24 + $param = $this->request->param();
  25 + $portalPostModel = new PortalPostModel();
  26 +
  27 + $param['where'] = ['recommended' => 1];
  28 +
  29 + $articles = $portalPostModel->getDatas($param);
  30 +
  31 + $this->success('ok', ['list' => $articles]);
  32 + }
  33 +
  34 + /**
  35 + * [getCategoryPostLists 分类文章列表]
  36 + * @Author: wuwu<15093565100@163.com>
  37 + * @DateTime: 2017-07-17T15:22:41+0800
  38 + * @since: 1.0
  39 + */
  40 + public function getCategoryPostLists()
  41 + {
  42 + $categoryId = $this->request->param('category_id', 0, 'intval');
  43 +
  44 +
  45 + $portalCategoryModel = new PortalCategoryModel();
  46 +
  47 + $findCategory = $portalCategoryModel->where('id', $categoryId)->find();
  48 +
  49 + //分类是否存在
  50 + if (empty($findCategory)) {
  51 + $this->error('分类不存在!');
  52 + }
  53 +
  54 + $param = $this->request->param();
  55 +
  56 + $articles = $portalCategoryModel->paramsFilter($param, $findCategory->articles()->alias('post'))->select();
  57 +
  58 + if (!empty($param['relation'])) {
  59 + if (count($articles) > 0) {
  60 + $articles->load('user');
  61 + $articles->append(['user']);
  62 + }
  63 + }
  64 +
  65 + $this->success('ok', ['list' => $articles]);
  66 + }
  67 +
  68 +}
  1 +<?php
  2 +// +----------------------------------------------------------------------
  3 +// | bronet [ 以客户为中心 以奋斗者为本 ]
  4 +// +----------------------------------------------------------------------
  5 +// | Copyright (c) 2013-2017 http://www.bronet.cn All rights reserved.
  6 +// +----------------------------------------------------------------------
  7 +
  8 +namespace api\portal\controller;
  9 +
  10 +use cmf\controller\RestBaseController;
  11 +use api\portal\model\PortalPostModel;
  12 +
  13 +class PagesController extends RestBaseController
  14 +{
  15 + protected $postModel;
  16 +
  17 + public function __construct(PortalPostModel $postModel)
  18 + {
  19 + parent::__construct();
  20 + $this->postModel = $postModel;
  21 + }
  22 +
  23 + /**
  24 + * 页面列表
  25 + */
  26 + public function index()
  27 + {
  28 + $params = $this->request->get();
  29 + $params['where']['post_type'] = 2;
  30 + $data = $this->postModel->getDatas($params);
  31 + $this->success('请求成功!', $data);
  32 + }
  33 +
  34 + /**
  35 + * 获取页面
  36 + * @param int $id
  37 + */
  38 + public function read($id)
  39 + {
  40 + $params = $this->request->get();
  41 + $params['where']['post_type'] = 2;
  42 + $params['id'] = $id;
  43 + $data = $this->postModel->getDatas($params);
  44 + $this->success('请求成功!', $data);
  45 + }
  46 +}
  1 +<?php
  2 +// +----------------------------------------------------------------------
  3 +// | bronet [ 以客户为中心 以奋斗者为本 ]
  4 +// +----------------------------------------------------------------------
  5 +// | Copyright (c) 2013-2017 http://www.bronet.cn All rights reserved.
  6 +// +----------------------------------------------------------------------
  7 +namespace api\portal\controller;
  8 +
  9 +use api\portal\model\PortalPostModel;
  10 +use cmf\controller\RestBaseController;
  11 +use api\portal\model\PortalTagModel;
  12 +
  13 +class TagsController extends RestBaseController
  14 +{
  15 + protected $tagModel;
  16 +
  17 + public function __construct(PortalTagModel $tagModel)
  18 + {
  19 + parent::__construct();
  20 + $this->tagModel = $tagModel;
  21 + }
  22 +
  23 + /**
  24 + * 获取标签列表
  25 + */
  26 + public function index()
  27 + {
  28 + $params = $this->request->get();
  29 + $data = $this->tagModel->getDatas($params);
  30 + $this->success('请求成功!', $data);
  31 + }
  32 +
  33 + /**
  34 + * 获取热门标签列表
  35 + */
  36 + public function hotTags()
  37 + {
  38 + $params = $this->request->get();
  39 + $params['where']['recommended'] = 1;
  40 + $data = $this->tagModel->getDatas($params);
  41 + $this->success('请求成功!', $data);
  42 + }
  43 +
  44 + /**
  45 + * 获取标签文章列表
  46 + * @param int $id
  47 + */
  48 + public function articles($id)
  49 + {
  50 + if (intval($id) === 0) {
  51 + $this->error('无效的标签id!');
  52 + } else {
  53 + $params = $this->request->param();
  54 + $params['id'] = $id;
  55 + $params['relation'] = 'articles';
  56 + $postModel = new PortalPostModel();
  57 +
  58 + $articles = $postModel->setCondition($params)->alias('a')->join('__PORTAL_TAG_POST__ tp', 'a.id = tp.post_id')
  59 + ->where(['tag_id' => $id])->select();
  60 +
  61 + $this->success('请求成功!', ['articles' => $articles]);
  62 + }
  63 + }
  64 +}
  1 +<?php
  2 +// +----------------------------------------------------------------------
  3 +// | bronet [ 以客户为中心 以奋斗者为本 ]
  4 +// +----------------------------------------------------------------------
  5 +// | Copyright (c) 2013-2017 http://www.bronet.cn All rights reserved.
  6 +// +----------------------------------------------------------------------
  7 +namespace api\portal\controller;
  8 +
  9 +use cmf\controller\RestUserBaseController;
  10 +use api\portal\logic\PortalPostModel;
  11 +
  12 +class UserArticlesController extends RestUserBaseController
  13 +{
  14 + protected $postModel;
  15 +
  16 + public function __construct(PortalPostModel $postModel)
  17 + {
  18 + parent::__construct();
  19 + $this->postModel = $postModel;
  20 + }
  21 +
  22 + /**
  23 + * 显示资源列表
  24 + */
  25 + public function index()
  26 + {
  27 + $params = $this->request->get();
  28 + $userId = $this->getUserId();
  29 + $datas = $this->postModel->getUserArticles($userId,$params);
  30 + $this->success('请求成功!', $datas);
  31 + }
  32 +
  33 + /**
  34 + * 保存新建的资源
  35 + */
  36 + public function save()
  37 + {
  38 + $datas = $this->request->post();
  39 + $datas['user_id'] = $this->getUserId();
  40 + $result = $this->validate($datas, 'Articles.article');
  41 + if ($result !== true) {
  42 + $this->error($result);
  43 + }
  44 + if (empty($datas['published_time'])) {
  45 + $datas['published_time'] = time();
  46 + }
  47 + $this->postModel->addArticle($datas);
  48 + $this->success('添加成功!');
  49 + }
  50 +
  51 + /**
  52 + * 显示指定的资源
  53 + *
  54 + * @param int $id
  55 + */
  56 + public function read($id)
  57 + {
  58 + if (empty($id)) {
  59 + $this->error('无效的文章id');
  60 + }
  61 + $params = $this->request->get();
  62 + $params['id'] = $id;
  63 + $userId = $this->getUserId();
  64 + $datas = $this->postModel->getUserArticles($userId,$params);
  65 + $this->success('请求成功!', $datas);
  66 + }
  67 +
  68 + /**
  69 + * 保存更新的资源
  70 + *
  71 + * @param int $id
  72 + */
  73 + public function update($id)
  74 + {
  75 + $data = $this->request->put();
  76 + $result = $this->validate($data, 'Articles.article');
  77 + if ($result !== true) {
  78 + $this->error($result);
  79 + }
  80 + if (empty($id)) {
  81 + $this->error('无效的文章id');
  82 + }
  83 + $result = $this->postModel->editArticle($data,$id,$this->getUserId());
  84 + if ($result === false) {
  85 + $this->error('编辑失败!');
  86 + } else {
  87 + $this->success('编辑成功!');
  88 + }
  89 + }
  90 +
  91 + /**
  92 + * 删除指定资源
  93 + *
  94 + * @param int $id
  95 + */
  96 + public function delete($id)
  97 + {
  98 + if (empty($id)) {
  99 + $this->error('无效的文章id');
  100 + }
  101 + $result = $this->postModel->deleteArticle($id,$this->getUserId());
  102 + if ($result == -1) {
  103 + $this->error('文章已删除');
  104 + }
  105 + if ($result) {
  106 + $this->success('删除成功!');
  107 + } else {
  108 + $this->error('删除失败!');
  109 + }
  110 + }
  111 + /**
  112 + * 批量删除文章
  113 + */
  114 + public function deletes()
  115 + {
  116 + $ids = $this->request->post('ids/a');
  117 + if (empty($ids)) {
  118 + $this->error('文章id不能为空');
  119 + }
  120 + $result = $this->postModel->deleteArticle($ids,$this->getUserId());
  121 + if ($result == -1) {
  122 + $this->error('文章已删除');
  123 + }
  124 + if ($result) {
  125 + $this->success('删除成功!');
  126 + } else {
  127 + $this->error('删除失败!');
  128 + }
  129 + }
  130 +
  131 + /**
  132 + * 我的文章列表
  133 + */
  134 + public function my()
  135 + {
  136 + $params = $this->request->get();
  137 + $userId = $this->getUserId();
  138 + $data = $this->postModel->getUserArticles($userId, $params);
  139 + $this->success('请求成功!', $data);
  140 + }
  141 +}
  1 +<?php
  2 +// +----------------------------------------------------------------------
  3 +// | bronet [ 以客户为中心 以奋斗者为本 ]
  4 +// +----------------------------------------------------------------------
  5 +// | Copyright (c) 2013-2017 http://www.bronet.cn All rights reserved.
  6 +// +----------------------------------------------------------------------
  7 +namespace api\portal\controller;
  8 +
  9 +use api\portal\model\PortalPostModel;
  10 +use cmf\controller\RestBaseController;
  11 +
  12 +class UserController extends RestBaseController
  13 +{
  14 + protected $postModel;
  15 +
  16 + public function __construct(PortalPostModel $postModel)
  17 + {
  18 + parent::__construct();
  19 + $this->postModel = $postModel;
  20 + }
  21 +
  22 + /**
  23 + * 会员文章列表
  24 + */
  25 + public function articles()
  26 + {
  27 + $userId = $this->request->param('user_id', 0, 'intval');
  28 +
  29 + if(empty($userId)){
  30 + $this->error('用户id不能空!');
  31 + }
  32 +
  33 + $data = $this->request->param();
  34 + $articles = $this->postModel->setCondition($data)->where(['user_id' => $userId])->select();
  35 +
  36 + if (count($articles) == 0) {
  37 + $this->error('没有数据');
  38 + } else {
  39 + $this->success('ok', ['list' => $articles]);
  40 + }
  41 +
  42 + }
  43 +
  44 +}
  1 +<?php
  2 +// +----------------------------------------------------------------------
  3 +// | bronet [ 以客户为中心 以奋斗者为本 ]
  4 +// +----------------------------------------------------------------------
  5 +// | Copyright (c) 2013-2017 http://www.bronet.cn All rights reserved.
  6 +// +----------------------------------------------------------------------
  7 +
  8 +namespace api\portal\logic;
  9 +
  10 +use api\portal\model\PortalPostModel as PortalPost;
  11 +use think\Db;
  12 +class PortalPostModel extends PortalPost
  13 +{
  14 + /**
  15 + * 获取相关文章
  16 + * @param int|string|array $postIds 文章id
  17 + * @return array
  18 + */
  19 + public function getRelationPosts($postIds)
  20 + {
  21 + $posts = $this->with('articleUser')
  22 + ->field('id,post_title,user_id,is_top,post_hits,post_like,comment_count,more')
  23 + ->whereIn('id', $postIds)
  24 + ->select();
  25 + foreach ($posts as $post) {
  26 + $post->appendRelationAttr('articleUser', 'user_nickname');
  27 + }
  28 + return $posts;
  29 + }
  30 + /**
  31 + * 获取用户文章
  32 + */
  33 + public function getUserArticles($userId, $params)
  34 + {
  35 + $where = [
  36 + 'post_type' => 1,
  37 + 'user_id' => $userId
  38 + ];
  39 + if (!empty($params)) {
  40 + $this->paramsFilter($params);
  41 + }
  42 + return $this->where($where)->select();
  43 + }
  44 +
  45 + /**
  46 + * 会员添加文章
  47 + * @param array $data 文章数据
  48 + * @return $this
  49 + */
  50 + public function addArticle($data)
  51 + {
  52 + //设置图片附件,写入字段过滤
  53 + $dataField = $this->setMoreField($data);
  54 + $data = $dataField[0];
  55 + array_push($dataField[1],'user_id');
  56 + $this->readonly = array_diff(['user_id'],$this->readonly);
  57 + $this->allowField($dataField[1])->data($data, true)->isUpdate(false)->save();
  58 + $categories = $this->strToArr($data['categories']);
  59 + $this->categories()->attach($categories);
  60 + if (!empty($data['post_keywords']) && is_string($data['post_keywords'])) {
  61 + //加入标签
  62 + $data['post_keywords'] = str_replace(',', ',', $data['post_keywords']);
  63 + $keywords = explode(',', $data['post_keywords']);
  64 + $this->addTags($keywords, $this->id);
  65 + }
  66 + return $this;
  67 + }
  68 +
  69 + /**
  70 + * 会员文章编辑
  71 + * @param array $data 文章数据
  72 + * @param int $id 文章id
  73 + * @param int $userId 文章所属用户id [可选]
  74 + * @return boolean 成功 true 失败 false
  75 + */
  76 + public function editArticle($data, $id, $userId = '')
  77 + {
  78 + if (!empty($userId)) {
  79 + $isBelong = $this->isuserPost($id, $userId);
  80 + if ($isBelong === false) {
  81 + return $isBelong;
  82 + }
  83 + }
  84 + //设置图片附件,写入字段过滤
  85 + $dataField = $this->setMoreField($data);
  86 + $data = $dataField[0];
  87 + $data['id'] = $id;
  88 + $this->allowField($dataField[1])->data($data, true)->isUpdate(true)->save();
  89 +
  90 + $categories = $this->strToArr($data['categories']);
  91 + $oldCategoryIds = $this->categories()->column('category_id');
  92 + $sameCategoryIds = array_intersect($categories, $oldCategoryIds);
  93 + $needDeleteCategoryIds = array_diff($oldCategoryIds, $sameCategoryIds);
  94 + $newCategoryIds = array_diff($categories, $sameCategoryIds);
  95 + if (!empty($needDeleteCategoryIds)) {
  96 + $this->categories()->detach($needDeleteCategoryIds);
  97 + }
  98 + if (!empty($newCategoryIds)) {
  99 + $this->categories()->attach(array_values($newCategoryIds));
  100 + }
  101 + if (!isset($data['post_keywords'])) {
  102 + $keywords = [];
  103 + } elseif (is_string($data['post_keywords'])) {
  104 + //加入标签
  105 + $data['post_keywords'] = str_replace(',', ',', $data['post_keywords']);
  106 + $keywords = explode(',', $data['post_keywords']);
  107 + }
  108 + $this->addTags($keywords, $data['id']);
  109 + return $this;
  110 + }
  111 +
  112 + /**
  113 + * 根据文章关键字,增加标签
  114 + * @param array $keywords 文章关键字数组
  115 + * @param int $articleId 文章id
  116 + * @return void
  117 + */
  118 + public function addTags($keywords, $articleId)
  119 + {
  120 + foreach ($keywords as $key => $value) {
  121 + $keywords[$key] = trim($value);
  122 + }
  123 + $continue = true;
  124 + $names = $this->tags()->column('name');
  125 + if (!empty($keywords) || !empty($names)) {
  126 + if (!empty($names)) {
  127 + $sameNames = array_intersect($keywords, $names);
  128 + $keywords = array_diff($keywords, $sameNames);
  129 + $shouldDeleteNames = array_diff($names, $sameNames);
  130 + if (!empty($shouldDeleteNames)) {
  131 + $tagIdNames = $this->tags()
  132 + ->where('name', 'in', $shouldDeleteNames)
  133 + ->column('pivot.id', 'tag_id');
  134 + $tagIds = array_keys($tagIdNames);
  135 + $tagPostIds = array_values($tagIdNames);
  136 + $tagPosts = DB::name('portal_tag_post')->where('tag_id', 'in', $tagIds)
  137 + ->field('id,tag_id,post_id')
  138 + ->select();
  139 + $keepTagIds = [];
  140 + foreach ($tagPosts as $key => $tagPost) {
  141 + if ($articleId != $tagPost['post_id']) {
  142 + array_push($keepTagIds, $tagPost['tag_id']);
  143 + }
  144 + }
  145 + $keepTagIds = array_unique($keepTagIds);
  146 + $shouldDeleteTagIds = array_diff($tagIds, $keepTagIds);
  147 + DB::name('PortalTag')->delete($shouldDeleteTagIds);
  148 + DB::name('PortalTagPost')->delete($tagPostIds);
  149 + }
  150 + } else {
  151 + $tagIdNames = DB::name('portal_tag')->where('name', 'in', $keywords)->column('name', 'id');
  152 + if (!empty($tagIdNames)) {
  153 + $tagIds = array_keys($tagIdNames);
  154 + $this->tags()->attach($tagIds);
  155 + $keywords = array_diff($keywords, array_values($tagIdNames));
  156 + if (empty($keywords)) {
  157 + $continue = false;
  158 + }
  159 + }
  160 + }
  161 + if ($continue) {
  162 + foreach ($keywords as $key => $value) {
  163 + if (!empty($value)) {
  164 + $this->tags()->attach(['name' => $value]);
  165 + }
  166 + }
  167 + }
  168 + }
  169 + }
  170 +
  171 + /**
  172 + * 设置缩略图,图片,附件
  173 + * 懒人方法
  174 + * @param $data 表单数据
  175 + */
  176 + public function setMoreField($data)
  177 + {
  178 + $allowField = [
  179 + 'post_title','post_keywords','post_source',
  180 + 'post_excerpt','post_content','thumbnail','more',
  181 + 'published_time'
  182 + ];
  183 + if (!empty($data['more'])) {
  184 + $data['more'] = $this->setMoreUrl($data['more']);
  185 + }
  186 + if (!empty($data['thumbnail'])) {
  187 + $data['more']['thumbnail'] = cmf_asset_relative_url($data['thumbnail']);
  188 + }
  189 + return [$data,$allowField];
  190 + }
  191 +
  192 + /**
  193 + * 获取图片附件url相对地址
  194 + * 默认上传名字 *_names 地址 *_urls
  195 + * @param $annex 上传附件
  196 + * @return array
  197 + */
  198 + public function setMoreUrl($annex)
  199 + {
  200 + $more = [];
  201 + if (!empty($annex)) {
  202 + foreach ($annex as $key => $value) {
  203 + $nameArr = $key . '_names';
  204 + $urlArr = $key . '_urls';
  205 + if (is_string($value[$nameArr]) && is_string($value[$urlArr])) {
  206 + $more[$key] = [$value[$nameArr], $value[$urlArr]];
  207 + } elseif (!empty($value[$nameArr]) && !empty($value[$urlArr])) {
  208 + $more[$key] = [];
  209 + foreach ($value[$urlArr] as $k => $url) {
  210 + $url = cmf_asset_relative_url($url);
  211 + array_push($more[$key], ['url' => $url, 'name' => $value[$nameArr][$k]]);
  212 + }
  213 + }
  214 + }
  215 + }
  216 + return $more;
  217 + }
  218 +
  219 + /**
  220 + * 删除文章
  221 + * @param $ids int|array 文章id
  222 + * @param int $userId 文章所属用户id [可选]
  223 + * @return bool|int 删除结果 true 成功 false 失败 -1 文章不存在
  224 + */
  225 + public function deleteArticle($ids, $userId)
  226 + {
  227 + $time = time();
  228 + $result = false;
  229 + $where = [];
  230 +
  231 + if (!empty($userId)) {
  232 + if (is_numeric($ids)) {
  233 + $article = $this->find($ids);
  234 + if (!empty($article)) {
  235 + if ($this->isUserPost($ids, $userId) || $userId == 1) {
  236 + $where['id'] = $ids;
  237 + }
  238 + }
  239 + } else {
  240 + $ids = $this->strToArr($ids);
  241 + $articles = $this->where('id', 'in', $ids)->select();
  242 + if (!empty($articles)) {
  243 + $deleteIds = $this->isUserPosts($ids, $userId);
  244 + if (!empty($deleteIds)) {
  245 + $where['id'] = ['in', $deleteIds];
  246 + }
  247 + }
  248 + }
  249 + } else {
  250 + if (is_numeric($ids)) {
  251 + $article = $this->find($ids);
  252 + if (!empty($article)) {
  253 + $where['id'] = $ids;
  254 + }
  255 + } else {
  256 + $ids = $this->strToArr($ids);
  257 + $articles = $this->where('id', 'in', $ids)->select();
  258 + if (!empty($articles)) {
  259 + $where['id'] = ['in', $ids];
  260 + }
  261 + }
  262 + }
  263 + if (empty($article) && empty($articles)) {
  264 + return -1;
  265 + }
  266 + if (!empty($where)) {
  267 + $result = $this->useGlobalScope(false)
  268 + ->where($where)
  269 + ->setField('delete_time', $time);
  270 + }
  271 + if ($result) {
  272 + $data = [
  273 + 'create_time' => $time,
  274 + 'table_name' => 'portal_post'
  275 + ];
  276 + if (!empty($article)) {
  277 + $data['name'] = $article['post_title'];
  278 + $article->recycleBin()->save($data);
  279 + }
  280 +
  281 + if (!empty($articles)) {
  282 + foreach ($articles as $article) {
  283 + $data['name'] = $article['post_title'];
  284 + $article->recycleBin()->save($data);
  285 + }
  286 + }
  287 + }
  288 + return $result;
  289 + }
  290 +
  291 + /**
  292 + * 判断文章所属用户是否为当前用户,超级管理员除外
  293 + * @params int $id 文章id
  294 + * @param int $userId 当前用户id
  295 + * @return boolean 是 true , 否 false
  296 + */
  297 + public function isUserPost($id, $userId)
  298 + {
  299 + $postUserId = $this->useGlobalScope(false)
  300 + ->getFieldById($id, 'user_id');
  301 + if ($postUserId != $userId || $userId != 1) {
  302 + return false;
  303 + } else {
  304 + return true;
  305 + }
  306 + }
  307 +
  308 + /**
  309 + * 过滤属于当前用户的文章,超级管理员除外
  310 + * @params array $ids 文章id的数组
  311 + * @param int $userId 当前用户id
  312 + * @return array 属于当前用户的文章id
  313 + */
  314 + public function isUserPosts($ids, $userId)
  315 + {
  316 + $postIds = $this->useGlobalScope(false)
  317 + ->where('user_id', $userId)
  318 + ->where('id', 'in', $ids)
  319 + ->column('id');
  320 + return array_intersect($ids, $postIds);
  321 + }
  322 +}
  1 +<?php
  2 +// +----------------------------------------------------------------------
  3 +// | bronet [ 以客户为中心 以奋斗者为本 ]
  4 +// +----------------------------------------------------------------------
  5 +// | Copyright (c) 2013-2017 http://www.bronet.cn All rights reserved.
  6 +// +----------------------------------------------------------------------
  7 +
  8 +namespace api\portal\model;
  9 +
  10 +use api\common\model\CommonModel;
  11 +
  12 +class PortalCategoryModel extends CommonModel
  13 +{
  14 + //类型转换
  15 + protected $type = [
  16 + 'more' => 'array',
  17 + ];
  18 +
  19 + //可查询字段
  20 + protected $visible = [
  21 + 'id', 'name', 'description', 'post_count',
  22 + 'seo_title', 'seo_keywords', 'seo_description',
  23 + 'more', 'PostIds', 'articles'
  24 + ];
  25 +
  26 + //模型关联方法
  27 + protected $relationFilter = ['articles'];
  28 +
  29 + /**
  30 + * 基础查询
  31 + */
  32 + protected function base($query)
  33 + {
  34 + $query->alias('portal_category')->where('delete_time', 0)
  35 + ->where('portal_category.status', 1);
  36 + }
  37 +
  38 + /**
  39 + * more 自动转化
  40 + * @param $value
  41 + * @return array
  42 + */
  43 + public function getMoreAttr($value)
  44 + {
  45 + $more = json_decode($value, true);
  46 + if (!empty($more['thumbnail'])) {
  47 + $more['thumbnail'] = cmf_get_image_url($more['thumbnail']);
  48 + }
  49 +
  50 + if (!empty($more['photos'])) {
  51 + foreach ($more['photos'] as $key => $value) {
  52 + $more['photos'][$key]['url'] = cmf_get_image_url($value['url']);
  53 + }
  54 + }
  55 + return $more;
  56 + }
  57 +
  58 + /**
  59 + * 关联文章表
  60 + * @return $this
  61 + */
  62 + public function articles()
  63 + {
  64 + return $this->belongsToMany('PortalPostModel', 'portal_category_post', 'post_id', 'category_id');
  65 + }
  66 +
  67 + /**
  68 + * [PostIds 关联]
  69 + * @Author: wuwu<15093565100@163.com>
  70 + * @DateTime: 2017-07-17T15:20:31+0800
  71 + * @since: 1.0
  72 + */
  73 + public function PostIds()
  74 + {
  75 + return self::hasMany('PortalCategoryPostModel', 'category_id', 'id');
  76 + }
  77 +
  78 + /**
  79 + * [categoryPostIds 此类文章id数组]
  80 + * @Author: wuwu<15093565100@163.com>
  81 + * @DateTime: 2017-07-17T15:21:08+0800
  82 + * @since: 1.0
  83 + * @param [type] $category_id [分类ID]
  84 + * @return [type] [文章id数组]
  85 + */
  86 + public static function categoryPostIds($category_id)
  87 + {
  88 + $ids = [];
  89 + $post_ids = self::relation('PostIds')->field(true)->where('id', $category_id)->find();
  90 + foreach ($post_ids['PostIds'] as $key => $id) {
  91 + $ids[] = $id['post_id'];
  92 + }
  93 + $post_ids['PostIds'] = $ids;
  94 + return $post_ids;
  95 + }
  96 +}
  1 +<?php
  2 +// +----------------------------------------------------------------------
  3 +// | bronet [ 以客户为中心 以奋斗者为本 ]
  4 +// +----------------------------------------------------------------------
  5 +// | Copyright (c) 2013-2017 http://www.bronet.cn All rights reserved.
  6 +// +----------------------------------------------------------------------
  7 +namespace api\portal\model;
  8 +
  9 +use think\Model;
  10 +
  11 +class PortalCategoryPostModel extends Model
  12 +{
  13 + /**
  14 + * 基础查询
  15 + */
  16 + protected function base($query)
  17 + {
  18 + $query->where('status', 1);
  19 + }
  20 +}
  1 +<?php
  2 +// +----------------------------------------------------------------------
  3 +// | bronet [ 以客户为中心 以奋斗者为本 ]
  4 +// +----------------------------------------------------------------------
  5 +// | Copyright (c) 2013-2017 http://www.bronet.cn All rights reserved.
  6 +// +----------------------------------------------------------------------
  7 +namespace api\portal\model;
  8 +
  9 +use think\Db;
  10 +use api\common\model\CommonModel;
  11 +
  12 +class PortalPostModel extends CommonModel
  13 +{
  14 + //可查询字段
  15 + protected $visible = [
  16 + 'id', 'articles.id', 'user_id', 'post_id', 'post_type', 'comment_status',
  17 + 'is_top', 'recommended', 'post_hits', 'post_like', 'comment_count',
  18 + 'create_time', 'update_time', 'published_time', 'post_title', 'post_keywords',
  19 + 'post_excerpt', 'post_source', 'post_content', 'more', 'user_nickname',
  20 + 'user', 'category_id'
  21 + ];
  22 +
  23 + //设置只读字段
  24 + protected $readonly = ['user_id'];
  25 + // 开启自动写入时间戳字段
  26 + protected $autoWriteTimestamp = true;
  27 + //类型转换
  28 + protected $type = [
  29 + 'more' => 'array',
  30 + ];
  31 + //模型关联方法
  32 + protected $relationFilter = ['user', 'categories'];
  33 +
  34 + /**
  35 + * 基础查询
  36 + */
  37 + protected function base($query)
  38 + {
  39 + $query->where('delete_time', 0)
  40 + ->where('post_status', 1)
  41 + ->whereTime('published_time', 'between', [1, time()]);
  42 + }
  43 +
  44 + /**
  45 + * 关联 user表
  46 + * @return $this
  47 + */
  48 + public function user()
  49 + {
  50 + return $this->belongsTo('api\portal\model\UserModel', 'user_id');
  51 + }
  52 +
  53 + /**
  54 + * 关联 user表
  55 + * @return $this
  56 + */
  57 + public function articleUser()
  58 + {
  59 + return $this->belongsTo('api\portal\model\UserModel', 'user_id')->field('id,user_nickname');
  60 + }
  61 +
  62 + /**
  63 + * 关联分类表
  64 + * @return $this
  65 + */
  66 + public function categories()
  67 + {
  68 + return $this->belongsToMany('api\portal\model\PortalCategoryModel', 'portal_category_post', 'category_id', 'post_id');
  69 + }
  70 +
  71 + /**
  72 + * 关联标签表
  73 + * @return $this
  74 + */
  75 + public function tags()
  76 + {
  77 + return $this->belongsToMany('api\portal\model\PortalTagModel', 'portal_tag_post', 'tag_id', 'post_id');
  78 + }
  79 +
  80 + /**
  81 + * 关联 回收站 表
  82 + */
  83 + public function recycleBin()
  84 + {
  85 + return $this->hasOne('api\portal\model\RecycleBinModel', 'object_id');
  86 + }
  87 +
  88 + /**
  89 + * published_time 自动转化
  90 + * @param $value
  91 + * @return string
  92 + */
  93 + public function getPublishedTimeAttr($value)
  94 + {
  95 + return date('Y-m-d H:i:s', $value);
  96 + }
  97 +
  98 + /**
  99 + * published_time 自动转化
  100 + * @param $value
  101 + * @return int
  102 + */
  103 + public function setPublishedTimeAttr($value)
  104 + {
  105 + if (is_numeric($value)) {
  106 + return $value;
  107 + }
  108 + return strtotime($value);
  109 + }
  110 +
  111 + /**
  112 + * post_content 自动转化
  113 + * @param $value
  114 + * @return string
  115 + */
  116 + public function getPostContentAttr($value)
  117 + {
  118 + return cmf_replace_content_file_url(htmlspecialchars_decode($value));
  119 + }
  120 +
  121 + /**
  122 + * post_content 自动转化
  123 + * @param $value
  124 + * @return string
  125 + */
  126 + public function setPostContentAttr($value)
  127 + {
  128 + return htmlspecialchars(cmf_replace_content_file_url(htmlspecialchars_decode($value), true));
  129 + }
  130 +
  131 + /**
  132 + * more 自动转化
  133 + * @param $value
  134 + * @return array
  135 + */
  136 + public function getMoreAttr($value)
  137 + {
  138 + $more = json_decode($value, true);
  139 + if (!empty($more['thumbnail'])) {
  140 + $more['thumbnail'] = cmf_get_image_url($more['thumbnail']);
  141 + }
  142 +
  143 + if (!empty($more['photos'])) {
  144 + foreach ($more['photos'] as $key => $value) {
  145 + $more['photos'][$key]['url'] = cmf_get_image_url($value['url']);
  146 + }
  147 + }
  148 +
  149 + if (!empty($more['files'])) {
  150 + foreach ($more['files'] as $key => $value) {
  151 + $more['files'][$key]['url'] = cmf_get_image_url($value['url']);
  152 + }
  153 + }
  154 + return $more;
  155 + }
  156 +
  157 + /**
  158 + * 获取用户文章
  159 + */
  160 + public function getUserArticles($userId, $params)
  161 + {
  162 + $where = [
  163 + 'post_type' => 1,
  164 + 'user_id' => $userId
  165 + ];
  166 +
  167 + $params['where'] = $where;
  168 +
  169 + return $this->getDatas($params);;
  170 + }
  171 +
  172 + /**
  173 + * 会员添加文章
  174 + * @param array $data 文章数据
  175 + * @return $this
  176 + */
  177 + public function addArticle($data)
  178 + {
  179 + if (!empty($data['more'])) {
  180 + $data['more'] = $this->setMoreUrl($data['more']);
  181 + }
  182 + if (!empty($data['thumbnail'])) {
  183 + $data['more']['thumbnail'] = cmf_asset_relative_url($data['thumbnail']);
  184 + }
  185 + $this->allowField(true)->data($data, true)->isUpdate(false)->save();
  186 + $categories = $this->strToArr($data['categories']);
  187 + $this->categories()->attach($categories);
  188 + if (!empty($data['post_keywords']) && is_string($data['post_keywords'])) {
  189 + //加入标签
  190 + $data['post_keywords'] = str_replace(',', ',', $data['post_keywords']);
  191 + $keywords = explode(',', $data['post_keywords']);
  192 + $this->addTags($keywords, $this->id);
  193 + }
  194 + return $this;
  195 + }
  196 +
  197 + /**
  198 + * 会员文章编辑
  199 + * @param array $data 文章数据
  200 + * @param int $id 文章id
  201 + * @param int $userId 文章所属用户id [可选]
  202 + * @return boolean 成功 true 失败 false
  203 + */
  204 + public function editArticle($data, $id, $userId = '')
  205 + {
  206 + if (!empty($userId)) {
  207 + $isBelong = $this->isuserPost($id, $userId);
  208 + if ($isBelong === false) {
  209 + return $isBelong;
  210 + }
  211 + }
  212 + if (!empty($data['more'])) {
  213 + $data['more'] = $this->setMoreUrl($data['more']);
  214 + }
  215 + if (!empty($data['thumbnail'])) {
  216 + $data['more']['thumbnail'] = cmf_asset_relative_url($data['thumbnail']);
  217 + }
  218 + $data['id'] = $id;
  219 + $data['post_status'] = empty($data['post_status']) ? 0 : 1;
  220 + $data['is_top'] = empty($data['is_top']) ? 0 : 1;
  221 + $data['recommended'] = empty($data['recommended']) ? 0 : 1;
  222 + $this->allowField(true)->data($data, true)->isUpdate(true)->save();
  223 +
  224 + $categories = $this->strToArr($data['categories']);
  225 + $oldCategoryIds = $this->categories()->column('category_id');
  226 + $sameCategoryIds = array_intersect($categories, $oldCategoryIds);
  227 + $needDeleteCategoryIds = array_diff($oldCategoryIds, $sameCategoryIds);
  228 + $newCategoryIds = array_diff($categories, $sameCategoryIds);
  229 + if (!empty($needDeleteCategoryIds)) {
  230 + $this->categories()->detach($needDeleteCategoryIds);
  231 + }
  232 +
  233 + if (!empty($newCategoryIds)) {
  234 + $this->categories()->attach(array_values($newCategoryIds));
  235 + }
  236 +
  237 + $keywords = [];
  238 +
  239 + if (!empty($data['post_keywords'])) {
  240 + if (is_string($data['post_keywords'])) {
  241 + //加入标签
  242 + $data['post_keywords'] = str_replace(',', ',', $data['post_keywords']);
  243 + $keywords = explode(',', $data['post_keywords']);
  244 + }
  245 + }
  246 +
  247 + $this->addTags($keywords, $data['id']);
  248 +
  249 + return $this;
  250 + }
  251 +
  252 + /**
  253 + * 根据文章关键字,增加标签
  254 + * @param array $keywords 文章关键字数组
  255 + * @param int $articleId 文章id
  256 + * @return void
  257 + */
  258 + public function addTags($keywords, $articleId)
  259 + {
  260 + foreach ($keywords as $key => $value) {
  261 + $keywords[$key] = trim($value);
  262 + }
  263 + $continue = true;
  264 + $names = $this->tags()->column('name');
  265 + if (!empty($keywords) || !empty($names)) {
  266 + if (!empty($names)) {
  267 + $sameNames = array_intersect($keywords, $names);
  268 + $keywords = array_diff($keywords, $sameNames);
  269 + $shouldDeleteNames = array_diff($names, $sameNames);
  270 + if (!empty($shouldDeleteNames)) {
  271 + $tagIdNames = $this->tags()
  272 + ->where('name', 'in', $shouldDeleteNames)
  273 + ->column('pivot.id', 'tag_id');
  274 + $tagIds = array_keys($tagIdNames);
  275 + $tagPostIds = array_values($tagIdNames);
  276 + $tagPosts = DB::name('portal_tag_post')->where('tag_id', 'in', $tagIds)
  277 + ->field('id,tag_id,post_id')
  278 + ->select();
  279 + $keepTagIds = [];
  280 + foreach ($tagPosts as $key => $tagPost) {
  281 + if ($articleId != $tagPost['post_id']) {
  282 + array_push($keepTagIds, $tagPost['tag_id']);
  283 + }
  284 + }
  285 + $keepTagIds = array_unique($keepTagIds);
  286 + $shouldDeleteTagIds = array_diff($tagIds, $keepTagIds);
  287 + DB::name('PortalTag')->delete($shouldDeleteTagIds);
  288 + DB::name('PortalTagPost')->delete($tagPostIds);
  289 + }
  290 + } else {
  291 + $tagIdNames = DB::name('portal_tag')->where('name', 'in', $keywords)->column('name', 'id');
  292 + if (!empty($tagIdNames)) {
  293 + $tagIds = array_keys($tagIdNames);
  294 + $this->tags()->attach($tagIds);
  295 + $keywords = array_diff($keywords, array_values($tagIdNames));
  296 + if (empty($keywords)) {
  297 + $continue = false;
  298 + }
  299 + }
  300 + }
  301 + if ($continue) {
  302 + foreach ($keywords as $key => $value) {
  303 + if (!empty($value)) {
  304 + $this->tags()->attach(['name' => $value]);
  305 + }
  306 + }
  307 + }
  308 + }
  309 + }
  310 +
  311 + /**
  312 + * 获取图片附件url相对地址
  313 + * 默认上传名字 *_names 地址 *_urls
  314 + * @param $annex 上传附件
  315 + * @return array
  316 + */
  317 + public function setMoreUrl($annex)
  318 + {
  319 + $more = [];
  320 + if (!empty($annex)) {
  321 + foreach ($annex as $key => $value) {
  322 + $nameArr = $key . '_names';
  323 + $urlArr = $key . '_urls';
  324 + if (is_string($value[$nameArr]) && is_string($value[$urlArr])) {
  325 + $more[$key] = [$value[$nameArr], $value[$urlArr]];
  326 + } elseif (!empty($value[$nameArr]) && !empty($value[$urlArr])) {
  327 + $more[$key] = [];
  328 + foreach ($value[$urlArr] as $k => $url) {
  329 + $url = cmf_asset_relative_url($url);
  330 + array_push($more[$key], ['url' => $url, 'name' => $value[$nameArr][$k]]);
  331 + }
  332 + }
  333 + }
  334 + }
  335 + return $more;
  336 + }
  337 +
  338 + /**
  339 + * 删除文章
  340 + * @param $ids int|array 文章id
  341 + * @param int $userId 文章所属用户id [可选]
  342 + * @return bool|int 删除结果 true 成功 false 失败 -1 文章不存在
  343 + */
  344 + public function deleteArticle($ids, $userId = '')
  345 + {
  346 + $time = time();
  347 + $result = false;
  348 + $where = [];
  349 +
  350 + if (!empty($userId)) {
  351 + if (is_numeric($ids)) {
  352 + $article = $this->find($ids);
  353 + if (!empty($article)) {
  354 + if ($this->isUserPost($ids, $userId) || $userId == 1) {
  355 + $where['id'] = $ids;
  356 + }
  357 + }
  358 + } else {
  359 + $ids = $this->strToArr($ids);
  360 + $articles = $this->where('id', 'in', $ids)->select();
  361 + if (!empty($articles)) {
  362 + $deleteIds = $this->isUserPosts($ids, $userId);
  363 + if (!empty($deleteIds)) {
  364 + $where['id'] = ['in', $deleteIds];
  365 + }
  366 + }
  367 + }
  368 + } else {
  369 + if (is_numeric($ids)) {
  370 + $article = $this->find($ids);
  371 + if (!empty($article)) {
  372 + $where['id'] = $ids;
  373 + }
  374 + } else {
  375 + $ids = $this->strToArr($ids);
  376 + $articles = $this->where('id', 'in', $ids)->select();
  377 + if (!empty($articles)) {
  378 + $where['id'] = ['in', $ids];
  379 + }
  380 + }
  381 + }
  382 + if (empty($article) && empty($articles)) {
  383 + return -1;
  384 + }
  385 + if (!empty($where)) {
  386 + $result = $this->useGlobalScope(false)
  387 + ->where($where)
  388 + ->setField('delete_time', $time);
  389 + }
  390 + if ($result) {
  391 + $data = [
  392 + 'create_time' => $time,
  393 + 'table_name' => 'portal_post'
  394 + ];
  395 + if (!empty($article)) {
  396 + $data['name'] = $article['post_title'];
  397 + $article->recycleBin()->save($data);
  398 + }
  399 +
  400 + if (!empty($articles)) {
  401 + foreach ($articles as $article) {
  402 + $data['name'] = $article['post_title'];
  403 + $article->recycleBin()->save($data);
  404 + }
  405 + }
  406 + }
  407 + return $result;
  408 + }
  409 +
  410 + /**
  411 + * 判断文章所属用户是否为当前用户,超级管理员除外
  412 + * @params int $id 文章id
  413 + * @param int $userId 当前用户id
  414 + * @return boolean 是 true , 否 false
  415 + */
  416 + public function isUserPost($id, $userId)
  417 + {
  418 + $postUserId = $this->useGlobalScope(false)
  419 + ->getFieldById($id, 'user_id');
  420 + if ($postUserId != $userId || $userId != 1) {
  421 + return false;
  422 + } else {
  423 + return true;
  424 + }
  425 + }
  426 +
  427 + /**
  428 + * 过滤属于当前用户的文章,超级管理员除外
  429 + * @params array $ids 文章id的数组
  430 + * @param int $userId 当前用户id
  431 + * @return array 属于当前用户的文章id
  432 + */
  433 + public function isUserPosts($ids, $userId)
  434 + {
  435 + $postIds = $this->useGlobalScope(false)
  436 + ->where('user_id', $userId)
  437 + ->where('id', 'in', $ids)
  438 + ->column('id');
  439 + return array_intersect($ids, $postIds);
  440 + }
  441 +}
  1 +<?php
  2 +// +----------------------------------------------------------------------
  3 +// | bronet [ 以客户为中心 以奋斗者为本 ]
  4 +// +----------------------------------------------------------------------
  5 +// | Copyright (c) 2013-2017 http://www.bronet.cn All rights reserved.
  6 +// +----------------------------------------------------------------------
  7 +
  8 +namespace api\portal\model;
  9 +
  10 +use api\common\model\CommonModel;
  11 +class PortalTagModel extends CommonModel
  12 +{
  13 + //可查询字段
  14 + protected $visible = [
  15 + 'id','articles.id','recommended', 'post_count', 'name','articles'
  16 + ];
  17 + //模型关联方法
  18 + protected $relationFilter = ['articles'];
  19 +
  20 + /**
  21 + * 基础查询
  22 + */
  23 + protected function base($query)
  24 + {
  25 + $query->alias('post_tag')->where('post_tag.status', 1);
  26 + }
  27 + /**
  28 + * 关联 文章表
  29 + * @return $this
  30 + */
  31 + public function articles()
  32 + {
  33 + return $this->belongsToMany('PortalPostModel','portal_tag_post','post_id','tag_id');
  34 + }
  35 +}
  1 +<?php
  2 +// +----------------------------------------------------------------------
  3 +// | bronet [ 以客户为中心 以奋斗者为本 ]
  4 +// +----------------------------------------------------------------------
  5 +// | Copyright (c) 2013-2017 http://www.bronet.cn All rights reserved.
  6 +// +----------------------------------------------------------------------
  7 +
  8 +namespace api\portal\model;
  9 +
  10 +use think\Model;
  11 +
  12 +class PortalTagPostModel extends Model
  13 +{
  14 + /**
  15 + * 获取指定id相关的文章id数组
  16 + * @param $post_id 文章id
  17 + * @return array 相关的文章id
  18 + */
  19 + function getRelationPostIds($post_id)
  20 + {
  21 + $tagIds = $this->where('post_id', $post_id)
  22 + ->column('tag_id');
  23 + $postIds = $this->whereIn('tag_id', $tagIds)
  24 + ->column('post_id');
  25 + return array_unique($postIds);
  26 + }
  27 +}
  1 +<?php
  2 +// +----------------------------------------------------------------------
  3 +// | bronet [ 以客户为中心 以奋斗者为本 ]
  4 +// +----------------------------------------------------------------------
  5 +// | Copyright (c) 2013-2017 http://www.bronet.cn All rights reserved.
  6 +// +----------------------------------------------------------------------
  7 +namespace api\portal\model;
  8 +
  9 +use think\Model;
  10 +
  11 +class RecycleBinModel extends Model
  12 +{
  13 +
  14 +}
  1 +<?php
  2 +// +----------------------------------------------------------------------
  3 +// | bronet [ 以客户为中心 以奋斗者为本 ]
  4 +// +----------------------------------------------------------------------
  5 +// | Copyright (c) 2013-2017 http://www.bronet.cn All rights reserved.
  6 +// +----------------------------------------------------------------------
  7 +
  8 +namespace api\portal\model;
  9 +use api\common\model\CommonModel;
  10 +
  11 +class UserModel extends CommonModel
  12 +{
  13 + //可查询字段
  14 +// protected $visible = [
  15 +// 'articles.id', 'user_nickname', 'avatar', 'signature','user'
  16 +// ];
  17 + //模型关联方法
  18 + protected $relationFilter = ['user'];
  19 +
  20 + /**
  21 + * 基础查询
  22 + */
  23 + protected function base($query)
  24 + {
  25 + $query->where('cmf_user.user_status', 1);
  26 + }
  27 +
  28 + /**
  29 + * more 自动转化
  30 + * @param $value
  31 + * @return array
  32 + */
  33 + public function getAvatarAttr($value)
  34 + {
  35 + $value = !empty($value) ? cmf_get_image_url($value) : $value;
  36 + return $value;
  37 + }
  38 +
  39 + /**
  40 + * 关联 user表
  41 + * @return $this
  42 + */
  43 + public function user()
  44 + {
  45 + return $this->belongsTo('UserModel', 'user_id')->setEagerlyType(1);
  46 + }
  47 +}
  1 +<?php
  2 +
  3 +use think\Route;
  4 +
  5 +Route::resource('portal/categories', 'portal/Categories');
  6 +Route::resource('portal/articles', 'portal/Articles');
  7 +Route::resource('portal/pages', 'portal/Pages');
  8 +Route::resource('portal/userArticles', 'portal/UserArticles');
  9 +
  10 +Route::get('portal/search','portal/Articles/search');
  11 +Route::get('portal/articles/my', 'portal/Articles/my');
  12 +Route::get('portal/tags/:id/articles', 'portal/Tags/articles');
  13 +Route::get('portal/tags', 'portal/Tags/index');
  14 +
  15 +Route::post('portal/userArticles/deletes','portal/UserArticles/deletes');
  1 +<?php
  2 +// +----------------------------------------------------------------------
  3 +// | bronet [ 以客户为中心 以奋斗者为本 ]
  4 +// +----------------------------------------------------------------------
  5 +// | Copyright (c) 2013-2017 http://www.bronet.cn All rights reserved.
  6 +// +----------------------------------------------------------------------
  7 +namespace api\portal\service;
  8 +
  9 +use api\portal\model\PortalPostModel as PortalPost;
  10 +use api\portal\model\PortalCategoryModel as PortalCategory;
  11 +
  12 +class PortalPostModel extends PortalPost
  13 +{
  14 + protected $name = "portal_post";
  15 +
  16 + /**
  17 + * [recommendedList 推荐列表]
  18 + * @Author: wuwu<15093565100@163.com>
  19 + * @DateTime: 2017-07-17T11:06:47+0800
  20 + * @since: 1.0
  21 + * @param integer $next_id [最后索引值]
  22 + * @param integer $num [一页多少条 默认10]
  23 + * @return [type] [数据]
  24 + */
  25 + public static function recommendedList($next_id = 0, $num = 10)
  26 + {
  27 + $limit = "{$next_id},{$num}";
  28 + $field = 'id,recommended,user_id,post_like,post_hits,comment_count,create_time,update_time,published_time,post_title,post_excerpt,more';
  29 + $list = self::with('user')->field($field)->where('recommended', 1)->order('published_time DESC')->limit($limit)->select();
  30 + return $list;
  31 + }
  32 +
  33 + /**
  34 + * [categoryPostList 分类文章列表]
  35 + * @Author: wuwu<15093565100@163.com>
  36 + * @DateTime: 2017-07-17T15:16:26+0800
  37 + * @since: 1.0
  38 + * @param [type] $category_id [分类ID]
  39 + * @param integer $next_id [limit索引]
  40 + * @param integer $num [limit每页数量]
  41 + * @return [type] [description]
  42 + */
  43 + public static function categoryPostList($category_id, $next_id = 0, $num = 10)
  44 + {
  45 + $limit = "{$next_id},{$num}";
  46 + $Postlist = PortalCategory::categoryPostIds($category_id);
  47 + $field = 'id,recommended,user_id,post_like,post_hits,comment_count,create_time,update_time,published_time,post_title,post_excerpt,more';
  48 + $list = self::with('user')->field($field)->whereIn('id', $Postlist['PostIds'])->order('published_time DESC')->limit($limit)->select()->toJson();
  49 + return $list;
  50 + }
  51 +}
  1 +<?php
  2 +// +----------------------------------------------------------------------
  3 +// | bronet [ 以客户为中心 以奋斗者为本 ]
  4 +// +----------------------------------------------------------------------
  5 +// | Copyright (c) 2013-2017 http://www.bronet.cn All rights reserved.
  6 +// +----------------------------------------------------------------------
  7 +namespace api\portal\validate;
  8 +
  9 +use think\Validate;
  10 +
  11 +class ArticlesValidate extends Validate
  12 +{
  13 + protected $rule = [
  14 + 'post_title' => 'require',
  15 + 'post_content' => 'require',
  16 + 'categories' => 'require'
  17 + ];
  18 + protected $message = [
  19 + 'post_title.require' => '文章标题不能为空',
  20 + 'post_content.require' => '内容不能为空',
  21 + 'categories.require' => '文章分类不能为空'
  22 + ];
  23 +
  24 + protected $scene = [
  25 + 'article' => [ 'post_title' , 'post_content' , 'categories' ],
  26 + 'page' => ['post_title']
  27 + ];
  28 +}
  1 +<?php
  2 +// +----------------------------------------------------------------------
  3 +// | bronet [ 以客户为中心 以奋斗者为本 ]
  4 +// +----------------------------------------------------------------------
  5 +// | Copyright (c) 2013-2017 http://www.bronet.cn All rights reserved.
  6 +// +----------------------------------------------------------------------
  7 +
  8 +return [
  9 + // 应用调试模式
  10 + 'app_debug' => false,
  11 + // 应用Trace
  12 + 'app_trace' => false,
  13 +
  14 +];
  1 +<?php
  2 +// +----------------------------------------------------------------------
  3 +// | bronet [ 以客户为中心 以奋斗者为本 ]
  4 +// +----------------------------------------------------------------------
  5 +// | Copyright (c) 2013-2017 http://www.bronet.cn All rights reserved.
  6 +// +----------------------------------------------------------------------
  7 +$apps = cmf_scan_dir(APP_PATH . '*', GLOB_ONLYDIR);
  8 +
  9 +foreach ($apps as $app) {
  10 + $routeFile = APP_PATH . $app . '/route.php';
  11 +
  12 + if (file_exists($routeFile)) {
  13 + include_once $routeFile;
  14 + }
  15 +
  16 +}
  17 +
  18 +
  19 +return [
  20 +];
  1 +<?php
  2 +// +----------------------------------------------------------------------
  3 +// | bronet [ 以客户为中心 以奋斗者为本 ]
  4 +// +----------------------------------------------------------------------
  5 +// | Copyright (c) 2013-2017 http://www.bronet.cn All rights reserved.
  6 +// +----------------------------------------------------------------------
  7 +
  8 +// 应用行为扩展定义文件
  9 +return [
  10 + // 应用初始化
  11 + 'app_init' => [
  12 + 'cmf\\behavior\\InitHookBehavior',
  13 + ],
  14 + // 应用开始
  15 + 'app_begin' => [
  16 + 'cmf\\behavior\\LangBehavior',
  17 + ],
  18 + // 模块初始化
  19 + 'module_init' => [],
  20 + // 操作开始执行
  21 + 'action_begin' => [],
  22 + // 视图内容过滤
  23 + 'view_filter' => [],
  24 + // 日志写入
  25 + 'log_write' => [],
  26 + // 应用结束
  27 + 'app_end' => [],
  28 +];
  1 +<?php
  2 +// +----------------------------------------------------------------------
  3 +// | 文件说明:评论
  4 +// +----------------------------------------------------------------------
  5 +// | Copyright (c) 2017 http://www.bronet.cn All rights reserved.
  6 +// +----------------------------------------------------------------------
  7 +// | Date: 2017-7-26
  8 +// +----------------------------------------------------------------------
  9 +namespace api\user\controller;
  10 +
  11 +use api\user\model\CommentModel as Comment;
  12 +use api\user\model\UserModel as User;
  13 +use cmf\controller\RestUserBaseController;
  14 +
  15 +class CommentsController extends RestUserBaseController
  16 +{
  17 +
  18 + /**
  19 + * [getUserComments 获取用户评论]
  20 + * @Author: wuwu<15093565100@163.com>
  21 + * @DateTime: 2017-05-25T20:48:53+0800
  22 + * @since: 1.0
  23 + * @return [array_json] [获取Comment]
  24 + */
  25 + public function getUserComments()
  26 + {
  27 + $input = $this->request->param();
  28 +
  29 + $comment = new Comment();
  30 + $map['where']['user_id'] = $this->getUserId();
  31 + $map['order'] = '-create_time';
  32 + $map['relation'] = 'user,to_user';
  33 + if (!empty($input['page'])) {
  34 + $map['page'] = $input['page'];
  35 + }
  36 + //处理不同的情况
  37 + $data = $comment->getDatas($map);
  38 + $this->success('请求成功', $data);
  39 +
  40 + }
  41 +
  42 + /**
  43 + * [getComments 获取评论]
  44 + * @Author: wuwu<15093565100@163.com>
  45 + * @DateTime: 2017-05-25T20:48:53+0800
  46 + * @since: 1.0
  47 + * @return [array_json] [获取Comment]
  48 + */
  49 + public function getComments()
  50 + {
  51 + $input = $this->request->param();
  52 + $id = $this->request->has('object_id') ? $input['object_id'] : $this->error('id参数不存在');
  53 + $table = $this->request->has('table_name') ? $input['table_name'] : $this->error('table参数不存在');
  54 + $comment = new Comment();
  55 + $map['where'] = [
  56 + 'object_id' => $id,
  57 + 'table_name' => $table
  58 + ];
  59 + $map['relation'] = 'user,to_user';
  60 +
  61 + if (!empty($input['page'])) {
  62 + $map['page'] = $input['page'];
  63 + }
  64 +
  65 + $data = $comment->getDatas($map);
  66 + //数据是否存在
  67 + if ($data->isEmpty()) {
  68 + $this->error('评论数据为空');
  69 + } else {
  70 + $this->success('评论获取成功!', $data);
  71 + }
  72 + }
  73 +
  74 + /**
  75 + * [delComments 删除评论]
  76 + * @Author: wuwu<15093565100@163.com>
  77 + * @DateTime: 2017-08-11T22:08:56+0800
  78 + * @since: 1.0
  79 + * @return
  80 + */
  81 + public function delComments()
  82 + {
  83 + $input = $this->request->param();
  84 + $id = $this->request->has('id') ? intval($input['id']) : $this->error('id参数不存在');
  85 + $userId = $this->getUserId();
  86 + Comment::destroy(['id' => $id, 'user_id' => $userId]);
  87 +
  88 + $this->success('删除成功');
  89 + }
  90 +
  91 + /**
  92 + * [setComments 添加评论]
  93 + * @Author: wuwu<15093565100@163.com>
  94 + * @DateTime: 2017-08-16T01:07:44+0800
  95 + * @since: 1.0
  96 + */
  97 + public function setComments()
  98 + {
  99 + $data = $this->_setComments();
  100 + if ($res = Comment::setComment($data)) {
  101 + $this->success('评论成功', $res);
  102 + } else {
  103 + $this->error('评论失败');
  104 + }
  105 + }
  106 +
  107 + /**
  108 + * [_setComments 评论数据组织]
  109 + * @Author: wuwu<15093565100@163.com>
  110 + * @DateTime: 2017-08-16T01:00:02+0800
  111 + * @since: 1.0
  112 + */
  113 + protected function _setComments()
  114 + {
  115 + $input = $this->request->param();
  116 + $data['object_id'] = $this->request->has('object_id') ? $input['object_id'] : $this->error('object_id参数不存在');
  117 + $data['table_name'] = $this->request->has('table_name') ? $input['table_name'] : $this->error('table_name参数不存在');
  118 + $data['url'] = $this->request->has('url') ? $input['url'] : $this->error('url参数不存在');
  119 + $data['content'] = $this->request->has('content') ? $input['content'] : $this->error('内容不为空');
  120 + $data['parent_id'] = $this->request->has('parent_id') ? $input['parent_id'] : 0;
  121 + $result = $this->validate($data,
  122 + [
  123 + 'object_id' => 'require|number',
  124 + 'content' => 'require',
  125 + ]);
  126 + if (true !== $result) {
  127 + // 验证失败 输出错误信息
  128 + $this->error($result);
  129 + }
  130 + $data['delete_time'] = 0;
  131 + $data['create_time'] = time();
  132 + if ($data['parent_id']) {
  133 + $res = Comment::field('parent_id', 'path', 'user_id')->find($data['parent_id']);
  134 + if ($res) {
  135 + $data['path'] = $res['path'] . $data['parent_id'] . ',';
  136 + $data['to_user_id'] = $res['user_id'];
  137 + } else {
  138 + $this->error('回复的评论不存在');
  139 + }
  140 + } else {
  141 + $data['path'] = '0,';
  142 + }
  143 + $data['user_id'] = $this->getUserId();
  144 + $userData = User::field(true)->find($data['user_id']);
  145 + if (!$userData) {
  146 + $this->error('评论用户不存在');
  147 + }
  148 +
  149 + $data['full_name'] = $userData['user_nickname'];
  150 + $data['email'] = $userData['user_email'];
  151 + return $data;
  152 + }
  153 +}
  1 +<?php
  2 +// +----------------------------------------------------------------------
  3 +// | bronet [ 以客户为中心 以奋斗者为本 ]
  4 +// +----------------------------------------------------------------------
  5 +// | Copyright (c) 2013-2017 http://www.bronet.cn All rights reserved.
  6 +// +----------------------------------------------------------------------
  7 +
  8 +namespace api\user\controller;
  9 +
  10 +use api\user\model\UserFavoriteModel;
  11 +use cmf\controller\RestUserBaseController;
  12 +
  13 +class FavoritesController extends RestUserBaseController
  14 +{
  15 + protected $userFavoriteModel;
  16 +
  17 + public function __construct(UserFavoriteModel $userFavoriteModel)
  18 + {
  19 + parent::__construct();
  20 + $this->userFavoriteModel = $userFavoriteModel;
  21 + }
  22 +
  23 + /**
  24 + * 显示收藏列表
  25 + */
  26 + public function getFavorites()
  27 + {
  28 + $userId = $this->getUserId();
  29 +
  30 + $param = $this->request->param();
  31 + $param['where'] = [
  32 + 'user_id' => $userId
  33 + ];
  34 + $param['order'] = '-create_time';
  35 +
  36 + $favoriteData = $this->userFavoriteModel->getDatas($param);
  37 + $this->success('请求成功', $favoriteData);
  38 + }
  39 +
  40 + /**
  41 + * [setFavorites 添加收藏]
  42 + * @Author: wuwu<15093565100@163.com>
  43 + * @DateTime: 2017-08-03T09:03:40+0800
  44 + * @since: 1.0
  45 + */
  46 + public function setFavorites()
  47 + {
  48 + $input = $this->request->param();
  49 +
  50 + //组装数据
  51 + $data = $this->_FavoritesObject($input['title'], $input['url'], $input['description'], $input['table_name'], $input['object_id']);
  52 + if (!$data) {
  53 + $this->error('收藏失败');
  54 + }
  55 + if ($this->userFavoriteModel->where('object_id', $input['object_id'])->where('table_name', $input['table'])->count() > 0) {
  56 + $this->error('已收藏');
  57 + }
  58 + if ($this->userFavoriteModel->setFavorite($data)) {
  59 + $this->success('收藏成功');
  60 + } else {
  61 + $this->error('收藏失败');
  62 + }
  63 +
  64 + }
  65 +
  66 + /**
  67 + * [_FavoritesObject 收藏数据组装]
  68 + * @Author: wuwu<15093565100@163.com>
  69 + * @DateTime: 2017-08-03T09:39:06+0800
  70 + * @since: 1.0
  71 + * @return [type] [description]
  72 + */
  73 + protected function _FavoritesObject($title, $url, $description, $table_name, $object_id)
  74 + {
  75 + $data['user_id'] = $this->getUserId();
  76 + $data['create_time'] = THINK_START_TIME;
  77 +
  78 + if (empty($title)) {
  79 + return false;
  80 + } else if (empty($url)) {
  81 + return false;
  82 + } elseif (empty($description)) {
  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 +// | bronet [ 以客户为中心 以奋斗者为本 ]
  4 +// +----------------------------------------------------------------------
  5 +// | Copyright (c) 2013-2017 http://www.bronet.cn All rights reserved.
  6 +// +----------------------------------------------------------------------
  7 +namespace api\user\controller;
  8 +
  9 +use cmf\controller\RestUserBaseController;
  10 +use think\Db;
  11 +use think\Validate;
  12 +
  13 +class ProfileController extends RestUserBaseController
  14 +{
  15 + // 用户密码修改
  16 + public function changePassword()
  17 + {
  18 + $validate = new Validate([
  19 + 'old_password' => 'require',
  20 + 'password' => 'require',
  21 + 'confirm_password' => 'require|confirm:password'
  22 + ]);
  23 +
  24 + $validate->message([
  25 + 'old_password.require' => '请输入您的旧密码!',
  26 + 'password.require' => '请输入您的新密码!',
  27 + 'confirm_password.require' => '请输入确认密码!',
  28 + 'confirm_password.confirm' => '两次输入的密码不一致!'
  29 + ]);
  30 +
  31 + $data = $this->request->param();
  32 + if (!$validate->check($data)) {
  33 + $this->error($validate->getError());
  34 + }
  35 +
  36 + $userId = $this->getUserId();
  37 + $userPassword = Db::name("user")->where('id', $userId)->value('user_pass');
  38 +
  39 + if (!cmf_compare_password($data['old_password'], $userPassword)) {
  40 + $this->error('旧密码不正确!');
  41 + }
  42 +
  43 + Db::name("user")->where('id', $userId)->update(['user_pass' => cmf_password($data['password'])]);
  44 +
  45 + $this->success("密码修改成功!");
  46 +
  47 + }
  48 +
  49 + // 用户绑定邮箱
  50 + public function bindingEmail()
  51 + {
  52 + $validate = new Validate([
  53 + 'email' => 'require|email|unique:user,user_email',
  54 + 'verification_code' => 'require'
  55 + ]);
  56 +
  57 + $validate->message([
  58 + 'email.require' => '请输入您的邮箱!',
  59 + 'email.email' => '请输入正确的邮箱格式!',
  60 + 'email.unique' => '正确账号已存在!',
  61 + 'verification_code.require' => '请输入数字验证码!'
  62 + ]);
  63 +
  64 + $data = $this->request->param();
  65 + if (!$validate->check($data)) {
  66 + $this->error($validate->getError());
  67 + }
  68 +
  69 + $userId = $this->getUserId();
  70 + $userEmail = Db::name("user")->where('id', $userId)->value('user_email');
  71 +
  72 + if (!empty($userEmail)) {
  73 + $this->error("您已经绑定邮箱!");
  74 + }
  75 +
  76 + $errMsg = cmf_check_verification_code($data['email'], $data['verification_code']);
  77 + if (!empty($errMsg)) {
  78 + $this->error($errMsg);
  79 + }
  80 +
  81 + Db::name("user")->where('id', $userId)->update(['user_email' => $data['email']]);
  82 +
  83 + $this->success("绑定成功!");
  84 + }
  85 +
  86 + // 用户绑定手机号
  87 + public function bindingMobile()
  88 + {
  89 + $validate = new Validate([
  90 + 'mobile' => 'require|unique:user,mobile',
  91 + 'verification_code' => 'require'
  92 + ]);
  93 +
  94 + $validate->message([
  95 + 'mobile.require' => '请输入您的手机号!',
  96 + 'mobile.unique' => '手机号已经存在!',
  97 + 'verification_code.require' => '请输入数字验证码!'
  98 + ]);
  99 +
  100 + $data = $this->request->param();
  101 + if (!$validate->check($data)) {
  102 + $this->error($validate->getError());
  103 + }
  104 +
  105 + if (!preg_match('/(^(13\d|15[^4\D]|17[13678]|18\d)\d{8}|170[^346\D]\d{7})$/', $data['mobile'])) {
  106 + $this->error("请输入正确的手机格式!");
  107 + }
  108 +
  109 +
  110 + $userId = $this->getUserId();
  111 + $mobile = Db::name("user")->where('id', $userId)->value('mobile');
  112 +
  113 + if (!empty($mobile)) {
  114 + $this->error("您已经绑定手机!");
  115 + }
  116 +
  117 + $errMsg = cmf_check_verification_code($data['mobile'], $data['verification_code']);
  118 + if (!empty($errMsg)) {
  119 + $this->error($errMsg);
  120 + }
  121 +
  122 + Db::name("user")->where('id', $userId)->update(['mobile' => $data['mobile']]);
  123 +
  124 + $this->success("绑定成功!");
  125 + }
  126 +
  127 + /**
  128 + * 用户基本信息获取及修改
  129 + * @param 请求为GET 获取信息
  130 + * @param [string] $[field] [要获取的一个或多个字段名] 可选
  131 + * @return 带参数,返回某个或多个字段信息。不带参数,返回所有信息
  132 + * @param 请求为POST 修改信息
  133 + */
  134 + public function userInfo($field = '')
  135 + {
  136 + //判断请求为GET,获取信息
  137 + if ($this->request->isGet()) {
  138 + $userId = $this->getUserId();
  139 + $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';
  140 + if (empty($field)) {
  141 + $userData = Db::name("user")->field($fieldStr)->find($userId);
  142 + } else {
  143 + $fieldArr = explode(',', $fieldStr);
  144 + $postFieldArr = explode(',', $field);
  145 + $mixedField = array_intersect($fieldArr, $postFieldArr);
  146 + if (empty($mixedField)) {
  147 + $this->error('您查询的信息不存在!');
  148 + }
  149 + if (count($mixedField) > 1) {
  150 + $fieldStr = implode(',', $mixedField);
  151 + $userData = Db::name("user")->field($fieldStr)->find($userId);
  152 + } else {
  153 + $userData = Db::name("user")->where('id', $userId)->value($mixedField);
  154 + }
  155 + }
  156 + $this->success('获取成功!', $userData);
  157 + }
  158 + //判断请求为POST,修改信息
  159 + if ($this->request->isPost()) {
  160 + $userId = $this->getUserId();
  161 + $fieldStr = 'user_nickname,avatar,signature,user_url,sex,birthday';
  162 + $data = $this->request->post();
  163 + if (empty($data)) {
  164 + $this->error('修改失败,提交表单为空!');
  165 + }
  166 +
  167 + if (!empty($data['birthday'])) {
  168 + $data['birthday'] = strtotime($data['birthday']);
  169 + }
  170 +
  171 + $upData = Db::name("user")->where('id', $userId)->field($fieldStr)->update($data);
  172 + if ($upData !== false) {
  173 + $this->success('修改成功!');
  174 + } else {
  175 + $this->error('修改失败!');
  176 + }
  177 + }
  178 + }
  179 +
  180 +}
  1 +<?php
  2 +// +----------------------------------------------------------------------
  3 +// | bronet [ 以客户为中心 以奋斗者为本 ]
  4 +// +----------------------------------------------------------------------
  5 +// | Copyright (c) 2013-2017 http://www.bronet.cn All rights reserved.
  6 +// +----------------------------------------------------------------------
  7 +namespace api\user\controller;
  8 +
  9 +use think\Db;
  10 +use think\Validate;
  11 +use cmf\controller\RestBaseController;
  12 +
  13 +class PublicController extends RestBaseController
  14 +{
  15 + // 用户注册
  16 + public function register()
  17 + {
  18 + $validate = new Validate([
  19 + 'username' => 'require',
  20 + 'password' => 'require',
  21 + 'verification_code' => 'require'
  22 + ]);
  23 +
  24 + $validate->message([
  25 + 'username.require' => '请输入手机号,邮箱!',
  26 + 'password.require' => '请输入您的密码!',
  27 + 'verification_code.require' => '请输入数字验证码!'
  28 + ]);
  29 +
  30 + $data = $this->request->param();
  31 + if (!$validate->check($data)) {
  32 + $this->error($validate->getError());
  33 + }
  34 +
  35 + $user = [];
  36 +
  37 + $userQuery = Db::name("user");
  38 +
  39 + if (Validate::is($data['username'], 'email')) {
  40 + $user['user_email'] = $data['username'];
  41 + //$userQuery = $userQuery->where('user_email', $data['username']);
  42 + } else if (preg_match('/(^(13\d|15[^4\D]|17[13678]|18\d)\d{8}|170[^346\D]\d{7})$/', $data['username'])) {
  43 + $user['mobile'] = $data['username'];
  44 + //$userQuery = $userQuery->where('mobile', $data['username']);
  45 + } else {
  46 + $this->error("请输入正确的手机或者邮箱格式!");
  47 + }
  48 +
  49 + $errMsg = cmf_check_verification_code($data['username'], $data['verification_code']);
  50 + if (!empty($errMsg)) {
  51 + $this->error($errMsg);
  52 + }
  53 +
  54 + $findUserCount = $userQuery->count();
  55 +
  56 + if ($findUserCount > 0) {
  57 + $this->error("此账号已存在!");
  58 + }
  59 +
  60 + $user['create_time'] = time();
  61 + $user['user_status'] = 1;
  62 + $user['user_type'] = 2;
  63 + $user['user_pass'] = cmf_password($data['password']);
  64 +
  65 + $result = $userQuery->insert($user);
  66 +
  67 +
  68 + if (empty($result)) {
  69 + $this->error("注册失败,请重试!");
  70 + }
  71 +
  72 + $this->success("注册并激活成功,请登录!");
  73 +
  74 + }
  75 +
  76 + // 用户登录 TODO 增加最后登录信息记录,如 ip
  77 + public function login()
  78 + {
  79 + $validate = new Validate([
  80 + 'username' => 'require',
  81 + 'password' => 'require'
  82 + ]);
  83 + $validate->message([
  84 + 'username.require' => '请输入手机号,邮箱或用户名!',
  85 + 'password.require' => '请输入您的密码!'
  86 + ]);
  87 +
  88 + $data = $this->request->param();
  89 + if (!$validate->check($data)) {
  90 + $this->error($validate->getError());
  91 + }
  92 +
  93 + $userQuery = Db::name("user");
  94 + if (Validate::is($data['username'], 'email')) {
  95 + $userQuery = $userQuery->where('user_email', $data['username']);
  96 + } else if (preg_match('/(^(13\d|15[^4\D]|17[13678]|18\d)\d{8}|170[^346\D]\d{7})$/', $data['username'])) {
  97 + $userQuery = $userQuery->where('mobile', $data['username']);
  98 + } else {
  99 + $userQuery = $userQuery->where('user_login', $data['username']);
  100 + }
  101 +
  102 + $findUser = $userQuery->find();
  103 +
  104 + if (empty($findUser)) {
  105 + $this->error("用户不存在!");
  106 + } else {
  107 +
  108 + switch ($findUser['user_status']) {
  109 + case 0:
  110 + $this->error('您已被拉黑!');
  111 + case 2:
  112 + $this->error('账户还没有验证成功!');
  113 + }
  114 +
  115 + if (!cmf_compare_password($data['password'], $findUser['user_pass'])) {
  116 + $this->error("密码不正确!");
  117 + }
  118 + }
  119 +
  120 + $allowedDeviceTypes = ['mobile', 'android', 'iphone', 'ipad', 'web', 'pc', 'mac'];
  121 +
  122 + if (empty($data['device_type']) || !in_array($data['device_type'], $allowedDeviceTypes)) {
  123 + $this->error("请求错误,未知设备!");
  124 + }
  125 +
  126 + $userTokenQuery = Db::name("user_token")
  127 + ->where('user_id', $findUser['id'])
  128 + ->where('device_type', $data['device_type']);
  129 + $findUserToken = $userTokenQuery->find();
  130 + $currentTime = time();
  131 + $expireTime = $currentTime + 24 * 3600 * 180;
  132 + $token = md5(uniqid()) . md5(uniqid());
  133 + if (empty($findUserToken)) {
  134 + $result = $userTokenQuery->insert([
  135 + 'token' => $token,
  136 + 'user_id' => $findUser['id'],
  137 + 'expire_time' => $expireTime,
  138 + 'create_time' => $currentTime,
  139 + 'device_type' => $data['device_type']
  140 + ]);
  141 + } else {
  142 + $result = $userTokenQuery
  143 + ->where('user_id', $findUser['id'])
  144 + ->where('device_type', $data['device_type'])
  145 + ->update([
  146 + 'token' => $token,
  147 + 'expire_time' => $expireTime,
  148 + 'create_time' => $currentTime
  149 + ]);
  150 + }
  151 +
  152 +
  153 + if (empty($result)) {
  154 + $this->error("登录失败!");
  155 + }
  156 +
  157 + $this->success("登录成功!", ['token' => $token]);
  158 + }
  159 +
  160 + // 用户退出
  161 + public function logout()
  162 + {
  163 + $userId = $this->getUserId();
  164 + Db::name('user_token')->where([
  165 + 'token' => $this->token,
  166 + 'user_id' => $userId,
  167 + 'device_type' => $this->deviceType
  168 + ])->update(['token' => '']);
  169 +
  170 + $this->success("退出成功!");
  171 + }
  172 +
  173 + // 用户密码重置
  174 + public function passwordReset()
  175 + {
  176 + $validate = new Validate([
  177 + 'username' => 'require',
  178 + 'password' => 'require',
  179 + 'verification_code' => 'require'
  180 + ]);
  181 +
  182 + $validate->message([
  183 + 'username.require' => '请输入手机号,邮箱!',
  184 + 'password.require' => '请输入您的密码!',
  185 + 'verification_code.require' => '请输入数字验证码!'
  186 + ]);
  187 +
  188 + $data = $this->request->param();
  189 + if (!$validate->check($data)) {
  190 + $this->error($validate->getError());
  191 + }
  192 +
  193 + $userWhere = [];
  194 + if (Validate::is($data['username'], 'email')) {
  195 + $userWhere['user_email'] = $data['username'];
  196 + } else if (preg_match('/(^(13\d|15[^4\D]|17[13678]|18\d)\d{8}|170[^346\D]\d{7})$/', $data['username'])) {
  197 + $userWhere['mobile'] = $data['username'];
  198 + } else {
  199 + $this->error("请输入正确的手机或者邮箱格式!");
  200 + }
  201 +
  202 + $errMsg = cmf_check_verification_code($data['username'], $data['verification_code']);
  203 + if (!empty($errMsg)) {
  204 + $this->error($errMsg);
  205 + }
  206 +
  207 + $userPass = cmf_password($data['password']);
  208 + Db::name("user")->where($userWhere)->update(['user_pass' => $userPass]);
  209 +
  210 + $this->success("密码重置成功,请使用新密码登录!");
  211 +
  212 + }
  213 +}
  1 +<?php
  2 +// +----------------------------------------------------------------------
  3 +// | bronet [ 以客户为中心 以奋斗者为本 ]
  4 +// +----------------------------------------------------------------------
  5 +// | Copyright (c) 2013-2017 http://www.bronet.cn All rights reserved.
  6 +// +----------------------------------------------------------------------
  7 +namespace api\user\controller;
  8 +
  9 +use cmf\controller\RestUserBaseController;
  10 +use Qiniu\Auth;
  11 +use Qiniu\Storage\UploadManager;
  12 +use think\Db;
  13 +
  14 +/**
  15 + * @title 文件上传
  16 + * @description 文件上传
  17 + * @package api\wxapp\controller
  18 + */
  19 +class UploadController extends RestUserBaseController
  20 +{
  21 +
  22 + /**
  23 + * @title 上传单个文件
  24 + * @description 上传单个文件
  25 + * @author Tiger Yang
  26 + * @url /user/upload/one
  27 + * @method POST
  28 + *
  29 + * @header name:XX-Token require:1 default: desc:登录标识
  30 + * @header name:XX-Device-Type require:0 default:wxapp desc:设备类型
  31 + *
  32 + * @param name:file type:file require:1 other: desc:上传文件
  33 + */
  34 + public function one()
  35 + {
  36 + $file = $this->request->file('file');
  37 + // 移动到框架应用根目录/public/upload/ 目录下
  38 + $info = $file->validate([
  39 + /*'size' => 15678,*/
  40 + 'ext' => 'jpg,png,gif'
  41 + ]);
  42 + $fileMd5 = $info->md5();
  43 + $fileSha1 = $info->sha1();
  44 +
  45 + $findFile = Db::name("asset")->where('file_md5', $fileMd5)->where('file_sha1', $fileSha1)->find();
  46 +
  47 + if (!empty($findFile)) {
  48 + $this->success("上传成功!", ['url' => cmf_get_asset_url($findFile['file_path']), 'filename' => $findFile['filename']]);
  49 + }
  50 + $info = $info->move(ROOT_PATH . 'public' . DS . 'upload');
  51 + if ($info) {
  52 + $saveName = $info->getSaveName();
  53 + $originalName = $info->getInfo('name');//name,type,size
  54 + $fileSize = $info->getInfo('size');
  55 + $suffix = $info->getExtension();
  56 +
  57 + $fileKey = $fileMd5 . md5($fileSha1);
  58 +
  59 + $userId = $this->getUserId();
  60 + Db::name('asset')->insert([
  61 + 'user_id' => $userId,
  62 + 'file_key' => $fileKey,
  63 + 'filename' => $originalName,
  64 + 'file_size' => $fileSize,
  65 + 'file_path' => cmf_get_asset_url($saveName),
  66 + 'file_md5' => $fileMd5,
  67 + 'file_sha1' => $fileSha1,
  68 + 'create_time' => time(),
  69 + 'suffix' => $suffix
  70 + ]);
  71 +
  72 + $storage = cmf_get_option('storage');
  73 +
  74 + if (isset($storage['type'])&&$storage['type']=='Qiniu') {
  75 + $this->uploadToQiniu($saveName);
  76 + }
  77 +
  78 + $this->success("上传成功!", ['url' => cmf_get_asset_url($saveName), 'filename' => $originalName]);
  79 + } else {
  80 + // 上传失败获取错误信息
  81 + $this->error($file->getError());
  82 + }
  83 +
  84 + }
  85 +
  86 +
  87 + /**
  88 + * 上传到七牛云
  89 + * @param $save_name
  90 + * @param bool $is_del_local
  91 + * @return array
  92 + * @throws \think\db\exception\DataNotFoundException
  93 + * @throws \think\db\exception\ModelNotFoundException
  94 + * @throws \think\exception\DbException
  95 + */
  96 + public function uploadToQiniu($save_name,$is_del_local=true){
  97 + $plugin=Db::name('plugin')->field('config')->where(['name'=>'Qiniu'])->find();
  98 + $config=json_decode($plugin['config'],true);
  99 +
  100 + $filePath = './../upload/'.$save_name;
  101 + // 上传到七牛后保存的文件名
  102 + $key =$save_name;
  103 + require_once VENDOR_PATH . 'qiniu/php-sdk/autoload.php';
  104 + // 需要填写你的 Access Key 和 Secret Key
  105 + $accessKey = $config['accessKey'];
  106 + $secretKey = $config['secretKey'];
  107 + // 构建鉴权对象
  108 + $auth = new Auth($accessKey,$secretKey);
  109 + // 要上传的空间
  110 + $bucket = $config['bucket'];
  111 +
  112 + $token = $auth->uploadToken($bucket);
  113 + // 初始化 UploadManager 对象并进行文件的上传
  114 + $uploadMgr = new UploadManager();
  115 + // 调用 UploadManager 的 putFile 方法进行文件的上传
  116 + list($ret, $err) = $uploadMgr->putFile($token, $key, $filePath);
  117 + if($is_del_local){
  118 + unlink($filePath);
  119 + }
  120 + if ($err !== null) {
  121 + return ["err"=>1,"msg"=>$err,"data"=>""];
  122 + } else {
  123 + return ["err"=>0,"msg"=>"上传完成","data"=>cmf_get_image_url($ret['key'])];
  124 + }
  125 + }
  126 +
  127 +
  128 +}
  1 +<?php
  2 +// +----------------------------------------------------------------------
  3 +// | bronet [ 以客户为中心 以奋斗者为本 ]
  4 +// +----------------------------------------------------------------------
  5 +// | Copyright (c) 2013-2017 http://www.bronet.cn All rights reserved.
  6 +// +----------------------------------------------------------------------
  7 +namespace api\user\controller;
  8 +
  9 +use cmf\controller\RestBaseController;
  10 +use think\Validate;
  11 +use think\View;
  12 +
  13 +class VerificationCodeController extends RestBaseController
  14 +{
  15 + public function send()
  16 + {
  17 + $validate = new Validate([
  18 + 'username' => 'require',
  19 + ]);
  20 +
  21 + $validate->message([
  22 + 'username.require' => '请输入手机号或邮箱!',
  23 + ]);
  24 +
  25 + $data = $this->request->param();
  26 + if (!$validate->check($data)) {
  27 + $this->error($validate->getError());
  28 + }
  29 +
  30 + $accountType = '';
  31 +
  32 + if (Validate::is($data['username'], 'email')) {
  33 + $accountType = 'email';
  34 + } else if (preg_match('/(^(13\d|15[^4\D]|17[13678]|18\d)\d{8}|170[^346\D]\d{7})$/', $data['username'])) {
  35 + $accountType = 'mobile';
  36 + } else {
  37 + $this->error("请输入正确的手机或者邮箱格式!");
  38 + }
  39 +
  40 + //TODO 限制 每个ip 的发送次数
  41 +
  42 + $code = cmf_get_verification_code($data['username']);
  43 + if (empty($code)) {
  44 + $this->error("验证码发送过多,请明天再试!");
  45 + }
  46 +
  47 + if ($accountType == 'email') {
  48 +
  49 + $emailTemplate = cmf_get_option('email_template_verification_code');
  50 +
  51 + $user = cmf_get_current_user();
  52 + $username = empty($user['user_nickname']) ? $user['user_login'] : $user['user_nickname'];
  53 +
  54 + $message = htmlspecialchars_decode($emailTemplate['template']);
  55 + $view = new View();
  56 + $message = $view->display($message, ['code' => $code, 'username' => $username]);
  57 + $subject = empty($emailTemplate['subject']) ? 'bronet验证码' : $emailTemplate['subject'];
  58 + $result = cmf_send_email($data['username'], $subject, $message);
  59 +
  60 + if (empty($result['error'])) {
  61 + cmf_verification_code_log($data['username'], $code);
  62 + $this->success("验证码已经发送成功!");
  63 + } else {
  64 + $this->error("邮箱验证码发送失败:" . $result['message']);
  65 + }
  66 +
  67 + } else if ($accountType == 'mobile') {
  68 +
  69 + $param = ['mobile' => $data['username'], 'code' => $code];
  70 + $result = hook_one("send_mobile_verification_code", $param);
  71 +
  72 + if ($result !== false && !empty($result['error'])) {
  73 + $this->error($result['message']);
  74 + }
  75 +
  76 + if ($result === false) {
  77 + $this->error('未安装验证码发送插件,请联系管理员!');
  78 + }
  79 +
  80 + cmf_verification_code_log($data['username'], $code);
  81 +
  82 + if (!empty($result['message'])) {
  83 + $this->success($result['message']);
  84 + } else {
  85 + $this->success('验证码已经发送成功!');
  86 + }
  87 +
  88 + }
  89 +
  90 +
  91 + }
  92 +
  93 +}
  1 +<?php
  2 +// +----------------------------------------------------------------------
  3 +// | 文件说明:评论
  4 +// +----------------------------------------------------------------------
  5 +// | Copyright (c) 2013-2017 http://www.bronet.cn All rights reserved.
  6 +// +----------------------------------------------------------------------
  7 +// | Date: 2017-7-29
  8 +// +----------------------------------------------------------------------
  9 +
  10 +namespace api\user\model;
  11 +
  12 +use api\common\model\CommonModel;
  13 +
  14 +class CommentModel extends CommonModel
  15 +{
  16 +
  17 + //模型关联方法
  18 + protected $relationFilter = ['user', 'to_user'];
  19 +
  20 + /**
  21 + * 基础查询
  22 + */
  23 + protected function base($query)
  24 + {
  25 + $query->where('delete_time', 0)
  26 + ->where('status', 1);
  27 + }
  28 +
  29 + /**
  30 + * post_content 自动转化
  31 + * @param $value
  32 + * @return string
  33 + */
  34 + public function getContentAttr($value)
  35 + {
  36 + return cmf_replace_content_file_url(htmlspecialchars_decode($value));
  37 + }
  38 +
  39 + /**
  40 + * more 自动转化
  41 + * @param $value
  42 + * @return array
  43 + */
  44 + public function getMoreAttr($value)
  45 + {
  46 + $more = json_decode($value, true);
  47 + if (!empty($more['thumbnail'])) {
  48 + $more['thumbnail'] = cmf_get_image_url($more['thumbnail']);
  49 + }
  50 +
  51 + if (!empty($more['photos'])) {
  52 + foreach ($more['photos'] as $key => $value) {
  53 + $more['photos'][$key]['url'] = cmf_get_image_url($value['url']);
  54 + }
  55 + }
  56 +
  57 + if (!empty($more['files'])) {
  58 + foreach ($more['files'] as $key => $value) {
  59 + $more['files'][$key]['url'] = cmf_get_image_url($value['url']);
  60 + }
  61 + }
  62 + return $more;
  63 + }
  64 +
  65 + /**
  66 + * 关联 user表
  67 + * @return $this
  68 + */
  69 + public function user()
  70 + {
  71 + return $this->belongsTo('UserModel', 'user_id');
  72 + }
  73 +
  74 + public function toUser()
  75 + {
  76 + return $this->belongsTo('UserModel', 'to_user_id');
  77 + }
  78 +
  79 + /**
  80 + * [CommentList 评论列表获取]
  81 + * @Author: wuwu<15093565100@163.com>
  82 + * @DateTime: 2017-05-25T20:52:27+0800
  83 + * @since: 1.0
  84 + */
  85 + public function CommentList($map, $limit, $order)
  86 + {
  87 + if (empty($map)) {
  88 + return [];
  89 + }
  90 + $data = $this->with('to_user')->field(true)->where($map)->order($order)->limit($limit)->select();
  91 + return $data;
  92 + }
  93 +
  94 + /**
  95 + * [setComment 添加评论]
  96 + * @Author: wuwu<15093565100@163.com>
  97 + * @DateTime: 2017-08-15T23:57:04+0800
  98 + * @since: 1.0
  99 + */
  100 + public static function setComment($data)
  101 + {
  102 + if (!$data) {
  103 + return false;
  104 + }
  105 +
  106 + if ($obj = self::create($data)) {
  107 + return $obj->id;
  108 + } else {
  109 + return false;
  110 + }
  111 + }
  112 +}
  1 +<?php
  2 +// +----------------------------------------------------------------------
  3 +// | bronet [ 以客户为中心 以奋斗者为本 ]
  4 +// +----------------------------------------------------------------------
  5 +// | Copyright (c) 2013-2017 http://www.bronet.cn All rights reserved.
  6 +// +----------------------------------------------------------------------
  7 +namespace api\user\model;
  8 +
  9 +use think\Model;
  10 +
  11 +class RecycleBinModel extends Model
  12 +{
  13 +
  14 +}
  1 +<?php
  2 +// +----------------------------------------------------------------------
  3 +// | bronet [ 以客户为中心 以奋斗者为本 ]
  4 +// +----------------------------------------------------------------------
  5 +// | Copyright (c) 2013-2017 http://www.bronet.cn All rights reserved.
  6 +// +----------------------------------------------------------------------
  7 +
  8 +namespace api\user\model;
  9 +
  10 +use api\common\model\CommonModel;
  11 +
  12 +class UserFavoriteModel extends CommonModel
  13 +{
  14 + /**
  15 + * [base 基础查询条件]
  16 + */
  17 + protected function base($query)
  18 + {
  19 + $query->field('id,title,url,description,create_time');
  20 + }
  21 +
  22 + /**
  23 + * 关联表
  24 + * @param [string] $table_name [关联表名]
  25 + */
  26 + protected function unionTable($table_name)
  27 + {
  28 + return $this->hasOne($table_name . 'Model', 'object_id');
  29 + }
  30 +
  31 + /**
  32 + * url 自动转化
  33 + * @param $value
  34 + * @return string
  35 + */
  36 + public function getUrlAttr($value)
  37 + {
  38 + $url = json_decode($value,true);
  39 + if (!empty($url)) {
  40 + $url = url($url['action'], $url['param'], true, true);
  41 + } else {
  42 + $url = '';
  43 + }
  44 + return $url;
  45 + }
  46 +
  47 + /**
  48 + * 获取收藏内容
  49 + * @param [array] $data [select,find查询结果]
  50 + * @return [array] [收藏对应表的内容]
  51 + */
  52 + public function getFavorite($data)
  53 + {
  54 + if (!is_string($data[0])) {
  55 + foreach ($data as $key => $value) {
  56 + $where[$value['table_name']][] = $value['object_id'];
  57 + }
  58 + foreach ($where as $key => $value) {
  59 + $favoriteData[] = $this->unionTable($key)->select($value);
  60 + }
  61 + } else {
  62 + $favoriteData = $this->unionTable($data['table_name'])->find($data['object_id']);
  63 + }
  64 +
  65 + return $favoriteData;
  66 + }
  67 +
  68 + /**
  69 + * [setFavorite 设置收藏]
  70 + * @Author: wuwu<15093565100@163.com>
  71 + * @DateTime: 2017-08-03T09:16:37+0800
  72 + * @since: 1.0
  73 + */
  74 + public function setFavorite($data)
  75 + {
  76 + //获取收藏内容信息
  77 + $Favorite = self::create($data);
  78 + return $Favorite->id;
  79 + }
  80 +
  81 + /**
  82 + * [unsetFavorite 取消收藏]
  83 + * @Author: wuwu<15093565100@163.com>
  84 + * @DateTime: 2017-08-03T09:17:30+0800
  85 + * @since: 1.0
  86 + * @return [type] [description]
  87 + */
  88 + public function unsetFavorite($id)
  89 + {
  90 + return self::destroy($id); //执行删除
  91 + }
  92 +}
  1 +<?php
  2 +// +----------------------------------------------------------------------
  3 +// | 文件说明:用户表关联model
  4 +// +----------------------------------------------------------------------
  5 +// | Copyright (c) 2017 http://www.bronet.cn 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\model;
  14 +
  15 +use think\Model;
  16 +
  17 +class UserModel extends Model
  18 +{
  19 + //
  20 +}
  1 +<?php
  2 +// +----------------------------------------------------------------------
  3 +// | 文件说明:路由
  4 +// +----------------------------------------------------------------------
  5 +// | Copyright (c) 2017 http://www.wuwuseo.com All rights reserved.
  6 +// +----------------------------------------------------------------------
  7 +// | Author: bronet
  8 +// +----------------------------------------------------------------------
  9 +// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  10 +// +----------------------------------------------------------------------
  11 +// | Date: 2017-8-11
  12 +// +----------------------------------------------------------------------
  13 +use think\Route;
  14 +
  15 +Route::get([
  16 + 'user/favorites/my' => 'user/favorites/getFavorites', //获取收藏列表
  17 + 'user/comments/my' => 'user/comments/getUserComments', //获取我的评论列表
  18 + 'user/comments' => 'user/comments/getComments', //获评论列表
  19 +]);
  20 +
  21 +Route::post([
  22 + 'user/articles/deletes' => 'user/Articles/deletes',
  23 + 'user/favorites' => 'user/favorites/setFavorites', //添加收藏
  24 + 'user/comments' => 'user/comments/setComments', //添加评论
  25 +]);
  26 +
  27 +Route::delete([
  28 + 'user/favorites/:id' => 'user/favorites/unsetFavorites', //删除收藏
  29 + 'user/comments/:id' => 'user/comments/delComments', //删除评论
  30 +]);
  1 +<?php
  2 +// +----------------------------------------------------------------------
  3 +// | bronet [ 以客户为中心 以奋斗者为本 ]
  4 +// +----------------------------------------------------------------------
  5 +// | Copyright (c) 2013-2017 http://www.bronet.cn All rights reserved.
  6 +// +----------------------------------------------------------------------
  7 +namespace api\wxapp\controller;
  8 +
  9 +use think\Db;
  10 +use cmf\controller\RestBaseController;
  11 +use wxapp\aes\WXBizDataCrypt;
  12 +use think\Validate;
  13 +
  14 +/**
  15 + * @title 公共模块
  16 + * @description 公共模块
  17 + * @package api\wxapp\controller
  18 + */
  19 +class PublicController extends RestBaseController
  20 +{
  21 + /**
  22 + * @title 获取sessionKey和openid
  23 + * @description 小程序登录注册
  24 + * @author Tiger Yang
  25 + * @url /wxapp/public/getSessionKey
  26 + * @method POST
  27 + *
  28 + * @param name:code type:string require:1 other: desc:code
  29 + *
  30 + * @return session_key:session_key
  31 + * @return openid:openid
  32 + */
  33 + public function getSessionKey(){
  34 + $validate = new Validate([
  35 + 'code' => 'require',
  36 + ]);
  37 +
  38 + $validate->message([
  39 + 'code.require' => '缺少参数code!',
  40 + ]);
  41 +
  42 + $data = $this->request->param();
  43 + if (!$validate->check($data)) {
  44 + $this->error(['code'=>'40003','msg'=>$validate->getError()]);
  45 + }
  46 +
  47 + $code = $data['code'];
  48 + $appId = config('app_id');
  49 + $appSecret = config('app_secret');
  50 +
  51 + $response = cmf_curl_get("https://api.weixin.qq.com/sns/jscode2session?appid=$appId&secret=$appSecret&js_code=$code&grant_type=authorization_code");
  52 +
  53 + $response = json_decode($response, true);
  54 + if (!empty($response['errcode'])) {
  55 + $this->error(['code'=>'41001','msg'=>'操作失败:'.$response['errcode']]);
  56 + }
  57 + $this->success('获取成功',$response);
  58 + }
  59 +
  60 + /**
  61 + * @title 小程序登录注册
  62 + * @description 小程序登录注册
  63 + * @author Tiger Yang
  64 + * @url /wxapp/public/login
  65 + * @method POST
  66 + *
  67 + * @param name:openid type:string require:1 other: desc:openid
  68 + * @param name:session_key type:string require:1 other: desc:session_key
  69 + * @param name:encrypted_data type:string require:1 other: desc:encrypted_data
  70 + * @param name:iv type:string require:1 other: desc:iv
  71 + *
  72 + * @return token:登录唯一标识
  73 + */
  74 + public function login()
  75 + {
  76 + $validate = new Validate([
  77 + 'openid' => 'require',
  78 + 'session_key' => 'require',
  79 + 'encrypted_data' => 'require',
  80 + 'iv' => 'require',
  81 + ]);
  82 +
  83 + $validate->message([
  84 + 'openid.require' => '缺少参数openid!',
  85 + 'session_key.require' => '缺少参数session_key!',
  86 + 'encrypted_data.require' => '缺少参数encrypted_data!',
  87 + 'iv.require' => '缺少参数iv!',
  88 + ]);
  89 +
  90 + $data = $this->request->param();
  91 + if (!$validate->check($data)) {
  92 + $this->error(['code'=>'40003','msg'=>$validate->getError()]);
  93 + }
  94 +
  95 + $appId = config('app_id');
  96 +
  97 + $openid = $data['openid'];
  98 + $sessionKey = $data['session_key'];
  99 +
  100 + $pc = new WXBizDataCrypt($appId, $sessionKey);
  101 + $errCode = $pc->decryptData($data['encrypted_data'], $data['iv'], $wxUserData);
  102 +
  103 + if ($errCode != 0) {
  104 + $this->error(['code'=>'41002','msg'=>'检验数据失败!'],['errCode'=>$errCode,'param'=>$data]);
  105 + }
  106 +
  107 + $findThirdPartyUser = Db::name("third_party_user")
  108 + ->where('openid', $openid)
  109 + ->where('app_id', $appId)
  110 + ->find();
  111 +
  112 + $currentTime = time();
  113 + $ip = $this->request->ip(0, true);
  114 +
  115 + $wxUserData['sessionKey'] = $sessionKey;
  116 + unset($wxUserData['watermark']);
  117 +
  118 + if ($findThirdPartyUser) {
  119 + $token = cmf_generate_user_token($findThirdPartyUser['user_id'], 'wxapp');
  120 +
  121 + $userData = [
  122 + 'last_login_ip' => $ip,
  123 + 'last_login_time' => $currentTime,
  124 + 'login_times' => ['exp', 'login_times+1'],
  125 + 'more' => json_encode($wxUserData)
  126 + ];
  127 +
  128 + if (isset($wxUserData['unionId'])) {
  129 + $userData['union_id'] = $wxUserData['unionId'];
  130 + }
  131 +
  132 + Db::name("third_party_user")
  133 + ->where('openid', $openid)
  134 + ->where('app_id', $appId)
  135 + ->update($userData);
  136 + $this->success("登录成功!", ['token' => $token]);
  137 + } else {
  138 +
  139 + Db::startTrans();
  140 + $userId = Db::name("user")->insertGetId([
  141 + 'create_time' => $currentTime,
  142 + 'user_status' => 1,
  143 + 'user_type' => 2,
  144 + 'sex' => $wxUserData['gender'],
  145 + 'user_nickname' => $wxUserData['nickName'],
  146 + 'avatar' => $wxUserData['avatarUrl'],
  147 + 'last_login_ip' => $ip,
  148 + 'last_login_time' => $currentTime
  149 + ]);
  150 +
  151 + $row=Db::name("third_party_user")->insert([
  152 + 'openid' => $openid,
  153 + 'user_id' => $userId,
  154 + 'third_party' => 'wxapp',
  155 + 'app_id' => $appId,
  156 + 'last_login_ip' => $ip,
  157 + 'union_id' => isset($wxUserData['unionId']) ? $wxUserData['unionId'] : '',
  158 + 'last_login_time' => $currentTime,
  159 + 'create_time' => $currentTime,
  160 + 'login_times' => 1,
  161 + 'status' => 1,
  162 + 'more' => json_encode($wxUserData)
  163 + ]);
  164 +
  165 + if($userId && $row){
  166 + Db::commit();
  167 + $token = cmf_generate_user_token($userId, 'wxapp');
  168 + $this->success("登录成功!", ['token' => $token]);
  169 + }else{
  170 + Db::rollback();
  171 + $this->error(['code'=>'40004','msg'=>'登录失败']);
  172 + }
  173 +
  174 + }
  175 +
  176 + }
  177 +
  178 +}
  1 +<?php
  2 +// +----------------------------------------------------------------------
  3 +// | bronet [ 以客户为中心 以奋斗者为本 ]
  4 +// +----------------------------------------------------------------------
  5 +// | Copyright (c) 2013-2017 http://www.bronet.cn All rights reserved.
  6 +// +----------------------------------------------------------------------
  7 +namespace api\wxapp\controller;
  8 +
  9 +use cmf\controller\RestBaseController;
  10 +use wxapp\aes\WXBizDataCrypt;
  11 +
  12 +class UserController extends RestBaseController
  13 +{
  14 + // 获取用户信息
  15 + public function getUserInfo()
  16 + {
  17 +
  18 +
  19 +
  20 + }
  21 +
  22 +}
  1 +<?php
  2 +// +----------------------------------------------------------------------
  3 +// | bronet [ 以客户为中心 以奋斗者为本 ]
  4 +// +----------------------------------------------------------------------
  5 +// | Copyright (c) 2013-2017 http://www.bronet.cn All rights reserved.
  6 +// +----------------------------------------------------------------------
  7 +namespace app\admin\annotation;
  8 +
  9 +use mindplay\annotations\Annotation;
  10 +
  11 +/**
  12 + * Specifies validation of a string, requiring a minimum and/or maximum length.
  13 + *
  14 + * @usage('method'=>true, 'inherited'=>true, 'multiple'=>false)
  15 + */
  16 +class AdminMenuAnnotation extends Annotation
  17 +{
  18 + public $remark = '';
  19 +
  20 + public $icon = '';
  21 +
  22 + public $name = '';
  23 +
  24 + public $param = '';
  25 +
  26 + public $parent = '';
  27 +
  28 + public $display = false;
  29 +
  30 + public $order = 10000;
  31 +
  32 + public $hasView = true;
  33 +
  34 + /**
  35 + * Initialize the annotation.
  36 + * @param array $properties
  37 + */
  38 + public function initAnnotation(array $properties)
  39 + {
  40 + parent::initAnnotation($properties);
  41 + }
  42 +}
  1 +<?php
  2 +// +----------------------------------------------------------------------
  3 +// | bronet [ 以客户为中心 以奋斗者为本 ]
  4 +// +----------------------------------------------------------------------
  5 +// | Copyright (c) 2013-2017 http://www.bronet.cn All rights reserved.
  6 +// +----------------------------------------------------------------------
  7 +namespace app\admin\annotation;
  8 +
  9 +use mindplay\annotations\AnnotationException;
  10 +use mindplay\annotations\Annotation;
  11 +
  12 +/**
  13 + * Specifies validation of a string, requiring a minimum and/or maximum length.
  14 + *
  15 + * @usage('class'=>true, 'inherited'=>true, 'multiple'=>true)
  16 + */
  17 +class AdminMenuRootAnnotation extends Annotation
  18 +{
  19 + /**
  20 + * @var int|null Minimum string length (or null, if no minimum)
  21 + */
  22 + public $remark = '';
  23 +
  24 + /**
  25 + * @var int|null Maximum string length (or null, if no maximum)
  26 + */
  27 + public $icon = '';
  28 +
  29 + /**
  30 + * @var int|null Minimum string length (or null, if no minimum)
  31 + */
  32 + public $name = '';
  33 +
  34 + public $action = '';
  35 +
  36 + public $param = '';
  37 +
  38 + public $parent = '';
  39 +
  40 + public $display = false;
  41 +
  42 + public $order = 10000;
  43 +
  44 + /**
  45 + * Initialize the annotation.
  46 + * @param array $properties
  47 + */
  48 + public function initAnnotation(array $properties)
  49 + {
  50 + parent::initAnnotation($properties);
  51 + }
  52 +}
  1 +<?php
  2 +// +----------------------------------------------------------------------
  3 +// | bronet [ 以客户为中心 以奋斗者为本 ]
  4 +// +----------------------------------------------------------------------
  5 +// | Copyright (c) 2013-2017 http://www.bronet.cn All rights reserved.
  6 +// +----------------------------------------------------------------------
  7 +namespace app\admin\api;
  8 +
  9 +use app\admin\model\NavModel;
  10 +
  11 +class NavApi
  12 +{
  13 + // 分类列表 用于模板设计
  14 + public function index($param = [])
  15 + {
  16 + $navModel = new NavModel();
  17 +
  18 + $where = [];
  19 +
  20 + if (!empty($param['keyword'])) {
  21 + $where['name'] = ['like', "%{$param['keyword']}%"];
  22 + }
  23 +
  24 + return $navModel->where($where)->select();
  25 + }
  26 +
  27 +}
  1 +<?php
  2 +// +----------------------------------------------------------------------
  3 +// | bronet [ 以客户为中心 以奋斗者为本 ]
  4 +// +----------------------------------------------------------------------
  5 +// | Copyright (c) 2013-2017 http://www.bronet.cn All rights reserved.
  6 +// +----------------------------------------------------------------------
  7 +namespace app\admin\api;
  8 +
  9 +use app\admin\model\NavMenuModel;
  10 +
  11 +class NavMenuApi
  12 +{
  13 + // 分类列表 用于模板设计
  14 + public function index($param = [])
  15 + {
  16 + $navMenuModel = new NavMenuModel();
  17 +
  18 + $where = [];
  19 +
  20 + if (!empty($param['keyword'])) {
  21 + $where['name'] = ['like', "%{$param['keyword']}%"];
  22 + }
  23 + if (!empty($param['id'])) {
  24 + $where['nav_id'] = $param['id'];
  25 + }
  26 +
  27 + return $navMenuModel->where($where)->select();
  28 + }
  29 +
  30 +}
  1 +<?php
  2 +// +----------------------------------------------------------------------
  3 +// | bronet [ 以客户为中心 以奋斗者为本 ]
  4 +// +----------------------------------------------------------------------
  5 +// | Copyright (c) 2013-2017 http://www.bronet.cn All rights reserved.
  6 +// +----------------------------------------------------------------------
  7 +namespace app\admin\api;
  8 +
  9 +use app\admin\model\SlideModel;
  10 +
  11 +class SlideApi
  12 +{
  13 + /**
  14 + * 幻灯片列表 用于模板设计
  15 + * @param array $param
  16 + * @return false|\PDOStatement|string|\think\Collection
  17 + */
  18 + public function index($param = [])
  19 + {
  20 + $slideModel = new SlideModel();
  21 +
  22 + $where = [];
  23 +
  24 + if (!empty($param['keyword'])) {
  25 + $where['name'] = ['like', "%{$param['keyword']}%"];
  26 + }
  27 +
  28 + //返回的数据必须是数据集或数组,item里必须包括id,name,如果想表示层级关系请加上 parent_id
  29 + return $slideModel->where($where)->select();
  30 + }
  31 +
  32 +}
  1 +<?php
  2 +namespace app\admin\controller;
  3 +use cmf\controller\AdminBaseController;
  4 +class BakController extends AdminBaseController {
  5 + public function index(){
  6 + $type = $this->request->param('tp');
  7 + $name = $this->request->param('name');
  8 + $sql=new \org\Baksql(config('database'));
  9 + switch ($type)
  10 + {
  11 + case "backup": //备份
  12 + $ret=$sql->backup();
  13 + if($ret['code']=1){
  14 + $this->success($ret['msg']);
  15 + }else{
  16 + $this->error($ret['msg']);
  17 + }
  18 + break;
  19 + case "dowonload": //下载
  20 + $sql->downloadFile($name);
  21 + break;
  22 + case "restore": //还原
  23 + $ret= $sql->restore($name);
  24 + if($ret['code']=1){
  25 + $this->success($ret['msg']);
  26 + }else{
  27 + $this->error($ret['msg']);
  28 + }
  29 + break;
  30 + case "del": //删除
  31 + $ret= $sql->delfilename($name);
  32 + if($ret['code']=1){
  33 + $this->success($ret['msg']);
  34 + }else{
  35 + $this->error($ret['msg']);
  36 + }
  37 + break;
  38 + default: //获取备份文件列表
  39 + return $this->fetch("index",["list"=>$sql->get_filelist()]);
  40 + }
  41 + }
  42 +
  43 +}
  44 +
  45 +?>
  1 +<?php
  2 +// +----------------------------------------------------------------------
  3 +// | bronet [ 以客户为中心 以奋斗者为本 ]
  4 +// +----------------------------------------------------------------------
  5 +// | Copyright (c) 2013-2017 http://www.bronet.cn All rights reserved.
  6 +// +----------------------------------------------------------------------
  7 +namespace app\admin\controller;
  8 +
  9 +use cmf\controller\AdminBaseController;
  10 +use think\Db;
  11 +
  12 +class DialogController extends AdminBaseController
  13 +{
  14 + public function _initialize()
  15 + {
  16 +
  17 + }
  18 +
  19 + public function map()
  20 + {
  21 + $location = $this->request->param('location');
  22 + $location = explode(',', $location);
  23 + $lng = empty($location[0]) ? 116.424966 : $location[0];
  24 + $lat = empty($location[1]) ? 39.907851 : $location[1];
  25 +
  26 + $this->assign(['lng' => $lng, 'lat' => $lat]);
  27 + return $this->fetch();
  28 + }
  29 +
  30 + public function map2()
  31 + {
  32 + $location = $this->request->param('location');
  33 + $location = explode(',', $location);
  34 + $lng = empty($location[0]) ? 116.424966 : $location[0];
  35 + $lat = empty($location[1]) ? 39.907851 : $location[1];
  36 +
  37 + $this->assign(['lng' => $lng, 'lat' => $lat]);
  38 + return $this->fetch();
  39 + }
  40 +
  41 + public function getShopHtml(){
  42 + $data = $this->request->param();
  43 + $where=[];
  44 + if (!empty($data['category_id'])) {
  45 + $where['category_id']=$data['category_id'];
  46 + }
  47 + $list=Db::name('shop')->where($where)->select()->toArray();
  48 + if(!empty($list)){
  49 + $this->success('','',$list);
  50 + }else{
  51 + $this->error('该分类下没有店铺');
  52 + }
  53 +
  54 + }
  55 +
  56 +}
  1 +<?php
  2 +// +----------------------------------------------------------------------
  3 +// | bronet [ 以客户为中心 以奋斗者为本 ]
  4 +// +----------------------------------------------------------------------
  5 +// | Copyright (c) 2013-2017 http://www.bronet.cn All rights reserved.
  6 +// +----------------------------------------------------------------------
  7 +namespace app\admin\controller;
  8 +
  9 +use cmf\controller\AdminBaseController;
  10 +use app\admin\model\HookModel;
  11 +use app\admin\model\PluginModel;
  12 +use app\admin\model\HookPluginModel;
  13 +use think\Db;
  14 +
  15 +/**
  16 + * Class HookController 钩子管理控制器
  17 + * @package app\admin\controller
  18 + */
  19 +class HookController extends AdminBaseController
  20 +{
  21 + /**
  22 + * 钩子管理
  23 + * @adminMenu(
  24 + * 'name' => '钩子管理',
  25 + * 'parent' => 'admin/Plugin/default',
  26 + * 'display'=> true,
  27 + * 'hasView'=> true,
  28 + * 'order' => 10000,
  29 + * 'icon' => '',
  30 + * 'remark' => '钩子管理',
  31 + * 'param' => ''
  32 + * )
  33 + */
  34 + public function index()
  35 + {
  36 + $hookModel = new HookModel();
  37 + $hooks = $hookModel->select();
  38 + $this->assign('hooks', $hooks);
  39 + return $this->fetch();
  40 + }
  41 +
  42 + /**
  43 + * 钩子插件管理
  44 + * @adminMenu(
  45 + * 'name' => '钩子插件管理',
  46 + * 'parent' => 'index',
  47 + * 'display'=> false,
  48 + * 'hasView'=> true,
  49 + * 'order' => 10000,
  50 + * 'icon' => '',
  51 + * 'remark' => '钩子插件管理',
  52 + * 'param' => ''
  53 + * )
  54 + */
  55 + public function plugins()
  56 + {
  57 + $hook = $this->request->param('hook');
  58 + $pluginModel = new PluginModel();
  59 + $plugins = $pluginModel->field('a.*,b.hook,b.plugin,b.list_order,b.status as hook_plugin_status,b.id as hook_plugin_id')->alias('a')->join('__HOOK_PLUGIN__ b', 'a.name = b.plugin')->where('b.hook', $hook)->select();
  60 + $this->assign('plugins', $plugins);
  61 + return $this->fetch();
  62 + }
  63 +
  64 + /**
  65 + * 钩子插件排序
  66 + * @adminMenu(
  67 + * 'name' => '钩子插件排序',
  68 + * 'parent' => 'index',
  69 + * 'display'=> false,
  70 + * 'hasView'=> false,
  71 + * 'order' => 10000,
  72 + * 'icon' => '',
  73 + * 'remark' => '钩子插件排序',
  74 + * 'param' => ''
  75 + * )
  76 + */
  77 + public function pluginListOrder()
  78 + {
  79 + $hookPluginModel = new HookPluginModel();
  80 + parent::listOrders($hookPluginModel);
  81 +
  82 + $this->success("排序更新成功!");
  83 + }
  84 +
  85 + /**
  86 + * 同步钩子
  87 + * @adminMenu(
  88 + * 'name' => '同步钩子',
  89 + * 'parent' => 'index',
  90 + * 'display'=> false,
  91 + * 'hasView'=> true,
  92 + * 'order' => 10000,
  93 + * 'icon' => '',
  94 + * 'remark' => '同步钩子',
  95 + * 'param' => ''
  96 + * )
  97 + */
  98 + public function sync()
  99 + {
  100 +
  101 + $apps = cmf_scan_dir(APP_PATH . '*', GLOB_ONLYDIR);
  102 +
  103 + foreach ($apps as $app) {
  104 + $hookConfigFile = APP_PATH . $app . '/hooks.php';
  105 + if (file_exists($hookConfigFile)) {
  106 + $hooksInFile = include $hookConfigFile;
  107 +
  108 + foreach ($hooksInFile as $hookName => $hook) {
  109 +
  110 + $hook['type'] = empty($hook['type']) ? 2 : $hook['type'];
  111 +
  112 + if (!in_array($hook['type'], [2, 3, 4])) {
  113 + $hook['type'] = 2;
  114 + }
  115 +
  116 + $findHook = Db::name('hook')->where(['hook' => $hookName])->count();
  117 +
  118 + $hook['app'] = $app;
  119 +
  120 + if ($findHook > 0) {
  121 + Db::name('hook')->where(['hook' => $hookName])->strict(false)->field(true)->update($hook);
  122 + } else {
  123 + $hook['hook'] = $hookName;
  124 + Db::name('hook')->insert($hook);
  125 + }
  126 + }
  127 + }
  128 + }
  129 +
  130 + return $this->fetch();
  131 + }
  132 +
  133 +
  134 +}
  1 +<?php
  2 +// +----------------------------------------------------------------------
  3 +// | bronet [ 以客户为中心 以奋斗者为本 ]
  4 +// +----------------------------------------------------------------------
  5 +// | Copyright (c) 2013-2017 http://www.bronet.cn All rights reserved.
  6 +// +----------------------------------------------------------------------
  7 +namespace app\admin\controller;
  8 +
  9 +use cmf\controller\AdminBaseController;
  10 +use think\Db;
  11 +use app\admin\model\AdminMenuModel;
  12 +
  13 +class IndexController extends AdminBaseController
  14 +{
  15 +
  16 + public function _initialize()
  17 + {
  18 + $adminSettings = cmf_get_option('admin_settings');
  19 + if (empty($adminSettings['admin_password']) || $this->request->path() == $adminSettings['admin_password']) {
  20 + $adminId = cmf_get_current_admin_id();
  21 + if (empty($adminId)) {
  22 + session("__LOGIN_BY_CMF_ADMIN_PW__", 1);//设置后台登录加密码
  23 + }
  24 + }
  25 +
  26 + parent::_initialize();
  27 + }
  28 +
  29 + /**
  30 + * 后台首页
  31 + */
  32 + public function index()
  33 + {
  34 + $adminMenuModel = new AdminMenuModel();
  35 + $menus = $adminMenuModel->menuTree();
  36 +
  37 + $this->assign("menus", $menus);
  38 +
  39 + $admin = Db::name("user")->where('id', cmf_get_current_admin_id())->find();
  40 + $this->assign('admin', $admin);
  41 + return $this->fetch();
  42 + }
  43 +}
  1 +<?php
  2 +namespace app\admin\controller;
  3 +
  4 +use cmf\controller\AdminBaseController;
  5 +use think\Db;
  6 +use app\admin\model\AdminMenuModel;
  7 +
  8 +class KehuController extends AdminBaseController{
  9 +
  10 +
  11 + //显示客户列表
  12 + public function kehulist(){
  13 +
  14 + $data = Db::name('customer_data') -> order('status') -> paginate(10);
  15 + $this -> assign('data',$data);
  16 + return $this -> fetch();
  17 +
  18 +
  19 + }
  20 +
  21 + //查看用户资料
  22 + public function khlook(){
  23 +
  24 + $id = input('post.id');
  25 + $data = Db::name('customer_data') -> where('id',$id) -> find();
  26 + return json_encode($data);
  27 +
  28 + }
  29 +
  30 + //修改客户状态
  31 + public function khup(){
  32 +
  33 + $id = input('post.id');
  34 + $status = input('post.status');
  35 + $data = Db::name('customer_data') -> where('id',$id) -> update(['status'=>$status]);
  36 + if($data){
  37 + return true;
  38 + }else{
  39 + return false;
  40 + }
  41 +
  42 + }
  43 +
  44 +
  45 + //删除客户资料
  46 + public function khdel(){
  47 +
  48 + $id = input('post.id');
  49 + $data = Db::name('customer_data') -> where('id',$id) -> delete();
  50 + if($data){
  51 + return true;
  52 + }else{
  53 + return false;
  54 + }
  55 +
  56 +
  57 +
  58 + }
  59 +
  60 +
  61 +
  62 +
  63 +
  64 +
  65 +}
  66 +
  67 +
  68 +
  69 +
  70 +
  71 +
  72 +
  73 +
  74 +
  75 +
  76 +
  77 +
  78 +
  79 +
  80 +
  81 +
  82 +
  83 +
  84 +
  85 +
  86 +
  87 + ?>