123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522 |
- <?php
- /**
- * @copyright Copyright (c) 2017 https://www.sapixx.com All rights reserved.
- * @license Licensed (http://www.apache.org/licenses/LICENSE-2.0).
- * @author pillar<ltmn@qq.com>
- * 应答服务
- */
- namespace app\system\controller\passport;
- use app\common\model\SystemMemberKeyword;
- use app\common\facade\WechatMp;
- use app\common\facade\WechatProgram;
- use think\facade\Config;
- use Yurun\Util\HttpRequest;
- use util\Util;
- use Exception;
- class Keyword extends Common{
- public function initialize() {
- parent::initialize();
- if($this->user->lock_config){
- $this->error('你账户锁定配置权限');
- }
- if($this->user->parent_id){
- $this->error('无权限访问,只有创始人身份才允许使用。');
- }
- }
- /**
- * 自动切换
- */
- public function index(){
- if(empty($this->member_miniapp->mp_appid) && empty($this->member_miniapp->miniapp_appid)){
- $this->error('请先授权公众号或小程序',url('passport.setting/index'),'去授权应用');
- }
- if($this->member_miniapp->miniapp->types == 'program' || $this->member_miniapp->miniapp->types == 'mp_program'){
- return redirect('passport.keyword/miniapp');
- }else{
- return redirect('passport.keyword/official');
- }
- }
-
- /**
- * 公众号应答
- */
- public function official(){
- $tabs = [];
- if ($this->member_miniapp->mp_appid){
- $tabs[] = ['name' =>'公众号应答','url' =>url('passport.keyword/official')];
- }
- if ($this->member_miniapp->miniapp_appid){
- $tabs[] = ['name' =>'小程序应答','url' =>url('passport.keyword/miniapp')];
- }
- $this->assign('tabs',$tabs);
- $this->assign('pathMaps', [['name'=>'应答服务','url'=>'javascript::'],['name'=>'公众号应答','url'=>url('passport.keyword/official')]]);
- $this->assign('list',SystemMemberKeyword::where(['member_miniapp_id' => $this->member_miniapp_id,'is_miniapp' => 0])->order('id desc')->paginate(20));
- return view();
- }
- /**
- * 弹出选择公众号
- */
- public function selectOfficial(){
- $view['input'] = $this->request->param('input');
- $view['list'] = SystemMemberKeyword::where(['member_miniapp_id' => $this->member_miniapp_id,'is_miniapp' => 0])->order('id desc')->paginate(20);
- return view()->assign($view);;
- }
- /**
- * 文字应答
- */
- public function text(){
- $id = $this->request->param('id/d',0);
- $condition['member_miniapp_id'] = $this->member_miniapp_id;
- $condition['id'] = $id;
- $info = SystemMemberKeyword::where($condition)->find();
- if(request()->isPost()){
- $data = [
- 'id' => $id,
- 'keyword' => $this->request->param('keyword/s'),
- 'content' => $this->request->param('content/s'),
- ];
- $validate = $this->validate($data,'Keyword.text');
- if(true !== $validate){
- return json(['code'=>0,'msg'=>$validate]);
- }
- if(empty($info)){
- $data['member_miniapp_id'] = $this->member_miniapp_id;
- $data['type'] = 'text';
- $data['is_miniapp'] = 0;
- $result = SystemMemberKeyword::create($data);
- }else{
- $result = SystemMemberKeyword::update($data);
- }
- if($result){
- return enjson(200);
- }else{
- return enjson(0);
- }
- }else{
- $view['info'] = $info;
- return view()->assign($view);;
- }
- }
- /**
- * 图片应答
- */
- public function image(){
- $id = $this->request->param('id/d',0);
- $condition['member_miniapp_id'] = $this->member_miniapp_id;
- $condition['id'] = $id;
- $info = SystemMemberKeyword::where($condition)->find();
- if(request()->isPost()){
- try {
- $data = [
- 'id' => $id,
- 'keyword' => $this->request->param('keyword/s'),
- 'image' => $this->request->param('image/s'),
- ];
- $validate = $this->validate($data,'Keyword.image');
- if(true !== $validate){
- return json(['code'=>0,'msg'=>$validate]);
- }
- $official = WechatMp::isTypes($this->member_miniapp_id);
- if(!$official){
- return enjson(0,'微信认证失败,请确认应用已授权.');
- }
- //上传图片到微信服务器,并返回mediaId
- $config = config::get('upload.');
- $thumb_img = substr(parse_url($data['image'])['path'],1);
- if(empty($info) || $info->image != $data['image']){
- if($config['upload_driver'] == 'oss'){
- $thumb_path = PATH_PUBLIC.$thumb_img;
- if (!file_exists($thumb_path)) {
- if(Util::mkdir(dirname($thumb_path))){
- $http = new HttpRequest;
- $http->download($thumb_path,$data['image']);
- }
- }
- }else{
- $thumb_path = PATH_PUBLIC.$thumb_img;
- }
- if(file_exists($thumb_path)){
- $thumb = $official->material->uploadThumb($thumb_path);
- if(empty($thumb['media_id'])){
- return enjson(0,'上传资源到微信服务器失败');
- }
- $data['media_id'] = $thumb['media_id'];
- $data['media'] = json_encode($thumb);
- }
- }
- if(empty($info)){
- $data['member_miniapp_id'] = $this->member_miniapp_id;
- $data['type'] = 'image';
- $data['is_miniapp'] = 0;
- $result = SystemMemberKeyword::create($data);
- }else{
- $result = SystemMemberKeyword::update($data);
- }
- if($result){
- return enjson(200,'成功',['url' => url('system/passport.keyword/official')]);
- }else{
- return enjson(0);
- }
- } catch (Exception $e) {
- return enjson(0,$e->getMessage());
- }
- }else{
- $view['info'] = $info;
- return view()->assign($view);;
- }
- }
- /**
- * 链接应答
- */
- public function link(){
- $id = $this->request->param('id/d',0);
- $condition['member_miniapp_id'] = $this->member_miniapp_id;
- $condition['id'] = $id;
- $info = SystemMemberKeyword::where($condition)->find();
- if(request()->isPost()){
- try {
- $data = [
- 'id' => $id,
- 'media_id' => $this->request->param('media_id/s'),
- 'keyword' => $this->request->param('keyword/s'),
- 'image' => $this->request->param('image/s'),
- 'title' => $this->request->param('title/s'),
- 'url' => $this->request->param('url/s'),
- 'content' => $this->request->param('content/s'),
- ];
- $validate = $this->validate($data,'Keyword.link');
- if(true !== $validate){
- return json(['code'=>0,'msg'=>$validate]);
- }
- $official = WechatMp::isTypes($this->member_miniapp_id);
- if(!$official){
- return enjson(0,'微信认证失败,请确认应用已授权.');
- }
- //上传图片到微信服务器,并返回mediaId
- $config = config::get('upload.');
- $thumb_img = substr(parse_url($data['image'])['path'],1);
- if(empty($info) || $info->image != $data['image']){
- if($config['upload_driver'] == 'oss'){
- $thumb_path = PATH_PUBLIC.$thumb_img;
- if (!file_exists($thumb_path)) {
- if(Util::mkdir(dirname($thumb_path))){
- $http = new HttpRequest;
- $http->download($thumb_path,$data['image']);
- }
- }
- }else{
- $thumb_path = PATH_PUBLIC.$thumb_img;
- }
- if(file_exists($thumb_path)){
- $thumb = $official->material->uploadThumb($thumb_path);
- if(empty($thumb['media_id'])){
- return enjson(0,'上传资源到微信服务器失败');
- }
- $data['media_id'] = $thumb['media_id'];
- $data['media'] = json_encode($thumb);
- }
- }
- if(empty($info)){
- $data['member_miniapp_id'] = $this->member_miniapp_id;
- $data['type'] = 'link';
- $data['is_miniapp'] = 0;
- $result = SystemMemberKeyword::create($data);
- }else{
- $result = SystemMemberKeyword::update($data);
- }
- if($result){
- return enjson(200,'成功',['url' => url('system/passport.keyword/official')]);
- }else{
- return enjson(0);
- }
- } catch (Exception $e) {
- return enjson(0,$e->getMessage());
- }
- }else{
- $view['info'] = $info;
- return view()->assign($view);;
- }
- }
-
- /**
- * 微信图文素材
- */
- public function media(){
- $id = $this->request->param('id/d',0);
- $condition['member_miniapp_id'] = $this->member_miniapp_id;
- $condition['id'] = $id;
- $info = SystemMemberKeyword::where($condition)->find();
- if(request()->isPost()){
- $data = [
- 'id' => $id,
- 'keyword' => $this->request->param('keyword/s'),
- 'media_id' => $this->request->param('media_id/s'),
- ];
- $validate = $this->validate($data,'Keyword.media');
- if(true !== $validate){
- return json(['code'=>0,'msg'=>$validate]);
- }
- if(empty($info)){
- $data['member_miniapp_id'] = $this->member_miniapp_id;
- $data['type'] = 'media';
- $data['is_miniapp'] = 0;
- $result = SystemMemberKeyword::create($data);
- }else{
- $result = SystemMemberKeyword::update($data);
- }
- if($result){
- return json(['code'=>200,'msg'=>'修改成功','url' => url('system/passport.keyword/official')]);
- }else{
- return json(['code'=>0,'msg'=>'修改失败']);
- }
- }else{
- $view['info'] = $info;
- return view()->assign($view);;
- }
- }
- /**
- * 小程序文字应答
- */
- public function miniappText(){
- $id = $this->request->param('id/d',0);
- $condition['member_miniapp_id'] = $this->member_miniapp_id;
- $condition['id'] = $id;
- $info = SystemMemberKeyword::where($condition)->find();
- if(request()->isPost()){
- $data = [
- 'id' => $id,
- 'keyword' => $this->request->param('keyword/s'),
- 'content' => $this->request->param('content/s'),
- ];
- $validate = $this->validate($data,'Keyword.text');
- if(true !== $validate){
- return json(['code'=>0,'msg'=>$validate]);
- }
- if(empty($info)){
- $data['member_miniapp_id'] = $this->member_miniapp_id;
- $data['type'] = 'text';
- $data['is_miniapp'] = 1;
- $result = SystemMemberKeyword::create($data);
- }else{
- $result = SystemMemberKeyword::update($data);
- }
- if($result){
- return json(['code'=>200,'msg'=>'修改成功','url' => url('system/passport.keyword/miniapp')]);
- }else{
- return json(['code'=>0,'msg'=>'修改失败']);
- }
- }else{
- $view['info'] = $info;
- return view()->assign($view);;
- }
- }
- /**
- * 小程序图片应答
- */
- public function miniappImage(){
- $id = $this->request->param('id/d',0);
- $condition['member_miniapp_id'] = $this->member_miniapp_id;
- $condition['id'] = $id;
- $info = SystemMemberKeyword::where($condition)->find();
- if(request()->isPost()){
- try {
- $data = [
- 'id' => $id,
- 'media_id' => $this->request->param('media_id/s'),
- 'keyword' => $this->request->param('keyword/s'),
- 'image' => $this->request->param('image/s'),
- ];
- $validate = $this->validate($data,'Keyword.image');
- if(true !== $validate){
- return json(['code'=>0,'msg'=>$validate]);
- }
- $program = WechatProgram::isTypes($this->member_miniapp_id);
- if(!$program){
- return enjson(0,'微信认证失败,请确认应用已授权.');
- }
- //上传图片到微信服务器,并返回mediaId
- $config = config::get('upload.');
- $thumb_img = substr(parse_url($data['image'])['path'],1);
- if(empty($info) || $info->image != $data['image']){
- if($config['upload_driver'] == 'oss'){
- $thumb_path = PATH_PUBLIC.$thumb_img;
- if (!file_exists($thumb_path)) {
- if(Util::mkdir(dirname($thumb_path))){
- $http = new HttpRequest;
- $http->download($thumb_path,$data['image']);
- }
- }
- }else{
- $thumb_path = PATH_PUBLIC.$thumb_img;
- }
- if(file_exists($thumb_path)){
- $thumb = $program->media->uploadImage($thumb_path);
- if(empty($thumb['media_id'])){
- return enjson(0,'上传资源到微信服务器失败');
- }
- $data['media_id'] = $thumb['media_id'];
- $data['media'] = json_encode($thumb);
- }
- }
- if(empty($info)){
- $data['member_miniapp_id'] = $this->member_miniapp_id;
- $data['type'] = 'image';
- $data['is_miniapp'] = 1;
- $result = SystemMemberKeyword::create($data);
- }else{
- $result = SystemMemberKeyword::update($data);
- }
- if($result){
- return enjson(200,'成功',['url' => url('system/passport.keyword/miniapp')]);
- }else{
- return enjson(0);
- }
- } catch (Exception $e) {
- return enjson(0,$e->getMessage());
- }
- }else{
- $view['info'] = $info;
- return view()->assign($view);;
- }
- }
- /**
- * 链接应答
- */
- public function miniappLink(){
- $id = $this->request->param('id/d',0);
- $condition['member_miniapp_id'] = $this->member_miniapp_id;
- $condition['id'] = $id;
- $info = SystemMemberKeyword::where($condition)->find();
- if(request()->isPost()){
- try {
- $data = [
- 'id' => $id,
- 'media_id' => $this->request->param('media_id/s'),
- 'keyword' => $this->request->param('keyword/s'),
- 'image' => $this->request->param('image/s'),
- 'title' => $this->request->param('title/s'),
- 'url' => $this->request->param('url/s'),
- 'content' => $this->request->param('content/s'),
- ];
- $validate = $this->validate($data,'Keyword.link');
- if(true !== $validate){
- return json(['code'=>0,'msg'=>$validate]);
- }
- $program = WechatProgram::isTypes($this->member_miniapp_id);
- if(!$program){
- return enjson(0,'微信认证失败,请确认应用已授权.');
- }
- //上传图片到微信服务器,并返回mediaId
- $config = config::get('upload.');
- $thumb_img = substr(parse_url($data['image'])['path'],1);
- if(empty($info) || $info->image != $data['image']){
- if($config['upload_driver'] == 'oss'){
- $thumb_path = PATH_PUBLIC.$thumb_img;
- if (!file_exists($thumb_path)) {
- if(Util::mkdir(dirname($thumb_path))){
- $http = new HttpRequest;
- $http->download($thumb_path,$data['image']);
- }
- }
- }else{
- $thumb_path = PATH_PUBLIC.$thumb_img;
- }
- if(file_exists($thumb_path)){
- $thumb = $program->media->uploadImage($thumb_path);
- if(empty($thumb['media_id'])){
- return enjson(0,'上传资源到微信服务器失败');
- }
- $data['media_id'] = $thumb['media_id'];
- $data['media'] = json_encode($thumb);
- }
- }
- if(empty($info)){
- $data['member_miniapp_id'] = $this->member_miniapp_id;
- $data['type'] = 'link';
- $data['is_miniapp'] = 1;
- $result = SystemMemberKeyword::create($data);
- }else{
- $result = SystemMemberKeyword::update($data);
- }
- if($result){
- return enjson(200,'成功',['url' => url('system/passport.keyword/miniapp')]);
- }else{
- return enjson(0);
- }
- } catch (Exception $e) {
- return enjson(0,$e->getMessage());
- }
- }else{
- $view['info'] = $info;
- return view()->assign($view);;
- }
- }
- /**
- * 小程序
- */
- public function miniapp(){
- $tabs = [];
- if ($this->member_miniapp->mp_appid){
- $tabs[] = ['name' =>'公众号应答','url' =>url('passport.keyword/official')];
- }
- if ($this->member_miniapp->miniapp_appid){
- $tabs[] = ['name' =>'小程序应答','url' =>url('passport.keyword/miniapp')];
- }
- $this->assign('tabs',$tabs);
- $this->assign('pathMaps', [['name'=>'应答服务','url'=>'javascript::'],['name'=>'小程序应答','url'=>url('passport.keyword/miniapp')]]);
- $this->assign('list',SystemMemberKeyword::where(['member_miniapp_id' => $this->member_miniapp_id,'is_miniapp' => 1])->order('id desc')->paginate(20));
- return view();
- }
- /**
- * 弹出小程序
- */
- public function selectMiniapp(){
- $view['input'] = $this->request->param('input');
- $view['list'] = SystemMemberKeyword::where(['member_miniapp_id' => $this->member_miniapp_id,'is_miniapp' => 1])->order('id desc')->paginate(20);
- return view()->assign($view);;
- }
- /**
- * 关键字重复
- * @param integer $id
- * @return void
- */
- public function keyword(){
- $condition[] = ['member_miniapp_id','=',$this->member_miniapp_id];
- $condition[] = ['keyword','=',$this->request->param('param/s')];
- $condition[] = ['id','<>',$this->request->param('id/d',0)];
- $result = SystemMemberKeyword::where($condition)->count();
- if($result){
- return json(['status'=>'n','info'=>'关键字重复']);
- }else{
- return json(['status'=>'y','info'=>'可以使用']);
- }
- }
- /**
- * 删除
- * @param integer $id 删除ID
- */
- public function delete(int $id){
- $condition['member_miniapp_id'] = $this->member_miniapp_id;
- $condition['id'] = $id;
- $result = SystemMemberKeyword::where( $condition)->delete();
- if(!$result){
- return json(['code'=>0,'message'=>'操作失败']);
- }else{
- return json(['code'=>200,'message'=>'操作成功']);
- }
- }
- }
|