| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590 |
- <?php
- use app\frontend\modules\member\services\MemberAnchorAppService;
- use app\frontend\modules\member\services\MemberCpsAppService;
- use Illuminate\Support\Str;
- use app\common\services\PermissionService;
- use app\common\models\Menu;
- use app\common\services\Session;
- use app\common\exceptions\NotFoundException;
- //商城根目录
- define('SHOP_ROOT', dirname(__FILE__));
- class YunShop
- {
- private static $_req;
- private static $_app;
- private static $_plugin;
- private static $_notice;
- public function __construct()
- {
- }
- /**
- * Configures an object with the initial property values.
- * @param object $object the object to be configured
- * @param array $properties the property initial values given in terms of name-value pairs.
- * @return object the object itself
- */
- public static function configure($object, $properties)
- {
- foreach ($properties as $name => $value) {
- $object->$name = $value;
- }
- return $object;
- }
- public static function getAppNamespace()
- {
- $rootName = 'app';
- if (self::isWeb()) {
- $rootName .= '\\backend';
- }
- if (self::isApp() || self::isApi()) {
- $rootName .= '\\frontend';
- }
- return $rootName;
- }
- public static function getAppPath()
- {
- $path = dirname(__FILE__);
- if (self::isWeb()) {
- $path .= '/backend';
- }
- if (self::isApp() || self::isApi()) {
- $path .= '/frontend';
- }
- return $path;
- }
- public static function isPHPUnit()
- {
- return strpos($_SERVER['PHP_SELF'], 'phpunit') !== false ? true : false;
- }
- public static function isWeb()
- {
- return request()->isBackend();
- }
- public static function isApp()
- {
- if (self::isPHPUnit()) {
- return true;
- }
- return strpos($_SERVER['PHP_SELF'], '/app/index.php') !== false ? true : false;
- }
- public static function isApi()
- {
- return (strpos($_SERVER['PHP_SELF'], '/addons/') !== false &&
- strpos($_SERVER['PHP_SELF'], '/api.php') !== false) ? true : false;
- }
- /**
- *
- * @return bool
- */
- public static function isWechatApi()
- {
- if (config('app.framework') == 'platform') {
- return (strpos($_SERVER['REQUEST_URI'], '/wechat') !== false &&
- strpos($_SERVER['REQUEST_URI'], '/api') !== false) ? true : false;
- } else {
- return (strpos($_SERVER['PHP_SELF'], '/addons/') === false &&
- strpos($_SERVER['PHP_SELF'], '/api.php') !== false) ? true : false;
- }
- }
- /**
- * 是否插件
- * @return bool
- */
- public static function isPlugin()
- {
- if (config('app.framework') == 'platform') {
- return (strpos(request()->getRequestUri(), config('app.isWeb')) !== false &&
- strpos(request()->getRequestUri(), '/plugin') !== false) ? true : false;
- } else {
- return (strpos($_SERVER['PHP_SELF'], '/web/') !== false &&
- strpos($_SERVER['PHP_SELF'], '/plugin.php') !== false) ? true : false;
- }
- }
- /**
- * @name 验证是否商城操作员
- * @return array|bool|null|stdClass
- * @author
- */
- public static function isRole()
- {
- global $_W;
- if (app('plugins')->isEnabled('supplier')) {
- $res = \Illuminate\Support\Facades\DB::table('yz_supplier')->where('uid', $_W['uid'])->first();
- if (!$res) {
- return false;
- }
- return $res;
- }
- return false;
- }
- /**
- * @name 验证是否文章营销管理员
- * @author
- * @return array|bool|null|stdClass
- */
- public static function isArticle()
- {
- global $_W;
- if (app('plugins')->isEnabled('article')) {
- if (!\Illuminate\Support\Facades\Schema::hasTable('yz_plugin_article_manager')) {
- return false;
- }
- $res = \Illuminate\Support\Facades\DB::table('yz_plugin_article_manager')->where('uid', $_W['uid'])->first();
- if (!$res) {
- return false;
- }
- return $res;
- }
- return false;
- }
- /**
- * @name 验证是否商城操作员
- * @author
- * @return array|bool|null|stdClass
- */
- public static function isAgencyCompany()
- {
- global $_W;
- if (app('plugins')->isEnabled('agency-statistics')) {
- $res = \Illuminate\Support\Facades\DB::table('yz_agency_company')->where('uid', $_W['uid'])->first();
- if (!$res) {
- return false;
- }
- return $res;
- }
- return false;
- }
- public static function cleanApp()
- {
- self::$_app = null;
- }
- /**
- * @name 验证是否门店店长
- * @return array|bool|null|stdClass
- * @author
- */
- public static function isStore()
- {
- global $_W;
- if (app('plugins')->isEnabled('store-cashier')) {
- $res = \Illuminate\Support\Facades\DB::table('yz_store')->where('user_uid', $_W['uid'])->first();
- if (!$res) {
- return false;
- }
- return $res;
- }
- return false;
- }
- public static function isPayment()
- {
- return strpos($_SERVER['PHP_SELF'], '/payment/') > 0 ? true : false;
- }
- public static function request()
- {
- if (self::$_req !== null) {
- return self::$_req;
- } else {
- self::$_req = new YunRequest();
- return self::$_req;
- }
- }
- /**
- * @return YunApp
- */
- public static function app()
- {
- if (self::$_app !== null) {
- return self::$_app;
- } else {
- self::$_app = new YunApp();
- return self::$_app;
- }
- }
- /**
- * 解析路由
- *
- * 后台访问 /web/index.php?c=site&a=entry&m=sz_yi&do=xxx&route=module.controller.action
- * 前台 /app/index.php....
- *
- * 多字母的路由用中划线隔开 比如:
- * TestCacheController
- * function testClean()
- * 路由写法为 test-cache.test-clean
- *
- */
- // public static function parseRoute($requestRoute)
- // {
- // try {
- // $vers = [];
- // $routes_params = explode('.', $requestRoute);
- //
- // if (preg_match('/(v\d+)\./', $requestRoute, $vers)) {
- // foreach ($routes_params as $key => $item) {
- // if ($item != $vers[1]) {
- // $routes[] = $item;
- // }
- // }
- // } else {
- // $routes = $routes_params;
- // }
- //
- // $path = self::getAppPath();
- // $namespace = self::getAppNamespace();
- // $action = '';
- // $controllerName = '';
- // $currentRoutes = [];
- // $modules = [];
- //
- // if ($routes) {
- // $length = count($routes);
- // $routeFirst = array_first($routes);
- // $countRoute = count($routes);
- // if ($routeFirst === 'plugin' || self::isPlugin()) {
- // if (self::isPlugin()) {
- // $currentRoutes[] = 'plugin';
- // $countRoute += 1;
- // } else {
- // $currentRoutes[] = $routeFirst;
- // array_shift($routes);
- // }
- // $namespace = 'Yunshop';
- // $pluginName = array_shift($routes);
- // if ($pluginName || plugin($pluginName)) {
- // $currentRoutes[] = $pluginName;
- // $namespace .= '\\' . ucfirst(Str::camel($pluginName));
- // $path = base_path() . '/plugins/' . $pluginName . '/src';
- // $length = $countRoute;
- //
- // self::findRouteFile($controllerName, $action, $routes, $namespace, $path, $length, $currentRoutes, $requestRoute, true, $vers);
- //
- // if (!app('plugins')->isEnabled($pluginName)) {
- // throw new NotFoundException("{$pluginName}插件已禁用");
- //
- // }
- // } else {
- // throw new NotFoundException('无此插件');
- //
- // }
- // } else {
- //
- // self::findRouteFile($controllerName, $action, $routes, $namespace, $path, $length, $currentRoutes, $requestRoute, false, $vers);
- //
- // }
- // }
- // } catch (Exception $exception) {
- //// dd($exception);
- //// exit;
- //
- // }
- // //执行run
- // return static::run($namespace, $modules, $controllerName, $action, $currentRoutes);
- //
- // }
- /**
- * 定位路由相关文件
- * @param $controllerName
- * @param $action
- * @param $routes
- * @param $namespace
- * @param $path
- * @param $length
- * @param $requestRoute
- * @param $isPlugin
- */
- public static function findRouteFile(&$controllerName, &$action, $routes, &$namespace, &$path, $length, &$currentRoutes, $requestRoute, $isPlugin, $vers)
- {
- foreach ($routes as $k => $r) {
- $ucFirstRoute = ucfirst(Str::camel($r));
- if (empty($vers)) {
- $controllerFile = $path . ($isPlugin ? '/' : '/controllers/') . $ucFirstRoute . 'Controller.php';
- } else {
- $controllerFile = $path . ($isPlugin ? '/' : '/controllers/') . 'vers/' . $vers[1] . '/' . $ucFirstRoute . 'Controller.php';
- }
- if (is_file($controllerFile)) {
- if (empty($vers)) {
- $namespace .= ($isPlugin ? '' : '\\controllers') . '\\' . $ucFirstRoute . 'Controller';
- } else {
- $namespace .= ($isPlugin ? '\\' : '\\controllers\\') . 'vers\\' . $vers[1] . '\\' . $ucFirstRoute . 'Controller';
- }
- $controllerName = $ucFirstRoute;
- $path = $controllerFile;
- $currentRoutes[] = $r;
- } elseif (is_dir($path .= ($isPlugin ? '' : '/modules') . '/' . $r)) {
- $namespace .= ($isPlugin ? '' : '\\modules') . '\\' . $r;
- $modules[] = $r;
- $currentRoutes[] = $r;
- } else {
- if ($length !== ($isPlugin ? $k + 3 : $k + 1)) {
- throw new NotFoundException('路由长度有误:' . $requestRoute);
- }
- $action = strpos($r, '-') === false ? $r : Str::camel($r);
- $currentRoutes[] = $r;
- }
- }
- }
- public static function getUcfirstName($name)
- {
- if (strpos($name, '-')) {
- $names = explode('-', $name);
- $name = '';
- foreach ($names as $v) {
- $name .= ucfirst($v);
- }
- }
- return ucfirst($name);
- }
- public static function plugin()
- {
- self::$_plugin = new YunPlugin();
- return self::$_plugin;
- }
- public static function notice()
- {
- self::$_notice = new YunNotice();
- return self::$_notice;
- }
- private static function getContent($controller, $action)
- {
- return (new \Illuminate\Pipeline\Pipeline(new \Illuminate\Container\Container()))
- ->send(Illuminate\Http\Request::capture())
- ->through(collect($controller->getMiddleware())->pluck('middleware')->all())
- ->then(function ($request) use ($controller, $action) {
- return $controller->$action($request);
- });
- }
- }
- class YunComponent implements ArrayAccess
- {
- protected $values = [];
- public function __set($name, $value)
- {
- return $this->values[$name] = $value;
- }
- public function __get($name)
- {
- if (!array_key_exists($name, $this->values)) {
- $this->values[$name] = null;
- }
- return $this->values[$name];
- }
- function __isset($name)
- {
- return array_key_exists($name, $this->values);
- }
- public function set($name, $value)
- {
- $this->values[$name] = $value;
- return $this;
- }
- public function get($key = null)
- {
- if (isset($key)) {
- $result = json_decode(array_get($this->values, $key, null), true);
- if (@is_array($result)) {
- return $result;
- }
- return array_get($this->values, $key, null);
- }
- return $this->values;
- }
- public function offsetUnset($offset)
- {
- unset($this->values[$offset]);
- }
- public function offsetSet($offset, $value)
- {
- $this->values[$offset] = $value;
- }
- public function offsetGet($offset)
- {
- if (isset($this->values[$offset])) {
- return $this->values[$offset];
- }
- return null;
- }
- public function offsetExists($offset)
- {
- if (isset($this->values[$offset])) {
- return true;
- }
- return false;
- }
- }
- class YunRequest extends YunComponent
- {
- public function __construct()
- {
- $this->values = request()->input();
- }
- }
- /**
- * Class YunApp
- * @property int uniacid
- * @property int uid
- */
- class YunApp extends YunComponent
- {
- protected $values;
- public $currentItems = [];
- public function __construct()
- {
- global $_W;
- $this->values = !YunShop::isWeb() && !YunShop::isWechatApi() ? $this->getW() : (array)$_W;
- }
- public function getW()
- {
- $uniacid = intval(trim(request()->get('i')));
- $account = \app\common\models\AccountWechats::getAccountByUniacid($uniacid);
- return [
- 'uniacid' => $uniacid,
- 'weid' => $uniacid,
- 'acid' => $uniacid,
- 'account' => $account ? $account->toArray() : '',
- ];
- }
- /**
- * @return int
- * @todo set member id from session
- */
- public function getMemberId($get_type = 0)
- {
- $member_id = 0;
- $type = \Yunshop::request()->type ?: '';
- $token = \Yunshop::request()->yz_token ?: '';
- if ($get_type == 0 && request()->is_shop_pos && app('plugins')->isEnabled('shop-pos') && ($pos_uid = \Yunshop\ShopPos\services\CustomerService::getPosUid())) {
- $member_id = $pos_uid;
- } elseif ($type == 9) {
- $native_app = new \app\frontend\modules\member\services\MemberNativeAppService();
- $member_id = $native_app->getMemberId($token);
- } elseif ($type == 14) {
- $anchor_app = new MemberAnchorAppService();
- $member_id = $anchor_app->getMemberId($token);
- } elseif ($type == 15 && app('plugins')->isEnabled('aggregation-cps') && (!request()->appid || \Yunshop\AggregationCps\services\SettingManageService::getTrueKey() == request()->appid)) {
- $cps_app = new MemberCpsAppService();
- $member_id = $cps_app->getMemberId($token);
- } elseif (Session::get('member_id')) {
- $member_id = Session::get('member_id');
- }
- return $member_id;
- }
- }
- class YunPlugin
- {
- protected $values;
- public function __construct()
- {
- $this->values = false;
- }
- /**
- * @param null $key
- * @return bool
- */
- public function get($key = null)
- {
- if (isset($key)) {
- if (app('plugins')->isEnabled($key)) {
- return true;
- }
- }
- return $this->values;
- }
- }
- class YunNotice
- {
- protected $key;
- protected $value;
- public function __construct()
- {
- $this->key = 'shop';
- }
- /**
- * @param null $key
- * @return bool
- */
- public function getNotSend($routes = null)
- {
- $this->value = $routes;
- $routesData = explode('.', $routes);
- if (count($routesData) > 1) {
- $this->key = $routesData[0];
- $this->value = $routesData[1];
- }
- $noticeConfig = Config::get('notice.' . $this->key);
- return in_array($this->value, $noticeConfig) ? 0 : 1;
- }
- }
|