SupplyDemandLogic.php 14 KB

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