123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- <?php
- /**
- * Created by: PhpStorm
- * User: lytian
- * Date: 2020/2/27
- * Time: 14:02
- */
- namespace app\admin\controller;
- use app\common\controller\Backend;
- use app\common\library\Redis;
- use app\main\constants\AdminConstants;
- use app\main\service\AdminService;
- use app\main\service\GdtService;
- use think\Config;
- use think\Model;
- class Gdtaccount extends Backend
- {
- /**
- * @var Model
- */
- protected $model = null;
- protected $noNeedLogin = ["callback", "test", "callbacktime"];
- public function _initialize()
- {
- parent::_initialize();
- $this->model = model('GdtAccount');
- }
- /**
- * 查看
- */
- public function index()
- {
- //设置过滤方法
- $this->request->filter(['strip_tags']);
- list($where, $sort, $order, $offset, $limit) = $this->buildparams();
- $maps = [
- 'admin_id' => ['eq', $this->auth->id],
- ];
- $total = $this->model
- ->where($where)
- ->where($maps)
- ->order($sort, $order)
- ->count();
- if ($this->request->isAjax()) {
- //如果发送的来源是Selectpage,则转发到Selectpage
- if ($this->request->request('pkey_name')) {
- return $this->selectpage();
- }
- $list = $this->model
- ->where($where)
- ->where($maps)
- ->order($sort, $order)
- ->limit($offset, $limit)
- ->select();
- $result = array("total" => $total, "rows" => $list);
- return json($result);
- }
- if ($total == 0) {
- $authUrl = $this->getauthurl();
- $this->assign('auth_url', $authUrl);
- }
- $this->assign("total", $total);
- return $this->view->fetch();
- }
- /**
- * 删除
- */
- public function del($ids = "")
- {
- if ($ids) {
- $pk = $this->model->getPk();
- $adminIds = $this->getDataLimitAdminIds();
- if (is_array($adminIds)) {
- $count = $this->model->where($this->dataLimitField, 'in', $adminIds);
- }
- $list = $this->model->where($pk, 'in', $ids)->select();
- $count = 0;
- foreach ($list as $k => $v) {
- if ($this->model->update(['admin_id' => 0, 'updatetime' => time()], ['id' => $v['id']])) {
- Redis::instance()->del("GDTI:".$v['admin_id']);
- $count ++;
- }
- }
- if ($count) {
- $this->success();
- } else {
- $this->error(__('No rows were deleted'));
- }
- }
- $this->error(__('Parameter %s can not be empty', 'ids'));
- }
- /**
- * 授权回调
- */
- public function callback()
- {
- $code = $this->request->param("authorization_code");
- $admin_id = $this->request->param("applyid");
- if (empty($code)) {
- $this->error("授权失败,没有接收到 authorization_code");
- }
- $gdtConfig = Config::get("gdt");
- $redirect_uri = trim($gdtConfig['callback_host'], '/'). '/admin/gdtaccount/callback?applyid='.($this->auth->id ?: $admin_id);
- $reponse = GdtService::instance()->apiGetAccessToken($code, $gdtConfig, $redirect_uri);
- if (is_null($reponse)) {
- $this->error("授权失败");
- }
- if ($reponse['code'] != 0) {
- $this->error($reponse['message'], null, null, 300);
- }
- $result = $reponse['data'];
- $authorizerInfo = $result['authorizer_info'];
- $data = [
- 'admin_id' => $this->auth->id ?: $admin_id,
- 'access_token' => $result['access_token'],
- 'refresh_token' => $result['refresh_token'],
- 'access_token_expire_time' => time() + 85400,
- 'authorizer_info' => json_encode($authorizerInfo, JSON_UNESCAPED_UNICODE),
- 'updatetime' => time(),
- ];
- $row = $this->model->where('account_id', 'eq', $authorizerInfo['account_id'])->find();
- if ($row) {
- //已存在了进行更新
- $this->model->update($data, ['id' => $row['id']]);
- Redis::instance()->del("GDTI:".$row['admin_id']);
- } else {
- $data['account_id'] = $authorizerInfo['account_id'];
- $data['createtime'] = time();
- $this->model->allowField(true)->insertGetId($data);
- }
- //授权成功 跳转页面
- $jumpUrl = Config::get("site.scheme")."://".trim(Config::get("site.url_root"), '/').'/admin/gdtaccount?ref=addtabs';
- $this->success("授权成功", $jumpUrl);
- }
- /**
- * 授权地址
- * @return string
- */
- private function getauthurl()
- {
- $gdtConfig = Config::get("gdt");
- if (empty($gdtConfig)) {
- $this->error(__('请先配置GDT信息'), null, null, 30);
- }
- $callback = trim($gdtConfig['callback_host'], '/'). '/admin/gdtaccount/callback?applyid='.$this->auth->id;
- $authUrl = "https://developers.e.qq.com/oauth/authorize?client_id=".$gdtConfig['client_id']."&redirect_uri=".urlencode($callback)."&state=&scope=&account_type=ACCOUNT_TYPE_WECHAT&account_display_number=2";
- return $authUrl;
- }
- public function callbacktime()
- {
- $adminConfig = AdminService::instance()->getAdminConfigModel()->getAdminInfoAll($this->auth->id);
- if ($this->request->isAjax()) {
- AdminService::instance()->getAdminConfigModel()->update($this->request->post(), ['admin_id'=>$this->auth->id]);
- model('AdminConfig')->delAdminInfoAllCache($this->auth->id);
- $this->success();
- }
- $this->view->assign('admin', $adminConfig);
- $adminConfig['callback_time_mp'] = $adminConfig['callback_time_gdt'] ?? AdminConstants::CALLBACK_TIME_ONCE_24;
- return $this->view->fetch();
- }
- }
|