Keyword.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 https://www.sapixx.com All rights reserved.
  4. * @license Licensed (http://www.apache.org/licenses/LICENSE-2.0).
  5. * @author pillar<ltmn@qq.com>
  6. * 应答服务
  7. */
  8. namespace app\system\controller\passport;
  9. use app\common\model\SystemMemberKeyword;
  10. use app\common\facade\WechatMp;
  11. use app\common\facade\WechatProgram;
  12. use think\facade\Config;
  13. use Yurun\Util\HttpRequest;
  14. use util\Util;
  15. use Exception;
  16. class Keyword extends Common{
  17. public function initialize() {
  18. parent::initialize();
  19. if($this->user->lock_config){
  20. $this->error('你账户锁定配置权限');
  21. }
  22. if($this->user->parent_id){
  23. $this->error('无权限访问,只有创始人身份才允许使用。');
  24. }
  25. }
  26. /**
  27. * 自动切换
  28. */
  29. public function index(){
  30. if(empty($this->member_miniapp->mp_appid) && empty($this->member_miniapp->miniapp_appid)){
  31. $this->error('请先授权公众号或小程序',url('passport.setting/index'),'去授权应用');
  32. }
  33. if($this->member_miniapp->miniapp->types == 'program' || $this->member_miniapp->miniapp->types == 'mp_program'){
  34. return redirect('passport.keyword/miniapp');
  35. }else{
  36. return redirect('passport.keyword/official');
  37. }
  38. }
  39. /**
  40. * 公众号应答
  41. */
  42. public function official(){
  43. $tabs = [];
  44. if ($this->member_miniapp->mp_appid){
  45. $tabs[] = ['name' =>'公众号应答','url' =>url('passport.keyword/official')];
  46. }
  47. if ($this->member_miniapp->miniapp_appid){
  48. $tabs[] = ['name' =>'小程序应答','url' =>url('passport.keyword/miniapp')];
  49. }
  50. $this->assign('tabs',$tabs);
  51. $this->assign('pathMaps', [['name'=>'应答服务','url'=>'javascript::'],['name'=>'公众号应答','url'=>url('passport.keyword/official')]]);
  52. $this->assign('list',SystemMemberKeyword::where(['member_miniapp_id' => $this->member_miniapp_id,'is_miniapp' => 0])->order('id desc')->paginate(20));
  53. return view();
  54. }
  55. /**
  56. * 弹出选择公众号
  57. */
  58. public function selectOfficial(){
  59. $view['input'] = $this->request->param('input');
  60. $view['list'] = SystemMemberKeyword::where(['member_miniapp_id' => $this->member_miniapp_id,'is_miniapp' => 0])->order('id desc')->paginate(20);
  61. return view()->assign($view);;
  62. }
  63. /**
  64. * 文字应答
  65. */
  66. public function text(){
  67. $id = $this->request->param('id/d',0);
  68. $condition['member_miniapp_id'] = $this->member_miniapp_id;
  69. $condition['id'] = $id;
  70. $info = SystemMemberKeyword::where($condition)->find();
  71. if(request()->isPost()){
  72. $data = [
  73. 'id' => $id,
  74. 'keyword' => $this->request->param('keyword/s'),
  75. 'content' => $this->request->param('content/s'),
  76. ];
  77. $validate = $this->validate($data,'Keyword.text');
  78. if(true !== $validate){
  79. return json(['code'=>0,'msg'=>$validate]);
  80. }
  81. if(empty($info)){
  82. $data['member_miniapp_id'] = $this->member_miniapp_id;
  83. $data['type'] = 'text';
  84. $data['is_miniapp'] = 0;
  85. $result = SystemMemberKeyword::create($data);
  86. }else{
  87. $result = SystemMemberKeyword::update($data);
  88. }
  89. if($result){
  90. return enjson(200);
  91. }else{
  92. return enjson(0);
  93. }
  94. }else{
  95. $view['info'] = $info;
  96. return view()->assign($view);;
  97. }
  98. }
  99. /**
  100. * 图片应答
  101. */
  102. public function image(){
  103. $id = $this->request->param('id/d',0);
  104. $condition['member_miniapp_id'] = $this->member_miniapp_id;
  105. $condition['id'] = $id;
  106. $info = SystemMemberKeyword::where($condition)->find();
  107. if(request()->isPost()){
  108. try {
  109. $data = [
  110. 'id' => $id,
  111. 'keyword' => $this->request->param('keyword/s'),
  112. 'image' => $this->request->param('image/s'),
  113. ];
  114. $validate = $this->validate($data,'Keyword.image');
  115. if(true !== $validate){
  116. return json(['code'=>0,'msg'=>$validate]);
  117. }
  118. $official = WechatMp::isTypes($this->member_miniapp_id);
  119. if(!$official){
  120. return enjson(0,'微信认证失败,请确认应用已授权.');
  121. }
  122. //上传图片到微信服务器,并返回mediaId
  123. $config = config::get('upload.');
  124. $thumb_img = substr(parse_url($data['image'])['path'],1);
  125. if(empty($info) || $info->image != $data['image']){
  126. if($config['upload_driver'] == 'oss'){
  127. $thumb_path = PATH_PUBLIC.$thumb_img;
  128. if (!file_exists($thumb_path)) {
  129. if(Util::mkdir(dirname($thumb_path))){
  130. $http = new HttpRequest;
  131. $http->download($thumb_path,$data['image']);
  132. }
  133. }
  134. }else{
  135. $thumb_path = PATH_PUBLIC.$thumb_img;
  136. }
  137. if(file_exists($thumb_path)){
  138. $thumb = $official->material->uploadThumb($thumb_path);
  139. if(empty($thumb['media_id'])){
  140. return enjson(0,'上传资源到微信服务器失败');
  141. }
  142. $data['media_id'] = $thumb['media_id'];
  143. $data['media'] = json_encode($thumb);
  144. }
  145. }
  146. if(empty($info)){
  147. $data['member_miniapp_id'] = $this->member_miniapp_id;
  148. $data['type'] = 'image';
  149. $data['is_miniapp'] = 0;
  150. $result = SystemMemberKeyword::create($data);
  151. }else{
  152. $result = SystemMemberKeyword::update($data);
  153. }
  154. if($result){
  155. return enjson(200,'成功',['url' => url('system/passport.keyword/official')]);
  156. }else{
  157. return enjson(0);
  158. }
  159. } catch (Exception $e) {
  160. return enjson(0,$e->getMessage());
  161. }
  162. }else{
  163. $view['info'] = $info;
  164. return view()->assign($view);;
  165. }
  166. }
  167. /**
  168. * 链接应答
  169. */
  170. public function link(){
  171. $id = $this->request->param('id/d',0);
  172. $condition['member_miniapp_id'] = $this->member_miniapp_id;
  173. $condition['id'] = $id;
  174. $info = SystemMemberKeyword::where($condition)->find();
  175. if(request()->isPost()){
  176. try {
  177. $data = [
  178. 'id' => $id,
  179. 'media_id' => $this->request->param('media_id/s'),
  180. 'keyword' => $this->request->param('keyword/s'),
  181. 'image' => $this->request->param('image/s'),
  182. 'title' => $this->request->param('title/s'),
  183. 'url' => $this->request->param('url/s'),
  184. 'content' => $this->request->param('content/s'),
  185. ];
  186. $validate = $this->validate($data,'Keyword.link');
  187. if(true !== $validate){
  188. return json(['code'=>0,'msg'=>$validate]);
  189. }
  190. $official = WechatMp::isTypes($this->member_miniapp_id);
  191. if(!$official){
  192. return enjson(0,'微信认证失败,请确认应用已授权.');
  193. }
  194. //上传图片到微信服务器,并返回mediaId
  195. $config = config::get('upload.');
  196. $thumb_img = substr(parse_url($data['image'])['path'],1);
  197. if(empty($info) || $info->image != $data['image']){
  198. if($config['upload_driver'] == 'oss'){
  199. $thumb_path = PATH_PUBLIC.$thumb_img;
  200. if (!file_exists($thumb_path)) {
  201. if(Util::mkdir(dirname($thumb_path))){
  202. $http = new HttpRequest;
  203. $http->download($thumb_path,$data['image']);
  204. }
  205. }
  206. }else{
  207. $thumb_path = PATH_PUBLIC.$thumb_img;
  208. }
  209. if(file_exists($thumb_path)){
  210. $thumb = $official->material->uploadThumb($thumb_path);
  211. if(empty($thumb['media_id'])){
  212. return enjson(0,'上传资源到微信服务器失败');
  213. }
  214. $data['media_id'] = $thumb['media_id'];
  215. $data['media'] = json_encode($thumb);
  216. }
  217. }
  218. if(empty($info)){
  219. $data['member_miniapp_id'] = $this->member_miniapp_id;
  220. $data['type'] = 'link';
  221. $data['is_miniapp'] = 0;
  222. $result = SystemMemberKeyword::create($data);
  223. }else{
  224. $result = SystemMemberKeyword::update($data);
  225. }
  226. if($result){
  227. return enjson(200,'成功',['url' => url('system/passport.keyword/official')]);
  228. }else{
  229. return enjson(0);
  230. }
  231. } catch (Exception $e) {
  232. return enjson(0,$e->getMessage());
  233. }
  234. }else{
  235. $view['info'] = $info;
  236. return view()->assign($view);;
  237. }
  238. }
  239. /**
  240. * 微信图文素材
  241. */
  242. public function media(){
  243. $id = $this->request->param('id/d',0);
  244. $condition['member_miniapp_id'] = $this->member_miniapp_id;
  245. $condition['id'] = $id;
  246. $info = SystemMemberKeyword::where($condition)->find();
  247. if(request()->isPost()){
  248. $data = [
  249. 'id' => $id,
  250. 'keyword' => $this->request->param('keyword/s'),
  251. 'media_id' => $this->request->param('media_id/s'),
  252. ];
  253. $validate = $this->validate($data,'Keyword.media');
  254. if(true !== $validate){
  255. return json(['code'=>0,'msg'=>$validate]);
  256. }
  257. if(empty($info)){
  258. $data['member_miniapp_id'] = $this->member_miniapp_id;
  259. $data['type'] = 'media';
  260. $data['is_miniapp'] = 0;
  261. $result = SystemMemberKeyword::create($data);
  262. }else{
  263. $result = SystemMemberKeyword::update($data);
  264. }
  265. if($result){
  266. return json(['code'=>200,'msg'=>'修改成功','url' => url('system/passport.keyword/official')]);
  267. }else{
  268. return json(['code'=>0,'msg'=>'修改失败']);
  269. }
  270. }else{
  271. $view['info'] = $info;
  272. return view()->assign($view);;
  273. }
  274. }
  275. /**
  276. * 小程序文字应答
  277. */
  278. public function miniappText(){
  279. $id = $this->request->param('id/d',0);
  280. $condition['member_miniapp_id'] = $this->member_miniapp_id;
  281. $condition['id'] = $id;
  282. $info = SystemMemberKeyword::where($condition)->find();
  283. if(request()->isPost()){
  284. $data = [
  285. 'id' => $id,
  286. 'keyword' => $this->request->param('keyword/s'),
  287. 'content' => $this->request->param('content/s'),
  288. ];
  289. $validate = $this->validate($data,'Keyword.text');
  290. if(true !== $validate){
  291. return json(['code'=>0,'msg'=>$validate]);
  292. }
  293. if(empty($info)){
  294. $data['member_miniapp_id'] = $this->member_miniapp_id;
  295. $data['type'] = 'text';
  296. $data['is_miniapp'] = 1;
  297. $result = SystemMemberKeyword::create($data);
  298. }else{
  299. $result = SystemMemberKeyword::update($data);
  300. }
  301. if($result){
  302. return json(['code'=>200,'msg'=>'修改成功','url' => url('system/passport.keyword/miniapp')]);
  303. }else{
  304. return json(['code'=>0,'msg'=>'修改失败']);
  305. }
  306. }else{
  307. $view['info'] = $info;
  308. return view()->assign($view);;
  309. }
  310. }
  311. /**
  312. * 小程序图片应答
  313. */
  314. public function miniappImage(){
  315. $id = $this->request->param('id/d',0);
  316. $condition['member_miniapp_id'] = $this->member_miniapp_id;
  317. $condition['id'] = $id;
  318. $info = SystemMemberKeyword::where($condition)->find();
  319. if(request()->isPost()){
  320. try {
  321. $data = [
  322. 'id' => $id,
  323. 'media_id' => $this->request->param('media_id/s'),
  324. 'keyword' => $this->request->param('keyword/s'),
  325. 'image' => $this->request->param('image/s'),
  326. ];
  327. $validate = $this->validate($data,'Keyword.image');
  328. if(true !== $validate){
  329. return json(['code'=>0,'msg'=>$validate]);
  330. }
  331. $program = WechatProgram::isTypes($this->member_miniapp_id);
  332. if(!$program){
  333. return enjson(0,'微信认证失败,请确认应用已授权.');
  334. }
  335. //上传图片到微信服务器,并返回mediaId
  336. $config = config::get('upload.');
  337. $thumb_img = substr(parse_url($data['image'])['path'],1);
  338. if(empty($info) || $info->image != $data['image']){
  339. if($config['upload_driver'] == 'oss'){
  340. $thumb_path = PATH_PUBLIC.$thumb_img;
  341. if (!file_exists($thumb_path)) {
  342. if(Util::mkdir(dirname($thumb_path))){
  343. $http = new HttpRequest;
  344. $http->download($thumb_path,$data['image']);
  345. }
  346. }
  347. }else{
  348. $thumb_path = PATH_PUBLIC.$thumb_img;
  349. }
  350. if(file_exists($thumb_path)){
  351. $thumb = $program->media->uploadImage($thumb_path);
  352. if(empty($thumb['media_id'])){
  353. return enjson(0,'上传资源到微信服务器失败');
  354. }
  355. $data['media_id'] = $thumb['media_id'];
  356. $data['media'] = json_encode($thumb);
  357. }
  358. }
  359. if(empty($info)){
  360. $data['member_miniapp_id'] = $this->member_miniapp_id;
  361. $data['type'] = 'image';
  362. $data['is_miniapp'] = 1;
  363. $result = SystemMemberKeyword::create($data);
  364. }else{
  365. $result = SystemMemberKeyword::update($data);
  366. }
  367. if($result){
  368. return enjson(200,'成功',['url' => url('system/passport.keyword/miniapp')]);
  369. }else{
  370. return enjson(0);
  371. }
  372. } catch (Exception $e) {
  373. return enjson(0,$e->getMessage());
  374. }
  375. }else{
  376. $view['info'] = $info;
  377. return view()->assign($view);;
  378. }
  379. }
  380. /**
  381. * 链接应答
  382. */
  383. public function miniappLink(){
  384. $id = $this->request->param('id/d',0);
  385. $condition['member_miniapp_id'] = $this->member_miniapp_id;
  386. $condition['id'] = $id;
  387. $info = SystemMemberKeyword::where($condition)->find();
  388. if(request()->isPost()){
  389. try {
  390. $data = [
  391. 'id' => $id,
  392. 'media_id' => $this->request->param('media_id/s'),
  393. 'keyword' => $this->request->param('keyword/s'),
  394. 'image' => $this->request->param('image/s'),
  395. 'title' => $this->request->param('title/s'),
  396. 'url' => $this->request->param('url/s'),
  397. 'content' => $this->request->param('content/s'),
  398. ];
  399. $validate = $this->validate($data,'Keyword.link');
  400. if(true !== $validate){
  401. return json(['code'=>0,'msg'=>$validate]);
  402. }
  403. $program = WechatProgram::isTypes($this->member_miniapp_id);
  404. if(!$program){
  405. return enjson(0,'微信认证失败,请确认应用已授权.');
  406. }
  407. //上传图片到微信服务器,并返回mediaId
  408. $config = config::get('upload.');
  409. $thumb_img = substr(parse_url($data['image'])['path'],1);
  410. if(empty($info) || $info->image != $data['image']){
  411. if($config['upload_driver'] == 'oss'){
  412. $thumb_path = PATH_PUBLIC.$thumb_img;
  413. if (!file_exists($thumb_path)) {
  414. if(Util::mkdir(dirname($thumb_path))){
  415. $http = new HttpRequest;
  416. $http->download($thumb_path,$data['image']);
  417. }
  418. }
  419. }else{
  420. $thumb_path = PATH_PUBLIC.$thumb_img;
  421. }
  422. if(file_exists($thumb_path)){
  423. $thumb = $program->media->uploadImage($thumb_path);
  424. if(empty($thumb['media_id'])){
  425. return enjson(0,'上传资源到微信服务器失败');
  426. }
  427. $data['media_id'] = $thumb['media_id'];
  428. $data['media'] = json_encode($thumb);
  429. }
  430. }
  431. if(empty($info)){
  432. $data['member_miniapp_id'] = $this->member_miniapp_id;
  433. $data['type'] = 'link';
  434. $data['is_miniapp'] = 1;
  435. $result = SystemMemberKeyword::create($data);
  436. }else{
  437. $result = SystemMemberKeyword::update($data);
  438. }
  439. if($result){
  440. return enjson(200,'成功',['url' => url('system/passport.keyword/miniapp')]);
  441. }else{
  442. return enjson(0);
  443. }
  444. } catch (Exception $e) {
  445. return enjson(0,$e->getMessage());
  446. }
  447. }else{
  448. $view['info'] = $info;
  449. return view()->assign($view);;
  450. }
  451. }
  452. /**
  453. * 小程序
  454. */
  455. public function miniapp(){
  456. $tabs = [];
  457. if ($this->member_miniapp->mp_appid){
  458. $tabs[] = ['name' =>'公众号应答','url' =>url('passport.keyword/official')];
  459. }
  460. if ($this->member_miniapp->miniapp_appid){
  461. $tabs[] = ['name' =>'小程序应答','url' =>url('passport.keyword/miniapp')];
  462. }
  463. $this->assign('tabs',$tabs);
  464. $this->assign('pathMaps', [['name'=>'应答服务','url'=>'javascript::'],['name'=>'小程序应答','url'=>url('passport.keyword/miniapp')]]);
  465. $this->assign('list',SystemMemberKeyword::where(['member_miniapp_id' => $this->member_miniapp_id,'is_miniapp' => 1])->order('id desc')->paginate(20));
  466. return view();
  467. }
  468. /**
  469. * 弹出小程序
  470. */
  471. public function selectMiniapp(){
  472. $view['input'] = $this->request->param('input');
  473. $view['list'] = SystemMemberKeyword::where(['member_miniapp_id' => $this->member_miniapp_id,'is_miniapp' => 1])->order('id desc')->paginate(20);
  474. return view()->assign($view);;
  475. }
  476. /**
  477. * 关键字重复
  478. * @param integer $id
  479. * @return void
  480. */
  481. public function keyword(){
  482. $condition[] = ['member_miniapp_id','=',$this->member_miniapp_id];
  483. $condition[] = ['keyword','=',$this->request->param('param/s')];
  484. $condition[] = ['id','<>',$this->request->param('id/d',0)];
  485. $result = SystemMemberKeyword::where($condition)->count();
  486. if($result){
  487. return json(['status'=>'n','info'=>'关键字重复']);
  488. }else{
  489. return json(['status'=>'y','info'=>'可以使用']);
  490. }
  491. }
  492. /**
  493. * 删除
  494. * @param integer $id 删除ID
  495. */
  496. public function delete(int $id){
  497. $condition['member_miniapp_id'] = $this->member_miniapp_id;
  498. $condition['id'] = $id;
  499. $result = SystemMemberKeyword::where( $condition)->delete();
  500. if(!$result){
  501. return json(['code'=>0,'message'=>'操作失败']);
  502. }else{
  503. return json(['code'=>200,'message'=>'操作成功']);
  504. }
  505. }
  506. }