objDElement = new DElement('default'); } /** * 添加区块元素 * * @param array $params 数据 * * @return ResultWrapper */ public function addElement($params) { $id = $this->objDElement->insert($params); if ($id === false) { return ResultWrapper::fail($this->objDElement->error(), ErrorCode::$dberror); } return ResultWrapper::success($id); } /** * 获取指定元素信息 * @param $id * @return ResultWrapper */ public function getElementInfoById($id) { $dbResult = $this->objDElement->get_by('id', $id); if ($dbResult === false) { return ResultWrapper::fail($this->objDElement->error(), ErrorCode::$dberror); } return ResultWrapper::success($dbResult); } /** * 编辑指定元素 * * @param int|array $params 修改元素的信息 * * @return ResultWrapper */ public function editElement($params) { if (empty($params['id'])) { return ResultWrapper::fail('没有指定要修改的元素字段id', ErrorCode::$paramError); } $id = $params['id']; unset($params['id']); $dbResult = $this->objDElement->update($params, ['id' => $id]); if ($dbResult === false) { return ResultWrapper::fail($this->objDElement->error(), ErrorCode::$dberror); } return ResultWrapper::success($dbResult); } /** * 删除指定元素 * * @param array $ids 要删除元素字段id * * @return ResultWrapper */ public function delElement($ids) { if (empty($ids)) { return ResultWrapper::fail('参数错误', ErrorCode::$paramError); } $dbResult = $this->objDElement->update(['deleteStatus' => StatusCode::$delete], $ids); if ($dbResult === false) { return ResultWrapper::fail($this->objDElement->error(), ErrorCode::$dberror); } return ResultWrapper::success($dbResult); } /** * 获取指定区块下的所有元素 * @param $selectParams * @return ResultWrapper */ public function getElementByZoneId($selectParams) { $time = time();//当前时间 $limit = $selectParams['limit']; unset($selectParams['limit']); $offset = $selectParams['offset']; unset($selectParams['offset']); $sql = "`zoneId` = " . $selectParams['zoneId'] . " AND `deleteStatus` = " . StatusCode::$standard; if (!isset($selectParams['type'])) { $sql .= " AND `startTime` <= " . $time . " AND `endTime` >= " . $time;//后台用 } //$selectParams['deleteStatus'] = StatusCode::$standard; $dbResult = $this->objDElement->select($sql, '*', 'sort DESC', $limit, $offset); if ($dbResult === false) { return ResultWrapper::fail($this->objDElement->error(), ErrorCode::$dberror); } $total = $this->objDElement->count($sql); $return = [ 'data' => self::format($dbResult), 'total' => ($total) ? intval($total) : 0, ]; return ResultWrapper::success($return); } /** * 格式化返回数据 * @param $data * @return array */ private function format($data) { foreach ($data as &$v) { if (isset($v['elementData'])) { $v['elementData'] = json_decode($v['elementData'], true); if (!empty($v['elementData'])) { if (!is_array($v['elementData'])) { return $data; } foreach ($v['elementData'] as &$val) { if (in_array($val['key'], ['image', 'img'])) { $val['val'] = URL_UPLOAD . $val['val']; } } } } } return $data; } }