link.ctrl.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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()->model('module');
  8. load()->model('miniapp');
  9. load()->model('phoneapp');
  10. load()->model('account');
  11. load()->model('user');
  12. $dos = array('display', 'link_account', 'get_sub_accounts', 'list', 'search_link_account');
  13. $do = in_array($do, $dos) ? $do : 'display';
  14. if ($do == 'list') {
  15. $module_name = safe_gpc_string($_GPC['m']);
  16. $module_info = module_fetch($module_name);
  17. $module_link_uniacids = module_link_uniacid_fetch($_W['uid'], $module_name);
  18. if (!empty($module_link_uniacids)) {
  19. foreach ($module_link_uniacids as $uniacid_val) {
  20. $account_list[] = uni_fetch($uniacid_val['uniacid']);
  21. }
  22. }
  23. template('module/link-list');
  24. }
  25. if ($do == 'display') {
  26. $user_modules = user_modules();
  27. foreach ($user_modules as $key => $module) {
  28. $total_support = 0;
  29. foreach ($module_all_support as $support => $item) {
  30. if ($support == 'welcome_support') {
  31. continue;
  32. }
  33. if ($module[$support] == $item['support']) {
  34. if ($support == 'wxapp_support') {
  35. $total_support += 2;
  36. } else {
  37. $total_support += 1;
  38. }
  39. }
  40. }
  41. if ($total_support < 2) {
  42. unset($user_modules[$key]);
  43. }
  44. }
  45. template('module/link');
  46. }
  47. if ($do == 'search_link_account') {
  48. $module_name = safe_gpc_string($_GPC['module_name']);
  49. $type_sign = safe_gpc_string($_GPC['type_sign']);
  50. if (empty($module_name) || empty($type_sign)) {
  51. iajax(1, '参数不能为空');
  52. }
  53. $module = module_fetch($module_name);
  54. if (empty($module)) {
  55. iajax(1, '模块不存在或已删除');
  56. }
  57. $link_sub_uniacids = table('uni_link_uniacid')->getAllSubUniacidsByModuleName($module_name);
  58. $account_list = uni_search_link_account($module_name, $type_sign);
  59. if (!empty($account_list)) {
  60. foreach ($account_list as $key => $account) {
  61. if (in_array($account['uniacid'], $link_sub_uniacids)) {
  62. unset($account_list[$key]);
  63. continue;
  64. }
  65. $account_list[$key]['type_sign'] = $account_all_type[$account['type']]['type_sign'];
  66. $account_list[$key]['type_title'] = $account_all_type[$account['type']]['title'];
  67. $account_list[$key]['logo'] = is_file(IA_ROOT . '/attachment/headimg_' . $account['acid'] . '.jpg') ? tomedia('headimg_'.$account['acid']. '.jpg').'?time='.time() : './resource/images/nopic-107.png';
  68. $account_list[$key]['module_name'] = $module_name;
  69. }
  70. }
  71. iajax(0, $account_list);
  72. }
  73. if ($do == 'get_sub_accounts') {
  74. $type_sign = safe_gpc_string($_GPC['type_sign']);
  75. if (empty($type_sign)) {
  76. iajax(1, '账号类型不能为空');
  77. }
  78. $main_uniacid = intval($_GPC['main_uniacid']);
  79. if (empty($main_uniacid)) {
  80. iajax(1, '主账号不能为空');
  81. }
  82. $main_account = uni_fetch($main_uniacid);
  83. if (is_error($main_account)) {
  84. iajax(1, $main_account['message']);
  85. }
  86. if ($main_account['type_sign'] != WXAPP_TYPE_SIGN && $main_account['type_sign'] == $type_sign) {
  87. iajax(1, '不可关联此类账号');
  88. }
  89. $module_name = safe_gpc_string($_GPC['module_name']);
  90. if (empty($module_name)) {
  91. iajax(1, '模块信息不能为空');
  92. }
  93. $account_list = uni_user_accounts($_W['uid'], $type_sign);
  94. if (!empty($account_list)) {
  95. $link_main_uniacids = table('uni_link_uniacid')->getAllMainUniacidsByModuleName($module_name); foreach ($account_list as $key => $account) {
  96. if ($account['uniacid'] == $main_uniacid) {
  97. unset($account_list[$key]);
  98. continue;
  99. }
  100. if (in_array($account['uniacid'], $link_main_uniacids)) {
  101. unset($account_list[$key]);
  102. continue;
  103. }
  104. if (in_array($account_all_type[$account['type']]['type_sign'], array(WXAPP_TYPE_SIGN, ALIAPP_TYPE_SIGN, PHONEAPP_TYPE_SIGN, BAIDUAPP_TYPE_SIGN, TOUTIAOAPP_TYPE_SIGN))) {
  105. $miniapp = miniapp_fetch($account['uniacid']);
  106. if (empty($miniapp['version']['modules'])) {
  107. unset($account_list[$key]);
  108. continue;
  109. } else {
  110. $version_module = current($miniapp['version']['modules']);
  111. if ($version_module['name'] != $module_name) {
  112. unset($account_list[$key]);
  113. continue;
  114. }
  115. }
  116. } else {
  117. $uni_modules = uni_modules_by_uniacid($account['uniacid']);
  118. if (!in_array($module_name, array_keys($uni_modules))) {
  119. unset($account_list[$key]);
  120. continue;
  121. }
  122. }
  123. $account_list[$key]['link_type'] = trim($_GPC['link_type']);
  124. $account_list[$key]['type_title'] = $account_all_type[$account['type']]['title'];
  125. $account_list[$key]['type_sign'] = $account_all_type[$account['type']]['type_sign'];
  126. $account_list[$key]['logo'] = is_file(IA_ROOT . '/attachment/headimg_' . $account['acid'] . '.jpg') ? tomedia('headimg_'.$account['acid']. '.jpg').'?time='.time() : './resource/images/nopic-107.png';
  127. $account_list[$key]['module_name'] = $module_name;
  128. }
  129. }
  130. iajax(0, $account_list);
  131. }
  132. if ($do == 'link_account') {
  133. $link_info = safe_gpc_array($_GPC['link_info']);
  134. if (empty($link_info['module_name'])) {
  135. iajax(-1, '应用不能为空');
  136. }
  137. $module = module_fetch($link_info['module_name']);
  138. if (empty($module)) {
  139. iajax(-1, '应用不存在或已被删除');
  140. }
  141. if (empty($link_info['main_account']) || empty($link_info['link_accounts'])) {
  142. iajax(-1, '关联账号不能为空');
  143. }
  144. foreach ($link_info['link_accounts'] as $uniacid => $account) {
  145. if ($uniacid == $link_info['main_account']['uniacid']) {
  146. continue;
  147. }
  148. $version_id = 0;
  149. if (in_array($account['type_sign'], array(WXAPP_TYPE_SIGN, PHONEAPP_TYPE_SIGN, ALIAPP_TYPE_SIGN, BAIDUAPP_TYPE_SIGN, TOUTIAOAPP_TYPE_SIGN))) {
  150. $last_version = miniapp_fetch($account['uniacid']);
  151. if (empty($last_version['version']) || empty($last_version['version']['modules']) || !is_array($last_version['version']['modules'])) {
  152. continue;
  153. }
  154. $module_version = array();
  155. foreach ($last_version['version']['modules'] as $item) {
  156. if ($item['name'] == $link_info['module_name']) {
  157. $module_version = $item;
  158. break;
  159. }
  160. }
  161. if (empty($module_version)) {
  162. continue;
  163. }
  164. $version_id = $last_version['version']['id'];
  165. }
  166. $link_uniacid_table = table('uni_link_uniacid');
  167. $sub_uniacids = $link_uniacid_table->getSubUniacids($uniacid, $link_info['module_name'], $last_version['version']['id']);
  168. if (!empty($sub_uniacids)) {
  169. continue;
  170. }
  171. $has_main_uniacid = $link_uniacid_table->searchWithUniacidModulenameVersionid($uniacid, $link_info['module_name'], $version_id)->get();
  172. if (!empty($has_main_uniacid)) {
  173. $link_uniacid_table->where('id', $has_main_uniacid['id']);
  174. }
  175. $link_uniacid_table->fill(array(
  176. 'uniacid' => $uniacid,
  177. 'link_uniacid' => $link_info['main_account']['uniacid'],
  178. 'version_id' => $version_id,
  179. 'module_name' => $link_info['module_name'],
  180. ))->save();
  181. cache_delete(cache_system_key('module_info', array('module_name' => $link_info['module_name'])));
  182. if (empty($version_id)) {
  183. cache_clean(cache_system_key('unimodules', array('uniacid' => $link_info['main_account']['uniacid'])));
  184. if (!empty($has_main_uniacid['link_uniacid'])) {
  185. cache_clean(cache_system_key('unimodules', array('uniacid' => $has_main_uniacid['link_uniacid'])));
  186. }
  187. } else {
  188. cache_delete(cache_system_key('miniapp_version', array('version_id' => $version_id)));
  189. }
  190. }
  191. iajax(0, '关联成功', url('module/display'));
  192. }