Wechat.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  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. $wechat_model = model('wechat');
  64. $menu_list = $wechat_model->getWxmenuList(array(array('pid','=',0)));
  65. $menu_json=array();
  66. foreach($menu_list as $key => $val){
  67. $menu_json[$key]=array();
  68. $menu_json[$key][]=array('name'=>'name','value'=>$val['name']);
  69. if($val['type']){
  70. $menu_json[$key][]=array('name'=>'type','value'=>$val['type']);
  71. }
  72. if($val['value']){
  73. $val['value']= json_decode($val['value'], true);
  74. foreach($val['value'] as $k => $v){
  75. if($k=='url'){
  76. if($val['type']=='view'){
  77. $menu_json[$key][]=array('name'=>'url1','value'=>$v);
  78. }else if($val['type']=='miniprogram'){
  79. $menu_json[$key][]=array('name'=>'url2','value'=>$v);
  80. }
  81. }else{
  82. $menu_json[$key][]=array('name'=>$k,'value'=>$v);
  83. }
  84. }
  85. }
  86. $menu_json[$key][]=array('child'=>[]);
  87. $menu_sub_list=$wechat_model->getWxmenuList(array(array('pid','=',$val['id'])));
  88. $menu_list[$key]['child']=$menu_sub_list;
  89. if(!empty($menu_sub_list)){
  90. foreach($menu_sub_list as $key1 => $val1){
  91. $temp=array();
  92. $temp[]=array('name'=>'name','value'=>$val1['name']);
  93. if($val1['type']){
  94. $temp[]=array('name'=>'type','value'=>$val1['type']);
  95. }
  96. if($val1['value']){
  97. $val1['value']= json_decode($val1['value'], true);
  98. foreach($val1['value'] as $k => $v){
  99. if($k=='url'){
  100. if($val1['type']=='view'){
  101. $temp[]=array('name'=>'url1','value'=>$v);
  102. }else if($val1['type']=='miniprogram'){
  103. $temp[]=array('name'=>'url2','value'=>$v);
  104. }
  105. }else{
  106. $temp[]=array('name'=>$k,'value'=>$v);
  107. }
  108. }
  109. }
  110. $menu_json[$key][count($menu_json[$key])-1]['child'][]=$temp;
  111. }
  112. }
  113. }
  114. View::assign('menu_list', $menu_list);
  115. View::assign('menu_json', $menu_json);
  116. $this->setAdminCurItem('menu');
  117. return View::fetch();
  118. }
  119. public function add_menu(){
  120. $menus=input('param.menus/a');
  121. $result='';
  122. $menu_list=array();
  123. Db::startTrans();
  124. try {
  125. $wechat_model = model('wechat');
  126. $condition=array(array('id','>',0));
  127. $wechat_model->delWxmenu($condition);
  128. foreach($menus as $key => $val){
  129. $temp=array();
  130. foreach($val as $k => $v){
  131. if($k!=(count($val)-1)){
  132. $temp[$v['name']]=$v['value'];
  133. }
  134. }
  135. $id=count($menu_list)+1;
  136. $menu_list[]=array('id'=>$id,'pid'=>0,'child_count'=>isset($val[count($val)-1]['child'])?count($val[count($val)-1]['child']):0,'value'=>$temp,'index1'=>$key,'index2'=>-1);
  137. if(isset($val[count($val)-1]['child'])){
  138. foreach($val[count($val)-1]['child'] as $k1 => $v1){
  139. $temp=array();
  140. foreach($v1 as $k => $v){
  141. $temp[$v['name']]=$v['value'];
  142. }
  143. $menu_list[]=array('id'=>count($menu_list)+1,'pid'=>$id,'value'=>$temp,'index1'=>$key,'index2'=>$k1);
  144. }
  145. }
  146. }
  147. $menu_array=array();
  148. foreach($menu_list as $val){
  149. if(trim($val['value']['name'])==''){
  150. $result=array('index1'=>$val['index1'],'index2'=>$val['index2'],'name'=>'name');
  151. throw new \think\Exception('', 10006);
  152. }
  153. if(isset($val['child_count']) && $val['child_count']>0){
  154. $menu_array[]=array(
  155. 'id'=>$val['id'],
  156. 'pid'=>$val['pid'],
  157. 'name'=>$val['value']['name'],
  158. 'type'=>'',
  159. 'value'=>''
  160. );
  161. }else{
  162. $temp=array(
  163. 'id'=>$val['id'],
  164. 'pid'=>$val['pid'],
  165. 'name'=>$val['value']['name'],
  166. 'type'=>$val['value']['type'],
  167. );
  168. switch($val['value']['type']){
  169. case 'article_id':
  170. if(trim($val['value']['article_id'])==''){
  171. $result=array('index1'=>$val['index1'],'index2'=>$val['index2'],'name'=>'article_id');
  172. throw new \think\Exception('', 10006);
  173. }
  174. $temp['value']=json_encode(array('article_id'=>$val['value']['article_id']));
  175. break;
  176. case 'click':
  177. if(trim($val['value']['key'])==''){
  178. $result=array('index1'=>$val['index1'],'index2'=>$val['index2'],'name'=>'key');
  179. throw new \think\Exception('', 10006);
  180. }
  181. $temp['value']=json_encode(array('key'=>$val['value']['key']));
  182. break;
  183. case 'view':
  184. if(trim($val['value']['url1'])==''){
  185. $result=array('index1'=>$val['index1'],'index2'=>$val['index2'],'name'=>'url1');
  186. throw new \think\Exception('', 10006);
  187. }
  188. $temp['value']=json_encode(array('url'=>$val['value']['url1']));
  189. break;
  190. case 'miniprogram':
  191. if(trim($val['value']['url2'])=='' || trim($val['value']['appid'])=='' || trim($val['value']['pagepath'])==''){
  192. $result=array('index1'=>$val['index1'],'index2'=>$val['index2'],'name'=>((trim($val['value']['url2'])=='')?'url2':((trim($val['value']['appid'])=='')?'appid':'pagepath')));
  193. throw new \think\Exception('', 10006);
  194. }
  195. $temp['value']=json_encode(array('url'=>$val['value']['url2'],'appid'=>$val['value']['appid'],'pagepath'=>$val['value']['pagepath']));
  196. break;
  197. default:
  198. $result=array('index1'=>$val['index1'],'index2'=>$val['index2'],'name'=>'type');
  199. throw new \think\Exception('', 10006);
  200. }
  201. $menu_array[]=$temp;
  202. }
  203. }
  204. if(!empty($menu_array)){
  205. Db::name('wxmenu')->insertAll($menu_array);
  206. }
  207. } catch (\Exception $e) {
  208. Db::rollback();
  209. ds_json_encode(10001, $e->getMessage(),$result);
  210. }
  211. Db::commit();
  212. ds_json_encode(10000, lang('ds_common_op_succ'));
  213. }
  214. //更新公众号菜单
  215. public function pub_menu() {
  216. //获取父级菜单
  217. $wechat_model = model('wechat');
  218. //获取菜单
  219. $config = $wechat_model->getOneWxconfig();
  220. $p_menus = $wechat_model->getWxmenuList(array('pid' => 0), 'id ASC');
  221. $p_menus = ds_change_arraykey($p_menus, 'id');
  222. $post_str = $this->convert_menu($p_menus);
  223. // http post请求
  224. if (!count($p_menus) > 0) {
  225. $this->error(lang('no_menu'), 'Wechat/menu');
  226. exit;
  227. }
  228. //查看access_token是否过期
  229. $wechat = new WechatApi($config);
  230. $expire_time = $config['expires_in'];
  231. if ($expire_time > TIMESTAMP) {
  232. //有效期内
  233. $wechat->access_token_ = $config['access_token'];
  234. } else {
  235. $access_token = $wechat->checkAuth();
  236. if ($access_token == FALSE) {
  237. $this->error(lang('ds_common_op_fail') . $wechat->errCode . $wechat->errMsg, 'Wechat/menu');
  238. }
  239. $web_expires = TIMESTAMP + 7000; // 提前200秒过期
  240. $condition = array();
  241. $condition[] = array('id', '=', $config['id']);
  242. $data = array('access_token' => $access_token, 'expires_in' => $web_expires);
  243. $wechat_model->editWxconfig($condition, $data);
  244. }
  245. $return = $wechat->createMenu($post_str);
  246. if ($return) {
  247. $this->success(lang('ds_common_op_succ'), 'Wechat/menu');
  248. } else {
  249. $this->error(lang('ds_common_op_fail') . $wechat->errCode . $wechat->errMsg);
  250. }
  251. }
  252. //菜单转换
  253. private function convert_menu($p_menus) {
  254. $wechat_model = model('wechat');
  255. $new_arr = array();
  256. $count = 0;
  257. foreach ($p_menus as $k => $v) {
  258. $new_arr[$count]['name'] = $v['name'];
  259. //获取子菜单
  260. $c_menus = $wechat_model->getMenulist(array('pid' => $k));
  261. if ($c_menus) {
  262. foreach ($c_menus as $kk => $vv) {
  263. $add = array();
  264. $add['name'] = $vv['name'];
  265. $add['type'] = $vv['type'];
  266. $add=array_merge($add, json_decode($vv['value'],true));
  267. $new_arr[$count]['sub_button'][] = $add;
  268. }
  269. } else {
  270. $new_arr[$count]['type'] = $v['type'];
  271. $new_arr[$count]=array_merge($new_arr[$count], json_decode($v['value'],true));
  272. }
  273. $count++;
  274. }
  275. return array('button' => $new_arr);
  276. }
  277. /**
  278. * 关键字文本回复
  279. */
  280. public function k_text() {
  281. $wechat_model = model('wechat');
  282. $wechat = $wechat_model->getOneWxconfig();
  283. if (empty($wechat)) {
  284. $this->error(lang('please_set_wechat_config'), 'Wechat/setting');
  285. }
  286. $lists = $wechat_model->getWxkeywordList(array('type' => 'TEXT'), 'k.id,k.keyword,t.text', 10, 't.createtime DESC');
  287. View::assign('lists', $lists);
  288. View::assign('show_page', $wechat_model->page_info->render());
  289. return View::fetch();
  290. }
  291. /*
  292. * 添加文本回复
  293. */
  294. public function text_form() {
  295. $wechat_model = model('wechat');
  296. $wechat = $wechat_model->getOneWxconfig();
  297. if (empty($wechat)) {
  298. $this->error(lang('please_set_wechat_config'), 'Wechat/setting');
  299. }
  300. if (request()->isPost()) {
  301. $kid = input('param.id');
  302. $add['keyword'] = input('param.keyword');
  303. $add['text'] = input('param.text');
  304. if (empty($kid)) {
  305. //添加模式
  306. $add['createtime'] = TIMESTAMP;
  307. $add['pid'] = $wechat_model->addWxtext($add);
  308. unset($add['text']);
  309. unset($add['createtime']);
  310. $add['type'] = 'TEXT';
  311. $row = $wechat_model->addWxkeyword($add);
  312. } else {
  313. //编辑模式
  314. $data = $wechat_model->getOneWxkeyword(array('id' => $kid));
  315. if ($data) {
  316. $update['keyword'] = $add['keyword'];
  317. $wechat_model->editWxkeyword(array('id' => $kid), $update);
  318. $row = $wechat_model->editWxtext(array('id' => $data['pid']), $add);
  319. }
  320. }
  321. $row >= 0 ? dsLayerOpenSuccess(lang('ds_common_op_succ')) : $this->error(lang('ds_common_op_fail'), 'Wechat/k_text');
  322. exit;
  323. } else {
  324. //编辑状态
  325. $id = intval(input('param.id'));
  326. $key = array();
  327. if ($id) {
  328. $where = "k.id={$id} AND k.type='TEXT'";
  329. $res = Db::name('wxkeyword')->alias('k')->join('wxtext t', 't.id=k.id', 'LEFT')->where($where)->field('k.id,k.keyword,t.text')->find();
  330. View::assign('key', $res);
  331. }
  332. return View::fetch();
  333. }
  334. }
  335. /*
  336. * 删除文本回复
  337. */
  338. public function del_text() {
  339. $wechat_model = model('wechat');
  340. $id = input('param.id');
  341. $row = $wechat_model->getOneWxkeyword(array('id' => $id));
  342. if ($row) {
  343. $wechat_model->delWxkeyword(array('id' => $id));
  344. $wechat_model->delWxtext(array('id' => $row['pid']));
  345. ds_json_encode(10000, lang('ds_common_del_succ'));
  346. } else {
  347. ds_json_encode(10001, lang('ds_common_del_fail'));
  348. }
  349. }
  350. /**
  351. * 删除消息推送
  352. */
  353. public function del_wxmsg() {
  354. $wechat_model = model('wechat');
  355. $id = input('param.id');
  356. $id_array = ds_delete_param($id);
  357. if ($id_array === FALSE) {
  358. ds_json_encode(10001, lang('param_error'));
  359. }
  360. $condition = array(array('id', 'in', $id_array));
  361. $result = $wechat_model->delWxmsg($condition);
  362. if ($result) {
  363. ds_json_encode(10000, lang('ds_common_del_succ'));
  364. } else {
  365. ds_json_encode(10001, lang('ds_common_del_fail'));
  366. }
  367. }
  368. /* * 微信注册会员列表 */
  369. public function member() {
  370. $wechat_model = model('wechat');
  371. $wxmember_list = $wechat_model->getWxmemberList();
  372. foreach($wxmember_list as $key => $val){
  373. @$wxmember_list[$key]['member_wxinfo']=unserialize($val['member_wxinfo']);
  374. }
  375. View::assign('show_page', $wechat_model->page_info->render());
  376. View::assign('wxmember_list', $wxmember_list);
  377. return View::fetch('member');
  378. }
  379. /* * 消息推送 */
  380. public function msend() {
  381. $touser = input('param.openid');
  382. $id = input('param.member_id');
  383. if (request()->isPost()) {
  384. $wechat_model = model('wechat');
  385. $config = $wechat_model->getOneWxconfig();
  386. $wechat = new WechatApi($config);
  387. $type = input('param.type');
  388. if ($type == 'text') {
  389. //发送文本消息
  390. $content = input('param.text');
  391. $send = array(
  392. 'touser' => $touser, 'msgtype' => 'text', 'text' => array('content' => $content)
  393. );
  394. } else {
  395. //发送图文消息
  396. $title = input('param.title');
  397. $description = input('param.description');
  398. $url = input('param.url');
  399. $picUrl = '';
  400. if (!empty($_FILES['s_pic']['name'])) {
  401. $prefix = $id;
  402. $file_name = $prefix . '_' . date('YmdHis') . rand(10000, 99999) . '.png';
  403. $res = ds_upload_pic(DIR_ADMIN . DIRECTORY_SEPARATOR . 'wechat', 's_pic', $file_name);
  404. if ($res['code']) {
  405. $file_name = $res['data']['file_name'];
  406. } else {
  407. $this->error($res['msg']);
  408. }
  409. $picUrl = ds_get_pic( DIR_ADMIN . DIRECTORY_SEPARATOR . 'wechat' , $file_name);
  410. }
  411. $content = array(
  412. array(
  413. 'title' => $title, 'description' => $description, 'url' => $url, 'picurl' => $picUrl
  414. )
  415. );
  416. $send = array(
  417. 'touser' => $touser, 'msgtype' => 'news', 'news' => array('articles' => $content)
  418. );
  419. }
  420. $SendInfo = serialize($send);
  421. $data['member_id'] = $id;
  422. $data['content'] = $SendInfo;
  423. $data['createtime'] = TIMESTAMP;
  424. $ret = $wechat->sendCustomMessage($send);
  425. if ($ret) {
  426. //添加至推送列表
  427. $data['issend'] = '1';
  428. $wechat_model->addWxmsg($data);
  429. dsLayerOpenSuccess(lang('ds_common_op_succ'));
  430. } else {
  431. $data['issend'] = '0';
  432. $wechat_model->addWxmsg($data);
  433. $this->error(lang('ds_common_op_fail') . $wechat->errCode . $wechat->errMsg);
  434. }
  435. } else {
  436. return View::fetch();
  437. }
  438. }
  439. /* * 消息推送列表 */
  440. public function SendList() {
  441. $wechat_model = model('wechat');
  442. $list = $wechat_model->getWxmsgList();
  443. foreach ($list as $key => $val) {
  444. $info = unserialize($val['content']);
  445. $type = $info['msgtype'];
  446. $list[$key]['type'] = $type == 'text' ? lang('message_type_text') : lang('message_type_news');
  447. if ($type == 'text') {
  448. $list[$key]['content'] = $info['text']['content'];
  449. } else {
  450. $content = $info['news']['articles']['0'];
  451. $content = json_encode($content);
  452. $list[$key]['content'] = "<a href='javascript:void(0);' class='news' content=''>查看图文消息</a>";
  453. /* View::assign('title',$content['title']);
  454. View::assign('description',$content['description']);
  455. View::assign('url',$content['url']);
  456. echo View::fetch('news'); */
  457. }
  458. }
  459. View::assign('show_page', $wechat_model->page_info->render());
  460. View::assign('lists', $list);
  461. return View::fetch('list');
  462. }
  463. /* * 消息群发 */
  464. public function Sendgroup() {
  465. if (request()->isPost()) {
  466. $m_info = model('wechat')->getWxmemberList();
  467. $openid = '';
  468. foreach ($m_info as $k => $val) {
  469. $openid .= $val['member_wxopenid'] . ',';
  470. }
  471. $openid = explode(',', $openid);
  472. $content = input('param.text');
  473. $send = array(
  474. 'touser' => $openid,
  475. 'msgtype' => 'text',
  476. 'text' => array('content' => $content)
  477. );
  478. $config = model('wechat')->getOneWxconfig();
  479. $wechat = new WechatApi($config);
  480. $res = $wechat->massSend($send);
  481. if ($res) {
  482. dsLayerOpenSuccess(lang('ds_common_op_succ'));
  483. } else {
  484. $this->error(lang('ds_common_op_fail') . $wechat->errCode . $wechat->errMsg);
  485. }
  486. } else {
  487. return View::fetch('sendgroup');
  488. }
  489. }
  490. public function material(){
  491. $wechat_model = model('wechat');
  492. $wechat = $wechat_model->getOneWxconfig();
  493. if (empty($wechat)) {
  494. $this->error(lang('please_set_wechat_config'), 'Wechat/setting');
  495. }
  496. $template=array(
  497. 'offset'=>(input('param.page',1)-1)*10,
  498. 'count'=>10,
  499. );
  500. $res=$wechat_model->getMaterialList($template);
  501. if(!$res['code']){
  502. $this->error($res['msg']);
  503. }
  504. $paginate = Db::name('wxconfig')->paginate(10,$res['data']['total_count'],['query' => request()->param()]);
  505. View::assign('show_page', $paginate->render());
  506. View::assign('list', $res['data']['item']);
  507. $this->setAdminCurItem('material');
  508. return View::fetch();
  509. }
  510. public function freepublish(){
  511. $wechat_model = model('wechat');
  512. $wechat = $wechat_model->getOneWxconfig();
  513. if (empty($wechat)) {
  514. $this->error(lang('please_set_wechat_config'), 'Wechat/setting');
  515. }
  516. $template=array(
  517. 'offset'=>(input('param.page',1)-1)*10,
  518. 'count'=>10,
  519. );
  520. $res=$wechat_model->getFreepublishList($template);
  521. if(!$res['code']){
  522. $this->error($res['msg']);
  523. }
  524. $paginate = Db::name('wxconfig')->paginate(10,$res['data']['total_count'],['query' => request()->param()]);
  525. View::assign('show_page', $paginate->render());
  526. View::assign('list', $res['data']['item']);
  527. $this->setAdminCurItem('freepublish');
  528. return View::fetch();
  529. }
  530. public function freepublish_del(){
  531. $wechat_model = model('wechat');
  532. $wechat = $wechat_model->getOneWxconfig();
  533. $res=$wechat_model->delFreepublish(input('param.article_id'));
  534. if(!$res['code']){
  535. ds_json_encode(10001, $res['msg']);
  536. }
  537. ds_json_encode(10000, lang('ds_common_op_succ'));
  538. }
  539. public function material_select(){
  540. $wechat_model = model('wechat');
  541. $wechat = $wechat_model->getOneWxconfig();
  542. if (empty($wechat)) {
  543. $this->error(lang('please_set_wechat_config'), 'Wechat/setting');
  544. }
  545. $template=array(
  546. 'type'=>'news',
  547. 'offset'=>(input('param.page',1)-1)*10,
  548. 'count'=>10,
  549. );
  550. $res=$wechat_model->getFreepublishList($template);
  551. if(!$res['code']){
  552. $this->error($res['msg']);
  553. }
  554. $paginate = Db::name('wxconfig')->paginate(10,$res['data']['total_count'],['query' => request()->param()]);
  555. View::assign('show_page', $paginate->render());
  556. View::assign('list', $res['data']['item']);
  557. return View::fetch();
  558. }
  559. public function material_add(){
  560. if (request()->isPost()) {
  561. $wechat_model = model('wechat');
  562. $wechat = $wechat_model->getOneWxconfig();
  563. $temp=input('param.articles/a');
  564. $articles=array();
  565. foreach($temp as $key => $val){
  566. $a=array();
  567. foreach($val as $v){
  568. if(in_array($v['name'],['thumb_media_id','title','author','content','content_source_url','need_open_comment','only_fans_can_comment'])){
  569. if(in_array($v['name'],['thumb_media_id','title','content','content_source_url']) && $v['value']==''){
  570. ds_json_encode(10001, '', array('index'=>$key,'name'=>$v['name']));
  571. }
  572. $a[$v['name']]=$v['value'];
  573. }
  574. }
  575. $articles[]=$a;
  576. }
  577. $template=array(
  578. 'articles'=>$articles
  579. );
  580. $res=$wechat_model->addMaterial($template);
  581. if(!$res['code']){
  582. ds_json_encode(10001, $res['msg']);
  583. }
  584. ds_json_encode(10000, lang('ds_common_op_succ'));
  585. }else{
  586. $this->setAdminCurItem('material_add');
  587. return View::fetch('material_form');
  588. }
  589. }
  590. public function get_freepublish(){
  591. $wechat_model = model('wechat');
  592. $wechat = $wechat_model->getOneWxconfig();
  593. $article_id=input('param.article_id');
  594. return $wechat_model->getFreepublishInfo($article_id);
  595. }
  596. public function material_edit(){
  597. $wechat_model = model('wechat');
  598. $wechat = $wechat_model->getOneWxconfig();
  599. $media_id=input('param.media_id');
  600. $res=$wechat_model->getMaterialInfo($media_id);
  601. if (request()->isPost()) {
  602. if(!$res['code']){
  603. ds_json_encode(10001, $res['msg']);
  604. }
  605. $material_info=$res['data']['news_item'];
  606. $temp=input('param.articles/a');
  607. $index=0;
  608. foreach($temp as $key => $val){
  609. $a=array();
  610. foreach($val as $v){
  611. if(in_array($v['name'],['thumb_media_id','title','author','content','content_source_url','need_open_comment','only_fans_can_comment'])){
  612. if(in_array($v['name'],['thumb_media_id','title','content','content_source_url']) && $v['value']==''){
  613. ds_json_encode(10001, '', array('index'=>$key,'name'=>$v['name']));
  614. }
  615. $a[$v['name']]=$v['value'];
  616. }
  617. }
  618. if(!isset($material_info[$index])){
  619. ds_json_encode(10001, '图文消息('.$index.')不存在');
  620. }
  621. ksort($a);
  622. ksort($material_info[$index]);
  623. if(json_encode($a)!=json_encode($material_info[$index])){
  624. $template=array(
  625. 'media_id'=>$media_id,
  626. 'index'=>$index,
  627. 'articles'=>$a
  628. );
  629. $res=$wechat_model->editMaterial($template);
  630. if(!$res['code']){
  631. ds_json_encode(10001, $res['msg']);
  632. }
  633. }
  634. $index++;
  635. }
  636. ds_json_encode(10000, lang('ds_common_op_succ'));
  637. }else{
  638. if(!$res['code']){
  639. $this->error($res['msg']);
  640. }
  641. foreach($res['data']['news_item'] as $key => $val){
  642. $res['data']['news_item'][$key]['content']=str_replace('data-src','src',$val['content']);
  643. }
  644. View::assign('material_info', $res['data']['news_item']);
  645. $this->setAdminCurItem('material_edit');
  646. return View::fetch('material_form');
  647. }
  648. }
  649. public function material_del(){
  650. $wechat_model = model('wechat');
  651. $wechat = $wechat_model->getOneWxconfig();
  652. $res=$wechat_model->delMaterial(input('param.media_id'));
  653. if(!$res['code']){
  654. ds_json_encode(10001, $res['msg']);
  655. }
  656. ds_json_encode(10000, lang('ds_common_op_succ'));
  657. }
  658. public function freepublish_submit(){
  659. $wechat_model = model('wechat');
  660. $wechat = $wechat_model->getOneWxconfig();
  661. $res=$wechat_model->submitFreepublish(input('param.media_id'));
  662. if(!$res['code']){
  663. ds_json_encode(10001, $res['msg']);
  664. }
  665. ds_json_encode(10000, lang('ds_common_op_succ'));
  666. }
  667. public function get_material_image(){
  668. $wechat_model = model('wechat');
  669. $wechat = $wechat_model->getOneWxconfig();
  670. return $wechat_model->getImage(input('param.media_id'));
  671. }
  672. public function upload_material_image(){
  673. $type= intval(input('param.type'));
  674. $wechat_model = model('wechat');
  675. $wechat = $wechat_model->getOneWxconfig();
  676. $res=$wechat_model->uploadMaterialImage($_FILES['file'],$type);
  677. if(!$res['code']){
  678. ds_json_encode(10001, $res['msg']);
  679. }
  680. ds_json_encode(10000, '',$res['data']);
  681. }
  682. protected function getAdminItemList() {
  683. if(strpos(request()->action(),'material')!==false || strpos(request()->action(),'freepublish')!==false){
  684. $menu_array = array(
  685. array(
  686. 'name' => 'material',
  687. 'text' => lang('draft_list'),
  688. 'url' => (string) url('Wechat/material')
  689. ),
  690. array(
  691. 'name' => 'freepublish',
  692. 'text' => lang('freepublish_list'),
  693. 'url' => (string) url('Wechat/freepublish')
  694. ),
  695. );
  696. if(request()->action()=='material_edit'){
  697. $menu_array[]=array(
  698. 'name' => 'material_edit',
  699. 'text' => lang('ds_edit'),
  700. 'url' => 'javascript:void(0)'
  701. );
  702. }else{
  703. $menu_array[]=array(
  704. 'name' => 'material_add',
  705. 'text' => lang('ds_new'),
  706. 'url' => (string) url('Wechat/material_add')
  707. );
  708. }
  709. }else{
  710. $menu_array = array(
  711. array(
  712. 'name' => 'menu',
  713. 'text' => lang('wechat_menu'),
  714. 'url' => (string) url('Wechat/menu')
  715. ),
  716. );
  717. }
  718. return $menu_array;
  719. }
  720. }