From f2ff95e1ceb1e427332661066ca3522136db3a8f Mon Sep 17 00:00:00 2001 From: root Date: Fri, 24 Feb 2017 16:16:00 +0800 Subject: [PATCH] =?UTF-8?q?fecshop=20=E6=94=B6=E8=97=8F=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- services/payment/Paypal.php | 12 ++- services/product/Favorite.php | 168 ++++++++++++++++++++++++++++++++++ 2 files changed, 178 insertions(+), 2 deletions(-) create mode 100644 services/product/Favorite.php diff --git a/services/payment/Paypal.php b/services/payment/Paypal.php index b038f416..9c42ebe3 100644 --- a/services/payment/Paypal.php +++ b/services/payment/Paypal.php @@ -89,7 +89,7 @@ class Paypal extends Service return $verifyUrl; } - protected function curlGet($url){ + protected function curlGet($url,$i=0){ $ch = curl_init($url); curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1); curl_setopt($ch, CURLOPT_POST, 1); @@ -98,7 +98,7 @@ class Paypal extends Service curl_setopt($ch, CURLOPT_SSLVERSION, 6); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); - + curl_setopt($ch, CURLOPT_TIMEOUT,30); // This is often required if the server is missing a global cert bundle, or is using an outdated one. if ($this->use_local_certs) { curl_setopt($ch, CURLOPT_CAINFO, __DIR__ . "/cert/paypal.crt"); @@ -107,6 +107,14 @@ class Paypal extends Service curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Connection: Close')); $httpResponse = curl_exec($ch); + if(!$httpResponse){ + $i++; + if($i<=5){ + return $this->curlGet($url,$i); + }else{ + return $httpResponse; + } + } return $httpResponse; } diff --git a/services/product/Favorite.php b/services/product/Favorite.php new file mode 100644 index 00000000..33195bab --- /dev/null +++ b/services/product/Favorite.php @@ -0,0 +1,168 @@ + + * @since 1.0 + */ +class Favorite extends Service +{ + + protected function actionGetPrimaryKey(){ + return '_id'; + } + + protected function actionGetByPrimaryKey($val){ + $one = FavoriteModel::findOne($val); + if($one[$this->getPrimaryKey()]){ + return $one; + }else{ + return new FavoriteModel; + } + } + + protected function actionGetByProductIdAndUserId($product_id,$user_id=''){ + + if(!$user_id){ + $identity = Yii::$app->user->identity; + $user_id = $identity['id']; + } + if($user_id){ + $one = FavoriteModel::findOne([ + 'product_id' => $product_id, + 'user_id' => $user_id, + ]); + if($one[$this->getPrimaryKey()]){ + return $one; + } + } + } + + protected function actionAdd($product_id,$user_id){ + $user_id = (int)$user_id; + $productPrimaryKey = Yii::$service->product->getPrimaryKey(); + $product = Yii::$service->product->getByPrimaryKey($product_id); + # 检查产品是否存在,如果不存在,输出报错信息。 + if(!isset($product[$productPrimaryKey])){ + Yii::$service->helper->errors->add('product is not exist!'); + return ; + } + //echo $product_id;exit; + $favoritePrimaryKey = Yii::$service->product->favorite->getPrimaryKey(); + $one = FavoriteModel::findOne([ + 'product_id' => $product_id, + 'user_id' => $user_id, + ]); + if(isset($one[$favoritePrimaryKey])){ + $one->updated_at = time(); + $one->store = Yii::$service->store->currentStore; + $one->save(); + return true; + } + $one = new FavoriteModel; + $one->product_id = $product_id; + $one->user_id = $user_id; + $one->created_at = time(); + $one->updated_at = time(); + $one->store = Yii::$service->store->currentStore; + $one->save(); + # 更新该用户总的收藏产品个数到用户表 + $this->updateUserFavoriteCount($user_id); + $this->updateProductFavoriteCount($product_id); + return true; + } + /** + * @property $product_id | String + * 更新该产品被收藏的总个数。 + */ + protected function updateProductFavoriteCount($product_id){ + if($product_id){ + $count = FavoriteModel::find()->where(['product_id'=>$product_id])->count(); + $product = Yii::$service->product->getByPrimaryKey($product_id); + if($product['_id']){ + $product->favorite_count = $count; + $product->save(); + } + } + + } + /** + * @property $user_id | Int + * 更新该用户总的收藏产品个数到用户表 + */ + protected function updateUserFavoriteCount($user_id = ''){ + $identity = Yii::$app->user->identity; + if(!$user_id){ + $user_id = $identity['id']; + } + if($user_id){ + $count = FavoriteModel::find()->where(['user_id'=>$user_id])->count(); + $identity->favorite_product_count = $count; + $identity->save(); + } + } + /* + * example filter: + * [ + * 'numPerPage' => 20, + * 'pageNum' => 1, + * 'orderBy' => [$this->getPrimaryKey() => SORT_DESC, 'sku' => SORT_ASC ], + * 'where' => [ + ['>','price',1], + ['<=','price',10] + * ['sku' => 'uk10001'], + * ], + * 'asArray' => true, + * ] + */ + protected function actionList($filter){ + $query = FavoriteModel::find(); + $query = Yii::$service->helper->ar->getCollByFilter($query,$filter); + return [ + 'coll' => $query->all(), + 'count'=> $query->count(), + ]; + + } + + protected function actionColl($filter){ + return $this->list($filter); + } + /** + * @property $favorite_id|String + * 通过id删除favorite + */ + protected function actionCurrentUserRemove($favorite_id){ + $identity = Yii::$app->user->identity; + $user_id = $identity['id']; + + $one = FavoriteModel::findOne([ + '_id' => new \MongoId($favorite_id), + 'user_id' => $user_id, + ]); + if($one['_id']){ + $one->delete(); + $this->updateUserFavoriteCount($user_id); + $this->updateProductFavoriteCount($product_id); + return true; + } + + return; + } + + + +} \ No newline at end of file -- GitLab