Home.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace app\controller\api;
  3. use app\extra\basic\Base;
  4. use app\extra\tools\CodeExtend;
  5. use app\extra\weMini\Crypt;
  6. use app\middleware\WxMiddleware;
  7. use app\model\saas\SaasShop;
  8. use app\model\saas\SaasUser;
  9. use app\model\saas\SaasUserOpen;
  10. use LinFly\Annotation\Route\Controller;
  11. use LinFly\Annotation\Route\Route;
  12. use support\Request;
  13. use support\Response;
  14. use Webman\Annotation\Middleware;
  15. use Shopwwi\WebmanAuth\Facade\Auth as AuthMode;
  16. #[Controller(prefix: "/wx_api/default"),Middleware(WxMiddleware::class)]
  17. class Home extends Base
  18. {
  19. /**
  20. * 跳出授权
  21. * @var array|string[]
  22. */
  23. protected array $noNeedLogin = ['loginHome'];
  24. /**
  25. * 首页信息
  26. * @return Response
  27. */
  28. #[Route(path: "home",methods: "post")]
  29. public function loginHome(Request $request)
  30. {
  31. try {
  32. $param = $this->_valid([
  33. "shop.require" => "",
  34. "print.require" => "",
  35. "code.require" => "",
  36. "card.default" => ""
  37. ],$request->method());
  38. if (!is_array($param)) return error($param);
  39. $userinfo = (new Crypt([
  40. "appid" => sConf("wechat.mini_appid"),
  41. "appsecret" => sConf("wechat.mini_secret")
  42. ]))->session($param['code']);
  43. if (!isset($userinfo['openid'])) return error("获取数据失败");
  44. $map = ['is_deleted' => 0, 'openid' => $userinfo['openid']];
  45. $user = (new SaasUserOpen)->where($map)->findOrEmpty();
  46. if ($user->isEmpty()) {
  47. $user->insertGetId(['openid' => $userinfo['openid'],"create_ip" => $request->getRealIp(),"nickname" => "微信用户","headimg" => "https://inmei-print.oss-cn-guangzhou.aliyuncs.com/logo.png"]);
  48. $user = (new SaasUserOpen)->where($map)->findOrEmpty();
  49. }
  50. $userAuth = get_object_vars(AuthMode::guard("member")->login($user->toArray()));
  51. $shopData = [];
  52. $cardState = 0;
  53. if (!empty($param['shop']))
  54. {
  55. $shop = (new SaasShop)->where("shop_id", $param['shop'])->field("shop_name,start_at,end_at,vip_end,shop_status,shop_notice,line_time,status,shop_address")->findOrEmpty();
  56. if ($shop->isEmpty()) return error("店铺已被关闭");
  57. $currentTime = time();
  58. $vipEndTime = strtotime($shop['vip_end'] ?? '');
  59. $lineEndTime = strtotime("+10020000 min", strtotime($shop['line_time'] ?? ''));
  60. $today = date('Y-m-d');
  61. $startTime = strtotime("{$today} {$shop['start_at']}");
  62. $endTime = strtotime("{$today} {$shop['end_at']}");
  63. if ($currentTime > $vipEndTime){
  64. $status = ['text' => '已过期', 'val' => 0];
  65. } else if ($shop['status'] == 2) {
  66. $status = ['text' => '已冻结', 'val' => 0];
  67. } else if ($currentTime > $lineEndTime) {
  68. $status = ['text' => '已离线', 'val' => 0];
  69. } else if ($currentTime > $endTime || $currentTime < $startTime) // 过了营业时间
  70. {
  71. $status = ['text' => '休息中', 'val' => 0];
  72. } else {
  73. $status = ['text' => '营业中', 'val' => 1];
  74. }
  75. $shop['status_text'] = $status['text'];
  76. $shop['status_val'] = $status['val'];
  77. $shop['time'] = ($shop['start_at'] == '00:00' && $shop['end_at'] == '23:59')
  78. ? '24小时营业'
  79. : date('H:i', $startTime) . '-' . date('H:i', $endTime);
  80. if ($param['card'] == 1) { // 领取会员卡
  81. $cardInfo = (new SaasUser)->where("shop_id",$param['shop'])->where("openid",$userinfo['openid'])->findOrEmpty();
  82. if ($cardInfo->isEmpty()) {
  83. $cardInfo->insertGetId([
  84. "shop_id" => $param['shop'],
  85. "openid" => $userinfo['openid'],
  86. "card_no" => CodeExtend::uniqidDate(16,"1088")
  87. ]);
  88. $cardState = 1;
  89. }
  90. }
  91. $shopData = $shop->toArray();
  92. }
  93. $share = sConf("wechat.share");
  94. return success("ok",["shop" => $shopData,'user' => ['nickname' => $user['nickname']??'','avatar' => $user['avatar']??''],"token" => $userAuth,"card" => $cardState,"share" => $share]);
  95. }catch (\Throwable $e){
  96. echo $e->getMessage()."\n";
  97. echo $e->getFile()."\n";
  98. echo $e->getLine()."\n";
  99. return error($e->getMessage());
  100. }
  101. }
  102. }