auth.ctrl.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. load()->classs('weixin.platform');
  9. load()->model('account');
  10. set_time_limit(0);
  11. $dos = array('ticket', 'forward', 'test', 'confirm');
  12. $do = in_array($do, $dos) ? $do : 'forward';
  13. $account_platform = new WeixinPlatform();
  14. if ($do == 'forward') {
  15. if (empty($_GPC['auth_code'])) {
  16. itoast('授权登录失败,请重试', url('account/manage'), 'error');
  17. }
  18. $auth_info = $account_platform->getAuthInfo($_GPC['auth_code']);
  19. if (is_error($auth_info)) {
  20. itoast('授权登录新建公众号失败:' . $auth_info['message'], url('account/manage'), 'error');
  21. }
  22. $auth_refresh_token = $auth_info['authorization_info']['authorizer_refresh_token'];
  23. $auth_appid = $auth_info['authorization_info']['authorizer_appid'];
  24. $account_info = $account_platform->getAuthorizerInfo($auth_appid);
  25. if (is_error($account_info)) {
  26. itoast('授权登录新建公众号失败:' . $account_info['message'], url('account/manage'), 'error');
  27. }
  28. if (!empty($_GPC['test'])) {
  29. echo "此为测试平台接入返回结果:<br/> 公众号名称:{$account_info['authorizer_info']['nick_name']} <br/> 接入状态:成功";
  30. exit;
  31. }
  32. if ($account_info['authorizer_info']['service_type_info']['id'] == '0' || $account_info['authorizer_info']['service_type_info']['id'] == '1') {
  33. if ($account_info['authorizer_info']['verify_type_info']['id'] > '-1') {
  34. $level = '3';
  35. } else {
  36. $level = '1';
  37. }
  38. } elseif ($account_info['authorizer_info']['service_type_info']['id'] == '2') {
  39. if ($account_info['authorizer_info']['verify_type_info']['id'] > '-1') {
  40. $level = '4';
  41. } else {
  42. $level = '2';
  43. }
  44. }
  45. $account_found = $account_platform->fetchSameAccountByAppid($auth_appid);
  46. if (!empty($account_found)) {
  47. message('公众号已经在系统中接入,是否要更改为授权接入方式? <div><a class="btn btn-primary" href="' . url('account/auth/confirm', array('level' => $level, 'auth_refresh_token' => $auth_refresh_token, 'auth_appid' => $auth_appid, 'acid' => $account_found['acid'], 'uniacid' => $account_found['uniacid'])) . '">是</a> &nbsp;&nbsp;<a class="btn btn-default" href="index.php">否</a></div>', '', 'tips');
  48. }
  49. $account_insert = array(
  50. 'name' => $account_info['authorizer_info']['nick_name'],
  51. 'description' => '',
  52. 'groupid' => 0,
  53. );
  54. if(!pdo_insert('uni_account', $account_insert)) {
  55. itoast('授权登录新建公众号失败,请重试', url('account/manage'), 'error');
  56. }
  57. $uniacid = pdo_insertid();
  58. $template = pdo_fetch('SELECT id,title FROM ' . tablename('site_templates') . " WHERE name = 'default'");
  59. $style_insert = array(
  60. 'uniacid' => $uniacid,
  61. 'templateid' => $template['id'],
  62. 'name' => $template['title'] . '_' . random(4),
  63. );
  64. pdo_insert('site_styles', $style_insert);
  65. $styleid = pdo_insertid();
  66. $multi_insert = array(
  67. 'uniacid' => $uniacid,
  68. 'title' => $account_insert['name'],
  69. 'styleid' => $styleid,
  70. );
  71. pdo_insert('site_multi', $multi_insert);
  72. $multi_id = pdo_insertid();
  73. $unisetting_insert = array(
  74. 'creditnames' => iserializer(array(
  75. 'credit1' => array('title' => '积分', 'enabled' => 1),
  76. 'credit2' => array('title' => '余额', 'enabled' => 1)
  77. )),
  78. 'creditbehaviors' => iserializer(array(
  79. 'activity' => 'credit1',
  80. 'currency' => 'credit2'
  81. )),
  82. 'uniacid' => $uniacid,
  83. 'default_site' => $multi_id,
  84. 'sync' => iserializer(array('switch' => 0, 'acid' => '')),
  85. );
  86. pdo_insert('uni_settings', $unisetting_insert);
  87. pdo_insert('mc_groups', array('uniacid' => $uniacid, 'title' => '默认会员组', 'isdefault' => 1));
  88. $account_index_insert = array(
  89. 'uniacid' => $uniacid,
  90. 'type' => ACCOUNT_OAUTH_LOGIN,
  91. 'hash' => random(8),
  92. 'isconnect' => 1
  93. );
  94. pdo_insert('account', $account_index_insert);
  95. $acid = pdo_insertid();
  96. $subaccount_insert = array(
  97. 'acid' => $acid,
  98. 'uniacid' => $uniacid,
  99. 'name' => $account_insert['name'],
  100. 'account' => $account_info['authorizer_info']['alias'],
  101. 'original' => $account_info['authorizer_info']['user_name'],
  102. 'level' => $level,
  103. 'key' => $auth_appid,
  104. 'auth_refresh_token' => $auth_refresh_token,
  105. 'encodingaeskey' => $account_platform->encodingaeskey,
  106. 'token' => $account_platform->token,
  107. );
  108. pdo_insert('account_wechats', $subaccount_insert);
  109. if(is_error($acid)) {
  110. itoast('授权登录新建公众号失败,请重试', url('account/manage'), 'error');
  111. }
  112. if (user_is_vice_founder()) {
  113. uni_user_account_role($uniacid, $_W['uid'], ACCOUNT_MANAGE_NAME_VICE_FOUNDER);
  114. }
  115. if (empty($_W['isfounder'])) {
  116. uni_user_account_role($uniacid, $_W['uid'], ACCOUNT_MANAGE_NAME_OWNER);
  117. if (!empty($_W['user']['owner_uid'])) {
  118. uni_user_account_role($uniacid, $_W['user']['owner_uid'], ACCOUNT_MANAGE_NAME_VICE_FOUNDER);
  119. }
  120. }
  121. pdo_update('uni_account', array('default_acid' => $acid), array('uniacid' => $uniacid));
  122. $headimg = ihttp_request($account_info['authorizer_info']['head_img']);
  123. $qrcode = ihttp_request($account_info['authorizer_info']['qrcode_url']);
  124. file_put_contents(IA_ROOT . '/attachment/headimg_'.$acid.'.jpg', $headimg['content']);
  125. file_put_contents(IA_ROOT . '/attachment/qrcode_'.$acid.'.jpg', $qrcode['content']);
  126. cache_build_account($uniacid);
  127. cache_delete(cache_system_key('proxy_wechatpay_account'));
  128. cache_clean(cache_system_key('user_accounts'));
  129. itoast('授权登录成功', url('account/manage'), 'success');
  130. } elseif ($do == 'confirm') {
  131. $auth_refresh_token = $_GPC['auth_refresh_token'];
  132. $auth_appid = $_GPC['auth_appid'];
  133. $level = intval($_GPC['level']);
  134. $uniacid = intval($_GPC['uniacid']);
  135. if (user_is_founder($_W['uid'])) {
  136. $user_accounts = table('account')->getUniAccountList();
  137. } else {
  138. $user_accounts = uni_user_accounts($_W['uid']);
  139. }
  140. $user_accounts = array_column($user_accounts, 'uniacid');
  141. if (empty($user_accounts) || !in_array($uniacid, $user_accounts)) {
  142. itoast('账号或用户信息错误!', url('account/post', array('uniacid' => $uniacid)), 'error');
  143. }
  144. pdo_update('account_wechats', array(
  145. 'auth_refresh_token' => $auth_refresh_token,
  146. 'encodingaeskey' => $account_platform->encodingaeskey,
  147. 'token' => $account_platform->token,
  148. 'level' => $level,
  149. 'key' => $auth_appid,
  150. ), array('uniacid' => $uniacid));
  151. pdo_update('account', array('isconnect' => '1', 'type' => ACCOUNT_OAUTH_LOGIN, 'isdeleted' => 0), array('uniacid' => $uniacid));
  152. cache_delete(cache_system_key('uniaccount', array('uniacid' => $uniacid)));
  153. cache_delete(cache_system_key('accesstoken', array('uniacid' => $uniacid)));
  154. cache_delete(cache_system_key('account_auth_refreshtoken', array('uniacid' => $uniacid)));
  155. itoast('更改公众号授权接入成功', url('account/post', array('uniacid' => $uniacid)), 'success');
  156. } elseif ($do == 'ticket') {
  157. $post = file_get_contents('php://input');
  158. WeUtility::logging('debug', 'account-ticket' . $post);
  159. $encode_ticket = isimplexml_load_string($post, 'SimpleXMLElement', LIBXML_NOCDATA);
  160. if (empty($post) || empty($encode_ticket)) {
  161. exit('fail');
  162. }
  163. $decode_ticket = aes_decode($encode_ticket->Encrypt, $account_platform->encodingaeskey);
  164. $ticket_xml = isimplexml_load_string($decode_ticket, 'SimpleXMLElement', LIBXML_NOCDATA);
  165. if (empty($ticket_xml)) {
  166. exit('fail');
  167. }
  168. if (!empty($ticket_xml->ComponentVerifyTicket) && $ticket_xml->InfoType == 'component_verify_ticket') {
  169. $ticket = strval($ticket_xml->ComponentVerifyTicket);
  170. setting_save($ticket, 'account_ticket');
  171. }
  172. exit('success');
  173. } elseif ($do == 'test') {
  174. $authurl = $account_platform->getAuthLoginUrl();
  175. echo '<a href="'.$authurl.'%26test=1"><img src="https://open.weixin.qq.com/zh_CN/htmledition/res/assets/res-design-download/icon_button3_2.png" /></a>';
  176. }