Wxauto.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <?php
  2. namespace app\api\controller;
  3. use think\facade\Lang;
  4. /**
  5. * ============================================================================
  6. * DSMall多用户商城
  7. * ============================================================================
  8. * 版权所有 2014-2028 长沙德尚网络科技有限公司,并保留所有权利。
  9. * 网站地址: http://www.csdeshang.com
  10. * ----------------------------------------------------------------------------
  11. * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
  12. * 不允许对程序代码以任何形式任何目的的再发布。
  13. * ============================================================================
  14. * 微信登录控制器
  15. */
  16. class Wxauto extends MobileMall {
  17. public function initialize() {
  18. parent::initialize();
  19. Lang::load(base_path() . 'home/lang/' . config('lang.default_lang') . '/wxauto.lang.php');
  20. $this->wxconfig = model('wechat')->getOneWxconfig();
  21. $from = input('param.from');
  22. $this->from = $from;
  23. if ($from == 'app') {
  24. $logic_payment = model('payment', 'logic');
  25. $result = $logic_payment->getPaymentInfo('wxpay_app');
  26. if (!$result['code']) {
  27. ds_json_encode(10001, $result['msg']);
  28. }
  29. $payment_info = $result['data'];
  30. $this->appId = $payment_info['payment_config']['wx_appid'];
  31. $this->appSecret = $payment_info['payment_config']['wx_appsecret'];
  32. } else {
  33. $this->appId = $this->wxconfig['appid'];
  34. $this->appSecret = $this->wxconfig['appsecret'];
  35. }
  36. }
  37. /**
  38. * @api {POST} api/Wxauto/login 微信登录
  39. * @apiVersion 1.0.0
  40. * @apiGroup Wxauto
  41. *
  42. * @apiParam {String} ref 返回连接
  43. * @apiParam {Int} inviter_id 推荐人id
  44. *
  45. * @apiSuccess {String} code 返回码,10000为成功
  46. * @apiSuccess {String} message 返回消息
  47. * @apiSuccess {Object} result 返回数据
  48. */
  49. public function login() {
  50. $ref = htmlspecialchars_decode(input('param.ref'));
  51. $inviter_id = intval(input('param.inviter_id')); #推荐人ID
  52. $redirect_uri = API_SITE_URL . "/Wxauto/checkAuth?ref=" . urlencode($ref) . "&inviter_id=" . $inviter_id;
  53. $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
  54. ds_json_encode(10000, '', $code_url);
  55. }
  56. //获取openid的统一方法
  57. public function getOpenid() {
  58. $code = input('param.code');
  59. $from = input('param.from');
  60. if (!empty($code)) {
  61. if ($from == 'miniprogram') {
  62. //小程序支付获取openid
  63. $xcx_appid = $this->wxconfig['xcx_appid'];
  64. $xcx_appsecret = $this->wxconfig['xcx_appsecret'];
  65. //说明:https://developers.weixin.qq.com/minigame/dev/document/open-api/login/code2accessToken.html?search-key=jscode2session
  66. $url = "https://api.weixin.qq.com/sns/jscode2session?appid=" . $xcx_appid . "&secret=" . $xcx_appsecret . "&js_code=" . $code . "&grant_type=authorization_code";
  67. if (!$xcx_appid || !$xcx_appsecret) {
  68. $return = array('done' => false, 'msg' => lang('xcx_not_set'));
  69. return $return;
  70. }
  71. } else {
  72. //微信自动登录
  73. $wxappid = $this->appId;
  74. $wxappsecret = $this->appSecret;
  75. $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$wxappid&secret=$wxappsecret&code=$code&grant_type=authorization_code";
  76. if (!$wxappid || !$wxappsecret) {
  77. $return = array('done' => false, 'msg' => lang('wechat_not_set'));
  78. return $return;
  79. }
  80. }
  81. //获取 用户唯一标识,请注意,在未关注公众号时,用户访问公众号的网页,也会产生一个用户和公众号唯一的OpenID
  82. $res = json_decode(http_request($url), true);
  83. if (isset($res['errcode'])) {
  84. $return = array('done' => false, 'msg' => lang('error_code') . $res['errcode'] . ',' . $res['errmsg']);
  85. return $return;
  86. }
  87. $return = array('done' => true, 'data' => $res);
  88. return $return;
  89. } else {
  90. $return = array('done' => false, 'msg' => lang('openid_not_get'));
  91. return $return;
  92. }
  93. }
  94. /**
  95. *
  96. * @param type $inviter_id 推荐人ID
  97. * @return boolean|string|array
  98. */
  99. public function autoLogin($inviter_id) {
  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. $ref = htmlspecialchars_decode(input('param.ref'));
  187. $inviter_id = intval(input('param.inviter_id')); #推荐人ID
  188. $return = $this->autoLogin($inviter_id);
  189. if ($return['done']) {
  190. $ret_url = '/pages/home/memberlogin/Login';
  191. if (isset($return['data']['ret_url'])) {
  192. $ret_url = $return['data']['ret_url'];
  193. }
  194. $ret_url.=(strpos($ret_url,'?')?'&':'?').'referrer='.urlencode($ref).'&id='.$return['data']['unionid'];
  195. if ($this->from) {
  196. ds_json_encode(10000, '', array('has_user'=>isset($return['data']['has_user'])?1:0,'unionid'=>$return['data']['unionid'],'ret_url' => $ret_url), '', false);
  197. } else {
  198. $this->redirect(config('ds_config.h5_site_url').$ret_url);
  199. }
  200. } else {
  201. if ($this->from) {
  202. ds_json_encode(10001, $return['msg']);
  203. } else {
  204. halt($return['msg']);
  205. }
  206. }
  207. }
  208. /**
  209. * 微信小程序调用接口 用户获取小程序用户信息
  210. */
  211. public function getUser() {
  212. $return = $this->getOpenid();
  213. if ($return['done']) {
  214. ds_json_encode(10000, '', $return['data']);
  215. } else {
  216. ds_json_encode(10001, $return['msg']);
  217. }
  218. }
  219. }