oauth.ctrl.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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 = '';
  30. if (isset($_GPC['state']) && !empty($_GPC['state']) && strexists($_GPC['state'], 'we7sid-')) {
  31. $state = $_GPC['state'];
  32. }
  33. $str = '';
  34. if(uni_is_multi_acid()) {
  35. $str = "&j={$_W['acid']}";
  36. }
  37. $url = "{$_W['siteroot']}app/index.php?i={$_W['uniacid']}{$str}&c=auth&a=oauth&scope=snsapi_base";
  38. $callback = urlencode($url);
  39. $forward = $oauth_account->getOauthCodeUrl($callback, $state);
  40. header('Location: ' . $forward);
  41. exit;
  42. }
  43. if (!empty($_SESSION['pay_params'])) {
  44. if (!empty($oauth['openid'])) {
  45. header("Location: ".url('mc/cash/wechat', array('payopenid' => $oauth['openid'], 'params' => $_SESSION['pay_params'])));
  46. exit;
  47. }else{
  48. message('非法访问.');
  49. }
  50. }
  51. $_SESSION['oauth_openid'] = $oauth['openid'];
  52. $_SESSION['oauth_acid'] = $_W['account']['oauth']['acid'];
  53. if (intval($_W['account']['level']) == ACCOUNT_SERVICE_VERIFY) {
  54. $fan = mc_fansinfo($oauth['openid']);
  55. if (!empty($fan)) {
  56. $_SESSION['openid'] = $oauth['openid'];
  57. if (empty($_SESSION['uid'])) {
  58. if (!empty($fan['uid'])) {
  59. $member = mc_fetch($fan['uid'], array('uid'));
  60. if (!empty($member) && $member['uniacid'] == $_W['uniacid']) {
  61. $_SESSION['uid'] = $member['uid'];
  62. }
  63. }
  64. }
  65. } else {
  66. $accObj = WeAccount::create($_W['account']);
  67. $userinfo = $accObj->fansQueryInfo($oauth['openid']);
  68. if(!is_error($userinfo) && !empty($userinfo) && !empty($userinfo['subscribe'])) {
  69. $userinfo['nickname'] = stripcslashes($userinfo['nickname']);
  70. $userinfo['avatar'] = $userinfo['headimgurl'];
  71. $_SESSION['userinfo'] = base64_encode(iserializer($userinfo));
  72. $record = array(
  73. 'openid' => $userinfo['openid'],
  74. 'uid' => 0,
  75. 'acid' => $_W['acid'],
  76. 'uniacid' => $_W['uniacid'],
  77. 'salt' => random(8),
  78. 'updatetime' => TIMESTAMP,
  79. 'nickname' => stripslashes($userinfo['nickname']),
  80. 'follow' => $userinfo['subscribe'],
  81. 'followtime' => $userinfo['subscribe_time'],
  82. 'unfollowtime' => 0,
  83. 'unionid' => $userinfo['unionid'],
  84. 'tag' => base64_encode(iserializer($userinfo))
  85. );
  86. if (!isset($unisetting['passport']) || empty($unisetting['passport']['focusreg'])) {
  87. $email = md5($oauth['openid']).'@we7.cc';
  88. $email_exists_member = pdo_getcolumn('mc_members', array('email' => $email, 'uniacid' => $_W['uniacid']), 'uid');
  89. if (!empty($email_exists_member)) {
  90. $uid = $email_exists_member;
  91. } else {
  92. $default_groupid = pdo_fetchcolumn('SELECT groupid FROM ' .tablename('mc_groups') . ' WHERE uniacid = :uniacid AND isdefault = 1', array(':uniacid' => $_W['uniacid']));
  93. $data = array(
  94. 'uniacid' => $_W['uniacid'],
  95. 'email' => $email,
  96. 'salt' => random(8),
  97. 'groupid' => $default_groupid,
  98. 'createtime' => TIMESTAMP,
  99. 'password' => md5($message['from'] . $data['salt'] . $_W['config']['setting']['authkey']),
  100. 'nickname' => stripslashes($userinfo['nickname']),
  101. 'avatar' => $userinfo['headimgurl'],
  102. 'gender' => $userinfo['sex'],
  103. 'nationality' => $userinfo['country'],
  104. 'resideprovince' => $userinfo['province'] . '省',
  105. 'residecity' => $userinfo['city'] . '市',
  106. );
  107. pdo_insert('mc_members', $data);
  108. $uid = pdo_insertid();
  109. }
  110. $record['uid'] = $uid;
  111. $_SESSION['uid'] = $uid;
  112. }
  113. pdo_insert('mc_mapping_fans', $record);
  114. $mc_fans_tag_table = table('mc_fans_tag');
  115. $mc_fans_tag_fields = mc_fans_tag_fields();
  116. $fans_tag_update_info = array();
  117. foreach ($userinfo as $fans_field_key => $fans_field_info) {
  118. if (in_array($fans_field_key, array_keys($mc_fans_tag_fields))) {
  119. $fans_tag_update_info[$fans_field_key] = $fans_field_info;
  120. }
  121. $fans_tag_update_info['tagid_list'] = iserializer($fans_tag_update_info['tagis_list']);
  122. }
  123. $fans_tag_exists = $mc_fans_tag_table->getByOpenid($fans_tag_update_info['openid']);
  124. if (!empty($fans_tag_exists)) {
  125. pdo_update('mc_fans_tag', $fans_tag_update_info, array('openid' => $fans_tag_update_info['openid']));
  126. } else {
  127. pdo_insert('mc_fans_tag', $fans_tag_update_info);
  128. }
  129. } else {
  130. $record = array(
  131. 'openid' => $oauth['openid'],
  132. 'nickname' => '',
  133. 'subscribe' => '0',
  134. 'subscribe_time' => '',
  135. 'headimgurl' => '',
  136. );
  137. }
  138. $_SESSION['openid'] = $oauth['openid'];
  139. $_W['fans'] = $record;
  140. $_W['fans']['from_user'] = $record['openid'];
  141. }
  142. }
  143. if (intval($_W['account']['level']) != ACCOUNT_SERVICE_VERIFY) {
  144. if (!empty($oauth['unionid'])) {
  145. $fan = pdo_get('mc_mapping_fans', array('unionid' => $oauth['unionid'], 'uniacid' => $_W['uniacid']));
  146. if (!empty($fan)) {
  147. if (!empty($fan['uid'])) {
  148. $_SESSION['uid'] = intval($fan['uid']);
  149. }
  150. if (!empty($fan['openid'])) {
  151. $_SESSION['openid'] = strval($fan['openid']);
  152. }
  153. }
  154. } else {
  155. $mc_oauth_fan = mc_oauth_fans($oauth['openid'], $_W['acid']);
  156. if (empty($mc_oauth_fan) && (!empty($_SESSION['openid']) || !empty($_SESSION['uid']))) {
  157. $data = array(
  158. 'acid' => $_W['acid'],
  159. 'oauth_openid' => $oauth['openid'],
  160. 'uid' => intval($_SESSION['uid']),
  161. 'openid' => $_SESSION['openid']
  162. );
  163. pdo_insert('mc_oauth_fans', $data);
  164. }
  165. if (!empty($mc_oauth_fan)) {
  166. if (empty($_SESSION['uid']) && !empty($mc_oauth_fan['uid'])) {
  167. $_SESSION['uid'] = intval($mc_oauth_fan['uid']);
  168. }
  169. if (empty($_SESSION['openid']) && !empty($mc_oauth_fan['openid'])) {
  170. $_SESSION['openid'] = strval($mc_oauth_fan['openid']);
  171. }
  172. }
  173. }
  174. }
  175. if ($scope == 'userinfo' || $scope == 'snsapi_userinfo') {
  176. $userinfo = $oauth_account->getOauthUserInfo($oauth['access_token'], $oauth['openid']);
  177. if (!is_error($userinfo)) {
  178. $userinfo['nickname'] = stripcslashes($userinfo['nickname']);
  179. $userinfo['avatar'] = $userinfo['headimgurl'];
  180. $_SESSION['userinfo'] = base64_encode(iserializer($userinfo));
  181. $fan = pdo_get('mc_mapping_fans', array('openid' => $oauth['openid']));
  182. if (!empty($fan)) {
  183. $record = array();
  184. $record['updatetime'] = TIMESTAMP;
  185. $record['nickname'] = stripslashes($userinfo['nickname']);
  186. $record['tag'] = base64_encode(iserializer($userinfo));
  187. if (empty($fan['unionid'])) {
  188. $record['unionid'] = !empty($userinfo['unionid']) ? $userinfo['unionid'] : '';
  189. }
  190. pdo_update('mc_mapping_fans', $record, array('openid' => $fan['openid'], 'acid' => $_W['acid'], 'uniacid' => $_W['uniacid']));
  191. if (!empty($fan['uid']) || !empty($_SESSION['uid'])) {
  192. $uid = $fan['uid'];
  193. if(empty($uid)){
  194. $uid = $_SESSION['uid'];
  195. }
  196. $user = mc_fetch($uid, array('nickname', 'gender', 'residecity', 'resideprovince', 'nationality', 'avatar'));
  197. $record = array();
  198. if(empty($user['nickname']) && !empty($userinfo['nickname'])) {
  199. $record['nickname'] = stripslashes($userinfo['nickname']);
  200. }
  201. if(empty($user['gender']) && !empty($userinfo['sex'])) {
  202. $record['gender'] = $userinfo['sex'];
  203. }
  204. if(empty($user['residecity']) && !empty($userinfo['city'])) {
  205. $record['residecity'] = $userinfo['city'] . '市';
  206. }
  207. if(empty($user['resideprovince']) && !empty($userinfo['province'])) {
  208. $record['resideprovince'] = $userinfo['province'] . '省';
  209. }
  210. if(empty($user['nationality']) && !empty($userinfo['country'])) {
  211. $record['nationality'] = $userinfo['country'];
  212. }
  213. if(empty($user['avatar']) && !empty($userinfo['headimgurl'])) {
  214. $record['avatar'] = $userinfo['headimgurl'];
  215. }
  216. if(!empty($record)) {
  217. mc_update($user['uid'], $record);
  218. }
  219. }
  220. } else {
  221. $record = array(
  222. 'openid' => $oauth['openid'],
  223. 'uid' => 0,
  224. 'acid' => $_W['acid'],
  225. 'uniacid' => $_W['uniacid'],
  226. 'salt' => random(8),
  227. 'updatetime' => TIMESTAMP,
  228. 'nickname' => $userinfo['nickname'],
  229. 'follow' => 0,
  230. 'followtime' => 0,
  231. 'unfollowtime' => 0,
  232. 'tag' => base64_encode(iserializer($userinfo)),
  233. 'unionid' => !empty($userinfo['unionid']) ? $userinfo['unionid'] : ''
  234. );
  235. if (!isset($unisetting['passport']) || empty($unisetting['passport']['focusreg'])) {
  236. $default_groupid = pdo_fetchcolumn('SELECT groupid FROM ' .tablename('mc_groups') . ' WHERE uniacid = :uniacid AND isdefault = 1', array(':uniacid' => $_W['uniacid']));
  237. $data = array(
  238. 'uniacid' => $_W['uniacid'],
  239. 'email' => md5($oauth['openid']).'@we7.cc',
  240. 'salt' => random(8),
  241. 'groupid' => $default_groupid,
  242. 'createtime' => TIMESTAMP,
  243. 'password' => md5($message['from'] . $data['salt'] . $_W['config']['setting']['authkey']),
  244. 'nickname' => $userinfo['nickname'],
  245. 'avatar' => $userinfo['headimgurl'],
  246. 'gender' => $userinfo['sex'],
  247. 'nationality' => $userinfo['country'],
  248. 'resideprovince' => $userinfo['province'] . '省',
  249. 'residecity' => $userinfo['city'] . '市',
  250. );
  251. pdo_insert('mc_members', $data);
  252. $uid = pdo_insertid();
  253. $record['uid'] = $uid;
  254. $_SESSION['uid'] = $uid;
  255. }
  256. pdo_insert('mc_mapping_fans', $record);
  257. }
  258. } else {
  259. message('微信授权获取用户信息失败,错误信息为: ' . $response['message']);
  260. }
  261. }
  262. $forward = urldecode($_SESSION['dest_url']);
  263. $str = '';
  264. if(uni_is_multi_acid()) {
  265. $str = "&j={$_W['acid']}";
  266. }
  267. $forward = strexists($forward, 'i=') ? $forward : "{$forward}&i={$_W['uniacid']}{$str}";
  268. if (strpos($forward, '&wxref=mp.weixin.qq.com')) {
  269. $forward = str_replace('&wxref=mp.weixin.qq.com', '', $forward) . '&wxref=mp.weixin.qq.com#wechat_redirect';
  270. } else {
  271. $forward .= '&wxref=mp.weixin.qq.com#wechat_redirect';
  272. }
  273. header('Location: ' . $forward);
  274. exit;