Wechat.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | A3Mall
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2020 http://www.a3-mall.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Author: xzncit <158373108@qq.com>
  8. // +----------------------------------------------------------------------
  9. namespace app\admin\service\common;
  10. use app\admin\service\Service;
  11. use app\admin\model\wechat\WechatNews as WechatNewsModel;
  12. use app\common\models\wechat\WechatNewsArticle as WechatNewsArticleModel;
  13. class Wechat extends Service {
  14. /**
  15. * 按公众号文章类型获取内容
  16. * @param $data
  17. * @return array
  18. * @throws \think\db\exception\DataNotFoundException
  19. * @throws \think\db\exception\DbException
  20. * @throws \think\db\exception\ModelNotFoundException
  21. */
  22. public static function getContent($data){
  23. $type = $data["type"]??"text";
  24. $content = $data["content"]??"";
  25. $news = [];
  26. if($type == "image"){
  27. $content = file_exists(ltrim($content,"/")) ? $content : "/static/images/default.jpg";
  28. }else if($type == "news"){
  29. $news = WechatNewsModel::where(["id"=>$content])->find();
  30. if(!empty($news["article_id"])){
  31. $news["article"] = WechatNewsArticleModel::where("id","in",$news["article_id"])
  32. ->orderRaw('find_in_set(id,"'.$news["article_id"].'")')
  33. ->select()->toArray();
  34. }
  35. }
  36. return [ "content"=>$content, "news"=>$news ];
  37. }
  38. /**
  39. * 获取文章列表
  40. * @param $data
  41. * @return array
  42. */
  43. public static function getArticle($data){
  44. $count = WechatUsersModel::withSearch(["title"],[ 'title'=>$data['key']["title"]??'' ])->count();
  45. $result = WechatUsersModel::withSearch(["title"],[ 'title'=>$data['key']["title"]??'' ])->order("id","desc")->page($data["page"]??1,$data["limit"]??10)->select()->toArray();
  46. foreach($result as $key=>$item){
  47. $article = WechatNewsArticleModel::where("id","in",$item["article_id"])->orderRaw('find_in_set(id,"'.$item["article_id"].'")')->find();
  48. $list[$key]["title"] = $article["title"];
  49. $list[$key]["local_url"] = $article["local_url"];
  50. }
  51. return ["count"=>$count, "data"=>$result];
  52. }
  53. }