人人商城

wxapp.work.class.php 1.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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('account/wxapp.account');
  8. class WxappWork extends WxappAccount {
  9. public function getAccessToken() {
  10. $cachekey = "accesstoken:{$this->account['key']}";
  11. $cache = cache_load($cachekey);
  12. if (!empty($cache) && !empty($cache['token']) && $cache['expire'] > TIMESTAMP) {
  13. $this->account['access_token'] = $cache;
  14. }
  15. if (empty($this->account['key']) || empty($this->account['secret'])) {
  16. return error('-1', '未填写小程序的 appid 或 appsecret!');
  17. }
  18. $url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={$this->account['key']}&corpsecret={$this->account['secret']}";
  19. $response = $this->requestApi($url);
  20. $record = array();
  21. $record['token'] = $response['access_token'];
  22. $record['expire'] = TIMESTAMP + $response['expires_in'] - 200;
  23. $this->account['access_token'] = $record;
  24. cache_write($cachekey, $record);
  25. return $record['token'];
  26. }
  27. public function getOauthInfo($code = '') {
  28. global $_W, $_GPC;
  29. if (!empty($_GPC['code'])) {
  30. $code = $_GPC['code'];
  31. }
  32. $token = $this->getAccessToken();
  33. $url = "https://qyapi.weixin.qq.com/cgi-bin/miniprogram/jscode2session?access_token={$token}&js_code={$code}&grant_type=authorization_code";
  34. $response = $this->requestApi($url);
  35. $response['openid'] = $response['userid'];
  36. return $response;
  37. }
  38. }