作者 郭文星

'v1'

  1 +<?php
  2 +/**
  3 + * Created by PhpStorm.
  4 + * User: ty01
  5 + * Date: 2022/3/10
  6 + * Time: 14:31
  7 + */
  8 +
  9 +namespace app\api\controller\v1;
  10 +
  11 +use think\Db;
  12 +
  13 +/**
  14 + * 文章管理
  15 + * Class Acquisition
  16 + * @package app\api\controller\core
  17 + */
  18 +class Acquisition extends Base
  19 +{
  20 +
  21 + /**
  22 + * 文章采集入库
  23 + */
  24 + public function article()
  25 + {
  26 + $response = $this->request->param();
  27 +
  28 + $title = $response['title'];//文章标题
  29 + $articlecontent = $response['content'];
  30 + $url = $response['url'];
  31 + $from = $response['source'];
  32 + $cjtime = $response['tacq'];
  33 + $fwtime = $response['DispatchTime'];
  34 + $Sensitive = $response['Sensitive'];
  35 + $group_id = $response['group_id'];
  36 + $fakeid = $response['fakeid'];//公众号id
  37 +
  38 + $ss = [];
  39 + $ids_string = "";
  40 + if (!empty($Sensitive)) {
  41 + foreach ($Sensitive as $kk=>$vv){
  42 + if (!empty($vv)) {
  43 + $ids = Db::name("theme_keywords")->where("name", $vv)->column("id");
  44 + if (!empty($ids)) {
  45 + $ss = array_unique(array_merge($ss, $ids));
  46 + }
  47 + }
  48 + }
  49 + if (!empty($ss)) {
  50 + $ids_string = implode(",", $ss);
  51 + }
  52 + $Sensitive = implode(",", $Sensitive);
  53 + }
  54 +
  55 + if (empty($title)) {
  56 + $this->error("文章标题不能为空");
  57 + }
  58 +
  59 + if (empty($articlecontent)) {
  60 + $this->error("文章内容不能为空");
  61 + }
  62 +
  63 + if (empty($from) || !in_array($from, ['微信', '网站', '论坛', '全部网页', '网络问政', '头条号', '广播电视', '搜狐号', '微博', '视频'])) {
  64 + $this->error("文章来源渠道参数异常");
  65 + }
  66 +
  67 + $admin_id = Db::name("auth_group_access")->where("group_id", $group_id)->value("uid");//获取角色组下第一层的某个管理id
  68 +
  69 + $data = [
  70 + "title" => $title,
  71 + "articlecontent" => $articlecontent,
  72 + "url" => $url,
  73 + "from" => $from,
  74 + "cjtime" => $cjtime,
  75 + "fwtime" => $fwtime,
  76 + "createtime" => time(),
  77 + "sensitive" => $Sensitive,
  78 + "theme_keywords_ids"=>$ids_string,
  79 + "admin_id" => $admin_id ? $admin_id : 1,
  80 + "fakeid"=>$fakeid,
  81 + ];
  82 +
  83 + $ac_id = Db::name("article_acquisition")->insertGetId($data);
  84 + if ($ac_id) {
  85 + $this->success("数据入库成功", $ac_id);
  86 + } else {
  87 + $this->error("数据入库失败");
  88 + }
  89 +
  90 + }
  91 +
  92 + /**
  93 + * 处理敏感词老数据(多个id用逗号隔开)
  94 + */
  95 + public function test()
  96 + {
  97 + $article = Db::name("article_acquisition")->order("id asc")->select();
  98 + if (!empty($article)) {
  99 + foreach ($article as $k => $v) {
  100 +
  101 + if (!empty($v['sensitive'])) {
  102 + $sensitive_array = explode(",", $v['sensitive']);
  103 + $ss = [];
  104 + foreach ($sensitive_array as $kk => $vv) {
  105 + if (!empty($vv)) {
  106 + $ids = Db::name("theme_keywords")->where("name", $vv)->column("id");
  107 + if (!empty($ids)) {
  108 + $ss = array_unique(array_merge($ss, $ids));
  109 + }
  110 + }
  111 + }
  112 + if (!empty($ss)) {
  113 + $ids_string = "";
  114 + $ids_string = implode(",", $ss);
  115 + Db::name("article_acquisition")->where("id", $v['id'])->update(["theme_keywords_ids" => $ids_string]);
  116 + }
  117 +
  118 + }
  119 +
  120 + }
  121 + }
  122 +
  123 + }
  124 +}
  1 +<?php
  2 +
  3 +namespace app\api\controller\v1;
  4 +
  5 +use app\common\controller\Api;
  6 +use think\Request;
  7 +
  8 +header('Content-type:text/html; Charset=utf-8');
  9 +date_default_timezone_set('PRC');
  10 +
  11 +class Base extends Api
  12 +{
  13 + protected $noNeedLogin = ['*'];//*无需验证登陆
  14 + protected $AppID;
  15 + protected $AppSecret;
  16 + protected $qiniuUrl;
  17 +
  18 + public function __construct(Request $request = null)
  19 + {
  20 + parent::__construct($request);
  21 + $this->qiniuUrl = 'https://coupon.xp.yn.cn';
  22 + $this->AppID = config("site.appid");//微信小程序AppID
  23 + $this->AppSecret = config("site.appsecret");//微信小程序密钥
  24 + }
  25 +
  26 +}
  1 +<?php
  2 +/**
  3 + * Created by PhpStorm.
  4 + * User: ty01
  5 + * Date: 2022/3/10
  6 + * Time: 14:31
  7 + */
  8 +
  9 +namespace app\api\controller\v1;
  10 +
  11 +use app\admin\model\collection\FocusCollection;
  12 +use app\common\helper\HttpHelper;
  13 +use think\Db;
  14 +use think\Request;
  15 +
  16 +/**
  17 + * 角色组
  18 + * Class Group
  19 + * @package app\api\controller\core
  20 + */
  21 +class Group extends Base
  22 +{
  23 +
  24 + /**
  25 + * 1.管理组及其检索关键词
  26 + */
  27 + public function list()
  28 + {
  29 + $where['searchword'] = ['neq', ''];
  30 + $where['id'] = ['gt', 1];
  31 + $result = Db::name("auth_group")->where($where)->field("id as group_id,searchword")->select();
  32 + if (!empty($result)) {
  33 + foreach ($result as $k => $v) {
  34 +
  35 + //所有微信公众号
  36 + $w = [];
  37 + $w = [
  38 + "from" => "微信",
  39 + "auth_group_id" => $v['group_id']
  40 + ];
  41 + $collection = Db::name("focus_collection")->where($w)->column("unikey");
  42 +// if (empty($collection)) {
  43 +// unset($result[$k]);
  44 +// continue;
  45 +// }
  46 + //获取角色组下所有管理员发布的敏感词返回数组
  47 + $uids = Db::name("auth_group_access")->where("group_id", $v['group_id'])->column("uid");
  48 + if (empty($uids)) {
  49 + unset($result[$k]);
  50 + continue;
  51 + }
  52 + $keywords = Db::name("theme_keywords")->where('admin_id', 'in', $uids)->column("name as keywords_name");
  53 + if (empty($keywords)) {
  54 + unset($result[$k]);
  55 + continue;
  56 + }
  57 +
  58 + $result[$k]['collection'] = $collection;
  59 + $result[$k]['keywords'] = $keywords;
  60 + }
  61 + }
  62 + $this->success("数据获取成功", $result);
  63 + }
  64 +
  65 + /**
  66 + * 2.获取管理组、检索关键词、最新begin数据
  67 + */
  68 + public function group_list()
  69 + {
  70 + $where['searchword'] = ['neq', ''];
  71 + $where['id'] = ['gt', 1];
  72 + $result = Db::name("auth_group")->where($where)->field("id as group_id,searchword")->select();
  73 + if (!empty($result)) {
  74 + foreach ($result as $k => $v) {
  75 +
  76 + //所有微信公众号
  77 + $w = [];
  78 + $w = [
  79 + "from" => "微信",
  80 + "auth_group_id" => $v['group_id']
  81 + ];
  82 + $begin = Db::name("focus_collection")->where($w)->order("begin desc")->value("begin");
  83 + $result[$k]['begin'] = $begin ? $begin : 0;
  84 + }
  85 + }
  86 + $this->success("数据获取成功!", $result);
  87 + }
  88 +
  89 + /**
  90 + * 2. 一键拉取对应角色组检索关键词的公众号
  91 + * @param group_id 角色组id
  92 + * @param begin 最新一篇文章的begin值;
  93 + */
  94 + public function pullFocusCollection()
  95 + {
  96 + echo "kevin";
  97 + die;
  98 + $response = $this->request->param();
  99 + if (empty($response['group_id'])) {
  100 + return array_callback(false, "角色组id不能为空");
  101 + }
  102 +
  103 + if (empty($response['key'])) {
  104 + return array_callback(false, "检索关键词不能为空");
  105 + }
  106 +
  107 + $params["group_id"] = $response['group_id'];
  108 + $params["key"] = $response['key'];
  109 + $params["begin"] = $response['begin'] > 0 ? intval($response['begin']) : 0;
  110 +
  111 + $query_string = is_array($params) ? http_build_query($params) : $params;
  112 +// $request_url = "http://op.cn/api/v1/group/test";
  113 + $request_url = "http://124.71.110.50:8080/getpublic";
  114 + $geturl = $query_string ? $request_url . (stripos($request_url, "?") !== false ? "&" : "?") . $query_string : $request_url;
  115 + $s = file_get_contents($geturl);
  116 + $res = json_decode($s, true);
  117 +
  118 +// $res = json_decode($res, true);
  119 +
  120 + $time = time();
  121 + $insert_data = [];
  122 +
  123 + //日志入库
  124 + $log = [
  125 + "auth_group_id" => $response['group_id'],
  126 + "request_url" => $request_url,
  127 + "request_method" => "GET",
  128 + "request_param" => json_encode($params, JSON_UNESCAPED_UNICODE),
  129 + "response_code" => $res['code'],
  130 + "createtime" => $time
  131 + ];
  132 +
  133 + if (!empty($res['data'])) {
  134 + foreach ($res['data'] as $k => $v) {
  135 + $from = "微信";
  136 + $admin_id = Db::name("auth_group_access")->where("group_id", $v['group_id'])->value("uid");
  137 + //可以优化成查询最新begin做对比
  138 + $count = Db::name("focus_collection")->where("unikey", $v['id'])->count();
  139 + if ($count == 0) {
  140 + $insert_data[] = [
  141 + "from" => $from,
  142 + "unikey" => $v['id'],
  143 + "name" => $v['name'],
  144 + "pinyin" => $v['alias'],
  145 + "headavatar" => $v['round_head_img'],
  146 + "service_type" => $v['service_type'] ? $v['service_type'] : 1,
  147 + "signature" => $v['signature'],
  148 + "begin" => $v['begin'],
  149 + "createtime" => $time,
  150 + "updatetime" => $time,
  151 + "admin_id" => $admin_id ? $admin_id : 1,
  152 + "auth_group_id" => $v['group_id']
  153 + ];
  154 + }
  155 + }
  156 + if (!empty($insert_data)) {
  157 + // 启动事务
  158 + Db::startTrans();
  159 + try {
  160 + $res = Db::name("focus_collection")->insertAll($insert_data);
  161 + if (!$res) {
  162 + Db::rollback();
  163 + return false;
  164 + }
  165 + // 执行提交操作
  166 + Db::commit();
  167 + } catch (\Exception $e) {
  168 + // 更新失败 回滚事务
  169 + Db::rollback();
  170 + Db::name("focus_collection_log")->insert($log);
  171 + $this->error("数据更新失败!" . $e->getMessage());
  172 + }
  173 + $log["upt_num"] = $res;
  174 + Db::name("focus_collection_log")->insert($log);
  175 + $this->success("数据更新成功【" . $res . "条】");
  176 + }
  177 +
  178 + }
  179 + Db::name("focus_collection_log")->insert($log);
  180 + $this->success("暂无需要更新的数据");
  181 + }
  182 +
  183 + public function test()
  184 + {
  185 + $data = [
  186 + [
  187 + "id" => "MzA5MDQxNjgyNw==",
  188 + "name" => "今日蒙自",
  189 + "alias" => "jrmzzx",
  190 + "round_head_img" => "https://open.hikvision.com/img/a985847.png",
  191 + "service_type" => 1,
  192 + "signature" => "发布蒙自人文、旅游、民生、时政最新资讯,关注大事、小事、身边事",
  193 + "begin" => 1,
  194 + "group_id" => 4
  195 + ],
  196 + [
  197 + "id" => "MzI3Nzc5NtcwMg==",
  198 + "name" => "蒙自招聘",
  199 + "alias" => "jrmzzx",
  200 + "round_head_img" => "https://open.hikvision.com/img/a985847.png",
  201 + "service_type" => 1,
  202 + "signature" => "发布蒙自人文、旅游、民生、时政最新资讯,关注大事、小事、身边事",
  203 + "begin" => 2,
  204 + "group_id" => 4
  205 + ],
  206 +// [
  207 +// "id" => "MzAwNzI2Njk4Mg==",
  208 +// "name" => "锡都个旧",
  209 +// "alias" => "gjsrmtzx",
  210 +// "round_head_img" => "https://open.hikvision.com/img/a985847.png",
  211 +// "service_type" => 1,
  212 +// "signature" => "发布个旧旅游、民生、时政最新资讯,关注大事、小事、身边事",
  213 +// "begin" => 1,
  214 +// "group_id" => 5
  215 +// ],
  216 +// [
  217 +// "id" => "MzAxNTMwOTE1Mg==",
  218 +// "name" => "早安个旧",
  219 +// "alias" => "zaoangejiu",
  220 +// "round_head_img" => "https://open.hikvision.com/img/a985847.png",
  221 +// "service_type" => 1,
  222 +// "signature" => "发布个旧民生、时政最新资讯,关注大事、小事、身边事",
  223 +// "begin" => 2,
  224 +// "group_id" => 5
  225 +// ],
  226 + ];
  227 + $this->success("成功", $data);
  228 + }
  229 +
  230 + /**
  231 + * 4. 公众号批量入库
  232 + */
  233 + public function addFocusCollection()
  234 + {
  235 + $request = Request::instance();
  236 +
  237 + $response = $this->request->param();
  238 + if (!is_array($response)) {
  239 + $this->error("请求数据格式异常");
  240 + }
  241 + $time = time();
  242 + $insert_data = [];
  243 +
  244 + if (!empty($response)) {
  245 + foreach ($response as $k => $v) {
  246 + $from = "微信";
  247 + $admin_id = Db::name("auth_group_access")->where("group_id", $v['group_id'])->value("uid");//获取角色组下第一层的某个管理id
  248 + $count = Db::name("focus_collection")->where("unikey", $v['id'])->count();//检查unikey是否存在
  249 + if ($count == 0) {
  250 + //没入过库
  251 + $insert_data[] = [
  252 + "from" => $from,
  253 + "unikey" => $v['id'],
  254 + "name" => $v['name'],
  255 + "pinyin" => $v['alias'],
  256 + "headavatar" => $v['round_head_img'],
  257 + "service_type" => $v['service_type'] ? $v['service_type'] : 1,
  258 + "signature" => $v['signature'],
  259 + "begin" => $v['begin'],
  260 + "createtime" => $time,
  261 + "updatetime" => $time,
  262 + "admin_id" => $admin_id ? $admin_id : 1,
  263 + "auth_group_id" => $v['group_id'],
  264 + "account_principal"=>$v['themainbody'] //微信号账号主体
  265 + ];
  266 + }
  267 + }
  268 + if (!empty($insert_data)) {
  269 + // 启动事务
  270 + Db::startTrans();
  271 + try {
  272 + $res = Db::name("focus_collection")->insertAll($insert_data);
  273 + if (!$res) {
  274 + Db::rollback();
  275 + return false;
  276 + }
  277 + // 执行提交操作
  278 + Db::commit();
  279 + } catch (\Exception $e) {
  280 + // 更新失败 回滚事务
  281 + Db::rollback();
  282 + //日志入库
  283 + $log = [];
  284 + $log = [
  285 + "auth_group_id" => $response[0]['group_id'],
  286 + "request_url" => $request->url(true),
  287 + "request_method" => $_SERVER['REQUEST_METHOD'],
  288 + "request_ip" => $request->ip(),
  289 + "request_param" => json_encode($response, JSON_UNESCAPED_UNICODE),
  290 + "response_code" => 0,
  291 + "upt_num" => 0,
  292 + "createtime" => $time
  293 + ];
  294 + Db::name("focus_collection_log")->insert($log);
  295 + $this->error("数据更新失败!" . $e->getMessage());
  296 + }
  297 +
  298 + $log = [];
  299 + $log = [
  300 + "auth_group_id" => $response[0]['group_id'],
  301 + "request_url" => $request->url(true),
  302 + "request_method" => $_SERVER['REQUEST_METHOD'],
  303 + "request_ip" => $request->ip(),
  304 + "request_param" => json_encode($response, JSON_UNESCAPED_UNICODE),
  305 + "response_code" => 0,
  306 + "upt_num" => $res,
  307 + "createtime" => $time
  308 + ];
  309 + Db::name("focus_collection_log")->insert($log);
  310 + $this->success("数据更新成功【" . $res . "条】");
  311 + }
  312 +
  313 + }
  314 + $log = [];
  315 + $log = [
  316 + "auth_group_id" => $response[0]['group_id'],
  317 + "request_url" => $request->url(true),
  318 + "request_method" => $_SERVER['REQUEST_METHOD'],
  319 + "request_ip" => $request->ip(),
  320 + "request_param" => json_encode($response, JSON_UNESCAPED_UNICODE),
  321 + "response_code" => 0,
  322 + "upt_num" => 0,
  323 + "createtime" => $time
  324 + ];
  325 + Db::name("focus_collection_log")->insert($log);
  326 + $this->success("暂无需要更新的数据");
  327 + }
  328 +
  329 + /**
  330 + * 5. 根据渠道名称获取渠道unikey
  331 + */
  332 + public function get_fakeid()
  333 + {
  334 +
  335 + //获取渠道数组
  336 + $focusCollectionModel = new FocusCollection();
  337 + $from_list = $focusCollectionModel->getFromList();
  338 + $from_list = array_keys($from_list);
  339 +
  340 + $param = $this->request->param();
  341 + $from = $param['from'];//渠道来源
  342 + $name = $param['name'];//渠道名称
  343 +
  344 + if (empty($from)) {
  345 + return array_callback(false, "请先选择渠道来源");
  346 + } elseif (!in_array($from, $from_list)) {
  347 + return array_callback(false, "请先选择有效的渠道来源");
  348 + }
  349 +
  350 + if (empty($name)) {
  351 + return array_callback(false, "请先填写渠道名称");
  352 + }
  353 + $where = [
  354 + "from" => $from,
  355 + "name" => $name
  356 + ];
  357 + $count = $focusCollectionModel->where($where)->count();
  358 + if ($count > 0) {
  359 + return array_callback(false, $param['name'] . "`已存在,请更换渠道名称后重新操作");
  360 + }
  361 +
  362 + //请求接口
  363 + $url = "http://124.71.110.50:8080/get_fakeid?name=" . $name;
  364 + $res = HttpHelper::curl($url,"GET");
  365 + $result = json_decode($res,true);
  366 + if($result['code']!=1){
  367 + return array_callback(false, "接口请求异常".$result['msg']);
  368 + }else{
  369 + return array_callback(true,"数据获取成功",$result['data']);
  370 + }
  371 +
  372 + }
  373 +
  374 +}
  1 +<?php
  2 +
  3 +namespace app\api\controller\v1;
  4 +
  5 +use app\common\controller\Api;
  6 +use app\common\exception\UploadException;
  7 +use app\common\library\Ems;
  8 +use app\common\library\Sms;
  9 +use app\common\library\Upload;
  10 +use fast\Random;
  11 +use think\Cache;
  12 +use think\Config;
  13 +use think\Db;
  14 +use think\Validate;
  15 +
  16 +/**
  17 + * 首页公共
  18 + */
  19 +class Index extends Api
  20 +{
  21 + protected $noNeedLogin = ["*"];
  22 + protected $noNeedRight = '*';
  23 +
  24 + public function _initialize()
  25 + {
  26 + parent::_initialize();
  27 +
  28 + }
  29 +
  30 + /***
  31 + * 1_0、上传图片、文件到服务器
  32 + */
  33 + public function upload()
  34 + {
  35 + if ($this->request->isPost()) {
  36 + $config = [
  37 + 'size' => 2097152,
  38 + 'ext' => 'jpeg,jpg,png,xls,xlsx'
  39 + ];
  40 +
  41 + $file = $this->request->file('file');
  42 +
  43 + $upload_path = str_replace('\\', '/', ROOT_PATH . 'public/uploads');
  44 +
  45 + $save_path = '/uploads/';
  46 +
  47 + $info = $file->validate($config)->move($upload_path);
  48 +
  49 + $domain = get_root_url();
  50 +
  51 + if ($info) {
  52 + $data['filepath'] = $domain . str_replace('\\', '/', $save_path . $info->getSaveName());
  53 + $data['fileurl'] = str_replace('\\', '/', $save_path . $info->getSaveName());
  54 + $files = $file->getInfo();
  55 + $data['oldname'] = explode('.', $files['name']);
  56 + $data['oldname'] = $data['oldname'][0];
  57 + $this->success('文件上传成功', $data);
  58 +
  59 + } else {
  60 + $this->error("图片上传失败");//$file->getError()
  61 + }
  62 +
  63 + }
  64 + }
  65 +
  66 + /**
  67 + * 1_1、重新获取管理员信息
  68 + */
  69 + public function check_mng()
  70 + {
  71 + $post = $this->request->param();
  72 + $client_type = $post['client_type'] == 2 ? 2 : 1; //1农场端 2食堂端
  73 +
  74 + $return_data = $this->auth->getUserinfo();
  75 +
  76 + $is_mng_user = 0;//是否是农场管理员 0不是 1是
  77 + $mng_area = [];//管辖农场(多个)
  78 + $is_mng_user2 = 0;//是否是食堂管理员 0不是 1是
  79 + $mng_area2 = [];//管辖食堂(多个)
  80 +
  81 + //检查下user表手机号,若有手机号就看下hc_area_code_mng有没有对应手机号,有的话就把id绑定上
  82 + if (!empty($return_data['mobile'])) {
  83 +
  84 + $exsit_mobile = Db::name("farm_manager")->where("mng_admin_phone", $return_data['mobile'])->find();
  85 + if (!empty($exsit_mobile)) {
  86 + //更新管理员表的mng_user_id
  87 + Db::name("farm_manager")->where("mng_admin_phone", $return_data['mobile'])->update(["mng_user_id" => $return_data['id']]);
  88 + //将用户表中的基础信息更新一下
  89 + if ($exsit_mobile['mng_admin_name']) {
  90 + Db::name("user")->where("id", $return_data['id'])->update(["username" => $exsit_mobile['mng_admin_name']]);
  91 + }
  92 + }
  93 +
  94 + $exsit_mobile2 = Db::name("farm_canteenmanager")->where("mng_admin_phone", $return_data['mobile'])->find();
  95 + if (!empty($exsit_mobile2)) {
  96 + //更新管理员表的mng_user_id
  97 + Db::name("farm_canteenmanager")->where("mng_admin_phone", $return_data['mobile'])->update(["mng_user_id" => $return_data['id']]);
  98 + //将用户表中的基础信息更新一下
  99 + if ($exsit_mobile2['mng_admin_name']) {
  100 + Db::name("user")->where("id", $return_data['id'])->update(["username" => $exsit_mobile2['mng_admin_name']]);
  101 + }
  102 + }
  103 + }
  104 +
  105 + $area = Db::name("farm_manager")->alias("a")
  106 + ->join("farm_farm b", "b.id=a.farm_farm_id")
  107 + ->where("a.mng_user_id", $return_data['id'])->field("b.id as code,b.name")->select();
  108 + if (!empty($area)) {
  109 + $is_mng_user = 1;
  110 + $mng_area = $area;
  111 + if ($exsit_mobile['mng_admin_name'] && $client_type == 1) {
  112 + $return_data['username'] = $exsit_mobile['mng_admin_name'];
  113 + }
  114 + }
  115 + $return_data['is_mng_user'] = $is_mng_user;
  116 + $return_data['mng_area'] = $mng_area;
  117 +
  118 + $area2 = Db::name("farm_canteenmanager")->alias("a")
  119 + ->join("farm_canteen b", "b.id=a.farm_canteen_id")
  120 + ->where("a.mng_user_id", $return_data['id'])->field("b.id as code,b.name")->select();
  121 + if (!empty($area2)) {
  122 + $is_mng_user2 = 1;
  123 + $mng_area2 = $area2;
  124 + if ($exsit_mobile['mng_admin_name'] && $client_type == 2) {
  125 + $return_data['username'] = $exsit_mobile['mng_admin_name'];
  126 + }
  127 + }
  128 +
  129 + $return_data['is_mng_user2'] = $is_mng_user2;
  130 + $return_data['mng_area2'] = $mng_area2;
  131 +
  132 + if (empty($area) && empty($area2)) {
  133 + $this->error("您还不是管理员,请在后台绑定后操作!");
  134 + }
  135 +
  136 + $return_data['avatar'] = full_image($return_data['avatar']);
  137 + $sex = 1;
  138 + if ($client_type == 1) {
  139 + $sex = Db::name("farm_manager")->where("mng_user_id", $return_data['id'])->value("sex");
  140 + $sex = $sex == "2" ? 2 : 1;
  141 + } elseif ($client_type == 2) {
  142 + $sex = Db::name("farm_canteenmanager")->where("mng_user_id", $return_data['id'])->value("sex");
  143 + $sex = $sex == "2" ? 2 : 1;
  144 + }
  145 + $return_data['sex'] = $sex;
  146 +
  147 + $this->success("用户信息获取成功", $return_data);
  148 +
  149 +
  150 + }
  151 +
  152 + /**
  153 + * 1_2、更新个人资料
  154 + */
  155 + public function update_userinfo()
  156 + {
  157 + $user_id = $this->auth->id;
  158 + $post = $this->request->param("");
  159 +
  160 + $update_user_data = [];//user表数据更新
  161 + $update_mng_data = [];//mng表的数据更新
  162 + if ($post['avatar']) {
  163 + //更新图片
  164 + $update_user_data["avatar"] = $post['avatar'];
  165 + }
  166 + if ($post['username']) {
  167 + $update_user_data["username"] = $post['username'];
  168 + $update_user_data["nickname"] = $post['username'];
  169 + $update_mng_data["mng_admin_name"] = $post['username'];
  170 + }
  171 + if ($post['sex']) {
  172 + $update_mng_data['sex'] = $post['sex'] == 2 ? "2" : "1";
  173 + }
  174 + if (!empty($update_user_data)) {
  175 + Db::name("user")->where("id", $user_id)->update($update_user_data);
  176 + }
  177 + if (!empty($update_mng_data)) {
  178 + Db::name("farm_manager")->where("mng_user_id", $user_id)->update($update_mng_data);
  179 + Db::name("farm_canteenmanager")->where("mng_user_id", $user_id)->update($update_mng_data);
  180 + }
  181 + $this->success("资料更新成功");
  182 + }
  183 +
  184 + /**
  185 + * 10. 各模块轮播图列表(启动页、首页顶部轮播)+填报须知
  186 + *
  187 + */
  188 + public function getWctImg()
  189 + {
  190 + //启动图片
  191 + $list = Db::name('xcx_img')->where(['type' => "1", 'status' => 1])->order('id', 'desc')->find();
  192 + $imgs = $list['images'] ? explode(',', $list['images']) : [];
  193 + $data['start_img'] = $imgs ? full_image($imgs) : [];
  194 +
  195 + //首页图片
  196 + $list2 = Db::name('xcx_img')
  197 + ->where(['type' => "2", 'status' => 1])
  198 + ->order('id', 'desc')
  199 + ->find();
  200 + $imgs2 = $list2['images'] ? explode(',', $list2['images']) : [];
  201 + $data['index_banner_img'] = $imgs2 ? full_image($imgs2) : [];
  202 +
  203 + //管理端图片
  204 + $list2 = Db::name('xcx_img')
  205 + ->where(['type' => "3", 'status' => 1])
  206 + ->order('id', 'desc')
  207 + ->find();
  208 + $imgs2 = $list2['images'] ? explode(',', $list2['images']) : [];
  209 + $data['admin_banner_img'] = $imgs2 ? full_image($imgs2) : [];
  210 +
  211 + //填报须知
  212 + $data['guidance_notes'] = Config::get("site.guidance_notes");
  213 +
  214 + //电话汇总连接
  215 + $data['tel_link'] = Config::get("site.tel_link");
  216 + $data['jkb_tel_content'] = Config::get("site.jkb_tel_content");
  217 +
  218 + $this->success('获取成功', $data);
  219 + }
  220 +
  221 + /**
  222 + * 用户注册协议、隐私协议
  223 + */
  224 + public function regiest_xieyi()
  225 + {
  226 +
  227 + $data['regist'] = config('site.user_xieyi');
  228 + $data['yinsi'] = config('site.yinsi_xieyi');
  229 +
  230 + return $this->success("", $data);
  231 + }
  232 +
  233 +}
  1 +<?php
  2 +/**
  3 + * Created by PhpStorm.
  4 + * Login: Kevin
  5 + * Date: 2023/03/22
  6 + * Time: 14:10
  7 + */
  8 +
  9 +namespace app\api\controller\v1;
  10 +
  11 +use lib\WXBizDataCrypt;
  12 +use think\Db;
  13 +use think\Request;
  14 +
  15 +header('Access-Control-Allow-Origin:*');
  16 +header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
  17 +header('Access-Control-Allow-Methods: GET, POST, PUT');
  18 +
  19 +class Login extends Base
  20 +{
  21 + /**
  22 + * 1、小程序授权注册用户、返回用户信息
  23 + */
  24 + public function get_user_by_shouquan()
  25 + {
  26 + $appid = $this->AppID;
  27 + $AppSecret = $this->AppSecret;
  28 + $post = $this->request->post();
  29 + $code = $post['code'];// I('post.code');
  30 + $encryptedData = $post['encryptedData'];//I('post.encryptedData');
  31 + $iv = $post['iv'];//I('post.iv');
  32 +// if ($post['type'] == 1) {
  33 +// $encryptedData = urldecode($encryptedData);
  34 +// $iv = urldecode($iv);
  35 +// }
  36 +
  37 + $url = "https://api.weixin.qq.com/sns/jscode2session?appid=" . $appid . "&secret=" . $AppSecret . "&js_code=" . $code . "&grant_type=authorization_code";
  38 +// file_put_contents("ccc.txt", "授权1:" . date("Y-m-d H:i:s") .":" . $url . PHP_EOL, FILE_APPEND);
  39 +
  40 + $res = json_decode(http_request($url), true);
  41 + if (!$res) {
  42 + $res = json_decode(send_post($url), true);
  43 + }
  44 + $sessionKey = $res['session_key'];
  45 + $openid = $res['openid'];//获取用户openid
  46 + $unionid = $res['unionid'];//获取用户openid
  47 + file_put_contents("ccc.txt", "授权0:" . date("Y-m-d H:i:s") .":" . json_encode($post,JSON_UNESCAPED_UNICODE) . PHP_EOL, FILE_APPEND);
  48 + file_put_contents("ccc.txt", "授权1:" . date("Y-m-d H:i:s") . ":" . json_encode($res, JSON_UNESCAPED_UNICODE) . PHP_EOL, FILE_APPEND);
  49 +
  50 + require_once '../extend/lib/WXBizDataCrypt.class.php';
  51 +
  52 + $pc = new WXBizDataCrypt($appid, $sessionKey);
  53 + $errCode = $pc->decryptData($encryptedData, $iv, $data);
  54 + $data = json_decode($data, true);
  55 + file_put_contents("ccc.txt", "授权2:" . date("Y-m-d H:i:s") . ":" . $errCode . PHP_EOL, FILE_APPEND);
  56 + file_put_contents("ccc.txt", "授权3:" . date("Y-m-d H:i:s") .":". json_encode($data,JSON_UNESCAPED_UNICODE) . PHP_EOL, FILE_APPEND);
  57 +
  58 + if ($errCode != 0) {
  59 + $ajax['code'] = 0;
  60 + $ajax['info'] = $errCode;
  61 + $errCode = $errCode == "-41001" || $errCode == "-41003" ? "授权失败,请尝试重新授权" : $errCode;
  62 + $this->error($errCode);
  63 + }
  64 + if ($openid) {
  65 + $openid_info = [
  66 + "openid" => $openid,
  67 + "unionid" => $unionid,
  68 + "from" => "wx",
  69 + ];
  70 + if (!empty($data['nickName'])) {
  71 + $openid_info['nickName'] = $data['nickName'];
  72 + }
  73 + if (!empty($data['gender'])) {
  74 + $openid_info['gender'] = $data['gender'];
  75 + }
  76 + if (!empty($data['avatarUrl'])) {
  77 + $openid_info['avatarUrl'] = $data['avatarUrl'];
  78 + }
  79 + if (!empty($data['country'])) {
  80 + $openid_info['country'] = $data['country'];
  81 + }
  82 + if (!empty($data['province'])) {
  83 + $openid_info['province'] = $data['province'];
  84 + }
  85 + if (!empty($data['city'])) {
  86 + $openid_info['city'] = $data['city'];
  87 + }
  88 + if (!empty($data['phoneNumber'])) {
  89 + $openid_info['phoneNumber'] = $data['phoneNumber'];
  90 + }
  91 + insert_openid_info($openid_info);//更新下微信用户信息到数据库
  92 + }
  93 +
  94 + //直接通过unionid 查找用户信息
  95 + $userres = Db::name("user")->where("wx_xcx_openid", $openid)->order("id desc")->find();
  96 +
  97 + if (empty($userres)) {
  98 + //如果通过小程序openid找不到会员
  99 + //注册处理
  100 + $extend_data = [
  101 + "nickname" => $openid_info['nickName'],
  102 + "avatar" => $openid_info["avatarUrl"]?$openid_info["avatarUrl"]:"/default.png",
  103 + "wx_xcx_openid" => $openid,
  104 + "unionid" => $unionid,
  105 + ];
  106 + $username = $openid ? $openid : suiji_num("TY");
  107 + $ret = $this->auth->register($username, "a123456", '', $data['phoneNumber'], $extend_data);
  108 + if ($ret) {
  109 + $return_data = $this->auth->getUserinfo();
  110 +
  111 + $is_mng_user = 0;//是否是农场管理员 0不是 1是
  112 + $mng_area = [];//管辖区域(多个)
  113 + $area = Db::name("farm_manager")->alias("a")
  114 + ->join("farm_farm b", "b.id=a.farm_farm_id")
  115 + ->where("a.mng_user_id", $return_data['id'])->field("b.id as code,b.name")->select();
  116 + if (!empty($area)) {
  117 + $is_mng_user = 1;
  118 + $mng_area = $area;
  119 + }
  120 + $return_data['is_mng_user'] = $is_mng_user;
  121 + $return_data['mng_area'] = $mng_area;
  122 +
  123 + $is_mng_user2 = 0;//是否是食堂管理员 0不是 1是
  124 + $mng_area2 = [];//管辖区域(多个)
  125 + $area2 = Db::name("farm_canteenmanager")->alias("a")
  126 + ->join("farm_canteen b", "b.id=a.farm_canteen_id")
  127 + ->where("a.mng_user_id", $return_data['id'])->field("b.id as code,b.name")->select();
  128 + if (!empty($area2)) {
  129 + $is_mng_user2 = 1;
  130 + $mng_area2 = $area2;
  131 + }
  132 + $return_data['is_mng_user2'] = $is_mng_user2;
  133 + $return_data['mng_area2'] = $mng_area2;
  134 +
  135 + if (empty($return_data['mobile'])) {
  136 + $this->success("授权成功,请继续认证手机号后才能正常使用", $return_data, 2);
  137 + } else {
  138 + //[农场]这里是新注册用户,有手机号,需要判断下手机号有没有在食堂中存在,若存在就将id绑定过去
  139 + $bind = Db::name("farm_manager")->where("mng_admin_phone", $return_data['mobile'])->order("id desc")->find();
  140 + if (!empty($bind)) {
  141 + Db::name("farm_manager")->where("mng_admin_phone", $return_data['mobile'])->update(["mng_user_id" => $return_data['id']]);
  142 + if ($bind['mng_admin_name']) {
  143 + //将管理员姓名更新到用户表中
  144 + Db::name("user")->where("id", $return_data['id'])->update(["username" => $bind['mng_admin_name']]);
  145 + }
  146 + }
  147 +
  148 + //[食堂]这里是新注册用户,有手机号,需要判断下手机号有没有在食堂管理员中存在,若存在就将id绑定过去
  149 + $bind2 = Db::name("farm_canteenmanager")->where("mng_admin_phone", $return_data['mobile'])->order("id desc")->find();
  150 + if (!empty($bind2)) {
  151 + Db::name("farm_canteenmanager")->where("mng_admin_phone", $return_data['mobile'])->update(["mng_user_id" => $return_data['id']]);
  152 + if ($bind2['mng_admin_name']) {
  153 + //将管理员姓名更新到用户表中
  154 + Db::name("user")->where("id", $return_data['id'])->update(["username" => $bind2['mng_admin_name']]);
  155 + }
  156 + }
  157 +
  158 + }
  159 +
  160 + $return_data['avatar'] =full_image($return_data['avatar']);
  161 + $this->success("登录成功!", $return_data);
  162 + } else {
  163 + $this->error($this->auth->getError());
  164 + }
  165 + } else {
  166 + //小程序openid找到了会员
  167 + $update_data = [];
  168 + $update_data['logintime'] = time();
  169 + if (empty($userres['mobile']) && !empty($openid_info['phoneNumber'])) {
  170 + $update_data['mobile'] = $openid_info['phoneNumber'];
  171 + }
  172 +// if (!empty($openid_info['phoneNumber'])) {
  173 +// $update_data['username'] = $openid_info['phoneNumber'];
  174 +// }
  175 + if (!empty($openid_info['nickName']) && $openid_info['nickName']!="微信用户") {
  176 + $update_data['nickname'] = $openid_info['nickName'];
  177 + }
  178 + if(!empty($unionid)){
  179 + $update_data['unionid'] = $unionid;
  180 + }
  181 +
  182 + if (!empty($openid_info['phoneNumber'])) {
  183 + //[农场]这里是已注册过的用户,有手机号,需要判断下手机号有没有在食堂中存在,若存在就将id绑定过去
  184 + $bind = Db::name("farm_manager")->where("mng_admin_phone", $openid_info['phoneNumber'])->order("id desc")->find();
  185 + if (!empty($bind)) {
  186 + Db::name("farm_manager")->where("mng_admin_phone", $openid_info['phoneNumber'])->update(["mng_user_id" => $userres['id']]);
  187 + if ($bind['mng_admin_name']) {
  188 + //将管理员姓名更新到用户表中
  189 + Db::name("user")->where("id", $userres['id'])->update(["username" => $bind['mng_admin_name']]);
  190 + }
  191 + }
  192 +
  193 + //[食堂]这里是已注册过的用户,有手机号,需要判断下手机号有没有在食堂管理员中存在,若存在就将id绑定过去
  194 + $bind2 = Db::name("farm_canteenmanager")->where("mng_admin_phone", $openid_info['phoneNumber'])->order("id desc")->find();
  195 + if (!empty($bind2)) {
  196 + Db::name("farm_canteenmanager")->where("mng_admin_phone", $openid_info['phoneNumber'])->update(["mng_user_id" => $userres['id']]);
  197 + if ($bind2['mng_admin_name']) {
  198 + //将管理员姓名更新到用户表中
  199 + Db::name("user")->where("id", $userres['id'])->update(["username" => $bind2['mng_admin_name']]);
  200 + }
  201 + }
  202 +
  203 + }
  204 +
  205 + Db::name("user")->where("wx_xcx_openid", $openid)->update($update_data);
  206 + $this->auth->direct($userres['id']);
  207 + $return_data = $this->auth->getUserinfo();
  208 +
  209 +
  210 + $is_mng_user = 0;//是否是农场管理员 0不是 1是
  211 + $mng_area = [];//管辖区域(多个)
  212 + $area = Db::name("farm_manager")->alias("a")
  213 + ->join("farm_farm b", "b.id=a.farm_farm_id")
  214 + ->where("a.mng_user_id", $return_data['id'])->field("b.id as code,b.name")->select();
  215 + if (!empty($area)) {
  216 + $is_mng_user = 1;
  217 + $mng_area = $area;
  218 + }
  219 + $return_data['is_mng_user'] = $is_mng_user;
  220 + $return_data['mng_area'] = $mng_area;
  221 +
  222 + $is_mng_user2 = 0;//是否是食堂管理员 0不是 1是
  223 + $mng_area2 = [];//管辖区域(多个)
  224 + $area2 = Db::name("farm_canteenmanager")->alias("a")
  225 + ->join("farm_canteen b", "b.id=a.farm_canteen_id")
  226 + ->where("a.mng_user_id", $return_data['id'])->field("b.id as code,b.name")->select();
  227 + if (!empty($area2)) {
  228 + $is_mng_user2 = 1;
  229 + $mng_area2 = $area2;
  230 + }
  231 + $return_data['is_mng_user2'] = $is_mng_user2;
  232 + $return_data['mng_area2'] = $mng_area2;
  233 +
  234 + $return_data['avatar'] =full_image($return_data['avatar']);
  235 +
  236 + $this->success(__('登录成功'), $return_data);
  237 + }
  238 + }
  239 +
  240 + /**
  241 + * 给后台添加用户时自动注册用
  242 + */
  243 + public function auto_regist_user($username, $password = "a123456", $email = "", $mobile, $extend_data)
  244 + {
  245 + $ret = $this->auth->register($username, $password, '', $mobile, $extend_data);
  246 + if ($ret) {
  247 + $return_data = $this->auth->getUserinfo();
  248 + return array_callback(true, "用户添加成功", $return_data['id']);
  249 + } else {
  250 + return array_callback(false, $this->auth->getError());
  251 + }
  252 + }
  253 +
  254 +}