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 orderPaymentCompleteEvent($order_increment_id) { if (!$order_increment_id) { Yii::$service->helper->errors->add('order increment id is empty'); return false; } $orderInfo = Yii::$service->order->getOrderInfoByIncrementId($order_increment_id); if (!$orderInfo['increment_id']) { Yii::$service->helper->errors->add('get order by increment_id: {increment_id} fail, order is not exist ', ['increment_id' => $order_increment_id]); return false; } // 追踪信息 Yii::$service->order->sendTracePaymentSuccessOrder($orderInfo); // 发送订单支付成功邮件 Yii::$service->email->order->sendCreateEmail($orderInfo); }
@param $order_increment_id | string,订单编号 increment_id 订单支付成功后,执行的代码,该代码只会在接收到支付成功信息后,才会执行。 在调用该函数前,会对IPN支付成功消息做验证,一次,无论发送多少次ipn消息,该函数只会执行一次。 您可以把订单支付成功需要做的事情都在这个函数里面完成。
orderPaymentCompleteEvent
php
fecshop/yii2_fecshop
services/Order.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Order.php
BSD-3-Clause
public function setSessionIncrementId($increment_id) { Yii::$service->session->set(self::CURRENT_ORDER_INCREAMENT_ID, $increment_id); }
@param $increment_id | String ,order订单号 将生成的订单号写入session
setSessionIncrementId
php
fecshop/yii2_fecshop
services/Order.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Order.php
BSD-3-Clause
public function getSessionIncrementId() { return Yii::$service->session->get(self::CURRENT_ORDER_INCREAMENT_ID); }
从session中取出来订单号.
getSessionIncrementId
php
fecshop/yii2_fecshop
services/Order.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Order.php
BSD-3-Clause
public function updateTokenByIncrementId($increment_id, $token) { $myOrder = Yii::$service->order->getByIncrementId($increment_id); if ($myOrder) { $myOrder->payment_token = $token; $myOrder->save(); } }
@param $increment_id | String 订单号 @param $token | String ,通过api支付的token 通过订单号,更新订单的支付token
updateTokenByIncrementId
php
fecshop/yii2_fecshop
services/Order.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Order.php
BSD-3-Clause
public function getOrderIncrementIdByTxnId($txnId) { $one = $this->_orderModel->findOne(['txn_id' => $txnId ]); if ($one['increment_id']) { return $one['increment_id']; } }
@param $txnId | string, 订单交易号 通过支付渠道交易号,得到订单交易号
getOrderIncrementIdByTxnId
php
fecshop/yii2_fecshop
services/Order.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Order.php
BSD-3-Clause
public function removeSessionIncrementId() { return Yii::$service->session->remove(self::CURRENT_ORDER_INCREAMENT_ID); }
从session中销毁订单号.
removeSessionIncrementId
php
fecshop/yii2_fecshop
services/Order.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Order.php
BSD-3-Clause
protected function generateIncrementIdByOrderId($order_id) { $this->_eventGenerateOrderId = $order_id; $beforeEventName = 'event_generate_order_increment_id_before'; Yii::$service->event->trigger($beforeEventName, $this); // 如果event中进行了订单号的生成,就会将其写入到 $this->_eventGeneratedIncrementId if ($eventGeneratedIncrementId = $this->_eventGeneratedIncrementId) { // 清空类变量,保证每次都是最新生成的订单id。 $this->_eventGeneratedIncrementId = ''; return $eventGeneratedIncrementId; } // 按照订单id进行生成订单编号,常规生成。 $increment_id = (int) $this->increment_id + (int) $order_id; return $increment_id; }
@param int $order_id the order id @return int $increment_id 通过 order_id 生成订单号。
generateIncrementIdByOrderId
php
fecshop/yii2_fecshop
services/Order.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Order.php
BSD-3-Clause
public function getCustomerOrderList($customer_id) { }
get order list by customer account id. @param int $customer_id @deprecated
getCustomerOrderList
php
fecshop/yii2_fecshop
services/Order.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Order.php
BSD-3-Clause
public function orderPaySuccess($order_id) { }
@param int $order_id the order id 订单支付成功后,更改订单的状态为支付成功状态。 @deprecated
orderPaySuccess
php
fecshop/yii2_fecshop
services/Order.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Order.php
BSD-3-Clause
public function cancel($increment_id = '', $customer_id = '') { if (!$increment_id) { $increment_id = $this->getSessionIncrementId(); } if (!$increment_id) { Yii::$service->helper->errors->add('order increment id is empty'); return false; } $order = $this->getByIncrementId($increment_id); if ($customer_id && $order['customer_id'] != $customer_id) { Yii::$service->helper->errors->add('do not have role to cancel this order'); return false; } // 通过updateAll的结果数,来判定是否取消成功。 $updateComules = $this->_orderModel->updateAll( [ 'order_status' => $this->status_canceled, 'updated_at' => time(), ], [ 'and', ['increment_id' => $increment_id], ['<>', 'order_status', $this->status_canceled], ] ); if (empty($updateComules)) { Yii::$service->helper->errors->add('order cancel fail'); return false; } // 返还库存 $order_primary_key = $this->getPrimaryKey(); $product_items = Yii::$service->order->item->getByOrderId($order[$order_primary_key], true); if (!Yii::$service->product->stock->returnQty($product_items)) { return false; } return true; }
@param $increment_id | String @param $customer_id | int,用户id @return bool 取消订单,更新订单的状态为cancel。 并且释放库存给产品
cancel
php
fecshop/yii2_fecshop
services/Order.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Order.php
BSD-3-Clause
public function trackingShip($orderId, $trackingCompanyCode, $trackingNumber, $trackingCompanyName='') { $updateComules = $this->_orderModel->updateAll( [ 'order_status' => $this->status_dispatched, 'tracking_company' => $trackingCompanyCode, 'tracking_company_name' => $trackingCompanyName, 'tracking_number' => $trackingNumber, 'dispatched_at' => time(), ], [ 'and', ['order_id' => $orderId], ['in', 'order_status', [ $this->payment_status_confirmed, $this->status_processing, ]] ] ); if (empty($updateComules)) { Yii::$service->helper->errors->add('order {order_id} ship fail', [ 'order_id' => $orderId]); return false; } return true; }
@property $orderId | int @property $trackingCompanyCode | string @property $trackingNumber | string 订单发货操作
trackingShip
php
fecshop/yii2_fecshop
services/Order.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Order.php
BSD-3-Clause
public function delivery($incrementId, $customerId) { $updateComules = $this->_orderModel->updateAll( [ 'order_status' => $this->status_completed, ], [ 'increment_id' => $incrementId, 'order_status' => $this->status_dispatched, 'customer_id' => $customerId, ] ); if (empty($updateComules)) { Yii::$service->helper->errors->add('customer delivery order fail'); return false; } return true; }
@param $incrementId | string, 订单编号 @param $customerId | int, 用户id 用户确认收货
delivery
php
fecshop/yii2_fecshop
services/Order.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/Order.php
BSD-3-Clause
public function removeByRoleId($role_id){ $this->_roleModel->deleteAll(['role_id' => $role_id]); return true; }
@param $role_id int @return bool 按照$role_id为条件进行删除
removeByRoleId
php
fecshop/yii2_fecshop
services/admin/UserRole.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/admin/UserRole.php
BSD-3-Clause
public function deleteByUserIds($user_ids){ $this->_roleModel->deleteAll(['in', 'user_id', $user_ids]); return true; }
@param $user_id array int @return bool 按照$user_id为条件进行删除
deleteByUserIds
php
fecshop/yii2_fecshop
services/admin/UserRole.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/admin/UserRole.php
BSD-3-Clause
public function getBootUrlKeyIds() { $urlKeyIds = []; $data = $this->_mode->find()->asArray()->where(['in', 'url_key', $this->bootUrlKey])->all(); if (!is_array($data) || empty($data)) { return $urlKeyIds; } foreach ($data as $one) { $urlKeyIds[] = $one['id']; } return $urlKeyIds; }
默认必须添加的url key id
getBootUrlKeyIds
php
fecshop/yii2_fecshop
services/admin/UrlKey.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/admin/UrlKey.php
BSD-3-Clause
public function getUrlKeyAndLabelArr(){ $arr = Yii::$app->cache->get(self::URLKEY_LABEL_ARR); if (!$arr) { $arr = []; $filter = [ 'fetchAll' => true, 'asArray' => true, ]; $data = $this->coll($filter); if (is_array($data['coll'])) { $tags = $this->getTags(false); foreach ($data['coll'] as $one) { $url_key = $one['url_key']; $label = $tags[$one['tag']] .' '. $one['name']; $arr[$url_key] = $label; } } $addArr = $this->getAddUrlKeyAndLabelArr(); if (is_array($addArr)) { $arr = array_merge($arr, $addArr); } Yii::$app->cache->set(self::URLKEY_LABEL_ARR, $arr); } return $arr; }
@return array 得到urlKey 和 label 对应的数组。 这样可以通过url_key得到当前操作的菜单的name
getUrlKeyAndLabelArr
php
fecshop/yii2_fecshop
services/admin/UrlKey.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/admin/UrlKey.php
BSD-3-Clause
public function save($one) { $currentDateTime = \fec\helpers\CDate::getCurrentDateTime(); $primaryVal = isset($one[$this->getPrimaryKey()]) ? $one[$this->getPrimaryKey()] : ''; if (!($this->validateUrlKey($one))) { Yii::$service->helper->errors->add('The url key exists, you must define a unique url key'); return; } if ($primaryVal) { $model = $this->_mode->findOne($primaryVal); if (!$model) { Yii::$service->helper->errors->add('Url key {primaryKey} is not exist', ['primaryKey' => $this->getPrimaryKey()]); return false; } if ($model->can_delete == $this->can_not_delete) { Yii::$service->helper->errors->add('resource(url key) created by system, can not edit and save'); return false; } } else { $model = new $this->_modelName(); $model->created_at = time(); } $model->updated_at = time(); foreach ($this->_lang_attr as $attrName) { if (is_array($one[$attrName]) && !empty($one[$attrName])) { $one[$attrName] = serialize($one[$attrName]); } } $primaryKey = $this->getPrimaryKey(); $model = Yii::$service->helper->ar->save($model, $one); $primaryVal = $model[$primaryKey]; return true; }
@param $one|array save $data to cms model,then,add url rewrite info to system service urlrewrite.
save
php
fecshop/yii2_fecshop
services/admin/UrlKey.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/admin/UrlKey.php
BSD-3-Clause
public function save($one) { $primaryKey = $this->getPrimaryKey(); if ($one[$primaryKey]) { $this->_model = $this->_model->findOne($one[$primaryKey]); } $this->_model->attributes = $one; if ($this->_model->validate()) { $this->_model->save(); return $this->_model[$primaryKey]; } else { $errors = $this->_model->errors; Yii::$service->helper->errors->addByModelErrors($errors); } return null; }
@param $one|array save $data to cms model,then,add url rewrite info to system service urlrewrite. @return mix, int or null
save
php
fecshop/yii2_fecshop
services/admin/Config.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/admin/Config.php
BSD-3-Clause
public function save($one) { $currentDateTime = \fec\helpers\CDate::getCurrentDateTime(); $primaryVal = isset($one[$this->getPrimaryKey()]) ? $one[$this->getPrimaryKey()] : ''; if (!($this->validateUrlKeyRoleId($one))) { Yii::$service->helper->errors->add('The url key && role id exists, you must define a unique url key && role id'); return; } if ($primaryVal) { $model = $this->_model->findOne($primaryVal); if (!$model) { Yii::$service->helper->errors->add('Role Url Key {primaryKey} is not exist', ['primaryKey' => $this->getPrimaryKey()]); return; } } else { $model = new $this->_modelName(); $model->created_at = time(); } $model->updated_at = time(); foreach ($this->_lang_attr as $attrName) { if (is_array($one[$attrName]) && !empty($one[$attrName])) { $one[$attrName] = serialize($one[$attrName]); } } $primaryKey = $this->getPrimaryKey(); $model = Yii::$service->helper->ar->save($model, $one); $primaryVal = $model[$primaryKey]; return true; }
@param $one|array save $data to cms model,then,add url rewrite info to system service urlrewrite.
save
php
fecshop/yii2_fecshop
services/admin/RoleUrlKey.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/admin/RoleUrlKey.php
BSD-3-Clause
public function removeByUrlKeyId($url_key_id){ $this->_model->deleteAll(['url_key_id' => $url_key_id]); return true; }
@param $url_key_id int @return bool 按照$url_key_id为条件进行删除,一般是url_key进行删除操作的时候,删除这里的数据
removeByUrlKeyId
php
fecshop/yii2_fecshop
services/admin/RoleUrlKey.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/admin/RoleUrlKey.php
BSD-3-Clause
public function removeByRoleId($role_id){ $this->_model->deleteAll(['role_id' => $role_id]); return true; }
@param $url_key_id int @return bool 按照$role_id为条件进行删除,一般是role进行删除操作的时候,删除这里的数据
removeByRoleId
php
fecshop/yii2_fecshop
services/admin/RoleUrlKey.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/admin/RoleUrlKey.php
BSD-3-Clause
public function save(){ if (!$this->enableLog) { return false; } $systemLog = $this->_model; $user = Yii::$app->user->identity; if($user){ $url_key = '/' . Yii::$app->controller->module->id . '/' . Yii::$app->controller->id . '/' . Yii::$app->controller->action->id; $username = $user['username']; $person = $user['person']; $currentData = date('Y-m-d H:i:s'); $url = CUrl::getCurrentUrl(); $systemLog->account = $username; $systemLog->person = $person; $systemLog->created_at = $currentData; $systemLog->url = $url; $systemLog->url_key = $url_key; $systemLog->menu = $this->getMenuByUrlKey($url_key); return $systemLog->save(); } }
保存系统日志。
save
php
fecshop/yii2_fecshop
services/admin/SystemLog.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/admin/SystemLog.php
BSD-3-Clause
public function saveRole($one) { $currentDateTime = \fec\helpers\CDate::getCurrentDateTime(); $primaryVal = isset($one[$this->getPrimaryKey()]) ? $one[$this->getPrimaryKey()] : ''; if (!($this->validateRoleName($one))) { Yii::$service->helper->errors->add('The role name exists, you must define a unique role_name'); return null; } if ($primaryVal) { $model = $this->_roleModel->findOne($primaryVal); if (!$model) { Yii::$service->helper->errors->add('role {primaryKey} is not exist', ['primaryKey' => $this->getPrimaryKey()]); return null; } } else { $model = new $this->_roleModelName(); $model->created_at = time(); } $model->updated_at = time(); foreach ($this->_lang_attr as $attrName) { if (is_array($one[$attrName]) && !empty($one[$attrName])) { $one[$attrName] = serialize($one[$attrName]); } } $primaryKey = $this->getPrimaryKey(); $model = Yii::$service->helper->ar->save($model, $one); $primaryVal = $model[$primaryKey]; return $model; }
@param $one|array save $data to cms model,then,add url rewrite info to system service urlrewrite.
saveRole
php
fecshop/yii2_fecshop
services/admin/Role.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/admin/Role.php
BSD-3-Clause
public function saveRoleAndResources($one){ $roleData = []; if (isset($one['role_id'])) { $roleData['role_id'] = $one['role_id']; } if (isset($one['role_name'])) { $roleData['role_name'] = $one['role_name']; } else { Yii::$service->helper->errors->add('role name can not empty'); return false; } if (isset($one['role_description'])) { $roleData['role_description'] = $one['role_description']; } $primaryKey = $this->getPrimaryKey(); $roleModel = $this->saveRole($roleData); if ($roleModel) { $roleId = $roleModel[$primaryKey]; if ($roleId && isset($one['resources'])) { $resources = $one['resources']; if (is_array($resources) && !empty($resources)) { Yii::$service->admin->roleUrlKey->repeatSaveRoleUrlKey($roleId, $resources); return true; } } } Yii::$service->helper->errors->add('save role and resource fail'); return false; }
@param array $one ,example [ 'role_id' => xx, 'role_name' => 'xxxx', 'role_description' => 'xxxx', 'resources' => [3, 5, 76, 876, 999], @return boolean save role info and resources ]
saveRoleAndResources
php
fecshop/yii2_fecshop
services/admin/Role.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/admin/Role.php
BSD-3-Clause
public function getCurrentRoleResources($fromCache=true){ if (!$this->_current_role_resources || !$fromCache) { if (Yii::$app->user->isGuest) { return []; } $user = Yii::$app->user->identity; $userId = $user->Id; // 通过userId得到这个用户所在的用户组 $userRoles = Yii::$service->admin->userRole->coll([ 'where' => [ [ 'user_id' => $userId, ] ], 'fetchAll' => true, ]); $role_ids = []; if (is_array($userRoles['coll']) && !empty($userRoles['coll'])) { foreach ($userRoles['coll'] as $one) { $role_ids[] = $one['role_id']; } } if (empty($role_ids)) { return []; } $this->_current_role_resources = $this->getRoleResourcesByRoleIds($role_ids, $fromCache); } return $this->_current_role_resources; }
* @param $fromCache | boolean, 是否使用缓存? @return array 得到当前用户的可用的resources数组
getCurrentRoleResources
php
fecshop/yii2_fecshop
services/admin/Role.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/admin/Role.php
BSD-3-Clause
public function getRoleResourcesByRoleIds($role_ids, $fromCache=true){ if (empty($role_ids)) { return []; } sort($role_ids); $role_ids_cache_str = self::ADMIN_ROLEIDS_RESOURCES . implode('-', $role_ids); $resources = null; if ($fromCache) { $resources = Yii::$app->cache->get($role_ids_cache_str); } if (!$resources) { // 通过role_ids 得到url_keys $roleUrlKeys = Yii::$service->admin->roleUrlKey->coll([ 'where' => [ ['in', 'role_id', $role_ids] ], 'fetchAll' => true, ]); $roleUrlKeyIds = []; if (is_array($roleUrlKeys['coll']) && !empty($roleUrlKeys['coll'])) { foreach ($roleUrlKeys['coll'] as $one) { if (!isset($roleUrlKeyIds[$one['url_key_id']])) { $roleUrlKeyIds[$one['url_key_id']] = $one['url_key_id']; } } } $urlKeys = Yii::$service->admin->urlKey->coll([ 'where' => [ ['in', 'id', $roleUrlKeyIds] ], 'fetchAll' => true, ]); $urlKeyIds = []; if (is_array($urlKeys['coll']) && !empty($urlKeys['coll'])) { foreach ($urlKeys['coll'] as $one) { if (!isset($urlKeyIds[$one['url_key']])) { $urlKeyIds[$one['url_key']] = $one['url_key']; } } } Yii::$app->cache->set($role_ids_cache_str, $urlKeyIds, $cacheRoleIdsTimeout); return $urlKeyIds; } return $resources; }
@param array $role_ids @param $fromCache | boolean, 是否使用缓存? @return array , 包含url_key_id的数组 通过$role_ids数组,获得相应的所有url_key_id数组
getRoleResourcesByRoleIds
php
fecshop/yii2_fecshop
services/admin/Role.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/admin/Role.php
BSD-3-Clause
public function hasChildRoleUrlKey($nodeL, &$roleUrlKeys) { if (!$this->hasChild($nodeL)) { return false; } $treeArr = $nodeL['child']; if (!is_array($treeArr)) { return false; } foreach ($treeArr as $node) { $url_key = $node["url_key"]; if ($url_key) { // 如果存在有权限的urlKey if (isset($roleUrlKeys[$url_key]) && $roleUrlKeys[$url_key]) { return true; } } else if($this->hasChild($node)) { // 如果子菜单存在有权限的url Key,返回true if ($this->hasChildRoleUrlKey($node, $roleUrlKeys)) { return true; } } } return false; }
@param $nodeL | array, 菜单结点 @param $roleUrlKeys | array, 权限urlKey数组 查看:当前节点的所有的子节点以及子子节点(递归)的urlKey,是否存在于$roleUrlKeys,只要存在一个,就返回true 此函数的作用为:查看当前阶段是否存在有权限的子菜单,如果没有,则返回false,当前菜单也将隐藏
hasChildRoleUrlKey
php
fecshop/yii2_fecshop
services/admin/Menu.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/admin/Menu.php
BSD-3-Clause
public function getPrimaryKey() { return 'item_id'; }
得到order 表的id字段。
getPrimaryKey
php
fecshop/yii2_fecshop
services/order/Item.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/order/Item.php
BSD-3-Clause
public function getByPrimaryKey($primaryKey) { $one = $this->_itemModel->findOne($primaryKey); $primaryKey = $this->getPrimaryKey(); if ($one[$primaryKey]) { return $one; } else { return new $this->_orderModelName(); } }
@param $primaryKey | Int @return Object($this->_itemModel) 通过主键值,返回Order Model对象
getByPrimaryKey
php
fecshop/yii2_fecshop
services/order/Item.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/order/Item.php
BSD-3-Clause
public function getByProductIdAndCustomerId($product_id, $month, $customer_id = 0) { if (!$customer_id) { if (Yii::$app->user->isGuest) { return false; } else { $customer_id = Yii::$app->user->identity->id; } } // 得到产品 $time_gt = strtotime("-0 year -$month month -0 day"); $orderStatusArr = Yii::$service->order->getOrderPaymentedStatusArr(); $orders = Yii::$service->order->coll([ 'select' => ['order_id'], 'where' => [ ['customer_id' => $customer_id], ['>', 'created_at', $time_gt], ['in', 'order_status', $orderStatusArr] ], 'asArray' => true, 'numPerPage' => 500, ]); $order_ids = []; if (isset($orders['coll']) && !empty($orders['coll'])) { foreach ($orders['coll'] as $order) { $order_ids[] = $order['order_id']; } } if (empty($order_ids)) { return false; } $items = $this->_itemModel->find()->asArray()->where([ 'product_id' => $product_id, ])->andWhere([ 'in', 'order_id', $order_ids ]) ->all(); if (!empty($items)) { return $items; } else { return false; } }
@param $product_id | string , 产品的id @param $customer_id | int, 用户的id @param $month | int, 几个月内的订单 通过product_id和customerId,得到$month个月内下单支付成功的产品
getByProductIdAndCustomerId
php
fecshop/yii2_fecshop
services/order/Item.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/order/Item.php
BSD-3-Clause
public function getProductImage($product_one, $item_one) { $custom_option = $product_one['custom_option']; $custom_option_sku = $item_one['custom_option_sku']; $image = ''; // 设置图片 if (isset($product_one['image']['main']['image'])) { $image = $product_one['image']['main']['image']; } $custom_option_image = isset($custom_option[$custom_option_sku]['image']) ? $custom_option[$custom_option_sku]['image'] : ''; if ($custom_option_image) { $image = $custom_option_image; } if (!$image) { $image = $item_one['image']; } return $image; }
@param $product_one | Object, product model @param $item_one | Array , order item ,是订单产品表取出来的数据 得到产品的图片。如果存在custom option image,则返回custom option image,如果不存在,则返回产品的主图
getProductImage
php
fecshop/yii2_fecshop
services/order/Item.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/order/Item.php
BSD-3-Clause
public function getProductOptions($item_one) { $custom_option_sku = $item_one['custom_option_sku']; $custom_option_info_arr = []; $custom_option = isset($item_one['custom_option']) ? $item_one['custom_option'] : ''; 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 = isset($item_one['spu_options']) ? $item_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; }
@param $item_one | Array , order item 通过$item_one 的$item_one['custom_option_sku'],$item_one['custom_option'] , $item_one['spu_options'] 将spu的选择属性和自定义属性custom_option 组合起来,返回一个统一的数组
getProductOptions
php
fecshop/yii2_fecshop
services/order/Item.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/order/Item.php
BSD-3-Clause
protected function getProductSpuOptions($productOb) { $custom_option_info_arr = []; $productPrimaryKey = Yii::$service->product->getPrimaryKey(); $productAttrGroup = $productOb['attr_group']; if (isset($productOb['attr_group']) && !empty($productOb['attr_group'])) { $spuArr = Yii::$service->product->getSpuAttr($productAttrGroup); //var_dump($productOb['attr_group_info']); // mysql存储 if (isset($productOb['attr_group_info']) && is_array($productOb['attr_group_info'])) { $attr_group_info = $productOb['attr_group_info']; if (is_array($spuArr) && !empty($spuArr)) { foreach ($spuArr as $spu_attr) { if (isset($attr_group_info[$spu_attr]) && !empty($attr_group_info[$spu_attr])) { // 进行翻译。 $spu_attr_label = Yii::$service->page->translate->__($spu_attr); $spu_attr_val = Yii::$service->page->translate->__($attr_group_info[$spu_attr]); $custom_option_info_arr[$spu_attr_label] = $spu_attr_val; } } } } else { // mongodb类型 Yii::$service->product->addGroupAttrs($productAttrGroup); $productOb = Yii::$service->product->getByPrimaryKey((string) $productOb[$productPrimaryKey ]); if (is_array($spuArr) && !empty($spuArr)) { foreach ($spuArr as $spu_attr) { if (isset($productOb[$spu_attr]) && !empty($productOb[$spu_attr])) { // 进行翻译。 $spu_attr_label = Yii::$service->page->translate->__($spu_attr); $spu_attr_val = Yii::$service->page->translate->__($productOb[$spu_attr]); $custom_option_info_arr[$spu_attr_label] = $spu_attr_val; } } } } } return $custom_option_info_arr; }
@param $productOb | Object,类型:\fecshop\models\mongodb\Product 得到产品的spu对应的属性以及值。 概念 - spu options:当多个产品是同一个spu,但是不同的sku的时候,他们的产品表里面的 spu attr 的值是不同的,譬如对应鞋子,size 和 color 就是spu attr,对于同一款鞋子,他们 是同一个spu,对于尺码,颜色不同的鞋子,是不同的sku,他们的spu attr 就是 color 和 size。
getProductSpuOptions
php
fecshop/yii2_fecshop
services/order/Item.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/order/Item.php
BSD-3-Clause
public function attrValConvertUrlStr($strVal) { if ($strVal) { return urlencode($strVal); } }
@param $strVal | String 把属性值转换成url格式的字符串,用于生成url.
attrValConvertUrlStr
php
fecshop/yii2_fecshop
services/url/Category.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/url/Category.php
BSD-3-Clause
public function urlStrConvertAttrVal($urlStr) { return urldecode($urlStr); }
@param $urlStr | String 把url格式的字符串转换成属性值,用于解析url,得到相应的属性值
urlStrConvertAttrVal
php
fecshop/yii2_fecshop
services/url/Category.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/url/Category.php
BSD-3-Clause
protected function strUrlRelation() { return [ ' ' => '!', '&' => '@', ]; }
对Url中的特殊字符进行转换。(用name生成url的时候,会使用这些字符进行转换)
strUrlRelation
php
fecshop/yii2_fecshop
services/url/Category.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/url/Category.php
BSD-3-Clause
public function getOriginUrl($urlKey) { return $this->_urlRewrite->getOriginUrl($urlKey); }
@param $urlKey | string 通过重写后的urlkey字符串,去url_rewrite表中查询,找到重写前的url字符串。
getOriginUrl
php
fecshop/yii2_fecshop
services/url/Rewrite.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/url/Rewrite.php
BSD-3-Clause
public function getPrimaryKey() { return $this->_urlRewrite->getPrimaryKey(); }
get artile's primary key.
getPrimaryKey
php
fecshop/yii2_fecshop
services/url/Rewrite.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/url/Rewrite.php
BSD-3-Clause
public function getByPrimaryKey($primaryKey) { return $this->_urlRewrite->getByPrimaryKey($primaryKey); }
get artile model by primary key.
getByPrimaryKey
php
fecshop/yii2_fecshop
services/url/Rewrite.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/url/Rewrite.php
BSD-3-Clause
public function coll($filter = '') { return $this->_urlRewrite->coll($filter); }
@param $filter|array get artile collection by $filter example filter: [ 'numPerPage' => 20, 'pageNum' => 1, 'orderBy' => ['_id' => SORT_DESC, 'sku' => SORT_ASC ], 'where' => [ ['>','price',1], ['<=','price',10] ['sku' => 'uk10001'], ], 'asArray' => true, ]
coll
php
fecshop/yii2_fecshop
services/url/Rewrite.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/url/Rewrite.php
BSD-3-Clause
public function save($one) { return $this->_urlRewrite->save($one); }
@param $one|array , save one data . @param $originUrlKey|string , article origin url key. save $data to cms model,then,add url rewrite info to system service urlrewrite.
save
php
fecshop/yii2_fecshop
services/url/Rewrite.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/url/Rewrite.php
BSD-3-Clause
public function remove($ids) { return $this->_urlRewrite->remove($ids); }
@param $ids | Array or String or Int 删除相应的url rewrite 记录
remove
php
fecshop/yii2_fecshop
services/url/Rewrite.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/url/Rewrite.php
BSD-3-Clause
public function removeByUpdatedAt($time) { return $this->_urlRewrite->removeByUpdatedAt($time); }
@param $time | Int 根据updated_at 更新时间,删除相应的url rewrite 记录
removeByUpdatedAt
php
fecshop/yii2_fecshop
services/url/Rewrite.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/url/Rewrite.php
BSD-3-Clause
public function find() { return $this->_urlRewrite->find(); }
返回url rewrite model 对应的query
find
php
fecshop/yii2_fecshop
services/url/Rewrite.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/url/Rewrite.php
BSD-3-Clause
public function findOne($where) { return $this->_urlRewrite->findOne($where); }
返回url rewrite 查询结果
findOne
php
fecshop/yii2_fecshop
services/url/Rewrite.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/url/Rewrite.php
BSD-3-Clause
public function newModel() { return $this->_urlRewrite->newModel(); }
返回url rewrite model
newModel
php
fecshop/yii2_fecshop
services/url/Rewrite.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/url/Rewrite.php
BSD-3-Clause
public function getOriginUrl($urlKey) { $UrlData = $this->_urlRewriteModel->find()->where([ 'custom_url_key' => $urlKey, ])->asArray()->one(); if ($UrlData['custom_url_key']) { return $UrlData['origin_url']; } }
@param $urlKey | string 通过重写后的urlkey字符串,去url_rewrite表中查询,找到重写前的url字符串。
getOriginUrl
php
fecshop/yii2_fecshop
services/url/rewrite/RewriteMongodb.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/url/rewrite/RewriteMongodb.php
BSD-3-Clause
public function save($one) { $primaryVal = isset($one[$this->getPrimaryKey()]) ? $one[$this->getPrimaryKey()] : ''; if ($primaryVal) { $model = $this->_urlRewriteModel->findOne($primaryVal); if (!$model) { Yii::$service->helper->errors->add('UrlRewrite {primaryKey} is not exist', ['primaryKey'=>$this->getPrimaryKey()]); return; } } else { $model = new $this->_urlRewriteModelName(); } unset($one['_id']); $saveStatus = Yii::$service->helper->ar->save($model, $one); return true; }
@param $one|array save $data to cms model,then,add url rewrite info to system service urlrewrite.
save
php
fecshop/yii2_fecshop
services/url/rewrite/RewriteMongodb.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/url/rewrite/RewriteMongodb.php
BSD-3-Clause
public function removeByUpdatedAt($time) { if ($time) { $this->_urlRewriteModel->deleteAll([ '$or' => [ [ 'updated_at' => [ '$lt' => (int) $time, ], ], [ 'updated_at' => [ '$exists' => false, ], ], ], ]); echo "delete complete \n"; } }
@param $time | Int 根据updated_at 更新时间,删除相应的url rewrite 记录
removeByUpdatedAt
php
fecshop/yii2_fecshop
services/url/rewrite/RewriteMongodb.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/url/rewrite/RewriteMongodb.php
BSD-3-Clause
public function find() { return $this->_urlRewriteModel->find(); }
返回url rewrite model 对应的query
find
php
fecshop/yii2_fecshop
services/url/rewrite/RewriteMongodb.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/url/rewrite/RewriteMongodb.php
BSD-3-Clause
public function findOne($where) { return $this->_urlRewriteModel->findOne($where); }
返回url rewrite 查询结果
findOne
php
fecshop/yii2_fecshop
services/url/rewrite/RewriteMongodb.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/url/rewrite/RewriteMongodb.php
BSD-3-Clause
public function newModel() { return new $this->_urlRewriteModelName(); }
返回url rewrite model
newModel
php
fecshop/yii2_fecshop
services/url/rewrite/RewriteMongodb.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/url/rewrite/RewriteMongodb.php
BSD-3-Clause
public function getOriginUrl($urlKey) { $UrlData = $this->_urlRewriteModel->find()->where([ 'custom_url_key' => $urlKey, ])->asArray()->one(); if ($UrlData) { return $UrlData['origin_url']; } }
@param $urlKey | string 通过重写后的urlkey字符串,去url_rewrite表中查询,找到重写前的url字符串。
getOriginUrl
php
fecshop/yii2_fecshop
services/url/rewrite/RewriteMysqldb.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/url/rewrite/RewriteMysqldb.php
BSD-3-Clause
public function save($one) { $primaryVal = isset($one[$this->getPrimaryKey()]) ? $one[$this->getPrimaryKey()] : ''; if ($primaryVal) { $model = $this->_urlRewriteModel->findOne($primaryVal); if (!$model) { Yii::$service->helper->errors->add('UrlRewrite {primaryKey} is not exist', ['primaryKey'=>$this->getPrimaryKey()]); return; } } else { $model = new $this->_urlRewriteModelName(); } unset($one['_id']); $saveStatus = Yii::$service->helper->ar->save($model, $one); return true; }
@param $one|array save $data to cms model,then,add url rewrite info to system service urlrewrite.
save
php
fecshop/yii2_fecshop
services/url/rewrite/RewriteMysqldb.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/url/rewrite/RewriteMysqldb.php
BSD-3-Clause
public function removeByUpdatedAt($time) { if ($time) { $this->_urlRewriteModel->deleteAll([ '<', 'updated_at', $time, ]); } }
@param $time | Int 根据updated_at 更新时间,删除相应的url rewrite 记录
removeByUpdatedAt
php
fecshop/yii2_fecshop
services/url/rewrite/RewriteMysqldb.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/url/rewrite/RewriteMysqldb.php
BSD-3-Clause
public function find() { return $this->_urlRewriteModel->find(); }
返回url rewrite model 对应的query
find
php
fecshop/yii2_fecshop
services/url/rewrite/RewriteMysqldb.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/url/rewrite/RewriteMysqldb.php
BSD-3-Clause
public function findOne($where) { return $this->_urlRewriteModel->findOne($where); }
返回url rewrite 查询结果
findOne
php
fecshop/yii2_fecshop
services/url/rewrite/RewriteMysqldb.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/url/rewrite/RewriteMysqldb.php
BSD-3-Clause
public function newModel() { return new $this->_urlRewriteModelName(); }
返回url rewrite model
newModel
php
fecshop/yii2_fecshop
services/url/rewrite/RewriteMysqldb.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/url/rewrite/RewriteMysqldb.php
BSD-3-Clause
public function login($data) { $model = new $this->_adminUserLoginModelName(); $model->username = $data['username']; $model->password = $data['password']; $loginStatus = $model->login(); $errors = $model->errors; if (!empty($errors)) { Yii::$service->helper->errors->addByModelErrors($errors); } return $loginStatus; }
@param $data|array 数组格式:['username'=>'[email protected]','password'=>'xxxx']
login
php
fecshop/yii2_fecshop
services/adminUser/UserLogin.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/adminUser/UserLogin.php
BSD-3-Clause
public function loginAndGetAccessToken($username, $password) { $header = Yii::$app->request->getHeaders(); if (isset($header['access-token']) && $header['access-token']) { $accessToken = $header['access-token']; } // 如果request header中有access-token,则查看这个 access-token 是否有效 if ($accessToken) { $identity = Yii::$app->user->loginByAccessToken($accessToken); if ($identity !== null) { $access_token_created_at = $identity->access_token_created_at; $timeout = Yii::$service->session->timeout; if ($access_token_created_at + $timeout > time()) { return $accessToken; } } } // 如果上面access-token不存在 $data = [ 'username' => $username, 'password' => $password, ]; if ($this->login($data)) { $identity = Yii::$app->user->identity; $identity->generateAccessToken(); $identity->access_token_created_at = time(); $identity->save(); $this->setHeaderAccessToken($identity->access_token); return $identity->access_token; } return null; }
Appapi 部分使用的函数 @param $username | String @param $password | String Appapi 和 第三方进行数据对接部分的用户登陆验证
loginAndGetAccessToken
php
fecshop/yii2_fecshop
services/adminUser/UserLogin.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/adminUser/UserLogin.php
BSD-3-Clause
public function loginByAccessToken($type = null) { $header = Yii::$app->request->getHeaders(); if (isset($header['access-token']) && $header['access-token']) { $accessToken = $header['access-token']; } if ($accessToken) { $identity = Yii::$app->user->loginByAccessToken($accessToken, $type); if ($identity !== null) { $access_token_created_at = $identity->access_token_created_at; $timeout = Yii::$service->session->timeout; // 如果时间没有过期,则返回identity if ($access_token_created_at + $timeout > time()) { //如果时间没有过期,但是快要过期了,在过$updateTimeLimit段时间就要过期,那么更新access_token_created_at。 $updateTimeLimit = Yii::$service->session->updateTimeLimit; if ($access_token_created_at + $timeout <= (time() + $updateTimeLimit)) { $identity->access_token_created_at = time(); $identity->save(); } return $identity; } else { $this->logoutByAccessToken(); return false; } } } }
AppServer 部分使用的函数 @param $type | null or Object 从request headers中获取access-token,然后执行登录 如果登录成功,然后验证时间是否过期 如果不过期,则返回identity ** 该方法为appserver用户通过access-token验证需要执行的函数。
loginByAccessToken
php
fecshop/yii2_fecshop
services/adminUser/UserLogin.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/adminUser/UserLogin.php
BSD-3-Clause
public function logoutByAccessToken() { $userComponent = Yii::$app->user; $identity = $userComponent->identity; if ($identity !== null) { if (!Yii::$app->user->isGuest) { $identity->access_token = null; $identity->access_token_created_at = null; $identity->save(); } $userComponent->switchIdentity(null); } return $userComponent->getIsGuest(); }
通过accessToek的方式,进行登出从操作。
logoutByAccessToken
php
fecshop/yii2_fecshop
services/adminUser/UserLogin.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/adminUser/UserLogin.php
BSD-3-Clause
public function resetCurrentPassword($data){ $this->_userPassResetModel->attributes = $data; if ($this->_userPassResetModel->validate()) { $this->_userPassResetModel->updatePassword(); return true; } else { $errors = $this->_userPassResetModel->errors; Yii::$service->helper->errors->addByModelErrors($errors); return false; } }
@param $data array @return boolean update current user password
resetCurrentPassword
php
fecshop/yii2_fecshop
services/adminUser/AdminUser.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/adminUser/AdminUser.php
BSD-3-Clause
public function saveUserAndRole($data, $roles){ $user_id = $this->save($data); if (!$user_id) { return false; } if (Yii::$service->admin->userRole->saveUserRole($user_id, $roles)) { return true; } return false; }
@param $data array, user form data @param $roles array, role id array @return boolean 保存用户的信息,以及用户的role信息。
saveUserAndRole
php
fecshop/yii2_fecshop
services/adminUser/AdminUser.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/adminUser/AdminUser.php
BSD-3-Clause
public function save($data) { $primaryKey = $this->getPrimaryKey(); $user_id = 0; if ($data[$primaryKey]) { $this->_userFormModel = $this->_userFormModel->findOne($data[$primaryKey]); } $this->_userFormModel->attributes = $data; if (!$data['access_token']) { $this->_userFormModel->access_token = ''; } if (!$data['auth_key']) { $this->_userFormModel->auth_key = ''; } if (!$data['password'] && !$data['id']) { Yii::$service->helper->errors->add("password can not empty"); return null; } if ($this->_userFormModel[$primaryKey]) { if ($this->_userFormModel->validate()) { $this->_userFormModel->save(); $user_id = $this->_userFormModel[$primaryKey]; } else { $errors = $this->_userFormModel->errors; Yii::$service->helper->errors->addByModelErrors($errors); return null; } } else { if ($this->_userFormModel->validate()) { $this->_userFormModel->save(); $user_id = Yii::$app->db->getLastInsertID(); } else { $errors = $this->_userFormModel->errors; Yii::$service->helper->errors->addByModelErrors($errors); return null; } } return $user_id; }
@param $data array, user form data @return mix ,return save user id | null 保存用户的信息。
save
php
fecshop/yii2_fecshop
services/adminUser/AdminUser.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/adminUser/AdminUser.php
BSD-3-Clause
public function getIdAndNameArrByIds($ids) { $user_coll = $this->_model->find() ->asArray() ->select(['id', 'username']) ->where([ 'in', 'id', $ids, ])->all(); $users = []; foreach ($user_coll as $one) { $users[$one['id']] = $one['username']; } return $users; }
@param $ids | Int Array @return 得到相应用户的数组。
getIdAndNameArrByIds
php
fecshop/yii2_fecshop
services/adminUser/AdminUser.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/adminUser/AdminUser.php
BSD-3-Clause
public function createAddonsFiles($param) { $package = isset($param['package']) ? $param['package'] : ''; $addon_folder = isset($param['addon_folder']) ? $param['addon_folder'] : ''; $namespaces = isset($param['namespaces']) ? $param['namespaces'] : ''; $addon_name = isset($param['addon_name']) ? $param['addon_name'] : ''; $addon_author = isset($param['addon_author']) ? $param['addon_author'] : ''; if (!$package || !$addon_folder || !$namespaces || !$addon_name || !$addon_author) { return false; } // 创建文件夹 if (!$this->createFolder($param)) { return false; } // 开始渲染gii 模板 //得到应用文件夹 $addonPath = Yii::getAlias('@addons/'.$package.'/'.$addon_folder); // config.php文件写入 $viewFile = '@fecshop/services/extension/generate/config.php'; $configContent =Yii::$app->view->renderFile($viewFile, $param); // 写入的文件路径 $addonConfigFile = $addonPath. '/config.php'; if (@file_put_contents($addonConfigFile, $configContent) === false) { Yii::$service->helper->errors->add('Unable to write the file '.$addonConfigFile); return false; } // 写入Install $viewFile = '@fecshop/services/extension/generate/administer/Install.php'; $configContent =Yii::$app->view->renderFile($viewFile, $param); // 写入的文件路径 $addonConfigFile = $addonPath. '/administer/Install.php'; if (@file_put_contents($addonConfigFile, $configContent) === false) { Yii::$service->helper->errors->add('Unable to write the file '.$addonConfigFile); return false; } // 写入Upgrade $viewFile = '@fecshop/services/extension/generate/administer/Upgrade.php'; $configContent =Yii::$app->view->renderFile($viewFile, $param); // 写入的文件路径 $addonConfigFile = $addonPath. '/administer/Upgrade.php'; if (@file_put_contents($addonConfigFile, $configContent) === false) { Yii::$service->helper->errors->add('Unable to write the file '.$addonConfigFile); return false; } // 写入 Uninstall $viewFile = '@fecshop/services/extension/generate/administer/Uninstall.php'; $configContent =Yii::$app->view->renderFile($viewFile, $param); // 写入的文件路径 $addonConfigFile = $addonPath. '/administer/Uninstall.php'; if (@file_put_contents($addonConfigFile, $configContent) === false) { Yii::$service->helper->errors->add('Unable to write the file '.$addonConfigFile); return false; } return true; }
@param $param | array 数组子项:package, addon_folder, namespaces, addon_name, addon_author 创建应用初始化包到指定的文件路径。
createAddonsFiles
php
fecshop/yii2_fecshop
services/extension/Generate.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/extension/Generate.php
BSD-3-Clause
public function upgrade($extension_namespace) { // 插件不存在 $modelOne = Yii::$service->extension->getByNamespace($extension_namespace); if (!$modelOne['namespace']) { Yii::$service->helper->errors->add('extension: {namespace} is not exist', ['namespace' =>$extension_namespace ]); return false; } $this->currentNamespace = $extension_namespace; // 插件如果没有安装 $installed_status = $modelOne['installed_status']; if (!Yii::$service->extension->isInstalledStatus($installed_status)) { Yii::$service->helper->errors->add('extension: {namespace} has not installed', ['namespace' =>$extension_namespace ]); return false; } // 通过数据库找到应用的配置文件路径,如果配置文件不存在 $extensionConfigFile = Yii::getAlias($modelOne['config_file_path']); if (!file_exists($extensionConfigFile)) { Yii::$service->helper->errors->add('extension: {namespace} config file is not exit', ['namespace' =>$extension_namespace ]); return false; } // 加载应用配置 $extensionConfig = require($extensionConfigFile); // 如果没有该配置,说明该插件不需要进行安装操作。 if (!isset($extensionConfig['administer']['upgrade'])) { Yii::$service->helper->errors->add('extension: {namespace}, have no upgrade file function', ['namespace' =>$extension_namespace ]); return false; } // 事务操作, 只对mysql有效,如果是mongodb,无法回滚 $innerTransaction = Yii::$app->db->beginTransaction(); try { // 执行应用的upgrade部分功能 if (!Yii::$service->extension->upgradeAddons($extensionConfig['administer']['upgrade'], $modelOne)) { $innerTransaction->rollBack(); return false; } $innerTransaction->commit(); $this->reflushAdminMenuRoleCache(); return true; } catch (\Exception $e) { $innerTransaction->rollBack(); Yii::$service->helper->errors->add($e->getMessage()); return false; } return false; }
应用升级函数 @param $extension_namespace | string , 插件的名称
upgrade
php
fecshop/yii2_fecshop
services/extension/Administer.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/extension/Administer.php
BSD-3-Clause
public function uninstall($extension_namespace) { // 插件不存在 $modelOne = Yii::$service->extension->getByNamespace($extension_namespace); if (!$modelOne['namespace']) { Yii::$service->helper->errors->add('extension: {namespace} is not exist', ['namespace' =>$extension_namespace ]); return false; } $this->currentNamespace = $extension_namespace; // 插件如果没有安装 $installed_status = $modelOne['installed_status']; if (!Yii::$service->extension->isInstalledStatus($installed_status)) { Yii::$service->helper->errors->add('extension: {namespace} has not installed', ['namespace' =>$extension_namespace ]); return false; } // 通过数据库找到应用的配置文件路径,如果配置文件不存在 $extensionConfigFile = Yii::getAlias($modelOne['config_file_path']); if (!file_exists($extensionConfigFile)) { Yii::$service->helper->errors->add('extension: {namespace} config file is not exit', ['namespace' =>$extension_namespace ]); return false; } // 加载应用配置 $extensionConfig = require($extensionConfigFile); // 如果没有该配置,说明该插件无法进行卸载操作 if (!isset($extensionConfig['administer']['uninstall'])) { Yii::$service->helper->errors->add('extension: {namespace}, have no uninstall file function', ['namespace' =>$extension_namespace ]); return false; } // 事务操作, 只对mysql有效,执行插件的uninstall,并在extensions表中进行删除扩展配置 $innerTransaction = Yii::$app->db->beginTransaction(); try { // 执行应用的upgrade部分功能 if (!Yii::$service->extension->uninstallAddons($extensionConfig['administer']['uninstall'], $modelOne)) { $innerTransaction->rollBack(); return false; } $innerTransaction->commit(); } catch (\Exception $e) { $innerTransaction->rollBack(); Yii::$service->helper->errors->add($e->getMessage()); return false; } // 进行应用源文件的删除操作。 $package = $modelOne['package']; $folder = $modelOne['folder']; if ($package && $folder && $this->uninstallRemoveFile) { $installPath = Yii::getAlias('@addons/' . $package . '/' . $folder); // 从配置中获取,是否进行应用文件夹的删除,如果是,则进行文件的删除。 Yii::$service->helper->deleteDir($installPath); } $this->reflushAdminMenuRoleCache(); return true; }
3.插件卸载。
uninstall
php
fecshop/yii2_fecshop
services/extension/Administer.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/extension/Administer.php
BSD-3-Clause
protected function installDbData($modelOne) { }
数据库的安装
installDbData
php
fecshop/yii2_fecshop
services/extension/Administer.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/extension/Administer.php
BSD-3-Clause
public function copyThemeFile($sourcePath) { if (!$this->currentNamespace) { Yii::$service->helper->errors->add('copyThemeFile: current extension: {namespace} is not exist', ['namespace' =>$this->currentNamespace ]); return false; } $targetPath = Yii::getAlias('@appimage/common/addons/'.$this->currentNamespace); Yii::$service->helper->copyDirImage($sourcePath, $targetPath); }
theme文件进行copy到@app/theme/base/addons 下面。
copyThemeFile
php
fecshop/yii2_fecshop
services/extension/Administer.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/extension/Administer.php
BSD-3-Clause
public function removeThemeFile() { if (!$this->currentNamespace) { Yii::$service->helper->errors->add('removeThemeFile: current extension: {namespace} is not exist', ['namespace' =>$this->currentNamespace ]); return false; } $sourcePath = Yii::getAlias('@appimage/common/addons/'.$this->currentNamespace); Yii::$service->helper->deleteDir($sourcePath); return true; }
theme文件进行copy到@app/theme/base/addons 下面。
removeThemeFile
php
fecshop/yii2_fecshop
services/extension/Administer.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/extension/Administer.php
BSD-3-Clause
public function login($param) { $url = $this->remoteUrl . $this->loginUrlKey ; $data = [ 'email' => $param['email'], 'password' => $param['password'], ]; list($responseHeader, $result) = $this->getCurlData($url, 'post', [], $data, 30); if ($result['code'] == 200) { $access_token = $responseHeader['Access-Token']; $this->setAccessToken($access_token); return true; } return false; }
远程登陆
login
php
fecshop/yii2_fecshop
services/extension/RemoteService.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/extension/RemoteService.php
BSD-3-Clause
public function getDeveloperInfo() { $accessToken = $this->getAccessToken(); if (!$accessToken) { return false; } $url = $this->remoteUrl . $this->getDeveloperInfoUrlKey ; $headerRequest = [ 'access-token: '.$accessToken, ]; list($responseHeader, $result) = $this->getCurlData($url, 'post', $headerRequest, [], 30); if ($result['code'] == 200) { return $result['data']; } return false; }
得到开发者的信息
getDeveloperInfo
php
fecshop/yii2_fecshop
services/extension/RemoteService.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/extension/RemoteService.php
BSD-3-Clause
public function getMyAddonsInfo($pageNum, $numPerPage) { $accessToken = $this->getAccessToken(); if (!$accessToken) { return false; } $url = $this->remoteUrl . $this->getAddonsListUrlKey ; $headerRequest = [ 'access-token: '.$accessToken, ]; $data = [ 'pageNum' => $pageNum, 'numPerPage' => $numPerPage, ]; list($responseHeader, $result) = $this->getCurlData($url, 'post', $headerRequest, $data, 30); if ($result['code'] == 200) { return $result['data']; } return false; }
得到远程的addon 信息(我的应用列表)
getMyAddonsInfo
php
fecshop/yii2_fecshop
services/extension/RemoteService.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/extension/RemoteService.php
BSD-3-Clause
public function getAddonsInfoByNamespace($namespace) { $accessToken = $this->getAccessToken(); if (!$accessToken) { return false; } $url = $this->remoteUrl . $this->getAddonInfoUrlKey ; $headerRequest = [ 'access-token: '.$accessToken, ]; $data = [ 'namespace' => $namespace, ]; list($responseHeader, $result) = $this->getCurlData($url, 'post', $headerRequest, $data, 30); if ($result['code'] == 200) { return $result['data']; } return false; }
得到应用的详细信息。
getAddonsInfoByNamespace
php
fecshop/yii2_fecshop
services/extension/RemoteService.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/extension/RemoteService.php
BSD-3-Clause
public function getExtensionZipFilePath($packageName, $folderName) { $filePath = Yii::getAlias('@addons/'.$packageName.'/'.$folderName.'/'.$folderName.'.zip'); return $filePath; }
应用zip文件报错的文件路径
getExtensionZipFilePath
php
fecshop/yii2_fecshop
services/extension/RemoteService.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/extension/RemoteService.php
BSD-3-Clause
public function downloadAddons($namespace, $packageName, $folderName, $addonName) { // 得到下载的url $url = $this->remoteUrl . '/customer/addons/download?namespace='.$namespace; // 当前应用的package,进行mkdir,然后chomod 777 $packagePath = Yii::getAlias('@addons/'.$packageName); if (!is_dir($packagePath)){ mkdir($packagePath); chmod($packagePath, 0777); } // 应用文件夹 $packagePath = Yii::getAlias('@addons/'.$packageName.'/'.$folderName); if (!is_dir($packagePath)){ mkdir($packagePath); chmod($packagePath, 0777); } $filePath = $this->getExtensionZipFilePath($packageName, $folderName); // 根据文件路径,以及addon的name,得到zip文件存放的文件完整路径 //$filePath = Yii::getAlias('@addons/'.$packageName.'/'.$folderName.'/'.$folderName.'.zip'); // 将url中的zip文件,存储到该文件目录。 if ($this->downCurl($url,$filePath)) { return $filePath; } return null; }
下载应用
downloadAddons
php
fecshop/yii2_fecshop
services/extension/RemoteService.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/extension/RemoteService.php
BSD-3-Clause
function downCurl($url, $filePath) { $accessToken = $this->getAccessToken(); if (!$accessToken) { return false; } $headerRequest = [ 'access-token: '.$accessToken, ]; //初始化 $ch = curl_init(); curl_setopt($ch, CURLOPT_HTTPHEADER, $headerRequest ); //设置抓取的url curl_setopt($ch, CURLOPT_URL, $url); //打开文件描述符 $fp = fopen ($filePath, 'w+'); curl_setopt($ch, CURLOPT_FILE, $fp); //这个选项是意思是跳转,如果你访问的页面跳转到另一个页面,也会模拟访问。 curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch,CURLOPT_TIMEOUT, 5000); //执行命令 curl_exec($ch); //关闭URL请求 curl_close($ch); //关闭文件描述符 fclose($fp); return true; }
远程下载zip包
downCurl
php
fecshop/yii2_fecshop
services/extension/RemoteService.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/extension/RemoteService.php
BSD-3-Clause
public function destroy() { }
销毁所有
destroy
php
fecshop/yii2_fecshop
services/session/SessionRedis.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/session/SessionRedis.php
BSD-3-Clause
public function destroy() { }
销毁所有
destroy
php
fecshop/yii2_fecshop
services/session/SessionMysqldb.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/session/SessionMysqldb.php
BSD-3-Clause
public function sendLoginEmail($emailInfo) { $toEmail = $emailInfo['email']; $loginInfo = $this->emailTheme['login']; if (isset($loginInfo['enable']) && $loginInfo['enable']) { $mailerConfigParam = ''; if (isset($loginInfo['mailerConfig']) && $loginInfo['mailerConfig']) { $mailerConfigParam = $loginInfo['mailerConfig']; } if (isset($loginInfo['widget']) && $loginInfo['widget']) { $widget = $loginInfo['widget']; } if (isset($loginInfo['viewPath']) && $loginInfo['viewPath']) { $viewPath = $loginInfo['viewPath']; } if ($widget && $viewPath) { list($subject, $htmlBody) = Yii::$service->email->getSubjectAndBody($widget, $viewPath, '', $emailInfo); $sendInfo = [ 'to' => $toEmail, 'subject' => $subject, 'htmlBody' => $htmlBody, 'senderName'=> Yii::$service->store->currentStore, ]; Yii::$service->email->send($sendInfo, $mailerConfigParam); return true; } } }
@param $emailInfo | Array ,数组格式格式如下: [ 'email' => '[email protected]' , [...] ] 其中email是必须有的数组key,对于其他的, 可以根据功能添加,添加后,可以在邮件模板的$params中调用,譬如调用email为 $params['email'] @return boolean , 如果发送成功,则返回true。 客户登录账号发送邮件
sendLoginEmail
php
fecshop/yii2_fecshop
services/email/Customer.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/email/Customer.php
BSD-3-Clause
public function sendForgotPasswordEmail($emailInfo) { $toEmail = $emailInfo['email']; $forgotPasswordInfo = $this->emailTheme['forgotPassword']; if (isset($forgotPasswordInfo['enable']) && $forgotPasswordInfo['enable']) { $mailerConfigParam = ''; if (isset($forgotPasswordInfo['mailerConfig']) && $forgotPasswordInfo['mailerConfig']) { $mailerConfigParam = $forgotPasswordInfo['mailerConfig']; } if (isset($forgotPasswordInfo['widget']) && $forgotPasswordInfo['widget']) { $widget = $forgotPasswordInfo['widget']; } if (isset($forgotPasswordInfo['viewPath']) && $forgotPasswordInfo['viewPath']) { $viewPath = $forgotPasswordInfo['viewPath']; } if ($widget && $viewPath) { list($subject, $htmlBody) = Yii::$service->email->getSubjectAndBody($widget, $viewPath, '', $emailInfo); $sendInfo = [ 'to' => $toEmail, 'subject' => $subject, 'htmlBody' => $htmlBody, 'senderName'=> Yii::$service->store->currentStore, ]; Yii::$service->email->send($sendInfo, $mailerConfigParam); return true; } } }
@param $emailInfo | Array ,数组格式格式如下: [ 'email' => '[email protected]' , [...] ] 其中email是必须有的数组key,对于其他的, 可以根据功能添加,添加后,可以在邮件模板的$params中调用,譬如调用email为 $params['email'] @return boolean , 如果发送成功,则返回true。 客户忘记秒发送的邮件
sendForgotPasswordEmail
php
fecshop/yii2_fecshop
services/email/Customer.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/email/Customer.php
BSD-3-Clause
public function getPasswordResetTokenExpire() { $forgotPasswordInfo = $this->emailTheme['forgotPassword']; if (isset($forgotPasswordInfo['passwordResetTokenExpire']) && $forgotPasswordInfo['passwordResetTokenExpire']) { return $forgotPasswordInfo['passwordResetTokenExpire']; } }
超时时间:忘记密码发送邮件,内容中的修改密码链接的超时时间。
getPasswordResetTokenExpire
php
fecshop/yii2_fecshop
services/email/Customer.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/email/Customer.php
BSD-3-Clause
public function getRegisterEnableTokenExpire() { return $this->registerAccountEnableTokenExpire; }
超时时间: 注册账户激活邮件的token的过去时间
getRegisterEnableTokenExpire
php
fecshop/yii2_fecshop
services/email/Customer.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/email/Customer.php
BSD-3-Clause
public function sendContactsEmail($emailInfo) { $contactsInfo = $this->emailTheme['contacts']; $toEmail = $contactsInfo['address']; if (!$toEmail) { Yii::$service->page->message->addError(['Contact us : receive email is empty , you must config it in email customer contacts email']); return; } if (isset($contactsInfo['enable']) && $contactsInfo['enable']) { $mailerConfigParam = ''; if (isset($contactsInfo['mailerConfig']) && $contactsInfo['mailerConfig']) { $mailerConfigParam = $contactsInfo['mailerConfig']; } if (isset($contactsInfo['widget']) && $contactsInfo['widget']) { $widget = $contactsInfo['widget']; } if (isset($contactsInfo['viewPath']) && $contactsInfo['viewPath']) { $viewPath = $contactsInfo['viewPath']; } if ($widget && $viewPath) { list($subject, $htmlBody) = Yii::$service->email->getSubjectAndBody($widget, $viewPath, '', $emailInfo); $sendInfo = [ 'to' => $toEmail, 'subject' => $subject, 'htmlBody' => $htmlBody, 'senderName'=> Yii::$service->store->currentStore, ]; // 添加表记录。 Yii::$service->customer->contacts->addCustomerContacts($emailInfo); Yii::$service->email->send($sendInfo, $mailerConfigParam); return true; } } }
@param $emailInfo | Array ,数组格式格式如下: [ 'email' => '[email protected]' , [...] ] 其中email是必须有的数组key,对于其他的, 可以根据功能添加,添加后,可以在邮件模板的$params中调用,譬如调用email为 $params['email'] @return boolean , 如果发送成功,则返回true。 客户联系我们邮件。
sendContactsEmail
php
fecshop/yii2_fecshop
services/email/Customer.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/email/Customer.php
BSD-3-Clause
public function sendNewsletterSubscribeEmail($emailInfo) { $toEmail = $emailInfo['email']; $newsletterInfo = $this->emailTheme['newsletter']; if (isset($newsletterInfo['enable']) && $newsletterInfo['enable']) { $mailerConfigParam = ''; if (isset($newsletterInfo['mailerConfig']) && $newsletterInfo['mailerConfig']) { $mailerConfigParam = $newsletterInfo['mailerConfig']; } if (isset($newsletterInfo['widget']) && $newsletterInfo['widget']) { $widget = $newsletterInfo['widget']; } if (isset($newsletterInfo['viewPath']) && $newsletterInfo['viewPath']) { $viewPath = $newsletterInfo['viewPath']; } if ($widget && $viewPath) { list($subject, $htmlBody) = Yii::$service->email->getSubjectAndBody($widget, $viewPath, '', $emailInfo); $sendInfo = [ 'to' => $toEmail, 'subject' => $subject, 'htmlBody' => $htmlBody, 'senderName'=> Yii::$service->store->currentStore, ]; Yii::$service->email->send($sendInfo, $mailerConfigParam); return true; } } }
@param $emailInfo | Array ,数组格式格式如下: [ 'email' => '[email protected]' , [...] ] 其中email是必须有的数组key,对于其他的, 可以根据功能添加,添加后,可以在邮件模板的$params中调用,譬如调用email为 $params['email'] @return boolean , 如果发送成功,则返回true。 订阅邮件成功邮件
sendNewsletterSubscribeEmail
php
fecshop/yii2_fecshop
services/email/Customer.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/email/Customer.php
BSD-3-Clause
public function sendCreateEmail($orderInfo) { $toEmail = $orderInfo['customer_email']; if (Yii::$app->user->isGuest) { $emailThemeInfo = $this->emailTheme['guestCreate']; } else { $emailThemeInfo = $this->emailTheme['loginedCreate']; } if (isset($emailThemeInfo['enable']) && $emailThemeInfo['enable']) { $mailerConfigParam = ''; if (isset($emailThemeInfo['mailerConfig']) && $emailThemeInfo['mailerConfig']) { $mailerConfigParam = $emailThemeInfo['mailerConfig']; } if (isset($emailThemeInfo['widget']) && $emailThemeInfo['widget']) { $widget = $emailThemeInfo['widget']; } if (isset($emailThemeInfo['viewPath']) && $emailThemeInfo['viewPath']) { $viewPath = $emailThemeInfo['viewPath']; } if ($widget && $viewPath) { list($subject, $htmlBody) = Yii::$service->email->getSubjectAndBody($widget, $viewPath, '', $orderInfo); $sendInfo = [ 'to' => $toEmail, 'subject' => $subject, 'htmlBody' => $htmlBody, 'senderName'=> Yii::$service->store->currentStore, ]; Yii::$service->email->send($sendInfo, $mailerConfigParam); return true; } } }
@param $emailInfo | Array ,数组格式格式如下: [ 'emcustomer_emailail' => '[email protected]' , [...] ] 其中customer_email是必须有的数组key,对于其他的, 可以根据功能添加,添加后,可以在邮件模板的$params中调用,譬如调用customer_email为 $params['customer_email'] @return boolean , 如果发送成功,则返回true。 新订单邮件
sendCreateEmail
php
fecshop/yii2_fecshop
services/email/Order.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/email/Order.php
BSD-3-Clause
public function getCartId() { if (!$this->_cart_id) { if (Yii::$service->store->isAppserver()) { // appserver 必须登陆用户才能加入购物车 if (Yii::$app->user->isGuest) { Yii::$service->helper->errors->add('appserver get cart id must login account'); return false; } $customerId = Yii::$app->user->getId(); $cart = $this->getCartByCustomerId($customerId); if ($cart && $cart['cart_id']) { $this->setCartId($cart['cart_id']); } } else { $cart_id = Yii::$service->session->get(self::SESSION_CART_ID); if (! $cart_id) { if (! Yii::$app->user->isGuest) { $customerId = Yii::$app->user->getId(); $cart = $this->getCartByCustomerId($customerId); if ($cart && $cart['cart_id']) { $this->setCartId($cart['cart_id']); } } } else { $this->_cart_id = $cart_id; } } } return $this->_cart_id; }
@return int|null get cart id, or null if user does not do any cart operation @see \fecshop\services\cart\Quote::createCart() @see \fecshop\services\cart\Quote::mergeCartAfterUserLogin()
getCartId
php
fecshop/yii2_fecshop
services/cart/Quote.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/cart/Quote.php
BSD-3-Clause
public function updateGuestCart($address, $shipping_method, $payment_method) { $cart = $this->getCurrentCart(); if ($cart) { $cart->customer_firstname = $address['first_name']; $cart->customer_lastname = $address['last_name']; $cart->customer_email = $address['email']; $cart->customer_telephone = $address['telephone']; $cart->customer_address_street1 = $address['street1']; $cart->customer_address_street2 = $address['street2']; $cart->customer_address_country = $address['country']; $cart->customer_address_city = $address['city']; $cart->customer_address_state = $address['state']; $cart->customer_address_zip = $address['zip']; $cart->shipping_method = $shipping_method; $cart->payment_method = $payment_method; return $cart->save(); } }
@param $address|array 地址信息数组,详细参看下面函数显示的字段。 @param $shipping_method | String 货运方式 @param $payment_method | String 支付方式 @param bool 更新游客购物车信息,用户下次下单 或者 重新下单,可以不需要重新填写货运地址信息。
updateGuestCart
php
fecshop/yii2_fecshop
services/cart/Quote.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/cart/Quote.php
BSD-3-Clause
public function updateLoginCart($address_id, $shipping_method, $payment_method) { $cart = $this->getCurrentCart(); if ($cart && $address_id) { $cart->customer_address_id = $address_id; $cart->shipping_method = $shipping_method; $cart->payment_method = $payment_method; return $cart->save(); } }
@param $address_id | int 用户customer address id @param $shipping_method 货运方式 @param $payment_method 支付方式 @param bool 登录用户的cart信息,进行更新,更新cart的$address_id,$shipping_method,$payment_method。 用途:对于登录用户,create new address(在下单页面),新创建的address会被保存, 然后需要把address_id更新到cart中。 对于 shipping_method 和 payment_method,保存到cart中,下次进入下单页面,会被记录 下次登录用户进行下单,进入下单页面,会自动填写。
updateLoginCart
php
fecshop/yii2_fecshop
services/cart/Quote.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/cart/Quote.php
BSD-3-Clause
public function getCurrentCart() { if (!$this->_cart) { $cart_id = $this->getCartId(); if ($cart_id) { $one = $this->_cartModel->findOne(['cart_id' => $cart_id]); if ($one['cart_id']) { $this->_cart = $one; } } } return $this->_cart; }
@return object 得到当前的cart,如果当前的cart不存在,则返回为空 注意:这是getCurrentCart() 和 getCart()两个函数的区别,getCart()函数在没有cart_id的时候会创建cart。
getCurrentCart
php
fecshop/yii2_fecshop
services/cart/Quote.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/cart/Quote.php
BSD-3-Clause
public function getCart() { if (!$this->_cart) { $cart_id = $this->getCartId(); if (!$cart_id) { $this->createCart(); } else { $one = $this->_cartModel->findOne(['cart_id' => $cart_id]); if ($one['cart_id']) { $this->_cart = $one; } else { // 如果上面查询为空,则创建cart $this->createCart(); } } } return $this->_cart; }
如果当前的Cart不存在,则创建Cart 如果当前的cart存在,则查询,如果查询得到cart,则返回,如果查询不到,则重新创建 设置$this->_cart 为 上面新建或者查询得到的cart对象。
getCart
php
fecshop/yii2_fecshop
services/cart/Quote.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/cart/Quote.php
BSD-3-Clause
public function setCart($cart) { $this->_cart = $cart; }
@param $cart | $this->_cartModel Object 设置$this->_cart 为 当前传递的$cart对象。
setCart
php
fecshop/yii2_fecshop
services/cart/Quote.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/cart/Quote.php
BSD-3-Clause
public function getCartItemCount() { $items_count = 0; if ($cart_id = $this->getCartId()) { if ($cart_id) { $cart = $this->getCart(); //$one = $this->_cartModel->findOne(['cart_id' => $cart_id]); if (isset($cart['items_count']) && $cart['items_count']) { $items_count = $cart['items_count']; } } } return $items_count; }
@return $items_count | Int , 得到购物车中产品的个数。头部的ajax请求一般访问这个. 目前是通过表查询获取的。
getCartItemCount
php
fecshop/yii2_fecshop
services/cart/Quote.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/cart/Quote.php
BSD-3-Clause
public function computeCartInfo($active_item_qty = null) { $items_all_count = 0; if ($active_item_qty === null) { $active_item_qty = Yii::$service->cart->quoteItem->getActiveItemQty(); } $items_all_count = Yii::$service->cart->quoteItem->getItemAllQty(); $cart = $this->getCart(); $cart->items_all_count = $items_all_count; $cart->items_count = $active_item_qty; $cart->save(); return true; }
@param $item_qty | Int 当$active_item_qty为null时,从cart items表中查询产品总数。 当$item_qty 不等于null时,代表已经知道购物车中active产品的个数,不需要去cart_item表中查询,譬如清空购物车操作,直接就知道产品个数肯定为零。 当购物车的产品变动后,会调用该函数,更新cart表的产品总数
computeCartInfo
php
fecshop/yii2_fecshop
services/cart/Quote.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/cart/Quote.php
BSD-3-Clause
public function setCartId($cart_id) { $this->_cart_id = $cart_id; if (!Yii::$service->store->isAppserver()) { Yii::$service->session->set(self::SESSION_CART_ID, $cart_id); } }
@param int $cart_id cart id 设置cart_id类变量以及session中记录当前cartId的值 Cart的session的超时时间由session组件决定。
setCartId
php
fecshop/yii2_fecshop
services/cart/Quote.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/cart/Quote.php
BSD-3-Clause
public function clearCart() { return Yii::$service->cart->quoteItem->removeNoActiveItemsByCartId(); }
删除掉active状态的购物车产品 对于active的产品,在支付成功后,这些产品从购物车清楚 而对于noActive产品,这些产品并没有支付,因而在购物车中保留。
clearCart
php
fecshop/yii2_fecshop
services/cart/Quote.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/cart/Quote.php
BSD-3-Clause
public function createCart() { $myCart = new $this->_cartModelName; $myCart->store = Yii::$service->store->currentStore; $myCart->created_at = time(); $myCart->updated_at = time(); if (!Yii::$app->user->isGuest) { $identity = Yii::$app->user->identity; $id = $identity['id']; $firstname = $identity['firstname']; $lastname = $identity['lastname']; $email = $identity['email']; $myCart->customer_id = $id; $myCart->customer_email = $email; $myCart->customer_firstname = $firstname; $myCart->customer_lastname = $lastname; $myCart->customer_is_guest = 2; } else { $myCart->customer_is_guest = 1; } $myCart->remote_ip = \fec\helpers\CFunc::get_real_ip(); $myCart->app_name = Yii::$service->helper->getAppName(); //if ($defaultShippingMethod = Yii::$service->shipping->getDefaultShippingMethod()) { // $myCart->shipping_method = $defaultShippingMethod; //} $myCart->save(); $cart_id = $myCart['cart_id']; $this->setCartId($cart_id); $this->setCart($this->_cartModel->findOne($cart_id)); }
初始化创建cart信息, 在用户的第一个产品加入购物车时,会在数据库中创建购物车.
createCart
php
fecshop/yii2_fecshop
services/cart/Quote.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/cart/Quote.php
BSD-3-Clause
public function getCartInfo($activeProduct = true, $shipping_method = '', $country = '', $region = '*') { // 根据传递的参数的不同,购物车数据计算一次后,第二次调用,不会重新计算数据。 $cartInfoKey = $shipping_method.'-shipping-'.$country.'-country-'.$region.'-region'; if (!isset($this->cartInfo[$cartInfoKey])) { $cart_id = $this->getCartId(); if (!$cart_id) { return false; } $cart = $this->getCart(); // 购物车中所有的产品个数 $items_all_count = $cart['items_all_count']; // 购物车中active状态的产品个数 $items_count = $cart['items_count']; if ($items_count <=0 && $items_all_count <= 0) { return false; } $coupon_code = $cart['coupon_code']; $cart_product_info = Yii::$service->cart->quoteItem->getCartProductInfo($activeProduct); //var_dump($cart_product_info); if (is_array($cart_product_info)) { $product_weight = $cart_product_info['product_weight']; $product_volume_weight = $cart_product_info['product_volume_weight']; $product_volume = $cart_product_info['product_volume']; $product_final_weight = max($product_weight, $product_volume_weight); $products = $cart_product_info['products']; $product_total = $cart_product_info['product_total']; $base_product_total = $cart_product_info['base_product_total']; $product_qty_total = $cart_product_info['product_qty_total']; if (is_array($products) && !empty($products)) { $currShippingCost = 0; $baseShippingCost = 0; if ($shipping_method && $product_final_weight) { $shippingCost = $this->getShippingCost($shipping_method, $product_final_weight, $country, $region); $currShippingCost = $shippingCost['currCost']; $baseShippingCost = $shippingCost['baseCost']; } $couponCost = $this->getCouponCost($base_product_total, $coupon_code); $baseDiscountCost = $couponCost['baseCost']; $currDiscountCost = $couponCost['currCost']; $curr_grand_total = $product_total + $currShippingCost - $currDiscountCost; $base_grand_total = $base_product_total + $baseShippingCost - $baseDiscountCost; if (!$shipping_method) { $shipping_method = $cart['shipping_method']; } $this->cartInfo[$cartInfoKey] = [ 'cart_id' => $cart_id, 'store' => $cart['store'], // store nme 'items_count' => $product_qty_total, // 因为购物车使用了active,因此生成订单的产品个数 = 购物车中active的产品的总个数(也就是在购物车页面用户勾选的产品的总数),而不是字段 $cart['items_count'] 'coupon_code' => $coupon_code, // coupon卷码 'shipping_method' => $shipping_method, 'payment_method' => $cart['payment_method'], 'grand_total' => Yii::$service->helper->format->numberFormat($curr_grand_total), // 当前货币总金额 'shipping_cost' => Yii::$service->helper->format->numberFormat($currShippingCost), // 当前货币,运费 'coupon_cost' => Yii::$service->helper->format->numberFormat($currDiscountCost), // 当前货币,优惠券优惠金额 'product_total' => Yii::$service->helper->format->numberFormat($product_total), // 当前货币,购物车中产品的总金额 'base_grand_total' => Yii::$service->helper->format->numberFormat($base_grand_total), // 基础货币总金额 'base_shipping_cost'=> Yii::$service->helper->format->numberFormat($baseShippingCost), // 基础货币,运费 'base_coupon_cost' => Yii::$service->helper->format->numberFormat($baseDiscountCost), // 基础货币,优惠券优惠金额 'base_product_total'=> Yii::$service->helper->format->numberFormat($base_product_total), // 基础货币,购物车中产品的总金额 'products' => $products, //产品信息。 'product_weight' => Yii::$service->helper->format->numberFormat($product_weight), //产品的总重量。 'product_volume_weight' => Yii::$service->helper->format->numberFormat($product_volume_weight), 'product_volume' => Yii::$service->helper->format->numberFormat($product_volume), ]; } } } return $this->cartInfo[$cartInfoKey]; }
@param $activeProduct | boolean , 是否只要active的产品 @param $shipping_method | String 传递的货运方式 @param $country | String 货运国家 @param $region | String 省市 @return bool OR array ,如果存在问题返回false,对于返回的数组的格式参看下面$this->cartInfo[$cartInfoKey] 部分的数组。 返回当前购物车的信息。包括购物车对应的产品信息。 对于可选参数,如果不填写,就是返回当前的购物车的数据。 对于填写了参数,返回的是填写参数后的数据,这个一般是用户选择了了货运方式,国家等,然后 实时的计算出来数据反馈给用户,但是,用户选择的数据并没有进入cart表
getCartInfo
php
fecshop/yii2_fecshop
services/cart/Quote.php
https://github.com/fecshop/yii2_fecshop/blob/master/services/cart/Quote.php
BSD-3-Clause