WechatLogic.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. <?php
  2. namespace app\mobile\logic;
  3. //关于微信相关操作的工具类
  4. use think\exception\ValidateException;
  5. use think\facade\Validate;
  6. class WechatLogic
  7. {
  8. //获取微信网页授权URL
  9. //$param $callback string 微信回跳地址(接口已经默认url_encode处理,授权成功会有$_GET['code']值,可用于下个步骤)
  10. //$param $state string 重定向后会带上state参数(开发者可以填写a-zA-Z0-9的参数值,最多128字节)
  11. //$param $scope string 应用授权作用域(snsapi_base | snsapi_userinfo)
  12. public static function getOauthRedirect(string $callback = '', string $state = '', string $scope = 'snsapi_base'): string
  13. {
  14. $val = Validate::rule([
  15. 'callback|微信回跳地址' => 'require|url',
  16. 'state|携带参数' => 'require|length:1,255',
  17. 'scope|应用授权作用域' => 'require|in:snsapi_base,snsapi_userinfo'
  18. ]);
  19. if (!$val->check(['callback' => $callback, 'state' => $state, 'scope' => $scope])) throw new ValidateException($val->getError());
  20. $oauth = &load_wechat('Oauth');
  21. $rs = $oauth->getOauthRedirect($callback, $state, $scope);
  22. halt('调用成功', $rs);
  23. }
  24. }