123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- <?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\service\GdtService;
- use think\Config;
- use think\Model;
- class Tdcaccount extends Backend
- {
- /**
- * @var Model
- */
- protected $model = null;
- protected $noNeedLogin = ["callback", "test"];
- public function _initialize()
- {
- parent::_initialize();
- $this->model = model('TdcAccount');
- }
- /**
- * 查看
- */
- 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) {
- $count += $v->delete();
- }
- 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");
- if (empty($code)) {
- $this->error("授权失败,没有接收到 authorization_code");
- }
- $admin_id = $this->request->param("applyid");
- $tdcConfig = Config::get("tdc");
- $callback = trim($tdcConfig['callback_host'], '/'). '/admin/tdcaccount/callback?applyid='.$admin_id;
- $reponse = GdtService::instance()->apiGetAccessToken($code, $tdcConfig, $callback);
- if (is_null($reponse)) {
- $this->error("授权失败");
- }
- if ($reponse['code'] != 0) {
- $this->error($reponse['message'], null, null, 300);
- }
- $result = $reponse['data'];
- $authorizerInfo = $result['authorizer_info'];
- //读取set_id
- $user_action_set_id = GdtService::instance()->apiUserActionSetsAdd($authorizerInfo['account_id'], $result['access_token']);
- $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']]);
- } 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/tdcaccount?ref=addtabs';
- $this->success("授权成功", $jumpUrl);
- }
- /**
- * 授权地址
- * @return string
- */
- private function getauthurl()
- {
- $tdcConfig = Config::get("tdc");
- if (empty($tdcConfig)) {
- $this->error(__('请先配置TDC应用信息'), null, null, 30);
- }
- $callback = trim($tdcConfig['callback_host'], '/'). '/admin/tdcaccount/callback?applyid='.$this->auth->id;
- $authUrl = "https://developers.e.qq.com/oauth/authorize?client_id=".$tdcConfig['client_id']."&redirect_uri=".urlencode($callback)."&state=&scope=&account_type=ACCOUNT_TYPE_WECHAT&account_display_number=2";
- return $authUrl;
- }
- }
|