miniapp.mod.php 23 KB

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