ServiceLogic.php 14 KB

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