Wechat.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. /**
  3. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com
  8. * =========================================================
  9. */
  10. namespace addon\wechat\api\controller;
  11. use addon\wechat\model\Config as ConfigModel;
  12. use addon\wechat\model\Fans;
  13. use addon\wechat\model\Wechat as WechatModel;
  14. use app\api\controller\BaseApi;
  15. use app\model\member\Member as MemberModel;
  16. use think\facade\Cache;
  17. class Wechat extends BaseApi
  18. {
  19. /**
  20. * 获取openid
  21. */
  22. public function authCodeToOpenid()
  23. {
  24. $weapp_model = new WechatModel($this->site_id);
  25. $res = $weapp_model->getAuthByCode($this->params);
  26. return $this->response($res);
  27. }
  28. /**
  29. * 获取网页授权code
  30. */
  31. public function authcode()
  32. {
  33. $redirect_url = $this->params[ 'redirect_url' ] ?? '';
  34. $scopes = $this->params[ 'scopes' ] ?? 'snsapi_base';
  35. $weapp_model = new WechatModel($this->site_id);
  36. $res = $weapp_model->getAuthCode($redirect_url, $scopes);
  37. return $this->response($res);
  38. }
  39. /**
  40. * 获取jssdk配置
  41. */
  42. public function jssdkConfig()
  43. {
  44. $url = $this->params[ 'url' ] ?? '';
  45. $weapp_model = new WechatModel($this->site_id);
  46. $res = $weapp_model->getJssdkConfig($url);
  47. return $this->response($res);
  48. }
  49. /**
  50. * 分享设置
  51. */
  52. public function share()
  53. {
  54. $this->checkToken();
  55. //页面路径
  56. $url = $this->params[ 'url' ] ?? '';
  57. //sdk配置
  58. $weapp_model = new WechatModel($this->site_id);
  59. $jssdk_config = $weapp_model->getJssdkConfig($url);
  60. if ($jssdk_config[ 'code' ] < 0) return $this->response($jssdk_config);
  61. //分享配置
  62. $share_config = [];
  63. $share_data = event('WchatShareData', [
  64. 'url' => $url,
  65. 'site_id' => $this->site_id,
  66. 'member_id' => $this->member_id,
  67. ], true);
  68. if (!empty($share_data)) {
  69. $share_config[ 'permission' ] = $share_data[ 'permission' ];
  70. $share_config[ 'data' ] = $share_data[ 'data' ];
  71. } else {
  72. $share_config[ 'permission' ] = [
  73. 'hideOptionMenu' => true,
  74. 'hideMenuItems' => [],
  75. ];
  76. $share_config[ 'data' ] = null;
  77. }
  78. $data = [
  79. 'jssdk_config' => $jssdk_config[ 'data' ],
  80. 'share_config' => $share_config,
  81. ];
  82. return $this->response($this->success($data));
  83. }
  84. /**
  85. * 绑定店铺openid
  86. * @return false|string
  87. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  88. */
  89. public function shopBindOpenid()
  90. {
  91. $key = $this->params[ "key" ];
  92. $weapp_model = new WechatModel($this->site_id);
  93. $res = $weapp_model->authCodeToOpenid($this->params);
  94. if ($res[ "code" ] >= 0) {
  95. Cache::set($key, $res[ "data" ][ "openid" ]);
  96. return $this->response($res);
  97. }
  98. }
  99. /**
  100. * 获取登录二维码
  101. * @return false|string
  102. */
  103. public function loginCode()
  104. {
  105. $key = str_replace('==', '', base64_encode(uniqid('')));
  106. $expire_time = 600;
  107. $wechat_model = new WechatModel($this->site_id);
  108. $res = $wechat_model->getTempQrcode('key_' . $key, $expire_time);
  109. if ($res[ 'code' ] != 0) return $this->response($res);
  110. Cache::set('wechat_' . $key, [ 'expire_time' => ( time() + $expire_time ) ], 3600);
  111. $data = $this->success([
  112. 'key' => $key,
  113. 'expire_time' => $expire_time,
  114. 'qrcode' => $res[ 'data' ]
  115. ]);
  116. return $this->response($data);
  117. }
  118. /*
  119. * 验证公众号是否配置
  120. */
  121. public function verificationWx()
  122. {
  123. $config_model = new ConfigModel();
  124. $config_info = $config_model->getWechatConfig($this->site_id);
  125. if (!empty($config_info[ 'data' ])) {
  126. $config = $config_info[ 'data' ][ "value" ];
  127. if ($config[ 'appid' ] || $config[ 'appsecret' ]) {
  128. return $this->response($this->success());
  129. }
  130. }
  131. return $this->response($this->error());
  132. }
  133. /**
  134. * 获取公众号关注二维码
  135. * @return false|string
  136. */
  137. public function followQrcode()
  138. {
  139. if (!in_array($this->params[ 'app_type' ], [ 'weapp', 'wechat' ])) return $this->response($this->error('', '该接口仅支持微信浏览器中调用'));
  140. $token = $this->checkToken();
  141. if ($token[ 'code' ] < 0) return $this->response($token);
  142. $member_model = new MemberModel();
  143. $member_info = $member_model->getMemberInfo([ [ 'member_id', '=', $token[ 'data' ][ 'member_id' ], [ 'site_id', '=', $this->site_id ] ] ], 'member_id,wx_openid')[ 'data' ];
  144. if (!empty($member_info) && !empty($member_info[ 'wx_openid' ])) {
  145. $wechat_fans = ( new Fans() )->getFansInfo([ [ 'site_id', '=', $this->site_id ], [ 'openid', '=', $member_info[ 'wx_openid' ] ], [ 'is_subscribe', '=', 1 ] ], 'fans_id')[ 'data' ];
  146. if (!empty($wechat_fans)) return $this->response($this->success());
  147. if (!empty($wechat_fans[ 'fans_id' ])) {
  148. $wechat_model = new WechatModel();
  149. $res = $wechat_model->getFollowQrcode($wechat_fans[ 'fans_id' ]);
  150. if ($res[ 'code' ] == 0) return $this->response($res);
  151. }
  152. }
  153. // $fans_info = ( new MemberFans() )->getFansInfo([ [ 'fans_id', '=', $this->fans_id ] ], 'openid')[ 'data' ];
  154. // $wechat_fans = ( new Fans() )->getFansInfo([ [ 'site_id', '=', $this->site_id ], [ 'is_subscribe', '=', 1 ], [ 'openid', '=', $fans_info[ 'openid' ] ] ], 'fans_id');
  155. // if (!empty($wechat_fans[ 'data' ])) return $this->response($this->success());
  156. $config_model = new ConfigModel();
  157. $config_result = $config_model->getWechatConfig($this->site_id)[ 'data' ][ 'value' ];
  158. return $this->response($this->success([ 'qrcode' => $config_result[ 'qrcode' ] ]));
  159. }
  160. }