WechatService.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkAdmin
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2014~2022 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: https://thinkadmin.top
  8. // +----------------------------------------------------------------------
  9. // | 开源协议 ( https://mit-license.org )
  10. // | 免费声明 ( https://thinkadmin.top/disclaimer )
  11. // +----------------------------------------------------------------------
  12. // | gitee 代码仓库:https://gitee.com/zoujingli/ThinkAdmin
  13. // | github 代码仓库:https://github.com/zoujingli/ThinkAdmin
  14. // +----------------------------------------------------------------------
  15. namespace app\wechat\service;
  16. use think\admin\Exception;
  17. use think\admin\extend\JsonRpcClient;
  18. use think\admin\Service;
  19. use think\admin\storage\LocalStorage;
  20. use think\exception\HttpResponseException;
  21. /**
  22. * Class WechatService
  23. * @package app\wechat\serivce
  24. *
  25. * @method \WeChat\Card WeChatCard() static 微信卡券管理
  26. * @method \WeChat\Custom WeChatCustom() static 微信客服消息
  27. * @method \WeChat\Limit WeChatLimit() static 接口调用频次限制
  28. * @method \WeChat\Media WeChatMedia() static 微信素材管理
  29. * @method \WeChat\Menu WeChatMenu() static 微信菜单管理
  30. * @method \WeChat\Oauth WeChatOauth() static 微信网页授权
  31. * @method \WeChat\Pay WeChatPay() static 微信支付商户
  32. * @method \WeChat\Product WeChatProduct() static 微信商店管理
  33. * @method \WeChat\Qrcode WeChatQrcode() static 微信二维码管理
  34. * @method \WeChat\Receive WeChatReceive() static 微信推送管理
  35. * @method \WeChat\Scan WeChatScan() static 微信扫一扫接入管理
  36. * @method \WeChat\Script WeChatScript() static 微信前端支持
  37. * @method \WeChat\Shake WeChatShake() static 微信揺一揺周边
  38. * @method \WeChat\Tags WeChatTags() static 微信用户标签管理
  39. * @method \WeChat\Template WeChatTemplate() static 微信模板消息
  40. * @method \WeChat\User WeChatUser() static 微信粉丝管理
  41. * @method \WeChat\Wifi WeChatWifi() static 微信门店WIFI管理
  42. *
  43. * ----- WeMini -----
  44. * @method \WeMini\Account WeMiniAccount() static 小程序账号管理
  45. * @method \WeMini\Basic WeMiniBasic() static 小程序基础信息设置
  46. * @method \WeMini\Code WeMiniCode() static 小程序代码管理
  47. * @method \WeMini\Domain WeMiniDomain() static 小程序域名管理
  48. * @method \WeMini\Tester WeMinitester() static 小程序成员管理
  49. * @method \WeMini\User WeMiniUser() static 小程序帐号管理
  50. * --------------------
  51. * @method \WeMini\Crypt WeMiniCrypt() static 小程序数据加密处理
  52. * @method \WeMini\Delivery WeMiniDelivery() static 小程序即时配送
  53. * @method \WeMini\Image WeMiniImage() static 小程序图像处理
  54. * @method \WeMini\Logistics WeMiniLogistics() static 小程序物流助手
  55. * @method \WeMini\Message WeMiniMessage() static 小程序动态消息
  56. * @method \WeMini\Ocr WeMiniOcr() static 小程序ORC服务
  57. * @method \WeMini\Plugs WeMiniPlugs() static 小程序插件管理
  58. * @method \WeMini\Poi WeMiniPoi() static 小程序地址管理
  59. * @method \WeMini\Qrcode WeMiniQrcode() static 小程序二维码管理
  60. * @method \WeMini\Security WeMiniSecurity() static 小程序内容安全
  61. * @method \WeMini\Soter WeMiniSoter() static 小程序生物认证
  62. * @method \WeMini\Template WeMiniTemplate() static 小程序模板消息支持
  63. * @method \WeMini\Total WeMiniTotal() static 小程序数据接口
  64. * @method \WeMini\Newtmpl WeMiniNewtmpl() static 小程序订阅消息支持
  65. *
  66. * ----- WePay -----
  67. * @method \WePay\Bill WePayBill() static 微信商户账单及评论
  68. * @method \WePay\Order WePayOrder() static 微信商户订单
  69. * @method \WePay\Refund WePayRefund() static 微信商户退款
  70. * @method \WePay\Coupon WePayCoupon() static 微信商户代金券
  71. * @method \WePay\Redpack WePayRedpack() static 微信红包支持
  72. * @method \WePay\Transfers WePayTransfers() static 微信商户打款到零钱
  73. * @method \WePay\TransfersBank WePayTransfersBank() static 微信商户打款到银行卡
  74. *
  75. * ----- WeOpen -----
  76. * @method \WeOpen\Login WeOpenLogin() static 第三方微信登录
  77. * @method \WeOpen\Service WeOpenService() static 第三方服务
  78. *
  79. * ----- ThinkService -----
  80. * @method mixed ThinkServiceConfig() static 平台服务配置
  81. */
  82. class WechatService extends Service
  83. {
  84. /**
  85. * 静态初始化对象
  86. * @param string $name
  87. * @param array $arguments
  88. * @return mixed
  89. * @throws \think\admin\Exception
  90. * @throws \think\db\exception\DataNotFoundException
  91. * @throws \think\db\exception\DbException
  92. * @throws \think\db\exception\ModelNotFoundException
  93. */
  94. public static function __callStatic(string $name, array $arguments)
  95. {
  96. [$type, $base, $class] = static::parseName($name);
  97. if ("{$type}{$base}" !== $name) {
  98. throw new Exception("抱歉,实例 {$name} 不符合规则!");
  99. }
  100. if (sysconf('wechat.type') === 'api' || $type === 'WePay') {
  101. if (class_exists($class)) {
  102. return new $class(static::instance()->getConfig());
  103. } else {
  104. throw new Exception("抱歉,接口模式无法实例 {$class} 对象!");
  105. }
  106. } else {
  107. [$appid, $appkey] = [sysconf('wechat.thr_appid'), sysconf('wechat.thr_appkey')];
  108. $data = ['class' => $name, 'appid' => $appid, 'time' => time(), 'nostr' => uniqid()];
  109. $data['sign'] = md5("{$data['class']}#{$appid}#{$appkey}#{$data['time']}#{$data['nostr']}");
  110. // 创建远程连接,默认使用 JSON-RPC 方式调用接口
  111. $token = enbase64url(json_encode($data, JSON_UNESCAPED_UNICODE));
  112. return new JsonRpcClient("https://open.cuci.cc/service/api.client/jsonrpc?not_init_session=1&token={$token}");
  113. }
  114. }
  115. /**
  116. * 解析调用对象名称
  117. * @param string $name
  118. * @return array
  119. */
  120. private static function parseName(string $name): array
  121. {
  122. foreach (['WeChat', 'WeMini', 'WeOpen', 'WePay', 'ThinkService'] as $type) {
  123. if (strpos($name, $type) === 0) {
  124. [, $base] = explode($type, $name);
  125. return [$type, $base, "\\{$type}\\{$base}"];
  126. }
  127. }
  128. return ['-', '-', $name];
  129. }
  130. /**
  131. * 获取当前微信APPID
  132. * @return string
  133. * @throws \think\admin\Exception
  134. * @throws \think\db\exception\DataNotFoundException
  135. * @throws \think\db\exception\DbException
  136. * @throws \think\db\exception\ModelNotFoundException
  137. */
  138. public function getAppid(): string
  139. {
  140. if ($this->getType() === 'api') {
  141. return sysconf('wechat.appid');
  142. } else {
  143. return sysconf('wechat.thr_appid');
  144. }
  145. }
  146. /**
  147. * 获取接口授权模式
  148. * @return string
  149. * @throws \think\admin\Exception
  150. * @throws \think\db\exception\DataNotFoundException
  151. * @throws \think\db\exception\DbException
  152. * @throws \think\db\exception\ModelNotFoundException
  153. */
  154. public function getType(): string
  155. {
  156. $type = strtolower(sysconf('wechat.type'));
  157. if (in_array($type, ['api', 'thr'])) return $type;
  158. throw new Exception('请在后台配置微信对接授权模式');
  159. }
  160. /**
  161. * 获取公众号配置参数
  162. * @return array
  163. * @throws \think\admin\Exception
  164. * @throws \think\db\exception\DataNotFoundException
  165. * @throws \think\db\exception\DbException
  166. * @throws \think\db\exception\ModelNotFoundException
  167. */
  168. public function getConfig(): array
  169. {
  170. $options = [
  171. 'appid' => $this->getAppid(),
  172. 'token' => sysconf('wechat.token'),
  173. 'appsecret' => sysconf('wechat.appsecret'),
  174. 'encodingaeskey' => sysconf('wechat.encodingaeskey'),
  175. 'mch_id' => sysconf('wechat.mch_id'),
  176. 'mch_key' => sysconf('wechat.mch_key'),
  177. 'cache_path' => $this->app->getRuntimePath() . 'wechat',
  178. ];
  179. $local = LocalStorage::instance();
  180. switch (strtolower(sysconf('wechat.mch_ssl_type'))) {
  181. case 'p12':
  182. $options['ssl_p12'] = $local->path(sysconf('wechat.mch_ssl_p12'), true);
  183. break;
  184. case 'pem':
  185. $options['ssl_key'] = $local->path(sysconf('wechat.mch_ssl_key'), true);
  186. $options['ssl_cer'] = $local->path(sysconf('wechat.mch_ssl_cer'), true);
  187. break;
  188. }
  189. return $options;
  190. }
  191. /**
  192. * 通过网页授权获取粉丝信息
  193. * @param string $source 回跳URL地址
  194. * @param integer $isfull 获取资料模式
  195. * @param boolean $redirect 是否直接跳转
  196. * @return array
  197. * @throws \WeChat\Exceptions\InvalidResponseException
  198. * @throws \WeChat\Exceptions\LocalCacheException
  199. * @throws \think\admin\Exception
  200. * @throws \think\db\exception\DataNotFoundException
  201. * @throws \think\db\exception\DbException
  202. * @throws \think\db\exception\ModelNotFoundException
  203. */
  204. public function getWebOauthInfo(string $source, int $isfull = 0, bool $redirect = true): array
  205. {
  206. $appid = $this->getAppid();
  207. $openid = $this->app->session->get("{$appid}_openid");
  208. $userinfo = $this->app->session->get("{$appid}_fansinfo");
  209. if ((empty($isfull) && !empty($openid)) || (!empty($isfull) && !empty($openid) && !empty($userinfo))) {
  210. empty($userinfo) || FansService::instance()->set($userinfo, $appid);
  211. return ['openid' => $openid, 'fansinfo' => $userinfo];
  212. }
  213. if ($this->getType() === 'api') {
  214. // 解析 GET 参数
  215. parse_str(parse_url($source, PHP_URL_QUERY), $params);
  216. $getVars = [
  217. 'code' => $params['code'] ?? input('code', ''),
  218. 'rcode' => $params['rcode'] ?? input('rcode', ''),
  219. 'state' => $params['state'] ?? input('state', ''),
  220. ];
  221. $wechat = static::WeChatOauth();
  222. if ($getVars['state'] !== $appid || empty($getVars['code'])) {
  223. $params['rcode'] = enbase64url($source);
  224. $location = strstr("{$source}?", '?', true) . '?' . http_build_query($params);
  225. $oauthurl = $wechat->getOauthRedirect($location, $appid, $isfull ? 'snsapi_userinfo' : 'snsapi_base');
  226. throw new HttpResponseException($redirect ? redirect($oauthurl, 301) : response("location.href='{$oauthurl}'"));
  227. } elseif (($token = $wechat->getOauthAccessToken($getVars['code'])) && isset($token['openid'])) {
  228. $this->app->session->set("{$appid}_openid", $openid = $token['openid']);
  229. if ($isfull && isset($token['access_token'])) {
  230. $userinfo = $wechat->getUserInfo($token['access_token'], $openid);
  231. $this->app->session->set("{$appid}_fansinfo", $userinfo);
  232. empty($userinfo) || FansService::instance()->set($userinfo, $appid);
  233. }
  234. }
  235. if ($getVars['rcode']) {
  236. $location = debase64url($getVars['rcode']);
  237. throw new HttpResponseException($redirect ? redirect($location, 301) : response("location.href='{$location}'"));
  238. } elseif ((empty($isfull) && !empty($openid)) || (!empty($isfull) && !empty($openid) && !empty($userinfo))) {
  239. return ['openid' => $openid, 'fansinfo' => $userinfo];
  240. } else {
  241. throw new Exception('Query params [rcode] not find.');
  242. }
  243. } else {
  244. $result = static::ThinkServiceConfig()->oauth($this->app->session->getId(), $source, $isfull);
  245. $this->app->session->set("{$appid}_openid", $openid = $result['openid']);
  246. $this->app->session->set("{$appid}_fansinfo", $userinfo = $result['fans']);
  247. if ((empty($isfull) && !empty($openid)) || (!empty($isfull) && !empty($openid) && !empty($userinfo))) {
  248. empty($userinfo) || FansService::instance()->set($userinfo, $appid);
  249. return ['openid' => $openid, 'fansinfo' => $userinfo];
  250. }
  251. if ($redirect) {
  252. throw new HttpResponseException(redirect($result['url'], 301));
  253. } else {
  254. throw new HttpResponseException(response("location.href='{$result['url']}'"));
  255. }
  256. }
  257. }
  258. /**
  259. * 获取微信网页JSSDK签名参数
  260. * @param null|string $location 签名地址
  261. * @return array
  262. * @throws \WeChat\Exceptions\InvalidResponseException
  263. * @throws \WeChat\Exceptions\LocalCacheException
  264. * @throws \think\admin\Exception
  265. * @throws \think\db\exception\DataNotFoundException
  266. * @throws \think\db\exception\DbException
  267. * @throws \think\db\exception\ModelNotFoundException
  268. */
  269. public function getWebJssdkSign(?string $location = null): array
  270. {
  271. $location = $location ?: $this->app->request->url(true);
  272. if ($this->getType() === 'api') {
  273. return static::WeChatScript()->getJsSign($location);
  274. } else {
  275. return static::ThinkServiceConfig()->jsSign($location);
  276. }
  277. }
  278. }