人人商城

oauth.ctrl.php 9.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php
  2. /**
  3. * [WeEngine System] Copyright (c) 2014 WE7.CC
  4. * WeEngine is NOT a free software, it under the license terms, visited http://www.we7.cc/ for more details.
  5. */
  6. defined('IN_IA') or exit('Access Denied');
  7. load()->func('communication');
  8. $code = $_GPC['code'];
  9. $scope = $_GPC['scope'];
  10. if (!empty($_SESSION['pay_params'])) {
  11. $setting = uni_setting($_W['uniacid'], array('payment'));
  12. $uniacid = !empty($setting['payment']['wechat']['service']) ? $setting['payment']['wechat']['service'] : $setting['payment']['wechat']['borrow'];
  13. $acid = pdo_getcolumn('uni_account', array('uniacid' => $uniacid), 'default_acid');
  14. $setting = account_fetch($acid);
  15. $_W['account']['oauth'] = array(
  16. 'key' => $setting['key'],
  17. 'secret' => $setting['secret'],
  18. 'type' => $setting['type'],
  19. 'level' => $setting['level'],
  20. 'acid' => $setting['acid'],
  21. );
  22. }
  23. if (empty($_W['account']['oauth']) || empty($code)) {
  24. exit('通信错误,请在微信中重新发起请求');
  25. }
  26. $oauth_account = WeAccount::create($_W['account']['oauth']);
  27. $oauth = $oauth_account->getOauthInfo($code);
  28. if (is_error($oauth) || empty($oauth['openid'])) {
  29. $state = 'we7sid-'.$_W['session_id'];
  30. $str = '';
  31. if(uni_is_multi_acid()) {
  32. $str = "&j={$_W['acid']}";
  33. }
  34. $url = "{$_W['siteroot']}app/index.php?i={$_W['uniacid']}{$str}&c=auth&a=oauth&scope=snsapi_base";
  35. $callback = urlencode($url);
  36. $forward = $oauth_account->getOauthCodeUrl($callback, $state);
  37. header('Location: ' . $forward);
  38. exit;
  39. }
  40. if (!empty($_SESSION['pay_params'])) {
  41. if (!empty($oauth['openid'])) {
  42. header("Location: ".url('mc/cash/wechat', array('payopenid' => $oauth['openid'], 'params' => $_SESSION['pay_params'])));
  43. exit;
  44. }else{
  45. message('非法访问.');
  46. }
  47. }
  48. $_SESSION['oauth_openid'] = $oauth['openid'];
  49. $_SESSION['oauth_acid'] = $_W['account']['oauth']['acid'];
  50. if (intval($_W['account']['level']) == 4) {
  51. $fan = mc_fansinfo($oauth['openid']);
  52. if (!empty($fan)) {
  53. $_SESSION['openid'] = $oauth['openid'];
  54. if (empty($_SESSION['uid'])) {
  55. if (!empty($fan['uid'])) {
  56. $member = mc_fetch($fan['uid'], array('uid'));
  57. if (!empty($member) && $member['uniacid'] == $_W['uniacid']) {
  58. $_SESSION['uid'] = $member['uid'];
  59. }
  60. }
  61. }
  62. } else {
  63. $accObj = WeAccount::create($_W['account']);
  64. $userinfo = $accObj->fansQueryInfo($oauth['openid']);
  65. if(!is_error($userinfo) && !empty($userinfo) && !empty($userinfo['subscribe'])) {
  66. $userinfo['nickname'] = stripcslashes($userinfo['nickname']);
  67. $userinfo['avatar'] = $userinfo['headimgurl'];
  68. $_SESSION['userinfo'] = base64_encode(iserializer($userinfo));
  69. $record = array(
  70. 'openid' => $userinfo['openid'],
  71. 'uid' => 0,
  72. 'acid' => $_W['acid'],
  73. 'uniacid' => $_W['uniacid'],
  74. 'salt' => random(8),
  75. 'updatetime' => TIMESTAMP,
  76. 'nickname' => stripslashes($userinfo['nickname']),
  77. 'follow' => $userinfo['subscribe'],
  78. 'followtime' => $userinfo['subscribe_time'],
  79. 'unfollowtime' => 0,
  80. 'unionid' => $userinfo['unionid'],
  81. 'tag' => base64_encode(iserializer($userinfo))
  82. );
  83. if (!isset($unisetting['passport']) || empty($unisetting['passport']['focusreg'])) {
  84. $email = md5($oauth['openid']).'@we7.cc';
  85. $email_exists_member = pdo_getcolumn('mc_members', array('email' => $email), 'uid');
  86. if (!empty($email_exists_member)) {
  87. $uid = $email_exists_member;
  88. } else {
  89. $default_groupid = pdo_fetchcolumn('SELECT groupid FROM ' .tablename('mc_groups') . ' WHERE uniacid = :uniacid AND isdefault = 1', array(':uniacid' => $_W['uniacid']));
  90. $data = array(
  91. 'uniacid' => $_W['uniacid'],
  92. 'email' => $email,
  93. 'salt' => random(8),
  94. 'groupid' => $default_groupid,
  95. 'createtime' => TIMESTAMP,
  96. 'password' => md5($message['from'] . $data['salt'] . $_W['config']['setting']['authkey']),
  97. 'nickname' => stripslashes($userinfo['nickname']),
  98. 'avatar' => $userinfo['headimgurl'],
  99. 'gender' => $userinfo['sex'],
  100. 'nationality' => $userinfo['country'],
  101. 'resideprovince' => $userinfo['province'] . '省',
  102. 'residecity' => $userinfo['city'] . '市',
  103. );
  104. pdo_insert('mc_members', $data);
  105. $uid = pdo_insertid();
  106. }
  107. $record['uid'] = $uid;
  108. $_SESSION['uid'] = $uid;
  109. }
  110. pdo_insert('mc_mapping_fans', $record);
  111. } else {
  112. $record = array(
  113. 'openid' => $oauth['openid'],
  114. 'nickname' => '',
  115. 'subscribe' => '0',
  116. 'subscribe_time' => '',
  117. 'headimgurl' => '',
  118. );
  119. }
  120. $_SESSION['openid'] = $oauth['openid'];
  121. $_W['fans'] = $record;
  122. $_W['fans']['from_user'] = $record['openid'];
  123. }
  124. }
  125. if (intval($_W['account']['level']) != 4) {
  126. if (!empty($oauth['unionid'])) {
  127. $fan = pdo_get('mc_mapping_fans', array('unionid' => $oauth['unionid'], 'uniacid' => $_W['uniacid']));
  128. if (!empty($fan)) {
  129. if (!empty($fan['uid'])) {
  130. $_SESSION['uid'] = intval($fan['uid']);
  131. }
  132. if (!empty($fan['openid'])) {
  133. $_SESSION['openid'] = strval($fan['openid']);
  134. }
  135. }
  136. } else {
  137. $mc_oauth_fan = mc_oauth_fans($oauth['openid'], $_W['acid']);
  138. if (empty($mc_oauth_fan) && (!empty($_SESSION['openid']) || !empty($_SESSION['uid']))) {
  139. $data = array(
  140. 'acid' => $_W['acid'],
  141. 'oauth_openid' => $oauth['openid'],
  142. 'uid' => intval($_SESSION['uid']),
  143. 'openid' => $_SESSION['openid']
  144. );
  145. pdo_insert('mc_oauth_fans', $data);
  146. }
  147. if (!empty($mc_oauth_fan)) {
  148. if (empty($_SESSION['uid']) && !empty($mc_oauth_fan['uid'])) {
  149. $_SESSION['uid'] = intval($mc_oauth_fan['uid']);
  150. }
  151. if (empty($_SESSION['openid']) && !empty($mc_oauth_fan['openid'])) {
  152. $_SESSION['openid'] = strval($mc_oauth_fan['openid']);
  153. }
  154. }
  155. }
  156. }
  157. if ($scope == 'userinfo' || $scope == 'snsapi_userinfo') {
  158. $userinfo = $oauth_account->getOauthUserInfo($oauth['access_token'], $oauth['openid']);
  159. if (!is_error($userinfo)) {
  160. $userinfo['nickname'] = stripcslashes($userinfo['nickname']);
  161. $userinfo['avatar'] = $userinfo['headimgurl'];
  162. $_SESSION['userinfo'] = base64_encode(iserializer($userinfo));
  163. $fan = pdo_get('mc_mapping_fans', array('openid' => $oauth['openid']));
  164. if (!empty($fan)) {
  165. $record = array();
  166. $record['updatetime'] = TIMESTAMP;
  167. $record['nickname'] = stripslashes($userinfo['nickname']);
  168. $record['tag'] = base64_encode(iserializer($userinfo));
  169. pdo_update('mc_mapping_fans', $record, array('openid' => $fan['openid'], 'acid' => $_W['acid'], 'uniacid' => $_W['uniacid']));
  170. if (!empty($fan['uid']) || !empty($_SESSION['uid'])) {
  171. $uid = $fan['uid'];
  172. if(empty($uid)){
  173. $uid = $_SESSION['uid'];
  174. }
  175. $user = mc_fetch($uid, array('nickname', 'gender', 'residecity', 'resideprovince', 'nationality', 'avatar'));
  176. $record = array();
  177. if(empty($user['nickname']) && !empty($userinfo['nickname'])) {
  178. $record['nickname'] = stripslashes($userinfo['nickname']);
  179. }
  180. if(empty($user['gender']) && !empty($userinfo['sex'])) {
  181. $record['gender'] = $userinfo['sex'];
  182. }
  183. if(empty($user['residecity']) && !empty($userinfo['city'])) {
  184. $record['residecity'] = $userinfo['city'] . '市';
  185. }
  186. if(empty($user['resideprovince']) && !empty($userinfo['province'])) {
  187. $record['resideprovince'] = $userinfo['province'] . '省';
  188. }
  189. if(empty($user['nationality']) && !empty($userinfo['country'])) {
  190. $record['nationality'] = $userinfo['country'];
  191. }
  192. if(empty($user['avatar']) && !empty($userinfo['headimgurl'])) {
  193. $record['avatar'] = $userinfo['headimgurl'];
  194. }
  195. if(!empty($record)) {
  196. mc_update($user['uid'], $record);
  197. }
  198. }
  199. } else {
  200. $record = array(
  201. 'openid' => $oauth['openid'],
  202. 'uid' => 0,
  203. 'acid' => $_W['acid'],
  204. 'uniacid' => $_W['uniacid'],
  205. 'salt' => random(8),
  206. 'updatetime' => TIMESTAMP,
  207. 'nickname' => $userinfo['nickname'],
  208. 'follow' => 0,
  209. 'followtime' => 0,
  210. 'unfollowtime' => 0,
  211. 'tag' => base64_encode(iserializer($userinfo))
  212. );
  213. if (!isset($unisetting['passport']) || empty($unisetting['passport']['focusreg'])) {
  214. $default_groupid = pdo_fetchcolumn('SELECT groupid FROM ' .tablename('mc_groups') . ' WHERE uniacid = :uniacid AND isdefault = 1', array(':uniacid' => $_W['uniacid']));
  215. $data = array(
  216. 'uniacid' => $_W['uniacid'],
  217. 'email' => md5($oauth['openid']).'@we7.cc',
  218. 'salt' => random(8),
  219. 'groupid' => $default_groupid,
  220. 'createtime' => TIMESTAMP,
  221. 'password' => md5($message['from'] . $data['salt'] . $_W['config']['setting']['authkey']),
  222. 'nickname' => $userinfo['nickname'],
  223. 'avatar' => $userinfo['headimgurl'],
  224. 'gender' => $userinfo['sex'],
  225. 'nationality' => $userinfo['country'],
  226. 'resideprovince' => $userinfo['province'] . '省',
  227. 'residecity' => $userinfo['city'] . '市',
  228. );
  229. pdo_insert('mc_members', $data);
  230. $uid = pdo_insertid();
  231. $record['uid'] = $uid;
  232. $_SESSION['uid'] = $uid;
  233. }
  234. pdo_insert('mc_mapping_fans', $record);
  235. }
  236. } else {
  237. message('微信授权获取用户信息失败,错误信息为: ' . $response['message']);
  238. }
  239. }
  240. $forward = urldecode($_SESSION['dest_url']);
  241. $str = '';
  242. if(uni_is_multi_acid()) {
  243. $str = "&j={$_W['acid']}";
  244. }
  245. $forward = strexists($forward, 'i=') ? $forward : "{$forward}&i={$_W['uniacid']}{$str}";
  246. $forward = strexists($forward, '&wxref=mp.weixin.qq.com#wechat_redirect') ? $forward : $forward . '&wxref=mp.weixin.qq.com#wechat_redirect';
  247. header('Location: ' . $forward);
  248. exit;