人人商城

wxapp.platform.class.php 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. class WxAppPlatform extends WeiXinPlatform {
  8. 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';
  9. function __construct($account = array()) {
  10. parent::__construct($account);
  11. $this->menuFrame = 'wxapp';
  12. $this->type = ACCOUNT_TYPE_APP_AUTH;
  13. $this->typeName = '小程序';
  14. $this->typeSign = WEBAPP_TYPE_SIGN;
  15. }
  16. function fetchAccountInfo() {
  17. if ($this->uniaccount['key'] == 'wx570bc396a51b8ff8') {
  18. $this->uniaccount['key'] = $this->appid;
  19. $this->account = $this->uniaccount;
  20. $this->openPlatformTestCase();
  21. }
  22. $account_table = table('account');
  23. $account = $account_table->getWxappAccount($this->uniaccount['acid']);
  24. $account['encrypt_key'] = $this->appid;
  25. return $account;
  26. }
  27. function accountDisplayUrl() {
  28. return url('account/display', array('type' => WXAPP_TYPE_SIGN));
  29. }
  30. public function getAuthLoginUrl() {
  31. $preauthcode = $this->getPreauthCode();
  32. if (is_error($preauthcode)) {
  33. $authurl = "javascript:alert('{$preauthcode['message']}');";
  34. } else {
  35. $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);
  36. }
  37. return $authurl;
  38. }
  39. public function getOauthInfo($code = '') {
  40. $component_accesstoken = $this->getComponentAccesstoken();
  41. if (is_error($component_accesstoken)) {
  42. return $component_accesstoken;
  43. }
  44. $apiurl = sprintf(self::JSCODEURL, $this->account['key'], $code, $this->appid, $component_accesstoken);
  45. $response = $this->request($apiurl);
  46. if (is_error($response)) {
  47. return $response;
  48. }
  49. cache_write(cache_system_key('account_auth_accesstoken', array('key' => $this->account['key'])), $response['refresh_token']);
  50. return $response;
  51. }
  52. protected function setAuthRefreshToken($token) {
  53. $tablename = 'account_wxapp';
  54. pdo_update($tablename, array('auth_refresh_token' => $token), array('acid' => $this->account['acid']));
  55. cache_write(cache_system_key('account_auth_accesstoken', array('key' => $this->account['key'])), $token);
  56. }
  57. public function pkcs7Encode($encrypt_data, $iv) {
  58. $key = base64_decode($_SESSION['session_key']);
  59. $result = aes_pkcs7_decode($encrypt_data, $key, $iv);
  60. if (is_error($result)) {
  61. return error(1, '解密失败');
  62. }
  63. $result = json_decode($result, true);
  64. if (empty($result)) {
  65. return error(1, '解密失败');
  66. }
  67. if ($result['watermark']['appid'] != $this->account['key']) {
  68. return error(1, '解密失败');
  69. }
  70. unset($result['watermark']);
  71. return $result;
  72. }
  73. public function result($errno, $message = '', $data = '') {
  74. exit(json_encode(array(
  75. 'errno' => $errno,
  76. 'message' => $message,
  77. 'data' => $data,
  78. )));
  79. }
  80. }