123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213 |
- <?php
- namespace app\admin\controller\wechat;
- use app\common\controller\Backend;
- use addons\wechat\library\Wechat;
- use app\common\library\WeChatObject;
- use app\common\service\LogService;
- use app\main\service\UrlService;
- use think\Session;
- use EasyWeChat\Factory;
- use app\common\library\Redis;
- use Symfony\Component\Cache\Simple\RedisCache;
- /**
- * 资源管理
- *
- * @icon fa fa-list-alt
- */
- class Response extends Backend
- {
- protected $model = null;
- protected $searchFields = 'id,title';
- protected $dataLimit = 'personal';
- public function _initialize()
- {
- parent::_initialize();
- $this->model = model('WechatResponse');
- }
- /**
- * 查看
- */
- public function index()
- {
- //设置过滤方法
- $this->request->filter(['strip_tags']);
- if ($this->request->isAjax())
- {
- //如果发送的来源是Selectpage,则转发到Selectpage
- if ($this->request->request('pkey_name'))
- {
- return $this->selectpage();
- }
- if($this->request->get("filter1")){
- if($this->request->get('filter')){
- $filter1 = json_decode($this->request->get("filter1"),true);
- $filter = json_decode($this->request->get("filter"),true);
- $this->request->get(["filter"=>json_encode(array_merge($filter,$filter1))]);
- }else{
- $this->request->get(["filter"=> $this->request->get("filter1")]);
- }
- }
- if($this->request->get("op1")){
- if($this->request->get("op")){
- $op1 = json_decode($this->request->get("op1"),true);
- $op = json_decode($this->request->get("op"),true);
- $this->request->get(["op"=>json_encode(array_merge($op,$op1))]);
- }else{
- $this->request->get(["op"=> $this->request->get("op1")]);
- }
- }
- list($where, $sort, $order, $offset, $limit) = $this->buildparams();
- $total = $this->model
- ->where($where)
- ->order($sort, $order)
- ->count();
- $list = $this->model
- ->where($where)
- ->order($sort, $order)
- ->limit($offset, $limit)
- ->select();
- $result = array("total" => $total, "rows" => $list);
- return json($result);
- }
- if($this->auth->agent_id || $this->group == 3){
- if(!model('AdminConfig')->checkWechatConfig($this->auth->id)){
- $this->error('请先授权微信服务号给本系统,正在跳转授权设置页面~', url('admin/config'));
- }
- }
- return $this->view->fetch();
- }
- /**
- * 选择素材
- */
- public function select()
- {
- return $this->view->fetch();
- }
- /**
- * 添加
- */
- public function add()
- {
- $is_subscribe = 0;
- if($this->request->get("filter1")){
- $is_subscribeAr = json_decode($this->request->get("filter1"),true);
- if(is_array($is_subscribeAr)&&!empty($is_subscribeAr)){
- $is_subscribe = ($is_subscribeAr['is_subscribe']==0) ? 1 : 0;
- }
- }
- $this->assign('is_subscribe',$is_subscribe);
- if ($this->request->isPost())
- {
- $params = $this->request->post("row/a");
- $params['admin_id'] = $this->auth->id;
- if($params['is_subscribe'] == 0){
- $conArr = json_decode($params['content'],true);
- if(is_array($conArr)&&!empty($conArr)){
- $params['content'] = json_encode(array($conArr[0]));
- }
- }
- $params['content'] = UrlService::instance()->replaceReferralHost($this->getCurrentAccountChannelId(), $params['content'])->data;
- $params['eventkey'] = isset($params['eventkey']) && $params['eventkey'] ? $params['eventkey'] : $this->auth->id . '_' . uniqid();
- switch ($params['type']) {
- case 'text': //文本
- break;
- case 'image': //图片
- $row = model('AdminConfig')->getAdminInfoAll($this->auth->id);
- $wechat = new WeChatObject($row);
- $officialAccount = $wechat->getOfficialAccount();
- $upload_url = str_replace(config('site.cdnurl'),'',ROOT_PATH . 'public' . $params['content']);
- $result = $officialAccount->material->uploadImage($upload_url);
- if (!isset($result['media_id'])) {
- $this->error('图片素材上传微信失败!');
- }
- $result['url'] = $params['content']; //保存图片原始上传相对url
- $params['content'] = json_encode($result);
- break;
- case 'news': //图文
- break;
- }
- if ($params)
- {
- $this->model->save($params);
- $this->success('',null, $params);
- $this->content = $params;
- }
- $this->error();
- }
- return $this->view->fetch();
- }
- /**
- * 编辑
- */
- public function edit($ids = NULL)
- {
- $row = $this->model->get($ids);
- if (!$row)
- $this->error(__('No Results were found'));
- if ($this->request->isPost())
- {
- $params = $this->request->post("row/a");
- $params['eventkey'] = isset($params['eventkey']) && $params['eventkey'] ? $params['eventkey'] : $this->auth->id . '_' . uniqid();
- if($params['is_subscribe'] == 0){
- $conArr = json_decode($params['content'],true);
- if(is_array($conArr)&&!empty($conArr)){
- $params['content'] = json_encode(array($conArr[0]));
- }
- }
- $params['content'] = UrlService::instance()->replaceReferralHost($this->getCurrentAccountChannelId(), $params['content'])->data;
- switch ($params['type']) {
- case 'text': //文本
- break;
- case 'image': //图片
- $content = json_decode($row->content, true);
- if ($content['url'] != $params['content']) {
- $adminConfig = model('AdminConfig')->getAdminInfoAll($this->auth->id);
- $wechat = new WeChatObject($adminConfig);
- $officialAccount = $wechat->getOfficialAccount();
- $result = $officialAccount->media->uploadImage(ROOT_PATH . 'public' . $params['content']);
- if (!isset($result['media_id'])) {
- $this->error('图片素材上传微信失败!');
- }
- $result['url'] = $params['content']; //保存图片原始上传相对url
- $params['content'] = json_encode($result);
- } else {
- unset($params['content']);
- }
- break;
- case 'news': //图文
- break;
- }
- if ($params)
- {
- $row->save($params);
- $this->success();
- }
- $this->error();
- }
- $row->content = UrlService::instance()->replaceReferralHost($this->getCurrentAccountChannelId(), $row->content, false)->data;
- if ($row->type == 'image') {
- $row->content = json_decode($row->content, true);
- }
- $this->view->assign("row", $row);
- return $this->view->fetch();
- }
- }
|