Wxauto.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <?php
  2. namespace app\api\controller;
  3. use think\facade\Lang;
  4. /**
  5. *
  6. *
  7. * ----------------------------------------------------------------------------
  8. *
  9. * 微信登录控制器
  10. */
  11. class Wxauto extends MobileMall
  12. {
  13. public function initialize()
  14. {
  15. parent::initialize();
  16. Lang::load(base_path() . 'home/lang/' . config('lang.default_lang') . '/wxauto.lang.php');
  17. $this->wxconfig = model('wechat')->getOneWxconfig();
  18. $from = input('param.from');
  19. $this->from = $from;
  20. if ($from == 'app') {
  21. $logic_payment = model('payment', 'logic');
  22. $result = $logic_payment->getPaymentInfo('wxpay_app');
  23. if (!$result['code']) {
  24. ds_json_encode(10001, $result['msg']);
  25. }
  26. $payment_info = $result['data'];
  27. $this->appId = $payment_info['payment_config']['wx_appid'];
  28. $this->appSecret = $payment_info['payment_config']['wx_appsecret'];
  29. } else {
  30. $this->appId = $this->wxconfig['appid'];
  31. $this->appSecret = $this->wxconfig['appsecret'];
  32. }
  33. }
  34. /**
  35. * @api {POST} api/Wxauto/login 微信登录
  36. * @apiVersion 1.0.0
  37. * @apiGroup Wxauto
  38. *
  39. * @apiParam {String} ref 返回连接
  40. * @apiParam {Int} inviter_id 推荐人id
  41. *
  42. * @apiSuccess {String} code 返回码,10000为成功
  43. * @apiSuccess {String} message 返回消息
  44. * @apiSuccess {Object} result 返回数据
  45. */
  46. public function login()
  47. {
  48. $ref = htmlspecialchars_decode(input('param.ref'));
  49. $inviter_id = intval(input('param.inviter_id')); #推荐人ID
  50. $redirect_uri = API_SITE_URL . "/Wxauto/checkAuth?ref=" . urlencode($ref) . "&inviter_id=" . $inviter_id;
  51. $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
  52. ds_json_encode(10000, '', $code_url);
  53. }
  54. //获取openid的统一方法
  55. public function getOpenid()
  56. {
  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. {
  100. //获取openid格外用一个方法,因为有些地方(如小程序支付)不需要获取用户信息,只需要openid
  101. if ($this->from) {
  102. $res = input('param.');
  103. } else {
  104. $return = $this->getOpenid();
  105. if (!$return['done']) {
  106. return $return;
  107. }
  108. $res = $return['data'];
  109. }
  110. // $accessToken5 = model('wechat')->getAccessToken($from);
  111. // if (model('wechat')->error_code != 0) {
  112. // $return = array('done' => false, 'msg' => '错误码' . model('wechat')->error_code . ',' . model('wechat')->error_message);
  113. // return $return;
  114. // }
  115. // 授权 https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140842
  116. //获取用户基本信息(UnionID机制) 必须要先关注公众号 https://mp.weixin.qq.com/wiki?t=resource/res_main&id=mp1421140839
  117. // $url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=$accessToken5&openid=" . $res['openid'] . "&lang=zh_CN"; //获取用户信息
  118. if ($this->from != 'miniprogram') {
  119. $url = "https://api.weixin.qq.com/sns/userinfo?access_token=" . $res['access_token'] . "&openid=" . $res['openid'] . "&lang=zh_CN";
  120. $userinfo = json_decode(http_request($url), true);
  121. if (isset($userinfo['errcode'])) {
  122. $return = array('done' => false, 'msg' => $userinfo['errcode'] . ',' . $userinfo['errmsg']);
  123. return $return;
  124. }
  125. } else {
  126. $userinfo = $res;
  127. }
  128. if (empty($userinfo['unionid'])) {
  129. $return = array('done' => false, 'msg' => lang('unionid_not_get'));
  130. return $return;
  131. }
  132. $member_model = model('member');
  133. $member_info = $member_model->getMemberInfo(array('member_wxunionid' => $userinfo['unionid']));
  134. if (!empty($member_info)) {
  135. //更新信息
  136. $token = $member_model->getBuyerToken($member_info['member_id'], $member_info['member_name'], 'wap');
  137. //如果用户是PC端扫码登录,则未有 member_wxopenid 需更新当前的openid
  138. if (empty($member_info['member_wxopenid'])) {
  139. $reg_info = array(
  140. 'member_wxopenid' => $userinfo['openid'],
  141. 'member_wxunionid' => $userinfo['unionid'],
  142. 'nickname' => isset($userinfo['nickname']) ? $userinfo['nickname'] : '',
  143. 'inviter_id' => $inviter_id, #推荐人ID
  144. 'headimgurl' => isset($userinfo['headimgurl']) ? $userinfo['headimgurl'] : '',
  145. );
  146. $member_model->editMember(array('member_id' => $member_info['member_id']), array('member_wxopenid' => $userinfo['openid'], 'member_wxinfo' => serialize($reg_info)), $member_info['member_id']);
  147. }
  148. wkcache($userinfo['unionid'], array(
  149. 'user_info' => $this->getMemberUser($member_info),
  150. 'key' => $token,
  151. ));
  152. $return = array('done' => true, 'data' => array('has_user' => true, 'openid' => $userinfo['openid'], 'unionid' => $userinfo['unionid'], 'username' => $member_info["member_name"], 'token' => $token));
  153. return $return;
  154. } elseif (config('ds_config.auto_register')) { //如果开启了自动注册
  155. $logic_connect_api = model('connectapi', 'logic');
  156. //注册会员信息 返回会员信息
  157. $reg_info = array(
  158. 'member_wxopenid' => $userinfo['openid'],
  159. 'member_wxunionid' => $userinfo['unionid'],
  160. 'nickname' => isset($userinfo['nickname']) ? $userinfo['nickname'] : '',
  161. 'inviter_id' => $inviter_id, #推荐人ID
  162. 'headimgurl' => isset($userinfo['headimgurl']) ? $userinfo['headimgurl'] : '',
  163. );
  164. $wx_member = $logic_connect_api->wx_register($reg_info, 'wx');
  165. if (!empty($wx_member)) {
  166. $token = $member_model->getBuyerToken($wx_member['member_id'], $wx_member['member_name'], 'wap');
  167. wkcache($userinfo['unionid'], array(
  168. 'user_info' => $this->getMemberUser($wx_member),
  169. 'key' => $token,
  170. ));
  171. $return = array('done' => true, 'data' => array('has_user' => true, 'openid' => $userinfo['openid'], 'unionid' => $userinfo['unionid'], 'username' => $wx_member["member_name"], 'token' => $token));
  172. return $return;
  173. } else {
  174. $return = array('done' => false, 'msg' => lang('register_fail'));
  175. return $return;
  176. }
  177. } else { //绑定
  178. wkcache($userinfo['unionid'], array(
  179. 'wxinfo' => $userinfo,
  180. ));
  181. $return = array('done' => true, 'data' => array('openid' => $userinfo['openid'], 'unionid' => $userinfo['unionid'], 'ret_url' => '/pages/home/memberbind/Bind'));
  182. return $return;
  183. }
  184. }
  185. public function checkAuth()
  186. {
  187. $ref = htmlspecialchars_decode(input('param.ref'));
  188. $inviter_id = intval(input('param.inviter_id')); #推荐人ID
  189. $return = $this->autoLogin($inviter_id);
  190. if ($return['done']) {
  191. $ret_url = '/pages/home/memberlogin/Login';
  192. if (isset($return['data']['ret_url'])) {
  193. $ret_url = $return['data']['ret_url'];
  194. }
  195. $ret_url .= (strpos($ret_url, '?') ? '&' : '?') . 'referrer=' . urlencode($ref) . '&id=' . $return['data']['unionid'];
  196. if ($this->from) {
  197. ds_json_encode(10000, '', array('has_user' => isset($return['data']['has_user']) ? 1 : 0, 'unionid' => $return['data']['unionid'], 'ret_url' => $ret_url), '', false);
  198. } else {
  199. $this->redirect(config('ds_config.h5_site_url') . $ret_url);
  200. }
  201. } else {
  202. if ($this->from) {
  203. ds_json_encode(10001, $return['msg']);
  204. } else {
  205. halt($return['msg']);
  206. }
  207. }
  208. }
  209. /**
  210. * 微信小程序调用接口 用户获取小程序用户信息
  211. */
  212. public function getUser()
  213. {
  214. $return = $this->getOpenid();
  215. if ($return['done']) {
  216. ds_json_encode(10000, '', $return['data']);
  217. } else {
  218. ds_json_encode(10001, $return['msg']);
  219. }
  220. }
  221. }