/** * 只通过 treeKey 和 name 来加载一个 节点 * * 注意: treeKey , name 组合未必是唯一的,只能你自己保证唯一性了 * * @param string $treeKey * @param string $name * @param int $ttl * * @return DataMapper * @throws \InvalidArgumentException */ public function loadTreeNodeWithTreeKeyAndName($treeKey, $name, $ttl = 0) { if (empty($treeKey) || empty($name)) { throw new \InvalidArgumentException('treeKey, name can not be empty'); } $dataMapper = new DataMapper('meta'); $dataMapper->loadOne(array('meta_type = ? and meta_key = ? and meta_name = ?', Tree::META_TYPE, $treeKey, $name), null, $ttl); return $dataMapper; }
/** * 根据用户 id 取得用户的第一个地址 * * @return object 用户信息记录 * @param int $userId 用户数字 ID * @param int $ttl 缓存时间 * * */ public function loadUserFirstAddress($userId, $ttl = 0) { // 参数验证 $validator = new Validator(array('userId' => $userId, 'ttl' => $ttl)); $userId = $validator->required()->digits()->min(1)->validate('userId'); $ttl = $validator->digits()->min(0)->validate('ttl'); $this->validate($validator); $user = new DataMapper('user_address'); $user->loadOne(array('user_id=?', $userId), array('order' => 'address_id asc', 'offset' => 0, 'limit' => 1), $ttl); return $user; }
/** * 取得商品属性值 * * @param string $itemName * * @return \Core\Modal\SqlMapper */ public function loadGoodsAttrItem($groupId, $itemName) { // 参数验证 $validator = new Validator(array('groupId' => $groupId, 'itemName' => $itemName)); $groupId = $validator->required()->digits()->min(1)->validate('groupId'); $itemName = $validator->required()->validate('itemName'); $this->validate($validator); // 数据查询 $dataMapper = new DataMapper('meta'); $dataMapper->loadOne(array('meta_type = ? and parent_meta_id = ? and meta_name = ?', GoodsAttrItem::META_TYPE, $groupId, $itemName)); return $dataMapper; }
/** * 检查 order_goods 对应的评论记录是否已经存在 * * @param int $rec_id * * @return bool */ public function isOrderGoodsCommentExist($rec_id) { if (!$rec_id) { return false; } // 参数验证 $validator = new Validator(array('rec_id' => $rec_id)); $rec_id = $validator->required()->digits()->min(1)->validate('rec_id'); $this->validate($validator); $dataMapper = new DataMapper('goods_comment'); $dataMapper->loadOne(array('rec_id = ?', $rec_id), null, 0); return !$dataMapper->isEmpty(); }
/** * 单表查询 * * 根据 id 取得记录信息,只有用 load 加载的数据支持 CRUD 操作 * 比如,可以 record->save()做更新,record->erase() 做删除操作 * * @return object 记录信息 * * @param string $table 表名 * @param string $filter 查询条件,例如 'user_id = ?' * @param int $id 数字 ID,如果 ID为0,则返回一个新建立的 DataMapper 对象,方便之后做 save() 操作 * @param int $ttl 缓存时间 * * */ public function _loadById($table, $filter, $id, $ttl = 0) { $id = $id ?: 0; //如果没有 ID 则设置为 0 // 参数验证 $validator = new Validator(array('table' => $table, 'filter' => $filter, 'id' => $id, 'ttl' => $ttl)); $table = $validator->required()->validate('table'); $filter = $validator->required()->validate('filter'); $id = $validator->digits()->min(0)->validate('id'); $ttl = $validator->digits()->min(0)->validate('ttl'); $this->validate($validator); $dataMapper = new DataMapper($table); if ($id > 0) { $dataMapper->loadOne(array($filter, $id), null, $ttl); } return $dataMapper; }
/** * 通过 sn 号取得 bonus 记录 * * @return object * @param string $sn SN号码 * @param int $ttl 缓存时间 */ public function loadBonusBySn($sn, $ttl = 0) { // 参数验证 $validator = new Validator(array('sn' => $sn, 'ttl' => $ttl)); $sn = $validator->required()->validate('sn'); $ttl = $validator->digits()->min(0)->validate('ttl'); $this->validate($validator); $userBonus = new DataMapper('user_bonus'); $userBonus->loadOne(array('bonus_sn=?', $sn), null, $ttl); return $userBonus; }
/** * 用户认证,检查用户名密码是否正确 * * @return mixed 失败-返回false,成功-返回用户信息 * * @param string $username 用户名 * @param string $email 邮箱 * @param string $password 密码原文 * */ public function doAuthAdmin($username, $email, $password) { // 参数验证 if (Utils::isBlank($username) && Utils::isBlank($email)) { throw new \InvalidArgumentException('user_name, email can not both empty'); } $validator = new Validator(array('password' => $password)); $password = $validator->required()->validate('password'); $this->validate($validator); $sqlPrepare = array(); $sqlParam = array(); $sqlParam[0] = ''; // 查询语句 if (!Utils::isBlank($username)) { $sqlPrepare[] = 'user_name=?'; $sqlParam[] = $username; } if (!Utils::isBlank($email)) { $sqlPrepare[] = 'email=?'; $sqlParam[] = $email; } $sqlParam[0] = implode(' or ', $sqlPrepare); $admin = new DataMapper('admin_user'); $admin->loadOne($sqlParam); if ($admin->isEmpty()) { return false; } // 禁止登陆 if ($admin['disable']) { return false; } // 验证密码 if ($admin->password !== $this->encryptPassword($password, $admin->ec_salt)) { return false; } return $admin; }
/** * 根据 meta_type 和 meta_id 取得唯一的值 * * @param string $meta_type * @param int $meta_id * @param int $ttl * * @return DataMapper * @throws \InvalidArgumentException */ public function loadMetaByTypeAndId($meta_type, $meta_id, $ttl = 0) { $meta_id = abs(intval($meta_id)); if (empty($meta_type)) { throw new \InvalidArgumentException('meta_type can not be empty'); } $dataMapper = new DataMapper('meta'); $dataMapper->loadOne(array('meta_type = ? and meta_id = ?', $meta_type, $meta_id), null, $ttl); return $dataMapper; }
/** * 增加商品关联 * * @param $f3 */ public function ajaxAddLink($f3) { // 权限检查 $this->requirePrivilege('manage_goods_edit_edit_post', true); // 首先做参数验证 $validator = new Validator($f3->get('GET')); $errorMessage = ''; $goods_id = $validator->required()->digits()->min(1)->validate('goods_id'); $link_goods_id = $validator->required()->digits()->min(1)->validate('link_goods_id'); if (!$this->validate($validator)) { $errorMessage = implode('|', $this->flashMessageArray); goto out_fail; } $dataMapper = new DataMapper('link_goods'); $dataMapper->loadOne(array('goods_id = ? and link_goods_id = ?', $goods_id, $link_goods_id)); // 已经关联了,不要重复关联 if (!$dataMapper->isEmpty()) { goto out; } $authAdminUser = AuthHelper::getAuthUser(); // 添加记录 $dataMapper->goods_id = $goods_id; $dataMapper->link_goods_id = $link_goods_id; $dataMapper->admin_id = $authAdminUser['user_id']; $dataMapper->save(); //清除缓存,确保商品显示正确 ClearHelper::clearGoodsCacheById($goods_id); // 记录商品编辑日志 $goodsLogService = new GoodsLogService(); $goodsLogService->addGoodsLog($goods_id, $authAdminUser['user_id'], $authAdminUser['user_name'], '添加商品关联', $link_goods_id); out: Ajax::header(); echo Ajax::buildResult(null, null, null); return; out_fail: // 失败,返回出错信息 Ajax::header(); echo Ajax::buildResult(-1, $errorMessage, null); }
/** * 用户认证,检查用户名密码是否正确 * * @return mixed 失败-返回false,成功-返回用户信息 * * @param string $username 用户名 * @param string $password 密码原文 * */ public function doAuthSupplier($username, $password) { // 参数验证 $validator = new Validator(array('username' => $username, 'password' => $password)); $username = $validator->required()->validate('username'); $password = $validator->required()->validate('password'); $this->validate($validator); $supplier = new DataMapper('suppliers'); $supplier->loadOne(array('suppliers_account = ?', $username)); if ($supplier->isEmpty()) { return false; } // 验证密码 if ($supplier->password !== $this->encryptPassword($password, $supplier->ec_salt)) { return false; } return $supplier; }
public function doAuthSnsUser($sns_login, $user_name, $email, $autoRegister = true) { global $f3; $user = new DataMapper('users'); $user->loadOne(array('sns_login = ?', $sns_login), array('order' => 'user_id asc')); if (!$user->isEmpty()) { // 记录登录时间和IP地址 $user->last_login = Time::gmTime(); $user->last_ip = $f3->get('IP'); $user->save(); return $user; } if (!$autoRegister) { return false; } // 自动注册用户 $user->sns_login = $sns_login; $user->user_name = $user_name; $user->email = $email; $user->password = uniqid(); // 记录登录时间和IP地址 $user->last_login = $user->reg_time = Time::gmTime(); $user->last_ip = $user->reg_ip = $f3->get('IP'); $user->save(); return $user; }