type = 1; $file->own_type = $type; $file->own_id = $oid; $file->path = $data['fileName']; $file->alias = $data['alias']; if (!$file->save()) { throw new ServerErrorHttpException('服务器保存文件失败'); } } /** * @param $dirs * @param $file * @return array * @throws ServerErrorHttpException */ public static function uploadImage($dirs, $file) { $savePath = Yii::getAlias('@app') . '/web/uploads'; if (!is_dir($savePath)) { mkdir($savePath, 0755); } foreach ($dirs as $dir) { $savePath .= "/$dir"; if (!is_dir($savePath)) { mkdir($savePath, 0755); } } $fileName = time() . rand(0, 9999) . '.' . $file->extension; $path = $savePath . $fileName; if (!$file->saveAs($path)) { throw new ServerErrorHttpException('服务器保存图片失败'); } return ['fileName' => '/uploads/' . $dirs . '/' . $fileName, 'alias' => $file->baseName . $file->extension]; } /** * @param $array * @return string */ public static function errorMessageStr($array): string { $data = []; foreach ($array as $k => $v) { $data[] = implode(' ', $v); } return implode(' ', $data); } /** * @param array $ids 购物车id 数组 * @throws NotFoundHttpException * @throws ServerErrorHttpException * @throws \Throwable * @throws \yii\db\StaleObjectException */ public static function delCarts($ids) { foreach ($ids as $id) { $cart = Cart::find() ->where(['id' => $id]) ->andWhere(['user_id' => Yii::$app->user->getId()]) ->one(); if (!$cart) { throw new NotFoundHttpException("未找到该购物车"); } if (!$cart->delete()) { throw new ServerErrorHttpException('服务器删除购物车失败'); } } } /** * @param $goodsId * @param $count * @param null $skuId * @return bool true代表无限库存,false代表有限库存需要进行增删库存操作 * 判断库存 * @throws NotFoundHttpException * @throws BadRequestHttpException */ public static function checkStock($goodsId, $count, $skuId = null) { $sku = GoodsSku::findOne($skuId); $goods = Goods::findOne($goodsId); if (!$goods) { throw new NotFoundHttpException('商品未找到'); } if ($goods->stock == Goods::UNLIMITED_STOCK) { return true; } if ($sku && $sku->stock == Goods::UNLIMITED_STOCK) { return true; } $stock = $sku ? $sku->stock : $goods->stock; if ($stock < $count) { throw new BadRequestHttpException("{$goods->name}库存不足"); } return false; } /** * @param $model * @param $view * post的返回头设置 */ public static function createdResponse($model, $view) { $response = Yii::$app->getResponse()->setStatusCode(201); $id = implode(',', array_values($model->getPrimaryKey(true))); $response->getHeaders()->set('Location', Url::toRoute([$view, 'id' => $id], true)); } /** * @param $goodsId * @param $skuId * @return int * @throws NotFoundHttpException * 计算商品价格 */ public static function goodsPrice($goodsId, $skuId) { $goods = Goods::findOne($goodsId); if (!$goods) { throw new NotFoundHttpException('商品未找到'); } $price = $goods->price; $sku = GoodsSku::findOne($skuId); if ($sku) { $price = $sku->price; } return $price; } /** * @param $skuId * @return string * @throws NotFoundHttpException * 获取拼接后的sku值 */ public static function skuValue($skuId) { $sku = GoodsSku::findOne($skuId); if (!$sku) { return ''; } $data = []; $goodsAttr = array_filter(explode(',', $sku->goods_attr)); if (empty($goodsAttr)) { throw new NotFoundHttpException('sku无对应商品属性'); } foreach ($goodsAttr as $k => $v) { $attr = GoodsAttr::findOne($v); if (!$attr) { throw new NotFoundHttpException('商品属性未找到'); } $data[] = $attr->attr_value; } return implode(',', $data); } /** * @param $query * @return object * @throws \yii\base\InvalidConfigException * 获取数据 */ public static function index($query) { $requestParams = Yii::$app->request->getBodyParams(); return Yii::createObject([ 'class' => ActiveDataProvider::className(), 'query' => $query, 'pagination' => [ 'params' => $requestParams, ], 'sort' => [ 'params' => $requestParams, ], ]); } }