| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336 |
- <?php
- namespace app\controller\wap;
- use app\extra\basic\Base;
- use app\extra\wechat\WechatService;
- use app\middleware\WxMiddleware;
- use app\model\system\SystemConfig;
- use app\model\system\SystemUser;
- use app\model\system\SystemUserOpen;
- use LinFly\Annotation\Route\Controller;
- use LinFly\Annotation\Route\Middleware;
- use LinFly\Annotation\Route\Route;
- use Shopwwi\WebmanAuth\Auth;
- use support\Request;
- use support\Response;
- #[Controller(prefix: "/wap/login"),Middleware(WxMiddleware::class)]
- class Login extends Base
- {
- protected array $noNeedLogin = ["checkLogin","bindLogin"];
- #[Route(path: "check",methods: "post")]
- public function checkLogin(Request $request): Response
- {
- try {
- $postData = $request->all();
- if (empty($postData)) {
- $source = $request->header("referer")."login";
- } else {
- $source = $request->header("referer")."login?".http_build_query($postData);
- }
- $userInfo = WechatService::getWebOauthInfo($source,1,false);
- // $userInfo['openid'] = "otdgy3S5ZzPUD5DYOsHacX5KhZ9I";
- // $userInfo['openid'] = "omf322AlD9wFjm5Ucix9uKmRXd4I";
- if (empty($userInfo['openid'])) {
- return success("ok",['url' => $userInfo['url'],'type' => 1]); // 跳转
- } else {
- $service = (new SystemConfig)->where("type","service")->column("value","name");
- $user = (new SystemUserOpen)->where(['openid' => $userInfo['openid']])->findOrEmpty();
- if ($user->isEmpty())
- {
- return success("ok",['url' => '','shop_name' => "",'type' => 2,'service' => $service,'token' => ['access_token' => $userInfo['openid']]]); // 跳到绑定用户
- }
- $loginUser = (new SystemUser)->where(['id' => $user['uid']])->with(['account' => function($query){
- $query->field("shop_id,shop_name");
- }])->findOrEmpty();
- return success("ok",['url' => '','shop_name' => $loginUser['account']['shop_name'],'type' => 3,'service' => $service,'menu' => $this->getMenu(),'token' => get_object_vars((new Auth)->guard("mp")->login($user->toArray()))]); // 正常登陆进入
- }
- } catch (\Throwable $th) {
- return error($th->getMessage());
- }
- }
- /**
- * @param Request $request
- * @return Response
- */
- #[Route(path: "bind",methods: "post")]
- public function bindLogin(Request $request): Response
- {
- try {
- $param = $this->_valid([
- "username.require" => trans("empty.user"),
- "password.require" => trans("empty.passwd"),
- "open.require" => trans("empty.passwd"),
- ],"post");
- if (!is_array($param)) return error($param);
- $map = ["is_deleted" => 0,"username" => $param['username']];
- $user = (new SystemUser)->where($map)->with(['account' => function($query){
- $query->field("shop_id,shop_name");
- }])->findOrEmpty();
- if ($user->isEmpty()) return errorTrans("error.user-empty");
- if ($user['status'] <> 1) return errorTrans("error.user-status");
- if (md5($param['password'].$user['salt']) <> $user['password']) return errorTrans("error.passwd");
- $userOpen = (new SystemUserOpen)->where(['openid' => $param['open'],'shop_id' => $user['agent_id']])->findOrEmpty();
- if (!$userOpen->isEmpty()) return error("请勿重复授权");
- $openData = [
- "uid" => $user['id'],
- "openid" => $param['open'],
- "shop_id" => $user['agent_id'],
- "shop_name" => $user['account']['shop_name']??'',
- 'is_msg' => 1
- ];
- $state = $userOpen->insertGetId($openData);
- $openData['id'] = $state;
- if (!$state) return errorTrans("error.data");
- return success("ok",['shop_name' => $user['account']['shop_name'],'menu' => $this->getMenu(),'token' => get_object_vars((new Auth)->guard("mp")->login($openData))]); // 正常登陆进入
- } catch (\Throwable $th) {
- return error($th->getMessage());
- }
- }
- /**
- * @return array[]
- */
- protected function getMenu(): array
- {
- return [
- [
- "path" => "/dashboard",
- "name" => "dashboard",
- "component" => "dashboard",
- "meta" => [
- "icon" => "",
- "title" => "总揽",
- "type" => "menu",
- "color" => "#333"
- ],
- "hide" => 1
- ],[
- "path" => "/shop/detail",
- "name" => "shop/detail",
- "component" => "shop/detail",
- "meta" => [
- "icon" => "",
- "title" => "店铺详情",
- "type" => "menu",
- "color" => "#333"
- ],
- "hide" => 1
- ],[
- "path" => "/shop/change",
- "name" => "shop/change",
- "component" => "shop/change",
- "meta" => [
- "icon" => "",
- "title" => "切换店铺",
- "type" => "menu",
- "color" => "#333"
- ],
- "hide" => 1
- ],[
- "path" => "/print",
- "name" => "print/index",
- "component" => "print/index",
- "meta" => [
- "icon" => "el-icon-printer",
- "title" => "打印机",
- "type" => "menu",
- "color" => "#333"
- ],
- "hide" => 0
- ],[
- "path" => "/print/price",
- "name" => "print/price",
- "component" => "print/price",
- "meta" => [
- "icon" => "el-icon-shopping-bag",
- "title" => "价格设置",
- "type" => "menu",
- "color" => "#333"
- ],
- "hide" => 0
- ],[
- "path" => "/print/discount",
- "name" => "print/discount",
- "component" => "print/discount",
- "meta" => [
- "icon" => "el-icon-price-tag",
- "title" => "折扣设置",
- "type" => "menu",
- "color" => "#333"
- ],
- "hide" => 0
- ],[
- "path" => "/order",
- "name" => "order/index",
- "component" => "order/index",
- "meta" => [
- "icon" => "el-icon-tickets",
- "title" => "实时订单",
- "type" => "menu",
- "color" => "#333"
- ],
- "hide" => 0
- ],[
- "path" => "/order/detail",
- "name" => "order/detail",
- "component" => "order/detail",
- "meta" => [
- "icon" => "el-icon-tickets",
- "title" => "订单详情",
- "type" => "menu",
- "color" => "#333"
- ],
- "hide" => 1
- ],[
- "path" => "/turnover/index",
- "name" => "turnover/index",
- "component" => "turnover/index",
- "meta" => [
- "icon" => "el-icon-money",
- "title" => "营业额",
- "type" => "menu",
- "color" => "#333"
- ],
- "hide" => 0
- ],[
- "path" => "/member/index",
- "name" => "member/index",
- "component" => "member/index",
- "meta" => [
- "icon" => "el-icon-credit-card",
- "title" => "会员卡",
- "type" => "menu",
- "color" => "#333"
- ],
- "hide" => 0
- ],[
- "path" => "/member/list",
- "name" => "member/list",
- "component" => "member/list",
- "meta" => [
- "icon" => "el-icon-credit-card",
- "title" => "会员列表",
- "type" => "menu",
- "color" => "#333"
- ],
- "hide" => 1
- ],[
- "path" => "/member/recharge",
- "name" => "member/recharge",
- "component" => "member/recharge",
- "meta" => [
- "icon" => "el-icon-credit-card",
- "title" => "充值套餐",
- "type" => "menu",
- "color" => "#333"
- ],
- "hide" => 1
- ],[
- "path" => "/wallet/index",
- "name" => "wallet/index",
- "component" => "wallet/index",
- "meta" => [
- "icon" => "el-icon-wallet",
- "title" => "我的钱包",
- "type" => "menu",
- "color" => "#333"
- ],
- "hide" => 0
- ],[
- "path" => "/wallet/log",
- "name" => "wallet/log",
- "component" => "wallet/log",
- "meta" => [
- "icon" => "el-icon-wallet",
- "title" => "钱包记录",
- "type" => "menu",
- "color" => "#333"
- ],
- "hide" => 1
- ],[
- "path" => "/wallet/card",
- "name" => "wallet/card",
- "component" => "wallet/card",
- "meta" => [
- "icon" => "el-icon-postcard",
- "title" => "结算卡",
- "type" => "menu",
- "color" => "#333"
- ],
- "hide" => 0
- ],[
- "path" => "/shop/bind",
- "name" => "shop/bind",
- "component" => "shop/bind",
- "meta" => [
- "icon" => "el-icon-connection",
- "title" => "绑定门店",
- "type" => "menu",
- "color" => "#333"
- ],
- "hide" => 0
- ],[
- "path" => "/shop/msg",
- "name" => "shop/msg",
- "component" => "shop/msg",
- "meta" => [
- "icon" => "el-icon-notification",
- "title" => "消息开关",
- "type" => "menu",
- "color" => "#333"
- ],
- "hide" => 0
- ]
- ];
- }
- /**
- * 获取当前请求的完整 URL
- * @param bool $withQuery 是否包含查询参数
- * @return string
- */
- protected function getFullUrl($withQuery = true) {
- // 1. 获取协议(支持代理)
- $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off')
- || (isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == 443)
- ? 'https' : 'http';
- // 2. 获取主机名(支持代理)
- $host = $_SERVER['HTTP_X_FORWARDED_HOST']
- ?? $_SERVER['HTTP_HOST']
- ?? $_SERVER['SERVER_NAME']
- ?? 'localhost';
- // 3. 获取端口(支持代理)
- $port = $_SERVER['HTTP_X_FORWARDED_PORT']
- ?? $_SERVER['SERVER_PORT']
- ?? 80;
- // 4. 获取请求URI(包含路径和查询参数)
- $requestUri = $_SERVER['REQUEST_URI'] ?? '/';
- // 5. 构建基础URL
- $baseUrl = $protocol . '://' . $host;
- // 6. 如果不是标准端口,添加端口号
- $isStandardPort = ($protocol === 'http' && $port == 80)
- || ($protocol === 'https' && $port == 443);
- if (!$isStandardPort) {
- $baseUrl .= ':' . $port;
- }
- // 7. 返回完整URL
- return $baseUrl . $requestUri;
- }
- }
|