post.ctrl.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. defined('IN_IA') or exit('Access Denied');
  3. load()->model('module');
  4. load()->model('wxapp');
  5. $dos = array('design_method', 'post', 'get_wxapp_modules', 'module_binding');
  6. $do = in_array($do, $dos) ? $do : 'post';
  7. $_W['page']['title'] = '小程序 - 新建版本';
  8. $account_info = permission_user_account_num();
  9. if ($do == 'design_method') {
  10. $uniacid = intval($_GPC['uniacid']);
  11. template('wxapp/design-method');
  12. }
  13. if ($do == 'post') {
  14. $uniacid = intval($_GPC['uniacid']);
  15. $design_method = intval($_GPC['design_method']);
  16. $create_type = intval($_GPC['create_type']);
  17. $version_id = intval($_GPC['version_id']);
  18. $isedit = $version_id > 0 ? 1 : 0;
  19. if ($isedit) {
  20. $wxapp_version = wxapp_version($version_id);
  21. }
  22. if (empty($design_method)) {
  23. itoast('请先选择要添加小程序类型', referer(), 'error');
  24. }
  25. if ($design_method == WXAPP_TEMPLATE) {
  26. itoast('拼命开发中。。。', referer(), 'info');
  27. }
  28. if (checksubmit('submit')) {
  29. if ($account_info['wxapp_limit'] <= 0 && empty($uniacid) && !$_W['isfounder']) {
  30. iajax(-1, '创建的小程序已达上限!');
  31. }
  32. if ($design_method == WXAPP_TEMPLATE && empty($_GPC['choose']['modules'])) {
  33. iajax(2, '请选择要打包的模块应用', url('wxapp/post'));
  34. }
  35. if (!preg_match('/^[0-9]{1,2}\.[0-9]{1,2}(\.[0-9]{1,2})?$/', trim($_GPC['version']))) {
  36. iajax('-1', '版本号错误,只能是数字、点,数字最多2位,例如 1.1.1 或1.2');
  37. }
  38. if (empty($uniacid)) {
  39. if (empty($_GPC['name'])) {
  40. iajax(1, '请填写小程序名称', url('wxapp/post'));
  41. }
  42. $account_wxapp_data = array(
  43. 'name' => trim($_GPC['name']),
  44. 'description' => trim($_GPC['description']),
  45. 'original' => trim($_GPC['original']),
  46. 'level' => 1,
  47. 'key' => trim($_GPC['appid']),
  48. 'secret' => trim($_GPC['appsecret']),
  49. 'type' => ACCOUNT_TYPE_APP_NORMAL,
  50. );
  51. $uniacid = wxapp_account_create($account_wxapp_data);
  52. if (is_error($uniacid)) {
  53. iajax(3, '添加小程序信息失败', url('wxapp/post'));
  54. }
  55. } else {
  56. $wxapp_info = wxapp_fetch($uniacid);
  57. if (empty($wxapp_info)) {
  58. iajax(4, '小程序不存在或是已经被删除', url('wxapp/post'));
  59. }
  60. }
  61. $wxapp_version = array(
  62. 'uniacid' => $uniacid,
  63. 'multiid' => '0',
  64. 'description' => trim($_GPC['description']),
  65. 'version' => trim($_GPC['version']),
  66. 'modules' => '',
  67. 'design_method' => $design_method,
  68. 'quickmenu' => '',
  69. 'createtime' => TIMESTAMP,
  70. 'template' => $design_method == WXAPP_TEMPLATE ? intval($_GPC['choose']['template']) : 0,
  71. 'type' => 0
  72. );
  73. if (!in_array($create_type, array(WXAPP_CREATE_DEFAULT, WXAPP_CREATE_MODULE, WXAPP_CREATE_MUTI_MODULE))) {
  74. $create_type = WXAPP_CREATE_DEFAULT;
  75. }
  76. $wxapp_version['type'] = $create_type;
  77. if ($design_method == WXAPP_TEMPLATE) {
  78. $multi_data = array(
  79. 'uniacid' => $uniacid,
  80. 'title' => $account_wxapp_data['name'],
  81. 'styleid' => 0,
  82. );
  83. pdo_insert('site_multi', $multi_data);
  84. $wxapp_version['multiid'] = pdo_insertid();
  85. }
  86. if (!empty($_GPC['choose']['modules'])) {
  87. $select_modules = array();
  88. foreach ($_GPC['choose']['modules'] as $post_module) {
  89. $module = module_fetch($post_module['module']);
  90. if (empty($module)) {
  91. continue;
  92. }
  93. $select_modules[$module['name']] = array('name' => $module['name'],
  94. 'newicon' => $post_module['newicon'],
  95. 'version' => $module['version'], 'defaultentry'=>$post_module['defaultentry']);
  96. }
  97. $wxapp_version['modules'] = serialize($select_modules);
  98. }
  99. if (!empty($_GPC['quickmenu'])) {
  100. $quickmenu = array(
  101. 'color' => $_GPC['quickmenu']['bottom']['color'],
  102. 'selected_color' => $_GPC['quickmenu']['bottom']['selectedColor'],
  103. 'boundary' => $_GPC['quickmenu']['bottom']['boundary'],
  104. 'bgcolor' => $_GPC['quickmenu']['bottom']['bgcolor'],
  105. 'show' => $_GPC['quickmenu']['show'] == 'true' ? 1 : 0,
  106. 'menus' => array(),
  107. );
  108. if (!empty($_GPC['quickmenu']['menus'])) {
  109. foreach ($_GPC['quickmenu']['menus'] as $row) {
  110. $quickmenu['menus'][] = array(
  111. 'name' => $row['name'],
  112. 'icon' => $row['defaultImage'],
  113. 'selectedicon' => $row['selectedImage'],
  114. 'url' => $row['module']['url'],
  115. 'defaultentry' => $row['defaultentry']['eid'],
  116. );
  117. }
  118. }
  119. $wxapp_version['quickmenu'] = serialize($quickmenu);
  120. }
  121. if ($isedit) {
  122. $msg = '小程序修改成功';
  123. pdo_update('wxapp_versions', $wxapp_version, array('id'=>$version_id, 'uniacid'=>$uniacid));
  124. } else {
  125. $msg = '小程序创建成功';
  126. pdo_insert('wxapp_versions', $wxapp_version);
  127. }
  128. iajax(0, $msg, url('wxapp/display/switch', array('uniacid' => $uniacid)));
  129. }
  130. if (!empty($uniacid)) {
  131. $wxapp_info = wxapp_fetch($uniacid);
  132. }
  133. template('wxapp/post');
  134. }
  135. if ($do == 'get_wxapp_modules') {
  136. $wxapp_modules = wxapp_support_wxapp_modules();
  137. foreach ($wxapp_modules as $name => $module) {
  138. if ($module['issystem']) {
  139. $path = '/framework/builtin/'.$module['name'];
  140. } else {
  141. $path = '../addons/'.$module['name'];
  142. }
  143. $icon = $path.'/icon-custom.jpg';
  144. if (!file_exists($cion)) {
  145. $icon = $path.'/icon.jpg';
  146. if (!file_exists($icon)) {
  147. $icon = './resource/images/nopic-small.jpg';
  148. }
  149. }
  150. $module['logo'] = $icon;
  151. }
  152. iajax(0, $wxapp_modules, '');
  153. }
  154. if ($do == 'module_binding') {
  155. $modules = $_GPC['modules'];
  156. if (empty($modules)) {
  157. iajax(1, '参数无效');
  158. return;
  159. }
  160. $modules = explode(',', $modules);
  161. $modules = array_map(function($item) {
  162. return trim($item);
  163. }, $modules);
  164. $modules = table('module')->with(array('bindings' => function($query){
  165. return $query->where('entry', 'cover');
  166. }))->where('name', $modules)->getall();
  167. $modules = array_filter($modules, function($module){
  168. return count($module['bindings']) > 0;
  169. });
  170. iajax(0, $modules);
  171. }