wxapp.platform.class.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. /**
  3. * [WeEngine System] Copyright (c) 2014 WE7.CC
  4. * WeEngine is NOT a free software, it under the license terms, visited http://www.we7.cc/ for more details.
  5. */
  6. defined('IN_IA') or exit('Access Denied');
  7. load()->classs('weixin.platform');
  8. class WxappPlatform extends WeixinPlatform {
  9. const JSCODEURL = 'https://api.weixin.qq.com/sns/component/jscode2session?appid=%s&js_code=%s&grant_type=authorization_code&component_appid=%s&component_access_token=%s';
  10. public $appid;
  11. protected $appsecret;
  12. public $encodingaeskey;
  13. public $token;
  14. protected $refreshtoken;
  15. protected $tablename = 'account_wxapp';
  16. protected $menuFrame = 'wxapp';
  17. protected $type = ACCOUNT_TYPE_APP_AUTH;
  18. protected $typeName = '小程序';
  19. protected $typeSign = WXAPP_TYPE_SIGN;
  20. protected $supportVersion = STATUS_ON;
  21. protected $typeTempalte = '-wxapp';
  22. public function __construct($uniaccount = array()) {
  23. $setting = setting_load('platform');
  24. $this->appid = $setting['platform']['appid'];
  25. $this->appsecret = $setting['platform']['appsecret'];
  26. $this->token = $setting['platform']['token'];
  27. $this->encodingaeskey = $setting['platform']['encodingaeskey'];
  28. parent::__construct($uniaccount);
  29. }
  30. protected function getAccountInfo($acid) {
  31. if ($this->account['key'] == 'wx570bc396a51b8ff8') {
  32. $this->account['key'] = $this->appid;
  33. $this->openPlatformTestCase();
  34. }
  35. $account = table('account_wxapp')->getAccount($acid);
  36. $account['encrypt_key'] = $this->appid;
  37. return $account;
  38. }
  39. public function getAuthLoginUrl() {
  40. $preauthcode = $this->getPreauthCode();
  41. if (is_error($preauthcode)) {
  42. $authurl = "javascript:alert('{$preauthcode['message']}');";
  43. } else {
  44. $authurl = sprintf(ACCOUNT_PLATFORM_API_LOGIN, $this->appid, $preauthcode, urlencode($GLOBALS['_W']['siteroot'] . 'index.php?c=wxapp&a=auth&do=forward'), ACCOUNT_PLATFORM_API_LOGIN_WXAPP);
  45. }
  46. return $authurl;
  47. }
  48. public function getOauthInfo($code = '') {
  49. $component_accesstoken = $this->getComponentAccesstoken();
  50. if (is_error($component_accesstoken)) {
  51. return $component_accesstoken;
  52. }
  53. $apiurl = sprintf(self::JSCODEURL, $this->account['key'], $code, $this->appid, $component_accesstoken);
  54. $response = $this->request($apiurl);
  55. if (is_error($response)) {
  56. return $response;
  57. }
  58. cache_write('account_oauth_refreshtoken'.$this->account['key'], $response['refresh_token']);
  59. return $response;
  60. }
  61. protected function setAuthRefreshToken($token) {
  62. $tablename = 'account_wxapp';
  63. pdo_update($tablename, array('auth_refresh_token' => $token), array('uniacid' => $this->account['uniacid']));
  64. cache_write(cache_system_key('account_auth_refreshtoken', array('uniacid' => $this->account['uniacid'])), $token);
  65. }
  66. public function pkcs7Encode($encrypt_data, $iv) {
  67. $key = base64_decode($_SESSION['session_key']);
  68. $result = aes_pkcs7_decode($encrypt_data, $key, $iv);
  69. if (is_error($result)) {
  70. return error(1, '解密失败');
  71. }
  72. $result = json_decode($result, true);
  73. if (empty($result)) {
  74. return error(1, '解密失败');
  75. }
  76. if ($result['watermark']['appid'] != $this->account['key']) {
  77. return error(1, '解密失败');
  78. }
  79. unset($result['watermark']);
  80. return $result;
  81. }
  82. public function result($errno, $message = '', $data = '') {
  83. exit(json_encode(array(
  84. 'errno' => $errno,
  85. 'message' => $message,
  86. 'data' => $data,
  87. )));
  88. }
  89. public function getDailyVisitTrend($date) {
  90. $token = $this->getAccessToken();
  91. if (is_error($token)) {
  92. return $token;
  93. }
  94. $url = "https://api.weixin.qq.com/datacube/getweanalysisappiddailyvisittrend?access_token={$token}";
  95. $response = $this->requestApi($url, json_encode(array('begin_date' => $date, 'end_date' => $date)));
  96. if (is_error($response)) {
  97. return $response;
  98. }
  99. return $response['list'][0];
  100. }
  101. }