Response.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. namespace app\admin\controller\wechat;
  3. use app\common\controller\Backend;
  4. use addons\wechat\library\Wechat;
  5. use app\common\library\WeChatObject;
  6. use app\common\service\LogService;
  7. use app\main\service\UrlService;
  8. use think\Session;
  9. use EasyWeChat\Factory;
  10. use app\common\library\Redis;
  11. use Symfony\Component\Cache\Simple\RedisCache;
  12. /**
  13. * 资源管理
  14. *
  15. * @icon fa fa-list-alt
  16. */
  17. class Response extends Backend
  18. {
  19. protected $model = null;
  20. protected $searchFields = 'id,title';
  21. protected $dataLimit = 'personal';
  22. public function _initialize()
  23. {
  24. parent::_initialize();
  25. $this->model = model('WechatResponse');
  26. }
  27. /**
  28. * 查看
  29. */
  30. public function index()
  31. {
  32. //设置过滤方法
  33. $this->request->filter(['strip_tags']);
  34. if ($this->request->isAjax())
  35. {
  36. //如果发送的来源是Selectpage,则转发到Selectpage
  37. if ($this->request->request('pkey_name'))
  38. {
  39. return $this->selectpage();
  40. }
  41. if($this->request->get("filter1")){
  42. if($this->request->get('filter')){
  43. $filter1 = json_decode($this->request->get("filter1"),true);
  44. $filter = json_decode($this->request->get("filter"),true);
  45. $this->request->get(["filter"=>json_encode(array_merge($filter,$filter1))]);
  46. }else{
  47. $this->request->get(["filter"=> $this->request->get("filter1")]);
  48. }
  49. }
  50. if($this->request->get("op1")){
  51. if($this->request->get("op")){
  52. $op1 = json_decode($this->request->get("op1"),true);
  53. $op = json_decode($this->request->get("op"),true);
  54. $this->request->get(["op"=>json_encode(array_merge($op,$op1))]);
  55. }else{
  56. $this->request->get(["op"=> $this->request->get("op1")]);
  57. }
  58. }
  59. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  60. $total = $this->model
  61. ->where($where)
  62. ->order($sort, $order)
  63. ->count();
  64. $list = $this->model
  65. ->where($where)
  66. ->order($sort, $order)
  67. ->limit($offset, $limit)
  68. ->select();
  69. $result = array("total" => $total, "rows" => $list);
  70. return json($result);
  71. }
  72. if($this->auth->agent_id || $this->group == 3){
  73. if(!model('AdminConfig')->checkWechatConfig($this->auth->id)){
  74. $this->error('请先授权微信服务号给本系统,正在跳转授权设置页面~', url('admin/config'));
  75. }
  76. }
  77. return $this->view->fetch();
  78. }
  79. /**
  80. * 选择素材
  81. */
  82. public function select()
  83. {
  84. return $this->view->fetch();
  85. }
  86. /**
  87. * 添加
  88. */
  89. public function add()
  90. {
  91. $is_subscribe = 0;
  92. if($this->request->get("filter1")){
  93. $is_subscribeAr = json_decode($this->request->get("filter1"),true);
  94. if(is_array($is_subscribeAr)&&!empty($is_subscribeAr)){
  95. $is_subscribe = ($is_subscribeAr['is_subscribe']==0) ? 1 : 0;
  96. }
  97. }
  98. $this->assign('is_subscribe',$is_subscribe);
  99. if ($this->request->isPost())
  100. {
  101. $params = $this->request->post("row/a");
  102. $params['admin_id'] = $this->auth->id;
  103. if($params['is_subscribe'] == 0){
  104. $conArr = json_decode($params['content'],true);
  105. if(is_array($conArr)&&!empty($conArr)){
  106. $params['content'] = json_encode(array($conArr[0]));
  107. }
  108. }
  109. $params['content'] = UrlService::instance()->replaceReferralHost($this->getCurrentAccountChannelId(), $params['content'])->data;
  110. $params['eventkey'] = isset($params['eventkey']) && $params['eventkey'] ? $params['eventkey'] : $this->auth->id . '_' . uniqid();
  111. switch ($params['type']) {
  112. case 'text': //文本
  113. break;
  114. case 'image': //图片
  115. $row = model('AdminConfig')->getAdminInfoAll($this->auth->id);
  116. $wechat = new WeChatObject($row);
  117. $officialAccount = $wechat->getOfficialAccount();
  118. $upload_url = str_replace(config('site.cdnurl'),'',ROOT_PATH . 'public' . $params['content']);
  119. $result = $officialAccount->material->uploadImage($upload_url);
  120. if (!isset($result['media_id'])) {
  121. $this->error('图片素材上传微信失败!');
  122. }
  123. $result['url'] = $params['content']; //保存图片原始上传相对url
  124. $params['content'] = json_encode($result);
  125. break;
  126. case 'news': //图文
  127. break;
  128. }
  129. if ($params)
  130. {
  131. $this->model->save($params);
  132. $this->success('',null, $params);
  133. $this->content = $params;
  134. }
  135. $this->error();
  136. }
  137. return $this->view->fetch();
  138. }
  139. /**
  140. * 编辑
  141. */
  142. public function edit($ids = NULL)
  143. {
  144. $row = $this->model->get($ids);
  145. if (!$row)
  146. $this->error(__('No Results were found'));
  147. if ($this->request->isPost())
  148. {
  149. $params = $this->request->post("row/a");
  150. $params['eventkey'] = isset($params['eventkey']) && $params['eventkey'] ? $params['eventkey'] : $this->auth->id . '_' . uniqid();
  151. if($params['is_subscribe'] == 0){
  152. $conArr = json_decode($params['content'],true);
  153. if(is_array($conArr)&&!empty($conArr)){
  154. $params['content'] = json_encode(array($conArr[0]));
  155. }
  156. }
  157. $params['content'] = UrlService::instance()->replaceReferralHost($this->getCurrentAccountChannelId(), $params['content'])->data;
  158. switch ($params['type']) {
  159. case 'text': //文本
  160. break;
  161. case 'image': //图片
  162. $content = json_decode($row->content, true);
  163. if ($content['url'] != $params['content']) {
  164. $adminConfig = model('AdminConfig')->getAdminInfoAll($this->auth->id);
  165. $wechat = new WeChatObject($adminConfig);
  166. $officialAccount = $wechat->getOfficialAccount();
  167. $result = $officialAccount->media->uploadImage(ROOT_PATH . 'public' . $params['content']);
  168. if (!isset($result['media_id'])) {
  169. $this->error('图片素材上传微信失败!');
  170. }
  171. $result['url'] = $params['content']; //保存图片原始上传相对url
  172. $params['content'] = json_encode($result);
  173. } else {
  174. unset($params['content']);
  175. }
  176. break;
  177. case 'news': //图文
  178. break;
  179. }
  180. if ($params)
  181. {
  182. $row->save($params);
  183. $this->success();
  184. }
  185. $this->error();
  186. }
  187. $row->content = UrlService::instance()->replaceReferralHost($this->getCurrentAccountChannelId(), $row->content, false)->data;
  188. if ($row->type == 'image') {
  189. $row->content = json_decode($row->content, true);
  190. }
  191. $this->view->assign("row", $row);
  192. return $this->view->fetch();
  193. }
  194. }