人人商城

miniapp.mod.php 22KB

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