You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
225 lines
6.1 KiB
225 lines
6.1 KiB
<?php
|
|
|
|
namespace api\logic;
|
|
|
|
use backend\modules\file\models\ars\File;
|
|
use backend\modules\goods\models\ars\Goods;
|
|
use backend\modules\goods\models\ars\GoodsAttr;
|
|
use backend\modules\goods\models\ars\GoodsSku;
|
|
use backend\modules\shop\models\ars\Cart;
|
|
use yii\data\ActiveDataProvider;
|
|
use yii\helpers\Url;
|
|
use yii\web\BadRequestHttpException;
|
|
use yii\web\NotFoundHttpException;
|
|
use Yii;
|
|
use yii\web\ServerErrorHttpException;
|
|
|
|
/**
|
|
* @author iron
|
|
* @email weiriron@gmail.com
|
|
* Class Helper
|
|
* @package api\logic
|
|
*/
|
|
class Helper
|
|
{
|
|
const REQUEST_BAD_PARAMS = '参数缺失或包含无效参数';
|
|
|
|
|
|
public static function timeRandomNum($digit, $Prefix = '')
|
|
{
|
|
$max = pow(10, $digit + 1);
|
|
return $Prefix . date('YmdHis', time()) . rand(0, $max - 1);
|
|
}
|
|
|
|
/**
|
|
* @param $data
|
|
* @param $oid
|
|
* @param $type
|
|
* @throws ServerErrorHttpException
|
|
*/
|
|
public static function saveFileMsg($data, $oid, $type)
|
|
{
|
|
$file = new File();
|
|
$file->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,
|
|
],
|
|
]);
|
|
}
|
|
}
|