SupplyDemandLogic.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeadmin快速开发前后端分离管理后台(PHP版)
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | 开源版本可自由商用,可去除界面版权logo
  7. // | gitee下载:https://gitee.com/likeshop_gitee/likeadmin
  8. // | github下载:https://github.com/likeshop-github/likeadmin
  9. // | 访问官网:https://www.likeadmin.cn
  10. // | likeadmin团队 版权所有 拥有最终解释权
  11. // +----------------------------------------------------------------------
  12. // | author: likeadminTeam
  13. // +----------------------------------------------------------------------
  14. namespace app\api\logic;
  15. use app\common\cache\WebScanLoginCache;
  16. use app\common\logic\BaseLogic;
  17. use app\api\service\{ WechatUserService};
  18. use app\common\enum\{ PayEnum, user\UserTerminalEnum, YesNoEnum};
  19. use app\common\service\{
  20. ConfigService,
  21. FileService,
  22. wechat\WeChatConfigService,
  23. wechat\WeChatMnpService,
  24. wechat\WeChatOaService,
  25. wechat\WeChatRequestService
  26. };
  27. use app\common\model\agricultural_machinery\UserService;
  28. use app\common\model\agricultural_machinery\ServiceCategory;
  29. use app\common\model\supply_demand\SupplyDemandCate;
  30. use app\common\model\ServiceCharge;
  31. use app\common\model\asset\AssetArea;
  32. use app\common\model\recharge\RechargeOrder;
  33. use app\common\model\supply_demand\SupplyDemandInfo;
  34. use app\common\model\user\{User, UserAuth};
  35. use think\facade\{Db};
  36. /**
  37. * 供应逻辑
  38. * Class LoginLogic
  39. * @package app\api\logic
  40. */
  41. class SupplyDemandLogic extends BaseLogic
  42. {
  43. /**
  44. * @notes 入驻
  45. * @param array $params
  46. * @return bool
  47. * @author 段誉
  48. * @date 2022/9/7 15:37
  49. */
  50. public static function addSupplyDemand(array $params)
  51. {
  52. try {
  53. SupplyDemandInfo::create([
  54. 'user_id' => $params['user_id'],
  55. 'type' => $params['type'] ?? 1,
  56. 'cate_id' => $params['cate_id'],
  57. 'title' => $params['title'],
  58. 'mobile' => $params['mobile'],
  59. 'content' =>$params['content'],
  60. 'images' => $params['images'],
  61. 'address' => $params['address'],
  62. 'number' => $params['number'],
  63. ]);
  64. return true;
  65. } catch (\Exception $e) {
  66. self::setError($e->getMessage());
  67. return false;
  68. }
  69. }
  70. /**
  71. * @notes
  72. * @param $params
  73. * @return array|false
  74. * @author 段誉
  75. * @date 2022/9/6 19:26
  76. */
  77. public static function getInfo($params,$user_id)
  78. {
  79. try {
  80. $where = [];
  81. $where[]=['id','=',$params['id']];
  82. $supplyDemandInfo = SupplyDemandInfo::where($where) ->append(['type_desc','cateInfo','images','user'])->findOrEmpty()->toArray();
  83. return ['code'=>true,'data'=>$supplyDemandInfo];
  84. } catch (\Exception $e) {
  85. self::setError($e->getMessage());
  86. return ['code'=>false,'data'=>[]];
  87. }
  88. }
  89. /**
  90. * @notes 地址列表
  91. * @author 段誉
  92. * @date 2022/9/16 17:56
  93. */
  94. public static function getsAreaLists($get)
  95. {
  96. $where=[];
  97. $where[]=['status','=',1];
  98. if(isset($get['title'])){
  99. $where[]=['title','like','%'.$get['title'].'%'];
  100. }
  101. $area_list = AssetArea::where($where)->field('id,title')->order('sort desc,id asc')->select()->toArray();
  102. return $area_list;
  103. }
  104. /**
  105. * @notes 地址列表
  106. * @author 段誉
  107. * @date 2022/9/16 17:56
  108. */
  109. public static function getsCateLists($get)
  110. {
  111. $where=[];
  112. $where[]=['type','=',$get['type']];
  113. $where[]=['status','=',1];
  114. if(isset($get['name'])){
  115. $where[]=['name','like','%'.$get['name'].'%'];
  116. }
  117. $cate_list = ServiceCategory::where($where)->field('id,name')->order('sort desc,id asc')->select()->toArray();
  118. return $cate_list;
  119. }
  120. /**
  121. * @notes 获取微信请求code的链接
  122. * @param string $url
  123. * @return string
  124. * @author 段誉
  125. * @date 2022/9/20 19:47
  126. */
  127. public static function codeUrl(string $url)
  128. {
  129. return (new WeChatOaService())->getCodeUrl($url);
  130. }
  131. /**
  132. * @notes 公众号登录
  133. * @param array $params
  134. * @return array|false
  135. * @throws \GuzzleHttp\Exception\GuzzleException
  136. * @author 段誉
  137. * @date 2022/9/20 19:47
  138. */
  139. public static function oaLogin(array $params)
  140. {
  141. Db::startTrans();
  142. try {
  143. //通过code获取微信 openid
  144. $response = (new WeChatOaService())->getOaResByCode($params['code']);
  145. $userServer = new WechatUserService($response, UserTerminalEnum::WECHAT_OA);
  146. $userInfo = $userServer->getResopnseByUserInfo()->authUserLogin()->getUserInfo();
  147. // 更新登录信息
  148. self::updateLoginInfo($userInfo['id']);
  149. Db::commit();
  150. return $userInfo;
  151. } catch (\Exception $e) {
  152. Db::rollback();
  153. self::$error = $e->getMessage();
  154. return false;
  155. }
  156. }
  157. /**
  158. * @notes 小程序-静默登录
  159. * @param array $params
  160. * @return array|false
  161. * @author 段誉
  162. * @date 2022/9/20 19:47
  163. */
  164. public static function silentLogin(array $params)
  165. {
  166. try {
  167. //通过code获取微信 openid
  168. $response = (new WeChatMnpService())->getMnpResByCode($params['code']);
  169. $userServer = new WechatUserService($response, UserTerminalEnum::WECHAT_MMP);
  170. $userInfo = $userServer->getResopnseByUserInfo('silent')->getUserInfo();
  171. if (!empty($userInfo)) {
  172. // 更新登录信息
  173. self::updateLoginInfo($userInfo['id']);
  174. }
  175. return $userInfo;
  176. } catch (\Exception $e) {
  177. self::$error = $e->getMessage();
  178. return false;
  179. }
  180. }
  181. /**
  182. * @notes 小程序-授权登录
  183. * @param array $params
  184. * @return array|false
  185. * @author 段誉
  186. * @date 2022/9/20 19:47
  187. */
  188. public static function mnpLogin(array $params)
  189. {
  190. Db::startTrans();
  191. try {
  192. //通过code获取微信 openid
  193. $response = (new WeChatMnpService())->getMnpResByCode($params['code']);
  194. $userServer = new WechatUserService($response, UserTerminalEnum::WECHAT_MMP);
  195. $userInfo = $userServer->getResopnseByUserInfo()->authUserLogin()->getUserInfo();
  196. // 更新登录信息
  197. self::updateLoginInfo($userInfo['id']);
  198. Db::commit();
  199. return $userInfo;
  200. } catch (\Exception $e) {
  201. Db::rollback();
  202. self::$error = $e->getMessage();
  203. return false;
  204. }
  205. }
  206. /**
  207. * @notes 更新登录信息
  208. * @param $userId
  209. * @throws \Exception
  210. * @author 段誉
  211. * @date 2022/9/20 19:46
  212. */
  213. public static function updateLoginInfo($userId)
  214. {
  215. $user = User::findOrEmpty($userId);
  216. if ($user->isEmpty()) {
  217. throw new \Exception('用户不存在');
  218. }
  219. $time = time();
  220. $user->login_time = $time;
  221. $user->login_ip = request()->ip();
  222. $user->update_time = $time;
  223. $user->save();
  224. }
  225. /**
  226. * @notes 小程序端绑定微信
  227. * @param array $params
  228. * @return bool
  229. * @author 段誉
  230. * @date 2022/9/20 19:46
  231. */
  232. public static function mnpAuthLogin(array $params)
  233. {
  234. try {
  235. //通过code获取微信openid
  236. $response = (new WeChatMnpService())->getMnpResByCode($params['code']);
  237. $response['user_id'] = $params['user_id'];
  238. $response['terminal'] = UserTerminalEnum::WECHAT_MMP;
  239. return self::createAuth($response);
  240. } catch (\Exception $e) {
  241. self::$error = $e->getMessage();
  242. return false;
  243. }
  244. }
  245. /**
  246. * @notes 公众号端绑定微信
  247. * @param array $params
  248. * @return bool
  249. * @throws \GuzzleHttp\Exception\GuzzleException
  250. * @author 段誉
  251. * @date 2022/9/16 10:43
  252. */
  253. public static function oaAuthLogin(array $params)
  254. {
  255. try {
  256. //通过code获取微信openid
  257. $response = (new WeChatOaService())->getOaResByCode($params['code']);
  258. $response['user_id'] = $params['user_id'];
  259. $response['terminal'] = UserTerminalEnum::WECHAT_OA;
  260. return self::createAuth($response);
  261. } catch (\Exception $e) {
  262. self::$error = $e->getMessage();
  263. return false;
  264. }
  265. }
  266. /**
  267. * @notes 生成授权记录
  268. * @param $response
  269. * @return bool
  270. * @throws \Exception
  271. * @author 段誉
  272. * @date 2022/9/16 10:43
  273. */
  274. public static function createAuth($response)
  275. {
  276. //先检查openid是否有记录
  277. $isAuth = UserAuth::where('openid', '=', $response['openid'])->findOrEmpty();
  278. if (!$isAuth->isEmpty()) {
  279. throw new \Exception('该微信已被绑定');
  280. }
  281. if (isset($response['unionid']) && !empty($response['unionid'])) {
  282. //在用unionid找记录,防止生成两个账号,同个unionid的问题
  283. $userAuth = UserAuth::where(['unionid' => $response['unionid']])
  284. ->findOrEmpty();
  285. if (!$userAuth->isEmpty() && $userAuth->user_id != $response['user_id']) {
  286. throw new \Exception('该微信已被绑定');
  287. }
  288. }
  289. //如果没有授权,直接生成一条微信授权记录
  290. UserAuth::create([
  291. 'user_id' => $response['user_id'],
  292. 'openid' => $response['openid'],
  293. 'unionid' => $response['unionid'] ?? '',
  294. 'terminal' => $response['terminal'],
  295. ]);
  296. return true;
  297. }
  298. /**
  299. * @notes 获取扫码登录地址
  300. * @return array|false
  301. * @author 段誉
  302. * @date 2022/10/20 18:23
  303. */
  304. public static function getScanCode($redirectUri)
  305. {
  306. try {
  307. $config = WeChatConfigService::getOpConfig();
  308. $appId = $config['app_id'];
  309. $redirectUri = UrlEncode($redirectUri);
  310. // 设置有效时间标记状态, 超时扫码不可登录
  311. $state = MD5(time().rand(10000, 99999));
  312. (new WebScanLoginCache())->setScanLoginState($state);
  313. // 扫码地址
  314. $url = WeChatRequestService::getScanCodeUrl($appId, $redirectUri, $state);
  315. return ['url' => $url];
  316. } catch (\Exception $e) {
  317. self::$error = $e->getMessage();
  318. return false;
  319. }
  320. }
  321. /**
  322. * @notes 网站扫码登录
  323. * @param $params
  324. * @return array|false
  325. * @author 段誉
  326. * @date 2022/10/21 10:28
  327. */
  328. public static function scanLogin($params)
  329. {
  330. Db::startTrans();
  331. try {
  332. // 通过code 获取 access_token,openid,unionid等信息
  333. $userAuth = WeChatRequestService::getUserAuthByCode($params['code']);
  334. if (empty($userAuth['openid']) || empty($userAuth['access_token'])) {
  335. throw new \Exception('获取用户授权信息失败');
  336. }
  337. // 获取微信用户信息
  338. $response = WeChatRequestService::getUserInfoByAuth($userAuth['access_token'], $userAuth['openid']);
  339. // 生成用户或更新用户信息
  340. $userServer = new WechatUserService($response, UserTerminalEnum::PC);
  341. $userInfo = $userServer->getResopnseByUserInfo()->authUserLogin()->getUserInfo();
  342. // 更新登录信息
  343. self::updateLoginInfo($userInfo['id']);
  344. Db::commit();
  345. return $userInfo;
  346. } catch (\Exception $e) {
  347. Db::rollback();
  348. self::$error = $e->getMessage();
  349. return false;
  350. }
  351. }
  352. /**
  353. * @notes 更新用户信息
  354. * @param $params
  355. * @param $userId
  356. * @return User
  357. * @author 段誉
  358. * @date 2023/2/22 11:19
  359. */
  360. public static function updateUser($params, $userId)
  361. {
  362. return User::where(['id' => $userId])->update([
  363. 'nickname' => $params['nickname'],
  364. 'avatar' => FileService::setFileUrl($params['avatar']),
  365. 'is_new_user' => YesNoEnum::NO
  366. ]);
  367. }
  368. /**
  369. * @notes 供应分类列表
  370. * @author 段誉
  371. * @date 2022/9/16 17:56
  372. */
  373. public static function getCateList($get)
  374. {
  375. $where=[];
  376. $where[]=['type','=',$get['type']];
  377. $where[]=['status','=',1];
  378. if(isset($get['name'])){
  379. $where[]=['name','like','%'.$get['name'].'%'];
  380. }
  381. $cate_list = SupplyDemandCate::where($where)->field('id,name,pid')->order('sort desc,id asc')->select()->toArray();
  382. if($get['is_tree'] == 1) {
  383. $treeList = linear_to_tree($cate_list, 'children');
  384. if (empty($treeList) && !empty($cate_list)) {
  385. foreach ($cate_list as &$v) {
  386. $v['children'] = [];
  387. }
  388. return $cate_list;
  389. }
  390. return $treeList;
  391. }else{
  392. return $cate_list;
  393. }
  394. }
  395. }