ServiceLogic.php 14 KB

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