Wechat.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. <?php
  2. /**
  3. * 微信配置
  4. */
  5. namespace app\admin\controller;
  6. use think\facade\View;
  7. use app\api\controller\WechatApi;
  8. use think\facade\Db;
  9. use think\facade\Lang;
  10. /**
  11. * ============================================================================
  12. * DSMall多用户商城
  13. * ============================================================================
  14. * 版权所有 2014-2028 长沙德尚网络科技有限公司,并保留所有权利。
  15. * 网站地址: http://www.csdeshang.com
  16. * ----------------------------------------------------------------------------
  17. * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
  18. * 不允许对程序代码以任何形式任何目的的再发布。
  19. * ============================================================================
  20. * 控制器
  21. */
  22. class Wechat extends AdminControl {
  23. public function initialize() {
  24. parent::initialize(); // TODO: Change the autogenerated stub
  25. Lang::load(base_path() . 'admin/lang/' . config('lang.default_lang') . '/wechat.lang.php');
  26. }
  27. //公众号配置
  28. public function setting() {
  29. $wechat_model = model('wechat');
  30. if (!request()->isPost()) {
  31. //获取公众号配置信息
  32. $wx_config = $wechat_model->getOneWxconfig();
  33. View::assign('wx_config', $wx_config);
  34. //接口地址
  35. $wx_apiurl = HTTP_TYPE . $_SERVER['HTTP_HOST'] . substr($_SERVER['PHP_SELF'], 0, strpos($_SERVER['PHP_SELF'], 'index.php')) . 'api/Wechat/index';
  36. View::assign('wx_apiurl', $wx_apiurl);
  37. return View::fetch();
  38. } else {
  39. $data = [
  40. 'token' => input('post.wx_token'),
  41. 'appid' => input('post.wx_appid'),
  42. 'appsecret' => input('post.wx_AppSecret'),
  43. 'wxname' => input('post.wx_name'),
  44. 'xcx_appid' => input('post.xcx_appid'),
  45. 'xcx_appsecret' => input('post.xcx_AppSecret'),
  46. ];
  47. //公众号二维码图片待处理
  48. $id = input('param.wx_id');
  49. if (empty($id)) {
  50. $res = $wechat_model->addWxconfig($data);
  51. } else {
  52. $res = $wechat_model->editWxconfig(array('id' => $id), $data);
  53. }
  54. if ($res) {
  55. $this->success(lang('ds_common_op_succ'));
  56. } else {
  57. $this->error(lang('ds_common_op_fail'));
  58. }
  59. }
  60. }
  61. //公众号菜单
  62. public function menu() {
  63. //获取顶级菜单
  64. $wechat_model = model('wechat');
  65. $p_menu = $wechat_model->getWxmenuList(array('pid' => 0), 'sort ASC');
  66. //获取二级菜单
  67. $child_menu = array();
  68. foreach ($p_menu as $k => $v) {
  69. $child_list = $wechat_model->getWxmenuList(array('pid' => $v['id']), 'sort desc');
  70. $child_menu[$v['id']] = $child_list;
  71. }
  72. $menu_type = array('view' => lang('menu_type_1'), 'click' => lang('menu_type_2'), 'view_limited' => lang('menu_type_3'));
  73. View::assign('menu_type', $menu_type);
  74. View::assign('p_menu', $p_menu);
  75. View::assign('c_menu', $child_menu);
  76. $this->setAdminCurItem('menu');
  77. return View::fetch();
  78. }
  79. //菜单编辑
  80. public function menu_edit() {
  81. $wechat_model = model('wechat');
  82. $menu_id = intval(input('param.id'));
  83. if (empty($menu_id)) {
  84. $this->error(lang('param_error'));
  85. }
  86. if (!request()->isPost()) {
  87. $parents = $wechat_model->getWxmenuList(array('pid' => 0), 'id desc', 'name,id');
  88. View::assign('parents', $parents);
  89. $condition = array('id' => $menu_id);
  90. $menu = $wechat_model->getOneWxmenu($condition);
  91. View::assign('menu', $menu);
  92. return View::fetch('menu_form');
  93. } else {
  94. $menu_name = input('post.menu_name');
  95. $menu_value = input('post.menu_value');
  96. $menu_sort = input('post.menu_sort');
  97. $menu_type = input('post.menu_type');
  98. $menu_pid = input('post.menu_pid');
  99. $data = [
  100. 'name' => $menu_name,
  101. 'value' => $menu_value,
  102. 'sort' => $menu_sort,
  103. 'type' => $menu_type,
  104. 'pid' => $menu_pid,
  105. ];
  106. //添加顶级菜单时判断是否超过限定数量
  107. if ($data['pid'] == '0' && $menu_id > 0) {
  108. $num = $wechat_model->getWxmenuCount(array('pid' => 0));
  109. if ($num > 3) {
  110. $this->error(lang('top_menu_count_error'));
  111. }
  112. }
  113. $wechat_validate = ds_validate('wechat');
  114. if (!$wechat_validate->scene('menu_edit')->check($data)) {
  115. $this->error($wechat_validate->getError());
  116. }
  117. $result = $wechat_model->editWxmenu(array('id' => $menu_id), $data);
  118. if ($result) {
  119. dsLayerOpenSuccess(lang('ds_common_op_succ'));
  120. } else {
  121. $this->error(lang('ds_common_save_fail'));
  122. }
  123. }
  124. }
  125. //菜单添加
  126. public function menu_add() {
  127. $wechat_model = model('wechat');
  128. if (!request()->isPost()) {
  129. $data = [
  130. 'pid' => '',
  131. 'type' => 'view',
  132. ];
  133. $parents = $wechat_model->getWxmenuList(array('pid' => 0), 'id desc', 'name,id');
  134. View::assign('parents', $parents);
  135. View::assign('menu', $data);
  136. return View::fetch('menu_form');
  137. } else {
  138. $menu_name = input('post.menu_name');
  139. $menu_value = input('post.menu_value');
  140. $menu_sort = input('post.menu_sort');
  141. $menu_type = input('post.menu_type');
  142. $menu_pid = input('post.menu_pid');
  143. $data = [
  144. 'name' => $menu_name,
  145. 'value' => $menu_value,
  146. 'sort' => $menu_sort,
  147. 'type' => $menu_type,
  148. 'pid' => $menu_pid,
  149. ];
  150. //添加顶级菜单时判断是否超过限定数量
  151. if ($data['pid'] == '0') {
  152. $num = $wechat_model->getWxmenuCount(array('pid' => 0));
  153. if ($num > 2) {
  154. $this->error(lang('top_menu_count_error'));
  155. }
  156. }
  157. $wechat_validate = ds_validate('wechat');
  158. if (!$wechat_validate->scene('menu_add')->check($data)) {
  159. $this->error($wechat_validate->getError());
  160. }
  161. $result = $wechat_model->addWxmenu($data);
  162. if ($result) {
  163. dsLayerOpenSuccess(lang('ds_common_save_succ'));
  164. } else {
  165. $this->error(lang('ds_common_save_fail'));
  166. }
  167. }
  168. }
  169. //菜单删除
  170. public function menu_drop() {
  171. $wechat_model = model('wechat');
  172. $menu_id = input('param.id');
  173. if (empty($menu_id)) {
  174. $this->error(lang('param_error'));
  175. }
  176. $res = $wechat_model->delWxmenu(array('id' => $menu_id));
  177. if ($res) {
  178. ds_json_encode(10000, lang('ds_common_del_succ'));
  179. } else {
  180. ds_json_encode(10001, lang('ds_common_del_fail'));
  181. }
  182. }
  183. //更新公众号菜单
  184. public function pub_menu() {
  185. //获取父级菜单
  186. $wechat_model = model('wechat');
  187. //获取菜单
  188. $config = $wechat_model->getOneWxconfig();
  189. $p_menus = $wechat_model->getWxmenuList(array('pid' => 0), 'id ASC');
  190. $p_menus = ds_change_arraykey($p_menus, 'id');
  191. $post_str = $this->convert_menu($p_menus);
  192. // http post请求
  193. if (!count($p_menus) > 0) {
  194. $this->error(lang('no_menu'), 'Wechat/menu');
  195. exit;
  196. }
  197. //查看access_token是否过期
  198. $wechat = new WechatApi($config);
  199. $expire_time = $config['expires_in'];
  200. if ($expire_time > TIMESTAMP) {
  201. //有效期内
  202. $wechat->access_token_ = $config['access_token'];
  203. } else {
  204. $access_token = $wechat->checkAuth();
  205. if ($access_token == FALSE) {
  206. $this->error(lang('ds_common_op_fail') . $wechat->errCode . $wechat->errMsg, 'Wechat/menu');
  207. }
  208. $web_expires = TIMESTAMP + 7000; // 提前200秒过期
  209. $condition = array();
  210. $condition[] = array('id', '=', $config['id']);
  211. $data = array('access_token' => $access_token, 'expires_in' => $web_expires);
  212. $wechat_model->editWxconfig($condition, $data);
  213. }
  214. $return = $wechat->createMenu($post_str);
  215. if ($return) {
  216. $this->success(lang('ds_common_op_succ'), 'Wechat/menu');
  217. } else {
  218. $this->error(lang('ds_common_op_fail') . $wechat->errCode . $wechat->errMsg);
  219. }
  220. }
  221. //菜单转换
  222. private function convert_menu($p_menus) {
  223. $wechat_model = model('wechat');
  224. $new_arr = array();
  225. $count = 0;
  226. foreach ($p_menus as $k => $v) {
  227. $new_arr[$count]['name'] = $v['name'];
  228. //获取子菜单
  229. $c_menus = $wechat_model->getMenulist(array('pid' => $k));
  230. if ($c_menus) {
  231. foreach ($c_menus as $kk => $vv) {
  232. $add = array();
  233. $add['name'] = $vv['name'];
  234. $add['type'] = $vv['type'];
  235. // click类型
  236. if ($add['type'] == 'click') {
  237. $add['key'] = $vv['value'];
  238. } elseif ($add['type'] == 'view') {
  239. $add['url'] = $vv['value'];
  240. } else {
  241. $add['key'] = $vv['value'];
  242. }
  243. $add['sub_button'] = array();
  244. if ($add['name']) {
  245. $new_arr[$count]['sub_button'][] = $add;
  246. }
  247. }
  248. } else {
  249. $new_arr[$count]['type'] = $v['type'];
  250. // click类型
  251. if ($new_arr[$count]['type'] == 'click') {
  252. $new_arr[$count]['key'] = $v['value'];
  253. } elseif ($new_arr[$count]['type'] == 'view') {
  254. //跳转URL类型
  255. $new_arr[$count]['url'] = $v['value'];
  256. } else {
  257. //其他事件类型
  258. $new_arr[$count]['key'] = $v['value'];
  259. }
  260. }
  261. $count++;
  262. }
  263. return array('button' => $new_arr);
  264. }
  265. /**
  266. * 关键字文本回复
  267. */
  268. public function k_text() {
  269. $wechat_model = model('wechat');
  270. $wechat = $wechat_model->getOneWxconfig();
  271. if (empty($wechat)) {
  272. $this->error(lang('please_set_wechat_config'), 'Wechat/setting');
  273. }
  274. $lists = $wechat_model->getWxkeywordList(array('type' => 'TEXT'), 'k.id,k.keyword,t.text', 10, 't.createtime DESC');
  275. View::assign('lists', $lists);
  276. View::assign('show_page', $wechat_model->page_info->render());
  277. return View::fetch();
  278. }
  279. /*
  280. * 添加文本回复
  281. */
  282. public function text_form() {
  283. $wechat_model = model('wechat');
  284. $wechat = $wechat_model->getOneWxconfig();
  285. if (empty($wechat)) {
  286. $this->error(lang('please_set_wechat_config'), 'Wechat/setting');
  287. }
  288. if (request()->isPost()) {
  289. $kid = input('param.id');
  290. $add['keyword'] = input('param.keyword');
  291. $add['text'] = input('param.text');
  292. if (empty($kid)) {
  293. //添加模式
  294. $add['createtime'] = TIMESTAMP;
  295. $add['pid'] = $wechat_model->addWxtext($add);
  296. unset($add['text']);
  297. unset($add['createtime']);
  298. $add['type'] = 'TEXT';
  299. $row = $wechat_model->addWxkeyword($add);
  300. } else {
  301. //编辑模式
  302. $data = $wechat_model->getOneWxkeyword(array('id' => $kid));
  303. if ($data) {
  304. $update['keyword'] = $add['keyword'];
  305. $wechat_model->editWxkeyword(array('id' => $kid), $update);
  306. $row = $wechat_model->editWxtext(array('id' => $data['pid']), $add);
  307. }
  308. }
  309. $row >= 0 ? dsLayerOpenSuccess(lang('ds_common_op_succ')) : $this->error(lang('ds_common_op_fail'), 'Wechat/k_text');
  310. exit;
  311. } else {
  312. //编辑状态
  313. $id = intval(input('param.id'));
  314. $key = array();
  315. if ($id) {
  316. $where = "k.id={$id} AND k.type='TEXT'";
  317. $res = Db::name('wxkeyword')->alias('k')->join('wxtext t', 't.id=k.id', 'LEFT')->where($where)->field('k.id,k.keyword,t.text')->find();
  318. View::assign('key', $res);
  319. }
  320. return View::fetch();
  321. }
  322. }
  323. /*
  324. * 删除文本回复
  325. */
  326. public function del_text() {
  327. $wechat_model = model('wechat');
  328. $id = input('param.id');
  329. $row = $wechat_model->getOneWxkeyword(array('id' => $id));
  330. if ($row) {
  331. $wechat_model->delWxkeyword(array('id' => $id));
  332. $wechat_model->delWxtext(array('id' => $row['pid']));
  333. ds_json_encode(10000, lang('ds_common_del_succ'));
  334. } else {
  335. ds_json_encode(10001, lang('ds_common_del_fail'));
  336. }
  337. }
  338. /**
  339. * 删除消息推送
  340. */
  341. public function del_wxmsg() {
  342. $wechat_model = model('wechat');
  343. $id = input('param.id');
  344. $id_array = ds_delete_param($id);
  345. if ($id_array === FALSE) {
  346. ds_json_encode(10001, lang('param_error'));
  347. }
  348. $condition = array(array('id', 'in', $id_array));
  349. $result = $wechat_model->delWxmsg($condition);
  350. if ($result) {
  351. ds_json_encode(10000, lang('ds_common_del_succ'));
  352. } else {
  353. ds_json_encode(10001, lang('ds_common_del_fail'));
  354. }
  355. }
  356. /* * 微信注册会员列表 */
  357. public function member() {
  358. $wechat_model = model('wechat');
  359. $wxmember_list = $wechat_model->getWxmemberList();
  360. View::assign('show_page', $wechat_model->page_info->render());
  361. View::assign('wxmember_list', $wxmember_list);
  362. return View::fetch('member');
  363. }
  364. /* * 消息推送 */
  365. public function msend() {
  366. $touser = input('param.openid');
  367. $id = input('param.member_id');
  368. if (request()->isPost()) {
  369. $wechat_model = model('wechat');
  370. $config = $wechat_model->getOneWxconfig();
  371. $wechat = new WechatApi($config);
  372. $type = input('param.type');
  373. if ($type == 'text') {
  374. //发送文本消息
  375. $content = input('param.text');
  376. $send = array(
  377. 'touser' => $touser, 'msgtype' => 'text', 'text' => array('content' => $content)
  378. );
  379. } else {
  380. //发送图文消息
  381. $title = input('param.title');
  382. $description = input('param.description');
  383. $url = input('param.url');
  384. $picUrl = '';
  385. if (!empty($_FILES['s_pic']['name'])) {
  386. $prefix = $id;
  387. $file_name = $prefix . '_' . date('YmdHis') . rand(10000, 99999) . '.png';
  388. $res = ds_upload_pic(DIR_ADMIN . DIRECTORY_SEPARATOR . 'wechat', 's_pic', $file_name);
  389. if ($res['code']) {
  390. $file_name = $res['data']['file_name'];
  391. } else {
  392. $this->error($res['msg']);
  393. }
  394. $picUrl = ds_get_pic( DIR_ADMIN . DIRECTORY_SEPARATOR . 'wechat' , $file_name);
  395. }
  396. $content = array(
  397. array(
  398. 'title' => $title, 'description' => $description, 'url' => $url, 'picurl' => $picUrl
  399. )
  400. );
  401. $send = array(
  402. 'touser' => $touser, 'msgtype' => 'news', 'news' => array('articles' => $content)
  403. );
  404. }
  405. $SendInfo = serialize($send);
  406. $data['member_id'] = $id;
  407. $data['content'] = $SendInfo;
  408. $data['createtime'] = TIMESTAMP;
  409. $ret = $wechat->sendCustomMessage($send);
  410. if ($ret) {
  411. //添加至推送列表
  412. $data['issend'] = '1';
  413. $wechat_model->addWxmsg($data);
  414. dsLayerOpenSuccess(lang('ds_common_op_succ'));
  415. } else {
  416. $data['issend'] = '0';
  417. $wechat_model->addWxmsg($data);
  418. $this->error(lang('ds_common_op_fail') . $wechat->errCode . $wechat->errMsg);
  419. }
  420. } else {
  421. return View::fetch();
  422. }
  423. }
  424. /* * 消息推送列表 */
  425. public function SendList() {
  426. $wechat_model = model('wechat');
  427. $list = $wechat_model->getWxmsgList();
  428. foreach ($list as $key => $val) {
  429. $info = unserialize($val['content']);
  430. $type = $info['msgtype'];
  431. $list[$key]['type'] = $type == 'text' ? lang('message_type_text') : lang('message_type_news');
  432. if ($type == 'text') {
  433. $list[$key]['content'] = $info['text']['content'];
  434. } else {
  435. $content = $info['news']['articles']['0'];
  436. $content = json_encode($content);
  437. $list[$key]['content'] = "<a href='javascript:void(0);' class='news' content=''>查看图文消息</a>";
  438. /* View::assign('title',$content['title']);
  439. View::assign('description',$content['description']);
  440. View::assign('url',$content['url']);
  441. echo View::fetch('news'); */
  442. }
  443. }
  444. View::assign('show_page', $wechat_model->page_info->render());
  445. View::assign('lists', $list);
  446. return View::fetch('list');
  447. }
  448. /* * 消息群发 */
  449. public function Sendgroup() {
  450. if (request()->isPost()) {
  451. $m_info = model('wechat')->getWxmemberList();
  452. $openid = '';
  453. foreach ($m_info as $k => $val) {
  454. $openid .= $val['member_wxopenid'] . ',';
  455. }
  456. $openid = explode(',', $openid);
  457. $content = input('param.text');
  458. $send = array(
  459. 'touser' => $openid,
  460. 'msgtype' => 'text',
  461. 'text' => array('content' => $content)
  462. );
  463. $config = model('wechat')->getOneWxconfig();
  464. $wechat = new WechatApi($config);
  465. $res = $wechat->massSend($send);
  466. if ($res) {
  467. dsLayerOpenSuccess(lang('ds_common_op_succ'));
  468. } else {
  469. $this->error(lang('ds_common_op_fail') . $wechat->errCode . $wechat->errMsg);
  470. }
  471. } else {
  472. return View::fetch('sendgroup');
  473. }
  474. }
  475. protected function getAdminItemList() {
  476. $menu_array = array(
  477. array(
  478. 'name' => 'menu',
  479. 'text' => lang('wechat_menu'),
  480. 'url' => (string) url('Wechat/menu')
  481. ),
  482. array(
  483. 'name' => 'menu_add',
  484. 'text' => lang('ds_new') . lang('wechat_menu'),
  485. 'url' => "javascript:dsLayerOpen('" . (string) url('Wechat/menu_add') . "','" . lang('ds_new') . lang('wechat_menu') . "')"
  486. ),
  487. );
  488. return $menu_array;
  489. }
  490. }