Tdcaccount.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. /**
  3. * Created by: PhpStorm
  4. * User: lytian
  5. * Date: 2020/2/27
  6. * Time: 14:02
  7. */
  8. namespace app\admin\controller;
  9. use app\common\controller\Backend;
  10. use app\common\library\Redis;
  11. use app\main\service\GdtService;
  12. use think\Config;
  13. use think\Model;
  14. class Tdcaccount extends Backend
  15. {
  16. /**
  17. * @var Model
  18. */
  19. protected $model = null;
  20. protected $noNeedLogin = ["callback", "test"];
  21. public function _initialize()
  22. {
  23. parent::_initialize();
  24. $this->model = model('TdcAccount');
  25. }
  26. /**
  27. * 查看
  28. */
  29. public function index()
  30. {
  31. //设置过滤方法
  32. $this->request->filter(['strip_tags']);
  33. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  34. $maps = [
  35. //'admin_id' => ['eq', $this->auth->id],
  36. ];
  37. $total = $this->model
  38. ->where($where)
  39. ->where($maps)
  40. ->order($sort, $order)
  41. ->count();
  42. if ($this->request->isAjax()) {
  43. //如果发送的来源是Selectpage,则转发到Selectpage
  44. if ($this->request->request('pkey_name')) {
  45. return $this->selectpage();
  46. }
  47. $list = $this->model
  48. ->where($where)
  49. ->where($maps)
  50. ->order($sort, $order)
  51. ->limit($offset, $limit)
  52. ->select();
  53. $result = array("total" => $total, "rows" => $list);
  54. return json($result);
  55. }
  56. if ($total == 0) {
  57. $authUrl = $this->getauthurl();
  58. $this->assign('auth_url', $authUrl);
  59. }
  60. $this->assign("total", $total);
  61. return $this->view->fetch();
  62. }
  63. /**
  64. * 删除
  65. */
  66. public function del($ids = "")
  67. {
  68. if ($ids) {
  69. $pk = $this->model->getPk();
  70. $adminIds = $this->getDataLimitAdminIds();
  71. if (is_array($adminIds)) {
  72. $count = $this->model->where($this->dataLimitField, 'in', $adminIds);
  73. }
  74. $list = $this->model->where($pk, 'in', $ids)->select();
  75. $count = 0;
  76. foreach ($list as $k => $v) {
  77. $count += $v->delete();
  78. }
  79. if ($count) {
  80. $this->success();
  81. } else {
  82. $this->error(__('No rows were deleted'));
  83. }
  84. }
  85. $this->error(__('Parameter %s can not be empty', 'ids'));
  86. }
  87. /**
  88. * 授权回调
  89. */
  90. public function callback()
  91. {
  92. $code = $this->request->param("authorization_code");
  93. if (empty($code)) {
  94. $this->error("授权失败,没有接收到 authorization_code");
  95. }
  96. $admin_id = $this->request->param("applyid");
  97. $tdcConfig = Config::get("tdc");
  98. $callback = trim($tdcConfig['callback_host'], '/'). '/admin/tdcaccount/callback?applyid='.$admin_id;
  99. $reponse = GdtService::instance()->apiGetAccessToken($code, $tdcConfig, $callback);
  100. if (is_null($reponse)) {
  101. $this->error("授权失败");
  102. }
  103. if ($reponse['code'] != 0) {
  104. $this->error($reponse['message'], null, null, 300);
  105. }
  106. $result = $reponse['data'];
  107. $authorizerInfo = $result['authorizer_info'];
  108. //读取set_id
  109. $user_action_set_id = GdtService::instance()->apiUserActionSetsAdd($authorizerInfo['account_id'], $result['access_token']);
  110. $data = [
  111. 'admin_id' => $this->auth->id ?: $admin_id,
  112. 'access_token' => $result['access_token'],
  113. 'refresh_token' => $result['refresh_token'],
  114. 'access_token_expire_time' => time() + 85400,
  115. 'authorizer_info' => json_encode($authorizerInfo, JSON_UNESCAPED_UNICODE),
  116. 'updatetime' => time(),
  117. ];
  118. $row = $this->model->where('account_id', 'eq', $authorizerInfo['account_id'])->find();
  119. if ($row) {
  120. //已存在了进行更新
  121. $this->model->update($data, ['id' => $row['id']]);
  122. } else {
  123. $data['account_id'] = $authorizerInfo['account_id'];
  124. $data['createtime'] = time();
  125. $this->model->allowField(true)->insertGetId($data);
  126. }
  127. //授权成功 跳转页面
  128. $jumpUrl = Config::get("site.scheme")."://".trim(Config::get("site.url_root"), '/').'/admin/tdcaccount?ref=addtabs';
  129. $this->success("授权成功", $jumpUrl);
  130. }
  131. /**
  132. * 授权地址
  133. * @return string
  134. */
  135. private function getauthurl()
  136. {
  137. $tdcConfig = Config::get("tdc");
  138. if (empty($tdcConfig)) {
  139. $this->error(__('请先配置TDC应用信息'), null, null, 30);
  140. }
  141. $callback = trim($tdcConfig['callback_host'], '/'). '/admin/tdcaccount/callback?applyid='.$this->auth->id;
  142. $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";
  143. return $authUrl;
  144. }
  145. }