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.

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