/** * 记录用户日志 * @param string $logs 用户日志 * @param array $data 操作的数据 * @param boolean $autoUid 自动获取用户UID 否则传入UID */ function logs($logs, $data = [], $autoUid = true) { $uid = $autoUid === true ? UID : $autoUid; $data = !empty($data) ? json_encode($data, JSON_UNESCAPED_UNICODE) : ''; $data = ['uid' => $uid, 'logs' => $logs, 'datas' => $data, 'create_time' => NOW_TIME]; Db::name('Logs')->insert($data); }
/** * 检测继承分类是否是相同的模型 * @param [type] $value [description] * @param [type] $rule [description] * @param [type] $data [description] * @return [type] [description] */ protected function checkExtends($value, $rule, $data) { if (0 != $data['pid']) { return $value == Db::name('Category')->where('id', $data['pid'])->value('model'); } else { return true; } }
/** * 验证菜单是几级节点 * @param [type] $value [description] * @param [type] $rule [description] * @param [type] $data [description] * @return [type] [description] */ protected function checkNode($pid, $rule, $data) { // 一级菜单最多三个 if ($pid == 0) { return Db::name('WechatMenu')->where('pid', 0)->count() >= 3 ? '一级菜单数量最多三个' : true; } else { return Db::name('WechatMenu')->where('pid', $pid)->count() >= 5 ? '二级菜单数量最多五个' : true; } }
/** * 保存登陆信息 * @param [type] $user */ private function autoLogin($user) { $loginData = ['uid' => $user['id'], 'account' => $user['account'], 'login' => $user['login'], 'last_time' => $user['last_time'], 'last_ip' => $user['last_ip']]; session('user_auth', $loginData); session('user_auth_sign', data_auth_sign($loginData)); // 保存登陆信息 $saveData = ['last_ip' => get_client_ip(), 'last_time' => NOW_TIME, 'login' => ['exp', '`login`+1']]; Db::name('Member')->where('id', $user['id'])->update($saveData); logs('登陆成功', '', $user['id']); }
/** * 返回树形选择 * @param string 分类模型 * @param integer $unShowId 排除显示的节点 * @return array */ public function treeSelect($model = '', $unShowId = 0) { $map['status'] = 1; if ($unShowId) { $map['id'] = ['neq', $unShowId]; } if ($model) { $map['model'] = $model; } $categorys = Db::name('category')->where($map)->order('sort asc')->select(); return Tree::toFormatTree($categorys); }
/** * 上传文件管理 */ public function index($type = '') { $total = disk_total_space("."); $this->assign('disk_use', round(($total - disk_free_space(".")) / $total * 100, 2)); $map = []; if ($type) { $map['type'] = $type; } $list = parent::_list('Storage', $map); $this->assign('list', $list); $this->assign('size', Db::name('Storage')->where($map)->sum('size')); $this->assign('types', Db::name('Storage')->field('type')->distinct('type')->select()); return $this->fetch(); }
/** * 获取用户菜单节点Id集合 * @param [type] $uid 用户UID * @return array */ public static function getMenuIds($uid) { $gIds = self::getGroupIds($uid); $menuIds = ''; if ($gIds) { $menuIds = Db::name('Auth')->where('id', 'in', $gIds)->column('rules'); $menuIds = implode($menuIds, ','); $menuIds = trim($menuIds, ','); } $openMap = ['status' => 2, 'auth' => 0]; $openMenus = Db::name('Menu')->where($openMap)->column('id'); $openMenus = implode($openMenus, ','); if (!empty($openMenus)) { $menuIds .= ',' . $openMenus; } return trim($menuIds, ','); }
/** * 修改密码 */ public function password() { if (IS_POST) { $data = $this->request->post(); $validate = Loader::validate('Member'); if (!$validate->scene('changepass')->check($data)) { return $this->error($validate->getError()); } $passData = ['password' => umd5($data['newpass']), 'update_time' => NOW_TIME]; if (Db::name('Member')->where('id', UID)->update($passData)) { return $this->success('密码修改成功'); } else { return $this->error(); } } else { return $this->fetch(); } }
/** * 加载系统扩展配置 */ public static function load() { $config = \think\Cache::get('db_config_cache_data'); if (!$config) { // 在这里先判断一下数据库是否已经正确安装 $Db = \think\Loader::db(); $Query = $Db->query("SHOW TABLES LIKE '" . \think\Config::get('database.prefix') . "config'"); if (empty($Query)) { self::install(); } $data = \think\Db::name('Config')->where('status', 1)->field('type,name,value')->select(); $config = []; if ($data && is_array($data)) { foreach ($data as $value) { $config[$value['name']] = self::parse($value['type'], $value['value']); } } \think\Cache::set('db_config_cache_data', $config); } \think\Config::set($config); }
/** * 快速修改状态 */ public function sort($pid) { if (IS_POST) { $sort = $this->request->post('sort/a'); if (empty($sort)) { return $this->error(); } foreach ($sort as $key => $id) { Db::name('Category')->where('id', $id)->setField('sort', $key + 1); } return $this->success('操作成功', Url('system/category/index') . '?pid=' . $pid); } else { $map = ['pid' => $pid, 'status' => 1]; $list = Db::name('Category')->where($map)->order('sort asc')->select(); $this->assign('list', $list); return $this->fetch(); } }
/** * 通用分页列表数据集获取方法 * 如果表单字段有 status 默认会查询 status > 0 的数据 * @param sting|Model $model 模型名或模型实例 * @param array $where where查询条件(优先级: $where>模型设定) * @param array|string $order 排序条件,传入null时使用sql默认排序或模型属性(优先级最高); * 否则使用$order参数(如果$order参数,且模型也没有设定过order,则取主键降序); * @param boolean $field 单表模型用不到该参数,要用在多表join时为field()方法指定参数 * @param integer $listRows 分页条数 * @return array|false * 返回数据集 */ protected final function _list($model, $where = [], $order = null, $field = true, $listRows = 20) { if (is_string($model)) { $table = Db::name($model); } else { $table = $model; } $tableInfo = $table->getTableInfo(); $fields = $tableInfo['fields']; $_order = $this->request->get('_order'); if (!is_null($_order) && in_array($_order, $fields)) { $order = $_order . ' desc'; } elseif (is_null($order) && !empty($tableInfo['pk'])) { $order = $tableInfo['pk'] . ' desc'; } // 设置默认查询 status > 0 的数据 if (empty($where) && in_array('status', $fields)) { $where['status'] = ['egt', 0]; } $config = ['type' => 'tools\\Pager', 'list_rows' => $listRows, 'query' => $this->request->get()]; $list = $table->field($field)->where($where)->order($order)->paginate(null, false, $config); $this->_page = $list->render(); $this->_total = $list->total(); return $list; }
/** * 清空回收站 * @param [integer] $id */ public function clear($id = '') { if (is_numeric($id)) { $map['id'] = $id; } $map['status'] = -1; $data = ['status' => -2, 'update_time' => NOW_TIME]; if (Db::name('Article')->where($map)->update($data)) { return $this->success(); } else { return $this->error(); } }
/** * 获取我的好友关系列表 * @param integer $uid [description] * @param integer $type 1 关注列表,2 被关注 3 好友 * @return array 用户ID集合 */ public static function lists($uid, $type = 1, $page = 1, $rows = 10) { $model = Db::name('Friend'); switch ($type) { case 1: $result = $model->where('type IN (1,3) AND uid=' . $uid)->whereOr('type IN (2,3) AND fid=' . $uid)->page($page, $rows)->select(); break; case 2: $result = $model->where('type IN (2,3) AND uid=' . $uid)->whereOr('type IN (1,3) AND fid=' . $uid)->page($page, $rows)->select(); break; case 3: $result = $model->where('type=3 AND uid=' . $uid)->whereOr('type=3 AND fid=' . $uid)->page($page, $rows)->select(); break; } $ids = []; foreach ($result as $key => $value) { if ($value['uid'] == $uid) { array_push($ids, $value['fid']); } else { array_push($ids, $value['uid']); } } return $ids; }
public function index() { $data = Db::name('system_config')->find(); return json($data); }
/** * BELONGS TO MANY 关联定义 * @access public * @param string $model 模型名 * @param string $table 中间表名 * @param string $foreignKey 关联外键 * @param string $localKey 当前模型关联键 * @return \think\db\Query|string */ public function belongsToMany($model, $table = '', $foreignKey = '', $localKey = '') { // 记录当前关联信息 $model = $this->parseModel($model); $name = Loader::parseName(basename(str_replace('\\', '/', $model))); $table = $table ?: Db::name(Loader::parseName($this->name) . '_' . $name)->getTable(); $foreignKey = $foreignKey ?: $name . '_id'; $localKey = $localKey ?: Loader::parseName($this->name) . '_id'; return $this->relation()->belongsToMany($model, $table, $foreignKey, $localKey); }
/** * 快速排序 * @param string $group 分组 */ public function sort($group = '') { if (IS_POST) { $sort = $this->request->post('sort/a'); if (empty($sort)) { return $this->error(); } foreach ($sort as $key => $id) { Db::name('Config')->where('id', $id)->setField('sort', $key + 1); } return $this->success('操作成功', Url('system/config/params') . '?group=' . $group); } else { $map = ['hide' => 0, 'status' => 1]; if ($group) { $map['group'] = $group; } $list = Db::name('Config')->where($map)->order('sort asc')->select(); $this->assign('list', $list); return $this->fetch(); } }
/** * 快速修修改显隐状态 * @param [type] $id * @param [type] $hide */ public function auth($id, $auth) { if (!in_array($auth, [0, 1])) { return $this->error(); } $data = ['auth' => $auth, 'update_time' => NOW_TIME]; if (Db::name('Menu')->where('id', $id)->update($data)) { session('system_menu_list', null); return $this->success(); } else { return $this->error(); } }
/** * 获得用户资料,根据自己的情况读取数据库 */ protected function getUserInfo($uid) { static $userinfo = array(); if (!isset($userinfo[$uid])) { $userinfo[$uid] = Db::name($this->_config['AUTH_USER'])->find($uid); } return $userinfo[$uid]; }
/** * 绑定钉钉 * @return [type] [description] */ public function bindding($id, $type = 'bind') { if ($type == 'unbind') { $data = ['ding_uid' => '', 'ding_openid' => '', 'update_time' => NOW_TIME]; if (Db::name('Member')->where('id', $id)->update($data)) { logs('用户钉钉成功解除绑定'); return $this->success(); } else { return $this->error(); } } else { if (IS_POST) { $dingid = $this->request->post('dingid'); if (empty($dingid)) { return $this->error('必须选择一个钉钉用户'); } $dingUser = Db::name('DingUser')->where('id', $dingid)->field('userid,openid')->find(); if (!$dingUser) { return $this->error('钉钉用户不存在'); } $data = ['ding_openid' => $dingUser['openid'], 'ding_uid' => $dingUser['userid']]; if (Db::name('member')->where(['id' => $id])->update($data)) { logs('用户钉钉绑定成功', $data); /* 发送钉钉消息通知 */ if ($this->request->post('notice')) { parent::_initDing(); \tools\Ding\CompanyMessage::oa($dingUser['userid'], '', 30443659, '账户绑定成功', '您的账户与平台账户绑定成功', 'http://d.nxr.cjango.com/center/index?dd_nav_bgcolor=FF18B4ED'); } return $this->success(); } else { return $this->error(); } } else { $info = Db::name('Member')->where('id', $id)->find(); if (!empty($info['ding_openid'])) { $this->assign('info', $info); $this->assign('ding', Db::name('DingUser')->where('openid', $info['ding_openid'])->find()); } else { $hasBindIds = Db::name('Member')->where('ding_openid', 'neq', '')->column('ding_openid') ?: ['']; $list = Db::name('DingUser')->where('openid', 'notin', $hasBindIds)->field('id,name')->select(); $this->assign('list', $list); } } return $this->fetch(); } }
/** * 验证是否唯一 * @access protected * @param mixed $value 字段值 * @param mixed $rule 验证规则 格式:数据表,字段名,排除ID,主键名 * @param array $data 数据 * @param string $field 验证字段名 * @return bool */ protected function unique($value, $rule, $data, $field) { if (is_string($rule)) { $rule = explode(',', $rule); } $db = Db::name($rule[0]); $key = isset($rule[1]) ? $rule[1] : $field; if (strpos($key, '^')) { // 支持多个字段验证 $fields = explode('^', $key); foreach ($fields as $key) { $map[$key] = $data[$key]; } } elseif (strpos($key, '=')) { parse_str($key, $map); } else { $map[$key] = $data[$field]; } $pk = strval(isset($rule[3]) ? $rule[3] : $db->getPk()); if (isset($rule[2])) { $map[$pk] = ['neq', $rule[2]]; } elseif (isset($data[$pk])) { $map[$pk] = ['neq', $data[$pk]]; } if ($db->where($map)->field($pk)->find()) { return false; } return true; }
/** * 模板消息预览 * @param [type] $template_id */ public function template_show($template_id) { $info = Db::name('WechatTemplate')->find($template_id); $this->assign('info', $info); return $this->fetch(); }
/** * 移除授权 * @param [type] $id [description] * @param [type] $uid [description] */ public function remove($id, $uid) { $map = ['auth_id' => $id, 'uid' => $uid]; if (Db::name('AuthUser')->where($map)->delete()) { return $this->success('', '', 'delete'); } else { return $this->error(); } }
/** * 检查密码 * @param [type] $value [description] * @param [type] $rule [description] * @param [type] $data [description] * @return [type] [description] */ protected function checkPass($value, $rule, $data) { return umd5($value) == Db::name('member')->where('id', UID)->value('password'); }