Wxauto.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <?php
  2. namespace app\api\controller;
  3. use think\facade\Lang;
  4. /**
  5. * ============================================================================
  6. *
  7. * ============================================================================
  8. * 版权所有 2014-2028 浙江惠利玛产业互联网有限公司,并保留所有权利。
  9. * 网站地址: https://www.valimart.net/
  10. * ----------------------------------------------------------------------------
  11. *
  12. * ============================================================================
  13. * 微信登录控制器
  14. */
  15. class Wxauto extends MobileMall {
  16. public function initialize() {
  17. parent::initialize();
  18. Lang::load(base_path() . 'home/lang/' . config('lang.default_lang') . '/wxauto.lang.php');
  19. $this->wxconfig = model('wechat')->getOneWxconfig();
  20. $from = input('param.from');
  21. $this->from = $from;
  22. if ($from == 'app') {
  23. $logic_payment = model('payment', 'logic');
  24. $result = $logic_payment->getPaymentInfo('wxpay_app');
  25. if (!$result['code']) {
  26. ds_json_encode(10001, $result['msg']);
  27. }
  28. $payment_info = $result['data'];
  29. $this->appId = $payment_info['payment_config']['wx_appid'];
  30. $this->appSecret = $payment_info['payment_config']['wx_appsecret'];
  31. } else {
  32. $this->appId = $this->wxconfig['appid'];
  33. $this->appSecret = $this->wxconfig['appsecret'];
  34. }
  35. }
  36. /**
  37. * @api {POST} api/Wxauto/login 微信登录
  38. * @apiVersion 1.0.0
  39. * @apiGroup Wxauto
  40. *
  41. * @apiParam {String} ref 返回连接
  42. * @apiParam {Int} inviter_id 推荐人id
  43. *
  44. * @apiSuccess {String} code 返回码,10000为成功
  45. * @apiSuccess {String} message 返回消息
  46. * @apiSuccess {Object} result 返回数据
  47. */
  48. public function login() {
  49. $ref = htmlspecialchars_decode(input('param.ref'));
  50. $inviter_id = intval(input('param.inviter_id')); #推荐人ID
  51. $redirect_uri = API_SITE_URL . "/Wxauto/checkAuth?ref=" . urlencode($ref) . "&inviter_id=" . $inviter_id;
  52. $code_url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=$this->appId&redirect_uri=" . urlencode($redirect_uri) . "&response_type=code&scope=snsapi_userinfo&state=dsmall#wechat_redirect"; // 获取code
  53. ds_json_encode(10000, '', $code_url);
  54. }
  55. //获取openid的统一方法
  56. public function getOpenid() {
  57. $code = input('param.code');
  58. $from = input('param.from');
  59. if (!empty($code)) {
  60. if ($from == 'miniprogram') {
  61. //小程序支付获取openid
  62. $xcx_appid = $this->wxconfig['xcx_appid'];
  63. $xcx_appsecret = $this->wxconfig['xcx_appsecret'];
  64. //说明:https://developers.weixin.qq.com/minigame/dev/document/open-api/login/code2accessToken.html?search-key=jscode2session
  65. $url = "https://api.weixin.qq.com/sns/jscode2session?appid=" . $xcx_appid . "&secret=" . $xcx_appsecret . "&js_code=" . $code . "&grant_type=authorization_code";
  66. if (!$xcx_appid || !$xcx_appsecret) {
  67. $return = array('done' => false, 'msg' => lang('xcx_not_set'));
  68. return $return;
  69. }
  70. } else {
  71. //微信自动登录
  72. $wxappid = $this->appId;
  73. $wxappsecret = $this->appSecret;
  74. $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$wxappid&secret=$wxappsecret&code=$code&grant_type=authorization_code";
  75. if (!$wxappid || !$wxappsecret) {
  76. $return = array('done' => false, 'msg' => lang('wechat_not_set'));
  77. return $return;
  78. }
  79. }
  80. //获取 用户唯一标识,请注意,在未关注公众号时,用户访问公众号的网页,也会产生一个用户和公众号唯一的OpenID
  81. $res = json_decode(http_request($url), true);
  82. if (isset($res['errcode'])) {
  83. $return = array('done' => false, 'msg' => lang('error_code') . $res['errcode'] . ',' . $res['errmsg']);
  84. return $return;
  85. }
  86. $return = array('done' => true, 'data' => $res);
  87. return $return;
  88. } else {
  89. $return = array('done' => false, 'msg' => lang('openid_not_get'));
  90. return $return;
  91. }
  92. }
  93. /**
  94. *
  95. * @param type $inviter_id 推荐人ID
  96. * @return boolean|string|array
  97. */
  98. public function autoLogin($inviter_id) {
  99. //获取openid格外用一个方法,因为有些地方(如小程序支付)不需要获取用户信息,只需要openid
  100. if ($this->from) {
  101. $res = input('param.');
  102. } else {
  103. $return = $this->getOpenid();
  104. if (!$return['done']) {
  105. return $return;
  106. }
  107. $res = $return['data'];
  108. }
  109. // $accessToken5 = model('wechat')->getAccessToken($from);
  110. // if (model('wechat')->error_code != 0) {
  111. // $return = array('done' => false, 'msg' => '错误码' . model('wechat')->error_code . ',' . model('wechat')->error_message);
  112. // return $return;
  113. // }
  114. // 授权 https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140842
  115. //获取用户基本信息(UnionID机制) 必须要先关注公众号 https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140839
  116. // $url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=$accessToken5&openid=" . $res['openid'] . "&lang=zh_CN"; //获取用户信息
  117. if ($this->from!='miniprogram') {
  118. $url = "https://api.weixin.qq.com/sns/userinfo?access_token=" . $res['access_token'] . "&openid=" . $res['openid'] . "&lang=zh_CN";
  119. $userinfo = json_decode(http_request($url), true);
  120. if (isset($userinfo['errcode'])) {
  121. $return = array('done' => false, 'msg' => $userinfo['errcode'] . ',' . $userinfo['errmsg']);
  122. return $return;
  123. }
  124. }else{
  125. $userinfo = $res;
  126. }
  127. if (empty($userinfo['unionid'])) {
  128. $return = array('done' => false, 'msg' => lang('unionid_not_get'));
  129. return $return;
  130. }
  131. $member_model = model('member');
  132. $member_info = $member_model->getMemberInfo(array('member_wxunionid' => $userinfo['unionid']));
  133. if (!empty($member_info)) {
  134. //更新信息
  135. $token = $member_model->getBuyerToken($member_info['member_id'], $member_info['member_name'], 'wap');
  136. //如果用户是PC端扫码登录,则未有 member_wxopenid 需更新当前的openid
  137. if (empty($member_info['member_wxopenid'])) {
  138. $reg_info = array(
  139. 'member_wxopenid' => $userinfo['openid'],
  140. 'member_wxunionid' => $userinfo['unionid'],
  141. 'nickname' => isset($userinfo['nickname']) ? $userinfo['nickname'] : '',
  142. 'inviter_id' => $inviter_id, #推荐人ID
  143. 'headimgurl' => isset($userinfo['headimgurl']) ? $userinfo['headimgurl'] : '',
  144. );
  145. $member_model->editMember(array('member_id' => $member_info['member_id']), array('member_wxopenid' => $userinfo['openid'], 'member_wxinfo' => serialize($reg_info)), $member_info['member_id']);
  146. }
  147. wkcache($userinfo['unionid'],array(
  148. 'user_info'=>$this->getMemberUser($member_info),
  149. 'key'=>$token,
  150. ));
  151. $return = array('done' => true, 'data' => array('has_user' => true, 'openid' => $userinfo['openid'], 'unionid' => $userinfo['unionid'], 'username' => $member_info["member_name"], 'token' => $token));
  152. return $return;
  153. } elseif (config('ds_config.auto_register')) {//如果开启了自动注册
  154. $logic_connect_api = model('connectapi', 'logic');
  155. //注册会员信息 返回会员信息
  156. $reg_info = array(
  157. 'member_wxopenid' => $userinfo['openid'],
  158. 'member_wxunionid' => $userinfo['unionid'],
  159. 'nickname' => isset($userinfo['nickname']) ? $userinfo['nickname'] : '',
  160. 'inviter_id' => $inviter_id, #推荐人ID
  161. 'headimgurl' => isset($userinfo['headimgurl']) ? $userinfo['headimgurl'] : '',
  162. );
  163. $wx_member = $logic_connect_api->wx_register($reg_info, 'wx');
  164. if (!empty($wx_member)) {
  165. $token = $member_model->getBuyerToken($wx_member['member_id'], $wx_member['member_name'], 'wap');
  166. wkcache($userinfo['unionid'],array(
  167. 'user_info'=>$this->getMemberUser($wx_member),
  168. 'key'=>$token,
  169. ));
  170. $return = array('done' => true, 'data' => array('has_user' => true, 'openid' => $userinfo['openid'], 'unionid' => $userinfo['unionid'], 'username' => $wx_member["member_name"], 'token' => $token));
  171. return $return;
  172. } else {
  173. $return = array('done' => false, 'msg' => lang('register_fail'));
  174. return $return;
  175. }
  176. } else {//绑定
  177. wkcache($userinfo['unionid'],array(
  178. 'wxinfo'=>$userinfo,
  179. ));
  180. $return = array('done' => true, 'data' => array('openid' => $userinfo['openid'], 'unionid' => $userinfo['unionid'], 'ret_url' => '/pages/home/memberbind/Bind'));
  181. return $return;
  182. }
  183. }
  184. public function checkAuth() {
  185. $ref = htmlspecialchars_decode(input('param.ref'));
  186. $inviter_id = intval(input('param.inviter_id')); #推荐人ID
  187. $return = $this->autoLogin($inviter_id);
  188. if ($return['done']) {
  189. $ret_url = '/pages/home/memberlogin/Login';
  190. if (isset($return['data']['ret_url'])) {
  191. $ret_url = $return['data']['ret_url'];
  192. }
  193. $ret_url.=(strpos($ret_url,'?')?'&':'?').'referrer='.urlencode($ref).'&id='.$return['data']['unionid'];
  194. if ($this->from) {
  195. ds_json_encode(10000, '', array('has_user'=>isset($return['data']['has_user'])?1:0,'unionid'=>$return['data']['unionid'],'ret_url' => $ret_url), '', false);
  196. } else {
  197. $this->redirect(config('ds_config.h5_site_url').$ret_url);
  198. }
  199. } else {
  200. if ($this->from) {
  201. ds_json_encode(10001, $return['msg']);
  202. } else {
  203. halt($return['msg']);
  204. }
  205. }
  206. }
  207. /**
  208. * 微信小程序调用接口 用户获取小程序用户信息
  209. */
  210. public function getUser() {
  211. $return = $this->getOpenid();
  212. if ($return['done']) {
  213. ds_json_encode(10000, '', $return['data']);
  214. } else {
  215. ds_json_encode(10001, $return['msg']);
  216. }
  217. }
  218. }