Js.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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\controller\api;
  16. use app\wechat\service\WechatService;
  17. use think\admin\Controller;
  18. use think\Response;
  19. /**
  20. * 前端JS获取控制器
  21. * Class Js
  22. * @package app\wechat\controller\api
  23. */
  24. class Js extends Controller
  25. {
  26. /** @var string */
  27. protected $params;
  28. /** @var string */
  29. protected $openid;
  30. /** @var string */
  31. protected $fansinfo;
  32. /**
  33. * 生成网页授权的JS内容
  34. * @return \think\Response
  35. * @throws \WeChat\Exceptions\InvalidResponseException
  36. * @throws \WeChat\Exceptions\LocalCacheException
  37. * @throws \think\admin\Exception
  38. * @throws \think\db\exception\DataNotFoundException
  39. * @throws \think\db\exception\DbException
  40. * @throws \think\db\exception\ModelNotFoundException
  41. */
  42. public function index(): Response
  43. {
  44. $mode = $this->request->get('mode', 1);
  45. $source = $this->request->server('http_referer') ?: $this->request->url(true);
  46. $userinfo = WechatService::instance()->getWebOauthInfo($source, $mode, false);
  47. if (empty($userinfo['openid'])) {
  48. $content = 'alert("Wechat webOauth failed.")';
  49. } else {
  50. $this->openid = $userinfo['openid'];
  51. $this->params = json_encode(WechatService::instance()->getWebJssdkSign($source));
  52. $this->fansinfo = json_encode($userinfo['fansinfo'] ?? [], JSON_UNESCAPED_UNICODE);
  53. // 生成数据授权令牌
  54. $this->token = uniqid('oauth') . rand(10000, 99999);
  55. $this->app->cache->set($this->openid, $this->token, 3600);
  56. // 生成前端JS变量代码
  57. $content = $this->_buildContent();
  58. }
  59. return Response::create($content)->contentType('application/x-javascript');
  60. }
  61. /**
  62. * 给指定地址创建签名参数
  63. * @throws \WeChat\Exceptions\InvalidResponseException
  64. * @throws \WeChat\Exceptions\LocalCacheException
  65. * @throws \think\admin\Exception
  66. * @throws \think\db\exception\DataNotFoundException
  67. * @throws \think\db\exception\DbException
  68. * @throws \think\db\exception\ModelNotFoundException
  69. */
  70. public function sdk()
  71. {
  72. $data = $this->_vali(['url.require' => '签名地址不能为空!']);
  73. $this->success('获取签名参数', WechatService::instance()->getWebJssdkSign($data['url']));
  74. }
  75. /**
  76. * 生成授权内容
  77. * @return string
  78. */
  79. private function _buildContent(): string
  80. {
  81. return <<<EOF
  82. if(typeof wx === 'object'){
  83. wx.token="{$this->token}";
  84. wx.openid="{$this->openid}";
  85. wx.fansinfo={$this->fansinfo};
  86. wx.config({$this->params});
  87. wx.ready(function(){
  88. wx.hideOptionMenu();
  89. wx.hideAllNonBaseMenuItem();
  90. });
  91. }
  92. EOF;
  93. }
  94. }