ServiceController.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. <?php
  2. namespace addons\WechatVideoShop\api\modules\v1\controllers;
  3. use yii\web\Controller;
  4. class ServiceController extends Controller
  5. {
  6. public $enableCsrfValidation = false;
  7. public function actionCallback()
  8. {
  9. $params = $this->request->get();
  10. $encryptData = $this->request->post();
  11. \Yii::error(__METHOD__ . '->微信服务市场异步: ' . var_export($params, true) . '->encryptData: ' . var_export($encryptData, true));
  12. // 校验token
  13. if (\Yii::$app->request->isGet && !empty(\Yii::$app->request->get('echostr'))) {
  14. // 校验数据签名
  15. $data = $this->request->get();
  16. $valid = \Yii::$app->services->validate->validate($data, [
  17. [['signature', 'timestamp', 'nonce', 'echostr'], 'required'],
  18. ]);
  19. \Yii::error(__METHOD__ . '->params: ' . var_export($data, true));
  20. if ($valid === false) return false;
  21. $signature = $data["signature"];
  22. $timestamp = $data["timestamp"];
  23. $nonce = $data["nonce"];
  24. $tmpArr = array('zaNpZPZiemLXY6mHkZRpR2dBv6RgWce', $timestamp, $nonce);
  25. sort($tmpArr, SORT_STRING);
  26. $tmpStr = implode($tmpArr);
  27. $tmpStr = sha1($tmpStr);
  28. if ($tmpStr == $signature) {
  29. return $data['echostr'];
  30. } else {
  31. return 'fail';
  32. }
  33. }
  34. }
  35. }