人人商城

wxapp.platform.class.php 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. protected $appid;
  11. protected $appsecret;
  12. protected $encodingaeskey;
  13. protected $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. public function __construct($uniaccount = array()) {
  22. $setting = setting_load('platform');
  23. $this->appid = $setting['platform']['appid'];
  24. $this->appsecret = $setting['platform']['appsecret'];
  25. $this->token = $setting['platform']['token'];
  26. $this->encodingaeskey = $setting['platform']['encodingaeskey'];
  27. parent::__construct($uniaccount);
  28. }
  29. protected function getAccountInfo($acid) {
  30. if ($this->account['key'] == 'wx570bc396a51b8ff8') {
  31. $this->account['key'] = $this->appid;
  32. $this->openPlatformTestCase();
  33. }
  34. $account = table('account')->getWxappAccount($acid);
  35. $account['encrypt_key'] = $this->appid;
  36. return $account;
  37. }
  38. public function getAuthLoginUrl() {
  39. $preauthcode = $this->getPreauthCode();
  40. if (is_error($preauthcode)) {
  41. $authurl = "javascript:alert('{$preauthcode['message']}');";
  42. } else {
  43. $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);
  44. }
  45. return $authurl;
  46. }
  47. public function getOauthInfo($code = '') {
  48. $component_accesstoken = $this->getComponentAccesstoken();
  49. if (is_error($component_accesstoken)) {
  50. return $component_accesstoken;
  51. }
  52. $apiurl = sprintf(self::JSCODEURL, $this->account['key'], $code, $this->appid, $component_accesstoken);
  53. $response = $this->request($apiurl);
  54. if (is_error($response)) {
  55. return $response;
  56. }
  57. cache_write(cache_system_key('account_auth_accesstoken', array('key' => $this->account['key'])), $response['refresh_token']);
  58. return $response;
  59. }
  60. protected function setAuthRefreshToken($token) {
  61. $tablename = 'account_wxapp';
  62. pdo_update($tablename, array('auth_refresh_token' => $token), array('acid' => $this->account['acid']));
  63. cache_write(cache_system_key('account_auth_accesstoken', array('key' => $this->account['key'])), $token);
  64. }
  65. public function pkcs7Encode($encrypt_data, $iv) {
  66. $key = base64_decode($_SESSION['session_key']);
  67. $result = aes_pkcs7_decode($encrypt_data, $key, $iv);
  68. if (is_error($result)) {
  69. return error(1, '解密失败');
  70. }
  71. $result = json_decode($result, true);
  72. if (empty($result)) {
  73. return error(1, '解密失败');
  74. }
  75. if ($result['watermark']['appid'] != $this->account['key']) {
  76. return error(1, '解密失败');
  77. }
  78. unset($result['watermark']);
  79. return $result;
  80. }
  81. public function result($errno, $message = '', $data = '') {
  82. exit(json_encode(array(
  83. 'errno' => $errno,
  84. 'message' => $message,
  85. 'data' => $data,
  86. )));
  87. }
  88. public function getDailyVisitTrend($date) {
  89. $token = $this->getAccessToken();
  90. if (is_error($token)) {
  91. return $token;
  92. }
  93. $url = "https://api.weixin.qq.com/datacube/getweanalysisappiddailyvisittrend?access_token={$token}";
  94. $response = $this->requestApi($url, json_encode(array('begin_date' => $date, 'end_date' => $date)));
  95. if (is_error($response)) {
  96. return $response;
  97. }
  98. return $response['list'][0];
  99. }
  100. }