Base.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. namespace addons\shop\controller;
  3. use addons\shop\model\Carts;
  4. use think\Validate;
  5. /**
  6. * 商城控制器基类
  7. */
  8. class Base extends \think\addons\Controller
  9. {
  10. // 初始化
  11. public function __construct()
  12. {
  13. parent::__construct();
  14. $config = get_addon_config('shop');
  15. // 设定主题模板目录
  16. $this->view->engine->config('view_path', $this->view->engine->config('view_path') . $config['theme'] . DS);
  17. // 加载自定义标签库
  18. $this->view->engine->config('taglib_pre_load', 'addons\shop\taglib\Shop');
  19. // 默认渲染栏目为空
  20. $this->view->assign('__CHANNEL__', null);
  21. $this->view->assign('isWechat', strpos($this->request->server('HTTP_USER_AGENT'), 'MicroMessenger') !== false);
  22. // 定义SHOP首页的URL
  23. $config['indexurl'] = addon_url('shop/index/index', [], false);
  24. $config['cartnums'] = $this->auth->id ? Carts::where('user_id', $this->auth->id)->where('sceneval', 1)->count() : 0;
  25. \think\Config::set('shop', $config);
  26. //判断站点状态
  27. if (isset($config['openedsite']) && !in_array('pc', explode(',', $config['openedsite']))) {
  28. if ($this->controller != 'order' && $this->action != 'epay') {
  29. $this->error('站点已关闭');
  30. }
  31. }
  32. }
  33. public function _initialize()
  34. {
  35. parent::_initialize();
  36. // 如果请求参数action的值为一个方法名,则直接调用
  37. $action = $this->request->post("action");
  38. if ($action && $this->request->isPost()) {
  39. return $this->$action();
  40. }
  41. }
  42. protected function token()
  43. {
  44. $token = $this->request->post('__token__');
  45. //验证Token
  46. if (!Validate::make()->check(['__token__' => $token], ['__token__' => 'require|token'])) {
  47. $this->error("Token验证错误,请重试!", '', ['__token__' => $this->request->token()]);
  48. }
  49. //刷新Token
  50. $this->request->token();
  51. }
  52. // 判断是否跳转移动H5
  53. protected function checkredirect($type, $params = [])
  54. {
  55. $config = get_addon_config('shop');
  56. //自动跳转H5
  57. if (($config['autoredirectmobile'] ?? false) && ($config['mobileurl'] ?? '') && $this->request->isMobile()) {
  58. $pageArr = [
  59. 'cart' => "/pages/cart/cart",
  60. 'goods/goods' => "/pages/goods/goods",
  61. 'goods/detail' => "/pages/goods/detail",
  62. 'coupon/list' => "/pages/coupon/coupon",
  63. 'coupon/detail' => "/pages/coupon/detail",
  64. 'exchange/list' => "/pages/score/exchange",
  65. ];
  66. $pageUrl = $pageArr[$type] ?? '';
  67. $params = array_filter($params);
  68. $this->redirect($config['mobileurl'] . "#" . $pageUrl . ($params ? '?' . http_build_query($params) : ''));
  69. } else {
  70. return;
  71. }
  72. }
  73. }