SupplyDemandLogic.php 14 KB

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