Skip to content

Commit 2ff2c13

Browse files
committed
access token in cron
1 parent faf0102 commit 2ff2c13

File tree

7 files changed

+133
-106
lines changed

7 files changed

+133
-106
lines changed

‎modules/custom/demo_test/src/Form/DtForm.php‎

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
namespace Drupal\demo_test\Form;
44

55

6+
use Drupal\wechat_api\Service\WechatApiService;
67
use Drupal\Core\Form\FormBase;
78
use Drupal\Core\Form\FormStateInterface;
89
use Drupal\Core\Entity\EntityStorageInterface;
910
use Drupal\Core\Entity\EntityInterface;
11+
use Symfony\Component\DependencyInjection\ContainerInterface;
1012

1113
/**
1214
* Implements the SimpleForm form controller.
@@ -18,6 +20,18 @@
1820
*/
1921
class DtForm extends FormBase {
2022

23+
protected $wechat_api;
24+
25+
public static function create(ContainerInterface $container) {
26+
return new static(
27+
$container->get('service.wechatapi')
28+
);
29+
}
30+
31+
public function __construct(WechatApiService $service) {
32+
$this->wechat_api = $service;
33+
}
34+
2135
/**
2236
* Build the simple form.
2337
*
@@ -32,34 +46,38 @@ class DtForm extends FormBase {
3246
* @return array
3347
* The render array defining the elements of the form.
3448
*/
35-
public function buildForm(array $form, FormStateInterface $form_state) {
49+
public function buildForm(array $form, FormStateInterface $form_state) {
3650

37-
$form['title'] = [
38-
'#type' => 'textfield',
39-
'#title' => $this->t('Title'),
40-
'#description' => $this->t('Title must be at least 5 characters in length.'),
41-
'#required' => TRUE,
42-
];
51+
$form['title'] = [
52+
'#type' => 'textfield',
53+
'#title' => $this->t('Title'),
54+
'#description' => $this->t('Title must be at least 5 characters in length.'),
55+
'#required' => TRUE,
56+
];
4357

4458

45-
$node = \Drupal::entityManager()->getStorage('node')->load(16)->toArray();
59+
$node = \Drupal::entityManager()->getStorage('node')->load(16)->toArray();
4660

47-
//dpm($node);
48-
$ref_ids = $node['field_ref_wechat_api'];
49-
//dpm($ref_ids);
61+
//dpm($node);
62+
$ref_ids = $node['field_ref_wechat_api'];
63+
//dpm($ref_ids);
5064

51-
$token = \Drupal::config('dld.wxapp.config')->get('get access token');
52-
$AppID = \Drupal::config('dld.wxapp.config')->get('AppID');
53-
$AppSecret = \Drupal::config('dld.wxapp.config')->get('AppSecret');
54-
$token_url = t( $token, array( '@APPID' => $AppID, '@APPSECRET' => $AppSecret) )->render();
65+
// $token = \Drupal::config('dld.wxapp.config')->get('get access token');
66+
// $AppID = \Drupal::config('dld.wxapp.config')->get('AppID');
67+
// $AppSecret = \Drupal::config('dld.wxapp.config')->get('AppSecret');
68+
// $token_url = t( $token, array( '@APPID' => $AppID, '@APPSECRET' => $AppSecret) )->render();
5569

56-
dpm($token_url);
70+
//dpm($token_url);
5771

58-
$service = \Drupal::service('service.wechatapi');
59-
dpm($service->getName()->render());
72+
//$service = \Drupal::service('service.wechatapi');
6073

61-
return $form;
62-
}
74+
dpm($this->wechat_api->get_access_token());
75+
76+
// $result = $this->wechat_api->wechat_php_curl_https_get($token_url);
77+
// \Drupal::logger('DtForm')->notice( 'data: <pre>@data</pre>', array('@data' => print_r($result, true)) );
78+
79+
return $form;
80+
}
6381

6482
/**
6583
* Getter method for Form ID.
@@ -71,9 +89,9 @@ public function buildForm(array $form, FormStateInterface $form_state) {
7189
* @return string
7290
* The unique ID of the form defined by this class.
7391
*/
74-
public function getFormId() {
75-
return 'demo_test_form';
76-
}
92+
public function getFormId() {
93+
return 'demo_test_form';
94+
}
7795

7896
/**
7997
* Implements form validation.

‎modules/custom/dld_wxapp_cron/src/Plugin/QueueWorker/dld_wxapp_QueueToken.php‎

Lines changed: 42 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
namespace Drupal\dld_wxapp_cron\Plugin\QueueWorker;
44

5+
use Drupal\wechat_api\Service\WechatApiService;
56
use Drupal\Core\Queue\QueueWorkerBase;
67
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
78
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
9+
use Drupal\Core\Config\ConfigFactoryInterface;
810
use Symfony\Component\DependencyInjection\ContainerInterface;
911

1012
/**
@@ -25,65 +27,74 @@ class dld_wxapp_QueueToken extends QueueWorkerBase implements ContainerFactoryPl
2527
* @var \Psr\Log\LoggerInterface
2628
*/
2729
protected $logger;
28-
29-
/**
30-
* dld_wxapp_QueueToken constructor.
31-
*
32-
* @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $logger
33-
* The logger service the instance should use.
34-
*/
35-
//public function __construct(array $configuration, $plugin_id, $plugin_definition, LoggerChannelFactoryInterface $logger) {
36-
public function __construct(LoggerChannelFactoryInterface $logger) {
37-
//parent::__construct($configuration, $plugin_id, $plugin_definition);
38-
$this->logger = $logger;
39-
}
30+
protected $wechatApi;
31+
protected $configFactory;
4032

4133
/**
4234
* {@inheritdoc}
4335
*/
4436
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
45-
//public static function create(ContainerInterface $container) {
4637

4738
// \Drupal::logger('dld_wxapp_QueueToken')->notice(
4839
// 'configuration configuration: <pre>@configuration</pre>,
4940
// plugin_id: @plugin_id, plugin_definition: @plugin_definition',
50-
// array('@configuration' => $configuration,
41+
// array('@configuration' => print_r($configuration, true),
5142
// '@plugin_id' => $plugin_id,
5243
// '@plugin_definition' => $plugin_definition
5344
// )
5445
// );
46+
5547
return new static(
56-
$container->get('logger.factory')
48+
$configuration,
49+
$plugin_id,
50+
$plugin_definition,
51+
$container->get('logger.factory'),
52+
$container->get('config.factory'),
53+
$container->get('service.wechatapi')
5754
);
5855
}
5956

57+
/**
58+
* dld_wxapp_QueueToken constructor.
59+
*
60+
* @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $logger
61+
* The logger service the instance should use.
62+
*/
63+
public function __construct(array $configuration,
64+
$plugin_id,
65+
$plugin_definition,
66+
LoggerChannelFactoryInterface $logger,
67+
ConfigFactoryInterface $config_factory,
68+
WechatApiService $service ) {
69+
70+
parent::__construct($configuration, $plugin_id, $plugin_definition, $logger, $config_factory);
71+
72+
$this->logger = $logger;
73+
$this->configFactory = $config_factory;
74+
$this->wechatApi = $service;
75+
}
76+
6077
/**
6178
* {@inheritdoc}
6279
*/
6380
public function processItem($data) {
6481

6582
if(time() >= $data['lastrun'] + $data['interval']) {
6683

67-
$this->logger->get('dld_wxapp_QueueToken')->notice('execute access token');
68-
84+
//$this->logger->get('dld_wxapp_QueueToken')->notice('execute access token');
85+
86+
if ( $access_token = $this->wechatApi->get_access_token() ) {
87+
//return token
88+
$config = $this->configFactory->getEditable('dld.wxapp.config');
89+
$config->set('access_token', $access_token)->save();
90+
}
91+
6992
// Set last run execute time.
70-
$config = \Drupal::service('config.factory')->getEditable('dld_wxapp_cron.settings');
93+
$config = $this->configFactory->getEditable('dld_wxapp_cron.settings');
94+
//$config = \Drupal::service('config.factory')->getEditable('dld_wxapp_cron.settings');
7195
$config->set('LastRun', time())->save();
7296
}
7397

74-
$token = \Drupal::config('dld.wxapp.config')->get('get access token');
75-
$AppID = \Drupal::config('dld.wxapp.config')->get('AppID');
76-
$AppSecret = \Drupal::config('dld.wxapp.config')->get('AppSecret');
77-
$token_url = t( $token, array( '@APPID' => $AppID, '@APPSECRET' => $AppSecret) )->render();
78-
79-
// $this->logger->get('dld_wxapp_QueueToken')->notice('time: ' . time());
80-
// $this->logger->get('dld_wxapp_QueueToken')->notice('last_run: ' . $this->last_run);
81-
// $this->logger->get('dld_wxapp_QueueToken')->notice($data);
82-
// $this->logger->get('dld_wxapp_QueueToken')->notice('my cron queue worker');
83-
//\Drupal::logger('dld_wxapp_QueueToken')->notice($data);
84-
85-
// We access our configuration.
86-
//$cron_config = \Drupal::configFactory()->getEditable('examples.cron');
8798
}
8899

89100
}

‎modules/custom/wechat_api/src/Controller/WechatApiController.php‎

Lines changed: 0 additions & 39 deletions
This file was deleted.

‎modules/custom/wechat_api/src/Service/WechatApiService.php‎

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,23 @@
33
namespace Drupal\wechat_api\Service;
44

55
// These classes are used to implement a stream wrapper class.
6-
use Drupal\Component\Utility\Html;
7-
use Drupal\Core\Routing\UrlGeneratorTrait;
6+
use Drupal\Core\Config\ConfigFactoryInterface;
7+
use Drupal\Core\Logger\LoggerChannelFactory;
88

99
/**
1010
* wechat api service for all wechat api interface and include curl http interface
1111
**/
1212
class WechatApiService {
1313

14+
protected $logger;
15+
protected $configFactory;
16+
17+
public function __construct(ConfigFactoryInterface $config_factory, LoggerChannelFactory $loggerFactory) {
18+
$this->configFactory = $config_factory;
19+
$this->logger = $loggerFactory->get('WechatApiService');
20+
21+
}
22+
1423
/**
1524
* Implment php5 curl api to get https url
1625
*/
@@ -88,10 +97,45 @@ public function wechat_php_curl_https_post($url, $postfields, $ct = '') {
8897
* The stream wrapper name.
8998
**/
9099
public function getName() {
91-
return t('File Example Session files');
100+
$token = $this->configFactory->get('dld.wxapp.config')->get('AppID');
101+
//\Drupal::logger('WechatApiService')->critical('logger ok');
102+
$this->logger->notice('notice logger');
103+
return $token;
92104
}
93105

106+
public function get_jsapi_ticket() {
107+
}
108+
109+
/**
110+
* public get wechat general access token
111+
**/
112+
public function get_access_token() {
113+
114+
$token = $this->configFactory->get('dld.wxapp.config')->get('get access token');
115+
$AppID = $this->configFactory->get('dld.wxapp.config')->get('AppID');
116+
$AppSecret = $this->configFactory->get('dld.wxapp.config')->get('AppSecret');
117+
$token_url = t( $token, array( '@APPID' => $AppID, '@APPSECRET' => $AppSecret) )->render();
94118

95-
/*********************/
119+
$result = $this->wechat_php_curl_https_get($token_url);
120+
121+
if (!$result) {
122+
123+
$this->logger->notice("get_access_token: can't get result");
124+
return FALSE;
125+
}
126+
127+
$json_data = json_decode($result);
128+
if ( isset($json_data->errcode) ) {
129+
$this->logger->error("get_access_token: errcode @error and errmsg @errmsg",
130+
array(
131+
'@error' => $json_data->errcode,
132+
'@errmsg' => $json_data->errmsg
133+
)
134+
);
135+
return FALSE;
136+
}
137+
138+
return $json_data->access_token;
139+
}
96140

97141
}

‎modules/custom/wechat_api/wechat_api.routing.yml.bak‎

Lines changed: 0 additions & 8 deletions
This file was deleted.

‎modules/custom/wechat_api/wechat_api.services.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ services:
1010
# the name of the module that implements them.
1111
service.wechatapi:
1212
# Point to the class that will contain your implementation of
13-
# \Symfony\Component\EventDispatcher\EventSubscriberInterface
1413
class: Drupal\wechat_api\Service\WechatApiService
14+
arguments: ['@config.factory', '@logger.factory']
1515
tags:
1616
- {name: wechatapi}

‎modules/custom/wechat_service/src/Controller/WechatServiceController.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public function access() {
1818
if(isset($echostr) || !is_null($echostr) || !empty($echostr)){
1919
echo $echostr;
2020
\Drupal::logger('WechatServiceController')->notice('@data', array('@data' => $echostr));
21+
2122
return new Response(null, 200, array());
2223
}
2324
// \Drupal::logger('WechatServiceController')->notice('hello');

0 commit comments

Comments
 (0)