auth.ctrl.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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('miniapp');
  11. $dos = array('forward', 'confirm');
  12. $do = in_array($do, $dos) ? $do : 'forward';
  13. $account_platform = new WxappPlatform();
  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']['verify_type_info']['id'] > '-1') {
  33. $level = 2;
  34. } else {
  35. $level = 1;
  36. }
  37. $account_found = $account_platform->fetchSameAccountByAppid($auth_appid);
  38. if (!empty($account_found)) {
  39. 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');
  40. }
  41. $account_wxapp_data = array(
  42. 'name' => trim($account_info['authorizer_info']['nick_name']),
  43. 'original' => trim($account_info['authorizer_info']['user_name']),
  44. 'level' => $level,
  45. 'key' => trim($auth_appid),
  46. 'type' => ACCOUNT_TYPE_APP_AUTH,
  47. 'encodingaeskey' => $account_platform->encodingaeskey,
  48. 'auth_refresh_token'=>$auth_refresh_token,
  49. 'token' => $account_platform->token,
  50. 'headimg' => $account_info['authorizer_info']['head_img'],
  51. 'qrcode' => $account_info['authorizer_info']['qrcode_url'],
  52. );
  53. $uniacid = miniapp_create($account_wxapp_data);
  54. if (!$uniacid) {
  55. itoast('授权登录新建小程序失败,请重试', url('account/manage'), 'error');
  56. }
  57. cache_build_account($uniacid);
  58. itoast('授权登录成功', url('wxapp/post/design_method', array('uniacid' => $uniacid, 'choose_type'=>2)), 'success');
  59. }
  60. if ($do == 'confirm') {
  61. $auth_refresh_token = safe_gpc_string($_GPC['auth_refresh_token']);
  62. $auth_appid = safe_gpc_string($_GPC['auth_appid']);
  63. $level = intval($_GPC['level']);
  64. $uniacid = intval($_GPC['uniacid']);
  65. if (user_is_founder($_W['uid'])) {
  66. $user_accounts = table('account')->getUniAccountList();
  67. } else {
  68. $user_accounts = uni_user_accounts($_W['uid'], 'wxapp');
  69. }
  70. $user_accounts = array_column($user_accounts, 'uniacid');
  71. if (empty($user_accounts) || !in_array($uniacid, $user_accounts)) {
  72. itoast('账号或用户信息错误!', url('account/post', array('uniacid' => $uniacid)), 'error');
  73. }
  74. pdo_update('account_wxapp', array(
  75. 'auth_refresh_token' => $auth_refresh_token,
  76. 'encodingaeskey' => $account_platform->encodingaeskey,
  77. 'token' => $account_platform->token,
  78. 'level' => $level,
  79. 'key' => $auth_appid,
  80. ), array('uniacid' => $uniacid));
  81. pdo_update('account', array('isconnect' => '1', 'type' => ACCOUNT_TYPE_APP_AUTH, 'isdeleted' => 0), array('uniacid' => $uniacid));
  82. cache_delete(cache_system_key('uniaccount', array('uniacid' => $uniacid)));
  83. cache_delete(cache_system_key('accesstoken', array('uniacid' => $uniacid)));
  84. cache_delete(cache_system_key('account_auth_refreshtoken', array('uniacid' => $uniacid)));
  85. $url = url('wxapp/post/design_method', array('uniacid' => $uniacid, 'choose_type'=>2));
  86. itoast('更改小程序授权接入成功', $url, 'success');
  87. }