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.

208 lines
5.7 KiB

  1. <?php
  2. namespace api\logic;
  3. use backend\modules\file\models\ars\File;
  4. use backend\modules\goods\models\ars\Goods;
  5. use backend\modules\goods\models\ars\GoodsAttr;
  6. use backend\modules\goods\models\ars\GoodsSku;
  7. use backend\modules\shop\models\ars\Cart;
  8. use yii\data\ActiveDataProvider;
  9. use yii\di\Instance;
  10. use yii\helpers\Url;
  11. use yii\web\BadRequestHttpException;
  12. use yii\web\NotFoundHttpException;
  13. use Yii;
  14. use yii\web\ServerErrorHttpException;
  15. /**
  16. * @author iron
  17. * @email weiriron@gmail.com
  18. * Class Helper
  19. * @package api\logic
  20. */
  21. class Helper
  22. {
  23. const REQUEST_BAD_PARAMS = '参数缺失或包含无效参数';
  24. public static function saveFileMsg($data, $oid, $type)
  25. {
  26. $file = new File();
  27. $file->type = 1;
  28. $file->own_type = $type;
  29. $file->own_id = $oid;
  30. $file->path = $data['fileName'];
  31. $file->alias = $data['alias'];
  32. if (!$file->save()) {
  33. throw new ServerErrorHttpException('服务器保存文件失败');
  34. }
  35. }
  36. /**
  37. * @param $dirs
  38. * @param $file
  39. * @return array
  40. * @throws ServerErrorHttpException
  41. */
  42. public static function uploadImage($dirs, $file)
  43. {
  44. $savePath = Yii::getAlias('@app') . '/web/uploads';
  45. if (!is_dir($savePath)) {
  46. mkdir($savePath, 0755);
  47. }
  48. foreach ($dirs as $dir) {
  49. $savePath .= "/$dir";
  50. if (!is_dir($savePath)) {
  51. mkdir($savePath, 0755);
  52. }
  53. }
  54. $fileName = time() . rand(0, 9999) . '.' . $file->extension;
  55. $path = $savePath . $fileName;
  56. if (!$file->saveAs($path)) {
  57. throw new ServerErrorHttpException('服务器保存图片失败');
  58. }
  59. return ['fileName' => '/uploads/' . $dirs . '/' . $fileName, 'alias' => $file->baseName . $file->extension];
  60. }
  61. /**
  62. * @param $array
  63. * @return string
  64. */
  65. public static function errorMessageStr($array): string
  66. {
  67. $data = [];
  68. foreach ($array as $k => $v) {
  69. $data[] = implode(' ', $v);
  70. }
  71. return implode(' ', $data);
  72. }
  73. /**
  74. * @param array $ids 购物车id 数组
  75. * @throws NotFoundHttpException
  76. * @throws ServerErrorHttpException
  77. * @throws \Throwable
  78. * @throws \yii\db\StaleObjectException
  79. */
  80. public static function delCarts($ids)
  81. {
  82. foreach ($ids as $id) {
  83. $cart = Cart::find()
  84. ->where(['id' => $id])
  85. ->andWhere(['user_id' => Yii::$app->user->getId()])
  86. ->one();
  87. if (!$cart) {
  88. throw new NotFoundHttpException("未找到该购物车");
  89. }
  90. if (!$cart->delete()) {
  91. throw new ServerErrorHttpException('服务器删除购物车失败');
  92. }
  93. }
  94. }
  95. /**
  96. * @param $goodsId
  97. * @param $count
  98. * @param null $skuId
  99. * @throws BadRequestHttpException
  100. * @throws NotFoundHttpException
  101. * 判断库存
  102. */
  103. public static function checkStock($goodsId, $count, $skuId = null)
  104. {
  105. $sku = GoodsSku::findOne($skuId);
  106. $goods = Goods::findOne($goodsId);
  107. if (!$goods) {
  108. throw new NotFoundHttpException('商品未找到');
  109. }
  110. if (!$sku && $goods->stock == Goods::UNLIMITED_STOCK) {
  111. return;
  112. }
  113. $stock = $sku ? $sku->stock : $goods->stock;
  114. if ($stock < $count) {
  115. throw new BadRequestHttpException("{$goods->name}库存不足");
  116. }
  117. return;
  118. }
  119. /**
  120. * @param $model
  121. * @param $view
  122. * post的返回头设置
  123. */
  124. public static function createdResponse($model, $view)
  125. {
  126. $response = Yii::$app->getResponse()->setStatusCode(201);
  127. $id = implode(',', array_values($model->getPrimaryKey(true)));
  128. $response->getHeaders()->set('Location', Url::toRoute([$view, 'id' => $id], true));
  129. }
  130. /**
  131. * @param $goodsId
  132. * @param $skuId
  133. * @return int
  134. * @throws NotFoundHttpException
  135. * 计算商品价格
  136. */
  137. public static function goodsPrice($goodsId, $skuId)
  138. {
  139. $goods = Goods::findOne($goodsId);
  140. if (!$goods) {
  141. throw new NotFoundHttpException('商品未找到');
  142. }
  143. $price = $goods->price;
  144. $sku = GoodsSku::findOne($skuId);
  145. if ($sku) {
  146. $price = $sku->price;
  147. }
  148. return $price;
  149. }
  150. /**
  151. * @param $skuId
  152. * @return string
  153. * @throws NotFoundHttpException
  154. * 获取拼接后的sku值
  155. */
  156. public static function skuValue($skuId)
  157. {
  158. $sku = GoodsSku::findOne($skuId);
  159. if (!$sku) {
  160. return '';
  161. }
  162. $data = [];
  163. $goodsAttr = array_filter(explode(',', $sku->goods_attr));
  164. if (empty($goodsAttr)) {
  165. throw new NotFoundHttpException('sku无对应商品属性');
  166. }
  167. foreach ($goodsAttr as $k => $v) {
  168. $attr = GoodsAttr::findOne($v);
  169. if (!$attr) {
  170. throw new NotFoundHttpException('商品属性未找到');
  171. }
  172. $data[] = $attr->attr_value;
  173. }
  174. return implode(',', $data);
  175. }
  176. /**
  177. * @param $query
  178. * @return object
  179. * @throws \yii\base\InvalidConfigException
  180. * 获取数据
  181. */
  182. public static function index($query)
  183. {
  184. $requestParams = Yii::$app->request->getBodyParams();
  185. return Yii::createObject([
  186. 'class' => ActiveDataProvider::className(),
  187. 'query' => $query,
  188. 'pagination' => [
  189. 'params' => $requestParams,
  190. ],
  191. 'sort' => [
  192. 'params' => $requestParams,
  193. ],
  194. ]);
  195. }
  196. }