Microapp.Class.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /**
  3. * 头条小程序接口类
  4. * Created by PhpStorm.
  5. * User: phperstar
  6. * Date: 2020/6/16
  7. * Time: 6:35 PM
  8. */
  9. namespace Util\ByteDance;
  10. use Mall\Framework\Core\ResultWrapper;
  11. use Mall\Framework\Core\ErrorCode;
  12. use Mall\Framework\Factory;
  13. class Microapp
  14. {
  15. /**
  16. * 小程序 appId
  17. * @var string $appid
  18. */
  19. private $appid;
  20. /**
  21. * 小程序 appSecret
  22. * @var string $appSecret
  23. */
  24. private $appSecret;
  25. /**
  26. * openId 和 session_key关联关系缓存key
  27. */
  28. private $openidAndSessionKey = 'byteDanceOpenidAndSessionKey';
  29. // access_token 全局唯一接口调用凭据
  30. private $access_token = 'byteDanceAccessToken';
  31. /**
  32. * path和小程序码地址关联关系缓存key
  33. */
  34. private $pathAndWxacodeunlimitKey = 'byteDanceQRCode';
  35. /**
  36. * 微信小程序接口主域名
  37. * @var string $apiUrl
  38. */
  39. private $apiUrl = 'https://developer.toutiao.com/api/apps/';
  40. public function __construct($appid='', $appsecret='')
  41. {
  42. $this->appid = $appid;
  43. $this->appSecret = $appsecret;
  44. }
  45. /**
  46. * 登录凭证校验
  47. * 官方文档对应地址:https://microapp.bytedance.com/dev/cn/mini-app/develop/server/log-in/code2session
  48. * @param string $jsCode 通过 wx.login 接口获得临时登录凭证 code
  49. * @return ResultWrapper
  50. */
  51. public function code2session($jsCode)
  52. {
  53. $apiUrl = $this->apiUrl.'jscode2session?appid='.$this->appid.'&secret='.$this->appSecret.'&code='.$jsCode;
  54. $reponse = request($apiUrl);
  55. if($reponse['httpcode'] != 200){
  56. return ResultWrapper::fail('请求外部系统接口报错', ErrorCode::$apiNotResult);
  57. }else{
  58. $reponseData = json_decode($reponse['content'], true);
  59. if( isset($reponseData['errcode']) ){
  60. return ResultWrapper::fail($reponseData['errmsg'], $reponseData['errcode']);
  61. }
  62. Factory::cache('default')->hset($this->openidAndSessionKey, $reponseData['openid'], $reponseData['session_key']);
  63. return ResultWrapper::success(['openid'=>$reponseData['openid']]);
  64. }
  65. }
  66. /**
  67. * 获取基础access_token
  68. * 官方文档对应地址:https://microapp.bytedance.com/dev/cn/mini-app/develop/server/interface-request-credential/getaccesstoken
  69. * @return ResultWrapper
  70. */
  71. public function token()
  72. {
  73. $access_token = Factory::cache('default')->get($this->access_token.':'.$this->appid);
  74. if(!empty($access_token)){
  75. return ResultWrapper::success($access_token);
  76. }
  77. $apiUrl = $this->apiUrl.'token?grant_type=client_credential&appid='.$this->appid.'&secret='.$this->appSecret;
  78. $reponse = request($apiUrl);
  79. if($reponse['httpcode'] != 200){
  80. return ResultWrapper::fail('请求外部系统接口报错', ErrorCode::$apiNotResult);
  81. }else{
  82. $reponseData = json_decode($reponse['content'], true);
  83. if( isset($reponseData['errcode']) && $reponseData['errcode'] != 0){
  84. return ResultWrapper::fail($reponseData['errmsg'], $reponseData['errcode']);
  85. }
  86. Factory::cache('default')->set($this->access_token.':'.$this->appid, $reponseData['access_token'], 7000);
  87. return ResultWrapper::success($reponseData['access_token']);
  88. }
  89. }
  90. /**
  91. * 获取小程序/小游戏的二维码
  92. * 官方文档地址:https://microapp.bytedance.com/dev/cn/mini-app/develop/server/qr-code/createqrcode
  93. */
  94. public function createQRCode($access_token, $path)
  95. {
  96. $key = md5($this->appid.$path);
  97. $imgsrc = Factory::cache('default')->hget($this->pathAndWxacodeunlimitKey, $key);
  98. if(!empty($imgsrc)){
  99. return ResultWrapper::success($imgsrc);
  100. }
  101. $postData = [
  102. 'access_token' => $access_token,
  103. 'appname' => 'douyin',
  104. 'path' => $path,
  105. 'width' => 430,
  106. 'set_icon' => true,
  107. ];
  108. $curl_option = [
  109. CURLOPT_HTTPHEADER => [
  110. 'Content-Type:application/json',
  111. ],
  112. ];
  113. $apiUrl = $this->apiUrl.'qrcode';
  114. $reponse = request($apiUrl, json_encode($postData), 10, true, $curl_option);
  115. if($reponse['httpcode'] != 200){
  116. return ResultWrapper::fail('请求外部系统接口报错', ErrorCode::$apiNotResult);
  117. }else{
  118. if( $reponse['content_type'] == 'image/png' ){
  119. $base64 = 'data:image/png;base64,'.base64_encode($reponse['content']);
  120. $upload = Factory::baseImg();
  121. $upload->move($base64);
  122. $imgsrc = $upload->getSaveName();
  123. Factory::cache('default')->hset($this->pathAndWxacodeunlimitKey, $key, $imgsrc);
  124. return ResultWrapper::success($imgsrc);
  125. }
  126. $reponseData = json_decode($reponse['content'], true);
  127. if( isset($reponseData['errcode']) && $reponseData['errcode'] != 0 ){
  128. return ResultWrapper::fail($reponseData['errmsg'], $reponseData['errcode']);
  129. }
  130. }
  131. }
  132. }