| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- <?php
- namespace app\controller\api;
- use app\extra\basic\Base;
- use app\extra\tools\CodeExtend;
- use app\extra\weMini\Crypt;
- use app\middleware\WxMiddleware;
- use app\model\saas\SaasShop;
- use app\model\saas\SaasUser;
- use app\model\saas\SaasUserOpen;
- use LinFly\Annotation\Route\Controller;
- use LinFly\Annotation\Route\Route;
- use support\Request;
- use support\Response;
- use Webman\Annotation\Middleware;
- use Shopwwi\WebmanAuth\Facade\Auth as AuthMode;
- #[Controller(prefix: "/wx_api/default"),Middleware(WxMiddleware::class)]
- class Home extends Base
- {
- /**
- * 跳出授权
- * @var array|string[]
- */
- protected array $noNeedLogin = ['loginHome'];
- /**
- * 首页信息
- * @return Response
- */
- #[Route(path: "home",methods: "post")]
- public function loginHome(Request $request)
- {
- try {
- $param = $this->_valid([
- "shop.require" => "",
- "print.require" => "",
- "code.require" => "",
- "card.default" => ""
- ],$request->method());
- if (!is_array($param)) return error($param);
- $userinfo = (new Crypt([
- "appid" => sConf("wechat.mini_appid"),
- "appsecret" => sConf("wechat.mini_secret")
- ]))->session($param['code']);
- if (!isset($userinfo['openid'])) return error("获取数据失败");
- $map = ['is_deleted' => 0, 'openid' => $userinfo['openid']];
- $user = (new SaasUserOpen)->where($map)->findOrEmpty();
- if ($user->isEmpty()) {
- $user->insertGetId(['openid' => $userinfo['openid'],"create_ip" => $request->getRealIp(),"nickname" => "微信用户","headimg" => "https://inmei-print.oss-cn-guangzhou.aliyuncs.com/logo.png"]);
- $user = (new SaasUserOpen)->where($map)->findOrEmpty();
- }
- $userAuth = get_object_vars(AuthMode::guard("member")->login($user->toArray()));
- $shopData = [];
- $cardState = 0;
- if (!empty($param['shop']))
- {
- $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();
- if ($shop->isEmpty()) return error("店铺已被关闭");
- $currentTime = time();
- $vipEndTime = strtotime($shop['vip_end'] ?? '');
- $lineEndTime = strtotime("+10020000 min", strtotime($shop['line_time'] ?? ''));
- $today = date('Y-m-d');
- $startTime = strtotime("{$today} {$shop['start_at']}");
- $endTime = strtotime("{$today} {$shop['end_at']}");
- if ($currentTime > $vipEndTime){
- $status = ['text' => '已过期', 'val' => 0];
- } else if ($shop['status'] == 2) {
- $status = ['text' => '已冻结', 'val' => 0];
- } else if ($currentTime > $lineEndTime) {
- $status = ['text' => '已离线', 'val' => 0];
- } else if ($currentTime > $endTime || $currentTime < $startTime) // 过了营业时间
- {
- $status = ['text' => '休息中', 'val' => 0];
- } else {
- $status = ['text' => '营业中', 'val' => 1];
- }
- $shop['status_text'] = $status['text'];
- $shop['status_val'] = $status['val'];
- $shop['time'] = ($shop['start_at'] == '00:00' && $shop['end_at'] == '23:59')
- ? '24小时营业'
- : date('H:i', $startTime) . '-' . date('H:i', $endTime);
- if ($param['card'] == 1) { // 领取会员卡
- $cardInfo = (new SaasUser)->where("shop_id",$param['shop'])->where("openid",$userinfo['openid'])->findOrEmpty();
- if ($cardInfo->isEmpty()) {
- $cardInfo->insertGetId([
- "shop_id" => $param['shop'],
- "openid" => $userinfo['openid'],
- "card_no" => CodeExtend::uniqidDate(16,"1088")
- ]);
- $cardState = 1;
- }
- }
- $shopData = $shop->toArray();
- }
- $share = sConf("wechat.share");
- return success("ok",["shop" => $shopData,'user' => ['nickname' => $user['nickname']??'','avatar' => $user['avatar']??''],"token" => $userAuth,"card" => $cardState,"share" => $share]);
- }catch (\Throwable $e){
- echo $e->getMessage()."\n";
- echo $e->getFile()."\n";
- echo $e->getLine()."\n";
- return error($e->getMessage());
- }
- }
- }
|