Wechat.php 5.5 KB

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