Mini.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php namespace App\Servers\WechatMini;
  2. use EasyWeChat\Factory;
  3. use Ixudra\Curl\Facades\Curl;
  4. /**
  5. * 微信小程序
  6. *
  7. */
  8. class Mini
  9. {
  10. // 工作列表
  11. protected \EasyWeChat\MiniProgram\Application $mp;
  12. // 返回结果
  13. public function __construct(){
  14. // 获取配置
  15. $this->mp = Factory::miniProgram(config('wechat.mini',[]));
  16. }
  17. /**
  18. * 获取UrlLink
  19. * @param string $path 通过 URL Link 进入的小程序页面路径,必须是已经发布的小程序存在的页面,不可携带 query 。path 为空时会跳转小程序主页
  20. * @param string $query 通过 URL Link 进入小程序时的query,最大1024个字符,只支持数字,大小写英文以及部分特殊字符:!#$&'()*+,/:;=?@-._~%
  21. *
  22. */
  23. public function getUrlLink($path,$query=''){
  24. // 组合参数
  25. $params['path'] = $path;
  26. $params['query'] = $query;
  27. $params['expire_type'] = 1; // 默认值0.小程序 URL Link 失效类型,失效时间:0,失效间隔天数:1
  28. $params['expire_interval'] = 30; // 到期失效的URL Link的失效间隔天数
  29. $params['env_version'] = 'release';//config('app.env','production') == 'production' ? 'release' : 'trial';
  30. // 获取结果
  31. $result = $this->mp->url_link->generate($params);
  32. // 成功返回
  33. if( $result['errcode'] == 0 ) return $result['url_link'];
  34. // 失败
  35. return '';
  36. }
  37. /**
  38. * 手机号授权
  39. * @param string $code 授权码
  40. *
  41. */
  42. public function getUserPhone($code){
  43. // 获取手机号
  44. $result = $this->mp->phone_number->getUserPhoneNumber($code);
  45. // 判断结果
  46. if( !empty($result['errcode']) ) return ['error'=>$result['errcode'].'=>'.$result['errmsg']];
  47. // 获取不包含区号的手机号(因为绑定手机号字段会有国际区号)
  48. return $result['phone_info'];
  49. }
  50. /**
  51. * 获取AccessToken
  52. *
  53. */
  54. public function getAccessToken(){
  55. // 获取手机号
  56. $result = $this->mp->access_token->getToken();
  57. // 判断结果
  58. if( !empty($result['access_token']) ) return $result['access_token'];
  59. // 获取不包含区号的手机号(因为绑定手机号字段会有国际区号)
  60. return '';
  61. }
  62. /**
  63. * 查询加密URLLink
  64. *
  65. */
  66. public function queryUrlLink($urlLink){
  67. // 获取手机号
  68. $token = $this->getAccessToken();
  69. //
  70. $result = Curl::to('https://api.weixin.qq.com/wxa/query_urllink?access_token='.$token)->withData(['url_link'=>$urlLink])->asJsonRequest()->asJsonResponse(true)->post();
  71. // 判断结果
  72. if( !empty($result['url_link_info']) ) return $result['url_link_info'];
  73. // 获取不包含区号的手机号(因为绑定手机号字段会有国际区号)
  74. return ['error'=>$result['errcode'].'=>'.$result['errmsg']];
  75. }
  76. /**
  77. * 获取小程序码
  78. *
  79. * @param string $scene 小程序码的场景值
  80. * @param array $optional 其他参数
  81. * page 小程序码的页面路径
  82. * width 小程序码的宽度
  83. *
  84. */
  85. public function getUnlimit(string $scene, array $optional = []){
  86. // // 版本
  87. // $optional['env_version'] = 'production';//config('app.env','production') == 'production' ? 'release' : 'trial';
  88. // 执行生成
  89. $result = $this->mp->app_code->getUnlimit($scene, $optional);
  90. // 判断结果
  91. if( $result instanceof \EasyWeChat\Kernel\Http\StreamResponse ) return $result->getBody()->getContents();
  92. // 错误
  93. return ['error'=>$result['errcode'].'=>'.$result['errmsg']];
  94. }
  95. }