account.mod.php 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428
  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. function uni_account_type_sign($type_sign = '') {
  8. $all_account_type_sign = array(
  9. ACCOUNT_TYPE_SIGN => array(
  10. 'contain_type' => array(ACCOUNT_TYPE_OFFCIAL_NORMAL, ACCOUNT_TYPE_OFFCIAL_AUTH),
  11. 'icon' => 'wi wi-wx-circle',
  12. 'createurl' => url('account/post-step'),
  13. 'title' => '公众号',
  14. ),
  15. WXAPP_TYPE_SIGN => array(
  16. 'contain_type' => array(ACCOUNT_TYPE_APP_NORMAL, ACCOUNT_TYPE_APP_AUTH),
  17. 'icon' => 'wi wi-wxapp',
  18. 'createurl' => url('wxapp/post/design_method'),
  19. 'title' => '微信小程序',
  20. ),
  21. WEBAPP_TYPE_SIGN => array(
  22. 'contain_type' => array(ACCOUNT_TYPE_WEBAPP_NORMAL),
  23. 'icon' => 'wi wi-pc-circle',
  24. 'createurl' => url('account/create', array('sign' => 'webapp')),
  25. 'title' => 'PC',
  26. ),
  27. PHONEAPP_TYPE_SIGN => array(
  28. 'contain_type' => array(ACCOUNT_TYPE_PHONEAPP_NORMAL),
  29. 'icon' => 'wi wi-app',
  30. 'createurl' => url('account/create', array('sign' => 'phoneapp')),
  31. 'title' => 'APP',
  32. ),
  33. XZAPP_TYPE_SIGN => array(
  34. 'contain_type' => array(ACCOUNT_TYPE_XZAPP_NORMAL, ACCOUNT_TYPE_XZAPP_AUTH),
  35. 'icon' => 'wi wi-xzapp',
  36. 'createurl' => url('xzapp/post-step'),
  37. 'title' => '熊掌号',
  38. ),
  39. ALIAPP_TYPE_SIGN => array(
  40. 'contain_type' => array(ACCOUNT_TYPE_ALIAPP_NORMAL),
  41. 'icon' => 'wi wi-aliapp',
  42. 'createurl' => url('account/create', array('sign' => 'aliapp')),
  43. 'title' => '支付宝小程序',
  44. ),
  45. BAIDUAPP_TYPE_SIGN => array(
  46. 'contain_type' => array(ACCOUNT_TYPE_BAIDUAPP_NORMAL),
  47. 'icon' => 'wi wi-baiduapp',
  48. 'createurl' => url('account/create', array('sign' => 'baiduapp')),
  49. 'title' => '百度小程序',
  50. ),
  51. TOUTIAOAPP_TYPE_SIGN => array(
  52. 'contain_type' => array(ACCOUNT_TYPE_TOUTIAOAPP_NORMAL),
  53. 'icon' => 'wi wi-toutiaoapp',
  54. 'createurl' => url('account/create', array('sign' => 'toutiaoapp')),
  55. 'title' => '头条小程序',
  56. ),
  57. );
  58. if (!empty($type_sign)) {
  59. return !empty($all_account_type_sign[$type_sign]) ? $all_account_type_sign[$type_sign] : array();
  60. }
  61. return $all_account_type_sign;
  62. }
  63. function uni_account_type($type = 0) {
  64. $all_account_type = array(
  65. ACCOUNT_TYPE_OFFCIAL_NORMAL => array(
  66. 'title' => '公众号',
  67. 'type_sign' => ACCOUNT_TYPE_SIGN,
  68. 'table_name' => 'account_wechats',
  69. 'module_support_name' => MODULE_SUPPORT_ACCOUNT_NAME,
  70. 'module_support_value' => MODULE_SUPPORT_ACCOUNT,
  71. ),
  72. ACCOUNT_TYPE_OFFCIAL_AUTH => array(
  73. 'title' => '公众号',
  74. 'type_sign' => ACCOUNT_TYPE_SIGN,
  75. 'table_name' => 'account_wechats',
  76. 'module_support_name' => MODULE_SUPPORT_ACCOUNT_NAME,
  77. 'module_support_value' => MODULE_SUPPORT_ACCOUNT,
  78. ),
  79. ACCOUNT_TYPE_APP_NORMAL => array(
  80. 'title' => '微信小程序',
  81. 'type_sign' => WXAPP_TYPE_SIGN,
  82. 'table_name' => 'account_wxapp',
  83. 'support_version' => 1,
  84. 'version_tablename' => 'wxapp_versions',
  85. 'module_support_name' => MODULE_SUPPORT_WXAPP_NAME,
  86. 'module_support_value' => MODULE_SUPPORT_WXAPP,
  87. ),
  88. ACCOUNT_TYPE_APP_AUTH => array(
  89. 'title' => '微信小程序',
  90. 'type_sign' => WXAPP_TYPE_SIGN,
  91. 'table_name' => 'account_wxapp',
  92. 'support_version' => 1,
  93. 'version_tablename' => 'wxapp_versions',
  94. 'module_support_name' => MODULE_SUPPORT_WXAPP_NAME,
  95. 'module_support_value' => MODULE_SUPPORT_WXAPP,
  96. ),
  97. ACCOUNT_TYPE_WEBAPP_NORMAL => array(
  98. 'title' => 'PC',
  99. 'type_sign' => WEBAPP_TYPE_SIGN,
  100. 'table_name' => 'account_webapp',
  101. 'module_support_name' => MODULE_SUPPORT_WEBAPP_NAME,
  102. 'module_support_value' => MODULE_SUPPORT_WEBAPP,
  103. ),
  104. ACCOUNT_TYPE_PHONEAPP_NORMAL => array(
  105. 'title' => 'APP',
  106. 'type_sign' => PHONEAPP_TYPE_SIGN,
  107. 'table_name' => 'account_phoneapp',
  108. 'support_version' => 1,
  109. 'version_tablename' => 'wxapp_versions',
  110. 'module_support_name' => MODULE_SUPPORT_PHONEAPP_NAME,
  111. 'module_support_value' => MODULE_SUPPORT_PHONEAPP,
  112. ),
  113. ACCOUNT_TYPE_XZAPP_NORMAL => array(
  114. 'title' => '熊掌号',
  115. 'type_sign' => XZAPP_TYPE_SIGN,
  116. 'table_name' => 'account_xzapp',
  117. 'module_support_name' => MODULE_SUPPORT_XZAPP_NAME,
  118. 'module_support_value' => MODULE_SUPPORT_XZAPP,
  119. ),
  120. ACCOUNT_TYPE_XZAPP_AUTH => array(
  121. 'title' => '熊掌号',
  122. 'type_sign' => XZAPP_TYPE_SIGN,
  123. 'table_name' => 'account_xzapp',
  124. 'module_support_name' => MODULE_SUPPORT_XZAPP_NAME,
  125. 'module_support_value' => MODULE_SUPPORT_XZAPP,
  126. ),
  127. ACCOUNT_TYPE_ALIAPP_NORMAL => array(
  128. 'title' => '支付宝小程序',
  129. 'type_sign' => ALIAPP_TYPE_SIGN,
  130. 'table_name' => 'account_aliapp',
  131. 'support_version' => 1,
  132. 'version_tablename' => 'wxapp_versions',
  133. 'module_support_name' => MODULE_SUPPORT_ALIAPP_NAME,
  134. 'module_support_value' => MODULE_SUPPORT_ALIAPP,
  135. ),
  136. ACCOUNT_TYPE_BAIDUAPP_NORMAL => array(
  137. 'title' => '百度小程序',
  138. 'type_sign' => BAIDUAPP_TYPE_SIGN,
  139. 'table_name' => 'account_baiduapp',
  140. 'support_version' => 1,
  141. 'version_tablename' => 'wxapp_versions',
  142. 'module_support_name' => MODULE_SUPPORT_BAIDUAPP_NAME,
  143. 'module_support_value' => MODULE_SUPPORT_BAIDUAPP,
  144. ),
  145. ACCOUNT_TYPE_TOUTIAOAPP_NORMAL => array(
  146. 'title' => '头条小程序',
  147. 'type_sign' => TOUTIAOAPP_TYPE_SIGN,
  148. 'table_name' => 'account_toutiaoapp',
  149. 'support_version' => 1,
  150. 'version_tablename' => 'wxapp_versions',
  151. 'module_support_name' => MODULE_SUPPORT_TOUTIAOAPP_NAME,
  152. 'module_support_value' => MODULE_SUPPORT_TOUTIAOAPP,
  153. ),
  154. );
  155. if (!empty($type)) {
  156. return !empty($all_account_type[$type]) ? $all_account_type[$type] : array();
  157. }
  158. return $all_account_type;
  159. }
  160. function uni_account_extra_info($uniacid) {
  161. $uniacid = max(0, intval($uniacid));
  162. if (empty($uniacid)) {
  163. return array();
  164. }
  165. $account = pdo_get('account', array('uniacid' => $uniacid));
  166. if (empty($account) || empty($account['type'])) {
  167. return array();
  168. }
  169. $account_extra = uni_account_type($account['type']);
  170. return $account_extra;
  171. }
  172. /**
  173. * 获取当前用户可操作的所有公众号
  174. * @param int $uid 指定操作用户
  175. * @return array
  176. */
  177. function uni_owned($uid = 0, $is_uni_fetch = true, $type = 'app') {
  178. global $_W;
  179. $uid = intval($uid) > 0 ? intval($uid) : $_W['uid'];
  180. $uniaccounts = array();
  181. $user_accounts = uni_user_accounts($uid, $type);
  182. if (empty($user_accounts)) {
  183. return $uniaccounts;
  184. }
  185. if (!empty($user_accounts) && !empty($is_uni_fetch)) {
  186. foreach ($user_accounts as &$row) {
  187. $row = uni_fetch($row['uniacid']);
  188. }
  189. }
  190. return $user_accounts;
  191. }
  192. /**
  193. * 获取用户可操作的所有公众号
  194. * @param int $uid 要查找的用户
  195. * @return array()
  196. */
  197. function uni_user_accounts($uid = 0, $type = 'app') {
  198. global $_W;
  199. $uid = intval($uid) > 0 ? intval($uid) : $_W['uid'];
  200. if (!in_array($type, array('app', 'wxapp', 'webapp', 'phoneapp', 'xzapp', 'aliapp', 'baiduapp', 'toutiaoapp'))) {
  201. $type = 'app';
  202. }
  203. $type = $type == 'app' ? 'wechats' : $type;
  204. $cachekey = cache_system_key('user_accounts', array('type' => $type, 'uid' => $uid));
  205. $cache = cache_load($cachekey);
  206. if (!empty($cache)) {
  207. return $cache;
  208. }
  209. $select_fields = 'w.acid, w.uniacid, w.name, a.type';
  210. if (in_array($type, array('wechats', 'wxapp', 'xzapp'))) {
  211. $select_fields .= ', w.level, w.key, w.secret, w.token';
  212. }
  213. $where = '';
  214. $params = array();
  215. if (!user_is_founder($uid, true)) {
  216. $select_fields .= ', u.role';
  217. $where .= " LEFT JOIN " . tablename('uni_account_users') . " u ON u.uniacid = w.uniacid WHERE u.uid = :uid AND u.role IN(:role1, :role2) ";
  218. $params[':uid'] = $uid;
  219. $params[':role1'] = ACCOUNT_MANAGE_NAME_OWNER;
  220. $params[':role2'] = ACCOUNT_MANAGE_NAME_VICE_FOUNDER;
  221. }
  222. $where .= !empty($where) ? " AND a.isdeleted <> 1 AND u.role IS NOT NULL" : " WHERE a.isdeleted <> 1";
  223. $sql = "SELECT " . $select_fields . " FROM " . tablename('account_' . $type) . " w LEFT JOIN " . tablename('account') . " a ON a.acid = w.acid AND a.uniacid = w.uniacid" . $where;
  224. $result = pdo_fetchall($sql, $params, 'uniacid');
  225. cache_write($cachekey, $result);
  226. return $result;
  227. }
  228. /**
  229. * 获取某一公众号的主管理员信息
  230. * @param int $uniacid 指定的公众号
  231. * @return array
  232. */
  233. function account_owner($uniacid = 0) {
  234. global $_W;
  235. load()->model('user');
  236. $uniacid = intval($uniacid);
  237. if (empty($uniacid)) {
  238. return array();
  239. }
  240. $ownerid = pdo_getcolumn('uni_account_users', array('uniacid' => $uniacid, 'role' => 'owner'), 'uid');
  241. if (empty($ownerid)) {
  242. $ownerid = pdo_getcolumn('uni_account_users', array('uniacid' => $uniacid, 'role' => 'vice_founder'), 'uid');
  243. if (empty($ownerid)) {
  244. $founders = explode(',', $_W['config']['setting']['founder']);
  245. $ownerid = $founders[0];
  246. }
  247. }
  248. $owner = user_single($ownerid);
  249. if (empty($owner)) {
  250. return array();
  251. }
  252. return $owner;
  253. }
  254. /**
  255. * 获取当前公号的所有子公众号
  256. * @param int $uniacid 公众号ID
  257. * @return array 当前公号下所有子公众号
  258. */
  259. function uni_accounts($uniacid = 0) {
  260. global $_W;
  261. $uniacid = empty($uniacid) ? $_W['uniacid'] : intval($uniacid);
  262. $account_info = pdo_get('account', array('uniacid' => $uniacid));
  263. if (!empty($account_info)) {
  264. $account_tablename = uni_account_type($account_info['type']);
  265. $account_tablename = $account_tablename['table_name'];
  266. $accounts = pdo_fetchall("SELECT w.*, a.type, a.isconnect FROM " . tablename('account') . " a INNER JOIN " . tablename($account_tablename) . " w USING(acid) WHERE a.uniacid = :uniacid AND a.isdeleted <> 1 ORDER BY a.acid ASC", array(':uniacid' => $uniacid), 'acid');
  267. }
  268. return !empty($accounts) ? $accounts : array();
  269. }
  270. /**
  271. * 获取指定统一公号下默认子号的的信息
  272. * @param int $uniacid 公众号ID
  273. * @return array 当前公众号信息
  274. */
  275. function uni_fetch($uniacid = 0) {
  276. global $_W;
  277. $uniacid = empty($uniacid) ? $_W['uniacid'] : intval($uniacid);
  278. $account_api = WeAccount::createByUniacid($uniacid);
  279. if (is_error($account_api)) {
  280. return $account_api;
  281. }
  282. $account_api->__toArray();
  283. return $account_api;
  284. }
  285. /**
  286. * 获取指定公号在站内商城购买的模块
  287. * @param int $uniacid 公众号id
  288. * @return array 模块列表
  289. */
  290. function uni_site_store_buy_goods($uniacid, $type = STORE_TYPE_MODULE) {
  291. $cachekey = cache_system_key('site_store_buy', array('type' => $type, 'uniacid' => $uniacid));
  292. $site_store_buy_goods = cache_load($cachekey);
  293. if (!empty($site_store_buy_goods)) {
  294. return $site_store_buy_goods;
  295. }
  296. $store_table = table('store');
  297. if ($type != STORE_TYPE_API) {
  298. $store_table->searchWithEndtime();
  299. $site_store_buy_goods = $store_table->searchAccountBuyGoods($uniacid, $type);
  300. $site_store_buy_goods = array_keys($site_store_buy_goods);
  301. } else {
  302. $site_store_buy_goods = $store_table->searchAccountBuyGoods($uniacid, $type);
  303. $setting = uni_setting_load('statistics', $uniacid);
  304. $use_number = isset($setting['statistics']['use']) ? intval($setting['statistics']['use']) : 0;
  305. $site_store_buy_goods = $site_store_buy_goods - $use_number;
  306. }
  307. cache_write($cachekey, $site_store_buy_goods);
  308. return $site_store_buy_goods;
  309. }
  310. /**
  311. * 获取指定公号下所有安装模块及模块信息
  312. * 公众号的权限是owner所有套餐内的全部模块权限
  313. * @param int $uniacid 公众号id
  314. * @param boolean $enabled 是否只显示可用模块
  315. * @return array 模块列表
  316. */
  317. function uni_modules_by_uniacid($uniacid) {
  318. global $_W;
  319. load()->model('user');
  320. load()->model('module');
  321. load()->model('store');
  322. $account_info = uni_fetch($uniacid);
  323. $founders = explode(',', $_W['config']['setting']['founder']);
  324. $owner_uid = pdo_getall('uni_account_users', array('uniacid' => $uniacid, 'role' => array('owner', 'vice_founder')), array('uid', 'role'), 'role');
  325. $owner_uid = !empty($owner_uid['owner']) ? $owner_uid['owner']['uid'] : (!empty($owner_uid['vice_founder']) ? $owner_uid['vice_founder']['uid'] : 0);
  326. $cachekey = cache_system_key('unimodules', array('uniacid' => $uniacid));
  327. $modules = cache_load($cachekey);
  328. if (empty($modules)) {
  329. $enabled_modules = table('modules')->getNonRecycleModules();
  330. if (!empty($owner_uid) && !in_array($owner_uid, $founders)) {
  331. $group_modules = table('account')->accountGroupModules($uniacid);
  332. $goods_type = 0;
  333. $all_type_info = store_goods_type_info();
  334. foreach ($all_type_info as $info) {
  335. if ($info['sign'] == $account_info['type_sign']) {
  336. $goods_type = $info['type'];
  337. break;
  338. }
  339. }
  340. if ($goods_type) {
  341. $site_store_buy_goods = uni_site_store_buy_goods($uniacid, $goods_type);
  342. if (!empty($site_store_buy_goods)) {
  343. foreach ($site_store_buy_goods as $store_goods_module_name) {
  344. $store_goods_info = pdo_get('site_store_goods', array('module' => $store_goods_module_name));
  345. if ($store_goods_info['is_wish'] == 1) {
  346. $enabled_modules[$store_goods_module_name] = pdo_get('modules_cloud', array('name' => $store_goods_module_name));
  347. }
  348. }
  349. $group_modules = array_merge($group_modules, $site_store_buy_goods);
  350. }
  351. }
  352. $user_modules = user_modules($owner_uid);
  353. if (!empty($user_modules)) {
  354. $group_modules = array_unique(array_merge($group_modules, array_keys($user_modules)));
  355. $group_modules = array_intersect(array_keys($enabled_modules), $group_modules);
  356. }
  357. } else {
  358. $group_modules = array_keys($enabled_modules);
  359. }
  360. cache_write($cachekey, $group_modules);
  361. $modules = $group_modules;
  362. }
  363. $modules = array_merge($modules, module_system());
  364. $module_list = array();
  365. if (!empty($modules)) {
  366. foreach ($modules as $name) {
  367. if (empty($name)) {
  368. continue;
  369. }
  370. $module_info = module_fetch($name);
  371. if (empty($module_info)) {
  372. $module_cloud_info = pdo_get('modules_cloud', array('name' => $name));
  373. $store_goods_info = pdo_get('site_store_goods', array('module' => $name));
  374. if (!empty($module_cloud_info) && $store_goods_info['is_wish'] == 1) {
  375. $module_info = $module_cloud_info;
  376. }
  377. }
  378. if (!empty($module_info['recycle_info'])) {
  379. foreach (module_support_type() as $support => $value) {
  380. if ($module_info['recycle_info'][$support] > 0 && $module_info[$support] == $value['support']) {
  381. $module_info[$support] = $value['not_support'];
  382. }
  383. }
  384. }
  385. if ($module_info[MODULE_SUPPORT_ACCOUNT_NAME] != MODULE_SUPPORT_ACCOUNT &&
  386. in_array($account_info['type'], array(ACCOUNT_TYPE_OFFCIAL_NORMAL, ACCOUNT_TYPE_OFFCIAL_AUTH))) {
  387. continue;
  388. }
  389. if ($module_info[MODULE_SUPPORT_WEBAPP_NAME] != MODULE_SUPPORT_WEBAPP &&
  390. in_array($account_info['type'], array(ACCOUNT_TYPE_WEBAPP_NORMAL))) {
  391. continue;
  392. }
  393. if ($module_info[MODULE_SUPPORT_PHONEAPP_NAME] != MODULE_SUPPORT_PHONEAPP &&
  394. in_array($account_info['type'], array(ACCOUNT_TYPE_PHONEAPP_NORMAL))) {
  395. continue;
  396. }
  397. if ($module_info[MODULE_SUPPORT_XZAPP_NAME] != MODULE_SUPPORT_XZAPP &&
  398. in_array($account_info['type'], array(ACCOUNT_TYPE_XZAPP_NORMAL, ACCOUNT_TYPE_XZAPP_AUTH))) {
  399. continue;
  400. }
  401. if ($module_info[MODULE_SUPPORT_ALIAPP_NAME] != MODULE_SUPPORT_ALIAPP &&
  402. in_array($account_info['type'], array(ACCOUNT_TYPE_ALIAPP_NORMAL))) {
  403. continue;
  404. }
  405. if ($module_info[MODULE_SUPPORT_WXAPP_NAME] != MODULE_SUPPORT_WXAPP &&
  406. $module_info[MODULE_SUPPORT_ACCOUNT_NAME] != MODULE_SUPPORT_ACCOUNT &&
  407. in_array($account_info['type'], array(ACCOUNT_TYPE_APP_NORMAL, ACCOUNT_TYPE_APP_AUTH))) {
  408. continue;
  409. }
  410. if ($module_info[MODULE_SUPPORT_SYSTEMWELCOME_NAME] == MODULE_SUPPORT_SYSTEMWELCOME &&
  411. $module_info[MODULE_SUPPORT_ACCOUNT_NAME] != MODULE_SUPPORT_ACCOUNT &&
  412. $module_info[MODULE_SUPPORT_WEBAPP_NAME] != MODULE_SUPPORT_WEBAPP &&
  413. $module_info[MODULE_SUPPORT_PHONEAPP_NAME] != MODULE_SUPPORT_PHONEAPP &&
  414. $module_info[MODULE_SUPPORT_ALIAPP_NAME] != MODULE_SUPPORT_ALIAPP &&
  415. $module_info[MODULE_SUPPORT_WXAPP_NAME] != MODULE_SUPPORT_WXAPP) {
  416. continue;
  417. }
  418. if (!empty($module_info)) {
  419. $module_list[$name] = $module_info;
  420. }
  421. }
  422. }
  423. $module_list['core'] = array('title' => '系统事件处理模块', 'name' => 'core', 'issystem' => 1, 'enabled' => 1, 'isdisplay' => 0);
  424. return $module_list;
  425. }
  426. /**
  427. * 获取当前公号下所有安装模块及模块信息
  428. * @param boolean $enabled 是否只显示可用模块
  429. * @return array 模块列表
  430. */
  431. function uni_modules() {
  432. global $_W;
  433. return uni_modules_by_uniacid($_W['uniacid']);
  434. }
  435. function uni_modules_app_binding() {
  436. global $_W;
  437. $cachekey = cache_system_key('unimodules_binding', array('uniacid' => $_W['uniacid']));
  438. $cache = cache_load($cachekey);
  439. if (!empty($cache)) {
  440. return $cache;
  441. }
  442. load()->model('module');
  443. $result = array();
  444. $modules = uni_modules();
  445. if(!empty($modules)) {
  446. foreach($modules as $module) {
  447. if($module['type'] == 'system') {
  448. continue;
  449. }
  450. $entries = module_app_entries($module['name'], array('home', 'profile', 'shortcut', 'function', 'cover'));
  451. if(empty($entries)) {
  452. continue;
  453. }
  454. if($module['type'] == '') {
  455. $module['type'] = 'other';
  456. }
  457. $result[$module['name']] = array(
  458. 'name' => $module['name'],
  459. 'type' => $module['type'],
  460. 'title' => $module['title'],
  461. 'entries' => array(
  462. 'cover' => $entries['cover'],
  463. 'home' => $entries['home'],
  464. 'profile' => $entries['profile'],
  465. 'shortcut' => $entries['shortcut'],
  466. 'function' => $entries['function']
  467. )
  468. );
  469. unset($module);
  470. }
  471. }
  472. cache_write($cachekey, $result);
  473. return $result;
  474. }
  475. function uni_groups($groupids = array(), $show_all = false) {
  476. load()->model('module');
  477. global $_W;
  478. $cache_key_id = 0;
  479. if (!empty($groupids)) {
  480. foreach ($groupids as $groupid_key => $groupid_val) {
  481. $groupid_val = intval($groupid_val);
  482. $groupids[$groupid_key] = $groupid_val;
  483. $cache_key_id .= $groupid_val;
  484. }
  485. }
  486. $cachekey = cache_system_key('uni_groups', array('groupids' => $cache_key_id));
  487. $list = cache_load($cachekey);
  488. if (empty($list)) {
  489. $condition = ' WHERE uniacid = 0 AND uid = 0';
  490. if (!empty($groupids)) {
  491. foreach ($groupids as $groupid_key => $groupid_val) {
  492. $groupids[$groupid_key] = intval($groupid_val);
  493. }
  494. $groupids_string = implode(',', $groupids);
  495. $condition .= ' AND id IN (' . $groupids_string . ')';
  496. }
  497. $list = pdo_fetchall("SELECT * FROM " . tablename('uni_group') . $condition . " ORDER BY id DESC", array(), 'id');
  498. if (!empty($groupids)) {
  499. if (in_array('-1', $groupids)) {
  500. $list[-1] = array('id' => -1, 'name' => '所有服务', 'modules' => array('title' => '系统所有模块'), 'templates' => array('title' => '系统所有模板'));
  501. }
  502. if (in_array('0', $groupids)) {
  503. $list[0] = array('id' => 0, 'name' => '基础服务', 'modules' => array('title' => '系统模块'), 'templates' => array('title' => '系统模板'));
  504. }
  505. }
  506. if (!empty($list)) {
  507. foreach ($list as $k => &$row) {
  508. $modules = (array)iunserializer($row['modules']);
  509. $row['modules_all'] = array();
  510. if (!empty($modules)) {
  511. foreach ($modules as $type => $modulenames) {
  512. $type = $type == 'modules' ? 'account' : $type;
  513. if (empty($modulenames) || !is_array($modulenames)) {
  514. continue;
  515. }
  516. $row['modules_all'] = array_merge($row['modules_all'], $modulenames);
  517. $row[$type] = empty($row[$type]) ? $modulenames : array_merge($row[$type], $modulenames);
  518. }
  519. $row['modules_all'] = array_unique($row['modules_all']);
  520. }
  521. if (!empty($row['templates'])) {
  522. $row['templates'] = (array)iunserializer($row['templates']);
  523. if (!empty($row['templates'])) {
  524. $row['templates'] = pdo_getall('site_templates', array('id' => $row['templates']), array('id', 'name', 'title'), 'name');
  525. }
  526. }
  527. }
  528. }
  529. cache_write($cachekey, $list);
  530. }
  531. $group_list = array();
  532. if (empty($list)) {
  533. return $group_list;
  534. }
  535. $module_support_type = module_support_type();
  536. $module_support_type_sign = array_keys(uni_account_type_sign());
  537. $modules_info = array();
  538. foreach ($list as &$item) {
  539. if (empty($item['modules_all'])) {
  540. continue;
  541. }
  542. $modules_all = $item['modules_all'];
  543. $item['modules_all'] = array();
  544. foreach ($modules_all as $key => $name) {
  545. if (empty($modules_info[$name]) || !is_array($modules_info[$name])) {
  546. $modules_info[$name] = module_fetch($name);
  547. }
  548. $moduleinfo = $modules_info[$name];
  549. if (empty($moduleinfo)) {
  550. continue;
  551. }
  552. foreach ($module_support_type as $support => $info) {
  553. if ($moduleinfo[$support] == $info['support'] && in_array($name, (array)$item[$info['type']])) {
  554. $moduleinfo['group_support'][$support] = $info['support'];
  555. } else {
  556. $moduleinfo['group_support'][$support] = $info['not_support'];
  557. }
  558. }
  559. $item['modules_all'][$name] = $moduleinfo;
  560. }
  561. if (!empty($item['account'])) {
  562. $account_modules = $item['account'];
  563. $item['account'] = array();
  564. foreach ($account_modules as $name) {
  565. $item['account'][$name] = $modules_info[$name];
  566. }
  567. }
  568. if (!empty($item['wxapp'])) {
  569. $wxapp_modules = $item['wxapp'];
  570. $item['wxapp'] = array();
  571. foreach ($wxapp_modules as $name) {
  572. $item['wxapp'][$name] = $modules_info[$name];
  573. }
  574. }
  575. if (!empty($item['webapp'])) {
  576. $wxapp_modules = $item['webapp'];
  577. $item['webapp'] = array();
  578. foreach ($wxapp_modules as $name) {
  579. $item['webapp'][$name] = $modules_info[$name];
  580. }
  581. }
  582. if (!empty($item['phoneapp'])) {
  583. $phoneapp_modules = $item['phoneapp'];
  584. $item['phoneapp'] = array();
  585. foreach ($phoneapp_modules as $name) {
  586. $item['phoneapp'][$name] = $modules_info[$name];
  587. }
  588. }
  589. if (!empty($item['xzapp'])) {
  590. $xzapp_modules = $item['xzapp'];
  591. $item['xzapp'] = array();
  592. foreach ($xzapp_modules as $name) {
  593. $item['xzapp'][$name] = $modules_info[$name];
  594. }
  595. }
  596. if (!empty($item['aliapp'])) {
  597. $aliapp_modules = $item['aliapp'];
  598. $item['aliapp'] = array();
  599. foreach ($aliapp_modules as $name) {
  600. $item['aliapp'][$name] = $modules_info[$name];
  601. }
  602. }
  603. if (!empty($item['baiduapp'])) {
  604. $baiduapp_modules = $item['baiduapp'];
  605. $item['baiduapp'] = array();
  606. foreach ($baiduapp_modules as $name) {
  607. $item['baiduapp'][$name] = $modules_info[$name];
  608. }
  609. }
  610. if (!empty($item['toutiaoapp'])) {
  611. $toutiaoapp_modules = $item['toutiaoapp'];
  612. $item['toutiaoapp'] = array();
  613. foreach ($toutiaoapp_modules as $name) {
  614. $item['toutiaoapp'][$name] = $modules_info[$name];
  615. }
  616. }
  617. }
  618. if (!empty($groupids)) {
  619. foreach ($groupids as $id) {
  620. $group_list[$id] = $list[$id];
  621. }
  622. } else {
  623. if (user_is_vice_founder() && empty($show_all)) {
  624. $founder_own_table = table('users_founder_own_uni_groups');
  625. $founder_own_uni_groups = $founder_own_table->getOwnUniGroupsByFounderUid($_W['uid']);
  626. foreach ($list as $group_key => $group) {
  627. if (!in_array($group_key, array_keys($founder_own_uni_groups))) {
  628. unset($list[$group_key]);
  629. continue;
  630. }
  631. }
  632. }
  633. $group_list = $list;
  634. }
  635. return $group_list;
  636. }
  637. function uni_templates() {
  638. global $_W;
  639. load()->model('user');
  640. $owneruid = pdo_fetchcolumn("SELECT uid FROM ".tablename('uni_account_users')." WHERE uniacid = :uniacid AND role = 'owner'", array(':uniacid' => $_W['uniacid']));
  641. $owner = user_single(array('uid' => $owneruid));
  642. if (empty($owner) || user_is_founder($owner['uid'])) {
  643. $groupid = '-1';
  644. } else {
  645. $groupid = $owner['groupid'];
  646. }
  647. if ($groupid == '-1') {
  648. $templates = pdo_fetchall("SELECT * FROM " . tablename('site_templates') . " ORDER BY id ASC", array(), 'id');
  649. return $templates;
  650. }
  651. $extend = pdo_getall('uni_account_group', array('uniacid' => $_W['uniacid']), array(), 'groupid'); $uni_extend = pdo_get('uni_group', array('uniacid' => $_W['uniacid'])); $owner_extend_groups = table('users_extra_group')->getUniGroupsByUid($_W['uid']);
  652. $owner_extend_templates = table('users_extra_templates')->getExtraTemplatesIdsByUid($_W['uid']);
  653. $template_default = pdo_fetchall("SELECT * FROM " . tablename('site_templates') . " WHERE name = 'default'", array(), 'id');
  654. if (empty($groupid) && empty($extend) && empty($uni_extend) && empty($owner_extend_groups) && empty($owner_extend_templates)) {
  655. return $template_default;
  656. }
  657. $group = pdo_fetch("SELECT id, name, package FROM ".tablename('users_group')." WHERE id = :id", array(':id' => $groupid));
  658. $packageids = iunserializer($group['package']);
  659. if (!is_array($packageids)) {
  660. $packageids = array();
  661. }
  662. if (!empty($extend)) {
  663. foreach ($extend as $extend_packageid => $row) {
  664. $packageids[] = $extend_packageid;
  665. }
  666. }
  667. if (!empty($uni_extend)) {
  668. $packageids[] = $uni_extend['id'];
  669. }
  670. if (!empty($owner_extend_groups)) {
  671. foreach ($owner_extend_groups as $id => $row) {
  672. $packageids[] = $id;
  673. }
  674. }
  675. if (empty($packageids)) {
  676. return $template_default;
  677. }
  678. if (in_array('-1', $packageids)) {
  679. return pdo_getall('site_templates', array(), array(), 'id', 'id DESC');
  680. }
  681. $template_ids = array();
  682. $wechatgroup = pdo_getall('uni_group', array('id' => $packageids));
  683. if (!empty($wechatgroup)) {
  684. foreach ($wechatgroup as $row) {
  685. $row['templates'] = iunserializer($row['templates']);
  686. if (!is_array($row['templates']) || empty($row['templates'])) {
  687. continue;
  688. }
  689. foreach ($row['templates'] as $templateid) {
  690. if (!is_numeric($templateid)) {
  691. continue;
  692. }
  693. $template_ids[$templateid] = $templateid;
  694. }
  695. }
  696. }
  697. $template_ids[] = 1;
  698. if (is_array($owner_extend_templates)) {
  699. $template_ids = array_merge($template_ids, array_keys($owner_extend_templates));
  700. }
  701. return pdo_getall('site_templates', array('id' => $template_ids), array(), 'id', 'id DESC');
  702. }
  703. /**
  704. * 保存公众号的配置数据
  705. * @param string $name
  706. * @param mixed $value
  707. * @return boolean
  708. */
  709. function uni_setting_save($name, $value) {
  710. global $_W;
  711. $uniacid = !empty($_W['uniacid']) ? $_W['uniacid'] : $_W['account']['uniacid'];
  712. if (empty($name)) {
  713. return false;
  714. }
  715. if (is_array($value)) {
  716. $value = serialize($value);
  717. }
  718. $unisetting = pdo_get('uni_settings', array('uniacid' => $uniacid), array('uniacid'));
  719. if (!empty($unisetting)) {
  720. pdo_update('uni_settings', array($name => $value), array('uniacid' => $uniacid));
  721. } else {
  722. pdo_insert('uni_settings', array($name => $value, 'uniacid' => $uniacid));
  723. }
  724. cache_delete(cache_system_key('uniaccount', array('uniacid' => $uniacid)));
  725. return true;
  726. }
  727. /**
  728. * 获取公众号的配置项
  729. * @param string | array $name
  730. * @param int $uniacid 统一公号id, uniacid
  731. * @return array 设置项
  732. */
  733. function uni_setting_load($name = '', $uniacid = 0) {
  734. global $_W;
  735. $uniacid = empty($uniacid) ? $_W['uniacid'] : $uniacid;
  736. $cachekey = cache_system_key('unisetting', array('uniacid' => $uniacid));
  737. $unisetting = cache_load($cachekey);
  738. if (empty($unisetting)) {
  739. $unisetting = pdo_get('uni_settings', array('uniacid' => $uniacid));
  740. if (!empty($unisetting)) {
  741. $serialize = array('site_info', 'stat', 'oauth', 'passport', 'uc', 'notify',
  742. 'creditnames', 'default_message', 'creditbehaviors', 'payment',
  743. 'recharge', 'tplnotice', 'mcplugin', 'statistics', 'bind_domain', 'remote');
  744. foreach ($unisetting as $key => &$row) {
  745. if (in_array($key, $serialize) && !empty($row)) {
  746. $row = (array)iunserializer($row);
  747. }
  748. }
  749. } else {
  750. $unisetting = array();
  751. }
  752. cache_write($cachekey, $unisetting);
  753. }
  754. if (empty($unisetting)) {
  755. return array();
  756. }
  757. if (empty($name)) {
  758. return $unisetting;
  759. }
  760. if (!is_array($name)) {
  761. $name = array($name);
  762. }
  763. return array_elements($name, $unisetting);
  764. }
  765. if (!function_exists('uni_setting')) {
  766. function uni_setting($uniacid = 0, $fields = '*', $force_update = false) {
  767. global $_W;
  768. load()->model('account');
  769. if ($fields == '*') {
  770. $fields = '';
  771. }
  772. return uni_setting_load($fields, $uniacid);
  773. }
  774. }
  775. /**
  776. * 获取当前公号的默认子号,如果未指定则获取第一个公众号为默认子号
  777. * @param int $uniacid 公众号ID
  778. * @return array 当前公号下的默认子号信息
  779. */
  780. function uni_account_default($uniacid = 0) {
  781. global $_W;
  782. $uniacid = empty($uniacid) ? $_W['uniacid'] : intval($uniacid);
  783. $uni_account = pdo_fetch("SELECT * FROM ".tablename('uni_account')." a LEFT JOIN ".tablename('account')." w ON a.uniacid = w.uniacid AND a.default_acid = w.acid WHERE a.uniacid = :uniacid", array(':uniacid' => $uniacid));
  784. if (empty($uni_account)) {
  785. $uni_account = pdo_fetch("SELECT * FROM ".tablename('uni_account')." a LEFT JOIN ".tablename('account')." w ON a.uniacid = w.uniacid WHERE a.uniacid = :uniacid ORDER BY w.acid DESC", array(':uniacid' => $uniacid));
  786. }
  787. if (!empty($uni_account)) {
  788. $account_tablename = uni_account_type($uni_account['type']);
  789. $account_tablename = $account_tablename['table_name'];
  790. $account = pdo_get($account_tablename, array('acid' => $uni_account['acid']));
  791. if (empty($account)) {
  792. $account['uniacid'] = $uni_account['uniacid'];
  793. $account['acid'] = $uni_account['default_acid'];
  794. }
  795. $account['type'] = $uni_account['type'];
  796. $account['isconnect'] = $uni_account['isconnect'];
  797. $account['isdeleted'] = $uni_account['isdeleted'];
  798. $account['endtime'] = $uni_account['endtime'];
  799. return $account;
  800. }
  801. }
  802. function uni_user_account_role($uniacid, $uid, $role) {
  803. $vice_account = array(
  804. 'uniacid' => intval($uniacid),
  805. 'uid' => intval($uid),
  806. 'role' => trim($role)
  807. );
  808. $account_user = pdo_get('uni_account_users', $vice_account, array('id'));
  809. if (!empty($account_user)) {
  810. return false;
  811. }
  812. return pdo_insert('uni_account_users', $vice_account);
  813. }
  814. /**
  815. * 获取公众号和小程序真实数量
  816. * @param $uid
  817. * @param $role
  818. * @return array
  819. */
  820. function uni_owner_account_nums($uid, $role) {
  821. $account_all_type = uni_account_type();
  822. $account_all_type_sign = array_keys(uni_account_type_sign());
  823. foreach ($account_all_type_sign as $type_info) {
  824. $key_name = $type_info . '_num';
  825. $num[$key_name] = 0;
  826. }
  827. $uniacocunts = table('account')->searchAccountList();
  828. if (!empty($uniacocunts)) {
  829. $uni_account_users_table = table('uni_account_users');
  830. $uni_account_users_table->searchWithRole($role);
  831. $all_account = $uni_account_users_table->getCommonUserOwnAccountUniacids($uid);
  832. foreach ($all_account as $account) {
  833. foreach ($account_all_type as $type_key => $type_info) {
  834. if ($type_key == $account['type']) {
  835. $key_name = $type_info['type_sign'] . '_num';
  836. $num[$key_name] += 1;
  837. continue;
  838. }
  839. }
  840. }
  841. }
  842. return $num;
  843. }
  844. function uni_update_week_stat() {
  845. global $_W;
  846. $cachekey = cache_system_key('stat_todaylock', array('uniacid' => $_W['uniacid']));
  847. $cache = cache_load($cachekey);
  848. if(!empty($cache) && $cache['expire'] > TIMESTAMP) {
  849. return true;
  850. }
  851. $seven_days = array(
  852. date('Ymd', strtotime('-1 days')),
  853. date('Ymd', strtotime('-2 days')),
  854. date('Ymd', strtotime('-3 days')),
  855. date('Ymd', strtotime('-4 days')),
  856. date('Ymd', strtotime('-5 days')),
  857. date('Ymd', strtotime('-6 days')),
  858. date('Ymd', strtotime('-7 days')),
  859. );
  860. $week_stat_fans = pdo_getall('stat_fans', array('date' => $seven_days, 'uniacid' => $_W['uniacid']), '', 'date');
  861. $stat_update_yes = false;
  862. foreach ($seven_days as $sevens) {
  863. if (empty($week_stat_fans[$sevens]) || $week_stat_fans[$sevens]['cumulate'] <=0) {
  864. $stat_update_yes = true;
  865. break;
  866. }
  867. }
  868. if (empty($stat_update_yes)) {
  869. return true;
  870. }
  871. $account = uni_fetch($_W['uniacid']);
  872. if (is_error($account)) {
  873. return $account;
  874. }
  875. if ($account['level'] == ACCOUNT_SUBSCRIPTION_VERIFY || $account['level'] == ACCOUNT_SERVICE_VERIFY) {
  876. $account_obj = WeAccount::createByUniacid();
  877. $weixin_stat = $account_obj->getFansStat();
  878. if (empty($weixin_stat) || is_error($weixin_stat)) {
  879. return error(-1, '调用微信接口错误');
  880. }
  881. }
  882. foreach($seven_days as $sevens) {
  883. if ($account['level'] == ACCOUNT_SUBSCRIPTION_VERIFY || $account['level'] == ACCOUNT_SERVICE_VERIFY) {
  884. $update_stat = array(
  885. 'uniacid' => $_W['uniacid'],
  886. 'new' => $weixin_stat[$sevens]['new'],
  887. 'cancel' => $weixin_stat[$sevens]['cancel'],
  888. 'cumulate' => $weixin_stat[$sevens]['cumulate'],
  889. 'date' => $sevens,
  890. );
  891. } else {
  892. $update_stat = array(
  893. 'cumulate' => pdo_fetchcolumn("SELECT COUNT(*) FROM " . tablename('mc_mapping_fans') . " WHERE acid = :acid AND uniacid = :uniacid AND follow = :follow AND followtime < :endtime", array(':acid' => $_W['acid'], ':uniacid' => $_W['uniacid'], ':endtime' => strtotime($sevens)+86400, ':follow' => 1)),
  894. 'date' => $sevens,
  895. 'new' => $week_stat_fans[$sevens]['new'],
  896. 'cancel' => $week_stat_fans[$sevens]['cancel'],
  897. 'uniacid' => $_W['uniacid'],
  898. );
  899. }
  900. if(empty($week_stat_fans[$sevens])) {
  901. pdo_insert('stat_fans', $update_stat);
  902. } elseif (empty($week_stat_fans[$sevens]['cumulate']) || $week_stat_fans[$sevens]['cumulate'] < 0) {
  903. pdo_update('stat_fans', $update_stat, array('id' => $week_stat_fans[$sevens]['id']));
  904. }
  905. }
  906. cache_write($cachekey, array('expire' => TIMESTAMP + 7200));
  907. return true;
  908. }
  909. /**
  910. * 将公众号置顶
  911. * @param int $uniacid
  912. * @param int $rank
  913. */
  914. function uni_account_rank_top($uniacid) {
  915. global $_W;
  916. if (!empty($_W['isfounder'])) {
  917. $max_rank = pdo_getcolumn('uni_account', array(), 'max(rank)');
  918. pdo_update('uni_account', array('rank' => ($max_rank + 1)), array('uniacid' => $uniacid));
  919. }else {
  920. $max_rank = pdo_getcolumn('uni_account_users', array('uid' => $_W['uid']), 'max(rank)');
  921. pdo_update('uni_account_users', array('rank' => ($max_rank + 1)), array('uniacid' => $uniacid, 'uid' => $_W['uid']));
  922. }
  923. return true;
  924. }
  925. /**
  926. * 创建子公众号
  927. * @param int $uniacid 指定统一公号
  928. * @param array $account 子公号信息
  929. * @return int 新创建的子公号 acid
  930. */
  931. function account_create($uniacid, $account) {
  932. global $_W;
  933. $account_all_type = uni_account_type();
  934. $type = $account['type'];
  935. $type_sign = $account_all_type[$type]['type_sign'];
  936. unset($account['type']);
  937. $accountdata = array('uniacid' => $uniacid, 'type' => $type, 'hash' => random(8));
  938. $user_create_account_info = permission_user_account_num();
  939. if (empty($_W['isfounder']) && empty($user_create_account_info["usergroup_{$type_sign}_limit"])) {
  940. $accountdata['endtime'] = strtotime('+1 month', time());
  941. pdo_insert('site_store_create_account', array('endtime' => strtotime('+1 month', time()), 'uid' => $_W['uid'], 'uniacid' => $uniacid, 'type' => $type));
  942. }
  943. pdo_insert('account', $accountdata);
  944. $acid = pdo_insertid();
  945. $account['acid'] = $acid;
  946. $account['uniacid'] = $uniacid;
  947. if (in_array($type_sign, array(ACCOUNT_TYPE_SIGN, XZAPP_TYPE_SIGN))) {
  948. $account['token'] = random(32);
  949. $account['encodingaeskey'] = random(43);
  950. }
  951. pdo_insert($account_all_type[$type]['table_name'], $account);
  952. return $acid;
  953. }
  954. /**
  955. * 获取指定子公号信息
  956. * @param int $acid 子公号acid
  957. * @return array
  958. */
  959. function account_fetch($acid) {
  960. $account_info = pdo_get('account', array('acid' => $acid));
  961. if (empty($account_info)) {
  962. return error(-1, '公众号不存在');
  963. }
  964. return uni_fetch($account_info['uniacid']);
  965. }
  966. /*
  967. * 获取某个公众号的所有人和套餐有效期限(如果没有所有人,默认属于创始人,服务创始人)
  968. * */
  969. function uni_setmeal($uniacid = 0) {
  970. global $_W;
  971. if(!$uniacid) {
  972. $uniacid = $_W['uniacid'];
  973. }
  974. $owneruid = pdo_fetchcolumn("SELECT uid FROM ".tablename('uni_account_users')." WHERE uniacid = :uniacid AND role = 'owner'", array(':uniacid' => $uniacid));
  975. if(empty($owneruid)) {
  976. $user = array(
  977. 'uid' => -1,
  978. 'username' => '创始人',
  979. 'timelimit' => '未设置',
  980. 'groupid' => '-1',
  981. 'groupname' => '所有服务'
  982. );
  983. return $user;
  984. }
  985. load()->model('user');
  986. $groups = pdo_getall('users_group', array(), array('id', 'name'), 'id');
  987. $owner = user_single(array('uid' => $owneruid));
  988. $user = array(
  989. 'uid' => $owner['uid'],
  990. 'username' => $owner['username'],
  991. 'groupid' => $owner['groupid'],
  992. 'groupname' => $groups[$owner['groupid']]['name']
  993. );
  994. if(empty($owner['endtime'])) {
  995. $user['timelimit'] = date('Y-m-d', $owner['starttime']) . ' ~ 无限制' ;
  996. } else {
  997. if($owner['endtime'] <= TIMESTAMP) {
  998. $user['timelimit'] = '已到期';
  999. } else {
  1000. $year = 0;
  1001. $month = 0;
  1002. $day = 0;
  1003. $endtime = $owner['endtime'];
  1004. $time = strtotime('+1 year');
  1005. if ($endtime > $time) {
  1006. $year = $year + 1;
  1007. $time = strtotime("+1 year", $time);
  1008. }
  1009. $time = strtotime("-1 year", $time);
  1010. $time = strtotime("+1 month", $time);
  1011. if ($endtime > $time) {
  1012. $month = $month + 1;
  1013. $time = strtotime("+1 month", $time);
  1014. }
  1015. $time = strtotime("-1 month", $time);
  1016. $time = strtotime("+1 day", $time);
  1017. if ($endtime > $time) {
  1018. $day = $day + 1;
  1019. $time = strtotime("+1 day", $time);
  1020. }
  1021. if (empty($year)) {
  1022. $timelimit = empty($month)? $day.'天' : date('Y-m-d', $owner['starttime']) . '~'. date('Y-m-d', $owner['endtime']);
  1023. }else {
  1024. $timelimit = date('Y-m-d', $owner['starttime']) . '~'. date('Y-m-d', $owner['endtime']);
  1025. }
  1026. $user['timelimit'] = $timelimit;
  1027. }
  1028. }
  1029. return $user;
  1030. }
  1031. /*
  1032. * 检测公众号是否只有多个子号。如果有多个子号,返回true;
  1033. * */
  1034. function uni_is_multi_acid($uniacid = 0) {
  1035. global $_W;
  1036. if(!$uniacid) {
  1037. $uniacid = $_W['uniacid'];
  1038. }
  1039. $cachekey = cache_system_key('unicount', array('uniacid' => $uniacid));
  1040. $nums = cache_load($cachekey);
  1041. $nums = intval($nums);
  1042. if(!$nums) {
  1043. $nums = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename('account_wechats') . ' WHERE uniacid = :uniacid', array(':uniacid' => $_W['uniacid']));
  1044. cache_write($cachekey, $nums);
  1045. }
  1046. if($nums <= 1) {
  1047. return false;
  1048. }
  1049. return true;
  1050. }
  1051. /**
  1052. * 删除公众号
  1053. * @param string $acid 微信公众号的acid
  1054. */
  1055. function account_delete($acid) {
  1056. global $_W;
  1057. load()->func('file');
  1058. load()->model('module');
  1059. load()->model('job');
  1060. $jobid = 0;
  1061. //判断是不是主公众号
  1062. $account = pdo_get('uni_account', array('default_acid' => $acid));
  1063. if ($account) {
  1064. $uniacid = $account['uniacid'];
  1065. $state = permission_account_user_role($_W['uid'], $uniacid);
  1066. if (!in_array($state, array(ACCOUNT_MANAGE_NAME_OWNER, ACCOUNT_MANAGE_NAME_FOUNDER, ACCOUNT_MANAGE_NAME_VICE_FOUNDER))) {
  1067. itoast('没有该公众号操作权限!', url('account/recycle'), 'error');
  1068. }
  1069. if($uniacid == $_W['uniacid']) {
  1070. isetcookie('__uniacid', '');
  1071. }
  1072. cache_delete(cache_system_key('uniaccount', array('uniacid' => $uniacid)));
  1073. $modules = array();
  1074. //获取全部规则
  1075. $rules = pdo_fetchall("SELECT id, module FROM ".tablename('rule')." WHERE uniacid = '{$uniacid}'");
  1076. if (!empty($rules)) {
  1077. foreach ($rules as $index => $rule) {
  1078. $deleteid[] = intval($rule['id']);
  1079. }
  1080. pdo_delete('rule', "id IN ('".implode("','", $deleteid)."')");
  1081. }
  1082. $subaccount = pdo_fetchall("SELECT acid FROM ".tablename('account')." WHERE uniacid = :uniacid", array(':uniacid' => $uniacid));
  1083. if (!empty($subaccount)) {
  1084. foreach ($subaccount as $childaccount) {
  1085. @unlink(IA_ROOT . '/attachment/qrcode_'.$childaccount['acid'].'.jpg');
  1086. @unlink(IA_ROOT . '/attachment/headimg_'.$childaccount['acid'].'.jpg');
  1087. file_remote_delete('qrcode_'.$childaccount['acid'].'.jpg');
  1088. file_remote_delete('headimg_'.$childaccount['acid'].'.jpg');
  1089. }
  1090. if (!empty($acid)) {
  1091. $jobid = job_create_delete_account($uniacid, $account['name'], $_W['uid']);
  1092. }
  1093. }
  1094. //遍历全部表删除公众号数据
  1095. $tables = array(
  1096. 'account','account_wechats', 'account_wxapp', 'wxapp_versions', 'account_webapp', 'account_phoneapp',
  1097. 'phoneapp_versions','core_paylog','core_resource',
  1098. 'cover_reply', 'mc_chats_record','mc_credits_recharge','mc_credits_record',
  1099. 'mc_fans_groups','mc_groups','mc_handsel','mc_mapping_fans','mc_mapping_ucenter','mc_mass_record',
  1100. 'mc_member_address','mc_member_fields','mc_members','menu_event',
  1101. 'qrcode','qrcode_stat', 'rule','rule_keyword','site_article','site_category','site_multi','site_nav','site_slide',
  1102. 'site_styles','site_styles_vars','stat_keyword', 'stat_rule','uni_account','uni_account_modules','uni_account_users','uni_settings', 'uni_group', 'uni_verifycode','users_permission',
  1103. 'mc_member_fields', 'wechat_news', 'users_lastuse',
  1104. );
  1105. if (!empty($tables)) {
  1106. foreach ($tables as $table) {
  1107. $tablename = str_replace($GLOBALS['_W']['config']['db']['tablepre'], '', $table);
  1108. pdo_delete($tablename, array( 'uniacid'=> $uniacid));
  1109. }
  1110. }
  1111. } else {
  1112. $account = account_fetch($acid);
  1113. if (empty($account)) {
  1114. itoast('子公众号不存在或是已经被删除', '', '');
  1115. }
  1116. $uniacid = $account['uniacid'];
  1117. $state = permission_account_user_role($_W['uid'], $uniacid);
  1118. if($state != ACCOUNT_MANAGE_NAME_FOUNDER && $state != ACCOUNT_MANAGE_NAME_OWNER) {
  1119. itoast('没有该公众号操作权限!', url('account/recycle'), 'error');
  1120. }
  1121. $uniaccount = uni_fetch($account['uniacid']);
  1122. if ($uniaccount['default_acid'] == $acid) {
  1123. itoast('默认子公众号不能删除', '', '');
  1124. }
  1125. pdo_delete('account', array('acid' => $acid));
  1126. pdo_delete('account_wechats', array('acid' => $acid, 'uniacid' => $uniacid));
  1127. cache_delete(cache_system_key('uniaccount', array('uniacid' => $uniacid)));
  1128. cache_delete(cache_system_key('account_auth_refreshtoken', array('uniacid' => $uniacid)));
  1129. $oauth = uni_setting($uniacid, array('oauth'));
  1130. if(!empty($oauth['oauth']['account']) && $oauth['oauth']['account'] == $acid) {
  1131. $acid = pdo_fetchcolumn('SELECT acid FROM ' . tablename('account_wechats') . " WHERE uniacid = :id AND level = 4 AND secret != '' AND `key` != ''", array(':id' => $uniacid));
  1132. pdo_update('uni_settings', array('oauth' => iserializer(array('account' => $acid, 'host' => $oauth['oauth']['host']))), array('uniacid' => $uniacid));
  1133. }
  1134. @unlink(IA_ROOT . '/attachment/qrcode_'.$acid.'.jpg');
  1135. @unlink(IA_ROOT . '/attachment/headimg_'.$acid.'.jpg');
  1136. file_remote_delete('qrcode_'.$acid.'.jpg');
  1137. file_remote_delete('headimg_'.$acid.'.jpg');
  1138. }
  1139. return $jobid;
  1140. }
  1141. /**
  1142. * 获取所有可借用支付的公众号
  1143. * @return array() 微信支付可借用的的公众号和服务商公众号
  1144. */
  1145. function account_wechatpay_proxy () {
  1146. global $_W;
  1147. $proxy_account = cache_load(cache_system_key('proxy_wechatpay_account'));
  1148. if (empty($proxy_account)) {
  1149. $proxy_account = cache_build_proxy_wechatpay_account();
  1150. }
  1151. unset($proxy_account['borrow'][$_W['uniacid']]);
  1152. unset($proxy_account['service'][$_W['uniacid']]);
  1153. return $proxy_account;
  1154. }
  1155. /**
  1156. * 设置模块是否在快捷菜单显示
  1157. */
  1158. function uni_account_module_shortcut_enabled($modulename, $status = STATUS_ON) {
  1159. global $_W;
  1160. $module = module_fetch($modulename);
  1161. if(empty($module)) {
  1162. return error(1, '抱歉,你操作的模块不能被访问!');
  1163. }
  1164. $module_status = pdo_get('uni_account_modules', array('module' => $modulename, 'uniacid' => $_W['uniacid']), array('id', 'shortcut'));
  1165. if (empty($module_status)) {
  1166. $data = array(
  1167. 'uniacid' => $_W['uniacid'],
  1168. 'module' => $modulename,
  1169. 'enabled' => STATUS_ON,
  1170. 'shortcut' => $status ? STATUS_ON : STATUS_OFF,
  1171. 'settings' => '',
  1172. );
  1173. pdo_insert('uni_account_modules', $data);
  1174. } else {
  1175. $data = array(
  1176. 'shortcut' => $status ? STATUS_ON : STATUS_OFF,
  1177. );
  1178. pdo_update('uni_account_modules', $data, array('id' => $module_status['id']));
  1179. }
  1180. cache_build_module_info($modulename);
  1181. return true;
  1182. }
  1183. /**
  1184. * 获取某公众号下会员字段
  1185. * @param int $uniacid
  1186. * @return array 会员字段数组
  1187. */
  1188. function uni_account_member_fields($uniacid) {
  1189. if (empty($uniacid)) {
  1190. return array();
  1191. }
  1192. $account_member_fields = pdo_getall('mc_member_fields', array('uniacid' => $uniacid), array(), 'fieldid');
  1193. $system_member_fields = pdo_getall('profile_fields', array(), array(), 'id');
  1194. $less_field_indexes = array_diff(array_keys($system_member_fields), array_keys($account_member_fields));
  1195. if (empty($less_field_indexes)) {
  1196. foreach ($account_member_fields as &$field) {
  1197. $field['field'] = $system_member_fields[$field['fieldid']]['field'];
  1198. }
  1199. unset($field);
  1200. return $account_member_fields;
  1201. }
  1202. $account_member_add_fields = array('uniacid' => $uniacid);
  1203. foreach ($less_field_indexes as $field_index) {
  1204. $account_member_add_fields['fieldid'] = $system_member_fields[$field_index]['id'];
  1205. $account_member_add_fields['title'] = $system_member_fields[$field_index]['title'];
  1206. $account_member_add_fields['available'] = $system_member_fields[$field_index]['available'];
  1207. $account_member_add_fields['displayorder'] = $system_member_fields[$field_index]['displayorder'];
  1208. pdo_insert('mc_member_fields', $account_member_add_fields);
  1209. $insert_id = pdo_insertid();
  1210. $account_member_fields[$insert_id]['id'] = $insert_id;
  1211. $account_member_fields[$insert_id]['field'] = $system_member_fields[$field_index]['field'];
  1212. $account_member_fields[$insert_id]['fid'] = $system_member_fields[$field_index]['id'];
  1213. $account_member_fields[$insert_id] = array_merge($account_member_fields[$insert_id], $account_member_add_fields);
  1214. }
  1215. return $account_member_fields;
  1216. }
  1217. function uni_account_global_oauth() {
  1218. load()->model('setting');
  1219. $oauth = setting_load('global_oauth');
  1220. $oauth = !empty($oauth['global_oauth']) ? $oauth['global_oauth'] : array();
  1221. if (!empty($oauth['oauth']['account'])) {
  1222. $account_exist = uni_fetch($oauth['oauth']['account']);
  1223. if (empty($account_exist) || is_error($account_exist)) {
  1224. $oauth['oauth']['account'] = 0;
  1225. }
  1226. }
  1227. return $oauth;
  1228. }
  1229. function uni_search_link_account($module_name, $type_sign, $uniacid = 0) {
  1230. global $_W;
  1231. load()->model('miniapp');
  1232. load()->model('phoneapp');
  1233. $module_name = trim($module_name);
  1234. if (empty($module_name) || empty($type_sign)) {
  1235. return array();
  1236. }
  1237. $all_account_type = uni_account_type();
  1238. $all_account_type_sign = uni_account_type_sign();
  1239. if (empty($all_account_type_sign[$type_sign])) {
  1240. return array();
  1241. }
  1242. $owned_account = uni_user_accounts($_W['uid'], $type_sign);
  1243. if (!empty($owned_account)) {
  1244. foreach ($owned_account as $key => $account) {
  1245. if (!empty($uniacid) && $account['uniacid'] == $uniacid) {
  1246. unset($owned_account[$key]);
  1247. continue;
  1248. }
  1249. $account['role'] = permission_account_user_role($_W['uid'], $account['uniacid']);
  1250. if (!in_array($account['role'], array(ACCOUNT_MANAGE_NAME_OWNER, ACCOUNT_MANAGE_NAME_VICE_FOUNDER, ACCOUNT_MANAGE_NAME_FOUNDER))) {
  1251. unset($owned_account[$key]);
  1252. continue;
  1253. }
  1254. $account_modules = uni_modules_by_uniacid($account['uniacid']);
  1255. if (empty($account_modules[$module_name])) {
  1256. unset($owned_account[$key]);
  1257. continue;
  1258. }
  1259. $type = $all_account_type_sign[$type_sign]['contain_type'][0];
  1260. $type_info = $all_account_type[$type];
  1261. if ($account_modules[$module_name][$type_info['module_support_name']] != $type_info['module_support_value']) {
  1262. unset($owned_account[$key]);
  1263. continue;
  1264. }
  1265. $account_support_version = array_filter($all_account_type, function($item) {
  1266. return $item['support_version'];
  1267. });
  1268. $account_support_version = array_keys($account_support_version);
  1269. if (in_array($type, $account_support_version)) {
  1270. $last_version = miniapp_fetch($account['uniacid']);
  1271. if (empty($last_version['version']) || empty($last_version['version']['modules']) || !is_array($last_version['version']['modules'])) {
  1272. unset($owned_account[$key]);
  1273. continue;
  1274. }
  1275. $module_version = array();
  1276. foreach ($last_version['version']['modules'] as $item) {
  1277. if (!empty($item['name']) && $item['name'] == $module_name) {
  1278. $module_version = $item;
  1279. break;
  1280. }
  1281. }
  1282. if (empty($module_version) || !empty($module_version['account']) || !empty($module_version['uniacid'])) {
  1283. unset($owned_account[$key]);
  1284. continue;
  1285. }
  1286. }
  1287. }
  1288. }
  1289. return $owned_account;
  1290. }
  1291. function uni_account_oauth_host() {
  1292. global $_W;
  1293. $oauth_url = $_W['siteroot'];
  1294. $unisetting = uni_setting_load();
  1295. if (!empty($unisetting['bind_domain']) && !empty($unisetting['bind_domain']['domain'])) {
  1296. $oauth_url = $unisetting['bind_domain']['domain'] . '/';
  1297. } else {
  1298. if (!empty($unisetting['oauth']['host'])) {
  1299. $oauth_url = $unisetting['oauth']['host'] . '/';
  1300. } else {
  1301. $global_unisetting = uni_account_global_oauth();
  1302. $oauth_url = !empty($global_unisetting['oauth']['host']) ? $global_unisetting['oauth']['host'] . '/' : $oauth_url;
  1303. }
  1304. }
  1305. return $oauth_url;
  1306. }
  1307. function uni_user_see_more_info($user_type, $see_more = false) {
  1308. global $_W;
  1309. if (empty($user_type)) {
  1310. return false;
  1311. }
  1312. if ($user_type == ACCOUNT_MANAGE_NAME_VICE_FOUNDER && !empty($see_more) || $_W['role'] != $user_type) {
  1313. return true;
  1314. }
  1315. return false;
  1316. }
  1317. function uni_delete_rule($rid, $relate_table_name) {
  1318. global $_W;
  1319. $rid = intval($rid);
  1320. if (empty($rid)) {
  1321. return false;
  1322. }
  1323. $allowed_table_names = array('news_reply', 'cover_reply');
  1324. if (!in_array($relate_table_name, $allowed_table_names)) {
  1325. return false;
  1326. }
  1327. $rule_result = pdo_delete('rule', array('id' => $rid, 'uniacid' => $_W['uniacid']));
  1328. $rule_keyword_result = pdo_delete('rule_keyword', array('rid' => $rid, 'uniacid' => $_W['uniacid']));
  1329. if ($rule_result && $rule_keyword_result) {
  1330. $result = pdo_delete($relate_table_name, array('rid' => $rid));
  1331. }
  1332. return $result ? true : false;
  1333. }