Gdtaccount.php 5.8 KB

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