WechatService.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. namespace app\common\library\xmwechat;
  3. use think\exception\HttpResponseException;
  4. use think\facade\Config;
  5. use think\Response;
  6. class WechatService
  7. {
  8. // 公众号配置
  9. protected array $offiAccountConfig;
  10. // 小程序配置
  11. protected array $miniProgramConfig;
  12. // 微信支付配置
  13. protected array $wechatPayConfig;
  14. // 微信支付服务商配置
  15. protected array $wechatPartnerPayConfig;
  16. public function __construct()
  17. {
  18. $config = config('xmwechat');
  19. $this->offiAccountConfig = $config['offiAccount'];
  20. $this->miniProgramConfig = $config['miniProgram'];
  21. $this->wechatPayConfig = $config['payment'];
  22. $this->wechatPartnerPayConfig = $config['partnerPayment'];
  23. }
  24. /**
  25. * 返回 API 数据
  26. * @param string $msg 提示消息
  27. * @param mixed $data 返回数据
  28. * @param int $code 错误码
  29. * @param string|null $type 输出类型
  30. * @param array $header 发送的 header 信息
  31. * @param array $options Response 输出参数
  32. */
  33. public function result(string $msg, mixed $data = null, int $code = 0, string $type = null, array $header = [], array $options = [])
  34. {
  35. $result = [
  36. 'code' => $code,
  37. 'msg' => $msg,
  38. 'time' => request()->server('REQUEST_TIME'),
  39. 'data' => $data,
  40. ];
  41. // 如果未设置类型则自动判断
  42. $type = $type ?: (request()->param(Config::get('route.var_jsonp_handler')) ? 'jsonp' : 'json');
  43. $code = 200;
  44. if (isset($header['statuscode'])) {
  45. $code = $header['statuscode'];
  46. unset($header['statuscode']);
  47. }
  48. $response = Response::create($result, $type, $code)->header($header)->options($options);
  49. throw new HttpResponseException($response);
  50. }
  51. }