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; } }