PageRenderTime 49ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/app/admin/ajax/weixin.php

https://gitlab.com/Ltaimao/wecenter
PHP | 430 lines | 381 code | 37 blank | 12 comment | 17 complexity | 22e5fe049d56c55d104c89a0d0fec2be MD5 | raw file
  1. <?php
  2. /*
  3. +--------------------------------------------------------------------------
  4. | WeCenter [#RELEASE_VERSION#]
  5. | ========================================
  6. | by WeCenter Software
  7. | © 2011 - 2014 WeCenter. All Rights Reserved
  8. | http://www.wecenter.com
  9. | ========================================
  10. | Support: WeCenter@qq.com
  11. |
  12. +---------------------------------------------------------------------------
  13. */
  14. define('IN_AJAX', TRUE);
  15. if (!defined('IN_ANWSION'))
  16. {
  17. die;
  18. }
  19. class ajax_weixin extends AWS_ADMIN_CONTROLLER
  20. {
  21. public function setup()
  22. {
  23. HTTP::no_cache_header();
  24. }
  25. public function save_third_party_access_rule_status_action()
  26. {
  27. if (!$_POST['rule_ids'])
  28. {
  29. H::ajax_json_output(AWS_APP::RSM(null, -1, AWS_APP::lang()->_t('请选择要操作的规则')));
  30. }
  31. foreach ($_POST['rule_ids'] AS $rule_id)
  32. {
  33. $this->model('openid_weixin_third')->update_third_party_api($rule_id, 'update', null, null, $_POST['enabled'][$rule_id], null, $_POST['rank'][$rule_id]);
  34. }
  35. H::ajax_json_output(AWS_APP::RSM(null, -1, AWS_APP::lang()->_t('规则状态已自动保存')));
  36. }
  37. public function remove_third_party_access_rule_action()
  38. {
  39. if (!$_POST['id'])
  40. {
  41. H::ajax_json_output(AWS_APP::RSM(null, -1, AWS_APP::lang()->_t('请选择要删除的规则')));
  42. }
  43. if(!$this->model('openid_weixin_third')->get_third_party_api_by_id($_POST['id']))
  44. {
  45. H::ajax_json_output(AWS_APP::RSM(null, -1, AWS_APP::lang()->_t('接入规则不存在')));
  46. }
  47. $this->model('openid_weixin_third')->remove_third_party_api_by_id($_POST['id']);
  48. H::ajax_json_output(AWS_APP::RSM(null, 1, null));
  49. }
  50. public function save_third_party_access_rule_action()
  51. {
  52. if (!$_POST['url'] OR substr($_POST['url'], 0, 7) != 'http://' AND substr($_POST['url'], 0, 8) != 'https://')
  53. {
  54. H::ajax_json_output(AWS_APP::RSM(null, -1, AWS_APP::lang()->_t('请输入正确的 URL')));
  55. }
  56. $_POST['token'] = trim($_POST['token']);
  57. if (!$_POST['token'])
  58. {
  59. H::ajax_json_output(AWS_APP::RSM(null, -1, AWS_APP::lang()->_t('请输入第三方公众平台接口 Token')));
  60. }
  61. if ($_POST['id'])
  62. {
  63. $rule_info = $this->model('openid_weixin_third')->get_third_party_api_by_id($_POST['id']);
  64. if (!$rule_info)
  65. {
  66. H::ajax_json_output(AWS_APP::RSM(null, -1, AWS_APP::lang()->_t('接入规则不存在')));
  67. }
  68. $this->model('openid_weixin_third')->update_third_party_api($rule_info['id'], 'update', $_POST['url'], $_POST['token']);
  69. }
  70. else
  71. {
  72. $account_info = $this->model('weixin')->get_account_info_by_id($_POST['account_id']);
  73. if (!$account_info)
  74. {
  75. H::ajax_json_output(AWS_APP::RSM(null, -1, AWS_APP::lang()->_t('公众账号不存在')));
  76. }
  77. $this->model('openid_weixin_third')->update_third_party_api(null, 'add', $_POST['url'], $_POST['token'], 1, $account_info['id']);
  78. }
  79. H::ajax_json_output(AWS_APP::RSM(array(
  80. 'url' => get_js_url('/admin/weixin/third_party_access/id-' . $_POST['account_id'])
  81. ), 1, null));
  82. }
  83. public function create_qr_code_action()
  84. {
  85. if (!$_POST['description'])
  86. {
  87. H::ajax_json_output(AWS_APP::RSM(null, -1, AWS_APP::lang()->_t('请输入描述')));
  88. }
  89. $scene_id = $this->model('weixin')->insert('weixin_qr_code', array('description' => $_POST['description']));
  90. $result = $this->model('weixin')->create_qr_code($scene_id);
  91. if ($result)
  92. {
  93. H::ajax_json_output(AWS_APP::RSM(null, -1, $result));
  94. }
  95. H::ajax_json_output(AWS_APP::RSM(null, 1, null));
  96. }
  97. public function remove_qr_code_action()
  98. {
  99. if (!$_POST['scene_id'])
  100. {
  101. H::ajax_json_output(AWS_APP::RSM(null, -1, AWS_APP::lang()->_t('请选择要删除的二维码')));
  102. }
  103. $this->model('weixin')->remove_qr_code($_POST['scene_id']);
  104. H::ajax_json_output(AWS_APP::RSM(null, 1, null));
  105. }
  106. public function save_reply_rule_status_action()
  107. {
  108. if (!$_POST['rule_ids'])
  109. {
  110. H::ajax_json_output(AWS_APP::RSM(null, -1, AWS_APP::lang()->_t('请选择要操作的规则')));
  111. }
  112. foreach ($_POST['rule_ids'] AS $rule_id)
  113. {
  114. $this->model('weixin')->update_reply_rule_enabled($rule_id, $_POST['enabled_status'][$rule_id]);
  115. $this->model('weixin')->update_reply_rule_sort($rule_id, $_POST['sort_status'][$rule_id]);
  116. }
  117. if ($_POST['is_subscribe'])
  118. {
  119. $account_info['weixin_subscribe_message_key'] = $_POST['is_subscribe'];
  120. }
  121. if ($_POST['is_no_result'])
  122. {
  123. $account_info['weixin_no_result_message_key'] = $_POST['is_no_result'];
  124. }
  125. $this->model('weixin')->update_setting_or_account($_POST['account_id'], $account_info);
  126. H::ajax_json_output(AWS_APP::RSM(null, -1, AWS_APP::lang()->_t('规则状态已自动保存')));
  127. }
  128. public function remove_reply_rule_action()
  129. {
  130. $this->model('weixin')->remove_reply_rule($_POST['id']);
  131. H::ajax_json_output(AWS_APP::RSM(null, 1, null));
  132. }
  133. public function mp_menu_list_image_upload_action()
  134. {
  135. AWS_APP::upload()->initialize(array(
  136. 'allowed_types' => 'jpg,jpeg,png,gif',
  137. 'upload_path' => get_setting('upload_dir') . '/weixin/list_image/',
  138. 'is_image' => TRUE,
  139. 'file_name' => str_replace(array('/', '\\', '.'), '', $_GET['attach_access_key']) . '.jpg',
  140. 'encrypt_name' => FALSE
  141. ));
  142. if ($_GET['attach_access_key'])
  143. {
  144. AWS_APP::upload()->do_upload('aws_upload_file');
  145. }
  146. else
  147. {
  148. return false;
  149. }
  150. if (AWS_APP::upload()->get_error())
  151. {
  152. switch (AWS_APP::upload()->get_error())
  153. {
  154. default:
  155. die("{'error':'错误代码: " . AWS_APP::upload()->get_error() . "'}");
  156. break;
  157. case 'upload_invalid_filetype':
  158. die("{'error':'文件类型无效'}");
  159. break;
  160. case 'upload_invalid_filesize':
  161. die("{'error':'文件尺寸过大, 最大允许尺寸为 " . get_setting('upload_size_limit') . " KB'}");
  162. break;
  163. }
  164. }
  165. if (! $upload_data = AWS_APP::upload()->data())
  166. {
  167. die("{'error':'上传失败, 请与管理员联系'}");
  168. }
  169. if ($upload_data['is_image'] == 1)
  170. {
  171. AWS_APP::image()->initialize(array(
  172. 'quality' => 90,
  173. 'source_image' => $upload_data['full_path'],
  174. 'new_image' => $upload_data['full_path'],
  175. 'width' => 640,
  176. 'height' => 320
  177. ))->resize();
  178. AWS_APP::image()->initialize(array(
  179. 'quality' => 90,
  180. 'source_image' => $upload_data['full_path'],
  181. 'new_image' => get_setting('upload_dir') . '/weixin/list_image/square_' . basename($upload_data['full_path']),
  182. 'width' => 80,
  183. 'height' => 80
  184. ))->resize();
  185. }
  186. H::ajax_json_output(AWS_APP::RSM(null, 1, null));
  187. }
  188. public function save_account_action()
  189. {
  190. if (!$_POST['type'] OR $_POST['type'] == 'update' AND !$_POST['id'])
  191. {
  192. H::ajax_json_output(AWS_APP::RSM(null, -1, AWS_APP::lang()->_t('错误的请求')));
  193. }
  194. $_POST['weixin_mp_token'] = trim($_POST['weixin_mp_token']);
  195. if (!$_POST['weixin_mp_token'])
  196. {
  197. H::ajax_json_output(AWS_APP::RSM(null, -1, AWS_APP::lang()->_t('微信公众平台接口 Token 不能为空')));
  198. }
  199. $_POST['weixin_encoding_aes_key'] = trim($_POST['weixin_encoding_aes_key']);
  200. if ($_POST['weixin_encoding_aes_key'] AND strlen($_POST['weixin_encoding_aes_key']) != 43)
  201. {
  202. H::ajax_json_output(AWS_APP::RSM(null, -1, AWS_APP::lang()->_t('微信公众平台接口 EncodingAESKey 应为 43 位')));
  203. }
  204. if (!$_POST['weixin_account_role'] OR !in_array($_POST['weixin_account_role'], array('base', 'subscription', 'general', 'service')))
  205. {
  206. $_POST['weixin_account_role'] = 'base';
  207. }
  208. $account_info = array(
  209. 'weixin_mp_token' => $_POST['weixin_mp_token'],
  210. 'weixin_account_role' => $_POST['weixin_account_role'],
  211. 'weixin_app_id' => trim($_POST['weixin_app_id']),
  212. 'weixin_app_secret' => trim($_POST['weixin_app_secret']),
  213. 'weixin_encoding_aes_key' => $_POST['weixin_encoding_aes_key']
  214. );
  215. switch ($_POST['type'])
  216. {
  217. case 'add':
  218. $account_id = $this->model('weixin')->insert('weixin_accounts', $account_info);
  219. H::ajax_json_output(AWS_APP::RSM(array('url' => get_js_url('/admin/weixin/account/id-' . $account_id)), 1, null));
  220. break;
  221. case 'update':
  222. $this->model('weixin')->update_setting_or_account($_POST['id'], $account_info);
  223. H::ajax_json_output(AWS_APP::RSM(null, -1, AWS_APP::lang()->_t('保存设置成功')));
  224. break;
  225. }
  226. }
  227. public function remove_weixin_account_action()
  228. {
  229. $this->model('weixin')->remove_weixin_account($_POST['id']);
  230. H::ajax_json_output(AWS_APP::RSM(null, 1, null));
  231. }
  232. public function send_msg_action()
  233. {
  234. $group_id = intval($_POST['group_id']);
  235. $groups = $this->model('weixin')->get_groups();
  236. $group_name = $groups[$group_id]['name'];
  237. if (!isset($group_name))
  238. {
  239. H::ajax_json_output(AWS_APP::RSM(null, -1, AWS_APP::lang()->_t('选择的分组不存在')));
  240. }
  241. if (!$_POST['main_msg_title'])
  242. {
  243. H::ajax_json_output(AWS_APP::RSM(null, -1, AWS_APP::lang()->_t('请输入封面的标题')));
  244. }
  245. if (!$_POST['main_msg_author'])
  246. {
  247. H::ajax_json_output(AWS_APP::RSM(null, -1, AWS_APP::lang()->_t('请输入封面的作者')));
  248. }
  249. if (!$_POST['main_msg_content'])
  250. {
  251. H::ajax_json_output(AWS_APP::RSM(null, -1, AWS_APP::lang()->_t('请输入封面的内容')));
  252. }
  253. if (!$_POST['main_msg_url'])
  254. {
  255. H::ajax_json_output(AWS_APP::RSM(null, -1, AWS_APP::lang()->_t('请输入封面的原文链接')));
  256. }
  257. if ($_POST['show_cover_pic'] != 0 AND $_POST['show_cover_pic'] != 1)
  258. {
  259. H::ajax_json_output(AWS_APP::RSM(null, -1, AWS_APP::lang()->_t('请选择是否显示封面')));
  260. }
  261. $article_ids = array_unique(array_filter(explode(',', trim($_POST['article_ids'], ','))));
  262. $question_ids = array_unique(array_filter(explode(',', trim($_POST['question_ids'], ','))));
  263. /*
  264. if (!$article_ids AND !$question_ids)
  265. {
  266. H::ajax_json_output(AWS_APP::RSM(null, -1, AWS_APP::lang()->_t('请添加要群发的文章或问题')));
  267. }
  268. */
  269. if (count($article_ids) + count($question_ids) > 9)
  270. {
  271. H::ajax_json_output(AWS_APP::RSM(null, -1, AWS_APP::lang()->_t('最多可添加 9 个文章和问题')));
  272. }
  273. if ($_FILES['main_msg_img']['error'] === UPLOAD_ERR_OK)
  274. {
  275. if (!in_array($_FILES['main_msg_img']['type'], array('image/bmp', 'image/png', 'image/jpeg', 'image/gif')))
  276. {
  277. H::ajax_json_output(AWS_APP::RSM(null, -1, AWS_APP::lang()->_t('只允许上传 bmp/png/jpeg/jpg/gif 格式的图片')));
  278. }
  279. if ($_FILES['main_msg_img']['size'] > '2097152')
  280. {
  281. H::ajax_json_output(AWS_APP::RSM(null, -1, AWS_APP::lang()->_t('图片最大为 2M')));
  282. }
  283. $main_msg_img = TEMP_PATH . 'weixin_img.jpg';
  284. if (!is_uploaded_file($_FILES['main_msg_img']['tmp_name']) OR !move_uploaded_file($_FILES['main_msg_img']['tmp_name'], $main_msg_img))
  285. {
  286. H::ajax_json_output(AWS_APP::RSM(null, -1, AWS_APP::lang()->_t('上传失败, 请与管理员联系')));
  287. }
  288. }
  289. else
  290. {
  291. $main_msg_img = AWS_APP::config()->get('weixin')->default_list_image_path;
  292. }
  293. $main_msg = array(
  294. 'author' => $_POST['main_msg_author'],
  295. 'title' => $_POST['main_msg_title'],
  296. 'url' => $_POST['main_msg_url'],
  297. 'content' => $_POST['main_msg_content'],
  298. 'img' => $main_msg_img,
  299. 'show_cover_pic' => $_POST['show_cover_pic']
  300. );
  301. $error_msg = $this->model('weixin')->add_main_msg_to_mpnews($main_msg);
  302. if (isset($error_msg))
  303. {
  304. H::ajax_json_output(AWS_APP::RSM(null, -1, AWS_APP::lang()->_t('上传封面图失败, 错误信息: %s', $error_msg)));
  305. }
  306. if ($article_ids)
  307. {
  308. $error_msg = $this->model('weixin')->add_articles_to_mpnews($article_ids);
  309. if (isset($error_msg))
  310. {
  311. H::ajax_json_output(AWS_APP::RSM(null, -1, AWS_APP::lang()->_t('上传文章作者的头像失败, 错误信息: %s', $error_msg)));
  312. }
  313. }
  314. if ($question_ids)
  315. {
  316. $error_msg = $this->model('weixin')->add_questions_to_mpnews($question_ids);
  317. if (isset($error_msg))
  318. {
  319. H::ajax_json_output(AWS_APP::RSM(null, -1, AWS_APP::lang()->_t('上传问题作者的头像失败, 错误信息: %s', $error_msg)));
  320. }
  321. }
  322. $error_msg = $this->model('weixin')->upload_mpnews();
  323. if (isset($error_msg))
  324. {
  325. H::ajax_json_output(AWS_APP::RSM(null, -1, AWS_APP::lang()->_t('上传图文消息失败, 错误为: %s', $error_msg)));
  326. }
  327. $error_msg = $this->model('weixin')->send_msg($group_id, 'mpnews');
  328. if (isset($error_msg))
  329. {
  330. H::ajax_json_output(AWS_APP::RSM(null, -1, AWS_APP::lang()->_t('群发任务提交失败, 错误信息: %s', $error_msg)));
  331. }
  332. $msg_id = $this->model('weixin')->save_sent_msg($group_name, $groups[$group_id]['count']);
  333. if (is_file(TEMP_PATH . 'weixin_img.jpg'))
  334. {
  335. @unlink(TEMP_PATH . 'weixin_img.jpg');
  336. }
  337. H::ajax_json_output(AWS_APP::RSM(array('url' => get_js_url('/admin/weixin/sent_msg_details/id-' . $msg_id)), 1, null));
  338. }
  339. }