人人商城

auth.ctrl.php 7.4KB

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