| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475 |
- <?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\{ WechatUserService};
- use app\common\enum\{ PayEnum, 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\agricultural_machinery\ServiceCategory;
- use app\common\model\supply_demand\SupplyDemandCate;
- use app\common\model\ServiceCharge;
- use app\common\model\asset\AssetArea;
- use app\common\model\recharge\RechargeOrder;
- use app\common\model\supply_demand\SupplyDemandInfo;
- use app\common\model\user\{User, UserAuth};
- use think\facade\{Db};
- /**
- * 供应逻辑
- * Class LoginLogic
- * @package app\api\logic
- */
- class SupplyDemandLogic extends BaseLogic
- {
- /**
- * @notes 入驻
- * @param array $params
- * @return bool
- * @author 段誉
- * @date 2022/9/7 15:37
- */
- public static function addSupplyDemand(array $params)
- {
- try {
- SupplyDemandInfo::create([
- 'user_id' => $params['user_id'],
- 'type' => $params['type'] ?? 1,
- 'area_id' => $params['area_id'],
- 'cate_id' => $params['cate_id'],
- 'title' => $params['title'],
- 'mobile' => $params['mobile'],
- 'content' =>$params['content'],
- 'images' => $params['images'],
- 'address' => $params['address'],
- 'number' => $params['number'],
- ]);
- return true;
- } catch (\Exception $e) {
- self::setError($e->getMessage());
- return false;
- }
- }
- public static function delete(array $params){
- try {
- SupplyDemandInfo::destroy($params['id']);
- return true;
- } catch (\Exception $e) {
- self::setError($e->getMessage());
- return false;
- }
- }
- public static function submitSupplyDemand(array $params)
- {
- try {
- $updateWhere['id'] = $params['id'];
- $updatedata['status'] = 1;
- SupplyDemandInfo::where($updateWhere)->update($updatedata);
- return true;
- } catch (\Exception $e) {
- self::setError($e->getMessage());
- return false;
- }
- }
- /**
- * @notes
- * @param $params
- * @return array|false
- * @author 段誉
- * @date 2022/9/6 19:26
- */
- public static function getInfo($params,$user_id)
- {
- try {
- $where = [];
- $where[]=['id','=',$params['id']];
- $supplyDemandInfo = SupplyDemandInfo::where($where) ->append(['type_desc','cateInfo','images','user','area'])->findOrEmpty()->toArray();
- return ['code'=>true,'data'=>$supplyDemandInfo];
- } catch (\Exception $e) {
- self::setError($e->getMessage());
- return ['code'=>false,'data'=>[]];
- }
- }
- /**
- * @notes 地址列表
- * @author 段誉
- * @date 2022/9/16 17:56
- */
- public static function getsAreaLists($get)
- {
- $where=[];
- $where[]=['status','=',1];
- if(isset($get['title'])){
- $where[]=['title','like','%'.$get['title'].'%'];
- }
- $area_list = AssetArea::where($where)->field('id,title')->order('sort desc,id asc')->select()->toArray();
- return $area_list;
- }
- /**
- * @notes 地址列表
- * @author 段誉
- * @date 2022/9/16 17:56
- */
- public static function getsCateLists($get)
- {
- $where=[];
- $where[]=['type','=',$get['type']];
- $where[]=['status','=',1];
- if(isset($get['name'])){
- $where[]=['name','like','%'.$get['name'].'%'];
- }
- $cate_list = ServiceCategory::where($where)->field('id,name')->order('sort desc,id asc')->select()->toArray();
- return $cate_list;
- }
- /**
- * @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
- ]);
- }
- /**
- * @notes 供应分类列表
- * @author 段誉
- * @date 2022/9/16 17:56
- */
- public static function getCateList($get)
- {
- $where=[];
- $where[]=['status','=',1];
- if(isset($get['name'])){
- $where[]=['name','like','%'.$get['name'].'%'];
- }
- $cate_list = SupplyDemandCate::where($where)->field('id,id value,name label,pid,level')->order('sort desc,id asc')->select()->toArray();
- if($get['is_tree'] == 1) {
- $treeList = linear_to_tree($cate_list, 'children');
- if (empty($treeList) && !empty($cate_list)) {
- foreach ($cate_list as &$v) {
- $v['children'] = [];
- }
- return $cate_list;
- }
- return $treeList;
- }else{
- return $cate_list;
- }
- }
- }
|