code
stringlengths
15
9.96M
docstring
stringlengths
1
10.1k
func_name
stringlengths
1
124
language
stringclasses
1 value
repo
stringlengths
7
63
path
stringlengths
6
186
url
stringlengths
50
236
license
stringclasses
4 values
public function addGroupAttrs($attr_group) { return $this->_product->addGroupAttrs($attr_group); }
@param $attr_group | String , 属性组名称 给product model 增加相应的属性组对应的属性。
addGroupAttrs
php
fecshop/yii2_fecshop
services/Product.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Product.php
BSD-3-Clause
public function apicoll() { return $this->_product->apicoll(); }
api部分 和coll()的不同在于,该方式不走active record,因此可以获取产品的所有数据的。
apicoll
php
fecshop/yii2_fecshop
services/Product.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Product.php
BSD-3-Clause
public function apiGetByPrimaryKey($primaryKey) { return $this->_product->apiGetByPrimaryKey($primaryKey); }
api部分
apiGetByPrimaryKey
php
fecshop/yii2_fecshop
services/Product.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Product.php
BSD-3-Clause
public function apiSave($product_one) { return $this->_product->apiSave($product_one); }
api部分
apiSave
php
fecshop/yii2_fecshop
services/Product.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Product.php
BSD-3-Clause
public function apiDelete($primaryKey) { return $this->_product->apiDelete($primaryKey); }
api部分
apiDelete
php
fecshop/yii2_fecshop
services/Product.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Product.php
BSD-3-Clause
public function getModelName() { return get_class($this->_product->getByPrimaryKey()); }
得到Product model的全名.
getModelName
php
fecshop/yii2_fecshop
services/Product.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Product.php
BSD-3-Clause
public function getBySku($sku, $returnArr = true) { return $this->_product->getBySku($sku, $returnArr); }
@param $sku | string @param $returnArr | boolean , 是否返回数组格式 通过sku查询产品
getBySku
php
fecshop/yii2_fecshop
services/Product.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Product.php
BSD-3-Clause
public function getBySpu($spu) { return $this->_product->getBySpu($spu); }
@param $spu | string 通过spu查询产品
getBySpu
php
fecshop/yii2_fecshop
services/Product.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Product.php
BSD-3-Clause
public function coll($filter = []) { return $this->_product->coll($filter); }
@param $filter|array get artile collection by $filter example filter: [ 'numPerPage' => 20, 'pageNum' => 1, 'orderBy' => ['_id' => SORT_DESC, 'sku' => SORT_ASC ], 'where' => [ ['>','price',1], ['<=','price',10] ['sku' => 'uk10001'], ], 'asArray' => true, ] 根据传入的查询条件,得到产品的列表
coll
php
fecshop/yii2_fecshop
services/Product.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Product.php
BSD-3-Clause
public function getProducts($filter) { return $this->_product->getProducts($filter); }
通过where条件 和 查找的select 字段信息,得到产品的列表信息, 这里一般是用于前台的区块性的不分页的产品查找。 结果数据没有进行进一步处理,需要前端获取数据后在处理。
getProducts
php
fecshop/yii2_fecshop
services/Product.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Product.php
BSD-3-Clause
public function getCategoryProductIds($product_id_arr, $category_id) { return $this->_product->getCategoryProductIds($product_id_arr, $category_id); }
@param $product_id_arr | Array @param $category_id | String 在给予的产品id数组$product_id_arr中,找出来那些产品属于分类 $category_id 该功能是后台分类编辑中,对应的分类产品列表功能 也就是在当前的分类下,查看所有的产品,属于当前分类的产品,默认被勾选。
getCategoryProductIds
php
fecshop/yii2_fecshop
services/Product.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Product.php
BSD-3-Clause
public function save($one, $originUrlKey = 'catalog/product/index') { return $this->_product->save($one, $originUrlKey); }
@param $one|array , 产品数据数组 @param $originUrlKey|string , 分类的原来的url key ,也就是在前端,分类的自定义url。 保存产品(插入和更新),以及保存产品的自定义url 如果提交的数据中定义了自定义url,则按照自定义url保存到urlkey中,如果没有自定义urlkey,则会使用name进行生成。
save
php
fecshop/yii2_fecshop
services/Product.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Product.php
BSD-3-Clause
public function remove($ids) { return $this->_product->remove($ids); }
@param $ids | Array or String 删除产品,如果ids是数组,则删除多个产品,如果是字符串,则删除一个产品 在产品产品的同时,会在url rewrite表中删除对应的自定义url数据。
remove
php
fecshop/yii2_fecshop
services/Product.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Product.php
BSD-3-Clause
public function addAndDeleteProductCategory($category_id, $addCateProductIdArr, $deleteCateProductIdArr) { return $this->_product->addAndDeleteProductCategory($category_id, $addCateProductIdArr, $deleteCateProductIdArr); }
@param $category_id | String 分类的id的值 @param $addCateProductIdArr | Array 分类中需要添加的产品id数组,也就是给这个分类增加这几个产品。 @param $deleteCateProductIdArr | Array 分类中需要删除的产品id数组,也就是在这个分类下面去除这几个产品的对应关系。 这个函数是后台分类编辑功能中使用到的函数,在分类中可以一次性添加多个产品,也可以删除多个产品,产品和分类是多对多的关系。
addAndDeleteProductCategory
php
fecshop/yii2_fecshop
services/Product.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Product.php
BSD-3-Clause
public function getFrontCategoryProducts($filter) { return $this->_product->getFrontCategoryProducts($filter); }
[ 'category_id' => 1, 'pageNum' => 2, 'numPerPage' => 50, 'orderBy' => 'name', 'where' => [ ['>','price',11], ['<','price',22], ], 'select' => ['xx','yy'], 'group' => '$spu', ] 得到分类下的产品,在这里需要注意的是: 1.同一个spu的产品,有很多sku,但是只显示score最高的产品,这个score可以通过脚本取订单的销量(最近一个月,或者 最近三个月等等),或者自定义都可以。 2.结果按照filter里面的orderBy排序 3.由于使用的是mongodb的aggregate(管道)函数,因此,此函数有一定的限制,就是该函数 处理后的结果不能大约32MB,因此,如果一个分类下面的产品几十万的时候可能就会出现问题, 这种情况可以用专业的搜索引擎做聚合工具。 不过,对于一般的用户来说,这个不会成为瓶颈问题,一般一个分类下的产品不会出现几十万的情况。 4.最后就得到spu唯一的产品列表(多个spu相同,sku不同的产品,只要score最高的那个).
getFrontCategoryProducts
php
fecshop/yii2_fecshop
services/Product.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Product.php
BSD-3-Clause
public function getFrontCategoryFilter($filter_attr, $where) { return $this->_product->getFrontCategoryFilter($filter_attr, $where); }
@param $filter_attr | String 需要进行统计的字段名称 @propertuy $where | Array 搜索条件。这个需要些mongodb的搜索条件。 得到的是个属性,以及对应的个数。 这个功能是用于前端分类侧栏进行属性过滤。
getFrontCategoryFilter
php
fecshop/yii2_fecshop
services/Product.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Product.php
BSD-3-Clause
public function getSkusByIds($ids) { return $this->_product->getSkusByIds($ids); }
@param $ids | Array 通过产品ids得到产品sku
getSkusByIds
php
fecshop/yii2_fecshop
services/Product.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Product.php
BSD-3-Clause
public function updateProductReviewInfo($spu, $avag_rate, $count, $lang_code, $avag_lang_rate, $lang_count, $rate_total_arr, $rate_lang_total_arr) { return $this->_product->updateProductReviewInfo($spu, $avag_rate, $count, $lang_code, $avag_lang_rate, $lang_count, $rate_total_arr, $rate_lang_total_arr); }
@param $spu | String @param $avag_rate | Int 产品的总平均得分 @param $count | Int 产品的总评论数 @param $avag_lang_rate | 当前语言的总平均得分 @param $lang_count | 当前语言的总评论数
updateProductReviewInfo
php
fecshop/yii2_fecshop
services/Product.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Product.php
BSD-3-Clause
public function __call($originMethod, $arguments) { if (isset($this->_callFuncLog[$originMethod])) { $method = $this->_callFuncLog[$originMethod]; } else { $method = 'action'.ucfirst($originMethod); $this->_callFuncLog[$originMethod] = $method; } if (method_exists($this, $method)) { $this->beginCall($originMethod, $arguments); $return = call_user_func_array([$this, $method], $arguments); $this->endCall($originMethod, $arguments); return $return; } else { throw new InvalidCallException('fecshop service method is not exit. '.get_class($this)."::$method"); } }
通过call函数,去调用actionXxxx方法。
__call
php
fecshop/yii2_fecshop
services/Service.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Service.php
BSD-3-Clause
public function getChildService($childServiceName) { //var_dump($this->childService['xunSearch']);exit; if (!isset($this->_childService[$childServiceName]) || !$this->_childService[$childServiceName]) { $childService = $this->childService; if (isset($childService[$childServiceName])) { $service = $childService[$childServiceName]; if (!isset($service['enableService']) || $service['enableService'] !== false) { $this->_childService[$childServiceName] = Yii::createObject($service); } else { throw new InvalidConfigException('Child Service ['.$childServiceName.'] is disable in '.get_called_class().', you must config it! '); } } else { throw new InvalidConfigException('Child Service ['.$childServiceName.'] is not find in '.get_called_class().', you must config it! '); } } return isset($this->_childService[$childServiceName]) ? $this->_childService[$childServiceName] : null; }
得到services 里面配置的子服务childService的实例.
getChildService
php
fecshop/yii2_fecshop
services/Service.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Service.php
BSD-3-Clause
public function getAllChildServiceName() { $childService = $this->childService; $arr = []; if (is_array($childService) && !empty($childService)) { foreach ($childService as $childName => $service) { if ($service['enableService'] !== false) { $arr[] = $childName; } } } return $arr; }
得到所有的子服务 如果子服务含有enableService字段,并且设置成false,则该子服务会被判定为关闭
getAllChildServiceName
php
fecshop/yii2_fecshop
services/Service.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Service.php
BSD-3-Clause
protected function beginCall($originMethod, $arguments) { if (Yii::$app->serviceLog->isServiceLogEnable()) { $this->_beginCallTime = microtime(true); } }
如果开启service log,则记录开始的时间。
beginCall
php
fecshop/yii2_fecshop
services/Service.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Service.php
BSD-3-Clause
protected function endCall($originMethod, $arguments) { if (Yii::$app->serviceLog->isServiceLogEnable()) { list($logTrace, $isCalledByThis) = $this->debugBackTrace(); // if function is called by $this ,not log it to mongodb. if ($isCalledByThis) { return; } $begin_microtime = $this->_beginCallTime; $endCallTime = microtime(true); $used_time = round(($endCallTime - $begin_microtime), 4); if (is_object($arguments)) { $arguments = 'object'; } elseif (is_array($arguments)) { $arguments = 'array'; } else { $arguments = 'string or int or other'; } $serviceLogUid = Yii::$app->serviceLog->getLogUid(); $log_info = [ 'service_uid' => $serviceLogUid, 'current_url' => Yii::$service->url->getCurrentUrl(), 'home_url' => Yii::$service->url->homeUrl(), 'service_file' => get_class($this), 'service_method' => $originMethod, 'service_method_argument' => $arguments, 'begin_microtime' => $begin_microtime, 'end_microtime' => $endCallTime, 'used_time' => $used_time, 'process_date_time' => date('Y-m-d H:i:s'), 'log_trace' => $logTrace, ]; Yii::$app->serviceLog->printServiceLog($log_info); } }
@param $originMethod and $arguments,魔术方法传递的参数 调用service后,调用endCall,目前用来记录log信息 1. 如果service本身的调用,则不会记录,只会记录外部函数调用service 2. 同一次访问的service_uid 的值是一样的,这样可以把一次访问调用的serice找出来。
endCall
php
fecshop/yii2_fecshop
services/Service.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Service.php
BSD-3-Clause
protected function debugBackTrace() { $arr = []; $isCalledByThis = false; $d = debug_backtrace(); $funcNotContainArr = [ '__call', 'endCall', 'debugBackTrace', 'call_user_func_array', ]; $thisClass = get_class($this); //echo '**'.$thisClass.'**'; $i = 0; $last_invoke_class = ''; $last_sec_invoke_class = ''; foreach ($d as $e) { $function = $e['function']; $class = $e['class']; //echo '**'.$class.'**'; $file = $e['file']; $line = $e['line']; if ($file && !in_array($function, $funcNotContainArr)) { $arr[] = $file.'('.$line.'),'.$class.'::'.$function.'()'; $i++; if ($i === 1) { $last_invoke_class = $class; } elseif ($i === 2) { $last_sec_invoke_class = $class; } } } if ($last_invoke_class === $last_sec_invoke_class) { $isCalledByThis = true; } return [$arr, $isCalledByThis]; }
debug 追踪 返回调用当前service的所有的文件。以及 行,类,类方法 。 这几个方法将不会被记录:'__call','endCall','debugBackTrace','call_user_func_array' 如果$file 不存在,也不会记录。 @return Array, $arr里面存储执行的记录,$isCalledByThis 代表是否是当前的service内部方法调用, article 服务方法,在执行过程中,调用了一个内部的方法,追踪函数也会记录这个。
debugBackTrace
php
fecshop/yii2_fecshop
services/Service.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Service.php
BSD-3-Clause
public function saveRewriteUrlKeyByStr($str, $originUrl, $originUrlKey, $type = 'system') { $str = trim($str); $originUrl = $originUrl ? '/'.trim($originUrl, '/') : ''; $originUrlKey = $originUrlKey ? '/'.trim($originUrlKey, '/') : ''; if ($originUrlKey && $originUrl) { /** * if originUrlKey and originUrl is exist in url rewrite collectons. */ $model = $this->find(); // 如果已经存在,那么直接返回 $data_one = $model->where([ 'custom_url_key' => $originUrlKey, 'origin_url' => $originUrl, ])->one(); if (isset($data_one['custom_url_key'])) { return $originUrlKey; } } if ($originUrlKey) { $urlKey = $this->generateUrlByName($originUrlKey); } else { $urlKey = $this->generateUrlByName($str); } if (strlen($urlKey) <= 1) { $urlKey .= $this->getRandom(); } if (strlen($urlKey) <= 2) { $urlKey .= '-'.$this->getRandom(); } $urlKey = $this->getRewriteUrlKey($urlKey, $originUrl); $UrlRewrite = $this->findOne([ 'origin_url' => $originUrl, ]); if (!isset($UrlRewrite['origin_url'])) { $UrlRewrite = $this->newModel(); $UrlRewrite->created_at = time(); } $UrlRewrite->updated_at = time(); $UrlRewrite->type = $type; $UrlRewrite->custom_url_key = $urlKey; $UrlRewrite->origin_url = $originUrl; $UrlRewrite->save(); return $urlKey; }
save custom url to mongodb collection url_rewrite. @param $str|String, example: fashion handbag women @param $originUrl|string , origin url ,example: /cms/home/index?id=5 @param $originUrlKey|String,origin url key, it can be empty ,or generate by system , or custom url key. @param $type|String, url rewrite type. @return rewrite Key.
saveRewriteUrlKeyByStr
php
fecshop/yii2_fecshop
services/Url.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Url.php
BSD-3-Clause
public function saveRewriteUrlKeyByUpsert($str, $originUrl, $originUrlKey, $type = 'system') { $str = trim($str); $originUrl = $originUrl ? '/'.trim($originUrl, '/') : ''; $originUrlKey = $originUrlKey ? '/'.trim($originUrlKey, '/') : ''; if (!$originUrlKey || !$originUrl) { return; } /** * if originUrlKey and originUrl is exist in url rewrite collectons. */ $model = $this->find(); // 如果已经存在,那么直接返回 $data_one = $model->where([ 'custom_url_key' => $originUrlKey, 'origin_url' => $originUrl, ])->one(); if (isset($data_one['custom_url_key'])) { return $originUrlKey; } // 清除掉相同 custom_url_key 的内容 $model = $this->find(); $mOne = $model->where(['custom_url_key' => $originUrlKey])->one(); if ($mOne) { $mOne->delete(); } $UrlRewrite = $this->findOne([ 'origin_url' => $originUrl, ]); if (!isset($UrlRewrite['origin_url'])) { $UrlRewrite = $this->newModel(); $UrlRewrite->created_at = time(); } $UrlRewrite->updated_at = time(); $UrlRewrite->type = $type; $UrlRewrite->custom_url_key = $originUrlKey; $UrlRewrite->origin_url = $originUrl; $UrlRewrite->save(); return $originUrlKey; }
save custom url to mongodb collection url_rewrite. @param $str|String, example: fashion handbag women @param $originUrl|string , origin url ,example: /cms/home/index?id=5 @param $originUrlKey|String,origin url key, it can be empty ,or generate by system , or custom url key. @param $type|String, url rewrite type. @return rewrite Key.
saveRewriteUrlKeyByUpsert
php
fecshop/yii2_fecshop
services/Url.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Url.php
BSD-3-Clause
public function removeRewriteUrlKey($url_key) { $model = $this->findOne([ 'custom_url_key' => $url_key, ]); if ($model['custom_url_key']) { $model->delete(); } }
@param $url_key|string remove url rewrite data by $url_key,which is custom url key that saved in custom url modules,like articcle , product, category ,etc..
removeRewriteUrlKey
php
fecshop/yii2_fecshop
services/Url.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Url.php
BSD-3-Clause
public function getCurrentUrl() { if (!$this->_currentUrl) { $secure = Yii::$app->getRequest()->getIsSecureConnection(); $http = $secure ? 'https' : 'http'; $this->_currentUrl = $http . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; } return $this->_currentUrl; }
得到当前的url,使用的是php的方式,而不是Yii2的函数 对于Yii框架得到当前的url使用:\yii\helpers\BaseUrl::current([],true) 这里没有使用的原因是,因为fecshop存在url rewrite,在初始化的时候,会将当前的url转换成yii2框架的url 当前函数返回的url,是浏览器地址栏中的当前url,而\yii\helpers\BaseUrl::current([],true) 返回的yii2框架中的url 这个要分清楚使用 譬如分类页面的url,进行了url rewrite:http://fecshop.appfront.fancyecommerce.com/men 1.函数`\yii\helpers\BaseUrl::current([],true)`的输出为:http://fecshop.appfront.fancyecommerce.com/catalog/category/index?_id=57b6ac42f656f246653bf576 2.而当前函数`getCurrentUrl()`的输出为:http://fecshop.appfront.fancyecommerce.com/men 3.关于fecshop url rewrite,详细参看:http://www.fecshop.com/doc/fecshop-guide/instructions/cn-1.0/guide-fecshop_url_rewrite.html
getCurrentUrl
php
fecshop/yii2_fecshop
services/Url.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Url.php
BSD-3-Clause
public function getOriginUrl($urlKey) { return Yii::$service->url->rewrite->getOriginUrl($urlKey); }
@param $urlKey|string get $origin_url by $custom_url_key ,it is used for yii2 init, in (new fecshop\services\Request)->resolveRequestUri(), ## fecshop\services\Request is extend yii\web\Request
getOriginUrl
php
fecshop/yii2_fecshop
services/Url.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Url.php
BSD-3-Clause
public function getUrlByDomain($url_key, $params = [], $https = false, $domain, $showScriptName = false, $useHttpForUrl = false) { $first_str = substr($url_key, 0, 1); if ($first_str == '/') { $jg = ''; } else { $jg = '/'; } if ($useHttpForUrl) { if ($https) { $baseUrl = 'https://'.$domain; } else { $baseUrl = 'http://'.$domain; } } else { $baseUrl = '//'.$domain; } if ($showScriptName) { $baseUrl .= '/index.php'; } if (is_array($params) && !empty($params)) { $arr = []; foreach ($params as $k => $v) { $arr[] = $k.'='.$v; } return $baseUrl.$jg.$url_key.'?'.implode('&', $arr); } return $url_key ? $baseUrl.$jg.$url_key : $baseUrl; }
@param $url_key | String urlKey的值 @param $params | Array 。url里面个各个参数 @param https | boolean 是否使用https的方式 @param $domain | String , 相应的域名,譬如www.fecshop.com @proeprty $showScriptName | boolean,是否在url中包含index.php/部分 @param $useHttpForUrl | boolean ,是否在url中加入http部分、 通过传入domain的方式得到相应的url 该功能一般是在脚本中通过各个域名的传入得到相应的url,譬如sitemap的生成就是应用了这个方法得到 产品和分类的url。
getUrlByDomain
php
fecshop/yii2_fecshop
services/Url.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Url.php
BSD-3-Clause
public function getCurrentBaseUrl() { if (!$this->_currentBaseUrl) { $homeUrl = $this->homeUrl(); if ($this->showScriptName) { $homeUrl .= '/index.php'; } $this->_currentBaseUrl = $homeUrl; } return $this->_currentBaseUrl; }
get current base url , is was generate by http(or https ).'://'.store_code.
getCurrentBaseUrl
php
fecshop/yii2_fecshop
services/Url.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Url.php
BSD-3-Clause
public function homeUrl() { if ($this->_homeUrl) { return $this->_homeUrl; } return Yii::$app->getHomeUrl(); }
get current home url , is was generate by 'http://'.store_code.
homeUrl
php
fecshop/yii2_fecshop
services/Url.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Url.php
BSD-3-Clause
protected function getBaseUrl() { if (!$this->_baseUrl) { $this->_baseUrl = $this->homeUrl(); if ($this->showScriptName) { $this->_baseUrl .= '/index.php'; } } return $this->_baseUrl; }
get base url.
getBaseUrl
php
fecshop/yii2_fecshop
services/Url.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Url.php
BSD-3-Clause
protected function getRewriteUrlKey($urlKey, $originUrl) { $model = $this->find(); $data = $model->where([ 'custom_url_key' => $urlKey, ])->andWhere(['<>', 'origin_url', $originUrl]) ->asArray()->one(); if (isset($data['custom_url_key'])) { $urlKey = $this->getRandomUrlKey($urlKey); return $this->getRewriteUrlKey($urlKey, $originUrl); } else { return $urlKey; } }
get rewrite url key.
getRewriteUrlKey
php
fecshop/yii2_fecshop
services/Url.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Url.php
BSD-3-Clause
protected function getRandom($length = '') { if (!$length) { $length = $this->randomCount; } $str = null; $strPol = '123456789'; $max = strlen($strPol) - 1; for ($i = 0; $i < $length; $i++) { $str .= $strPol[rand(0, $max)]; //rand($min,$max)生成介于min和max两个数之间的一个随机整数 } return $str; }
generate random string.
getRandom
php
fecshop/yii2_fecshop
services/Url.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Url.php
BSD-3-Clause
protected function getRandomUrlKey($url) { if ($this->_origin_url) { $suffix = ''; $o_url = $this->_origin_url; if (strstr($this->_origin_url, '.')) { list($o_url, $suffix) = explode('.', $this->_origin_url); $l = $this->randomCount +1; if (strlen($o_url) > $l) { $randomStrSub = substr($o_url, strlen($o_url) - $l , $l ); $orignUrlK = substr($o_url, 0, strlen($o_url) - $l ); if ($this->isRandomStr($randomStrSub)) { $o_url = $orignUrlK; } } $randomStr = $this->getRandom(); return $o_url.'-'.$randomStr.'.'.$suffix; } else { $l = $this->randomCount +1; if (strlen($o_url) > $l) { $randomStr = substr($o_url, strlen($o_url) - $l , $l ); $orignUrlK = substr($o_url, 0, strlen($o_url) - $l ); if ($this->isRandomStr($randomStr)) { $o_url = $orignUrlK; } } $randomStr = $this->getRandom(); return $o_url.'-'.$randomStr; } } }
if url key is exist in url_rewrite table ,Behind url add some random string.
getRandomUrlKey
php
fecshop/yii2_fecshop
services/Url.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Url.php
BSD-3-Clause
protected function generateUrlByName($name) { setlocale(LC_ALL, ''); $url = iconv('UTF-8', 'ASCII//TRANSLIT', $name); $url = preg_replace('{[^a-zA-Z0-9_.| -]}', '', $url); $url = strtolower(trim($url, '-')); $url = preg_replace('{[_| -]+}', '-', $url); $url = '/'.trim($url, '/'); $this->_origin_url = $url; return $url; }
clear character that can not use for url.
generateUrlByName
php
fecshop/yii2_fecshop
services/Url.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Url.php
BSD-3-Clause
public function redirect($url) { if ($url) { Yii::$app->getResponse()->redirect($url)->send(); } }
url 跳转.
redirect
php
fecshop/yii2_fecshop
services/Url.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Url.php
BSD-3-Clause
public function isHomePage() { $rules = Yii::$app->urlManager->rules; $route = ''; if (!is_array($rules)) { return false; } foreach ($rules as $one) { $name = $one->name; if ($name === '') { $route = $one->route; } } if (!$route) { return false; } // $route 默认为 cms/home/index $arr = explode('/', $route); if (count($arr) != 3) { return false; } $mId = Yii::$app->controller->module->id; $cId = Yii::$app->controller->id; $aId = Yii::$app->controller->action->id; // 通过module controler action 的id 与 $routeArr核对,不一致则为false if ($mId != $arr[0] || $cId != $arr[1] || $aId != $arr[2]) { return false; } return true; }
判断是否是首页
isHomePage
php
fecshop/yii2_fecshop
services/Url.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Url.php
BSD-3-Clause
public function isValidUrl($url) { if (filter_var($url, FILTER_VALIDATE_URL) === FALSE) { return false; } return true; }
@param $url | string , 网址url字符串 @return bool 判断传递的网址url字符串,是否是合法的网址字符串。
isValidUrl
php
fecshop/yii2_fecshop
services/Url.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Url.php
BSD-3-Clause
public function init() { parent::init(); list($this->_modelName, $this->_model) = Yii::mapGet($this->_modelName); }
protected $_serilizeAttr = [ "service_db", ];
init
php
fecshop/yii2_fecshop
services/StoreDomain.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/StoreDomain.php
BSD-3-Clause
public function save($one) { $currentDateTime = \fec\helpers\CDate::getCurrentDateTime(); $primaryVal = isset($one[$this->getPrimaryKey()]) ? $one[$this->getPrimaryKey()] : ''; if ($primaryVal) { $model = $this->_model->findOne($primaryVal); if (!$model) { Yii::$service->helper->errors->add('config {primaryKey} is not exist', ['primaryKey' => $this->getPrimaryKey()]); return; } } else { $model = new $this->_modelName(); $model->created_at = time(); } $model->updated_at = time(); $primaryKey = $this->getPrimaryKey(); $model = Yii::$service->helper->ar->save($model, $one); $primaryVal = $model[$primaryKey]; return true; }
@param $one|array save $data to cms model,then,add url rewrite info to system service urlrewrite.
save
php
fecshop/yii2_fecshop
services/StoreDomain.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/StoreDomain.php
BSD-3-Clause
public function getByNamespace($extension_namespace) { return $this->_model->findOne(['namespace' => $extension_namespace]); }
@param $extension_name | string , 插件名称(唯一) @return model
getByNamespace
php
fecshop/yii2_fecshop
services/Extension.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Extension.php
BSD-3-Clause
public function save($one) { $currentDateTime = \fec\helpers\CDate::getCurrentDateTime(); $primaryVal = isset($one[$this->getPrimaryKey()]) ? $one[$this->getPrimaryKey()] : ''; if ($primaryVal) { $model = $this->_model->findOne($primaryVal); if (!$model) { Yii::$service->helper->errors->add('extension: {primaryKey} is not exist', ['primaryKey' => $this->getPrimaryKey()]); return; } } else { $model = new $this->_modelName(); $model->created_at = time(); } $model->updated_at = time(); $primaryKey = $this->getPrimaryKey(); $model = Yii::$service->helper->ar->save($model, $one); $primaryVal = $model[$primaryKey]; return true; }
@param $one|array save $data to cms model,then,add url rewrite info to system service urlrewrite.
save
php
fecshop/yii2_fecshop
services/Extension.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Extension.php
BSD-3-Clause
public function newLocalCreateInit($param) { $namespace = $param['namespaces']; //$package = $param['package']; //$name = $param['name']; //$config_file_path = $param['config_file_path']; //$version = $param['version']; if (!$namespace) { Yii::$service->helper->errors->add('namespace is empty'); return false; } // 查看namespace 是否存在 $modelOne = $this->_model->findOne(['namespace' => $namespace]); if ($modelOne['id']) { Yii::$service->helper->errors->add('this namespace is exist'); return false; } $config_file_path = '@addons/' . $param['package'] . '/' . $param['addon_folder'] . '/config.php'; $model = new $this->_modelName(); $model['namespace'] = $param['namespaces']; $model['package'] = $param['package']; $model['folder'] = $param['addon_folder']; $model['name'] = $param['addon_name']; $model['version'] = '1.0.0'; $model['config_file_path'] = $config_file_path; if (!$model->validate()) { $errors = $model->errors; Yii::$service->helper->errors->addByModelErrors($errors); return false; } $model->status = self::STATUS_ENABLE; $model->type = self::TYPE_LOCAL_CREATED; $model->created_at = time(); $model->updated_at = time(); $model->installed_status = self::INSTALLED_STATUS; $model->priority = 1; return $model->save(); }
@param $param | array, 参数数组 属于Gii代码生成功能部分,作为第三方开发者,如果想开发自己的应用扩展,在后台使用`初始化应用`, 本部分是初始化部分。
newLocalCreateInit
php
fecshop/yii2_fecshop
services/Extension.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Extension.php
BSD-3-Clause
public function newInstallInit($param) { $namespace = $param['namespace']; //$package = $param['package']; //$name = $param['name']; //$config_file_path = $param['config_file_path']; //$version = $param['version']; if (!$namespace) { Yii::$service->helper->errors->add('namespace is empty'); return false; } // 查看namespace 是否存在 $modelOne = $this->_model->findOne([ 'namespace' => $namespace, 'installed_status' => self::INSTALLED_STATUS, ]); if ($modelOne['id']) { Yii::$service->helper->errors->add('this namespace is exist'); return false; } $param['config_file_path'] = '@addons/' . $param['package'] . '/' . $param['folder'] . '/config.php'; // 查看是否上次未安装存在记录, $model = $this->_model->findOne([ 'namespace' => $namespace, 'package' => $param['package'], 'folder' => $param['folder'], ]); if (!$model['id']) { $model = new $this->_modelName(); } $model->attributes = $param; if (!$model->validate()) { $errors = $model->errors; Yii::$service->helper->errors->addByModelErrors($errors); return false; } $model->status = self::STATUS_DISABLE; $model->type = self::TYPE_INSTALL; $model->created_at = time(); $model->updated_at = time(); $model->installed_status = self::INSTALL_INIT_STATUS; $model->priority = 1; return $model->save(); }
新安装的插件,进行初始化
newInstallInit
php
fecshop/yii2_fecshop
services/Extension.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Extension.php
BSD-3-Clause
public function upgradeInit($param) { $namespace = $param['namespace']; if (!$namespace) { Yii::$service->helper->errors->add('namespace is empty'); return false; } // 查看namespace 是否存在 $modelOne = $this->_model->findOne(['namespace' => $namespace]); if (!$modelOne['id']) { Yii::$service->helper->errors->add('this namespace is exist'); return false; } if (!$this->isInstalledStatus($modelOne['installed_status'])) { Yii::$service->helper->errors->add('addon status is not install status'); return false; } $param['config_file_path'] = '@addons/' . $param['package'] . '/' . $param['folder'] . '/config.php'; // $param['config_file_path'] = '@addons/' . $param['package'] . '/' . $param['config_file_path'] ; //$model = new $this->_modelName(); $modelOne->attributes = $param; if (!$modelOne->validate()) { $errors = $modelOne->errors; Yii::$service->helper->errors->addByModelErrors($errors); return false; } //$modelOne->status = self::STATUS_ENABLE; //$modelOne->created_at = time(); $modelOne->updated_at = time(); //$modelOne->installed_status = self::INSTALL_INIT_STATUS; //$modelOne->priority = 1; return $modelOne->save(); }
更新的插件,zip文件下载解压后,进行数据库初始化
upgradeInit
php
fecshop/yii2_fecshop
services/Extension.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Extension.php
BSD-3-Clause
public function getAllNamespaces() { $filter = [ 'asArray' => true, 'fetchAll' => true, ]; $data = $this->coll($filter); $arr = []; if (is_array($data['coll'])) { foreach ($data['coll'] as $one) { $namespace = $one['namespace']; if ($namespace) { $arr[] = $namespace; } } } return $arr; }
从数据库中获取所有的namespace
getAllNamespaces
php
fecshop/yii2_fecshop
services/Extension.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Extension.php
BSD-3-Clause
public function isInstalledStatus($installed_status) { if ($installed_status == self::INSTALLED_STATUS) { return true; } return false; }
@param $installed_status | int @return boolean 插件是否已安装
isInstalledStatus
php
fecshop/yii2_fecshop
services/Extension.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Extension.php
BSD-3-Clause
public function addWarning($info) { $this->warnings[] = $info; return true; }
添加警告信息
addWarning
php
fecshop/yii2_fecshop
services/Extension.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Extension.php
BSD-3-Clause
public function getWarning() { return $this->warnings; }
得到警告信息
getWarning
php
fecshop/yii2_fecshop
services/Extension.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Extension.php
BSD-3-Clause
public function installAddons($installConfig, $modelOne) { $installOb = Yii::createObject($installConfig); if (!$installOb->version) { Yii::$service->helper->errors->add("Extension Install Object must have property `version`"); return false; } if (!($installOb instanceof \fecshop\services\extension\InstallInterface)) { Yii::$service->helper->errors->add("Extension install file must implements interface `\fecshop\services\extension\InstallInterface`"); return false; } if (!$installOb->run()) { return false; } // 更新数据库-扩展的安装信息。 $modelOne->installed_status = self::INSTALLED_STATUS; $modelOne->status = self::STATUS_ENABLE; $modelOne->installed_version = $installOb->version; $modelOne->updated_at = time(); return $modelOne->save(); }
@param $installConfig | array 进行应用的安装 通过扩展配置中获取安装部分的配置,通过该函数执行安装。
installAddons
php
fecshop/yii2_fecshop
services/Extension.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Extension.php
BSD-3-Clause
public function upgradeAddons($upgradeConfig, $modelOne) { $upgradeOb = Yii::createObject($upgradeConfig); if (!($upgradeOb instanceof \fecshop\services\extension\UpgradeInterface)) { Yii::$service->helper->errors->add("Extension upgrade file must implements interface `\fecshop\services\extension\UpgradeInterface`"); return false; } $versions = $upgradeOb->versions; if (!empty($versions) && !is_array($versions)) { Yii::$service->helper->errors->add("Upgrade Object property `versions` must be array"); return false; } $installed_version = $modelOne['installed_version']; $addon_remote_version = $modelOne['version']; $count = count($versions); for ($i = 0; $i < $count; $i++) { // 如果当前版本号 小于 此版本号 if (version_compare($installed_version, $versions[$i] ,'<') && version_compare($versions[$i], $addon_remote_version,'<=') // 应用里面update更新的版本号,如果大于应用version,那么不生效(这样可以通过远程来控制最大版本号) ) { // 执行插件更新版本操作。 if (!$upgradeOb->run($versions[$i])) { return false; } // 更新数据库插件安装版本信息 $modelOne->installed_version = $versions[$i]; $modelOne->updated_at = time(); if (!$modelOne->save()) { return false; } } } // 查看升级后的install_version和version是否一致, 可能插件无更新(db,文件复制等) $modelOne->installed_version = $modelOne['version']; $modelOne->updated_at = time(); if (!$modelOne->save()) { return false; } return true; }
@param $installConfig | array 进行应用的升级 通过扩展配置中获取安装部分的配置,通过该函数执行安装。
upgradeAddons
php
fecshop/yii2_fecshop
services/Extension.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Extension.php
BSD-3-Clause
public function uninstallAddons($unstallConfig, $modelOne) { $uninstallOb = Yii::createObject($unstallConfig); if (!($uninstallOb instanceof \fecshop\services\extension\UninstallInterface)) { Yii::$service->helper->errors->add("Extension unstall file must implements interface `\fecshop\services\extension\UninstallInterface`"); return false; } if (!$uninstallOb->run()) { return false; } // 进行extension数据的删除 return $modelOne->delete(); }
@param $installConfig | array 进行应用的卸载 通过扩展配置中获取安装部分的配置,通过该函数执行安装。
uninstallAddons
php
fecshop/yii2_fecshop
services/Extension.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Extension.php
BSD-3-Clause
public function isAllowImgType($file, $fileName) { $img = getimagesize($file); $imgType = $img['mime']; if (!in_array($imgType, $this->allowImgType)) { return false; } // 文件后缀检查 $fileNameArr = explode('.', $fileName); $fileSuffix = $fileNameArr[count($fileNameArr)-1]; $allowImgSuffix = $this->getAllowImgSuffix(); if (!in_array($fileSuffix, $allowImgSuffix)) { return false; } return true; }
@param $file | string, 图片文件路径 @return boolean, 是否是允许的图片类型
isAllowImgType
php
fecshop/yii2_fecshop
services/Image.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Image.php
BSD-3-Clause
public function getLogoImgUrl() { $logoImg = Yii::$app->store->get('base_info', 'logo_image'); if ($logoImg) { return $this->getUrlByRelativePath($logoImg); } return Yii::$service->image->getImgUrl('appfront/custom/logo.png'); }
得到logo的url
getLogoImgUrl
php
fecshop/yii2_fecshop
services/Image.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Image.php
BSD-3-Clause
public function getFecmallLogoImgUrl() { return Yii::$service->image->getImgUrl('appfront/custom/logo.png'); }
得到logo的url
getFecmallLogoImgUrl
php
fecshop/yii2_fecshop
services/Image.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Image.php
BSD-3-Clause
public function getImgDir($str = '') // , $app = 'common' 第二个参数废弃 { if ($str) { return Yii::getAlias($this->commonBaseDir) . '/'.$str; } return Yii::getAlias($this->commonBaseDir); }
@param $str | String 图片的相对路径 @param $app | String @appimage下面的文件夹的名称。各个名称对应各个入口的名字,譬如common appfront appadmin等 @return 返回图片的绝对路径。
getImgDir
php
fecshop/yii2_fecshop
services/Image.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Image.php
BSD-3-Clause
public function getImgUrl($str) // , $app = 'common' 第二个参数废弃 { if ($str) { return $this->commonBaseDomain.'/'.$str; } return $this->commonBaseDomain; }
@param $str | String 图片的相对路径 @param $app | String @appimage下面的文件夹的名称。各个名称对应各个入口的名字,譬如common appfront appadmin等 @return 返回图片的完整URL
getImgUrl
php
fecshop/yii2_fecshop
services/Image.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Image.php
BSD-3-Clause
public function getBaseImgDir($app = 'common') { return $this->getImgDir('', $app); }
@param $app | String @appimage下面的文件夹的名称。各个名称对应各个入口的名字,譬如common appfront appadmin等 @return 返回图片存放目录的绝对路径。
getBaseImgDir
php
fecshop/yii2_fecshop
services/Image.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Image.php
BSD-3-Clause
public function getBaseImgUrl($app = 'common') { return $this->getImgUrl('', $app); }
@param $app | String @appimage下面的文件夹的名称。各个名称对应各个入口的名字,譬如common appfront appadmin等 @return 返回图片存放目录的URL
getBaseImgUrl
php
fecshop/yii2_fecshop
services/Image.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Image.php
BSD-3-Clause
public function setMaxUploadSize($uploadSize) { $this->_maxUploadSize = $uploadSize * 1024 * 1024; }
@param $uploadSize | Int , 多少MB 设置上传图片的最大的size. 参数单位为MB
setMaxUploadSize
php
fecshop/yii2_fecshop
services/Image.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Image.php
BSD-3-Clause
public function getMaxUploadSize() { if (!$this->_maxUploadSize) { if ($this->maxUploadMSize) { $this->_maxUploadSize = $this->maxUploadMSize * 1024 * 1024; } } return $this->_maxUploadSize; }
得到上传图片的最大的size.
getMaxUploadSize
php
fecshop/yii2_fecshop
services/Image.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Image.php
BSD-3-Clause
public function getCurrentBaseImgUrl() { return $this->GetImgUrl($this->imageFloder, 'common'); }
得到(上传)保存图片所在相对根目录的url路径.
getCurrentBaseImgUrl
php
fecshop/yii2_fecshop
services/Image.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Image.php
BSD-3-Clause
public function getCurrentBaseImgDir() { return $this->GetImgDir($this->imageFloder, 'common'); }
得到(上传)保存图片所在相对根目录的文件夹路径.
getCurrentBaseImgDir
php
fecshop/yii2_fecshop
services/Image.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Image.php
BSD-3-Clause
public function getUrlByRelativePath($str) { return $this->GetImgUrl($this->imageFloder.$str, 'common'); }
@param $str | String , 图片的相对路径字符串 通过图片的相对路径得到产品图片的url.
getUrlByRelativePath
php
fecshop/yii2_fecshop
services/Image.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Image.php
BSD-3-Clause
public function getDirByRelativePath($str) { return $this->GetImgDir($this->imageFloder.$str, 'common'); }
@param $str | String , 图片的相对路径字符串 通过图片的相对路径得到产品图片的绝对路径.
getDirByRelativePath
php
fecshop/yii2_fecshop
services/Image.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Image.php
BSD-3-Clause
protected function generateImgName($name, $length = 15) { $arr = explode('.', $name); $fileType = '.'.$arr[count($arr)-1]; // 密码字符集,可任意添加你需要的字符 $chars = 'abcdefghijklmnopqrstuvwxyz0123456789'; $str =''; for ($i = 0; $i < $length; $i++) { // 这里提供两种字符获取方式 // 第一种是使用 substr 截取$chars中的任意一位字符; // 第二种是取字符数组 $chars 的任意元素 // $str .= substr($chars, mt_rand(0, strlen($chars) – 1), 1); $str .= $chars[ mt_rand(0, strlen($chars) - 1) ]; } $str .= time(); return $str.$fileType; }
@param $name | String , 图片的原始名字,也就是图片上传的时候的名字。 @param $length | String , 生成图片随机字符的长度。 随机生成图片的新名字,因为有的图片名字可能是中文或者其他语言,而fecshop在保存名字的时候会取名字的前2个字母生成2层文件夹 这样中文名字就会出现问题,因此需要使用随机生成的名字(生成2层文件夹,是为了让文件夹下面不至于太多的文件,linux文件夹下的文件超过几万个,查找文件就会有点慢,这样做是为了避免这个文件。)
generateImgName
php
fecshop/yii2_fecshop
services/Image.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Image.php
BSD-3-Clause
public function saveStreamImg($fileName, $fileStream, $imgFileSaveRelativePath='') { $imgFileSaveRelativePath = trim($imgFileSaveRelativePath, '/'); $imgFileSaveRelativePathArr = explode('/', $imgFileSaveRelativePath); if ($imgFileSaveRelativePath && count($imgFileSaveRelativePathArr) == 3) { $newName = $imgFileSaveRelativePathArr[2]; if (!$newName) { Yii::$service->helper->errors->add('generate img name fail'); return false; } $arr = explode('.', $fileName); $fileType = '.'.$arr[count($arr)-1]; $imgFileType = [ '.jpg', '.jpeg', '.gif', '.png', ]; if (!in_array($fileType, $imgFileType)) { Yii::$service->helper->errors->add('image file type ['.$fileType. '] is not allow '); return false; } // process image name. $imgSavedRelativePath = $this->getImgSavedRelativePath($newName, $imgFileSaveRelativePath); } else { $newName = $this->generateImgName($fileName); if (!$newName) { Yii::$service->helper->errors->add('generate img name fail'); return false; } $arr = explode('.', $fileName); $fileType = '.'.$arr[count($arr)-1]; $imgFileType = [ '.jpg', '.jpeg', '.gif', '.png', ]; if (!in_array($fileType, $imgFileType)) { Yii::$service->helper->errors->add('image file type ['.$fileType. '] is not allow '); return false; } // process image name. $imgSavedRelativePath = $this->getImgSavedRelativePath($newName); } $fpDst = fopen($this->getCurrentBaseImgDir().$imgSavedRelativePath, "wb"); fwrite($fpDst, $fileStream); fclose($fpDst); $imgUrl = $this->getUrlByRelativePath($imgSavedRelativePath); $imgPath = $this->getDirByRelativePath($imgSavedRelativePath); return [$imgSavedRelativePath, $imgUrl, $imgPath, $newName]; }
@param $fileName | string, 文件名称 @param $fileStream |string, 图片文件的二进制字符。 @param $imgFileSaveRelativePath | string, 图片存放的相对路径,设置该值后,图片将保存到这个相对路径,如果该路径下已经存在文件,则将会覆盖。
saveStreamImg
php
fecshop/yii2_fecshop
services/Image.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Image.php
BSD-3-Clause
protected function getImgSavedRelativePath($name,$imgFileSaveRelativePath='') { list($imgName, $imgType) = explode('.', $name); if (!$imgName || !$imgType) { throw new InvalidValueException('image file name and type is not correct'); } // 对于自定义存储路径的处理 if ($imgFileSaveRelativePath) { $imgFileSaveRelativePathArr = explode('/', $imgFileSaveRelativePath); $first_str = $imgFileSaveRelativePathArr[0]; $two_str = $imgFileSaveRelativePathArr[1]; $imgSaveFloder = CDir::createFloder($this->getCurrentBaseImgDir(), [$first_str, $two_str]); return '/'.$imgFileSaveRelativePath; } else { if (strlen($imgName) < 2) { $imgName .= time(). mt_rand(100, 999); } $first_str = substr($imgName, 0, 1); $two_str = substr($imgName, 1, 2); $imgSaveFloder = CDir::createFloder($this->getCurrentBaseImgDir(), [$first_str, $two_str]); if ($imgSaveFloder) { $imgName = $this->getUniqueImgNameInPath($imgSaveFloder, $imgName, $imgType); $relative_floder = '/'.$first_str.'/'.$two_str.'/'; return $relative_floder.$imgName; } } return false; }
get Image save file path, if floder is not exist, this function will create floder. if image file is exsit , image file name will be change to a not existed file name( by add radom string to file name ). return image saved relative path , like /a/d/advert.jpg.
getImgSavedRelativePath
php
fecshop/yii2_fecshop
services/Image.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Image.php
BSD-3-Clause
protected function getUniqueImgNameInPath($imgSaveFloder, $name, $imageType, $randStr = '') { $imagePath = $imgSaveFloder.'/'.$name.$randStr.'.'.$imageType; if (!file_exists($imagePath)) { return $name.$randStr.'.'.$imageType; } else { $randStr = time().rand(10000, 99999); return $this->getUniqueImgNameInPath($imgSaveFloder, $name, $imageType, $randStr); } }
@param $imgSaveFloder|string image save Floder absolute Path @param $name|string , image file name ,not contain image suffix. @param $imageType|string , image file suffix. like '.gif','jpg' return saved Image Name. 得到产品保存的唯一路径,因为可能存在名字重复的问题,因此使用该函数确保图片路径唯一。
getUniqueImgNameInPath
php
fecshop/yii2_fecshop
services/Image.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Image.php
BSD-3-Clause
public function isApiStore() { $appName = Yii::$app->params['appName']; if ($appName && in_array($appName, $this->apiAppNameArr)) { return true; } else { return false; } }
是否是api入口
isApiStore
php
fecshop/yii2_fecshop
services/Store.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Store.php
BSD-3-Clause
public function getCurrentAppName() { return Yii::$service->helper->getAppName(); }
得到当前入口的名字 @return mixed
getCurrentAppName
php
fecshop/yii2_fecshop
services/Store.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Store.php
BSD-3-Clause
public function initAppserverCurrentStoreConfig() { $appserver_store_config = Yii::$app->store->get('appserver_store'); $storeKey = $appserver_store_config['key']; $lang = $appserver_store_config['lang']; $lang_name = $appserver_store_config['lang_name']; $currency = $appserver_store_config['currency']; $facebook_login_app_id = $appserver_store_config['facebook_login_app_id']; $facebook_login_app_secret = $appserver_store_config['facebook_login_app_secret']; $google_login_client_id = $appserver_store_config['google_login_client_id']; $google_login_client_secret = $appserver_store_config['google_login_client_secret']; $https_enable = $appserver_store_config['https_enable'] == 1 ? true : false; // set config stores $this->stores[$storeKey] = [ 'language' => $lang, // 语言简码需要在@common/config/fecshop_local_services/FecshopLang.php 中定义。 'languageName' => $lang_name, // 语言简码对应的文字名称,将会出现在语言切换列表中显示。 'currency' => $currency, // 当前store的默认货币,这个货币简码,必须在货币配置中配置 // 用于sitemap生成中域名。 'https' => $https_enable, ]; // 第三方账号登录配置 if ($facebook_login_app_id && $facebook_login_app_secret) { $this->stores[$storeKey]['thirdLogin']['facebook'] = [ 'facebook_app_id' => $facebook_login_app_id, 'facebook_app_secret' => $facebook_login_app_secret, ]; } // 第三方账号登录配置 if ($google_login_client_id && $google_login_client_secret) { $this->stores[$storeKey]['thirdLogin']['google'] = [ 'CLIENT_ID' => $google_login_client_id, 'CLIENT_SECRET' => $google_login_client_secret, ]; } // 初始化语言。 $this->stores[$storeKey]['serverLangs'] = Yii::$app->store->get('appserver_store_lang'); // 通过该方法,初始化货币services,直接从headers中取出来currency。进行set,这样currency就不会从session中读取(fecshop-2版本对于appserver已经抛弃session servcies) Yii::$service->page->currency->appserverSetCurrentCurrency(); return true; }
如果入口是appserver,那么通过这个函数初始化
initAppserverCurrentStoreConfig
php
fecshop/yii2_fecshop
services/Store.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Store.php
BSD-3-Clause
public function bootstrap($app) { $host = explode('//', $app->getHomeUrl()); $stores = $this->stores; $init_complete = 0; if (is_array($stores) && !empty($stores)) { foreach ($stores as $store_code => $store) { if ($host[1] == $store_code) { $this->html5DeviceCheckAndRedirect($store_code, $store); Yii::$service->store->currentStore = $store_code; $this->store = $store; if (isset($store['language']) && !empty($store['language'])) { Yii::$service->store->currentLang = $store['language']; Yii::$service->store->currentLangCode = Yii::$service->fecshoplang->getLangCodeByLanguage($store['language']); Yii::$service->store->currentLangName = $store['languageName']; Yii::$service->page->translate->setLanguage($store['language']); } // set local theme dir. if (isset($store['localThemeDir']) && $store['localThemeDir']) { Yii::$service->page->theme->setLocalThemeDir($store['localThemeDir']); } // set third theme dir. if (isset($store['thirdThemeDir']) && $store['thirdThemeDir']) { Yii::$service->page->theme->setThirdThemeDir($store['thirdThemeDir']); } // init store currency. if (isset($store['currency']) && !empty($store['currency'])) { $currency = $store['currency']; } else { $currency = ''; } Yii::$service->page->currency->initCurrency($currency); /** * current domain is config is store config. */ $init_complete = 1; $this->thirdLogin = isset($store['thirdLogin']) ? $store['thirdLogin'] : []; /** * appserver 部分 */ if (isset($store['serverLangs']) && !empty($store['serverLangs'])) { $this->serverLangs = $store['serverLangs']; } $headers = Yii::$app->request->getHeaders(); if (isset($headers['fecshop-lang']) && $headers['fecshop-lang']) { $h_lang = $headers['fecshop-lang']; if (is_array($this->serverLangs)) { foreach ($this->serverLangs as $one) { if ($one['code'] == $h_lang) { Yii::$service->store->currentLangCode = $h_lang; Yii::$service->store->currentLang = $one['language']; Yii::$service->store->currentLangName = $one['languageName']; Yii::$service->page->translate->setLanguage($one['language']); break; } } } } break; } } } if (!$init_complete) { throw new InvalidValueException('this domain is not config in store service, you must config it in admin store config'); } }
Bootstrap:init website, class property $currentLang ,$currentTheme and $currentStore. if you not config this ,default class property will be set. if current store_code is not config , InvalidValueException will be throw. class property $currentStore will be set value $store_code. @param \yii\web\Application $app
bootstrap
php
fecshop/yii2_fecshop
services/Store.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Store.php
BSD-3-Clause
protected function html5DeviceCheckAndRedirect($store_code, $store) { if (!isset($store['mobile'])) { return; } $enable = isset($store['mobile']['enable']) ? $store['mobile']['enable'] : false; if (!$enable) { return; } $condition = isset($store['mobile']['condition']) ? $store['mobile']['condition'] : false; $redirectDomain = isset($store['mobile']['redirectDomain']) ? $store['mobile']['redirectDomain'] : false; $redirectType = isset($store['mobile']['type']) ? $store['mobile']['type'] : false; if (is_array($condition) && !empty($condition) && !empty($redirectDomain) && $redirectType === 'apphtml5') { $mobileDetect = Yii::$service->helper->mobileDetect; $mobile_https = (isset($store['mobile']['https']) && $store['mobile']['https']) ? true : false; if (in_array('phone', $condition) && in_array('tablet', $condition)) { if ($mobileDetect->isMobile()) { $this->redirectAppHtml5Mobile($store_code, $redirectDomain, $mobile_https); } } elseif (in_array('phone', $condition)) { if ($mobileDetect->isMobile() && !$mobileDetect->isTablet()) { $this->redirectAppHtml5Mobile($store_code, $redirectDomain, $mobile_https); } } elseif (in_array('tablet', $condition)) { if ($mobileDetect->isTablet()) { $this->redirectAppHtml5Mobile($store_code, $redirectDomain, $mobile_https); } } } }
@param $store_code | String @param $store | Array mobile device url redirect. pc端自动跳转到html5端的检测
html5DeviceCheckAndRedirect
php
fecshop/yii2_fecshop
services/Store.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Store.php
BSD-3-Clause
protected function redirectAppHtml5Mobile($store_code, $redirectDomain, $mobile_https) { $currentUrl = Yii::$service->url->getCurrentUrl(); $redirectUrl = str_replace($store_code, $redirectDomain, $currentUrl); // pc端跳转到html5,可能一个是https,一个是http,因此需要下面的代码进行转换。 if ($mobile_https) { if (strstr($redirectUrl, 'https://') || strstr($redirectUrl, 'http://')) { $redirectUrl = str_replace('http://', 'https://', $redirectUrl); } else { $redirectUrl = 'https:'.$redirectUrl; } } else { if (strstr($redirectUrl, 'https://') || strstr($redirectUrl, 'http://')) { $redirectUrl = str_replace('https://', 'http://', $redirectUrl); } else { $redirectUrl = 'http:'.$redirectUrl; } } header('Location:'.$redirectUrl); exit; }
@param $store_code | String @param $redirectDomain | String 检测,html5端跳转检测
redirectAppHtml5Mobile
php
fecshop/yii2_fecshop
services/Store.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Store.php
BSD-3-Clause
public function isAppServerMobile() { $store = $this->store; if (!isset($store['mobile'])) { return; } $enable = isset($store['mobile']['enable']) ? $store['mobile']['enable'] : false; if (!$enable) { return; } $condition = isset($store['mobile']['condition']) ? $store['mobile']['condition'] : false; $redirectDomain = isset($store['mobile']['redirectDomain']) ? $store['mobile']['redirectDomain'] : false; $redirectType = isset($store['mobile']['type']) ? $store['mobile']['type'] : false; if (is_array($condition) && !empty($condition) && !empty($redirectDomain) && $redirectType === 'appserver') { $mobileDetect = Yii::$service->helper->mobileDetect; if (in_array('phone', $condition) && in_array('tablet', $condition)) { if ($mobileDetect->isMobile()) { return true; } } elseif (in_array('phone', $condition)) { if ($mobileDetect->isMobile() && !$mobileDetect->isTablet()) { return true; } } elseif (in_array('tablet', $condition)) { if ($mobileDetect->isTablet()) { return true; } } } return false; }
@return boolean, 检测是否属于满足跳转到appserver的条件
isAppServerMobile
php
fecshop/yii2_fecshop
services/Store.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Store.php
BSD-3-Clause
public function redirectAppServerMobile($urlPath) { $store = $this->store; $redirectDomain = isset($store['mobile']['redirectDomain']) ? $store['mobile']['redirectDomain'] : false; $mobile_https = (isset($store['mobile']['https']) && $store['mobile']['https']) ? 'https://' : 'http://'; $host = $mobile_https.$redirectDomain.'/#/'; $urlParam = $_SERVER["QUERY_STRING"]; // 得到当前的语言 if ($urlParam) { $urlParam .= '&lang='.$this->currentLangCode; } else { $urlParam .= 'lang='.$this->currentLangCode; } $redirectUrl = $host.$urlPath.'?'.$urlParam; header('Location:'.$redirectUrl); exit; }
@param $urlPath | String,跳转到vue端的url Path @return boolean, 生成vue端的url,然后进行跳转。
redirectAppServerMobile
php
fecshop/yii2_fecshop
services/Store.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Store.php
BSD-3-Clause
public function getStoreAttrVal($attrVal, $attrName) { $lang = $this->currentLangCode; return Yii::$service->fecshoplang->getLangAttrVal($attrVal, $attrName, $lang); }
@param $attrVal|array , language attr array , like ['title_en' => 'xxxx','title_fr' => 'yyyy'] @param $attrName|String, attribute name ,like: title ,description. if object or array attribute is a language attribute, you can get current language value by this function. if lang attribute in current store language is empty , default language attribute will be return. if attribute in default language value is empty, $attrVal will be return.
getStoreAttrVal
php
fecshop/yii2_fecshop
services/Store.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Store.php
BSD-3-Clause
public function getStoresLang() { $stores = $this->stores; $topLang = []; foreach ($stores as $storeCode=> $store) { $languageName = $store['languageName']; $topLang[$storeCode] = $languageName; } return $topLang; }
@return array get all store info, one item in array format is: ['storeCode' => 'store language'].
getStoresLang
php
fecshop/yii2_fecshop
services/Store.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Store.php
BSD-3-Clause
public function getCheckoutTypeArr() { return [ self::CHECKOUT_TYPE_ADMIN_CREATE => self::CHECKOUT_TYPE_ADMIN_CREATE, self::CHECKOUT_TYPE_STANDARD => self::CHECKOUT_TYPE_STANDARD, self::CHECKOUT_TYPE_EXPRESS => self::CHECKOUT_TYPE_EXPRESS, ]; }
@return array 将订单所有的支付类型,组合成一个数组,进行返回。
getCheckoutTypeArr
php
fecshop/yii2_fecshop
services/Order.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Order.php
BSD-3-Clause
public function getOrderPaymentedStatusArr() { return [ $this->payment_status_confirmed, $this->status_holded, $this->status_processing, $this->status_dispatched, $this->status_received, $this->status_completed, ]; }
付款成功,而且订单付款状态正常的订单状态
getOrderPaymentedStatusArr
php
fecshop/yii2_fecshop
services/Order.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Order.php
BSD-3-Clause
public function getStatusArr() { return [ $this->payment_status_pending => $this->payment_status_pending, $this->payment_status_processing => $this->payment_status_processing, $this->payment_status_confirmed => $this->payment_status_confirmed, $this->payment_status_canceled => $this->payment_status_canceled, $this->payment_status_suspected_fraud => $this->payment_status_suspected_fraud, $this->status_holded => $this->status_holded, $this->status_processing => $this->status_processing, $this->status_dispatched => $this->status_dispatched, $this->status_received => $this->status_received, $this->status_refunded => $this->status_refunded, $this->status_completed => $this->status_completed, ]; }
@return array 将订单所有的状态,组合成一个数组,进行返回。
getStatusArr
php
fecshop/yii2_fecshop
services/Order.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Order.php
BSD-3-Clause
public function getSelectStatusArr() { return [ $this->payment_status_pending => '等待支付('.$this->payment_status_pending.')', $this->payment_status_processing => '支付处理中('.$this->payment_status_processing.')', $this->payment_status_confirmed => '支付成功('.$this->payment_status_confirmed.')', $this->payment_status_canceled => '支付取消('.$this->payment_status_canceled.')', $this->payment_status_suspected_fraud => '欺诈订单('.$this->payment_status_suspected_fraud.')', $this->status_holded => '审核订单('.$this->status_holded.')', $this->status_processing => '备货中订单('.$this->status_processing.')', $this->status_dispatched => '已发货订单('.$this->status_dispatched.')', $this->status_received => '已收货订单('.$this->status_received.')', $this->status_refunded => '已退款订单('.$this->status_refunded.')', $this->status_completed => '已完成订单('.$this->status_completed.')', ]; }
@return array 将订单所有的状态,组合成一个数组,进行返回。
getSelectStatusArr
php
fecshop/yii2_fecshop
services/Order.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Order.php
BSD-3-Clause
public function setCheckoutType($checkout_type) { $arr = [self::CHECKOUT_TYPE_STANDARD, self::CHECKOUT_TYPE_EXPRESS]; if (in_array($checkout_type, $arr)) { $this->checkout_type = $checkout_type; return true; } return false; }
@param $checkout_type | String ,支付类型 设置支付类型,其他计算以此设置作为基础,进而获取其他的配置。
setCheckoutType
php
fecshop/yii2_fecshop
services/Order.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Order.php
BSD-3-Clause
public function getCheckoutType() { return $this->checkout_type; }
得到支付类型
getCheckoutType
php
fecshop/yii2_fecshop
services/Order.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Order.php
BSD-3-Clause
public function checkRequiredAddressAttr($billing) { //$this->requiredAddressAttr; if (is_array($this->requiredAddressAttr) && !empty($this->requiredAddressAttr)) { foreach ($this->requiredAddressAttr as $attr) { if (!$attr) { continue; } if (!isset($billing[$attr]) || empty($billing[$attr])) { Yii::$service->helper->errors->add('{attr} can not empty', ['attr' => $attr]); return false; } } } return true; }
@param $billing | Array @return bool 通过$this->requiredAddressAttr,检查地址的必填。
checkRequiredAddressAttr
php
fecshop/yii2_fecshop
services/Order.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Order.php
BSD-3-Clause
public function getPrimaryKey() { return 'order_id'; }
得到order 表的id字段。
getPrimaryKey
php
fecshop/yii2_fecshop
services/Order.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Order.php
BSD-3-Clause
public function getByPrimaryKey($primaryKey) { $one = $this->_orderModel->findOne($primaryKey); $primaryKey = $this->getPrimaryKey(); if ($one[$primaryKey]) { return $one; } else { return new $this->_orderModelName(); } }
@param $primaryKey | Int @return Object($this->_orderModel) 通过主键值,返回Order Model对象
getByPrimaryKey
php
fecshop/yii2_fecshop
services/Order.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Order.php
BSD-3-Clause
public function getByIncrementId($increment_id) { $one = $this->_orderModel->findOne(['increment_id' => $increment_id]); $primaryKey = $this->getPrimaryKey(); if ($one[$primaryKey]) { return $one; } else { return false; } }
@param $increment_id | String , 订单号 @return object ($this->_orderModel),返回 $this->_orderModel model 通过订单号incrementId,得到订单Model对象。
getByIncrementId
php
fecshop/yii2_fecshop
services/Order.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Order.php
BSD-3-Clause
public function getCurrentOrderInfo($reflush = false) { if (!$this->_currentOrderInfo || $reflush) { if (Yii::$service->store->isAppserver()) { $increment_id = $this->getCurrentOrderIncrementId(); if (!$increment_id) { Yii::$service->helper->errors->add('current increment id is empty, you must setCurrentOrderIncrementId'); return null; } $this->_currentOrderInfo = Yii::$service->order->getOrderInfoByIncrementId($increment_id); } else { $increment_id = Yii::$service->order->getSessionIncrementId(); $this->_currentOrderInfo = Yii::$service->order->getOrderInfoByIncrementId($increment_id); } } return $this->_currentOrderInfo; }
@param $reflush | boolean 是否从数据库中重新获取,如果是,则不会使用类变量中计算的值 获取当前的订单信息,原理为: 通过从session中取出来订单的increment_id, 在通过increment_id(订单编号)取出来订单信息。
getCurrentOrderInfo
php
fecshop/yii2_fecshop
services/Order.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Order.php
BSD-3-Clause
public function getOrderInfoByIncrementId($increment_id) { $one = $this->getByIncrementId($increment_id); if (!$one) { return; } $primaryKey = $this->getPrimaryKey(); if (!isset($one[$primaryKey]) || empty($one[$primaryKey])) { return; } $order_info = []; foreach ($one as $k=>$v) { $order_info[$k] = $v; } $order_info['customer_address_state_name'] = Yii::$service->helper->country->getStateByContryCode($order_info['customer_address_country'], $order_info['customer_address_state']); $order_info['customer_address_country_name'] = Yii::$service->helper->country->getCountryNameByKey($order_info['customer_address_country']); $order_info['currency_symbol'] = Yii::$service->page->currency->getSymbol($order_info['order_currency_code']); $order_info['products'] = Yii::$service->order->item->getByOrderId($one[$primaryKey]); return $order_info; }
@param $increment_id | String 订单编号 @return array 通过increment_id 从数据库中取出来订单数据, 然后进行一系列的处理,返回订单数组数据。
getOrderInfoByIncrementId
php
fecshop/yii2_fecshop
services/Order.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Order.php
BSD-3-Clause
public function getOrderInfoById($order_id) { if (!$order_id) { return; } $one = $this->_orderModel->findOne($order_id); $primaryKey = $this->getPrimaryKey(); if (!isset($one[$primaryKey]) || empty($one[$primaryKey])) { return; } $order_info = []; foreach ($one as $k=>$v) { $order_info[$k] = $v; } $order_info['customer_address_state_name'] = Yii::$service->helper->country->getStateByContryCode($order_info['customer_address_country'], $order_info['customer_address_state']); $order_info['customer_address_country_name']= Yii::$service->helper->country->getCountryNameByKey($order_info['customer_address_country']); $order_info['currency_symbol'] = Yii::$service->page->currency->getSymbol($order_info['order_currency_code']); $order_info['products'] = Yii::$service->order->item->getByOrderId($order_id); return $order_info; }
@param $order_id | Int @return array 通过order_id 从数据库中取出来订单数据, 然后进行一系列的处理,返回订单数组数据。
getOrderInfoById
php
fecshop/yii2_fecshop
services/Order.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Order.php
BSD-3-Clause
public function save($one) { $time = time(); $primaryKey = $this->getPrimaryKey(); $primaryVal = isset($one[$primaryKey]) ? $one[$primaryKey] : ''; if ($primaryVal) { $model = $this->_orderModel->findOne($primaryVal); if (!$model) { Yii::$service->helper->errors->add('order {primaryKey} is not exist', ['primaryKey' => $this->getPrimaryKey()]); return; } } else { $model = new $this->_orderModelName(); $model->created_at = time(); } $model->updated_at = time(); $model = Yii::$service->helper->ar->save($model, $one); $primaryVal = $model[$this->getPrimaryKey()]; return $primaryVal; }
@param $one|array , save one data . @return int 保存order成功后,返回保存的id。
save
php
fecshop/yii2_fecshop
services/Order.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Order.php
BSD-3-Clause
public function getInfoByIncrementId($increment_id) { $order = $this->getByIncrementId($increment_id); $orderInfo = []; if ($order) { $primaryKey = $this->getPrimaryKey(); $order_id = $order[$primaryKey]; $items = Yii::$service->order->item->getByOrderId($order_id); foreach ($order as $k=>$v) { $orderInfo[$k] = $v; } $orderInfo['items'] = $items; return $orderInfo; } else { return; } }
@param $increment_id | String , 订单号 @return object ($this->_orderModel),返回 $this->_orderModel model 通过订单号,得到订单以及订单产品信息。
getInfoByIncrementId
php
fecshop/yii2_fecshop
services/Order.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Order.php
BSD-3-Clause
public function generatePPExpressOrder($token) { $myOrder = new $this->_orderModelName(); $myOrder->setGenerateOrderByPaypalToken(true); $myOrder->payment_token = $token; $myOrder->save(); $order_id = $myOrder['order_id']; if ($order_id) { $increment_id = $this->generateIncrementIdByOrderId($order_id); $myOrder['increment_id'] = $increment_id; $myOrder->save(); $this->createdOrder = $myOrder; if (!Yii::$service->store->isAppserver()) { // appserver入口,没有session机制。 $this->setSessionIncrementId($increment_id); } return true; } else { Yii::$service->helper->errors->add('generate order fail'); return false; } }
@param $token | String , paypal 支付获取的token,订单生成后只有三个字段 order_id, increment_id , payment_token ,目的就是将token对应到一个increment_id 在paypal 点击continue的时候,可以通过token找到对应的订单。
generatePPExpressOrder
php
fecshop/yii2_fecshop
services/Order.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Order.php
BSD-3-Clause
public function getByPaymentToken($token) { $one = $this->_orderModel->find()->where(['payment_token' => $token]) ->one(); if (isset($one['order_id']) && $one['order_id']) { return $one; } else { return ''; } }
@param $token | String , paypal 支付获取的token, 通过token 得到订单 Object
getByPaymentToken
php
fecshop/yii2_fecshop
services/Order.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Order.php
BSD-3-Clause
public function getInfoByPaymentToken($token) { $orderModel = $this->getByPaymentToken($token); $increment_id = isset($orderModel['increment_id']) ? $orderModel['increment_id'] : ''; return Yii::$service->order->getOrderInfoByIncrementId($increment_id); }
@param $reflush | boolean 是否从数据库中重新获取,如果是,则不会使用类变量中计算的值 通过从session中取出来订单的increment_id 在通过increment_id(订单编号)取出来订单信息。
getInfoByPaymentToken
php
fecshop/yii2_fecshop
services/Order.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Order.php
BSD-3-Clause
public function generateOrderByCart($address, $shipping_method, $payment_method, $clearCart = true, $token = '', $order_remark = '') { $cart = Yii::$service->cart->quote->getCurrentCart(); if (!$cart) { Yii::$service->helper->errors->add('current cart is empty'); } $currency_info = Yii::$service->page->currency->getCurrencyInfo(); $currency_code = $currency_info['code']; $currency_rate = $currency_info['rate']; $country = $address['country']; $state = $address['state']; $cartInfo = Yii::$service->cart->getCartInfo(true, $shipping_method, $country, $state); // 检查cartInfo中是否存在产品 if (!is_array($cartInfo) && empty($cartInfo)) { Yii::$service->helper->errors->add('current cart product is empty'); return false; } // 扣除库存。(订单生成后,库存产品库存。) // 备注)需要另起一个脚本,用来处理半个小时后,还没有支付的订单,将订单取消,然后将订单里面的产品库存返还。 // 如果是无限库存(没有库存就去采购的方式),那么不需要跑这个脚本,将库存设置的非常大即可。 $deductStatus = Yii::$service->product->stock->deduct($cartInfo['products']); if (!$deductStatus) { // 库存不足则返回 return false; } $beforeEventName = 'event_generate_order_before'; $afterEventName = 'event_generate_order_after'; Yii::$service->event->trigger($beforeEventName, $cartInfo); if ($token) { // 有token 代表前面已经生成了order,直接通过token查询出来即可。 $myOrder = $this->getByPaymentToken($token); if (!$myOrder) { Yii::$service->helper->errors->add('order increment id is not exist.'); return false; } else { $increment_id = $myOrder['increment_id']; } } else { $myOrder = new $this->_orderModelName(); } $myOrder['order_status'] = $this->payment_status_pending; $currentStore = Yii::$service->store->currentStore; $currentStore || $currentStore = $cartInfo['store']; $myOrder['store'] = $currentStore; $myOrder['created_at'] = time(); $myOrder['updated_at'] = time(); $myOrder['items_count'] = $cartInfo['items_count']; $myOrder['total_weight'] = $cartInfo['product_weight']; $myOrder['order_currency_code'] = $currency_code; $myOrder['order_to_base_rate'] = $currency_rate; $myOrder['grand_total'] = $cartInfo['grand_total']; $myOrder['base_grand_total'] = $cartInfo['base_grand_total']; $myOrder['subtotal'] = $cartInfo['product_total']; $myOrder['base_subtotal'] = $cartInfo['base_product_total']; $myOrder['subtotal_with_discount'] = $cartInfo['coupon_cost']; $myOrder['base_subtotal_with_discount'] = $cartInfo['base_coupon_cost']; $myOrder['shipping_total'] = $cartInfo['shipping_cost']; $myOrder['base_shipping_total'] = $cartInfo['base_shipping_cost']; $myOrder['checkout_method'] = $this->getCheckoutType(); !$order_remark || $myOrder['order_remark'] = \yii\helpers\Html::encode($order_remark); if ($address['customer_id']) { $is_guest = 2; } else { $is_guest = 1; } if (!Yii::$app->user->isGuest) { $customer_id = Yii::$app->user->identity->id; } else { $customer_id = ''; } $myOrder['customer_id'] = $customer_id; $myOrder['customer_email'] = $address['email']; $myOrder['customer_firstname'] = $address['first_name']; $myOrder['customer_lastname'] = $address['last_name']; $myOrder['customer_is_guest'] = $is_guest; $myOrder['customer_telephone'] = $address['telephone']; $myOrder['customer_address_country']= $address['country']; $myOrder['customer_address_state'] = $address['state']; $myOrder['customer_address_city'] = $address['city']; $myOrder['customer_address_zip'] = $address['zip']; $myOrder['customer_address_street1']= $address['street1']; $myOrder['customer_address_street2']= $address['street2']; $myOrder['coupon_code'] = $cartInfo['coupon_code']; $myOrder['payment_method'] = $payment_method; $myOrder['shipping_method'] = $shipping_method; // 进行model验证。 if (!$myOrder->validate()) { $errors = $myOrder->errors; Yii::$service->helper->errors->addByModelErrors($errors); return false; } // 保存订单 $saveOrderStatus = $myOrder->save(); if (!$saveOrderStatus) { return false; } $order_id = $myOrder['order_id']; if (!$increment_id) { $increment_id = $this->generateIncrementIdByOrderId($order_id); $myOrder['increment_id'] = $increment_id; // 保存订单 $saveOrderStatus = $myOrder->save(); if (!$saveOrderStatus) { return false; } } Yii::$service->event->trigger($afterEventName, $myOrder); if ($myOrder[$this->getPrimaryKey()]) { // 保存订单产品 $saveItemStatus = Yii::$service->order->item->saveOrderItems($cartInfo['products'], $order_id, $cartInfo['store']); if (!$saveItemStatus) { return false; } // 订单生成成功,通过api传递数据给trace系统 $this->sendTracePaymentPendingOrder($myOrder, $cartInfo['products']); // 如果是登录用户,那么,在生成订单后,需要清空购物车中的产品和coupon。 if (!Yii::$app->user->isGuest && $clearCart) { Yii::$service->cart->clearCartProductAndCoupon(); } $this->createdOrder = $myOrder; // 执行成功,则在session中设置increment_id if (!Yii::$service->store->isAppserver()) { // appserver入口,没有session机制。 $this->setSessionIncrementId($increment_id); } return true; } else { Yii::$service->helper->errors->add('generate order fail'); return false; } }
@param $address | Array 货运地址数组,数组里的具体子项,可以在函数中查看 @param $shipping_method | String 货运快递方式 @param $payment_method | String 支付方式、 @param $clearCartAndDeductStock | boolean 是否清空购物车,并扣除库存,这种情况是先 生成订单,在支付的情况下失败的处理方式。 @param $token | string 代表 通过payment_token得到order,然后更新order信息的方式生成order,这个是paypal购物车express支付对应的功能 @param $order_remark | string , 订单备注 @return bool 通过购物车的数据生成订单是否成功 通过购物车中的产品信息,以及传递的货运地址,货运快递方式,支付方式生成订单。
generateOrderByCart
php
fecshop/yii2_fecshop
services/Order.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Order.php
BSD-3-Clause