예제 #1
0
 public function actionSubtract()
 {
     $goodsId = (int) Yii::$app->request->post('goodsId');
     $output = ['status' => 'fail', 'data' => []];
     $goods = Goods::findOne(['id' => $goodsId, 'status' => Goods::STATUS_NORMAL]);
     if (!$goods) {
         return $output;
     }
     $model = CartGoods::findOne(['goods_id' => $goodsId, 'user_id' => Yii::$app->user->id]);
     if ($model) {
         $model->count--;
         if ($model->count < 1) {
             $model->delete();
         } else {
             if ($model->count > $goods->surplus) {
                 $model->count = $goods->surplus;
             }
             if (!$model->save(false)) {
                 return $output;
             }
         }
     }
     $output = ['status' => 'success', 'data' => ['name' => $goods->name, 'price' => $goods->price, 'surplus' => $goods->surplus, 'cart' => $model ? $model->count : 0]];
     return $output;
 }
예제 #2
0
 public function actionDetail($id)
 {
     $model = Goods::findOne(['id' => $id, 'status' => Goods::STATUS_NORMAL]);
     if (!$model) {
         throw new NotFoundHttpException('未找到该商品');
     }
     $output = ['status' => 'ok', 'name' => $model->name, 'price' => $model->price, 'surplus' => $model->surplus, 'description' => '【笑e购】' . $model->description, 'cart' => Yii::$app->user->isGuest ? 0 : Yii::$app->user->identity->getCartGoodsQuantity($id), 'image' => $model->images ? Url::toGoods($model->images[0]->name) : Yii::$app->params['goods.defaultImageUrl']];
     Yii::$app->response->format = Response::FORMAT_JSON;
     return $output;
 }
예제 #3
0
 /**
  * Creates a form model given a goods id.
  *
  * @param  string                          $id
  * @param  array                           $config name-value pairs that will be used to initialize the object properties
  * @throws \yii\base\InvalidParamException if goods id is empty or not valid
  */
 public function __construct($id, $config = [])
 {
     if (empty($id) || !is_numeric($id)) {
         throw new InvalidParamException('参数错误!');
     }
     $this->_goods = Goods::findOne($id);
     if (!$this->_goods) {
         throw new InvalidParamException('未找到该商品!');
     }
     parent::__construct($config);
 }
예제 #4
0
 /**
  * 从某个商品创建副本
  * 
  * @param string $goodsId
  * @return ApplyGoods
  */
 public static function createDuplicate($goodsId)
 {
     $goods = Goods::findOne($goodsId);
     $model = new self();
     $model->goods_id = $goodsId;
     $model->name = $goods->name;
     $model->category = $goods->category->name;
     $model->price = $goods->price;
     $model->cover = $goods->cover;
     $model->unit = $goods->unit;
     return $model;
 }
예제 #5
0
 public function actionSurplus($id)
 {
     /* @var $model Goods */
     $model = Goods::findOne(['id' => $id, 'store_id' => Yii::$app->user->identity->store_id]);
     if (!$model) {
         throw new NotFoundHttpException('未找到该商品。');
     }
     $dataProvider = new ActiveDataProvider(['query' => GoodsSurplus::find()->where(['goods_id' => $id]), 'sort' => ['defaultOrder' => ['id' => SORT_DESC]], 'pagination' => ['pageSize' => 20]]);
     return $this->render('surplus', ['model' => $model, 'dataProvider' => $dataProvider]);
 }
예제 #6
0
 /**
  * Finds the Goods model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param integer $id
  * @return Goods the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = Goods::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }