人人商城

auth.ctrl.php 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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()->classs('wxapp.platform');
  10. load()->model('wxapp');
  11. $account_platform = new WxAppPlatform();
  12. $dos = array('forward', 'confirm');
  13. $do = in_array($do, $dos) ? $do : 'forward';
  14. $setting = setting_load('platform');
  15. if ($do == 'forward') {
  16. if (empty($_GPC['auth_code'])) {
  17. itoast('授权登录失败,请重试', url('wxapp/manage'), 'error');
  18. }
  19. $auth_info = $account_platform->getAuthInfo($_GPC['auth_code']);
  20. if (is_error($auth_info)) {
  21. itoast('授权登录新建小程序失败:' . $auth_info['message'], url('wxapp/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('wxapp/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_wxapp', array('original' => $account_info['authorizer_info']['user_name']));
  48. if (!empty($account_found)) {
  49. message('小程序已经在系统中接入,是否要更改为授权接入方式? <div><a class="btn btn-primary" href="' . url('wxapp/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. $account_wxapp_data = array(
  58. 'name' => trim($account_info['authorizer_info']['nick_name']),
  59. 'description' => trim($_GPC['description']),
  60. 'original' => trim($account_info['authorizer_info']['user_name']),
  61. 'level' => 1,
  62. 'key' => trim($auth_appid),
  63. 'secret' => trim($_GPC['appsecret']),
  64. 'type' => ACCOUNT_TYPE_APP_AUTH,
  65. 'encodingaeskey'=>$account_platform->encodingaeskey,
  66. 'auth_refresh_token'=>$auth_refresh_token,
  67. 'token' => $account_platform->token,
  68. );
  69. $uniacid = wxapp_account_create($account_wxapp_data);
  70. if (!$uniacid) {
  71. itoast('授权登录新建小程序失败,请重试', url('wxapp/manage'), 'error');
  72. }
  73. $headimg = ihttp_request($account_info['authorizer_info']['head_img']);
  74. $qrcode = ihttp_request($account_info['authorizer_info']['qrcode_url']);
  75. file_put_contents(IA_ROOT . '/attachment/headimg_' . $acid . '.jpg', $headimg['content']);
  76. file_put_contents(IA_ROOT . '/attachment/qrcode_' . $acid . '.jpg', $qrcode['content']);
  77. cache_build_account($uniacid);
  78. itoast('授权登录成功', url('wxapp/post/design_method', array('uniacid' => $uniacid, 'choose_type'=>2)), 'success');
  79. }
  80. if ($do == 'confirm') {
  81. $auth_refresh_token = $_GPC['auth_refresh_token'];
  82. $auth_appid = $_GPC['auth_appid'];
  83. $level = intval($_GPC['level']);
  84. $acid = intval($_GPC['acid']);
  85. $uniacid = intval($_GPC['uniacid']);
  86. pdo_update('account_wxapp', array(
  87. 'auth_refresh_token' => $auth_refresh_token,
  88. 'encodingaeskey' => $account_platform->encodingaeskey,
  89. 'token' => $account_platform->token,
  90. 'level' => $level,
  91. 'key' => $auth_appid,
  92. ), array('acid' => $acid));
  93. pdo_update('account', array('isconnect' => '1', 'type' => ACCOUNT_TYPE_APP_AUTH, 'isdeleted' => 0), array('acid' => $acid));
  94. cache_delete("uniaccount:{$uniacid}");
  95. cache_delete("unisetting:{$uniacid}");
  96. cache_delete("accesstoken:{$acid}");
  97. cache_delete("jsticket:{$acid}");
  98. cache_delete("cardticket:{$acid}");
  99. cache_delete("account:auth:refreshtoken:{$acid}");
  100. $url = url('wxapp/post/design_method', array('acid' => $acid, 'uniacid' => $uniacid, 'choose_type'=>2));
  101. itoast('更改小程序授权接入成功', $url, 'success');
  102. }
  103. if ($do == 'test') {
  104. $auth_appid = '123';
  105. $account_wxapp_data = array(
  106. 'name' => trim($_GPC['name']),
  107. 'description' => trim($_GPC['description']),
  108. 'original' => trim($_GPC['original']),
  109. 'level' => 1,
  110. 'key' => trim($_GPC['appid']),
  111. 'secret' => trim($_GPC['appsecret']),
  112. 'type' => ACCOUNT_TYPE_APP_NORMAL,
  113. );
  114. $account_wxapp_data = array(
  115. 'name' => '阿凡',
  116. 'description' => '123',
  117. 'original' => 'default',
  118. 'level' => 1,
  119. 'key' => trim($auth_appid),
  120. 'secret' => 'empty',
  121. 'type' => ACCOUNT_TYPE_APP_AUTH,
  122. 'encodingaeskey'=>'ak',
  123. 'auth_refresh_token'=>'authken',
  124. 'token' => 'token', );
  125. $uniacid = wxapp_account_create($account_wxapp_data);
  126. if (!$uniacid) {
  127. itoast('授权登录新建小程序失败,请重试', url('wxapp/manage'), 'error');
  128. }
  129. itoast('授权登录成功', url('wxapp/post/design_method', array('uniacid' => $uniacid, 'choose_type'=>2)), 'success');
  130. }