SupplyDemandLogic.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  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. 'area_id' => $params['area_id'],
  57. 'cate_id' => $params['cate_id'],
  58. 'title' => $params['title'],
  59. 'mobile' => $params['mobile'],
  60. 'content' =>$params['content'],
  61. 'images' => $params['images'],
  62. 'address' => $params['address'],
  63. 'number' => $params['number'],
  64. ]);
  65. return true;
  66. } catch (\Exception $e) {
  67. self::setError($e->getMessage());
  68. return false;
  69. }
  70. }
  71. public static function delete(array $params){
  72. try {
  73. SupplyDemandInfo::destroy($params['id']);
  74. return true;
  75. } catch (\Exception $e) {
  76. self::setError($e->getMessage());
  77. return false;
  78. }
  79. }
  80. public static function submitSupplyDemand(array $params)
  81. {
  82. try {
  83. $updateWhere['id'] = $params['id'];
  84. $updatedata['status'] = 1;
  85. SupplyDemandInfo::where($updateWhere)->update($updatedata);
  86. return true;
  87. } catch (\Exception $e) {
  88. self::setError($e->getMessage());
  89. return false;
  90. }
  91. }
  92. /**
  93. * @notes
  94. * @param $params
  95. * @return array|false
  96. * @author 段誉
  97. * @date 2022/9/6 19:26
  98. */
  99. public static function getInfo($params,$user_id)
  100. {
  101. try {
  102. $where = [];
  103. $where[]=['id','=',$params['id']];
  104. $supplyDemandInfo = SupplyDemandInfo::where($where) ->append(['type_desc','cateInfo','images','user','area'])->findOrEmpty()->toArray();
  105. return ['code'=>true,'data'=>$supplyDemandInfo];
  106. } catch (\Exception $e) {
  107. self::setError($e->getMessage());
  108. return ['code'=>false,'data'=>[]];
  109. }
  110. }
  111. /**
  112. * @notes 地址列表
  113. * @author 段誉
  114. * @date 2022/9/16 17:56
  115. */
  116. public static function getsAreaLists($get)
  117. {
  118. $where=[];
  119. $where[]=['status','=',1];
  120. if(isset($get['title'])){
  121. $where[]=['title','like','%'.$get['title'].'%'];
  122. }
  123. $area_list = AssetArea::where($where)->field('id,title')->order('sort desc,id asc')->select()->toArray();
  124. return $area_list;
  125. }
  126. /**
  127. * @notes 地址列表
  128. * @author 段誉
  129. * @date 2022/9/16 17:56
  130. */
  131. public static function getsCateLists($get)
  132. {
  133. $where=[];
  134. $where[]=['type','=',$get['type']];
  135. $where[]=['status','=',1];
  136. if(isset($get['name'])){
  137. $where[]=['name','like','%'.$get['name'].'%'];
  138. }
  139. $cate_list = ServiceCategory::where($where)->field('id,name')->order('sort desc,id asc')->select()->toArray();
  140. return $cate_list;
  141. }
  142. /**
  143. * @notes 获取微信请求code的链接
  144. * @param string $url
  145. * @return string
  146. * @author 段誉
  147. * @date 2022/9/20 19:47
  148. */
  149. public static function codeUrl(string $url)
  150. {
  151. return (new WeChatOaService())->getCodeUrl($url);
  152. }
  153. /**
  154. * @notes 公众号登录
  155. * @param array $params
  156. * @return array|false
  157. * @throws \GuzzleHttp\Exception\GuzzleException
  158. * @author 段誉
  159. * @date 2022/9/20 19:47
  160. */
  161. public static function oaLogin(array $params)
  162. {
  163. Db::startTrans();
  164. try {
  165. //通过code获取微信 openid
  166. $response = (new WeChatOaService())->getOaResByCode($params['code']);
  167. $userServer = new WechatUserService($response, UserTerminalEnum::WECHAT_OA);
  168. $userInfo = $userServer->getResopnseByUserInfo()->authUserLogin()->getUserInfo();
  169. // 更新登录信息
  170. self::updateLoginInfo($userInfo['id']);
  171. Db::commit();
  172. return $userInfo;
  173. } catch (\Exception $e) {
  174. Db::rollback();
  175. self::$error = $e->getMessage();
  176. return false;
  177. }
  178. }
  179. /**
  180. * @notes 小程序-静默登录
  181. * @param array $params
  182. * @return array|false
  183. * @author 段誉
  184. * @date 2022/9/20 19:47
  185. */
  186. public static function silentLogin(array $params)
  187. {
  188. try {
  189. //通过code获取微信 openid
  190. $response = (new WeChatMnpService())->getMnpResByCode($params['code']);
  191. $userServer = new WechatUserService($response, UserTerminalEnum::WECHAT_MMP);
  192. $userInfo = $userServer->getResopnseByUserInfo('silent')->getUserInfo();
  193. if (!empty($userInfo)) {
  194. // 更新登录信息
  195. self::updateLoginInfo($userInfo['id']);
  196. }
  197. return $userInfo;
  198. } catch (\Exception $e) {
  199. self::$error = $e->getMessage();
  200. return false;
  201. }
  202. }
  203. /**
  204. * @notes 小程序-授权登录
  205. * @param array $params
  206. * @return array|false
  207. * @author 段誉
  208. * @date 2022/9/20 19:47
  209. */
  210. public static function mnpLogin(array $params)
  211. {
  212. Db::startTrans();
  213. try {
  214. //通过code获取微信 openid
  215. $response = (new WeChatMnpService())->getMnpResByCode($params['code']);
  216. $userServer = new WechatUserService($response, UserTerminalEnum::WECHAT_MMP);
  217. $userInfo = $userServer->getResopnseByUserInfo()->authUserLogin()->getUserInfo();
  218. // 更新登录信息
  219. self::updateLoginInfo($userInfo['id']);
  220. Db::commit();
  221. return $userInfo;
  222. } catch (\Exception $e) {
  223. Db::rollback();
  224. self::$error = $e->getMessage();
  225. return false;
  226. }
  227. }
  228. /**
  229. * @notes 更新登录信息
  230. * @param $userId
  231. * @throws \Exception
  232. * @author 段誉
  233. * @date 2022/9/20 19:46
  234. */
  235. public static function updateLoginInfo($userId)
  236. {
  237. $user = User::findOrEmpty($userId);
  238. if ($user->isEmpty()) {
  239. throw new \Exception('用户不存在');
  240. }
  241. $time = time();
  242. $user->login_time = $time;
  243. $user->login_ip = request()->ip();
  244. $user->update_time = $time;
  245. $user->save();
  246. }
  247. /**
  248. * @notes 小程序端绑定微信
  249. * @param array $params
  250. * @return bool
  251. * @author 段誉
  252. * @date 2022/9/20 19:46
  253. */
  254. public static function mnpAuthLogin(array $params)
  255. {
  256. try {
  257. //通过code获取微信openid
  258. $response = (new WeChatMnpService())->getMnpResByCode($params['code']);
  259. $response['user_id'] = $params['user_id'];
  260. $response['terminal'] = UserTerminalEnum::WECHAT_MMP;
  261. return self::createAuth($response);
  262. } catch (\Exception $e) {
  263. self::$error = $e->getMessage();
  264. return false;
  265. }
  266. }
  267. /**
  268. * @notes 公众号端绑定微信
  269. * @param array $params
  270. * @return bool
  271. * @throws \GuzzleHttp\Exception\GuzzleException
  272. * @author 段誉
  273. * @date 2022/9/16 10:43
  274. */
  275. public static function oaAuthLogin(array $params)
  276. {
  277. try {
  278. //通过code获取微信openid
  279. $response = (new WeChatOaService())->getOaResByCode($params['code']);
  280. $response['user_id'] = $params['user_id'];
  281. $response['terminal'] = UserTerminalEnum::WECHAT_OA;
  282. return self::createAuth($response);
  283. } catch (\Exception $e) {
  284. self::$error = $e->getMessage();
  285. return false;
  286. }
  287. }
  288. /**
  289. * @notes 生成授权记录
  290. * @param $response
  291. * @return bool
  292. * @throws \Exception
  293. * @author 段誉
  294. * @date 2022/9/16 10:43
  295. */
  296. public static function createAuth($response)
  297. {
  298. //先检查openid是否有记录
  299. $isAuth = UserAuth::where('openid', '=', $response['openid'])->findOrEmpty();
  300. if (!$isAuth->isEmpty()) {
  301. throw new \Exception('该微信已被绑定');
  302. }
  303. if (isset($response['unionid']) && !empty($response['unionid'])) {
  304. //在用unionid找记录,防止生成两个账号,同个unionid的问题
  305. $userAuth = UserAuth::where(['unionid' => $response['unionid']])
  306. ->findOrEmpty();
  307. if (!$userAuth->isEmpty() && $userAuth->user_id != $response['user_id']) {
  308. throw new \Exception('该微信已被绑定');
  309. }
  310. }
  311. //如果没有授权,直接生成一条微信授权记录
  312. UserAuth::create([
  313. 'user_id' => $response['user_id'],
  314. 'openid' => $response['openid'],
  315. 'unionid' => $response['unionid'] ?? '',
  316. 'terminal' => $response['terminal'],
  317. ]);
  318. return true;
  319. }
  320. /**
  321. * @notes 获取扫码登录地址
  322. * @return array|false
  323. * @author 段誉
  324. * @date 2022/10/20 18:23
  325. */
  326. public static function getScanCode($redirectUri)
  327. {
  328. try {
  329. $config = WeChatConfigService::getOpConfig();
  330. $appId = $config['app_id'];
  331. $redirectUri = UrlEncode($redirectUri);
  332. // 设置有效时间标记状态, 超时扫码不可登录
  333. $state = MD5(time().rand(10000, 99999));
  334. (new WebScanLoginCache())->setScanLoginState($state);
  335. // 扫码地址
  336. $url = WeChatRequestService::getScanCodeUrl($appId, $redirectUri, $state);
  337. return ['url' => $url];
  338. } catch (\Exception $e) {
  339. self::$error = $e->getMessage();
  340. return false;
  341. }
  342. }
  343. /**
  344. * @notes 网站扫码登录
  345. * @param $params
  346. * @return array|false
  347. * @author 段誉
  348. * @date 2022/10/21 10:28
  349. */
  350. public static function scanLogin($params)
  351. {
  352. Db::startTrans();
  353. try {
  354. // 通过code 获取 access_token,openid,unionid等信息
  355. $userAuth = WeChatRequestService::getUserAuthByCode($params['code']);
  356. if (empty($userAuth['openid']) || empty($userAuth['access_token'])) {
  357. throw new \Exception('获取用户授权信息失败');
  358. }
  359. // 获取微信用户信息
  360. $response = WeChatRequestService::getUserInfoByAuth($userAuth['access_token'], $userAuth['openid']);
  361. // 生成用户或更新用户信息
  362. $userServer = new WechatUserService($response, UserTerminalEnum::PC);
  363. $userInfo = $userServer->getResopnseByUserInfo()->authUserLogin()->getUserInfo();
  364. // 更新登录信息
  365. self::updateLoginInfo($userInfo['id']);
  366. Db::commit();
  367. return $userInfo;
  368. } catch (\Exception $e) {
  369. Db::rollback();
  370. self::$error = $e->getMessage();
  371. return false;
  372. }
  373. }
  374. /**
  375. * @notes 更新用户信息
  376. * @param $params
  377. * @param $userId
  378. * @return User
  379. * @author 段誉
  380. * @date 2023/2/22 11:19
  381. */
  382. public static function updateUser($params, $userId)
  383. {
  384. return User::where(['id' => $userId])->update([
  385. 'nickname' => $params['nickname'],
  386. 'avatar' => FileService::setFileUrl($params['avatar']),
  387. 'is_new_user' => YesNoEnum::NO
  388. ]);
  389. }
  390. /**
  391. * @notes 供应分类列表
  392. * @author 段誉
  393. * @date 2022/9/16 17:56
  394. */
  395. public static function getCateList($get)
  396. {
  397. $where=[];
  398. $where[]=['status','=',1];
  399. if(isset($get['name'])){
  400. $where[]=['name','like','%'.$get['name'].'%'];
  401. }
  402. $cate_list = SupplyDemandCate::where($where)->field('id,id value,name label,pid,level')->order('sort desc,id asc')->select()->toArray();
  403. if($get['is_tree'] == 1) {
  404. $treeList = linear_to_tree($cate_list, 'children');
  405. if (empty($treeList) && !empty($cate_list)) {
  406. foreach ($cate_list as &$v) {
  407. $v['children'] = [];
  408. }
  409. return $cate_list;
  410. }
  411. return $treeList;
  412. }else{
  413. return $cate_list;
  414. }
  415. }
  416. }