ServiceLogic.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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\{UserTokenService, WechatUserService};
  18. use app\common\enum\{LoginEnum, 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\ServiceCharge;
  29. use app\common\model\recharge\RechargeOrder;
  30. use app\common\model\user\{User, UserAuth};
  31. use think\facade\{Db, Config};
  32. /**
  33. * 服务商逻辑
  34. * Class LoginLogic
  35. * @package app\api\logic
  36. */
  37. class ServiceLogic extends BaseLogic
  38. {
  39. /**
  40. * @notes 入驻
  41. * @param array $params
  42. * @return bool
  43. * @author 段誉
  44. * @date 2022/9/7 15:37
  45. */
  46. public static function Settled(array $params)
  47. {
  48. Db::startTrans();
  49. try {
  50. // $userSn = User::createUserSn();
  51. // $passwordSalt = Config::get('project.unique_identification');
  52. // $password = create_password($params['password'], $passwordSalt);
  53. // $avatar = ConfigService::get('default_image', 'user_avatar');
  54. $result = UserService::create([
  55. 'user_id' => $params['user_id'],
  56. 'type' => $params['type'] ?? 1,
  57. 'name' => $params['name'],
  58. 'mobile' => $params['mobile'],
  59. 'agricultural_machinery_model' =>$params['agricultural_machinery_model'],
  60. 'images' => $params['images'],
  61. 'cate_id' => $params['cate_id'],
  62. 'area_id' => $params['area_id'],
  63. 'money' => $params['money'],
  64. ]);
  65. if($result){
  66. $service_fee = ServiceCharge::find(1);
  67. $order_money = 0;
  68. switch ($params['type']){
  69. case 1 :
  70. $order_money =$service_fee['agricultural_machinery_service_fee'];
  71. break;
  72. case 1 :
  73. $order_money =$service_fee['bake_service_fee'];
  74. break;
  75. case 1 :
  76. $order_money =$service_fee['air_control_service_fee'];
  77. break;
  78. }
  79. $data = [
  80. 'sn' => generate_sn(RechargeOrder::class, 'sn'),
  81. 'user_id' => $params['user_id'],
  82. 'service_id'=>$result['id'],
  83. 'pay_status' => PayEnum::UNPAID,
  84. 'order_amount' => $order_money,
  85. ];
  86. $order = RechargeOrder::create($data);
  87. $order_info = [
  88. 'order_id' => (int)$order['id'],
  89. 'order_sn' => $order['sn'],
  90. 'from' => 'service'
  91. ];
  92. }
  93. Db::commit();
  94. return ['code'=>1,'data'=>$order_info];
  95. } catch (\Exception $e) {
  96. Db::rollback();
  97. self::setError($e->getMessage());
  98. return ['code'=>0,'data'=>[]];
  99. }
  100. }
  101. /**
  102. * @notes 账号/手机号登录,手机号验证码
  103. * @param $params
  104. * @return array|false
  105. * @author 段誉
  106. * @date 2022/9/6 19:26
  107. */
  108. public static function login($params)
  109. {
  110. try {
  111. // 账号/手机号 密码登录
  112. $where = ['account|mobile' => $params['account']];
  113. if ($params['scene'] == LoginEnum::MOBILE_CAPTCHA) {
  114. //手机验证码登录
  115. $where = ['mobile' => $params['account']];
  116. }
  117. $user = User::where($where)->findOrEmpty();
  118. if ($user->isEmpty()) {
  119. throw new \Exception('用户不存在');
  120. }
  121. //更新登录信息
  122. $user->login_time = time();
  123. $user->login_ip = request()->ip();
  124. $user->save();
  125. //设置token
  126. $userInfo = UserTokenService::setToken($user->id, $params['terminal']);
  127. //返回登录信息
  128. $avatar = $user->avatar ?: Config::get('project.default_image.user_avatar');
  129. $avatar = FileService::getFileUrl($avatar);
  130. return [
  131. 'nickname' => $userInfo['nickname'],
  132. 'sn' => $userInfo['sn'],
  133. 'mobile' => $userInfo['mobile'],
  134. 'avatar' => $avatar,
  135. 'token' => $userInfo['token'],
  136. ];
  137. } catch (\Exception $e) {
  138. self::setError($e->getMessage());
  139. return false;
  140. }
  141. }
  142. /**
  143. * @notes 退出登录
  144. * @param $userInfo
  145. * @return bool
  146. * @throws \think\db\exception\DataNotFoundException
  147. * @throws \think\db\exception\DbException
  148. * @throws \think\db\exception\ModelNotFoundException
  149. * @author 段誉
  150. * @date 2022/9/16 17:56
  151. */
  152. public static function logout($userInfo)
  153. {
  154. //token不存在,不注销
  155. if (!isset($userInfo['token'])) {
  156. return false;
  157. }
  158. //设置token过期
  159. return UserTokenService::expireToken($userInfo['token']);
  160. }
  161. /**
  162. * @notes 获取微信请求code的链接
  163. * @param string $url
  164. * @return string
  165. * @author 段誉
  166. * @date 2022/9/20 19:47
  167. */
  168. public static function codeUrl(string $url)
  169. {
  170. return (new WeChatOaService())->getCodeUrl($url);
  171. }
  172. /**
  173. * @notes 公众号登录
  174. * @param array $params
  175. * @return array|false
  176. * @throws \GuzzleHttp\Exception\GuzzleException
  177. * @author 段誉
  178. * @date 2022/9/20 19:47
  179. */
  180. public static function oaLogin(array $params)
  181. {
  182. Db::startTrans();
  183. try {
  184. //通过code获取微信 openid
  185. $response = (new WeChatOaService())->getOaResByCode($params['code']);
  186. $userServer = new WechatUserService($response, UserTerminalEnum::WECHAT_OA);
  187. $userInfo = $userServer->getResopnseByUserInfo()->authUserLogin()->getUserInfo();
  188. // 更新登录信息
  189. self::updateLoginInfo($userInfo['id']);
  190. Db::commit();
  191. return $userInfo;
  192. } catch (\Exception $e) {
  193. Db::rollback();
  194. self::$error = $e->getMessage();
  195. return false;
  196. }
  197. }
  198. /**
  199. * @notes 小程序-静默登录
  200. * @param array $params
  201. * @return array|false
  202. * @author 段誉
  203. * @date 2022/9/20 19:47
  204. */
  205. public static function silentLogin(array $params)
  206. {
  207. try {
  208. //通过code获取微信 openid
  209. $response = (new WeChatMnpService())->getMnpResByCode($params['code']);
  210. $userServer = new WechatUserService($response, UserTerminalEnum::WECHAT_MMP);
  211. $userInfo = $userServer->getResopnseByUserInfo('silent')->getUserInfo();
  212. if (!empty($userInfo)) {
  213. // 更新登录信息
  214. self::updateLoginInfo($userInfo['id']);
  215. }
  216. return $userInfo;
  217. } catch (\Exception $e) {
  218. self::$error = $e->getMessage();
  219. return false;
  220. }
  221. }
  222. /**
  223. * @notes 小程序-授权登录
  224. * @param array $params
  225. * @return array|false
  226. * @author 段誉
  227. * @date 2022/9/20 19:47
  228. */
  229. public static function mnpLogin(array $params)
  230. {
  231. Db::startTrans();
  232. try {
  233. //通过code获取微信 openid
  234. $response = (new WeChatMnpService())->getMnpResByCode($params['code']);
  235. $userServer = new WechatUserService($response, UserTerminalEnum::WECHAT_MMP);
  236. $userInfo = $userServer->getResopnseByUserInfo()->authUserLogin()->getUserInfo();
  237. // 更新登录信息
  238. self::updateLoginInfo($userInfo['id']);
  239. Db::commit();
  240. return $userInfo;
  241. } catch (\Exception $e) {
  242. Db::rollback();
  243. self::$error = $e->getMessage();
  244. return false;
  245. }
  246. }
  247. /**
  248. * @notes 更新登录信息
  249. * @param $userId
  250. * @throws \Exception
  251. * @author 段誉
  252. * @date 2022/9/20 19:46
  253. */
  254. public static function updateLoginInfo($userId)
  255. {
  256. $user = User::findOrEmpty($userId);
  257. if ($user->isEmpty()) {
  258. throw new \Exception('用户不存在');
  259. }
  260. $time = time();
  261. $user->login_time = $time;
  262. $user->login_ip = request()->ip();
  263. $user->update_time = $time;
  264. $user->save();
  265. }
  266. /**
  267. * @notes 小程序端绑定微信
  268. * @param array $params
  269. * @return bool
  270. * @author 段誉
  271. * @date 2022/9/20 19:46
  272. */
  273. public static function mnpAuthLogin(array $params)
  274. {
  275. try {
  276. //通过code获取微信openid
  277. $response = (new WeChatMnpService())->getMnpResByCode($params['code']);
  278. $response['user_id'] = $params['user_id'];
  279. $response['terminal'] = UserTerminalEnum::WECHAT_MMP;
  280. return self::createAuth($response);
  281. } catch (\Exception $e) {
  282. self::$error = $e->getMessage();
  283. return false;
  284. }
  285. }
  286. /**
  287. * @notes 公众号端绑定微信
  288. * @param array $params
  289. * @return bool
  290. * @throws \GuzzleHttp\Exception\GuzzleException
  291. * @author 段誉
  292. * @date 2022/9/16 10:43
  293. */
  294. public static function oaAuthLogin(array $params)
  295. {
  296. try {
  297. //通过code获取微信openid
  298. $response = (new WeChatOaService())->getOaResByCode($params['code']);
  299. $response['user_id'] = $params['user_id'];
  300. $response['terminal'] = UserTerminalEnum::WECHAT_OA;
  301. return self::createAuth($response);
  302. } catch (\Exception $e) {
  303. self::$error = $e->getMessage();
  304. return false;
  305. }
  306. }
  307. /**
  308. * @notes 生成授权记录
  309. * @param $response
  310. * @return bool
  311. * @throws \Exception
  312. * @author 段誉
  313. * @date 2022/9/16 10:43
  314. */
  315. public static function createAuth($response)
  316. {
  317. //先检查openid是否有记录
  318. $isAuth = UserAuth::where('openid', '=', $response['openid'])->findOrEmpty();
  319. if (!$isAuth->isEmpty()) {
  320. throw new \Exception('该微信已被绑定');
  321. }
  322. if (isset($response['unionid']) && !empty($response['unionid'])) {
  323. //在用unionid找记录,防止生成两个账号,同个unionid的问题
  324. $userAuth = UserAuth::where(['unionid' => $response['unionid']])
  325. ->findOrEmpty();
  326. if (!$userAuth->isEmpty() && $userAuth->user_id != $response['user_id']) {
  327. throw new \Exception('该微信已被绑定');
  328. }
  329. }
  330. //如果没有授权,直接生成一条微信授权记录
  331. UserAuth::create([
  332. 'user_id' => $response['user_id'],
  333. 'openid' => $response['openid'],
  334. 'unionid' => $response['unionid'] ?? '',
  335. 'terminal' => $response['terminal'],
  336. ]);
  337. return true;
  338. }
  339. /**
  340. * @notes 获取扫码登录地址
  341. * @return array|false
  342. * @author 段誉
  343. * @date 2022/10/20 18:23
  344. */
  345. public static function getScanCode($redirectUri)
  346. {
  347. try {
  348. $config = WeChatConfigService::getOpConfig();
  349. $appId = $config['app_id'];
  350. $redirectUri = UrlEncode($redirectUri);
  351. // 设置有效时间标记状态, 超时扫码不可登录
  352. $state = MD5(time().rand(10000, 99999));
  353. (new WebScanLoginCache())->setScanLoginState($state);
  354. // 扫码地址
  355. $url = WeChatRequestService::getScanCodeUrl($appId, $redirectUri, $state);
  356. return ['url' => $url];
  357. } catch (\Exception $e) {
  358. self::$error = $e->getMessage();
  359. return false;
  360. }
  361. }
  362. /**
  363. * @notes 网站扫码登录
  364. * @param $params
  365. * @return array|false
  366. * @author 段誉
  367. * @date 2022/10/21 10:28
  368. */
  369. public static function scanLogin($params)
  370. {
  371. Db::startTrans();
  372. try {
  373. // 通过code 获取 access_token,openid,unionid等信息
  374. $userAuth = WeChatRequestService::getUserAuthByCode($params['code']);
  375. if (empty($userAuth['openid']) || empty($userAuth['access_token'])) {
  376. throw new \Exception('获取用户授权信息失败');
  377. }
  378. // 获取微信用户信息
  379. $response = WeChatRequestService::getUserInfoByAuth($userAuth['access_token'], $userAuth['openid']);
  380. // 生成用户或更新用户信息
  381. $userServer = new WechatUserService($response, UserTerminalEnum::PC);
  382. $userInfo = $userServer->getResopnseByUserInfo()->authUserLogin()->getUserInfo();
  383. // 更新登录信息
  384. self::updateLoginInfo($userInfo['id']);
  385. Db::commit();
  386. return $userInfo;
  387. } catch (\Exception $e) {
  388. Db::rollback();
  389. self::$error = $e->getMessage();
  390. return false;
  391. }
  392. }
  393. /**
  394. * @notes 更新用户信息
  395. * @param $params
  396. * @param $userId
  397. * @return User
  398. * @author 段誉
  399. * @date 2023/2/22 11:19
  400. */
  401. public static function updateUser($params, $userId)
  402. {
  403. return User::where(['id' => $userId])->update([
  404. 'nickname' => $params['nickname'],
  405. 'avatar' => FileService::setFileUrl($params['avatar']),
  406. 'is_new_user' => YesNoEnum::NO
  407. ]);
  408. }
  409. }