moonsflyer 8 месяцев назад
Родитель
Сommit
578514ea77

+ 264 - 0
app/api/controller/ServiceController.php

@@ -0,0 +1,264 @@
+<?php
+// +----------------------------------------------------------------------
+// | likeadmin快速开发前后端分离管理后台(PHP版)
+// +----------------------------------------------------------------------
+// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
+// | 开源版本可自由商用,可去除界面版权logo
+// | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
+// | github下载:https://github.com/likeshop-github/likeadmin
+// | 访问官网:https://www.likeadmin.cn
+// | likeadmin团队 版权所有 拥有最终解释权
+// +----------------------------------------------------------------------
+// | author: likeadminTeam
+// +----------------------------------------------------------------------
+
+namespace app\api\controller;
+
+use app\api\logic\UserGoodsLogic;
+use app\common\server\JsonServer;
+use app\api\validate\{ServiceValidate,
+    LoginAccountValidate,
+    RegisterValidate,
+    UserGoodsValidate,
+    WebScanLoginValidate,
+    WechatLoginValidate};
+use app\api\logic\LoginLogic;
+use app\api\logic\ServiceLogic;
+
+
+/**
+ *  服务商管理
+ * Class LoginController
+ * @package app\api\controller
+ */
+class ServiceController extends BaseApiController
+{
+
+    public array $notNeedLogin = ['register', 'account', 'logout', 'codeUrl', 'oaLogin',  'mnpLogin', 'getScanCode', 'scanLogin'];
+
+
+    /**
+     * @notes 服务商入住
+     * @return \think\response\Json
+     * @author 段誉
+     * @date 2022/9/7 15:38
+     */
+    public function serviceProviderSettled()
+    {
+        if($this->request->isPost()) {
+
+            $params = (new ServiceValidate())->post()->goCheck('agricultural_machinery_operator');
+            $userId = $this->userId;
+            $params['user_id'] = $userId;
+            $result = ServiceLogic::Settled($params);
+            if (true === $result) {
+                switch ($params['type']){
+                    case 1 :
+                        $type_name = '农机手';
+                        break;
+                    case 2 :
+                        $type_name = '烘干服务';
+                        break ;
+                    case 3 :
+                        $type_name = '飞防服务';
+                        break;
+                    default :
+                        $type_name = '';
+                        break;
+                }
+                return $this->success($type_name.'入驻成功', [], 1, 1);
+            }
+
+
+        }else{
+            return $this->fail('请求方式错误'); return JsonServer::error('请求方式错误');
+        }
+
+    }
+
+    /**
+     * @notes 注册账号
+     * @return \think\response\Json
+     * @author 段誉
+     * @date 2022/9/7 15:38
+     */
+    public function register()
+    {
+        $params = (new RegisterValidate())->post()->goCheck('register');
+        $result = LoginLogic::register($params);
+        if (true === $result) {
+            return $this->success('注册成功', [], 1, 1);
+        }
+        return $this->fail(LoginLogic::getError());
+    }
+
+
+    /**
+     * @notes 账号密码/手机号密码/手机号验证码登录
+     * @return \think\response\Json
+     * @author 段誉
+     * @date 2022/9/16 10:42
+     */
+    public function account()
+    {
+        $params = (new LoginAccountValidate())->post()->goCheck();
+        $result = LoginLogic::login($params);
+        if (false === $result) {
+            return $this->fail(LoginLogic::getError());
+        }
+        return $this->data($result);
+    }
+
+
+    /**
+     * @notes 退出登录
+     * @return \think\response\Json
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\DbException
+     * @throws \think\db\exception\ModelNotFoundException
+     * @author 段誉
+     * @date 2022/9/16 10:42
+     */
+    public function logout()
+    {
+        LoginLogic::logout($this->userInfo);
+        return $this->success();
+    }
+
+
+    /**
+     * @notes 获取微信请求code的链接
+     * @return \think\response\Json
+     * @author 段誉
+     * @date 2022/9/15 18:27
+     */
+    public function codeUrl()
+    {
+        $url = $this->request->get('url');
+        $result = ['url' => LoginLogic::codeUrl($url)];
+        return $this->success('获取成功', $result);
+    }
+
+
+    /**
+     * @notes 公众号登录
+     * @return \think\response\Json
+     * @throws \GuzzleHttp\Exception\GuzzleException
+     * @author 段誉
+     * @date 2022/9/20 19:48
+     */
+    public function oaLogin()
+    {
+        $params = (new WechatLoginValidate())->post()->goCheck('oa');
+        $res = LoginLogic::oaLogin($params);
+        if (false === $res) {
+            return $this->fail(LoginLogic::getError());
+        }
+        return $this->success('', $res);
+    }
+
+
+    /**
+     * @notes 小程序-登录接口
+     * @return \think\response\Json
+     * @author 段誉
+     * @date 2022/9/20 19:48
+     */
+    public function mnpLogin()
+    {
+        $params = (new WechatLoginValidate())->post()->goCheck('mnpLogin');
+        $res = LoginLogic::mnpLogin($params);
+        if (false === $res) {
+            return $this->fail(LoginLogic::getError());
+        }
+        return $this->success('', $res);
+    }
+
+
+    /**
+     * @notes 小程序绑定微信
+     * @return \think\response\Json
+     * @author 段誉
+     * @date 2022/9/20 19:48
+     */
+    public function mnpAuthBind()
+    {
+        $params = (new WechatLoginValidate())->post()->goCheck("wechatAuth");
+        $params['user_id'] = $this->userId;
+        $result = LoginLogic::mnpAuthLogin($params);
+        if ($result === false) {
+            return $this->fail(LoginLogic::getError());
+        }
+        return $this->success('绑定成功', [], 1, 1);
+    }
+
+
+
+    /**
+     * @notes 公众号绑定微信
+     * @return \think\response\Json
+     * @throws \GuzzleHttp\Exception\GuzzleException
+     * @author 段誉
+     * @date 2022/9/20 19:48
+     */
+    public function oaAuthBind()
+    {
+        $params = (new WechatLoginValidate())->post()->goCheck("wechatAuth");
+        $params['user_id'] = $this->userId;
+        $result = LoginLogic::oaAuthLogin($params);
+        if ($result === false) {
+            return $this->fail(LoginLogic::getError());
+        }
+        return $this->success('绑定成功', [], 1, 1);
+    }
+
+
+    /**
+     * @notes 获取扫码地址
+     * @return \think\response\Json
+     * @author 段誉
+     * @date 2022/10/20 18:25
+     */
+    public function getScanCode()
+    {
+        $redirectUri = $this->request->get('url/s');
+        $result = LoginLogic::getScanCode($redirectUri);
+        if (false === $result) {
+            return $this->fail(LoginLogic::getError() ?? '未知错误');
+        }
+        return $this->success('', $result);
+    }
+
+
+    /**
+     * @notes 网站扫码登录
+     * @return \think\response\Json
+     * @author 段誉
+     * @date 2022/10/21 10:28
+     */
+    public function scanLogin()
+    {
+        $params = (new WebScanLoginValidate())->post()->goCheck();
+        $result = LoginLogic::scanLogin($params);
+        if (false === $result) {
+            return $this->fail(LoginLogic::getError() ?? '登录失败');
+        }
+        return $this->success('', $result);
+    }
+
+
+    /**
+     * @notes 更新用户头像昵称
+     * @return \think\response\Json
+     * @author 段誉
+     * @date 2023/2/22 11:15
+     */
+    public function updateUser()
+    {
+        $params = (new WechatLoginValidate())->post()->goCheck("updateUser");
+        LoginLogic::updateUser($params, $this->userId);
+        return $this->success('操作成功', [], 1, 1);
+    }
+
+
+}

+ 120 - 0
app/api/lists/service/ServiceLists.php

@@ -0,0 +1,120 @@
+<?php
+// +----------------------------------------------------------------------
+// | likeadmin快速开发前后端分离管理后台(PHP版)
+// +----------------------------------------------------------------------
+// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
+// | 开源版本可自由商用,可去除界面版权logo
+// | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
+// | github下载:https://github.com/likeshop-github/likeadmin
+// | 访问官网:https://www.likeadmin.cn
+// | likeadmin团队 版权所有 拥有最终解释权
+// +----------------------------------------------------------------------
+// | author: likeadminTeam
+// +----------------------------------------------------------------------
+
+namespace app\api\lists\article;
+
+use app\api\lists\BaseApiDataLists;
+use app\common\enum\YesNoEnum;
+use app\common\lists\ListsSearchInterface;
+use app\common\model\article\Article;
+use app\common\model\article\ArticleCollect;
+
+
+/**
+ * 服务商列表
+ * Class ArticleLists
+ * @package app\api\lists\article
+ */
+class ServiceLists extends BaseApiDataLists implements ListsSearchInterface
+{
+
+    /**
+     * @notes 搜索条件
+     * @return \string[][]
+     * @author 段誉
+     * @date 2022/9/16 18:54
+     */
+    public function setSearch(): array
+    {
+        return [
+            '=' => ['cid']
+        ];
+    }
+
+
+    /**
+     * @notes 自定查询条件
+     * @return array
+     * @author 段誉
+     * @date 2022/10/25 16:53
+     */
+    public function queryWhere()
+    {
+        $where[] = ['is_show', '=', 1];
+        if (!empty($this->params['keyword'])) {
+            $where[] = ['title', 'like', '%' . $this->params['keyword'] . '%'];
+        }
+        return $where;
+    }
+
+
+    /**
+     * @notes 获取文章列表
+     * @return array
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\DbException
+     * @throws \think\db\exception\ModelNotFoundException
+     * @author 段誉
+     * @date 2022/9/16 18:55
+     */
+    public function lists(): array
+    {
+        $orderRaw = 'sort desc, id desc';
+        $sortType = $this->params['sort'] ?? 'default';
+        // 最新排序
+        if ($sortType == 'new') {
+            $orderRaw = 'id desc';
+        }
+        // 最热排序
+        if ($sortType == 'hot') {
+            $orderRaw = 'click_actual + click_virtual desc, id desc';
+        }
+
+        $field = 'id,cid,title,desc,image,click_virtual,click_actual,create_time';
+        $result = Article::field($field)
+            ->where($this->queryWhere())
+            ->where($this->searchWhere)
+            ->orderRaw($orderRaw)
+            ->append(['click'])
+            ->hidden(['click_virtual', 'click_actual'])
+            ->limit($this->limitOffset, $this->limitLength)
+            ->select()->toArray();
+
+        $articleIds = array_column($result, 'id');
+
+        $collectIds = ArticleCollect::where(['user_id' => $this->userId, 'status' => YesNoEnum::YES])
+            ->whereIn('article_id', $articleIds)
+            ->column('article_id');
+
+        foreach ($result as &$item) {
+            $item['collect'] = in_array($item['id'], $collectIds);
+        }
+
+        return $result;
+    }
+
+
+    /**
+     * @notes 获取文章数量
+     * @return int
+     * @author 段誉
+     * @date 2022/9/16 18:55
+     */
+    public function count(): int
+    {
+        return Article::where($this->searchWhere)
+            ->where($this->queryWhere())
+            ->count();
+    }
+}

+ 439 - 0
app/api/logic/ServiceLogic.php

@@ -0,0 +1,439 @@
+<?php
+// +----------------------------------------------------------------------
+// | likeadmin快速开发前后端分离管理后台(PHP版)
+// +----------------------------------------------------------------------
+// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
+// | 开源版本可自由商用,可去除界面版权logo
+// | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
+// | github下载:https://github.com/likeshop-github/likeadmin
+// | 访问官网:https://www.likeadmin.cn
+// | likeadmin团队 版权所有 拥有最终解释权
+// +----------------------------------------------------------------------
+// | author: likeadminTeam
+// +----------------------------------------------------------------------
+
+namespace app\api\logic;
+
+use app\common\cache\WebScanLoginCache;
+use app\common\logic\BaseLogic;
+use app\api\service\{UserTokenService, WechatUserService};
+use app\common\enum\{LoginEnum, user\UserTerminalEnum, YesNoEnum};
+use app\common\service\{
+    ConfigService,
+    FileService,
+    wechat\WeChatConfigService,
+    wechat\WeChatMnpService,
+    wechat\WeChatOaService,
+    wechat\WeChatRequestService
+};
+use app\common\model\agricultural_machinery\UserService;
+use app\common\model\user\{User, UserAuth};
+use think\facade\{Db, Config};
+
+/**
+ * 服务商逻辑
+ * Class LoginLogic
+ * @package app\api\logic
+ */
+class ServiceLogic extends BaseLogic
+{
+
+    /**
+     * @notes 入驻
+     * @param array $params
+     * @return bool
+     * @author 段誉
+     * @date 2022/9/7 15:37
+     */
+    public static function Settled(array $params)
+    {
+        Db::startTrans();
+        try {
+//            $userSn = User::createUserSn();
+//            $passwordSalt = Config::get('project.unique_identification');
+//            $password = create_password($params['password'], $passwordSalt);
+//            $avatar = ConfigService::get('default_image', 'user_avatar');
+
+            UserService::create([
+                'user_id' => $params['user_id'],
+                'type' => $params['type'] ?? 1,
+                'name' => $params['name'],
+                'mobile' => $params['mobile'],
+                'agricultural_machinery_model' =>$params['agricultural_machinery_model'],
+                'images' => $params['images'],
+                'cate_id' => $params['cate_id'],
+                'area_id' => $params['area_id'],
+                'money' => $params['money'],
+            ]);
+            Db::commit();
+            return true;
+        } catch (\Exception $e) {
+            Db::rollback();
+            self::setError($e->getMessage());
+            return false;
+        }
+    }
+
+
+    /**
+     * @notes 账号/手机号登录,手机号验证码
+     * @param $params
+     * @return array|false
+     * @author 段誉
+     * @date 2022/9/6 19:26
+     */
+    public static function login($params)
+    {
+        try {
+            // 账号/手机号 密码登录
+            $where = ['account|mobile' => $params['account']];
+            if ($params['scene'] == LoginEnum::MOBILE_CAPTCHA) {
+                //手机验证码登录
+                $where = ['mobile' => $params['account']];
+            }
+
+            $user = User::where($where)->findOrEmpty();
+            if ($user->isEmpty()) {
+                throw new \Exception('用户不存在');
+            }
+
+            //更新登录信息
+            $user->login_time = time();
+            $user->login_ip = request()->ip();
+            $user->save();
+
+            //设置token
+            $userInfo = UserTokenService::setToken($user->id, $params['terminal']);
+
+            //返回登录信息
+            $avatar = $user->avatar ?: Config::get('project.default_image.user_avatar');
+            $avatar = FileService::getFileUrl($avatar);
+
+            return [
+                'nickname' => $userInfo['nickname'],
+                'sn' => $userInfo['sn'],
+                'mobile' => $userInfo['mobile'],
+                'avatar' => $avatar,
+                'token' => $userInfo['token'],
+            ];
+        } catch (\Exception $e) {
+            self::setError($e->getMessage());
+            return false;
+        }
+    }
+
+
+    /**
+     * @notes 退出登录
+     * @param $userInfo
+     * @return bool
+     * @throws \think\db\exception\DataNotFoundException
+     * @throws \think\db\exception\DbException
+     * @throws \think\db\exception\ModelNotFoundException
+     * @author 段誉
+     * @date 2022/9/16 17:56
+     */
+    public static function logout($userInfo)
+    {
+        //token不存在,不注销
+        if (!isset($userInfo['token'])) {
+            return false;
+        }
+
+        //设置token过期
+        return UserTokenService::expireToken($userInfo['token']);
+    }
+
+
+    /**
+     * @notes 获取微信请求code的链接
+     * @param string $url
+     * @return string
+     * @author 段誉
+     * @date 2022/9/20 19:47
+     */
+    public static function codeUrl(string $url)
+    {
+        return (new WeChatOaService())->getCodeUrl($url);
+    }
+
+
+    /**
+     * @notes 公众号登录
+     * @param array $params
+     * @return array|false
+     * @throws \GuzzleHttp\Exception\GuzzleException
+     * @author 段誉
+     * @date 2022/9/20 19:47
+     */
+    public static function oaLogin(array $params)
+    {
+        Db::startTrans();
+        try {
+            //通过code获取微信 openid
+            $response = (new WeChatOaService())->getOaResByCode($params['code']);
+            $userServer = new WechatUserService($response, UserTerminalEnum::WECHAT_OA);
+            $userInfo = $userServer->getResopnseByUserInfo()->authUserLogin()->getUserInfo();
+
+            // 更新登录信息
+            self::updateLoginInfo($userInfo['id']);
+
+            Db::commit();
+            return $userInfo;
+
+        } catch (\Exception $e) {
+            Db::rollback();
+            self::$error = $e->getMessage();
+            return false;
+        }
+    }
+
+
+    /**
+     * @notes 小程序-静默登录
+     * @param array $params
+     * @return array|false
+     * @author 段誉
+     * @date 2022/9/20 19:47
+     */
+    public static function silentLogin(array $params)
+    {
+        try {
+            //通过code获取微信 openid
+            $response = (new WeChatMnpService())->getMnpResByCode($params['code']);
+            $userServer = new WechatUserService($response, UserTerminalEnum::WECHAT_MMP);
+            $userInfo = $userServer->getResopnseByUserInfo('silent')->getUserInfo();
+
+            if (!empty($userInfo)) {
+                // 更新登录信息
+                self::updateLoginInfo($userInfo['id']);
+            }
+
+            return $userInfo;
+        } catch (\Exception  $e) {
+            self::$error = $e->getMessage();
+            return false;
+        }
+    }
+
+
+    /**
+     * @notes 小程序-授权登录
+     * @param array $params
+     * @return array|false
+     * @author 段誉
+     * @date 2022/9/20 19:47
+     */
+    public static function mnpLogin(array $params)
+    {
+        Db::startTrans();
+        try {
+            //通过code获取微信 openid
+            $response = (new WeChatMnpService())->getMnpResByCode($params['code']);
+            $userServer = new WechatUserService($response, UserTerminalEnum::WECHAT_MMP);
+            $userInfo = $userServer->getResopnseByUserInfo()->authUserLogin()->getUserInfo();
+
+            // 更新登录信息
+            self::updateLoginInfo($userInfo['id']);
+
+            Db::commit();
+            return $userInfo;
+        } catch (\Exception  $e) {
+            Db::rollback();
+            self::$error = $e->getMessage();
+            return false;
+        }
+    }
+
+
+    /**
+     * @notes 更新登录信息
+     * @param $userId
+     * @throws \Exception
+     * @author 段誉
+     * @date 2022/9/20 19:46
+     */
+    public static function updateLoginInfo($userId)
+    {
+        $user = User::findOrEmpty($userId);
+        if ($user->isEmpty()) {
+            throw new \Exception('用户不存在');
+        }
+
+        $time = time();
+        $user->login_time = $time;
+        $user->login_ip = request()->ip();
+        $user->update_time = $time;
+        $user->save();
+    }
+
+
+    /**
+     * @notes 小程序端绑定微信
+     * @param array $params
+     * @return bool
+     * @author 段誉
+     * @date 2022/9/20 19:46
+     */
+    public static function mnpAuthLogin(array $params)
+    {
+        try {
+            //通过code获取微信openid
+            $response = (new WeChatMnpService())->getMnpResByCode($params['code']);
+            $response['user_id'] = $params['user_id'];
+            $response['terminal'] = UserTerminalEnum::WECHAT_MMP;
+
+            return self::createAuth($response);
+
+        } catch (\Exception  $e) {
+            self::$error = $e->getMessage();
+            return false;
+        }
+    }
+
+
+    /**
+     * @notes 公众号端绑定微信
+     * @param array $params
+     * @return bool
+     * @throws \GuzzleHttp\Exception\GuzzleException
+     * @author 段誉
+     * @date 2022/9/16 10:43
+     */
+    public static function oaAuthLogin(array $params)
+    {
+        try {
+            //通过code获取微信openid
+            $response = (new WeChatOaService())->getOaResByCode($params['code']);
+            $response['user_id'] = $params['user_id'];
+            $response['terminal'] = UserTerminalEnum::WECHAT_OA;
+
+            return self::createAuth($response);
+
+        } catch (\Exception  $e) {
+            self::$error = $e->getMessage();
+            return false;
+        }
+    }
+
+
+    /**
+     * @notes 生成授权记录
+     * @param $response
+     * @return bool
+     * @throws \Exception
+     * @author 段誉
+     * @date 2022/9/16 10:43
+     */
+    public static function createAuth($response)
+    {
+        //先检查openid是否有记录
+        $isAuth = UserAuth::where('openid', '=', $response['openid'])->findOrEmpty();
+        if (!$isAuth->isEmpty()) {
+            throw new \Exception('该微信已被绑定');
+        }
+
+        if (isset($response['unionid']) && !empty($response['unionid'])) {
+            //在用unionid找记录,防止生成两个账号,同个unionid的问题
+            $userAuth = UserAuth::where(['unionid' => $response['unionid']])
+                ->findOrEmpty();
+            if (!$userAuth->isEmpty() && $userAuth->user_id != $response['user_id']) {
+                throw new \Exception('该微信已被绑定');
+            }
+        }
+
+        //如果没有授权,直接生成一条微信授权记录
+        UserAuth::create([
+            'user_id' => $response['user_id'],
+            'openid' => $response['openid'],
+            'unionid' => $response['unionid'] ?? '',
+            'terminal' => $response['terminal'],
+        ]);
+        return true;
+    }
+
+
+    /**
+     * @notes 获取扫码登录地址
+     * @return array|false
+     * @author 段誉
+     * @date 2022/10/20 18:23
+     */
+    public static function getScanCode($redirectUri)
+    {
+        try {
+            $config = WeChatConfigService::getOpConfig();
+            $appId = $config['app_id'];
+            $redirectUri = UrlEncode($redirectUri);
+
+            // 设置有效时间标记状态, 超时扫码不可登录
+            $state = MD5(time().rand(10000, 99999));
+            (new WebScanLoginCache())->setScanLoginState($state);
+
+            // 扫码地址
+            $url = WeChatRequestService::getScanCodeUrl($appId, $redirectUri, $state);
+            return ['url' => $url];
+
+        } catch (\Exception $e) {
+            self::$error = $e->getMessage();
+            return false;
+        }
+    }
+
+
+    /**
+     * @notes 网站扫码登录
+     * @param $params
+     * @return array|false
+     * @author 段誉
+     * @date 2022/10/21 10:28
+     */
+    public static function scanLogin($params)
+    {
+        Db::startTrans();
+        try {
+            // 通过code 获取 access_token,openid,unionid等信息
+            $userAuth = WeChatRequestService::getUserAuthByCode($params['code']);
+
+            if (empty($userAuth['openid']) || empty($userAuth['access_token'])) {
+                throw new \Exception('获取用户授权信息失败');
+            }
+
+            // 获取微信用户信息
+            $response = WeChatRequestService::getUserInfoByAuth($userAuth['access_token'], $userAuth['openid']);
+
+            // 生成用户或更新用户信息
+            $userServer = new WechatUserService($response, UserTerminalEnum::PC);
+            $userInfo = $userServer->getResopnseByUserInfo()->authUserLogin()->getUserInfo();
+
+            // 更新登录信息
+            self::updateLoginInfo($userInfo['id']);
+
+            Db::commit();
+            return $userInfo;
+
+        } catch (\Exception $e) {
+            Db::rollback();
+            self::$error = $e->getMessage();
+            return false;
+        }
+    }
+
+
+    /**
+     * @notes 更新用户信息
+     * @param $params
+     * @param $userId
+     * @return User
+     * @author 段誉
+     * @date 2023/2/22 11:19
+     */
+    public static function updateUser($params, $userId)
+    {
+        return User::where(['id' => $userId])->update([
+            'nickname' => $params['nickname'],
+            'avatar' => FileService::setFileUrl($params['avatar']),
+            'is_new_user' => YesNoEnum::NO
+        ]);
+    }
+}

+ 112 - 0
app/api/validate/ServiceValidate.php

@@ -0,0 +1,112 @@
+<?php
+// +----------------------------------------------------------------------
+// | likeadmin快速开发前后端分离管理后台(PHP版)
+// +----------------------------------------------------------------------
+// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
+// | 开源版本可自由商用,可去除界面版权logo
+// | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
+// | github下载:https://github.com/likeshop-github/likeadmin
+// | 访问官网:https://www.likeadmin.cn
+// | likeadmin团队 版权所有 拥有最终解释权
+// +----------------------------------------------------------------------
+// | author: likeadminTeam
+// +----------------------------------------------------------------------
+
+namespace app\api\validate;
+
+use app\common\cache\UserAccountSafeCache;
+use app\common\enum\LoginEnum;
+use app\common\enum\notice\NoticeEnum;
+use app\common\enum\user\UserTerminalEnum;
+use app\common\enum\YesNoEnum;
+use app\common\service\ConfigService;
+use app\common\service\sms\SmsDriver;
+use app\common\validate\BaseValidate;
+use app\common\model\user\User;
+use app\common\model\agricultural_machinery\ServiceCategory;
+use app\common\model\asset\AssetArea;
+use think\facade\Config;
+
+/**
+ * 服务商校验
+ * Class LoginValidate
+ * @package app\api\validate
+ */
+class ServiceValidate extends BaseValidate
+{
+
+    protected $rule = [
+        'name'=>'require|min:1|max:64',
+        'mobile' => 'require|mobile',
+        'type'  => 'require|in:1,2,3',
+        'cate_id'=>'require|checkCateCode',
+        'agricultural_machinery_model'=>'require',
+        'image'=>'require',
+        'area_id'  => 'require|checkArea',
+        'images'=>'require',
+        'content'=>'require',
+        'money' => 'require|float|egt:0',
+    ];
+
+
+    protected $message = [
+        'name.require'                          => '请输入名称',
+        'name.min'                              => '名称长度至少1个字符',
+        'name.max'                              => '名称长度最多64个字符',
+        'mobile.require' => '联系方式参数缺失',
+        'mobile.mobile' => '请填写正确的手机号',
+        'type.require'  => '类型type参数必传',
+        'cate_id.require'  => '分类信息必传',
+        'type.in'    =>'类型type参数规则错误',
+        'image.require'                          => '请输入上传图片',
+        'images.require'                          => '请输入上传服务图片',
+        'agricultural_machinery_model.require'   => '农机型号必传',
+        'area_id.require'  => '地区信息必传',
+        'money.require' =>'服务费用必传',
+        'money.float' =>'服务费参数错误',
+        'money.egt' =>'服务费参数规则错误',
+    ];
+    protected $scene = [
+        'agricultural_machinery_operator' => ['name','mobile','type','cate_id','area_id','agricultural_machinery_model','images','money'], //
+        'search' => ['goods_type'], //
+        'check_enroll'=>['c_id'],
+        'checkFight'=>['fight_id'],
+    ];
+
+    /**
+     * @notes 场景相关校验
+     * @param $scene
+     * @param $rule
+     * @param $data
+     * @return bool|string
+     * @author 段誉
+     * @date 2022/9/15 14:37
+     */
+    public function checkCateCode($scene, $rule, $data)
+    {
+        $cate_id_arr = explode(',',$data['cate_id']);
+        foreach($cate_id_arr as $v){
+            $info = ServiceCategory::where(['id'=>$v,'type'=>$data['type']])->findOrEmpty();
+            if($info->isEmpty()){
+                return '传入的分类id:'.$v.'不存在!';
+            }
+        }
+
+        return true;
+    }
+
+    public function checkArea($scene, $rule, $data)
+    {
+
+        $area_id_arr = explode(',',$data['area_id']);
+        foreach($area_id_arr as $v){
+            $info = AssetArea::where(['id'=>$v,'status'=>1])->findOrEmpty();
+            if($info->isEmpty()){
+                return '传入的地区id:'.$v.'不存在!';
+            }
+        }
+        return true;
+    }
+
+
+}

+ 75 - 0
app/common/model/agricultural_machinery/UserService.php

@@ -0,0 +1,75 @@
+<?php
+// +----------------------------------------------------------------------
+// | likeadmin快速开发前后端分离管理后台(PHP版)
+// +----------------------------------------------------------------------
+// | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
+// | 开源版本可自由商用,可去除界面版权logo
+// | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
+// | github下载:https://github.com/likeshop-github/likeadmin
+// | 访问官网:https://www.likeadmin.cn
+// | likeadmin团队 版权所有 拥有最终解释权
+// +----------------------------------------------------------------------
+// | author: likeadminTeam
+// +----------------------------------------------------------------------
+
+namespace app\common\model\agricultural_machinery;
+
+use app\common\model\BaseModel;
+use think\model\concern\SoftDelete;
+
+/**
+ * 服务管理模型
+ * Class ArticleCate
+ * @package app\common\model\article;
+ */
+class UserService extends BaseModel
+{
+    use SoftDelete;
+
+    protected $deleteTime = 'delete_time';
+
+
+    /**
+     * @notes 关联文章
+     * @return \think\model\relation\HasMany
+     * @author 段誉
+     * @date 2022/10/19 16:59
+     */
+    public function article()
+    {
+        return $this->hasMany(Article::class, 'cid', 'id');
+    }
+
+
+    /**
+     * @notes 状态描述
+     * @param $value
+     * @param $data
+     * @return string
+     * @author 段誉
+     * @date 2022/9/15 11:25
+     */
+    public function getStatusDescAttr($value, $data)
+    {
+        return $data['status'] ? '启用' : '停用';
+    }
+
+
+    /**
+     * @notes 文章数量
+     * @param $value
+     * @param $data
+     * @return int
+     * @author 段誉
+     * @date 2022/9/15 11:32
+     */
+    public function getAriculturalMachineryCountAttr($value, $data)
+    {
+        return 10;
+//        return Article::where(['cid' => $data['id']])->count('id');
+    }
+
+
+
+
+}