SupplyDemandLogic.php 12 KB

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