人人商城

wxapp.mod.php 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  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 wxapp_getpackage($data, $if_single = false) {
  8. load()->classs('cloudapi');
  9. $api = new CloudApi();
  10. $result = $api->post('wxapp', 'download', $data, 'html');
  11. if (is_error($result)) {
  12. return error(-1, $result['message']);
  13. } else {
  14. if (strpos($result, 'error:') === 0) {
  15. return error(-1, substr($result, 6));
  16. }
  17. }
  18. return $result;
  19. }
  20. function wxapp_account_create($account) {
  21. global $_W;
  22. load()->model('account');
  23. load()->model('user');
  24. load()->model('permission');
  25. $uni_account_data = array(
  26. 'name' => $account['name'],
  27. 'description' => $account['description'],
  28. 'title_initial' => get_first_pinyin($account['name']),
  29. 'groupid' => 0,
  30. );
  31. if (!pdo_insert('uni_account', $uni_account_data)) {
  32. return error(1, '添加公众号失败');
  33. }
  34. $uniacid = pdo_insertid();
  35. $account_data = array(
  36. 'uniacid' => $uniacid,
  37. 'type' => $account['type'],
  38. 'hash' => random(8),
  39. );
  40. pdo_insert('account', $account_data);
  41. $acid = pdo_insertid();
  42. $wxapp_data = array(
  43. 'acid' => $acid,
  44. 'token' => isset($account['token']) ? $account['token'] : random(32),
  45. 'encodingaeskey' => isset($account['encodingaeskey']) ? $account['encodingaeskey'] : random(43),
  46. 'uniacid' => $uniacid,
  47. 'name' => $account['name'],
  48. 'original' => $account['original'],
  49. 'level' => $account['level'],
  50. 'key' => $account['key'],
  51. 'secret' => $account['secret'],
  52. );
  53. pdo_insert('account_wxapp', $wxapp_data);
  54. if (empty($_W['isfounder'])) {
  55. $user_info = permission_user_account_num($_W['uid']);
  56. uni_user_account_role($uniacid, $_W['uid'], ACCOUNT_MANAGE_NAME_OWNER);
  57. if (empty($user_info['usergroup_wxapp_limit'])) {
  58. pdo_update('account', array('endtime' => strtotime('+1 month', time())), array('uniacid' => $uniacid));
  59. pdo_insert('site_store_create_account', array('endtime' => strtotime('+1 month', time()), 'uid' => $_W['uid'], 'uniacid' => $uniacid, 'type' => ACCOUNT_TYPE_APP_NORMAL));
  60. }
  61. }
  62. if (user_is_vice_founder()) {
  63. uni_user_account_role($uniacid, $_W['uid'], ACCOUNT_MANAGE_NAME_VICE_FOUNDER);
  64. }
  65. if (!empty($_W['user']['owner_uid'])) {
  66. uni_user_account_role($uniacid, $_W['user']['owner_uid'], ACCOUNT_MANAGE_NAME_VICE_FOUNDER);
  67. }
  68. pdo_update('uni_account', array('default_acid' => $acid), array('uniacid' => $uniacid));
  69. return $uniacid;
  70. }
  71. function wxapp_support_wxapp_modules() {
  72. global $_W;
  73. load()->model('user');
  74. $modules = user_modules($_W['uid']);
  75. $wxapp_modules = array();
  76. if (!empty($modules)) {
  77. foreach ($modules as $module) {
  78. if ($module['wxapp_support'] == MODULE_SUPPORT_WXAPP) {
  79. $wxapp_modules[$module['name']] = $module;
  80. }
  81. }
  82. }
  83. $store_table = table('store');
  84. $store_table->searchWithEndtime();
  85. $buy_wxapp_modules = $store_table->searchAccountBuyGoods($_W['uniacid'], STORE_TYPE_WXAPP_MODULE);
  86. $extra_permission = table('account')->getAccountExtraPermission($_W['uniacid']);
  87. $extra_modules = empty($extra_permission['modules']) ? array() : $extra_permission['modules'];
  88. foreach ($extra_modules as $key => $value) {
  89. $extra_modules[$value] = module_fetch($value);
  90. unset($extra_modules[$key]);
  91. }
  92. $wxapp_modules = array_merge($buy_wxapp_modules, $wxapp_modules, $extra_modules);
  93. if (empty($wxapp_modules)) {
  94. return array();
  95. }
  96. $bindings = pdo_getall('modules_bindings', array('module' => array_keys($wxapp_modules), 'entry' => 'page'));
  97. if (!empty($bindings)) {
  98. foreach ($bindings as $bind) {
  99. $wxapp_modules[$bind['module']]['bindings'][] = array('title' => $bind['title'], 'do' => $bind['do']);
  100. }
  101. }
  102. return $wxapp_modules;
  103. }
  104. function wxapp_support_uniacid_modules($uniacid) {
  105. $uni_modules = uni_modules_by_uniacid($uniacid);
  106. $wxapp_modules = array();
  107. if (!empty($uni_modules)) {
  108. foreach ($uni_modules as $module_name => $module_info) {
  109. if ($module_info['wxapp_support'] == MODULE_SUPPORT_WXAPP) {
  110. $wxapp_modules[$module_name] = $module_info;
  111. }
  112. }
  113. }
  114. return $wxapp_modules;
  115. }
  116. function wxapp_fetch($uniacid, $version_id = '') {
  117. global $_GPC;
  118. load()->model('extension');
  119. $wxapp_info = array();
  120. $uniacid = intval($uniacid);
  121. if (empty($uniacid)) {
  122. return $wxapp_info;
  123. }
  124. if (!empty($version_id)) {
  125. $version_id = intval($version_id);
  126. }
  127. $wxapp_info = pdo_get('account_wxapp', array('uniacid' => $uniacid));
  128. if (empty($wxapp_info)) {
  129. return $wxapp_info;
  130. }
  131. if (empty($version_id)) {
  132. $wxapp_cookie_uniacids = array();
  133. if (!empty($_GPC['__wxappversionids'])) {
  134. $wxappversionids = json_decode(htmlspecialchars_decode($_GPC['__wxappversionids']), true);
  135. foreach ($wxappversionids as $version_val) {
  136. $wxapp_cookie_uniacids[] = $version_val['uniacid'];
  137. }
  138. }
  139. if (in_array($uniacid, $wxapp_cookie_uniacids)) {
  140. $wxapp_version_info = wxapp_version($wxappversionids[$uniacid]['version_id']);
  141. }
  142. if (empty($wxapp_version_info)) {
  143. $sql = 'SELECT * FROM ' . tablename('wxapp_versions') . ' WHERE `uniacid`=:uniacid ORDER BY `id` DESC';
  144. $wxapp_version_info = pdo_fetch($sql, array(':uniacid' => $uniacid));
  145. }
  146. } else {
  147. $wxapp_version_info = pdo_get('wxapp_versions', array('id' => $version_id));
  148. }
  149. if (!empty($wxapp_version_info) && !empty($wxapp_version_info['modules'])) {
  150. $wxapp_version_info['modules'] = iunserializer($wxapp_version_info['modules']);
  151. if ($wxapp_version_info['design_method'] == WXAPP_MODULE) {
  152. $module = current($wxapp_version_info['modules']);
  153. $manifest = ext_module_manifest($module['name']);
  154. if (!empty($manifest)) {
  155. $wxapp_version_info['modules'][$module['name']]['version'] = $manifest['application']['version'];
  156. } else {
  157. $last_install_module = module_fetch($module['name']);
  158. $wxapp_version_info['modules'][$module['name']]['version'] = $last_install_module['version'];
  159. }
  160. }
  161. }
  162. $wxapp_info['version'] = $wxapp_version_info;
  163. $wxapp_info['version_num'] = explode('.', $wxapp_version_info['version']);
  164. return $wxapp_info;
  165. }
  166. function wxapp_version_all($uniacid) {
  167. load()->model('module');
  168. $wxapp_versions = array();
  169. $uniacid = intval($uniacid);
  170. if (empty($uniacid)) {
  171. return $wxapp_versions;
  172. }
  173. $wxapp_versions = pdo_getall('wxapp_versions', array('uniacid' => $uniacid), array('id'), '', array('id DESC'));
  174. if (!empty($wxapp_versions)) {
  175. foreach ($wxapp_versions as &$version) {
  176. $version = wxapp_version($version['id']);
  177. }
  178. }
  179. return $wxapp_versions;
  180. }
  181. function wxapp_get_some_lastversions($uniacid) {
  182. $version_lasts = array();
  183. $uniacid = intval($uniacid);
  184. if (empty($uniacid)) {
  185. return $version_lasts;
  186. }
  187. $version_lasts = table('wxapp')->latestVersion($uniacid);
  188. $last_switch_version = wxapp_last_switch_version();
  189. if (!empty($last_switch_version[$uniacid]) && !empty($version_lasts[$last_switch_version[$uniacid]['version_id']])) {
  190. $version_lasts[$last_switch_version[$uniacid]['version_id']]['current'] = true;
  191. } else {
  192. reset($version_lasts);
  193. $firstkey = key($version_lasts);
  194. $version_lasts[$firstkey]['current'] = true;
  195. }
  196. return $version_lasts;
  197. }
  198. function wxapp_update_last_use_version($uniacid, $version_id) {
  199. global $_GPC;
  200. $uniacid = intval($uniacid);
  201. $version_id = intval($version_id);
  202. if (empty($uniacid) || empty($version_id)) {
  203. return false;
  204. }
  205. $cookie_val = array();
  206. if (!empty($_GPC['__wxappversionids'])) {
  207. $wxapp_uniacids = array();
  208. $cookie_val = json_decode(htmlspecialchars_decode($_GPC['__wxappversionids']), true);
  209. if (!empty($cookie_val)) {
  210. foreach ($cookie_val as &$version) {
  211. $wxapp_uniacids[] = $version['uniacid'];
  212. if ($version['uniacid'] == $uniacid) {
  213. $version['version_id'] = $version_id;
  214. $wxapp_uniacids = array();
  215. break;
  216. }
  217. }
  218. unset($version);
  219. }
  220. if (!empty($wxapp_uniacids) && !in_array($uniacid, $wxapp_uniacids)) {
  221. $cookie_val[$uniacid] = array('uniacid' => $uniacid, 'version_id' => $version_id);
  222. }
  223. } else {
  224. $cookie_val = array(
  225. $uniacid => array('uniacid' => $uniacid, 'version_id' => $version_id),
  226. );
  227. }
  228. isetcookie('__uniacid', $uniacid, 7 * 86400);
  229. isetcookie('__wxappversionids', json_encode($cookie_val), 7 * 86400);
  230. return true;
  231. }
  232. function wxapp_version($version_id) {
  233. $version_info = array();
  234. $version_id = intval($version_id);
  235. if (empty($version_id)) {
  236. return $version_info;
  237. }
  238. $cachekey = cache_system_key('wxapp_version', array('version_id' => $version_id));
  239. $cache = cache_load($cachekey);
  240. if (!empty($cache)) {
  241. return $cache;
  242. }
  243. $version_info = pdo_get('wxapp_versions', array('id' => $version_id));
  244. $version_info = wxapp_version_detail_info($version_info);
  245. cache_write($cachekey, $version_info);
  246. return $version_info;
  247. }
  248. function wxapp_version_by_version($version) {
  249. global $_W;
  250. $version_info = array();
  251. $version = trim($version);
  252. if (empty($version)) {
  253. return $version_info;
  254. }
  255. $version_info = pdo_get('wxapp_versions', array('uniacid' => $_W['uniacid'], 'version' => $version));
  256. $version_info = wxapp_version_detail_info($version_info);
  257. return $version_info;
  258. }
  259. function wxapp_version_detail_info($version_info) {
  260. global $_W;
  261. if (empty($version_info) || empty($version_info['uniacid'])) {
  262. return array();
  263. }
  264. $uni_modules = uni_modules_by_uniacid($version_info['uniacid']);
  265. $uni_modules = array_keys($uni_modules);
  266. $version_info['cover_entrys'] = array();
  267. $version_info['last_modules'] = iunserializer($version_info['last_modules']);
  268. if (!empty($version_info['modules'])) {
  269. $version_info['modules'] = iunserializer($version_info['modules']);
  270. if (!empty($version_info['modules'])) {
  271. foreach ($version_info['modules'] as $i => $module) {
  272. if (!empty($module['uniacid'])) {
  273. $account = uni_fetch($module['uniacid']);
  274. }
  275. $module_info = module_fetch($module['name']);
  276. $module_info['account'] = $account;
  277. unset($version_info['modules'][$module['name']]);
  278. if (!in_array($module['name'], $uni_modules)) {
  279. continue;
  280. }
  281. $module_info['cover_entrys'] = module_entries($module['name'], array('cover'));
  282. $module_info['defaultentry'] = $module['defaultentry'];
  283. $module_info['newicon'] = $module['newicon'];
  284. $version_info['modules'][] = $module_info;
  285. }
  286. }
  287. }
  288. if (count($version_info['modules']) > 0) {
  289. $cover_entrys = !empty($version_info['modules'][0]['cover_entrys']) ? $version_info['modules'][0]['cover_entrys'] : array();
  290. $version_info['cover_entrys'] = !empty($cover_entrys['cover']) ? $cover_entrys['cover'] : array();
  291. }
  292. if (!empty($version_info['quickmenu'])) {
  293. $version_info['quickmenu'] = iunserializer($version_info['quickmenu']);
  294. }
  295. return $version_info;
  296. }
  297. function wxapp_site_info($multiid) {
  298. $site_info = array();
  299. $multiid = intval($multiid);
  300. if (empty($multiid)) {
  301. return array();
  302. }
  303. $site_info['slide'] = pdo_getall('site_slide', array('multiid' => $multiid));
  304. $site_info['nav'] = pdo_getall('site_nav', array('multiid' => $multiid));
  305. if (!empty($site_info['nav'])) {
  306. foreach ($site_info['nav'] as &$nav) {
  307. $nav['css'] = iunserializer($nav['css']);
  308. }
  309. unset($nav);
  310. }
  311. $recommend_sql = 'SELECT a.name, b.* FROM ' . tablename('site_category') . ' AS a LEFT JOIN ' . tablename('site_article') . ' AS b ON a.id = b.pcate WHERE a.parentid = 0 AND a.multiid = :multiid';
  312. $site_info['recommend'] = pdo_fetchall($recommend_sql, array(':multiid' => $multiid));
  313. return $site_info;
  314. }
  315. function wxapp_payment_param() {
  316. global $_W;
  317. $setting = uni_setting_load('payment', $_W['uniacid']);
  318. $pay_setting = $setting['payment'];
  319. return $pay_setting;
  320. }
  321. function wxapp_update_daily_visittrend() {
  322. global $_W;
  323. $yesterday = date('Ymd', strtotime('-1 days'));
  324. $trend = pdo_get('wxapp_general_analysis', array('uniacid' => $_W['uniacid'], 'type' => WXAPP_STATISTICS_DAILYVISITTREND, 'ref_date' => $yesterday));
  325. if (!empty($trend)) {
  326. return true;
  327. }
  328. return wxapp_insert_date_visit_trend($yesterday);
  329. }
  330. function wxapp_insert_date_visit_trend($date) {
  331. global $_W;
  332. $account_api = WeAccount::create();
  333. $wxapp_stat = $account_api->getDailyVisitTrend($date);
  334. if (is_error($wxapp_stat) || empty($wxapp_stat)) {
  335. return error(-1, '调用微信接口错误');
  336. } else {
  337. $insert_stat = array(
  338. 'uniacid' => $_W['uniacid'],
  339. 'session_cnt' => $wxapp_stat['session_cnt'],
  340. 'visit_pv' => $wxapp_stat['visit_pv'],
  341. 'visit_uv' => $wxapp_stat['visit_uv'],
  342. 'visit_uv_new' => $wxapp_stat['visit_uv_new'],
  343. 'type' => WXAPP_STATISTICS_DAILYVISITTREND,
  344. 'stay_time_uv' => $wxapp_stat['stay_time_uv'],
  345. 'stay_time_session' => $wxapp_stat['stay_time_session'],
  346. 'visit_depth' => $wxapp_stat['visit_depth'],
  347. 'ref_date' => $wxapp_stat['ref_date'],
  348. );
  349. pdo_insert('wxapp_general_analysis', $insert_stat);
  350. }
  351. return $insert_stat;
  352. }
  353. function wxapp_search_link_account($module_name = '') {
  354. global $_W;
  355. $module_name = trim($module_name);
  356. if (empty($module_name)) {
  357. return array();
  358. }
  359. $owned_account = uni_owned();
  360. if (!empty($owned_account)) {
  361. foreach ($owned_account as $key => $account) {
  362. if (!in_array($account['type'], array(ACCOUNT_TYPE_OFFCIAL_NORMAL, ACCOUNT_TYPE_OFFCIAL_AUTH))) {
  363. unset($owned_account[$key]);
  364. }
  365. $account['role'] = permission_account_user_role($_W['uid'], $account['uniacid']);
  366. if (!in_array($account['role'], array(ACCOUNT_MANAGE_NAME_OWNER, ACCOUNT_MANAGE_NAME_FOUNDER))) {
  367. unset($owned_account[$key]);
  368. }
  369. }
  370. foreach ($owned_account as $key => $account) {
  371. $account_modules = uni_modules_by_uniacid($account['uniacid']);
  372. if (empty($account_modules[$module_name])) {
  373. unset($owned_account[$key]);
  374. } elseif ($account_modules[$module_name][MODULE_SUPPORT_ACCOUNT_NAME] != MODULE_SUPPORT_ACCOUNT || $account_modules[$module_name]['wxapp_support'] != MODULE_SUPPORT_WXAPP) {
  375. unset($owned_account[$key]);
  376. }
  377. }
  378. }
  379. return $owned_account;
  380. }
  381. function wxapp_last_switch_version() {
  382. global $_GPC;
  383. static $wxapp_cookie_uniacids;
  384. if (empty($wxapp_cookie_uniacids) && !empty($_GPC['__wxappversionids'])) {
  385. $wxapp_cookie_uniacids = json_decode(htmlspecialchars_decode($_GPC['__wxappversionids']), true);
  386. }
  387. return $wxapp_cookie_uniacids;
  388. }
  389. function wxapp_code_generate($version_id) {
  390. global $_W;
  391. load()->classs('cloudapi');
  392. $api = new CloudApi();
  393. $version_info = wxapp_version($version_id);
  394. $account_wxapp_info = wxapp_fetch($version_info['uniacid'], $version_id);
  395. if (empty($account_wxapp_info)) {
  396. return error(1, '版本不存在');
  397. }
  398. $siteurl = $_W['siteroot'] . 'app/index.php';
  399. if (!empty($account_wxapp_info['appdomain'])) {
  400. $siteurl = $account_wxapp_info['appdomain'];
  401. }
  402. if (!starts_with($siteurl, 'https')) { return error(1, '小程序域名必须为https');
  403. }
  404. if ($version_info['type'] == WXAPP_CREATE_MODULE && $version_info['entry_id'] <= 0) {
  405. return error(1, '请先设置小程序入口');
  406. }
  407. $appid = $account_wxapp_info['key'];
  408. $siteinfo = array(
  409. 'name' => $account_wxapp_info['name'],
  410. 'uniacid' => $account_wxapp_info['uniacid'],
  411. 'acid' => $account_wxapp_info['acid'],
  412. 'multiid' => $account_wxapp_info['version']['multiid'],
  413. 'version' => $account_wxapp_info['version']['version'],
  414. 'siteroot' => $siteurl,
  415. 'design_method' => $account_wxapp_info['version']['design_method'],
  416. );
  417. $commit_data = array('do' => 'generate',
  418. 'appid' => $appid,
  419. 'modules' => $account_wxapp_info['version']['modules'],
  420. 'siteinfo' => $siteinfo,
  421. 'tabBar' => json_decode($account_wxapp_info['version']['quickmenu'], true),
  422. 'wxapp_type' => isset($version_info['type']) ? $version_info['type'] : 0,
  423. );
  424. $do = 'upload2';
  425. if ($version_info['use_default'] == 0) {
  426. $appjson = wxapp_code_custom_appjson_tobase64($version_id);
  427. if ($appjson) {
  428. if (!isset($appjson['tabBar']['list'])) {
  429. unset($appjson['tabBar']);
  430. }
  431. $commit_data['appjson'] = $appjson;
  432. }
  433. }
  434. $data = $api->post('wxapp', $do, $commit_data,
  435. 'json', false);
  436. return $data;
  437. }
  438. function wxapp_check_code_isgen($code_uuid) {
  439. load()->classs('cloudapi');
  440. $api = new CloudApi();
  441. $data = $api->get('wxapp', 'upload', array('do' => 'check_gen',
  442. 'code_uuid' => $code_uuid, ),
  443. 'json', false);
  444. return $data;
  445. }
  446. function wxapp_code_token() {
  447. global $_W;
  448. load()->classs('cloudapi');
  449. $cloud_api = new CloudApi();
  450. $data = $cloud_api->get('wxapp', 'upload', array('do' => 'code_token'), 'json', false);
  451. return $data;
  452. }
  453. function wxapp_code_qrcode($code_token) {
  454. $cloud_api = new CloudApi();
  455. $data = $cloud_api->get('wxapp', 'upload', array('do' => 'qrcode',
  456. 'code_token' => $code_token, ),
  457. 'html', false);
  458. return $data;
  459. }
  460. function wxapp_code_check_scan($code_token, $last) {
  461. $cloud_api = new CloudApi();
  462. $data = $cloud_api->get('wxapp', 'upload',
  463. array('do' => 'checkscan',
  464. 'code_token' => $code_token,
  465. 'last' => $last,
  466. ),
  467. 'json', false);
  468. return $data;
  469. }
  470. function wxapp_code_preview_qrcode($code_uuid, $code_token) {
  471. $cloud_api = new CloudApi();
  472. $commit_data = array(
  473. 'do' => 'preview_qrcode',
  474. 'code_uuid' => $code_uuid,
  475. 'code_token' => $code_token,
  476. );
  477. $data = $cloud_api->post('wxapp', 'upload', $commit_data,
  478. 'json', false);
  479. return $data;
  480. }
  481. function wxapp_code_commit($code_uuid, $code_token, $user_version = 3, $user_desc = '代码提交') {
  482. $cloud_api = new CloudApi();
  483. $commit_data = array(
  484. 'do' => 'commitcode',
  485. 'code_uuid' => $code_uuid,
  486. 'code_token' => $code_token,
  487. 'user_version' => $user_version,
  488. 'user_desc' => $user_desc,
  489. );
  490. $data = $cloud_api->post('wxapp', 'upload', $commit_data,
  491. 'json', false);
  492. return $data;
  493. }
  494. function wxapp_update_entry($version_id, $entry_id) {
  495. return pdo_update('wxapp_versions', array('entry_id' => $entry_id), array('id' => $version_id));
  496. }
  497. function wxapp_code_current_appjson($version_id) {
  498. load()->classs('cloudapi');
  499. $version_info = wxapp_version($version_id);
  500. if (!$version_info['use_default'] && isset($version_info['appjson'])) {
  501. return iunserializer($version_info['appjson']);
  502. }
  503. if ($version_info['use_default']) {
  504. $appjson = $version_info['default_appjson'];
  505. if ($appjson) {
  506. return iunserializer($appjson);
  507. }
  508. $cloud_api = new CloudApi();
  509. $account_wxapp_info = wxapp_fetch($version_info['uniacid'], $version_id);
  510. $commit_data = array('do' => 'appjson',
  511. 'wxapp_type' => isset($version_info['type']) ? $version_info['type'] : 0,
  512. 'modules' => $account_wxapp_info['version']['modules'],
  513. );
  514. $cloud_appjson = $cloud_api->get('wxapp', 'upload2', $commit_data,
  515. 'json', false);
  516. if (is_error($cloud_appjson)) { return null;
  517. }
  518. $appjson = $cloud_appjson['data']['appjson'];
  519. pdo_update('wxapp_versions', array('default_appjson' => serialize($appjson)),
  520. array('id' => $version_id));
  521. cache_delete(cache_system_key("wxapp_version:{$version_id}"));
  522. return $appjson;
  523. }
  524. }
  525. function wxapp_code_custom_appjson_tobase64($version_id) {
  526. load()->classs('image');
  527. $version_info = wxapp_version($version_id);
  528. $appjson = iunserializer($version_info['appjson']);
  529. if (!$appjson) {
  530. return false;
  531. }
  532. if (isset($appjson['tabBar']) && isset($appjson['tabBar']['list'])) {
  533. $tablist = &$appjson['tabBar']['list'];
  534. foreach ($tablist as &$item) {
  535. if (isset($item['iconPath']) && !starts_with($item['iconPath'], 'data:image')) {
  536. $item['iconPath'] = Image::create($item['iconPath'])->resize(81, 81)->toBase64();
  537. }
  538. if (isset($item['selectedIconPath']) && !starts_with($item['selectedIconPath'], 'data:image')) {
  539. $item['selectedIconPath'] = Image::create($item['selectedIconPath'])->resize(81, 81)->toBase64();
  540. }
  541. }
  542. }
  543. return $appjson;
  544. }
  545. function wxapp_code_path_convert($attachment_id) {
  546. load()->classs('image');
  547. load()->func('file');
  548. $attchid = intval($attachment_id);
  549. global $_W;
  550. $att_table = table('attachment');
  551. $attachment = $att_table->getById($attchid);
  552. if ($attachment) {
  553. $attach_path = $attachment['attachment'];
  554. $ext = pathinfo($attach_path, PATHINFO_EXTENSION);
  555. $url = tomedia($attach_path);
  556. $uniacid = intval($_W['uniacid']);
  557. $path = "images/{$uniacid}/" . date('Y/m/');
  558. mkdirs($path);
  559. $filename = file_random_name(ATTACHMENT_ROOT . '/' . $path, $ext);
  560. Image::create($url)->resize(81, 81)->saveTo(ATTACHMENT_ROOT . $path . $filename);
  561. $attachdir = $_W['config']['upload']['attachdir'];
  562. return $_W['siteroot'] . $attachdir . '/' . $path . $filename;
  563. }
  564. return null;
  565. }
  566. function wxapp_code_save_appjson($version_id, $json) {
  567. $result = pdo_update('wxapp_versions', array('appjson' => serialize($json), 'use_default' => 0), array('id' => $version_id));
  568. cache_delete(cache_system_key("wxapp_version:{$version_id}"));
  569. return $result;
  570. }
  571. function wxapp_code_set_default_appjson($version_id) {
  572. $result = pdo_update('wxapp_versions', array('appjson' => '', 'use_default' => 1), array('id' => $version_id));
  573. cache_delete(cache_system_key("wxapp_version:{$version_id}"));
  574. return $result;
  575. }