post.ctrl.php 7.4 KB

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