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 filterProductImg($product_images){ $this->_image_thumbnails = $product_images; //$this->_image_detail['gallery'] = $product_images['gallery']; if (isset($product_images['main']['is_detail']) && $product_images['main']['is_detail'] == 1 ) { $this->_image_detail[] = $product_images['main']; } if (isset($product_images['gallery']) && is_array($product_images['gallery'])) { $thumbnails_arr = []; //$detail_arr = []; foreach ($product_images['gallery'] as $one) { $is_thumbnails = $one['is_thumbnails']; $is_detail = $one['is_detail']; if($is_thumbnails == 1){ $thumbnails_arr[] = $one; } if($is_detail == 1){ $this->_image_detail[] = $one; } } $this->_image_thumbnails['gallery'] = $thumbnails_arr; //$this->_image_detail = $detail_arr; } }
@param $product_images | Array ,产品的图片属性 根据图片数组,得到橱窗图,和描述图 橱窗图:在产品详细页面顶部,放大镜显示部分的产品列表 描述图,在产品description文字描述后面显示的产品图片。
filterProductImg
php
fecshop/yii2_fecshop
app/apphtml5/modules/Catalog/block/product/Index.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/apphtml5/modules/Catalog/block/product/Index.php
BSD-3-Clause
protected function getSpuData($select) { $spu = $this->_product['spu']; return Yii::$service->product->spuCollData($select, $this->_productSpuAttrArr, $spu); }
@param $select | Array , 需要查询的字段。 得到当前spu下面的所有的sku的数组、 这个是为了产品详细页面的spu下面的产品切换,譬如同一spu下的不同的颜色尺码切换。
getSpuData
php
fecshop/yii2_fecshop
app/apphtml5/modules/Catalog/block/product/Index.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/apphtml5/modules/Catalog/block/product/Index.php
BSD-3-Clause
protected function getSpuAttrInfo($spuAttr, $attrVal, $reverse_val_spu) { $current = $this->_currentSpuAttrValArr; $active = false; if (isset($this->_currentSpuAttrValArr[$spuAttr])) { if ($attrVal != $this->_currentSpuAttrValArr[$spuAttr]) { $current[$spuAttr] = $attrVal; } else { $active = true; } } $reverse_key = $this->generateSpuReverseKey($current); $return = []; $return['attr_val'] = $attrVal; $return['active'] = 'noactive'; if (isset($reverse_val_spu[$reverse_key]) && is_array($reverse_val_spu[$reverse_key])) { $return['active'] = 'active'; $arr = $reverse_val_spu[$reverse_key]; foreach ($arr as $k=>$v) { $return[$k] = $v; } if ($spuAttr == $this->_spuAttrShowAsImg) { $return['show_as_img'] = $arr['main_img']; } } else if ($this->_spuAttrShowAsImg){ // 如果是图片,不存在,则使用备用的。 if ($spuAttr == $this->_spuAttrShowAsImg) { $return['active'] = 'active'; $arr = $this->_spuAttrShowAsImgArr[$attrVal]; if (is_array($arr) && !empty($arr)) { foreach ($arr as $k=>$v) { $return[$k] = $v; } } $return['show_as_img'] = $arr['main_img']; } } else if ($this->_spuAttrShowAsTop){ if ($spuAttr == $this->_spuAttrShowAsTop) { $return['active'] = 'active'; $arr = $this->_spuAttrShowAsTopArr[$attrVal]; if (is_array($arr) && !empty($arr)) { foreach ($arr as $k=>$v) { $return[$k] = $v; } } } } if ($active) { $return['active'] = 'current'; } return $return; }
spu属性部分
getSpuAttrInfo
php
fecshop/yii2_fecshop
app/apphtml5/modules/Catalog/block/product/Index.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/apphtml5/modules/Catalog/block/product/Index.php
BSD-3-Clause
protected function sortSpuAttr($spuAttr, $data) { // 对size排序一下 $size = []; $attr_group = $this->_product['attr_group']; $attrInfo = Yii::$service->product->getGroupAttrInfo($attr_group); $size_arr = isset($attrInfo[$spuAttr]['display']['data']) ? $attrInfo[$spuAttr]['display']['data'] : ''; $d_arr = []; if (is_array($size_arr) && !empty($size_arr)) { foreach ($size_arr as $size) { if (isset($data[$size])) { $d_arr[$size] = $data[$size]; } } if (!empty($d_arr)) { // 不在里面的规格属性(新建产品添加的规格属性),添加进去 foreach ($data as $size=>$d) { if (!isset($d_arr[$size])) { $d_arr[$size] = $data[$size]; } } return $d_arr; } } return $data; }
@param $data | Array 各个尺码对应的产品数组 @return array 排序后的数组 该函数,按照在配置中的size的顺序,将$data中的数据进行排序,让其按照尺码的由小到大的顺序 排列,譬如 :s,m,l,xl,xxl,xxxl等
sortSpuAttr
php
fecshop/yii2_fecshop
app/apphtml5/modules/Catalog/block/product/Index.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/apphtml5/modules/Catalog/block/product/Index.php
BSD-3-Clause
protected function getProductPriceInfo() { $price = $this->_product['price']; $special_price = $this->_product['special_price']; $special_from = $this->_product['special_from']; $special_to = $this->_product['special_to']; return Yii::$service->product->price->getCurrentCurrencyProductPriceInfo($price, $special_price, $special_from, $special_to); }
@return array 得到当前货币状态下的产品的价格和特价信息。
getProductPriceInfo
php
fecshop/yii2_fecshop
app/apphtml5/modules/Catalog/block/product/Index.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/apphtml5/modules/Catalog/block/product/Index.php
BSD-3-Clause
protected function initProduct() { $primaryKey = Yii::$service->product->getPrimaryKey(); $primaryVal = Yii::$app->request->get($primaryKey); $this->_primaryVal = $primaryVal; $product = Yii::$service->product->getByPrimaryKey($primaryVal); if ($product) { $enableStatus = Yii::$service->product->getEnableStatus(); if ($product['status'] != $enableStatus){ return false; } } else { return false; } $this->_product = $product; Yii::$app->view->registerMetaTag([ 'name' => 'keywords', 'content' => Yii::$service->store->getStoreAttrVal($product['meta_keywords'], 'meta_keywords'), ]); Yii::$app->view->registerMetaTag([ 'name' => 'description', 'content' => Yii::$service->store->getStoreAttrVal($product['meta_description'], 'meta_description'), ]); $this->_title = Yii::$service->store->getStoreAttrVal($product['meta_title'], 'meta_title'); $name = Yii::$service->store->getStoreAttrVal($product['name'], 'name'); //$this->breadcrumbs($name); $this->_title = $this->_title ? $this->_title : $name; Yii::$app->view->title = $this->_title; //$this->_where = $this->initWhere(); // 通过上面查询的属性组,得到属性组对应的属性列表 // 然后重新查询产品 $attr_group = $this->_product['attr_group']; Yii::$service->product->addGroupAttrs($attr_group); // 重新查询产品信息。 $product = Yii::$service->product->getByPrimaryKey($primaryVal); $this->_product = $product; return true; }
初始化数据包括 主键值:$this->_primaryVal 当前产品对象:$this->_product Meta keywords , Meta Description等信息的设置。 Title的设置。 根据当前产品的attr_group(属性组信息)重新给Product Model增加相应的属性组信息 然后,重新获取当前产品对象:$this->_product,此时加入了配置中属性组对应的属性。
initProduct
php
fecshop/yii2_fecshop
app/apphtml5/modules/Catalog/block/product/Index.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/apphtml5/modules/Catalog/block/product/Index.php
BSD-3-Clause
protected function getProductBySkus($skus) { $buy_also_buy_sku = $this->_product['buy_also_buy_sku']; if ($buy_also_buy_sku) { $skus = explode(',', $buy_also_buy_sku); if (is_array($skus) && !empty($skus)) { $filter['select'] = [ 'sku', 'spu', 'name', 'image', 'price', 'special_price', 'special_from', 'special_to', 'url_key', 'score', ]; $filter['where'] = ['in', 'sku', $skus]; $products = Yii::$service->product->getProducts($filter); //var_dump($products); $products = Yii::$service->category->product->convertToCategoryInfo($products); return $products; } } }
买了的人还买了什么,通过产品字段取出来sku,然后查询得到。
getProductBySkus
php
fecshop/yii2_fecshop
app/apphtml5/modules/Catalog/block/product/Index.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/apphtml5/modules/Catalog/block/product/Index.php
BSD-3-Clause
protected function getAjaxProductHtml($products) { $parentThis['products'] = $products; $config = [ 'view' => 'cms/home/index/product.php', ]; $html = Yii::$service->page->widget->renderContent('category_product_price', $config, $parentThis); echo json_encode([ 'html' => $html, ]); exit; }
@param $products | Array 产品的数组。 ajax方式访问,得到产品的数据 这个是wap端手机页面访问,下拉自动加载下一页的数据的加载实现。
getAjaxProductHtml
php
fecshop/yii2_fecshop
app/apphtml5/modules/Catalog/block/category/Index.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/apphtml5/modules/Catalog/block/category/Index.php
BSD-3-Clause
protected function getFilterCategory() { $category_id = $this->_primaryVal; $parent_id = $this->_category['parent_id']; $filter_category = Yii::$service->category->getFilterCategory($category_id, $parent_id); return $filter_category; }
得到子分类,如果子分类不存在,则返回同级分类。
getFilterCategory
php
fecshop/yii2_fecshop
app/apphtml5/modules/Catalog/block/category/Index.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/apphtml5/modules/Catalog/block/category/Index.php
BSD-3-Clause
protected function getProductPageCount() { $productNumPerPage = $this->getNumPerPage(); $productCount = $this->_productCount; $pageNum = $this->getPageNum(); return $this->_page_count = ceil($productCount / $productNumPerPage); }
得到产品页面的toolbar部分 也就是分类页面的分页工具条部分。
getProductPageCount
php
fecshop/yii2_fecshop
app/apphtml5/modules/Catalog/block/category/Index.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/apphtml5/modules/Catalog/block/category/Index.php
BSD-3-Clause
protected function getFilterAttr() { return Yii::$service->category->getFilterAttr($this->_category); }
@return Array 得到当前分类,侧栏用于过滤的属性数组,由三部分计算得出 1.全局默认属性过滤(catalog module 配置文件中配置 category_filter_attr), 2.当前分类属性过滤,也就是分类表的 filter_product_attr_selected 字段 3.当前分类去除的属性过滤,也就是分类表的 filter_product_attr_unselected 最终出来一个当前分类,用于过滤的属性数组。
getFilterAttr
php
fecshop/yii2_fecshop
app/apphtml5/modules/Catalog/block/category/Index.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/apphtml5/modules/Catalog/block/category/Index.php
BSD-3-Clause
protected function getFormatFilterPrice($price_item) { list($f_price, $l_price) = explode('-', $price_item); $str = ''; if ($f_price == '0' || $f_price) { $f_price = Yii::$service->product->price->formatPrice($f_price); $str .= $f_price['symbol'].$f_price['value'].'---'; } if ($l_price) { $l_price = Yii::$service->product->price->formatPrice($l_price); $str .= $l_price['symbol'].$l_price['value']; } return $str; }
格式化价格格式,侧栏价格过滤部分
getFormatFilterPrice
php
fecshop/yii2_fecshop
app/apphtml5/modules/Catalog/block/category/Index.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/apphtml5/modules/Catalog/block/category/Index.php
BSD-3-Clause
protected function getOrderBy() { $primaryKey = Yii::$service->category->getPrimaryKey(); $sort = Yii::$app->request->get($this->_sort); $direction = Yii::$app->request->get($this->_direction); $sortConfig = $this->_sort_items;; if (is_array($sortConfig)) { if ($sort && isset($sortConfig[$sort])) { $orderInfo = $sortConfig[$sort]; } else { foreach ($sortConfig as $k => $v) { $orderInfo = $v; if (!$direction) { $direction = $v['direction']; } break; } } $db_columns = $orderInfo['db_columns']; $storageName = Yii::$service->product->serviceStorageName(); if ($direction == 'desc') { $direction = $storageName == 'mongodb' ? -1 : SORT_DESC; } else { $direction = $storageName == 'mongodb' ? 1 :SORT_ASC; } return [$db_columns => $direction]; } }
用于搜索条件的排序部分
getOrderBy
php
fecshop/yii2_fecshop
app/apphtml5/modules/Catalog/block/category/Index.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/apphtml5/modules/Catalog/block/category/Index.php
BSD-3-Clause
protected function getNumPerPage() { if (!$this->_numPerPageVal) { $numPerPage = Yii::$app->request->get($this->_numPerPage); //$category_query_config = Yii::$app->getModule('catalog')->params['category_query']; $appName = Yii::$service->helper->getAppName(); $categoryConfigNumPerPage = Yii::$app->store->get($appName.'_catalog','category_query_numPerPage'); $category_query_config['numPerPage'] = explode(',',$categoryConfigNumPerPage); if (!$numPerPage) { if (isset($category_query_config['numPerPage'])) { if (is_array($category_query_config['numPerPage'])) { $this->_numPerPageVal = $category_query_config['numPerPage'][0]; } } } elseif (!$this->_numPerPageVal) { if (isset($category_query_config['numPerPage']) && is_array($category_query_config['numPerPage'])) { $numPerPageArr = $category_query_config['numPerPage']; if (in_array((int) $numPerPage, $numPerPageArr)) { $this->_numPerPageVal = $numPerPage; } else { throw new InvalidValueException('Incorrect numPerPage value:'.$numPerPage); } } } } return $this->_numPerPageVal; }
分类页面的产品,每页显示的产品个数。 对于前端传递的个数参数,在后台验证一下是否是合法的个数(配置里面有一个分类产品个数列表) 如果不合法,则报异常 这个功能是为了防止分页攻击,伪造大量的不同个数的url,绕过缓存。
getNumPerPage
php
fecshop/yii2_fecshop
app/apphtml5/modules/Catalog/block/category/Index.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/apphtml5/modules/Catalog/block/category/Index.php
BSD-3-Clause
protected function getPageNum() { $numPerPage = Yii::$app->request->get($this->_page); return $numPerPage ? (int) $numPerPage : 1; }
得到当前第几页
getPageNum
php
fecshop/yii2_fecshop
app/apphtml5/modules/Catalog/block/category/Index.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/apphtml5/modules/Catalog/block/category/Index.php
BSD-3-Clause
protected function getCategoryProductColl() { $productPrimaryKey = Yii::$service->product->getPrimaryKey(); $select = [ $productPrimaryKey, 'sku', 'spu', 'name', 'image', 'price', 'special_price', 'special_from', 'special_to', 'url_key', 'score','is_in_stock', ]; if (is_array($this->_sort_items)) { foreach ($this->_sort_items as $sort_item) { $select[] = $sort_item['db_columns']; } } $filter = [ 'pageNum' => $this->getPageNum(), 'numPerPage' => $this->getNumPerPage(), 'orderBy' => $this->getOrderBy(), 'where' => $this->_where, 'select' => $select, ]; //var_dump($filter);exit; return Yii::$service->category->product->getFrontList($filter); }
得到当前分类的产品
getCategoryProductColl
php
fecshop/yii2_fecshop
app/apphtml5/modules/Catalog/block/category/Index.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/apphtml5/modules/Catalog/block/category/Index.php
BSD-3-Clause
protected function initCategory() { $primaryKey = Yii::$service->category->getPrimaryKey(); $primaryVal = Yii::$app->request->get($primaryKey); $this->_primaryVal = $primaryVal; $category = Yii::$service->category->getByPrimaryKey($primaryVal); if ($category) { $enableStatus = Yii::$service->category->getCategoryEnableStatus(); if ($category['status'] != $enableStatus){ return false; } } else { return false; } $this->_category = $category; Yii::$app->view->registerMetaTag([ 'name' => 'keywords', 'content' => Yii::$service->store->getStoreAttrVal($category['meta_keywords'], 'meta_keywords'), ]); Yii::$app->view->registerMetaTag([ 'name' => 'description', 'content' => Yii::$service->store->getStoreAttrVal($category['meta_description'], 'meta_description'), ]); $this->_title = Yii::$service->store->getStoreAttrVal($category['title'], 'title'); $name = Yii::$service->store->getStoreAttrVal($category['name'], 'name'); $this->breadcrumbs($name); $this->_title = $this->_title ? $this->_title : $name; Yii::$app->view->title = $this->_title; $this->_where = $this->initWhere(); return true; }
分类部分的初始化 对一些属性进行赋值。
initCategory
php
fecshop/yii2_fecshop
app/apphtml5/modules/Catalog/block/category/Index.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/apphtml5/modules/Catalog/block/category/Index.php
BSD-3-Clause
public function actionIndex() { $data = $this->getBlock()->getLastData(); if(is_array($data)){ return $this->render($this->action->id, $data); } }
网站信息管理
actionIndex
php
fecshop/yii2_fecshop
app/apphtml5/modules/Catalog/controllers/ProductController.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/apphtml5/modules/Catalog/controllers/ProductController.php
BSD-3-Clause
public function actionGetcoprice() { $custom_option_sku = Yii::$app->request->get('custom_option_sku'); $product_id = Yii::$app->request->get('product_id'); $qty = Yii::$app->request->get('qty'); $cart_price = 0; $custom_option_price = 0; $product = Yii::$service->product->getByPrimaryKey($product_id); $cart_price = Yii::$service->product->price->getCartPriceByProductId($product_id, $qty, $custom_option_sku); if (!$cart_price) { return; } $price_info = [ 'price' => $cart_price, ]; $priceView = [ 'view' => 'catalog/product/index/price.php', ]; $priceParam = [ 'price_info' => $price_info, ]; echo json_encode([ 'price' =>Yii::$service->page->widget->render($priceView, $priceParam), ]); exit; }
ajax 得到产品加入购物车的价格。
actionGetcoprice
php
fecshop/yii2_fecshop
app/apphtml5/modules/Catalog/controllers/ProductController.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/apphtml5/modules/Catalog/controllers/ProductController.php
BSD-3-Clause
public function actionAdd() { $data = $this->getBlock()->getLastData(); return $data; //return $this->render($this->action->id,$data); }
增加收藏
actionAdd
php
fecshop/yii2_fecshop
app/apphtml5/modules/Catalog/controllers/FavoriteproductController.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/apphtml5/modules/Catalog/controllers/FavoriteproductController.php
BSD-3-Clause
public function actionIndex() { $data = $this->getBlock()->getLastData(); if(is_array($data)){ return $this->render($this->action->id, $data); } }
分类页面。
actionIndex
php
fecshop/yii2_fecshop
app/apphtml5/modules/Catalog/controllers/CategoryController.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/apphtml5/modules/Catalog/controllers/CategoryController.php
BSD-3-Clause
public static function initReviewConfig() { // $reviewParam = Yii::$app->getModule('catalog')->params['review']; //$appName = Yii::$service->helper->getAppName(); //$reviewFilterByLang = Yii::$app->store->get($appName.'_catalog','review_filterByLang'); //$reviewFilterByLang = ($reviewFilterByLang == Yii::$app->store->enable) ? true : false; // //if (isset($reviewParam['filterByLang'])) { // Yii::$service->product->review->filterByLang = $reviewParam['filterByLang']; //} }
初始化当前appfront的设置,覆盖service的初始设置。
initReviewConfig
php
fecshop/yii2_fecshop
app/apphtml5/modules/Catalog/helpers/Review.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/apphtml5/modules/Catalog/helpers/Review.php
BSD-3-Clause
public static function getReviewAndStarCount($product) { // 这个是是否通过语言进行过滤评论,可以通过上面的函数 self::initReviewConfig进行初始化, // 也就是通过当前模块的配置,来覆盖service的配置 $filterByLang = Yii::$service->product->review->filterByLang; if ($filterByLang) { $langCode = Yii::$service->store->currentLangCode; if ($langCode) { $a = Yii::$service->fecshoplang->getLangAttrName('review_count_lang', $langCode); $b = Yii::$service->fecshoplang->getLangAttrName('reviw_rate_star_average_lang', $langCode); $c = Yii::$service->fecshoplang->getLangAttrName('reviw_rate_star_info_lang', $langCode); $review_count_lang = 0; if (isset($product['review_count_lang'][$a])) { $review_count_lang = $product['review_count_lang'][$a]; $review_count_lang = $review_count_lang ? $review_count_lang : 0; } $reviw_rate_star_average_lang = 0; if (isset($product['reviw_rate_star_average_lang'][$b])) { $reviw_rate_star_average_lang = $product['reviw_rate_star_average_lang'][$b]; $reviw_rate_star_average_lang = $reviw_rate_star_average_lang ? $reviw_rate_star_average_lang : 0; } $reviw_rate_star_info_lang = []; if (isset($product['reviw_rate_star_info_lang'][$c])) { $reviw_rate_star_info_lang = $product['reviw_rate_star_info_lang'][$c]; $reviw_rate_star_info_lang = $reviw_rate_star_info_lang ? $reviw_rate_star_info_lang : []; } $reviw_rate_star_info = self::getRateStarInfo($review_count_lang, $reviw_rate_star_info_lang); return [$review_count_lang, $reviw_rate_star_average_lang, $reviw_rate_star_info]; } } else { $review_count = $product['review_count'] ? $product['review_count'] : 0; $reviw_rate_star_average = $product['reviw_rate_star_average'] ? $product['reviw_rate_star_average'] : 0; $reviw_rate_star_info = $product['reviw_rate_star_info'] ? $product['reviw_rate_star_info'] : []; $reviw_rate_star_info = self::getRateStarInfo($review_count, $reviw_rate_star_info); return [$review_count, $reviw_rate_star_average, $reviw_rate_star_info]; } }
@param $product | Object @return array 通过service的配置,是否通过语言进行过滤产品的总个数 如果不通过语言,则直接将产品的属性 review_count reviw_rate_star_average 返回 如果通过语言,那么通过属性 review_count_lang reviw_rate_star_average_lang ,在通过当前的语言获取相应的属性值。
getReviewAndStarCount
php
fecshop/yii2_fecshop
app/apphtml5/modules/Catalog/helpers/Review.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/apphtml5/modules/Catalog/helpers/Review.php
BSD-3-Clause
public static function price($price, $bits = 2) { return number_format($price, $bits); }
@param $price | Float ,价格 @param $bits | Int , 小数点后几位的格式,譬如4.00 @return float, 返回格式化后的数据 一般用于模板中,按照显示格式显示产品数据。
price
php
fecshop/yii2_fecshop
app/apphtml5/helper/Format.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/apphtml5/helper/Format.php
BSD-3-Clause
public function beforeAction($action) { if ($this->action->id != 'complete') { $installLockFilePath = $this->getInstallLockFilePath(); try { if (file_exists($installLockFilePath) && "1" === file_get_contents($installLockFilePath)) { Yii::$app->response->data = "您的项目可能已安装成功,如果需要重新安装,请手动删除项目目录的 <code>".$installLockFilePath."</code> 文件"; return false; } else { file_put_contents($installLockFilePath, "0"); } } catch (\Exception $exception) { Yii::$app->response->data = $exception->getMessage(); return false; } } return parent::beforeAction($action); }
增加前置动作,检查指定目录是否存在 install.lock 锁文件,存在则不允许再次安装 @param \yii\base\Action $action @return bool @throws \yii\web\BadRequestHttpException @author Yanlongli<[email protected]>
beforeAction
php
fecshop/yii2_fecshop
app/appinstall/modules/Database/controllers/ConfigController.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appinstall/modules/Database/controllers/ConfigController.php
BSD-3-Clause
public function actionIndex() { $database = Yii::$app->request->get('database'); if (!$database) { return $this->render('readme', []); } $editForm = Yii::$app->request->post('editForm'); if ($editForm && $this->checkDatabaseData($editForm) && $this->updateDatabaseConfig($editForm)) { Yii::$app->session->setFlash('database-success', 'Mysql配置成功,写入的配置文件路径为: @common/config/main-local.php'); // 进行跳转 $homeUrl = Yii::$app->homeUrl; return $this->redirect($homeUrl . '/database/config/migrate'); } $errorInfo = Yii::$app->session->getFlash('database-errors'); $errorInfo = $this->getErrorHtml($errorInfo); return $this->render($this->action->id, [ 'errorInfo' => $errorInfo, 'editForm' => $editForm, ]); }
public $_migrateLog = ''; 安装默认第一步页面
actionIndex
php
fecshop/yii2_fecshop
app/appinstall/modules/Database/controllers/ConfigController.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appinstall/modules/Database/controllers/ConfigController.php
BSD-3-Clause
public function actionMigrate() { $isPost = Yii::$app->request->post('isPost'); if ($isPost ) { // 进行数据库初始化 if ($this->runMigrate()) { $successInfo = '数据库migrate初始化完成'; $successInfo = $this->getSuccessHtml($successInfo); //exit; return $this->render('migratesuccess', [ 'successInfo' => $successInfo, 'nextUrl' => Yii::$app->homeUrl . '/database/config/addtestdata', 'skipUrl' => Yii::$app->homeUrl . '/database/config/initdomain', ]); } else { $errors = 'migrate 失败,你可以在logs文件中查看具体原因(@appfront/config/main.php中log组件,对应的logFile配置,查看该log文件,如果没有可以手动创建该log文件,清空数据库,重新执行该操作)'; Yii::$app->session->setFlash('migrate-errors', $errors); } } $successInfo = Yii::$app->session->getFlash('database-success'); $successInfo = $this->getSuccessHtml($successInfo); $errorInfo = Yii::$app->session->getFlash('migrate-errors'); $errorInfo = $this->getErrorHtml($errorInfo); return $this->render($this->action->id, [ 'successInfo' => $successInfo, 'errorInfo' => $errorInfo, ]); }
数据库migrate页面
actionMigrate
php
fecshop/yii2_fecshop
app/appinstall/modules/Database/controllers/ConfigController.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appinstall/modules/Database/controllers/ConfigController.php
BSD-3-Clause
public function actionAddtestdata() { if ($this->addProductData()) { $successInfo = $this->getSuccessHtml('产品测试数据添加成功'); } else { $errorInfo = Yii::$app->session->getFlash('add-test-data-errors'); $errorInfo = $this->getErrorHtml($errorInfo); } return $this->render($this->action->id, [ 'errorInfo' => $errorInfo, 'successInfo' => $successInfo, 'initUrl' => Yii::$app->homeUrl . '/database/config/addtestdatainit', 'nextUrl' => Yii::$app->homeUrl . '/database/config/initdomain', //'migrateLog' => $this->_migrateLog ]); }
产品测试数据添加
actionAddtestdata
php
fecshop/yii2_fecshop
app/appinstall/modules/Database/controllers/ConfigController.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appinstall/modules/Database/controllers/ConfigController.php
BSD-3-Clause
public function actionInitdomain() { $editForm = Yii::$app->request->post('editForm'); if (is_array($editForm) && !empty($editForm)) { $appfront_domain = $editForm['appfront_domain']; $img_domain = $editForm['img_domain']; $mall_type = $editForm['mall_type']; if ($appfront_domain && $img_domain && $mall_type) { $this->updateDomainConfig($editForm); return $this->redirect(Yii::$app->homeUrl . '/database/config/initadminuser'); } else { $this->_install_errors .= 'pc域名,图片域名, 商城类型 不能为空'; } } $errorInfo = $this->getErrorHtml($this->_install_errors); return $this->render($this->action->id, [ 'errorInfo' => $errorInfo, 'editForm' => $editForm, 'demoDomainList' => $this->getDemoDomainList(), ]); }
域名部分
actionInitdomain
php
fecshop/yii2_fecshop
app/appinstall/modules/Database/controllers/ConfigController.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appinstall/modules/Database/controllers/ConfigController.php
BSD-3-Clause
public function updateDomainConfig($editForm) { $appfront_domain = $editForm['appfront_domain']; $img_domain = $editForm['img_domain']; $apphtml5_domain = $editForm['apphtml5_domain']; $appserver_domain = $editForm['appserver_domain']; $mall_type = $editForm['mall_type']; // 商城类型 $baseCurrencyCode = ''; $storeLanguageCode = ''; $storeLanguageName = ''; if ($mall_type == 'china') { $baseCurrencyCode = 'CNY'; $storeLanguageCode = 'zh-CN'; $storeLanguageName = '中文'; } else { $baseCurrencyCode = 'USD'; $storeLanguageCode = 'en-US'; $storeLanguageName = 'English'; } // pc if ($appfront_domain) { // appfront 更新 $appfrontOne = StoreDomain::findOne([ 'app_name' => 'appfront', 'status' => 1, ]); if ($appfrontOne['id']) { $appfrontOne['key'] = $appfront_domain; $appfrontOne['lang'] = $storeLanguageCode; $appfrontOne['lang_name'] = $storeLanguageName; $appfrontOne['currency'] = $baseCurrencyCode; $appfrontOne['mobile_type'] = 'apphtml5'; if ($apphtml5_domain) { $appfrontOne['mobile_redirect_domain'] = $apphtml5_domain; } else { $appfrontOne['mobile_enable'] = 2; } $appfrontOne['updated_at'] = time(); $appfrontOne->save(); } } if ($apphtml5_domain) { // apphtml5 更新 $apphtml5One = StoreDomain::findOne([ 'app_name' => 'apphtml5', 'status' => 1, ]); if ($apphtml5One['id']) { $apphtml5One['key'] = $apphtml5_domain; $apphtml5One['lang'] = $storeLanguageCode; $apphtml5One['lang_name'] = $storeLanguageName; $apphtml5One['currency'] = $baseCurrencyCode; $apphtml5One['updated_at'] = time(); $apphtml5One->save(); } } // base_info if ($img_domain) { $baseConfig = StoreBaseConfig::findOne([ 'key' => 'base_info', ]); if ($baseConfig['id']) { $configArr = unserialize($baseConfig['value']); $configArr['image_domain'] = 'http://'.$img_domain; $configArr['base_currency'] = $baseCurrencyCode; $configArr['default_currency'] = $baseCurrencyCode; $baseConfig['value'] = serialize($configArr); $baseConfig['updated_at'] = time(); $baseConfig->save(); } } // 设置汇率 // {s:13:"currency_code";s:3:"CNY";s:15:"currency_symbol";s:3:"¥";s:13:"currency_rate";s:3:"6.3";}} if ($mall_type == 'china') { $baseConfig = StoreBaseConfig::findOne([ 'key' => 'currency', ]); if ($baseConfig['id']) { $configArr = [ [ 'currency_code' => $baseCurrencyCode, 'currency_symbol' => '¥', 'currency_rate' => 1, ] ]; $baseConfig['value'] = serialize($configArr); $baseConfig['updated_at'] = time(); $baseConfig->save(); } } if ($appserver_domain) { $baseConfig = StoreBaseConfig::findOne([ 'key' => 'appserver_store', ]); if ($baseConfig['id']) { $configArr = unserialize($baseConfig['value']); $configArr['key'] = $appserver_domain; $configArr['lang'] = $storeLanguageCode; $configArr['lang_name'] = $storeLanguageName; $configArr['currency'] = $baseCurrencyCode; $baseConfig['value'] = serialize($configArr); $baseConfig['updated_at'] = time(); $baseConfig->save(); } } }
将提交的信息更新到数据库
updateDomainConfig
php
fecshop/yii2_fecshop
app/appinstall/modules/Database/controllers/ConfigController.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appinstall/modules/Database/controllers/ConfigController.php
BSD-3-Clause
public function actionInitadminuser() { $editForm = Yii::$app->request->post('editForm'); if (is_array($editForm) && !empty($editForm)) { $param = [ 'username' => $editForm['username'], 'password' => $editForm['password'], ]; if ($this->installUpdateUser($param)) { $homeUrl = Yii::$app->homeUrl; return $this->redirect($homeUrl . '/database/config/complete'); } } $errorInfo = $this->getErrorHtml($this->_install_errors); return $this->render($this->action->id, [ 'errorInfo' => $errorInfo, 'editForm' => $editForm, ]); }
安装默认第一步页面
actionInitadminuser
php
fecshop/yii2_fecshop
app/appinstall/modules/Database/controllers/ConfigController.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appinstall/modules/Database/controllers/ConfigController.php
BSD-3-Clause
public function addProductData() { // 1. 图片的复制 $sourcePath = dirname(Yii::getAlias('@common')) . '/environments/test_data/appimage'; //$sourcePath = Yii::getAlias('@fectmelani/app/appimage'); $targetPath = Yii::getAlias('@appimage'); $this->copyDir($sourcePath, $targetPath); // 2. sql文件的执行 $sqlFile = dirname(Yii::getAlias('@common')) . '/environments/test_data/fecshop.sql'; $sqlStr = file_get_contents($sqlFile); $conn = Yii::$app->db; $innerTransaction = $conn->beginTransaction(); try { $result = $conn->createCommand($sqlStr)->execute(); $innerTransaction->commit(); return true; } catch (\Exception $e) { $innerTransaction->rollBack(); $message = $e->getMessage(); Yii::$app->session->setFlash('add-test-data-errors', $message); } return false; }
进行sql migrate ,产品图片的复制
addProductData
php
fecshop/yii2_fecshop
app/appinstall/modules/Database/controllers/ConfigController.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appinstall/modules/Database/controllers/ConfigController.php
BSD-3-Clause
public function actionComplete() { // 锁定安装向导 $installLockFilePath = $this->getInstallLockFilePath(); file_put_contents($installLockFilePath, "1"); return $this->render($this->action->id, []); }
完成页面
actionComplete
php
fecshop/yii2_fecshop
app/appinstall/modules/Database/controllers/ConfigController.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appinstall/modules/Database/controllers/ConfigController.php
BSD-3-Clause
public function updateDatabaseConfig($editForm) { $host = $editForm['host']; $database = $editForm['database']; $user = $editForm['user']; $port = $editForm['port']; if (!$port) { $port = '3306'; } $password = $editForm['password']; $dbConfig = [ 'class' => 'yii\db\Connection', 'dsn' => 'mysql:host='.$host.';port='.$port.';dbname='.$database, 'username' => $user, 'password' => $password, 'charset' => 'utf8', ]; $connection = Yii::createObject($dbConfig); $connError = ''; try { $connection->open(); } catch (\Exception $e) { $connError = $e->getMessage(); Yii::$app->session->setFlash('database-errors', $connError); return false; } // 将信息写入配置文件 $mainLocalFile = Yii::getAlias("@common/config/main-local.php"); if(!file_exists($mainLocalFile)){ $errors = 'config file[@common/config/main-local.php] is not exist, you exec init command before install'; Yii::$app->session->setFlash('database-errors', $errors); return false; } // 得到文件的内容 $mainLocalInfo = file_get_contents($mainLocalFile); //$mainLocalInfo = require($mainLocalFile); // 进行文件替换 $mainLocalInfo = str_replace('{mysql_host}', $host, $mainLocalInfo); $mainLocalInfo = str_replace('{mysql_port}', $port, $mainLocalInfo); $mainLocalInfo = str_replace('{mysql_database}', $database, $mainLocalInfo); $mainLocalInfo = str_replace('{mysql_user}', $user, $mainLocalInfo); $mainLocalInfo = str_replace('{mysql_password}', $password, $mainLocalInfo); // 写入配置文件 if (@file_put_contents($mainLocalFile, $mainLocalInfo) === false) { $errors = 'Unable to write the file '.$mainLocalFile; Yii::$app->session->setFlash('database-errors', $errors); return false; } // 设置数据库文件644, 需要shell手动设置文件权限。 /* if (@chmod($mainLocalFile,0644) === false) { $errors = 'Unable to set mainLocalFile 644, please change it'; Yii::$app->session->setFlash('database', $errors); return false; } */ return true; }
进行数据库的信息的检查,以及将数据库信息写入文件
updateDatabaseConfig
php
fecshop/yii2_fecshop
app/appinstall/modules/Database/controllers/ConfigController.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appinstall/modules/Database/controllers/ConfigController.php
BSD-3-Clause
public function checkDatabaseData($editForm) { $session = Yii::$app->session; if (!$editForm['host']) { $session->setFlash('database-errors', 'Mysql数据库Host为空'); return false; } if (!$editForm['database']) { $session->setFlash('database-errors', 'Mysql数据库名称为空'); return false; } if (!$editForm['user']) { $session->setFlash('database-errors', 'Mysql数据库账户为空'); return false; } if (!$editForm['password']) { $session->setFlash('database-errors', 'Mysql数据库密码为空'); return false; } //if (!$editForm['username']) { // $session->setFlash('database-errors', '超级管理员账户为空'); // // return false; //} //if (!$editForm['useremail']) { // $session->setFlash('database-errors', '超级账户邮箱为空'); // // return false; // } //if (!$editForm['userpassword']) { // $session->setFlash('database-errors', '超级账户密码为空'); // // return false; // } return true; }
检查前端传递的参数
checkDatabaseData
php
fecshop/yii2_fecshop
app/appinstall/modules/Database/controllers/ConfigController.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appinstall/modules/Database/controllers/ConfigController.php
BSD-3-Clause
public function dirMkdir($path = '', $mode = 0777, $recursive = true) { clearstatcache(); if (!is_dir($path)) { mkdir($path, $mode, $recursive); return chmod($path, $mode); } return true; }
创建文件夹,在图片文件复制的过程中使用。
dirMkdir
php
fecshop/yii2_fecshop
app/appinstall/modules/Database/controllers/ConfigController.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appinstall/modules/Database/controllers/ConfigController.php
BSD-3-Clause
public function copyDir($sourcePath, $targetPath, $isForce = true) { if (empty($sourcePath) || empty($targetPath)) { return false; } $dir = opendir($sourcePath); $this->dirMkdir($targetPath); while (false !== ($file = readdir($dir))) { if (($file != '.') && ($file != '..')) { $sourcePathFile = $sourcePath . '/' . $file; $targetPathFile = $targetPath . '/' . $file; if (is_dir($sourcePathFile)) { $this->copyDir($sourcePathFile, $targetPathFile); } else { //copy($sourcePath . '/' . $file, $targetPath . '/' . $file); if ($isForce) { copy($sourcePathFile, $targetPathFile); } else if (!file_exists($targetPathFile)) { copy($sourcePathFile, $targetPathFile); } else { //Yii::$service->helper->errors->add('target path:' . $targetPathFile . ' is exist.'); } } } } closedir($dir); return true; }
文件夹文件拷贝(递归) @param string $sourcePath 来源文件夹 @param string $targetPath 目的地文件夹 @param boolean $isForce 是否强制复制 @return bool
copyDir
php
fecshop/yii2_fecshop
app/appinstall/modules/Database/controllers/ConfigController.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appinstall/modules/Database/controllers/ConfigController.php
BSD-3-Clause
public function getBlock($blockName = '') { if (!$blockName) { $blockName = $this->action->id; } if (!$this->blockNamespace) { $this->blockNamespace = Yii::$app->controller->module->blockNamespace; } if (!$this->blockNamespace) { throw new \yii\web\HttpException(406, 'blockNamespace is empty , you should config it in module->blockNamespace or controller blockNamespace '); } $viewId = $this->id; $viewId = str_replace('/', '\\', $viewId); $relativeFile = '\\'.$this->blockNamespace; $relativeFile .= '\\'.$viewId.'\\'.ucfirst($blockName); //查找是否在rewriteMap中存在重写 $relativeFile = Yii::mapGetName($relativeFile); return new $relativeFile(); }
get current block you can change $this->blockNamespace.
getBlock
php
fecshop/yii2_fecshop
app/appserver/modules/AppserverTokenController.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appserver/modules/AppserverTokenController.php
BSD-3-Clause
public function getBlock($blockName = '') { if (!$blockName) { $blockName = $this->action->id; } if (!$this->blockNamespace) { $this->blockNamespace = Yii::$app->controller->module->blockNamespace; } if (!$this->blockNamespace) { throw new \yii\web\HttpException(406, 'blockNamespace is empty , you should config it in module->blockNamespace or controller blockNamespace '); } $viewId = $this->id; $viewId = str_replace('/', '\\', $viewId); $relativeFile = '\\'.$this->blockNamespace; $relativeFile .= '\\'.$viewId.'\\'.ucfirst($blockName); //查找是否在rewriteMap中存在重写 $relativeFile = Yii::mapGetName($relativeFile); return new $relativeFile(); }
get current block you can change $this->blockNamespace.
getBlock
php
fecshop/yii2_fecshop
app/appserver/modules/AppserverController.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appserver/modules/AppserverController.php
BSD-3-Clause
public function sendLoginEmail($param) { if ($param) { Yii::$service->email->customer->sendLoginEmail($param); } }
发送用户登陆邮件
sendLoginEmail
php
fecshop/yii2_fecshop
app/appserver/modules/Customer/block/account/Login.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appserver/modules/Customer/block/account/Login.php
BSD-3-Clause
public function actionAccount(){ if(Yii::$app->request->getMethod() === 'OPTIONS'){ return []; } $identity = Yii::$service->customer->loginByAccessToken(get_class($this)); if($identity){ // 用户已经登录 $code = Yii::$service->helper->appserver->account_is_logined; $data = []; $responseData = Yii::$service->helper->appserver->getResponseData($code, $data); return $responseData; } $email = Yii::$app->request->post('email'); $password = Yii::$app->request->post('password'); //$loginParam = \Yii::$app->getModule('customer')->params['login']; //$loginCaptchaActive = isset($loginParam['loginPageCaptcha']) ? $loginParam['loginPageCaptcha'] : false; $appName = Yii::$service->helper->getAppName(); $loginPageCaptcha = Yii::$app->store->get($appName.'_account', 'loginPageCaptcha'); $loginCaptchaActive = ($loginPageCaptcha == Yii::$app->store->enable) ? true : false; if($loginCaptchaActive){ $captcha = Yii::$app->request->post('captcha'); if(!Yii::$service->helper->captcha->validateCaptcha($captcha)){ $code = Yii::$service->helper->appserver->status_invalid_captcha; $data = []; $responseData = Yii::$service->helper->appserver->getResponseData($code, $data); return $responseData; } } if (Yii::$service->email->customer->registerAccountIsNeedEnableByEmail) { $identity = Yii::$service->customer->getAvailableUserIdentityByEmail($email); if (!$identity['email']) { $code = Yii::$service->helper->appserver->account_login_invalid_email_or_password; $data = []; $responseData = Yii::$service->helper->appserver->getResponseData($code, $data); return $responseData; } // 账户未激活 if ($identity['status'] == $identity::STATUS_REGISTER_DISABLE) { $code = Yii::$service->helper->appserver->account_register_disable; $data = []; $responseData = Yii::$service->helper->appserver->getResponseData($code, $data); return $responseData; } } $accessToken = Yii::$service->customer->loginAndGetAccessToken($email,$password); if($accessToken){ $code = Yii::$service->helper->appserver->status_success; $data = []; $responseData = Yii::$service->helper->appserver->getResponseData($code, $data); return $responseData; }else{ $code = Yii::$service->helper->appserver->account_login_invalid_email_or_password; $data = []; $responseData = Yii::$service->helper->appserver->getResponseData($code, $data); return $responseData; } }
登录用户的部分
actionAccount
php
fecshop/yii2_fecshop
app/appserver/modules/Customer/controllers/LoginController.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appserver/modules/Customer/controllers/LoginController.php
BSD-3-Clause
public function actionIndex(){ if(Yii::$app->request->getMethod() === 'OPTIONS'){ return []; } $identity = Yii::$service->customer->loginByAccessToken(get_class($this)); if($identity){ // 用户已经登录 $code = Yii::$service->helper->appserver->account_is_logined; $data = [ ]; $responseData = Yii::$service->helper->appserver->getResponseData($code, $data); return $responseData; } $appName = Yii::$service->helper->getAppName(); $loginPageCaptcha = Yii::$app->store->get($appName.'_account', 'loginPageCaptcha'); $loginCaptchaActive = ($loginPageCaptcha == Yii::$app->store->enable) ? true : false; //$loginParam = \Yii::$app->getModule('customer')->params['login']; //$loginCaptchaActive = isset($loginParam['loginPageCaptcha']) ? $loginParam['loginPageCaptcha'] : false; $googleRedirectUrl = Yii::$app->request->get('googleRedirectUrl'); $facebookRedirectUrl = Yii::$app->request->get('facebookRedirectUrl'); $code = Yii::$service->helper->appserver->status_success; $data = [ 'loginCaptchaActive'=> $loginCaptchaActive, 'googleLoginUrl' => Yii::$service->customer->google->getLoginUrl($googleRedirectUrl,true), 'facebookLoginUrl' => Yii::$service->customer->facebook->getLoginUrl($facebookRedirectUrl,true), ]; $responseData = Yii::$service->helper->appserver->getResponseData($code, $data); return $responseData; }
登录页面
actionIndex
php
fecshop/yii2_fecshop
app/appserver/modules/Customer/controllers/LoginController.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appserver/modules/Customer/controllers/LoginController.php
BSD-3-Clause
public function actionWxindex() { if(Yii::$app->request->getMethod() === 'OPTIONS'){ return []; } $identity = Yii::$service->customer->loginByAccessToken(get_class($this)); if($identity){ // 用户已经登录 $code = Yii::$service->helper->appserver->account_is_logined; $data = [ ]; $responseData = Yii::$service->helper->appserver->getResponseData($code, $data); return $responseData; } $code = Yii::$service->helper->appserver->status_success; $data = [ ]; $responseData = Yii::$service->helper->appserver->getResponseData($code, $data); return $responseData; }
登录页面
actionWxindex
php
fecshop/yii2_fecshop
app/appserver/modules/Customer/controllers/LoginController.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appserver/modules/Customer/controllers/LoginController.php
BSD-3-Clause
public function loginByIdentity($identity, $wx_session_key) { // 进行用户登陆 return Yii::$service->customer->loginByIdentityAndGetAccessToken($identity, $wx_session_key); }
登陆用户
loginByIdentity
php
fecshop/yii2_fecshop
app/appserver/modules/Customer/controllers/LoginController.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appserver/modules/Customer/controllers/LoginController.php
BSD-3-Clause
public function actionBindaccount() { $wxCode = Yii::$app->request->post('code'); //echo $wxCode; // 通过code 和 微信的一些验证信息,得到微信的信息uid $wxUserInfo = Yii::$service->helper->wx->getUserInfoByCode($wxCode); // 如果通过code获取微信信息(api获取)失败 if (!$wxUserInfo) { // code 获取openid失败 $code = Yii::$service->helper->appserver->account_wx_get_user_info_fail; $data = [ ]; $responseData = Yii::$service->helper->appserver->getResponseData($code, $data); return $responseData; } // 得到 openid 和 session_key $wx_openid = $wxUserInfo['openid']; $wx_session_key = $wxUserInfo['session_key']; if (!$wx_openid || !$wx_session_key) { $code = Yii::$service->helper->appserver->no_account_openid_and_session_key; $data = [ ]; $responseData = Yii::$service->helper->appserver->getResponseData($code, $data); return $responseData; } if (Yii::$service->customer->getByWxOpenid($wx_openid)) { // 已经存在绑定的用户,绑定失败 $code = Yii::$service->helper->appserver->account_has_account_openid; $data = [ ]; $responseData = Yii::$service->helper->appserver->getResponseData($code, $data); return $responseData; } $isBindNew = Yii::$app->request->post('isBindNew'); $email = Yii::$app->request->post('email'); $password = Yii::$app->request->post('password'); if ($isBindNew == 1) { // 进行注册 // 查看该email是否存在 if (Yii::$service->customer->isRegistered($email)) { $code = Yii::$service->helper->appserver->account_register_email_exit; $data = [ 'errors' => Yii::$service->helper->errors->get(), ]; $responseData = Yii::$service->helper->appserver->getResponseData($code, $data); return $responseData; } $param = [ 'email' => $email, 'password' => $password, ]; if (!Yii::$service->customer->register($param)) { $code = Yii::$service->helper->appserver->account_register_fail; $data = [ 'errors' => Yii::$service->helper->errors->get(), ]; $responseData = Yii::$service->helper->appserver->getResponseData($code, $data); return $responseData; } } // 账户登陆,如果登陆成功,则绑定wx_session_key 和 wx_openid $access_token = Yii::$service->customer->loginAndGetAccessToken($email, $password); if ($access_token) { $identity = Yii::$app->user->identity; $identity->wx_session_key = $wx_session_key; $identity->wx_openid = $wx_openid; $identity->save(); $code = Yii::$service->helper->appserver->status_success; $data = [ ]; $responseData = Yii::$service->helper->appserver->getResponseData($code, $data); return $responseData; } else { $code = Yii::$service->helper->appserver->account_login_and_get_access_token_fail; $data = [ 'errors' => Yii::$service->helper->errors->get(), ]; $responseData = Yii::$service->helper->appserver->getResponseData($code, $data); return $responseData; } }
绑定账户
actionBindaccount
php
fecshop/yii2_fecshop
app/appserver/modules/Customer/controllers/LoginController.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appserver/modules/Customer/controllers/LoginController.php
BSD-3-Clause
public function actionWxcode() { $wxCode = Yii::$app->request->post('code'); //echo $wxCode; // 通过code 和 微信的一些验证信息,得到微信的信息uid $wxUserInfo = Yii::$service->helper->wx->getUserInfoByCode($wxCode); // 如果通过code获取微信信息(api获取)失败 if (!$wxUserInfo) { // code 获取openid失败 $code = Yii::$service->helper->appserver->account_wx_get_user_info_fail; $data = [ ]; $responseData = Yii::$service->helper->appserver->getResponseData($code, $data); return $responseData; } $session_key = $wxUserInfo['session_key']; $openid = $wxUserInfo['openid']; // 通过 $openid 得到 user $customer = Yii::$service->customer->getByWxMicroOpenid($openid); // 如果$openid 没有 对应的customer,则需要先绑定或者创建相应的账户 if (!$customer) { $code = Yii::$service->helper->appserver->account_wx_get_customer_by_openid_fail; $data = [ ]; $responseData = Yii::$service->helper->appserver->getResponseData($code, $data); return $responseData; } // 如果登陆失败 if (!$this->loginByIdentity($customer, $session_key)) { $code = Yii::$service->helper->appserver->account_wx_user_login_fail; $data = [ ]; $responseData = Yii::$service->helper->appserver->getResponseData($code, $data); return $responseData; } // 登陆成功 更新信息 $identity = Yii::$app->user->identity; $identity->wx_session_key = $session_key; // $identity->wx_openid = $openid; $identity->save(); $code = Yii::$service->helper->appserver->status_success; $data = [ ]; $responseData = Yii::$service->helper->appserver->getResponseData($code, $data); return $responseData; // 然后再数据库中查询,是否有相关数据,如果存在,则进行账户登陆,返回access-token // 如果没有数据,要求用户绑定,将uid保存到session中,前端页面,用户可以绑定已有的账户,或者新的邮箱账户 // 用户提交邮箱和密码,如果是已有账户,那么进行验证,成功后,绑定微信uid,如果不存在,则返回错误 // 如果是新的邮箱和密码,则邮箱验证成功后,直接绑定。 // 绑定成功后,进行账户登陆,返回access-token }
通过code,通过api获取session_key 和 openid 1.通过openid,查询数据库是否存在相应的用户 2.如果不存在则需要微信小程序进行用户绑定,或者新用户注册 3.如果存在用户,则进行登陆
actionWxcode
php
fecshop/yii2_fecshop
app/appserver/modules/Customer/controllers/LoginController.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appserver/modules/Customer/controllers/LoginController.php
BSD-3-Clause
public function actionLoginv() { Yii::$service->session->set('logintype', 'google'); $thirdLogin = Yii::$service->store->thirdLogin; global $googleapiinfo; $googleapiinfo['GOOGLE_CLIENT_ID'] = isset($thirdLogin['google']['CLIENT_ID']) ? $thirdLogin['google']['CLIENT_ID'] : ''; $googleapiinfo['GOOGLE_CLIENT_SECRET'] = isset($thirdLogin['google']['CLIENT_SECRET']) ? $thirdLogin['google']['CLIENT_SECRET'] : ''; $lib_google_base = Yii::getAlias('@fecshop/lib/google'); include $lib_google_base.'/Social.php'; $urlKey = 'customer/google/loginv'; $redirectUrl = Yii::$service->url->getUrl($urlKey); $Social_obj = new \Social($redirectUrl); $user = $Social_obj->google(); // 服务器放到国外才行。不然上面无法返回数据。 if (is_array($user) && !empty($user)) { $fullname = $user['name']; $email = $user['email']; if ($email) { return $this->accountLogin($fullname, $email); } } $code = Yii::$service->helper->appserver->account_google_login_error; $data = []; $responseData = Yii::$service->helper->appserver->getResponseData($code, $data); return $responseData; }
google登录确认成功后,返回的url 通过下面,得到用户的email,first_name,last_name 然后登录。 由于阿里云是国内服务器,暂时还没有具体测试,这个需要 用国外的服务器才可以。因为需要服务器方面访问google的接口。国内服务器会被墙的。
actionLoginv
php
fecshop/yii2_fecshop
app/appserver/modules/Customer/controllers/GoogleController.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appserver/modules/Customer/controllers/GoogleController.php
BSD-3-Clause
public function accountLogin($full_name, $email) { $name_arr = explode(' ', $full_name); $first_name = $name_arr[0]; $last_name = $name_arr[1]; $user = [ 'first_name' =>$first_name, 'last_name' =>$last_name, 'email' =>$email, ]; Yii::$service->customer->registerThirdPartyAccountAndLogin($user, 'google'); $code = Yii::$service->helper->appserver->status_success; $data = []; $responseData = Yii::$service->helper->appserver->getResponseData($code, $data); return $responseData; //echo '<script> // window.close(); // window.opener.location.reload(); // </script>'; //exit; }
google账户登录.
accountLogin
php
fecshop/yii2_fecshop
app/appserver/modules/Customer/controllers/GoogleController.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appserver/modules/Customer/controllers/GoogleController.php
BSD-3-Clause
public function actionIndex(){ if(Yii::$app->request->getMethod() === 'OPTIONS'){ return []; } $identity = Yii::$service->customer->loginByAccessToken(get_class($this)); $customer_name = ''; $customer_email= ''; if($identity){ $customer_name = $identity['firstname'].' '.$identity['lastname']; $customer_email= $identity['email']; } $contactsEmail = ''; //$contactsCaptcha = false; //$contacts = Yii::$app->getModule('customer')->params['contacts']; $appName = Yii::$service->helper->getAppName(); $contactsCaptcha = Yii::$app->store->get($appName.'_account', 'contactsCaptcha'); $contactsCaptcha = ($contactsCaptcha == Yii::$app->store->enable) ? true : false; if (isset($contacts['email']['address'])) { $contactsEmail = $contacts['email']['address']; } if (!$contactsEmail) { $contactsEmail = Yii::$service->email->contactsEmailAddress(); } $code = Yii::$service->helper->appserver->status_success; $data = [ 'customer_name' => $customer_name, 'customer_email' => $customer_email, 'contactsCaptchaActive' => $contactsCaptcha, 'contactsEmail' => $contactsEmail, ]; $responseData = Yii::$service->helper->appserver->getResponseData($code, $data); return $responseData; }
¼ûIJ
actionIndex
php
fecshop/yii2_fecshop
app/appserver/modules/Customer/controllers/ContactController.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appserver/modules/Customer/controllers/ContactController.php
BSD-3-Clause
public function actionLoginv() { //Yii::$service->session->set('fbs', 1); $thirdLogin = Yii::$service->store->thirdLogin; $facebook_app_id = isset($thirdLogin['facebook']['facebook_app_id']) ? $thirdLogin['facebook']['facebook_app_id'] : ''; $facebook_app_secret = isset($thirdLogin['facebook']['facebook_app_secret']) ? $thirdLogin['facebook']['facebook_app_secret'] : ''; $fb = new \Facebook\Facebook([ 'app_id' => $facebook_app_id, 'app_secret' => $facebook_app_secret, 'default_graph_version' => 'v2.10', ]); $helper = $fb->getRedirectLoginHelper(); if (isset($_GET['state'])) { $helper->getPersistentDataHandler()->set('state', $_GET['state']); } try { $accessToken = $helper->getAccessToken(); } catch(\Facebook\Exceptions\FacebookResponseException $e) { // When Graph returns an error $content = 'Graph returned an error: ' . $e->getMessage(); } catch(\Facebook\Exceptions\FacebookSDKException $e) { // When validation fails or other local issues $content = 'Facebook SDK returned an error: ' . $e->getMessage(); } if(!$content){ $code = Yii::$service->helper->appserver->account_facebook_login_error; $data = [ 'content' => $content, ]; $responseData = Yii::$service->helper->appserver->getResponseData($code, $data); return $responseData; } if (! isset($accessToken)) { if ($helper->getError()) { //header('HTTP/1.0 401 Unauthorized'); $content = "Error: " . $helper->getError() . "\n"; $content .= "Error Code: " . $helper->getErrorCode() . "\n"; $content .= "Error Reason: " . $helper->getErrorReason() . "\n"; $content .= "Error Description: " . $helper->getErrorDescription() . "\n"; } else { //header('HTTP/1.0 400 Bad Request'); $content .= 'Bad request'; } } if(!$content){ $code = Yii::$service->helper->appserver->account_facebook_login_error; $data = [ 'content' => $content, ]; $responseData = Yii::$service->helper->appserver->getResponseData($code, $data); return $responseData; } $fb->setDefaultAccessToken($accessToken->getValue()); $response = $fb->get('/me?locale=en_US&fields=name,email'); $userNode = $response->getGraphUser(); $email = $userNode->getField('email'); $name = $userNode['name']; $fbid = $userNode['id']; //echo $email.$name.$fbid;exit; if ($email) { $this->accountLogin($fbid,$name,$email); $code = Yii::$service->helper->appserver->status_success; $data = []; $responseData = Yii::$service->helper->appserver->getResponseData($code, $data); return $responseData; } else { $code = Yii::$service->helper->appserver->account_facebook_login_error; $data = [ 'content' => 'no email find from fb', ]; $responseData = Yii::$service->helper->appserver->getResponseData($code, $data); return $responseData; //$loginUrl = $helper->getLoginUrl(); //header('Location: '.$loginUrl); } }
facebook 账号在facebook确认后,返回网站的url地址。
actionLoginv
php
fecshop/yii2_fecshop
app/appserver/modules/Customer/controllers/FacebookController.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appserver/modules/Customer/controllers/FacebookController.php
BSD-3-Clause
public function accountLogin($fbid,$name,$email) { $name_arr = explode(' ', $name); $first_name = $name_arr[0]; $last_name = $name_arr[1]; $user = [ 'first_name' =>$first_name, 'last_name' =>$last_name, 'email' =>$email, ]; Yii::$service->customer->registerThirdPartyAccountAndLogin($user, 'facebook'); return true; //echo '<script> // window.close(); // window.opener.location.reload(); // </script>'; //exit; }
facebook账户登录
accountLogin
php
fecshop/yii2_fecshop
app/appserver/modules/Customer/controllers/FacebookController.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appserver/modules/Customer/controllers/FacebookController.php
BSD-3-Clause
public function actionLogout() { if(Yii::$app->request->getMethod() === 'OPTIONS'){ return []; } if (Yii::$app->user->isGuest) { $code = Yii::$service->helper->appserver->account_no_login_or_login_token_timeout; $data = []; $responseData = Yii::$service->helper->appserver->getResponseData($code, $data); return $responseData; } Yii::$service->customer->logoutByAccessToken(); //Yii::$service->cart->clearCart(); $code = Yii::$service->helper->appserver->status_success; $data = []; $responseData = Yii::$service->helper->appserver->getResponseData($code, $data); return $responseData; }
登出账户.
actionLogout
php
fecshop/yii2_fecshop
app/appserver/modules/Customer/controllers/AccountController.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appserver/modules/Customer/controllers/AccountController.php
BSD-3-Clause
public function actionRemove() { if(Yii::$app->request->getMethod() === 'OPTIONS'){ return []; } $favorite_id = Yii::$app->request->post('favorite_id'); if($favorite_id){ Yii::$service->product->favorite->currentUserRemove($favorite_id); $code = Yii::$service->helper->appserver->status_success; $data = []; $responseData = Yii::$service->helper->appserver->getResponseData($code, $data); return $responseData; } else { $code = Yii::$service->helper->appserver->account_favorite_id_not_exist; $data = []; $responseData = Yii::$service->helper->appserver->getResponseData($code, $data); return $responseData; } }
@param $favorite_id|string
actionRemove
php
fecshop/yii2_fecshop
app/appserver/modules/Customer/controllers/ProductfavoriteController.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appserver/modules/Customer/controllers/ProductfavoriteController.php
BSD-3-Clause
public function actionIndex(){ if(Yii::$app->request->getMethod() === 'OPTIONS'){ return []; } $code = Yii::$service->helper->appserver->status_success; $data = [ 'addressList' => $this->coll(), ]; $responseData = Yii::$service->helper->appserver->getResponseData($code, $data); return $responseData; }
登录用户的部分
actionIndex
php
fecshop/yii2_fecshop
app/appserver/modules/Customer/controllers/AddressController.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appserver/modules/Customer/controllers/AddressController.php
BSD-3-Clause
public function actionChangedefault() { $address_id = Yii::$app->request->post('address_id'); $identity = Yii::$app->user->identity; $customerId = $identity->id; $setStatus = Yii::$service->customer->address->setDefault($customerId, $address_id); if (!$setStatus) { $code = Yii::$service->helper->appserver->account_address_set_default_fail; $data = [ ]; $responseData = Yii::$service->helper->appserver->getResponseData($code, $data); return $responseData; } $code = Yii::$service->helper->appserver->status_success; $data = [ ]; $responseData = Yii::$service->helper->appserver->getResponseData($code, $data); return $responseData; }
设置默认地址
actionChangedefault
php
fecshop/yii2_fecshop
app/appserver/modules/Customer/controllers/AddressController.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appserver/modules/Customer/controllers/AddressController.php
BSD-3-Clause
public function sendRegisterEmail($param) { if ($param) { if (Yii::$service->email->customer->registerAccountIsNeedEnableByEmail) { $registerEnableToken = Yii::$service->customer->generateRegisterEnableToken($param['email']); if ($registerEnableToken) { $param['register_enable_token'] = $registerEnableToken; Yii::$service->email->customer->sendRegisterEmail($param); return true; } } else { Yii::$service->email->customer->sendRegisterEmail($param); return true; } } }
发送登录邮件.
sendRegisterEmail
php
fecshop/yii2_fecshop
app/appserver/modules/Customer/controllers/RegisterController.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appserver/modules/Customer/controllers/RegisterController.php
BSD-3-Clause
public function actionIndex(){ if(Yii::$app->request->getMethod() === 'OPTIONS'){ return []; } $identity = Yii::$service->customer->loginByAccessToken(get_class($this)); if($identity){ // 用户已经登录 $code = Yii::$service->helper->appserver->account_is_logined; $data = []; $responseData = Yii::$service->helper->appserver->getResponseData($code, $data); return $responseData; } //$registerParam = \Yii::$app->getModule('customer')->params['register']; //$registerPageCaptcha = isset($registerParam['registerPageCaptcha']) ? $registerParam['registerPageCaptcha'] : false; $appName = Yii::$service->helper->getAppName(); $registerPageCaptcha = Yii::$app->store->get($appName.'_account', 'registerSuccessAutoLogin'); $registerPageCaptcha = ($registerPageCaptcha == Yii::$app->store->enable) ? true : false; //$registerParam = \Yii::$app->getModule('customer')->params['register']; //$registerPageCaptcha = isset($registerParam['registerPageCaptcha']) ? $registerParam['registerPageCaptcha'] : false; $code = Yii::$service->helper->appserver->status_success; $data = [ 'minNameLength' => Yii::$service->customer->getRegisterNameMinLength(), 'maxNameLength' => Yii::$service->customer->getRegisterNameMaxLength(), 'minPassLength' => Yii::$service->customer->getRegisterPassMinLength(), 'maxPassLength' => Yii::$service->customer->getRegisterPassMaxLength(), 'registerCaptchaActive' => $registerPageCaptcha, ]; $responseData = Yii::$service->helper->appserver->getResponseData($code, $data); return $responseData; }
register页面
actionIndex
php
fecshop/yii2_fecshop
app/appserver/modules/Customer/controllers/RegisterController.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appserver/modules/Customer/controllers/RegisterController.php
BSD-3-Clause
public function actionIndex(){ if(Yii::$app->request->getMethod() === 'OPTIONS'){ return []; } $identity = Yii::$app->user->identity; if(isset($identity['email'])){ $code = Yii::$service->helper->appserver->status_success; $data = [ 'email' => $identity['email'], 'firstname' => $identity['firstname'], 'lastname' => $identity['lastname'], 'minNameLength' => Yii::$service->customer->getRegisterNameMinLength(), 'maxNameLength' => Yii::$service->customer->getRegisterNameMaxLength(), 'minPassLength' => Yii::$service->customer->getRegisterPassMinLength(), 'maxPassLength' => Yii::$service->customer->getRegisterPassMaxLength(), ]; $responseData = Yii::$service->helper->appserver->getResponseData($code, $data); return $responseData; } }
登录用户的部分
actionIndex
php
fecshop/yii2_fecshop
app/appserver/modules/Customer/controllers/EditaccountController.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appserver/modules/Customer/controllers/EditaccountController.php
BSD-3-Clause
protected function sendForgotPasswordEmail($identity) { if(Yii::$app->request->getMethod() === 'OPTIONS'){ return []; } if ($identity) { Yii::$service->email->customer->sendForgotPasswordEmail($identity); } }
发送忘记密码邮件.
sendForgotPasswordEmail
php
fecshop/yii2_fecshop
app/appserver/modules/Customer/controllers/ForgotController.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appserver/modules/Customer/controllers/ForgotController.php
BSD-3-Clause
public function actionIndex() { $url_key = Yii::$app->request->get('url_key'); $article = Yii::$service->cms->article->getByUrlKey($url_key); if ($article) { $data = [ 'content' => Yii::$service->store->getStoreAttrVal($article['content'], 'content'), 'created_at' => $article['created_at'], 'title' => Yii::$service->store->getStoreAttrVal($article['title'], 'title'), ]; $code = Yii::$service->helper->appserver->status_success; $responseData = Yii::$service->helper->appserver->getResponseData($code, $data); return $responseData; } else { $code = Yii::$service->helper->appserver->cms_article_not_exist; $data = []; $responseData = Yii::$service->helper->appserver->getResponseData($code, $data); return $responseData; } }
网站信息管理
actionIndex
php
fecshop/yii2_fecshop
app/appserver/modules/Cms/controllers/ArticleController.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appserver/modules/Cms/controllers/ArticleController.php
BSD-3-Clause
public function getLang() { $langs = Yii::$service->store->serverLangs; $currentLangCode = Yii::$service->store->currentLangCode; return [ 'langList' => $langs, 'currentLang' => $currentLangCode ]; }
语言
getLang
php
fecshop/yii2_fecshop
app/appserver/modules/Cms/controllers/HomeController.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appserver/modules/Cms/controllers/HomeController.php
BSD-3-Clause
public function getCurrency() { $currencys = Yii::$service->page->currency->getCurrencys(); $currentCurrencyCode = Yii::$service->page->currency->getCurrentCurrency(); return [ 'currencyList' => $currencys, 'currentCurrency' => $currentCurrencyCode ]; }
货币
getCurrency
php
fecshop/yii2_fecshop
app/appserver/modules/Cms/controllers/HomeController.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appserver/modules/Cms/controllers/HomeController.php
BSD-3-Clause
public function doCheckoutPayment($token) { $methodName_ = 'DoExpressCheckoutPayment'; $nvpStr_ = Yii::$service->payment->paypal->getCheckoutPaymentNvpStr($token); //echo $nvpStr_;exit; $doCheckoutReturn = Yii::$service->payment->paypal->PPHttpPost5($methodName_, $nvpStr_); //var_dump($doCheckoutReturn); //exit; if (strstr(strtolower($doCheckoutReturn['ACK']), 'success')) { return $doCheckoutReturn; } else { if ($doCheckoutReturn['ACK'] == 'Failure') { $message = $doCheckoutReturn['L_LONGMESSAGE0']; // 添加报错信息。 //Message::error($message); Yii::$service->helper->errors->add($message); } else { Yii::$service->helper->errors->add('paypal express payment error.'); } return false; } }
@param $token | String 通过paypal的api接口,进行支付下单
doCheckoutPayment
php
fecshop/yii2_fecshop
app/appserver/modules/Payment/block/paypal/express/Placeorder.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appserver/modules/Payment/block/paypal/express/Placeorder.php
BSD-3-Clause
public function updateAddress($post) { return Yii::$service->cart->updateGuestCart($this->_billing, $this->_shipping_method, $this->_payment_method); }
@param $post | Array 登录用户,保存货运地址到customer address ,然后把生成的 address_id 写入到cart中。 shipping method写入到cart中 payment method 写入到cart中 updateCart
updateAddress
php
fecshop/yii2_fecshop
app/appserver/modules/Payment/block/paypal/express/Placeorder.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appserver/modules/Payment/block/paypal/express/Placeorder.php
BSD-3-Clause
public function checkOrderInfoAndInit($post) { $address_one = ''; $billing = isset($post['billing']) ? $post['billing'] : ''; if (!Yii::$service->order->checkRequiredAddressAttr($billing)) { $code = Yii::$service->helper->appserver->order_generate_request_post_param_invaild; $data = [ 'error' => Yii::$service->helper->errors->get(','), ]; $responseData = Yii::$service->helper->appserver->getResponseData($code, $data); return $responseData; } $this->_billing = $billing; $shipping_method = isset($post['shipping_method']) ? $post['shipping_method'] : ''; $payment_method = isset($post['payment_method']) ? $post['payment_method'] : ''; // 验证货运方式 if (!$shipping_method) { $code = Yii::$service->helper->appserver->order_generate_request_post_param_invaild; $data = [ 'error' => 'shipping method can not empty', ]; $responseData = Yii::$service->helper->appserver->getResponseData($code, $data); return $responseData; } else { //if (!Yii::$service->shipping->ifIsCorrect($shipping_method)) { // $code = Yii::$service->helper->appserver->order_generate_request_post_param_invaild; // $data = [ // 'error' => 'shipping method is not correct', // ]; // $responseData = Yii::$service->helper->appserver->getResponseData($code, $data); // // return $responseData; //} } // 订单备注信息不能超过1500字符 $orderRemarkStrMaxLen = Yii::$service->order->orderRemarkStrMaxLen; $order_remark = isset($post['order_remark']) ? $post['order_remark'] : ''; if ($order_remark && $orderRemarkStrMaxLen) { $order_remark_strlen = strlen($order_remark); if ($order_remark_strlen > $orderRemarkStrMaxLen) { Yii::$service->helper->errors->add('order remark string length can not gt {orderRemarkStrMaxLen}', ['orderRemarkStrMaxLen' => $orderRemarkStrMaxLen]); return false; } else { // 去掉xss攻击字符,关于防止xss攻击的yii文档参看:http://www.yiichina.com/doc/guide/2.0/security-best-practices#fang-zhi-xss-gong-ji $this->_order_remark = $order_remark; } } $this->_shipping_method = $shipping_method; $this->_payment_method = $payment_method; Yii::$service->payment->setPaymentMethod($this->_payment_method); return true; }
@param $post | Array @return bool 检查前台传递的信息是否正确。同时初始化一部分类变量
checkOrderInfoAndInit
php
fecshop/yii2_fecshop
app/appserver/modules/Payment/block/paypal/express/Placeorder.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appserver/modules/Payment/block/paypal/express/Placeorder.php
BSD-3-Clause
public function initAddress() { $this->_address_list = Yii::$service->customer->address->currentAddressList(); if (is_array($this->_address_list) && !empty($this->_address_list)) { // 用户存在地址列表,但是,cart中没有customer_address_id // 这种情况下,从用户地址列表中取出来默认地址,然后设置成当前的地址。 foreach ($this->_address_list as $adss_id => $info) { if ($info['is_default'] == 1) { $this->_address_id = $adss_id; //$this->_address_view_file = 'checkout/onepage/index/address_select.php'; $addressModel = Yii::$service->customer->address->getByPrimaryKey($this->_address_id); if ($addressModel['country']) { $this->_country = $addressModel['country']; $this->_address['country'] = $this->_country; } if ($addressModel['state']) { $this->_state = $addressModel['state']; $this->_address['state'] = $this->_state; } if ($addressModel['first_name']) { $this->_address['first_name'] = $addressModel['first_name']; } if ($addressModel['last_name']) { $this->_address['last_name'] = $addressModel['last_name']; } if ($addressModel['email']) { $this->_address['email'] = $addressModel['email']; } if ($addressModel['telephone']) { $this->_address['telephone'] = $addressModel['telephone']; } if ($addressModel['street1']) { $this->_address['street1'] = $addressModel['street1']; } if ($addressModel['street2']) { $this->_address['street2'] = $addressModel['street2']; } if ($addressModel['city']) { $this->_address['city'] = $addressModel['city']; } if ($addressModel['zip']) { $this->_address['zip'] = $addressModel['zip']; } break; } } } else { $cart = Yii::$service->cart->quote->getCart(); $address_info = []; if (!Yii::$app->user->isGuest) { $identity = Yii::$app->user->identity; $address_info['email'] = $identity['email']; $address_info['first_name'] = $identity['firstname']; $address_info['last_name'] = $identity['lastname']; } if (isset($cart['customer_email']) && !empty($cart['customer_email'])) { $address_info['email'] = $cart['customer_email']; } if (isset($cart['customer_firstname']) && !empty($cart['customer_firstname'])) { $address_info['first_name'] = $cart['customer_firstname']; } if (isset($cart['customer_lastname']) && !empty($cart['customer_lastname'])) { $address_info['last_name'] = $cart['customer_lastname']; } if (isset($cart['customer_telephone']) && !empty($cart['customer_telephone'])) { $address_info['telephone'] = $cart['customer_telephone']; } if (isset($cart['customer_address_country']) && !empty($cart['customer_address_country'])) { $address_info['country'] = $cart['customer_address_country']; $this->_country = $address_info['country']; } if (isset($cart['customer_address_state']) && !empty($cart['customer_address_state'])) { $address_info['state'] = $cart['customer_address_state']; } if (isset($cart['customer_address_city']) && !empty($cart['customer_address_city'])) { $address_info['city'] = $cart['customer_address_city']; } if (isset($cart['customer_address_zip']) && !empty($cart['customer_address_zip'])) { $address_info['zip'] = $cart['customer_address_zip']; } if (isset($cart['customer_address_street1']) && !empty($cart['customer_address_street1'])) { $address_info['street1'] = $cart['customer_address_street1']; } if (isset($cart['customer_address_street2']) && !empty($cart['customer_address_street2'])) { $address_info['street2'] = $cart['customer_address_street2']; } $this->_address = $address_info; } if (!$this->_country) { $this->_country = Yii::$service->helper->country->getDefaultCountry(); $this->_address['country'] = $this->_country; } }
初始化地址信息,首先从当前用户里面取值,然后从cart表中取数据覆盖 1. 初始化 $this->_address,里面保存的各个地址信息。 2. 如果是登录用户,而且.
initAddress
php
fecshop/yii2_fecshop
app/appserver/modules/Payment/block/paypal/express/Review.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appserver/modules/Payment/block/paypal/express/Review.php
BSD-3-Clause
public function initCountry() { $this->_countrySelect = Yii::$service->helper->country->getAllCountryArray(); }
初始化国家下拉条。
initCountry
php
fecshop/yii2_fecshop
app/appserver/modules/Payment/block/paypal/express/Review.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appserver/modules/Payment/block/paypal/express/Review.php
BSD-3-Clause
public function initState($country = '') { $state = isset($this->_address['state']) ? $this->_address['state'] : ''; if (!$country) { $country = $this->_country; } $stateArr = Yii::$service->helper->country->getStateByContryCode($country, $state); if(empty($stateArr)){ $stateArr = ''; } $this->stateArr = $stateArr; //if (!$stateArr) { // $stateHtml = '<input id="state" name="billing[state]" value="'.$state.'" title="State" class="address_state input-text" style="" type="text">'; //} else { // $stateHtml = '<select id="address:state" class="address_state validate-select" title="State" name="billing[state]"> // <option value="">Please select region, state or province</option>' // .$stateHtml.'</select>'; //} //$this->_stateHtml = $stateHtml; }
初始化省市
initState
php
fecshop/yii2_fecshop
app/appserver/modules/Payment/block/paypal/express/Review.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appserver/modules/Payment/block/paypal/express/Review.php
BSD-3-Clause
public function ajaxChangecountry() { $country = Yii::$app->request->get('country'); $country = \Yii::$service->helper->htmlEncode($country); $state = $this->initState($country); return [ 'code' => 200, 'stateArr' => $this->stateArr, ]; }
当改变国家的时候,ajax获取省市信息.
ajaxChangecountry
php
fecshop/yii2_fecshop
app/appserver/modules/Payment/block/paypal/express/Review.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appserver/modules/Payment/block/paypal/express/Review.php
BSD-3-Clause
public function getProductOptions($product_one, $custom_option_sku) { $custom_option_info_arr = []; $custom_option = isset($product_one['custom_option']) ? $product_one['custom_option'] : ''; $custom_option_sku = $product_one['custom_option_sku']; if (isset($custom_option[$custom_option_sku]) && !empty($custom_option[$custom_option_sku])) { $custom_option_info = $custom_option[$custom_option_sku]; foreach ($custom_option_info as $attr=>$val) { if (!in_array($attr, ['qty', 'sku', 'price', 'image'])) { $attr = str_replace('_', ' ', $attr); $attr = ucfirst($attr); $custom_option_info_arr[$attr] = $val; } } } $spu_options = $product_one['spu_options']; if (is_array($spu_options) && !empty($spu_options)) { foreach ($spu_options as $label => $val) { $custom_option_info_arr[$label] = $val; } } return $custom_option_info_arr; }
将产品页面选择的颜色尺码等显示出来,包括custom option 和spu options部分的数据.
getProductOptions
php
fecshop/yii2_fecshop
app/appserver/modules/Payment/block/paypal/express/Review.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appserver/modules/Payment/block/paypal/express/Review.php
BSD-3-Clause
public function getShippings($custom_shipping_method = '') { $country = $this->_country; if (!$this->_state) { $region = '*'; } else { $region = $this->_state; } $cartProductInfo = Yii::$service->cart->quoteItem->getCartProductInfo(); $product_weight = $cartProductInfo['product_weight']; $product_volume_weight = $cartProductInfo['product_volume_weight']; $product_final_weight = max($product_weight, $product_volume_weight); $cartShippingMethod = $this->_cart_info['shipping_method']; // 当前的货运方式 $current_shipping_method = Yii::$service->shipping->getCurrentShippingMethod($custom_shipping_method, $cartShippingMethod, $country, $region, $product_final_weight); $this->_shipping_method = $current_shipping_method; // 得到所有,有效的shipping method $shippingArr = $this->getShippingArr($product_final_weight, $current_shipping_method, $country, $region = '*'); return $shippingArr; }
@param $current_shipping_method | String 当前选择的货运方式 @return Array,数据格式为: [ 'method'=> $method, 'label' => $label, 'name' => $name, 'cost' => $symbol.$currentCurrencyCost, 'check' => $check, 'shipping_i' => $shipping_i, ] 根据选择的货运方式,得到费用等信息。
getShippings
php
fecshop/yii2_fecshop
app/appserver/modules/Payment/block/paypal/express/Review.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appserver/modules/Payment/block/paypal/express/Review.php
BSD-3-Clause
public function getPayment() { $paymentArr = Yii::$service->payment->getStandardPaymentArr(); $pArr = []; if (!$this->_payment_method) { if (isset($this->_cart_info['payment_method']) && !empty($this->_cart_info['payment_method'])) { $this->_payment_method = $this->_cart_info['payment_method']; } //echo $this->_payment_method; if (!$this->_payment_method) { $i = 0; foreach ($paymentArr as $k => $v) { $i++; if ($i == 1) { $this->_payment_method = $k; $v['checked'] = true; } $pArr[$k] = $v; } } else { foreach ($paymentArr as $k => $v) { if ($this->_payment_method == $k) { $v['checked'] = true; } $pArr[$k] = $v; } //var_dump($paymentArr); } } return $pArr; }
@return 得到所有的支付方式 在获取的同时,判断$this->_payment_method 是否存在,不存在则取 第一个支付方式,作为$this->_payment_method的值。
getPayment
php
fecshop/yii2_fecshop
app/appserver/modules/Payment/block/paypal/express/Review.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appserver/modules/Payment/block/paypal/express/Review.php
BSD-3-Clause
public function setValue($getExpressCheckoutReturn) { if ($getExpressCheckoutReturn['FIRSTNAME']) { $this->_address['first_name'] = $getExpressCheckoutReturn['FIRSTNAME']; } if ($getExpressCheckoutReturn['LASTNAME']) { $this->_address['last_name'] = $getExpressCheckoutReturn['LASTNAME']; } if ($getExpressCheckoutReturn['EMAIL']) { $this->_address['email'] = $getExpressCheckoutReturn['EMAIL']; } if ($getExpressCheckoutReturn['SHIPTOCOUNTRYCODE']) { $this->_address['country'] = $getExpressCheckoutReturn['SHIPTOCOUNTRYCODE']; } if ($getExpressCheckoutReturn['SHIPTOSTATE']) { $this->_address['state'] = $getExpressCheckoutReturn['SHIPTOSTATE']; } if ($getExpressCheckoutReturn['SHIPTOCITY']) { $this->_address['city'] = $getExpressCheckoutReturn['SHIPTOCITY']; } if ($getExpressCheckoutReturn['SHIPTOSTREET']) { $this->_address['street1'] = $getExpressCheckoutReturn['SHIPTOSTREET']; } if ($getExpressCheckoutReturn['SHIPTOSTREET2']) { $this->_address['street2'] = $getExpressCheckoutReturn['SHIPTOSTREET2']; } if ($getExpressCheckoutReturn['SHIPTOZIP']) { $this->_address['zip'] = $getExpressCheckoutReturn['SHIPTOZIP']; } }
初始化信息。
setValue
php
fecshop/yii2_fecshop
app/appserver/modules/Payment/block/paypal/express/Review.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appserver/modules/Payment/block/paypal/express/Review.php
BSD-3-Clause
public function doCheckoutPayment($token) { $methodName_ = 'DoExpressCheckoutPayment'; //echo $token; $nvpStr_ = Yii::$service->payment->paypal->getCheckoutPaymentNvpStr($token); // echo $nvpStr_ ; //echo '<br/>nvpStr_:<br/>"'.$nvpStr_.'<br/><br/>'; $doCheckoutReturn = Yii::$service->payment->paypal->PPHttpPost5($methodName_, $nvpStr_); //echo '<br/>doCheckoutReturn <br/><br/>'; //var_dump($doCheckoutReturn); //echo '<br/>doCheckoutReturn <br/><br/>'; //exit; if (strstr(strtolower($doCheckoutReturn['ACK']), 'success')) { return $doCheckoutReturn; } else { if ($doCheckoutReturn['ACK'] == 'Failure') { $message = $doCheckoutReturn['L_LONGMESSAGE0']; // 添加报错信息。 //Message::error($message); Yii::$service->helper->errors->add($message); } else { Yii::$service->helper->errors->add('paypal express payment error.'); } return false; } }
@param $token | String 通过paypal的api接口,进行支付下单
doCheckoutPayment
php
fecshop/yii2_fecshop
app/appserver/modules/Payment/block/paypal/standard/Placeorder.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appserver/modules/Payment/block/paypal/standard/Placeorder.php
BSD-3-Clause
public function updateAddress($post) { return Yii::$service->cart->updateGuestCart($this->_billing, $this->_shipping_method, $this->_payment_method); }
@param $post | Array 登录用户,保存货运地址到customer address ,然后把生成的 address_id 写入到cart中。 shipping method写入到cart中 payment method 写入到cart中 updateCart
updateAddress
php
fecshop/yii2_fecshop
app/appserver/modules/Payment/block/paypal/standard/Placeorder.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appserver/modules/Payment/block/paypal/standard/Placeorder.php
BSD-3-Clause
public function checkOrderInfoAndInit($post) { $address_one = ''; $billing = isset($post['billing']) ? $post['billing'] : ''; if (!Yii::$service->order->checkRequiredAddressAttr($billing)) { return false; } $this->_billing = $billing; $shipping_method = isset($post['shipping_method']) ? $post['shipping_method'] : ''; $payment_method = isset($post['payment_method']) ? $post['payment_method'] : ''; // 验证货运方式 if (!$shipping_method) { Yii::$service->helper->errors->add('shipping method can not empty'); return false; } else { if (!Yii::$service->shipping->ifIsCorrect($shipping_method)) { Yii::$service->helper->errors->add('shipping method is not correct'); return false; } } $this->_shipping_method = $shipping_method; $this->_payment_method = $payment_method; Yii::$service->payment->setPaymentMethod($this->_payment_method); return true; }
@param $post | Array @return bool 检查前台传递的信息是否正确。同时初始化一部分类变量
checkOrderInfoAndInit
php
fecshop/yii2_fecshop
app/appserver/modules/Payment/block/paypal/standard/Placeorder.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appserver/modules/Payment/block/paypal/standard/Placeorder.php
BSD-3-Clause
public function actionStart() { if(Yii::$app->request->getMethod() === 'OPTIONS'){ return []; } $checkOrder = $this->checkOrder(); if($checkOrder !== true){ return $checkOrder; } $code = Yii::$app->request->post('code'); // 获取相关的code $data = Yii::$service->payment->wxpayJsApi->getScanCodeStart($code); $code = Yii::$service->helper->appserver->status_success; $responseData = Yii::$service->helper->appserver->getResponseData($code, $data); /** * 结果数据格式 * { * "code":200, * "message":"process success", * "data":{ * "jsApiParameters":{ * "appId":"wxb508f3849c440445", * "nonceStr":"8krwuurjmalx8ya8abj17z6rrjbnux2u", * "package":"prepay_id=wx19144638959303d9fc4573021193847200", * "signType":"MD5", * "timeStamp":"1560926798", * "paySign":"52C4BE71A104AF772EACA21DDECED45F" * }, * "editAddress":{ * "addrSign":"cac703ef80de9595064496820ba28b2a9019900b", * "signType":"sha1", * "scope":"jsapi_address", * "appId":"wxb508f3849c440445", * "timeStamp":"1560926798", * "nonceStr":"1234568" * }, * "total_amount":"0.12", * "increment_id":"1100003842" * } * } */ return $responseData; }
通过微信回传的code,进而获取相关的信息
actionStart
php
fecshop/yii2_fecshop
app/appserver/modules/Payment/controllers/WxpayjsapiController.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appserver/modules/Payment/controllers/WxpayjsapiController.php
BSD-3-Clause
public function actionOpenidurl() { $url = Yii::$app->request->post('url'); $openUrl = Yii::$service->payment->wxpayJsApi->getOpenidUrl($url); $code = Yii::$service->helper->appserver->status_success; $data = [ 'openUrl' => $openUrl, ]; $responseData = Yii::$service->helper->appserver->getResponseData($code, $data); return $responseData; }
返回用于获取code的微信url
actionOpenidurl
php
fecshop/yii2_fecshop
app/appserver/modules/Payment/controllers/WxpayjsapiController.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appserver/modules/Payment/controllers/WxpayjsapiController.php
BSD-3-Clause
public function actionIpn() { Yii::$service->payment->wxpay->ipn(); }
IPN消息推送地址 IPN过来后,不清除session中的 increment_id ,也不清除购物车 仅仅是更改订单支付状态。
actionIpn
php
fecshop/yii2_fecshop
app/appserver/modules/Payment/controllers/WxpayjsapiController.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appserver/modules/Payment/controllers/WxpayjsapiController.php
BSD-3-Clause
public function actionStart() { if(Yii::$app->request->getMethod() === 'OPTIONS'){ return []; } $checkOrder = $this->checkOrder(); if($checkOrder !== true){ return $checkOrder; } $return_Url = Yii::$app->request->post('return_url'); //Yii::$service->page->theme->layoutFile = 'wxpay_jsapi.php'; $objectxml = Yii::$service->payment->wxpayH5->getScanCodeStart(); //var_dump($objectxml); //$returnUrl = Yii::$service->payment->getStandardReturnUrl(); $return_Url = urlencode($return_Url); $redirectUrl = $objectxml['mweb_url'] . '&redirect_url=' . $return_Url; $data = [ 'redirectUrl' => $redirectUrl, ]; $code = Yii::$service->helper->appserver->status_success; $responseData = Yii::$service->helper->appserver->getResponseData($code, $data); return $responseData; }
支付开始页面.
actionStart
php
fecshop/yii2_fecshop
app/appserver/modules/Payment/controllers/Wxpayh5Controller.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appserver/modules/Payment/controllers/Wxpayh5Controller.php
BSD-3-Clause
public function actionIpn() { Yii::$service->payment->wxpay->ipn(); }
IPN消息推送地址 IPN过来后,不清除session中的 increment_id ,也不清除购物车 仅仅是更改订单支付状态。
actionIpn
php
fecshop/yii2_fecshop
app/appserver/modules/Payment/controllers/Wxpayh5Controller.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appserver/modules/Payment/controllers/Wxpayh5Controller.php
BSD-3-Clause
public function actionSuccess() { $data = [ 'increment_id' => $this->_increment_id, ]; // 清理购物车中的产品。(游客用户的购物车在成功页面清空) if (Yii::$app->user->isGuest) { Yii::$service->cart->clearCartProductAndCoupon(); } // 清理session中的当前的increment_id Yii::$service->order->removeSessionIncrementId(); return $this->render('../../payment/checkmoney/success', $data); }
废弃 成功支付页面.
actionSuccess
php
fecshop/yii2_fecshop
app/appserver/modules/Payment/controllers/Wxpayh5Controller.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appserver/modules/Payment/controllers/Wxpayh5Controller.php
BSD-3-Clause
public function actionStart() { if(Yii::$app->request->getMethod() === 'OPTIONS'){ return []; } $checkOrder = $this->checkOrder(); if($checkOrder !== true){ return $checkOrder; } $payment_method = isset($this->_order_model['payment_method']) ? $this->_order_model['payment_method'] : ''; if ($payment_method) { // 清空购物车 Yii::$service->cart->clearCartProductAndCoupon(); $code = Yii::$service->helper->appserver->status_success; $data = []; $responseData = Yii::$service->helper->appserver->getResponseData($code, $data); return $responseData; } }
支付开始页面.
actionStart
php
fecshop/yii2_fecshop
app/appserver/modules/Payment/controllers/CheckmoneyController.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appserver/modules/Payment/controllers/CheckmoneyController.php
BSD-3-Clause
public function actionReview() { if(Yii::$app->request->getMethod() === 'OPTIONS'){ return []; } $payment_method = Yii::$service->payment->paypal->express_payment_method; Yii::$service->payment->setPaymentMethod($payment_method); return $this->getBlock()->getLastData(); }
2.Review 从paypal确认后返回
actionReview
php
fecshop/yii2_fecshop
app/appserver/modules/Payment/controllers/paypal/ExpressController.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appserver/modules/Payment/controllers/paypal/ExpressController.php
BSD-3-Clause
public function actionSubmitorder(){ if(Yii::$app->request->getMethod() === 'OPTIONS'){ return []; } $payment_method = Yii::$service->payment->paypal->express_payment_method; Yii::$service->payment->setPaymentMethod($payment_method); return $this->getBlock('placeorder')->getLastData(); }
3. 提交订单
actionSubmitorder
php
fecshop/yii2_fecshop
app/appserver/modules/Payment/controllers/paypal/ExpressController.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appserver/modules/Payment/controllers/paypal/ExpressController.php
BSD-3-Clause
public function actionIpn() { if(Yii::$app->request->getMethod() === 'OPTIONS'){ return []; } \Yii::info('paypal ipn begin express', 'fecshop_debug'); $payment_method = Yii::$service->payment->paypal->express_payment_method; Yii::$service->payment->setPaymentMethod($payment_method); $post = Yii::$app->request->post(); if (is_array($post) && !empty($post)) { $post = \Yii::$service->helper->htmlEncode($post); ob_start(); ob_implicit_flush(false); var_dump($post); $post_log = ob_get_clean(); \Yii::info($post_log, 'fecshop_debug'); Yii::$service->payment->paypal->receiveIpn($post); } }
IPN已经关掉
actionIpn
php
fecshop/yii2_fecshop
app/appserver/modules/Payment/controllers/paypal/ExpressController.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appserver/modules/Payment/controllers/paypal/ExpressController.php
BSD-3-Clause
public function actionStart() { if(Yii::$app->request->getMethod() === 'OPTIONS'){ return []; } $payment_method = Yii::$service->payment->paypal->standard_payment_method; Yii::$service->payment->setPaymentMethod($payment_method); $increment_id = Yii::$app->request->post('increment_id'); Yii::$service->order->setCurrentOrderIncrementId($increment_id); return $this->getBlock()->startPayment($increment_id); }
1.start部分,跳转到paypal前的部分
actionStart
php
fecshop/yii2_fecshop
app/appserver/modules/Payment/controllers/paypal/StandardController.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appserver/modules/Payment/controllers/paypal/StandardController.php
BSD-3-Clause
public function actionReview() { if(Yii::$app->request->getMethod() === 'OPTIONS'){ return []; } $payment_method = Yii::$service->payment->paypal->standard_payment_method; Yii::$service->payment->setPaymentMethod($payment_method); return $this->getBlock('placeorder')->getLastData(); }
2.Review 从paypal确认后返回的部分
actionReview
php
fecshop/yii2_fecshop
app/appserver/modules/Payment/controllers/paypal/StandardController.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appserver/modules/Payment/controllers/paypal/StandardController.php
BSD-3-Clause
public function actionIpn() { if(Yii::$app->request->getMethod() === 'OPTIONS'){ return []; } \Yii::info('paypal ipn begin standard', 'fecshop_debug'); $payment_method = Yii::$service->payment->paypal->standard_payment_method; Yii::$service->payment->setPaymentMethod($payment_method); $post = Yii::$app->request->post(); if (is_array($post) && !empty($post)) { $post = \Yii::$service->helper->htmlEncode($post); ob_start(); ob_implicit_flush(false); var_dump($post); $post_log = ob_get_clean(); \Yii::info($post_log, 'fecshop_debug'); Yii::$service->payment->paypal->receiveIpn($post); } }
IPN,paypal消息接收部分
actionIpn
php
fecshop/yii2_fecshop
app/appserver/modules/Payment/controllers/paypal/StandardController.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appserver/modules/Payment/controllers/paypal/StandardController.php
BSD-3-Clause
public function actionStart() { if(Yii::$app->request->getMethod() === 'OPTIONS'){ return []; } //$AopSdkFile = Yii::getAlias('@fecshop/lib/alipay/AopSdk.php'); //require($AopSdkFile); //echo '支付宝支付跳转中...'; //Yii::$service->payment->alipay->devide = 'wap'; $return_url = Yii::$app->request->post('return_url'); $increment_id = Yii::$app->request->post('increment_id'); Yii::$service->order->setCurrentOrderIncrementId($increment_id); $code = Yii::$service->helper->appserver->status_success; $data = [ 'redirectUrl' => Yii::$service->payment->alipay->start($return_url,'GET'), ]; $responseData = Yii::$service->helper->appserver->getResponseData($code, $data); return $responseData; }
在网站下单页面,选择支付宝支付方式后, 跳转到支付宝支付页面前准备的部分。
actionStart
php
fecshop/yii2_fecshop
app/appserver/modules/Payment/controllers/alipay/StandardController.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appserver/modules/Payment/controllers/alipay/StandardController.php
BSD-3-Clause
public function actionReview() { $reviewStatus = Yii::$service->payment->alipay->review(); if($reviewStatus){ $code = Yii::$service->helper->appserver->status_success; $data = []; $responseData = Yii::$service->helper->appserver->getResponseData($code, $data); return $responseData; }else{ $code = Yii::$service->helper->appserver->order_alipay_payment_fail; $data = []; $responseData = Yii::$service->helper->appserver->getResponseData($code, $data); return $responseData; } }
从支付宝支付成功后,跳转返回 fec-shop 的部分
actionReview
php
fecshop/yii2_fecshop
app/appserver/modules/Payment/controllers/alipay/StandardController.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appserver/modules/Payment/controllers/alipay/StandardController.php
BSD-3-Clause
public function actionIpn() { \Yii::info('alipay ipn begin', 'fecshop_debug'); $post = Yii::$app->request->post(); if (is_array($post) && !empty($post)) { \Yii::info('', 'fecshop_debug'); $post = \Yii::$service->helper->htmlEncode($post); ob_start(); ob_implicit_flush(false); var_dump($post); $post_log = ob_get_clean(); \Yii::info($post_log, 'fecshop_debug'); $ipnStatus = Yii::$service->payment->alipay->receiveIpn(); if($ipnStatus){ echo 'success'; return; } } }
IPN,支付宝消息接收部分
actionIpn
php
fecshop/yii2_fecshop
app/appserver/modules/Payment/controllers/alipay/StandardController.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appserver/modules/Payment/controllers/alipay/StandardController.php
BSD-3-Clause
public function guestCreateAndLoginAccount($post) { $create_account = $post['create_account']; $billing = $post['billing']; //var_dump($create_account); if ($create_account) { //echo 22; //echo $create_account;exit; $customer_password = $billing['customer_password']; $confirm_password = $billing['confirm_password']; if ($customer_password != $confirm_password) { return 'the passwords are inconsistent'; } $passMin = Yii::$service->customer->getRegisterPassMinLength(); $passMax = Yii::$service->customer->getRegisterPassMaxLength(); if (strlen($customer_password) < $passMin) { return 'password must Greater than '.$passMin; } if (strlen($customer_password) > $passMax) { return 'password must less than '.$passMax; } $param['email'] = $billing['email']; $param['password'] = $billing['customer_password']; $param['firstname'] = $billing['first_name']; $param['lastname'] = $billing['last_name']; if (!Yii::$service->customer->register($param)) { return 'customer register account fail'; } else { Yii::$service->customer->Login([ 'email' => $billing['email'], 'password' => $billing['customer_password'], ]); } } return true; }
@param $post|Array,前台传递参数数组。 如果游客选择了创建账户,并且输入了密码,则使用address email作为账号, 进行账号的注册和登录。
guestCreateAndLoginAccount
php
fecshop/yii2_fecshop
app/appserver/modules/Checkout/block/onepage/Wxplaceorder.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appserver/modules/Checkout/block/onepage/Wxplaceorder.php
BSD-3-Clause
public function updateAddress($post) { if (!Yii::$app->user->isGuest) { $billing = $post['billing']; $address_id = $post['address_id']; if (!$address_id) { $identity = Yii::$app->user->identity; $customer_id = $identity['id']; $one = [ 'first_name' => $billing['first_name'], 'last_name' => $billing['last_name'], 'email' => $billing['email'], 'company' => '', 'telephone' => $billing['telephone'], 'fax' => '', 'street1' => $billing['street1'], 'street2' => $billing['street2'], 'city' => $billing['city'], 'state' => $billing['state'], 'zip' => $billing['zip'], 'country' => $billing['country'], 'customer_id' => $customer_id, 'is_default' => 1, ]; $address_id = Yii::$service->customer->address->save($one); $this->_address_id = $address_id; if (!$address_id) { return 'new customer address save fail'; } //echo "$address_id,$this->_shipping_method,$this->_payment_method"; } Yii::$service->cart->updateLoginCart($this->_address_id, $this->_shipping_method, $this->_payment_method); } else { Yii::$service->cart->updateGuestCart($this->_billing, $this->_shipping_method, $this->_payment_method); } return true; }
@param $post | Array 登录用户,保存货运地址到customer address ,然后把生成的 address_id 写入到cart中。 shipping method写入到cart中 payment method 写入到cart中 updateCart
updateAddress
php
fecshop/yii2_fecshop
app/appserver/modules/Checkout/block/onepage/Wxplaceorder.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appserver/modules/Checkout/block/onepage/Wxplaceorder.php
BSD-3-Clause
public function checkOrderInfoAndInit($post) { $address_one = ''; $address_id = isset($post['address_id']) ? $post['address_id'] : ''; $billing = isset($post['billing']) ? $post['billing'] : ''; if ($address_id) { $this->_address_id = $address_id; if (Yii::$app->user->isGuest) { return 'address id can not use for guest'; //return false; // address_id 这种情况,必须是登录用户。 } else { $customer_id = Yii::$app->user->identity->id; if (!$customer_id) { return 'customer id is empty'; } else { $address_one = Yii::$service->customer->address->getAddressByIdAndCustomerId($address_id, $customer_id); if (!$address_one) { return 'current address id is not belong to current user'; } else { // 从address_id中取出来的字段,查看是否满足必写的要求。 if (!Yii::$service->order->checkRequiredAddressAttr($address_one)) { return 'address info error'; } $arr['customer_id'] = $customer_id; foreach ($address_one as $k=>$v) { $arr[$k] = $v; } $this->_billing = $arr; } } } } elseif ($billing && is_array($billing)) { // 检查address的必写字段是否都存在 //var_dump($billing);exit; if (!Yii::$service->order->checkRequiredAddressAttr($billing)) { return 'address require attr is empty'; } $this->_billing = $billing; } $shipping_method = isset($post['shipping_method']) ? $post['shipping_method'] : ''; $payment_method = isset($post['payment_method']) ? $post['payment_method'] : ''; // 验证货运方式 if (!$shipping_method) { return 'shipping method can not empty'; } else { // if (!Yii::$service->shipping->ifIsCorrect($shipping_method)) { // return 'shipping method is not correct'; // } } // 验证支付方式 if (!$payment_method) { return 'payment method can not empty'; } else { if (!Yii::$service->payment->ifIsCorrectStandard($payment_method)) { return 'payment method is not correct'; } } // 增加event $beforeEventName = 'event_place_order_check_order'; Yii::$service->event->trigger($beforeEventName, $post); $eventCheckErr = Yii::$service->event->getErrStr(); // 事件检查结果,是否存在报错,得到报错信息 if ($eventCheckErr) { Yii::$service->helper->errors->add($eventCheckErr); return false; } // 订单备注信息不能超过1500字符 $orderRemarkStrMaxLen = Yii::$service->order->orderRemarkStrMaxLen; $order_remark = isset($post['order_remark']) ? $post['order_remark'] : ''; if ($order_remark && $orderRemarkStrMaxLen) { $order_remark_strlen = strlen($order_remark); if ($order_remark_strlen > $orderRemarkStrMaxLen) { Yii::$service->helper->errors->add('order remark string length can not gt {orderRemarkStrMaxLen}', ['orderRemarkStrMaxLen' => $orderRemarkStrMaxLen]); return false; } else { // 去掉xss攻击字符,关于防止xss攻击的yii文档参看:http://www.yiichina.com/doc/guide/2.0/security-best-practices#fang-zhi-xss-gong-ji $this->_order_remark = $order_remark; } } $this->_shipping_method = $shipping_method; $this->_payment_method = $payment_method; Yii::$service->payment->setPaymentMethod($this->_payment_method); return true; }
@param $post | Array @return bool 检查前台传递的信息是否正确。同时初始化一部分类变量
checkOrderInfoAndInit
php
fecshop/yii2_fecshop
app/appserver/modules/Checkout/block/onepage/Wxplaceorder.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appserver/modules/Checkout/block/onepage/Wxplaceorder.php
BSD-3-Clause
public function guestCreateAndLoginAccount($post) { $create_account = $post['create_account']; $billing = $post['billing']; //var_dump($create_account); if ($create_account) { //echo 22; //echo $create_account;exit; $customer_password = $billing['customer_password']; $confirm_password = $billing['confirm_password']; if ($customer_password != $confirm_password) { return 'the passwords are inconsistent'; } $passMin = Yii::$service->customer->getRegisterPassMinLength(); $passMax = Yii::$service->customer->getRegisterPassMaxLength(); if (strlen($customer_password) < $passMin) { return 'password must Greater than '.$passMin; } if (strlen($customer_password) > $passMax) { return 'password must less than '.$passMax; } $param['email'] = $billing['email']; $param['password'] = $billing['customer_password']; $param['firstname'] = $billing['first_name']; $param['lastname'] = $billing['last_name']; if (!Yii::$service->customer->register($param)) { return 'customer register account fail'; } else { Yii::$service->customer->Login([ 'email' => $billing['email'], 'password' => $billing['customer_password'], ]); } } return true; }
@param $post|Array,前台传递参数数组。 如果游客选择了创建账户,并且输入了密码,则使用address email作为账号, 进行账号的注册和登录。
guestCreateAndLoginAccount
php
fecshop/yii2_fecshop
app/appserver/modules/Checkout/block/onepage/Placeorder.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appserver/modules/Checkout/block/onepage/Placeorder.php
BSD-3-Clause
public function updateAddress($post) { if (!Yii::$app->user->isGuest) { $billing = $post['billing']; $address_id = $post['address_id']; if (!$address_id) { $identity = Yii::$app->user->identity; $customer_id = $identity['id']; $one = [ 'first_name' => $billing['first_name'], 'last_name' => $billing['last_name'], 'email' => $billing['email'], 'company' => '', 'telephone' => $billing['telephone'], 'fax' => '', 'street1' => $billing['street1'], 'street2' => $billing['street2'], 'city' => $billing['city'], 'state' => $billing['state'], 'zip' => $billing['zip'], 'country' => $billing['country'], 'customer_id' => $customer_id, 'is_default' => 1, ]; $address_id = Yii::$service->customer->address->save($one); $this->_address_id = $address_id; if (!$address_id) { return 'new customer address save fail'; } //echo "$address_id,$this->_shipping_method,$this->_payment_method"; } Yii::$service->cart->updateLoginCart($this->_address_id, $this->_shipping_method, $this->_payment_method); } else { Yii::$service->cart->updateGuestCart($this->_billing, $this->_shipping_method, $this->_payment_method); } return true; }
@param $post | Array 登录用户,保存货运地址到customer address ,然后把生成的 address_id 写入到cart中。 shipping method写入到cart中 payment method 写入到cart中 updateCart
updateAddress
php
fecshop/yii2_fecshop
app/appserver/modules/Checkout/block/onepage/Placeorder.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appserver/modules/Checkout/block/onepage/Placeorder.php
BSD-3-Clause
public function checkOrderInfoAndInit($post) { $address_one = ''; $address_id = isset($post['address_id']) ? $post['address_id'] : ''; $billing = isset($post['billing']) ? $post['billing'] : ''; if ($address_id) { $this->_address_id = $address_id; if (Yii::$app->user->isGuest) { return 'address id can not use for guest'; //return false; // address_id 这种情况,必须是登录用户。 } else { $customer_id = Yii::$app->user->identity->id; if (!$customer_id) { return 'customer id is empty'; } else { $address_one = Yii::$service->customer->address->getAddressByIdAndCustomerId($address_id, $customer_id); if (!$address_one) { return 'current address id is not belong to current user'; } else { // 从address_id中取出来的字段,查看是否满足必写的要求。 if (!Yii::$service->order->checkRequiredAddressAttr($address_one)) { return 'address info error'; } $arr['customer_id'] = $customer_id; foreach ($address_one as $k=>$v) { $arr[$k] = $v; } $this->_billing = $arr; } } } } elseif ($billing && is_array($billing)) { // 检查address的必写字段是否都存在 //var_dump($billing);exit; if (!Yii::$service->order->checkRequiredAddressAttr($billing)) { return 'address require attr is empty'; } $this->_billing = $billing; } $shipping_method = isset($post['shipping_method']) ? $post['shipping_method'] : ''; $payment_method = isset($post['payment_method']) ? $post['payment_method'] : ''; // 验证货运方式 if (!$shipping_method) { return 'shipping method can not empty'; } else { // if (!Yii::$service->shipping->ifIsCorrect($shipping_method)) { // return 'shipping method is not correct'; // } } // 验证支付方式 if (!$payment_method) { return 'payment method can not empty'; } else { if (!Yii::$service->payment->ifIsCorrectStandard($payment_method)) { return 'payment method is not correct'; } } // 增加event $beforeEventName = 'event_place_order_check_order'; Yii::$service->event->trigger($beforeEventName, $post); $eventCheckErr = Yii::$service->event->getErrStr(); // 事件检查结果,是否存在报错,得到报错信息 if ($eventCheckErr) { Yii::$service->helper->errors->add($eventCheckErr); return false; } // 订单备注信息不能超过1500字符 $orderRemarkStrMaxLen = Yii::$service->order->orderRemarkStrMaxLen; $order_remark = isset($post['order_remark']) ? $post['order_remark'] : ''; if ($order_remark && $orderRemarkStrMaxLen) { $order_remark_strlen = strlen($order_remark); if ($order_remark_strlen > $orderRemarkStrMaxLen) { Yii::$service->helper->errors->add('order remark string length can not gt {orderRemarkStrMaxLen}', ['orderRemarkStrMaxLen' => $orderRemarkStrMaxLen]); return false; } else { // 去掉xss攻击字符,关于防止xss攻击的yii文档参看:http://www.yiichina.com/doc/guide/2.0/security-best-practices#fang-zhi-xss-gong-ji $this->_order_remark = $order_remark; } } $this->_shipping_method = $shipping_method; $this->_payment_method = $payment_method; Yii::$service->payment->setPaymentMethod($this->_payment_method); return true; }
@param $post | Array @return bool 检查前台传递的信息是否正确。同时初始化一部分类变量
checkOrderInfoAndInit
php
fecshop/yii2_fecshop
app/appserver/modules/Checkout/block/onepage/Placeorder.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appserver/modules/Checkout/block/onepage/Placeorder.php
BSD-3-Clause
public function initAddress() { //$this->_cart_address = Yii::$service->cart->quote->getCartAddress(); $cart = Yii::$service->cart->quote->getCart(); $address_id = $cart['customer_address_id']; $address_info = []; if (!Yii::$app->user->isGuest) { $identity = Yii::$app->user->identity; $address_info['email'] = $identity['email']; $address_info['first_name'] = $identity['firstname']; $address_info['last_name'] = $identity['lastname']; } if (isset($cart['customer_email']) && !empty($cart['customer_email'])) { $address_info['email'] = $cart['customer_email']; } if (isset($cart['customer_firstname']) && !empty($cart['customer_firstname'])) { $address_info['first_name'] = $cart['customer_firstname']; } if (isset($cart['customer_lastname']) && !empty($cart['customer_lastname'])) { $address_info['last_name'] = $cart['customer_lastname']; } if (isset($cart['customer_telephone']) && !empty($cart['customer_telephone'])) { $address_info['telephone'] = $cart['customer_telephone']; } if (isset($cart['customer_address_country']) && !empty($cart['customer_address_country'])) { $address_info['country'] = $cart['customer_address_country']; $this->_country = $address_info['country']; } if (isset($cart['customer_address_state']) && !empty($cart['customer_address_state'])) { $address_info['state'] = $cart['customer_address_state']; } if (isset($cart['customer_address_city']) && !empty($cart['customer_address_city'])) { $address_info['city'] = $cart['customer_address_city']; } if (isset($cart['customer_address_zip']) && !empty($cart['customer_address_zip'])) { $address_info['zip'] = $cart['customer_address_zip']; } if (isset($cart['customer_address_street1']) && !empty($cart['customer_address_street1'])) { $address_info['street1'] = $cart['customer_address_street1']; } if (isset($cart['customer_address_street2']) && !empty($cart['customer_address_street2'])) { $address_info['street2'] = $cart['customer_address_street2']; } $this->_address = $address_info; $this->_address_list = Yii::$service->customer->address->currentAddressList(); if(empty($this->_address_list)){ $this->_address_list = ''; } //var_dump($this->_address_list); // 如果购物车存在customer_address_id,而且用户地址中也存在customer_address_id // 则执行if{}内代码。 if ($address_id && isset($this->_address_list[$address_id]) && !empty($this->_address_list[$address_id])) { $this->_address_id = $address_id; //$this->_address_view_file = 'checkout/onepage/index/address_select.php'; $addressModel = Yii::$service->customer->address->getByPrimaryKey($this->_address_id); if ($addressModel['country']) { $this->_country = $addressModel['country']; $this->_address['country'] = $this->_country; } if ($addressModel['state']) { $this->_state = $addressModel['state']; $this->_address['state'] = $this->_state; } if ($addressModel['first_name']) { $this->_address['first_name'] = $addressModel['first_name']; } if ($addressModel['last_name']) { $this->_address['last_name'] = $addressModel['last_name']; } if ($addressModel['email']) { $this->_address['email'] = $addressModel['email']; } if ($addressModel['telephone']) { $this->_address['telephone'] = $addressModel['telephone']; } if ($addressModel['street1']) { $this->_address['street1'] = $addressModel['street1']; } if ($addressModel['street2']) { $this->_address['street2'] = $addressModel['street2']; } if ($addressModel['city']) { $this->_address['city'] = $addressModel['city']; } if ($addressModel['zip']) { $this->_address['zip'] = $addressModel['zip']; } } elseif (is_array($this->_address_list) && !empty($this->_address_list)) { // 用户存在地址列表,但是,cart中没有customer_address_id // 这种情况下,从用户地址列表中取出来默认地址,然后设置成当前的地址。 foreach ($this->_address_list as $adss_id => $info) { if ($info['is_default'] == 1) { $this->_address_id = $adss_id; //$this->_address_view_file = 'checkout/onepage/index/address_select.php'; $addressModel = Yii::$service->customer->address->getByPrimaryKey($this->_address_id); if ($addressModel['country']) { $this->_country = $addressModel['country']; $this->_address['country'] = $this->_country; } if ($addressModel['state']) { $this->_state = $addressModel['state']; $this->_address['state'] = $this->_state; } if ($addressModel['first_name']) { $this->_address['first_name'] = $addressModel['first_name']; } if ($addressModel['last_name']) { $this->_address['last_name'] = $addressModel['last_name']; } if ($addressModel['email']) { $this->_address['email'] = $addressModel['email']; } if ($addressModel['telephone']) { $this->_address['telephone'] = $addressModel['telephone']; } if ($addressModel['street1']) { $this->_address['street1'] = $addressModel['street1']; } if ($addressModel['street2']) { $this->_address['street2'] = $addressModel['street2']; } if ($addressModel['city']) { $this->_address['city'] = $addressModel['city']; } if ($addressModel['zip']) { $this->_address['zip'] = $addressModel['zip']; } break; } } } else { //$this->_address_view_file = 'checkout/onepage/index/address.php'; // 从购物车里面取出来数据。 $_cartAddress //$cart_info = Yii::$service->cart->getCartInfo(); } if (!$this->_country) { $this->_country = Yii::$service->helper->country->getDefaultCountry(); $this->_address['country'] = $this->_country; } }
初始化地址信息,首先从当前用户里面取值,然后从cart表中取数据覆盖 1. 初始化 $this->_address,里面保存的各个地址信息。 2. 如果是登录用户,而且.
initAddress
php
fecshop/yii2_fecshop
app/appserver/modules/Checkout/block/onepage/Index.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appserver/modules/Checkout/block/onepage/Index.php
BSD-3-Clause
public function initCountry() { $this->_countrySelect = Yii::$service->helper->country->getAllCountryArray(); }
初始化国家下拉条。
initCountry
php
fecshop/yii2_fecshop
app/appserver/modules/Checkout/block/onepage/Index.php
https://github.com/fecshop/yii2_fecshop/blob/master/app/appserver/modules/Checkout/block/onepage/Index.php
BSD-3-Clause