Wechat.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. namespace app\api\controller\third;
  3. use think\Db;
  4. use think\exception\HttpResponseException;
  5. use app\api\controller\Base;
  6. use app\common\Service\Third\Wechat\Wechat as WechatService;
  7. class Wechat extends Base
  8. {
  9. protected $noNeedLogin = ['login', 'getSessionId', 'oauthLogin', 'jssdk', 'wxacode', 'subscribeTemplate'];
  10. protected $noNeedRight = ['*'];
  11. protected $payload = [];
  12. protected $wechat;
  13. protected $platform;
  14. public function _initialize()
  15. {
  16. parent::_initialize();
  17. $this->platform = $this->request->header('platform', '');
  18. if ($this->platform === '') {
  19. $this->error('参数错误');
  20. }
  21. $this->payload = $this->request->post('payload/a', '') ?? [];
  22. $this->wechat = new WechatService($this->platform, $this->payload);
  23. }
  24. // 微信登陆(小程序+公众号+开放平台)
  25. public function login()
  26. {
  27. $result = Db::transaction(function () {
  28. return $this->wechat->login();
  29. });
  30. if ($result) {
  31. $this->success('登陆成功');
  32. }
  33. $this->error('登陆失败');
  34. }
  35. // 获取小程序sessionId+自动登录
  36. public function getSessionId()
  37. {
  38. $result = $this->wechat->getSessionId();
  39. $this->success('', $result);
  40. }
  41. // 获取网页授权地址
  42. public function oauthLogin()
  43. {
  44. $result = $this->wechat->oauthLogin();
  45. if (isset($result['login_url'])) {
  46. $this->success('', $result);
  47. }
  48. if (isset($result['redirect_url'])) {
  49. return redirect($result['redirect_url']);
  50. }
  51. }
  52. // 绑定用户手机号
  53. public function bindUserPhoneNumber()
  54. {
  55. $result = Db::transaction(function () {
  56. $user = auth_user();
  57. $mobile = $this->wechat->getUserPhoneNumber();
  58. $this->validate(['mobile' => $mobile], '.bindWechatMiniProgramMobile');
  59. $user->mobile = $mobile;
  60. $verification = $user->verification;
  61. $verification->mobile = 1;
  62. $user->verification = $verification;
  63. return $user->save();
  64. });
  65. if ($result) {
  66. $this->success('绑定成功');
  67. }
  68. $this->error('操作失败');
  69. }
  70. // 绑定微信账号
  71. public function bind()
  72. {
  73. $result = Db::transaction(function () {
  74. $user = auth_user();
  75. return $this->wechat->bind($user);
  76. });
  77. if ($result) {
  78. $this->success('绑定成功');
  79. }
  80. $this->error('绑定失败');
  81. }
  82. // 解绑微信账号
  83. public function unbind()
  84. {
  85. $result = Db::transaction(function () {
  86. return $this->wechat->unbind();
  87. });
  88. if ($result) {
  89. $this->success('解绑成功');
  90. }
  91. $this->error('解绑失败');
  92. }
  93. // 微信网页jssdk
  94. public function jssdk()
  95. {
  96. $apis = [
  97. 'checkJsApi',
  98. 'updateTimelineShareData',
  99. 'updateAppMessageShareData',
  100. 'getLocation', //获取位置
  101. 'openLocation', //打开位置
  102. 'scanQRCode', //扫一扫接口
  103. 'chooseWXPay', //微信支付
  104. 'chooseImage', //拍照或从手机相册中选图接口
  105. 'previewImage', //预览图片接口 'uploadImage', //上传图片
  106. 'openAddress', // 获取微信地址
  107. 'requestMerchantTransfer', // 微信商家转账
  108. ];
  109. // $openTagList = [
  110. // 'wx-open-subscribe'
  111. // ];
  112. try {
  113. $data = $this->wechat->jssdk($apis);
  114. } catch (HttpResponseException $e) {
  115. $data = $e->getResponse()->getData();
  116. $message = $data ? ($data['msg'] ?? '') : $e->getMessage();
  117. $this->error($message);
  118. } catch (\Exception $e) {
  119. $this->error($e->getMessage());
  120. }
  121. $this->success('jssdkApi', $data);
  122. }
  123. /**
  124. * 微信小程序码接口
  125. */
  126. public function wxacode()
  127. {
  128. $mp = $this->wechat->getApp();
  129. $path = $this->payload['path'];
  130. list($page, $scene) = explode('?', $path);
  131. $content = $mp->app_code->getUnlimit($scene, [
  132. 'page' => substr($page, 1),
  133. 'is_hyaline' => true,
  134. // 'env_version' => 'develop'
  135. 'env_version' => 'release'
  136. ]);
  137. if ($content instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
  138. return response($content->getBody(), 200, ['Content-Length' => strlen($content->getBodyContents())])->contentType('image/png');
  139. } else {
  140. // 小程序码获取失败
  141. $msg = $content['errcode'] ?? '-';
  142. $msg .= $content['errmsg'] ?? '';
  143. $this->error($msg);
  144. }
  145. }
  146. }