WechatBase.php
2.0 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php
namespace app\common\controller;
use app\common\library\Auth;
use EasyWeChat\Foundation\Application;
use think\Config;
use think\Controller;
use think\Hook;
use think\Lang;
use think\Loader;
/**
* 前台控制器基类
*/
class WechatBase extends Controller
{
/**
* 布局模板
* @var string
*/
protected $layout = '';
/**
* 无需登录的方法,同时也就不需要鉴权了
* @var array
*/
protected $noNeedLogin = [];
/**
* 无需鉴权的方法,但需要登录
* @var array
*/
protected $noNeedRight = [];
/**
* 权限Auth
* @var Auth
*/
protected $auth = null;
public function _initialize()
{
}
/**
* 加载语言文件
* @param string $name
*/
protected function loadlang($name)
{
Lang::load(APP_PATH . $this->request->module() . '/lang/' . $this->request->langset() . '/' . str_replace('.', '/', $name) . '.php');
}
/**
* 渲染配置信息
* @param mixed $name 键名或数组
* @param mixed $value 值
*/
protected function assignconfig($name, $value = '')
{
$this->view->config = array_merge($this->view->config ? $this->view->config : [], is_array($name) ? $name : [$name => $value]);
}
/**
* 检查微信用户登录
*/
public function checkWeChatUserLogin()
{
$userId = get_current_user_id();
if (empty($userId)) {
$config = [
'app_id' => config('wechat.app_id'),
'secret' => config('wechat.secret'),
'oauth' => [
'scopes' => ['snsapi_userinfo'],
'callback' => url('home/user/callback'),
],
];
$app = new Application($config);
$target_url='http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
session('target_url',$target_url);
$app->oauth->redirect()->send();
}
}
}