yunshop.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. <?php
  2. use app\frontend\modules\member\services\MemberAnchorAppService;
  3. use app\frontend\modules\member\services\MemberCpsAppService;
  4. use Illuminate\Support\Str;
  5. use app\common\services\PermissionService;
  6. use app\common\models\Menu;
  7. use app\common\services\Session;
  8. use app\common\exceptions\NotFoundException;
  9. //商城根目录
  10. define('SHOP_ROOT', dirname(__FILE__));
  11. class YunShop
  12. {
  13. private static $_req;
  14. private static $_app;
  15. private static $_plugin;
  16. private static $_notice;
  17. public function __construct()
  18. {
  19. }
  20. /**
  21. * Configures an object with the initial property values.
  22. * @param object $object the object to be configured
  23. * @param array $properties the property initial values given in terms of name-value pairs.
  24. * @return object the object itself
  25. */
  26. public static function configure($object, $properties)
  27. {
  28. foreach ($properties as $name => $value) {
  29. $object->$name = $value;
  30. }
  31. return $object;
  32. }
  33. public static function getAppNamespace()
  34. {
  35. $rootName = 'app';
  36. if (self::isWeb()) {
  37. $rootName .= '\\backend';
  38. }
  39. if (self::isApp() || self::isApi()) {
  40. $rootName .= '\\frontend';
  41. }
  42. return $rootName;
  43. }
  44. public static function getAppPath()
  45. {
  46. $path = dirname(__FILE__);
  47. if (self::isWeb()) {
  48. $path .= '/backend';
  49. }
  50. if (self::isApp() || self::isApi()) {
  51. $path .= '/frontend';
  52. }
  53. return $path;
  54. }
  55. public static function isPHPUnit()
  56. {
  57. return strpos($_SERVER['PHP_SELF'], 'phpunit') !== false ? true : false;
  58. }
  59. public static function isWeb()
  60. {
  61. return request()->isBackend();
  62. }
  63. public static function isApp()
  64. {
  65. if (self::isPHPUnit()) {
  66. return true;
  67. }
  68. return strpos($_SERVER['PHP_SELF'], '/app/index.php') !== false ? true : false;
  69. }
  70. public static function isApi()
  71. {
  72. return (strpos($_SERVER['PHP_SELF'], '/addons/') !== false &&
  73. strpos($_SERVER['PHP_SELF'], '/api.php') !== false) ? true : false;
  74. }
  75. /**
  76. *
  77. * @return bool
  78. */
  79. public static function isWechatApi()
  80. {
  81. if (config('app.framework') == 'platform') {
  82. return (strpos($_SERVER['REQUEST_URI'], '/wechat') !== false &&
  83. strpos($_SERVER['REQUEST_URI'], '/api') !== false) ? true : false;
  84. } else {
  85. return (strpos($_SERVER['PHP_SELF'], '/addons/') === false &&
  86. strpos($_SERVER['PHP_SELF'], '/api.php') !== false) ? true : false;
  87. }
  88. }
  89. /**
  90. * 是否插件
  91. * @return bool
  92. */
  93. public static function isPlugin()
  94. {
  95. if (config('app.framework') == 'platform') {
  96. return (strpos(request()->getRequestUri(), config('app.isWeb')) !== false &&
  97. strpos(request()->getRequestUri(), '/plugin') !== false) ? true : false;
  98. } else {
  99. return (strpos($_SERVER['PHP_SELF'], '/web/') !== false &&
  100. strpos($_SERVER['PHP_SELF'], '/plugin.php') !== false) ? true : false;
  101. }
  102. }
  103. /**
  104. * @name 验证是否商城操作员
  105. * @return array|bool|null|stdClass
  106. * @author
  107. */
  108. public static function isRole()
  109. {
  110. global $_W;
  111. if (app('plugins')->isEnabled('supplier')) {
  112. $res = \Illuminate\Support\Facades\DB::table('yz_supplier')->where('uid', $_W['uid'])->first();
  113. if (!$res) {
  114. return false;
  115. }
  116. return $res;
  117. }
  118. return false;
  119. }
  120. /**
  121. * @name 验证是否文章营销管理员
  122. * @author
  123. * @return array|bool|null|stdClass
  124. */
  125. public static function isArticle()
  126. {
  127. global $_W;
  128. if (app('plugins')->isEnabled('article')) {
  129. if (!\Illuminate\Support\Facades\Schema::hasTable('yz_plugin_article_manager')) {
  130. return false;
  131. }
  132. $res = \Illuminate\Support\Facades\DB::table('yz_plugin_article_manager')->where('uid', $_W['uid'])->first();
  133. if (!$res) {
  134. return false;
  135. }
  136. return $res;
  137. }
  138. return false;
  139. }
  140. /**
  141. * @name 验证是否商城操作员
  142. * @author
  143. * @return array|bool|null|stdClass
  144. */
  145. public static function isAgencyCompany()
  146. {
  147. global $_W;
  148. if (app('plugins')->isEnabled('agency-statistics')) {
  149. $res = \Illuminate\Support\Facades\DB::table('yz_agency_company')->where('uid', $_W['uid'])->first();
  150. if (!$res) {
  151. return false;
  152. }
  153. return $res;
  154. }
  155. return false;
  156. }
  157. public static function cleanApp()
  158. {
  159. self::$_app = null;
  160. }
  161. /**
  162. * @name 验证是否门店店长
  163. * @return array|bool|null|stdClass
  164. * @author
  165. */
  166. public static function isStore()
  167. {
  168. global $_W;
  169. if (app('plugins')->isEnabled('store-cashier')) {
  170. $res = \Illuminate\Support\Facades\DB::table('yz_store')->where('user_uid', $_W['uid'])->first();
  171. if (!$res) {
  172. return false;
  173. }
  174. return $res;
  175. }
  176. return false;
  177. }
  178. public static function isPayment()
  179. {
  180. return strpos($_SERVER['PHP_SELF'], '/payment/') > 0 ? true : false;
  181. }
  182. public static function request()
  183. {
  184. if (self::$_req !== null) {
  185. return self::$_req;
  186. } else {
  187. self::$_req = new YunRequest();
  188. return self::$_req;
  189. }
  190. }
  191. /**
  192. * @return YunApp
  193. */
  194. public static function app()
  195. {
  196. if (self::$_app !== null) {
  197. return self::$_app;
  198. } else {
  199. self::$_app = new YunApp();
  200. return self::$_app;
  201. }
  202. }
  203. /**
  204. * 解析路由
  205. *
  206. * 后台访问 /web/index.php?c=site&a=entry&m=sz_yi&do=xxx&route=module.controller.action
  207. * 前台 /app/index.php....
  208. *
  209. * 多字母的路由用中划线隔开 比如:
  210. * TestCacheController
  211. * function testClean()
  212. * 路由写法为 test-cache.test-clean
  213. *
  214. */
  215. // public static function parseRoute($requestRoute)
  216. // {
  217. // try {
  218. // $vers = [];
  219. // $routes_params = explode('.', $requestRoute);
  220. //
  221. // if (preg_match('/(v\d+)\./', $requestRoute, $vers)) {
  222. // foreach ($routes_params as $key => $item) {
  223. // if ($item != $vers[1]) {
  224. // $routes[] = $item;
  225. // }
  226. // }
  227. // } else {
  228. // $routes = $routes_params;
  229. // }
  230. //
  231. // $path = self::getAppPath();
  232. // $namespace = self::getAppNamespace();
  233. // $action = '';
  234. // $controllerName = '';
  235. // $currentRoutes = [];
  236. // $modules = [];
  237. //
  238. // if ($routes) {
  239. // $length = count($routes);
  240. // $routeFirst = array_first($routes);
  241. // $countRoute = count($routes);
  242. // if ($routeFirst === 'plugin' || self::isPlugin()) {
  243. // if (self::isPlugin()) {
  244. // $currentRoutes[] = 'plugin';
  245. // $countRoute += 1;
  246. // } else {
  247. // $currentRoutes[] = $routeFirst;
  248. // array_shift($routes);
  249. // }
  250. // $namespace = 'Yunshop';
  251. // $pluginName = array_shift($routes);
  252. // if ($pluginName || plugin($pluginName)) {
  253. // $currentRoutes[] = $pluginName;
  254. // $namespace .= '\\' . ucfirst(Str::camel($pluginName));
  255. // $path = base_path() . '/plugins/' . $pluginName . '/src';
  256. // $length = $countRoute;
  257. //
  258. // self::findRouteFile($controllerName, $action, $routes, $namespace, $path, $length, $currentRoutes, $requestRoute, true, $vers);
  259. //
  260. // if (!app('plugins')->isEnabled($pluginName)) {
  261. // throw new NotFoundException("{$pluginName}插件已禁用");
  262. //
  263. // }
  264. // } else {
  265. // throw new NotFoundException('无此插件');
  266. //
  267. // }
  268. // } else {
  269. //
  270. // self::findRouteFile($controllerName, $action, $routes, $namespace, $path, $length, $currentRoutes, $requestRoute, false, $vers);
  271. //
  272. // }
  273. // }
  274. // } catch (Exception $exception) {
  275. //// dd($exception);
  276. //// exit;
  277. //
  278. // }
  279. // //执行run
  280. // return static::run($namespace, $modules, $controllerName, $action, $currentRoutes);
  281. //
  282. // }
  283. /**
  284. * 定位路由相关文件
  285. * @param $controllerName
  286. * @param $action
  287. * @param $routes
  288. * @param $namespace
  289. * @param $path
  290. * @param $length
  291. * @param $requestRoute
  292. * @param $isPlugin
  293. */
  294. public static function findRouteFile(&$controllerName, &$action, $routes, &$namespace, &$path, $length, &$currentRoutes, $requestRoute, $isPlugin, $vers)
  295. {
  296. foreach ($routes as $k => $r) {
  297. $ucFirstRoute = ucfirst(Str::camel($r));
  298. if (empty($vers)) {
  299. $controllerFile = $path . ($isPlugin ? '/' : '/controllers/') . $ucFirstRoute . 'Controller.php';
  300. } else {
  301. $controllerFile = $path . ($isPlugin ? '/' : '/controllers/') . 'vers/' . $vers[1] . '/' . $ucFirstRoute . 'Controller.php';
  302. }
  303. if (is_file($controllerFile)) {
  304. if (empty($vers)) {
  305. $namespace .= ($isPlugin ? '' : '\\controllers') . '\\' . $ucFirstRoute . 'Controller';
  306. } else {
  307. $namespace .= ($isPlugin ? '\\' : '\\controllers\\') . 'vers\\' . $vers[1] . '\\' . $ucFirstRoute . 'Controller';
  308. }
  309. $controllerName = $ucFirstRoute;
  310. $path = $controllerFile;
  311. $currentRoutes[] = $r;
  312. } elseif (is_dir($path .= ($isPlugin ? '' : '/modules') . '/' . $r)) {
  313. $namespace .= ($isPlugin ? '' : '\\modules') . '\\' . $r;
  314. $modules[] = $r;
  315. $currentRoutes[] = $r;
  316. } else {
  317. if ($length !== ($isPlugin ? $k + 3 : $k + 1)) {
  318. throw new NotFoundException('路由长度有误:' . $requestRoute);
  319. }
  320. $action = strpos($r, '-') === false ? $r : Str::camel($r);
  321. $currentRoutes[] = $r;
  322. }
  323. }
  324. }
  325. public static function getUcfirstName($name)
  326. {
  327. if (strpos($name, '-')) {
  328. $names = explode('-', $name);
  329. $name = '';
  330. foreach ($names as $v) {
  331. $name .= ucfirst($v);
  332. }
  333. }
  334. return ucfirst($name);
  335. }
  336. public static function plugin()
  337. {
  338. self::$_plugin = new YunPlugin();
  339. return self::$_plugin;
  340. }
  341. public static function notice()
  342. {
  343. self::$_notice = new YunNotice();
  344. return self::$_notice;
  345. }
  346. private static function getContent($controller, $action)
  347. {
  348. return (new \Illuminate\Pipeline\Pipeline(new \Illuminate\Container\Container()))
  349. ->send(Illuminate\Http\Request::capture())
  350. ->through(collect($controller->getMiddleware())->pluck('middleware')->all())
  351. ->then(function ($request) use ($controller, $action) {
  352. return $controller->$action($request);
  353. });
  354. }
  355. }
  356. class YunComponent implements ArrayAccess
  357. {
  358. protected $values = [];
  359. public function __set($name, $value)
  360. {
  361. return $this->values[$name] = $value;
  362. }
  363. public function __get($name)
  364. {
  365. if (!array_key_exists($name, $this->values)) {
  366. $this->values[$name] = null;
  367. }
  368. return $this->values[$name];
  369. }
  370. function __isset($name)
  371. {
  372. return array_key_exists($name, $this->values);
  373. }
  374. public function set($name, $value)
  375. {
  376. $this->values[$name] = $value;
  377. return $this;
  378. }
  379. public function get($key = null)
  380. {
  381. if (isset($key)) {
  382. $result = json_decode(array_get($this->values, $key, null), true);
  383. if (@is_array($result)) {
  384. return $result;
  385. }
  386. return array_get($this->values, $key, null);
  387. }
  388. return $this->values;
  389. }
  390. public function offsetUnset($offset)
  391. {
  392. unset($this->values[$offset]);
  393. }
  394. public function offsetSet($offset, $value)
  395. {
  396. $this->values[$offset] = $value;
  397. }
  398. public function offsetGet($offset)
  399. {
  400. if (isset($this->values[$offset])) {
  401. return $this->values[$offset];
  402. }
  403. return null;
  404. }
  405. public function offsetExists($offset)
  406. {
  407. if (isset($this->values[$offset])) {
  408. return true;
  409. }
  410. return false;
  411. }
  412. }
  413. class YunRequest extends YunComponent
  414. {
  415. public function __construct()
  416. {
  417. $this->values = request()->input();
  418. }
  419. }
  420. /**
  421. * Class YunApp
  422. * @property int uniacid
  423. * @property int uid
  424. */
  425. class YunApp extends YunComponent
  426. {
  427. protected $values;
  428. public $currentItems = [];
  429. public function __construct()
  430. {
  431. global $_W;
  432. $this->values = !YunShop::isWeb() && !YunShop::isWechatApi() ? $this->getW() : (array)$_W;
  433. }
  434. public function getW()
  435. {
  436. $uniacid = intval(trim(request()->get('i')));
  437. $account = \app\common\models\AccountWechats::getAccountByUniacid($uniacid);
  438. return [
  439. 'uniacid' => $uniacid,
  440. 'weid' => $uniacid,
  441. 'acid' => $uniacid,
  442. 'account' => $account ? $account->toArray() : '',
  443. ];
  444. }
  445. /**
  446. * @return int
  447. * @todo set member id from session
  448. */
  449. public function getMemberId($get_type = 0)
  450. {
  451. $member_id = 0;
  452. $type = \Yunshop::request()->type ?: '';
  453. $token = \Yunshop::request()->yz_token ?: '';
  454. if ($get_type == 0 && request()->is_shop_pos && app('plugins')->isEnabled('shop-pos') && ($pos_uid = \Yunshop\ShopPos\services\CustomerService::getPosUid())) {
  455. $member_id = $pos_uid;
  456. } elseif ($type == 9) {
  457. $native_app = new \app\frontend\modules\member\services\MemberNativeAppService();
  458. $member_id = $native_app->getMemberId($token);
  459. } elseif ($type == 14) {
  460. $anchor_app = new MemberAnchorAppService();
  461. $member_id = $anchor_app->getMemberId($token);
  462. } elseif ($type == 15 && app('plugins')->isEnabled('aggregation-cps') && (!request()->appid || \Yunshop\AggregationCps\services\SettingManageService::getTrueKey() == request()->appid)) {
  463. $cps_app = new MemberCpsAppService();
  464. $member_id = $cps_app->getMemberId($token);
  465. } elseif (Session::get('member_id')) {
  466. $member_id = Session::get('member_id');
  467. }
  468. return $member_id;
  469. }
  470. }
  471. class YunPlugin
  472. {
  473. protected $values;
  474. public function __construct()
  475. {
  476. $this->values = false;
  477. }
  478. /**
  479. * @param null $key
  480. * @return bool
  481. */
  482. public function get($key = null)
  483. {
  484. if (isset($key)) {
  485. if (app('plugins')->isEnabled($key)) {
  486. return true;
  487. }
  488. }
  489. return $this->values;
  490. }
  491. }
  492. class YunNotice
  493. {
  494. protected $key;
  495. protected $value;
  496. public function __construct()
  497. {
  498. $this->key = 'shop';
  499. }
  500. /**
  501. * @param null $key
  502. * @return bool
  503. */
  504. public function getNotSend($routes = null)
  505. {
  506. $this->value = $routes;
  507. $routesData = explode('.', $routes);
  508. if (count($routesData) > 1) {
  509. $this->key = $routesData[0];
  510. $this->value = $routesData[1];
  511. }
  512. $noticeConfig = Config::get('notice.' . $this->key);
  513. return in_array($this->value, $noticeConfig) ? 0 : 1;
  514. }
  515. }