Login.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. <?php
  2. namespace app\controller\wap;
  3. use app\extra\basic\Base;
  4. use app\extra\wechat\WechatService;
  5. use app\middleware\WxMiddleware;
  6. use app\model\system\SystemConfig;
  7. use app\model\system\SystemUser;
  8. use app\model\system\SystemUserOpen;
  9. use LinFly\Annotation\Route\Controller;
  10. use LinFly\Annotation\Route\Middleware;
  11. use LinFly\Annotation\Route\Route;
  12. use Shopwwi\WebmanAuth\Auth;
  13. use support\Request;
  14. use support\Response;
  15. #[Controller(prefix: "/wap/login"),Middleware(WxMiddleware::class)]
  16. class Login extends Base
  17. {
  18. protected array $noNeedLogin = ["checkLogin","bindLogin"];
  19. #[Route(path: "check",methods: "post")]
  20. public function checkLogin(Request $request): Response
  21. {
  22. try {
  23. $postData = $request->all();
  24. if (empty($postData)) {
  25. $source = $request->header("referer")."login";
  26. } else {
  27. $source = $request->header("referer")."login?".http_build_query($postData);
  28. }
  29. $userInfo = WechatService::getWebOauthInfo($source,1,false);
  30. // $userInfo['openid'] = "otdgy3S5ZzPUD5DYOsHacX5KhZ9I";
  31. // $userInfo['openid'] = "omf322AlD9wFjm5Ucix9uKmRXd4I";
  32. if (empty($userInfo['openid'])) {
  33. return success("ok",['url' => $userInfo['url'],'type' => 1]); // 跳转
  34. } else {
  35. $service = (new SystemConfig)->where("type","service")->column("value","name");
  36. $user = (new SystemUserOpen)->where(['openid' => $userInfo['openid']])->findOrEmpty();
  37. if ($user->isEmpty())
  38. {
  39. return success("ok",['url' => '','shop_name' => "",'type' => 2,'service' => $service,'token' => ['access_token' => $userInfo['openid']]]); // 跳到绑定用户
  40. }
  41. $loginUser = (new SystemUser)->where(['id' => $user['uid']])->with(['account' => function($query){
  42. $query->field("shop_id,shop_name");
  43. }])->findOrEmpty();
  44. 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()))]); // 正常登陆进入
  45. }
  46. } catch (\Throwable $th) {
  47. return error($th->getMessage());
  48. }
  49. }
  50. /**
  51. * @param Request $request
  52. * @return Response
  53. */
  54. #[Route(path: "bind",methods: "post")]
  55. public function bindLogin(Request $request): Response
  56. {
  57. try {
  58. $param = $this->_valid([
  59. "username.require" => trans("empty.user"),
  60. "password.require" => trans("empty.passwd"),
  61. "open.require" => trans("empty.passwd"),
  62. ],"post");
  63. if (!is_array($param)) return error($param);
  64. $map = ["is_deleted" => 0,"username" => $param['username']];
  65. $user = (new SystemUser)->where($map)->with(['account' => function($query){
  66. $query->field("shop_id,shop_name");
  67. }])->findOrEmpty();
  68. if ($user->isEmpty()) return errorTrans("error.user-empty");
  69. if ($user['status'] <> 1) return errorTrans("error.user-status");
  70. if (md5($param['password'].$user['salt']) <> $user['password']) return errorTrans("error.passwd");
  71. $userOpen = (new SystemUserOpen)->where(['openid' => $param['open'],'shop_id' => $user['agent_id']])->findOrEmpty();
  72. if (!$userOpen->isEmpty()) return error("请勿重复授权");
  73. $openData = [
  74. "uid" => $user['id'],
  75. "openid" => $param['open'],
  76. "shop_id" => $user['agent_id'],
  77. "shop_name" => $user['account']['shop_name']??'',
  78. 'is_msg' => 1
  79. ];
  80. $state = $userOpen->insertGetId($openData);
  81. $openData['id'] = $state;
  82. if (!$state) return errorTrans("error.data");
  83. return success("ok",['shop_name' => $user['account']['shop_name'],'menu' => $this->getMenu(),'token' => get_object_vars((new Auth)->guard("mp")->login($openData))]); // 正常登陆进入
  84. } catch (\Throwable $th) {
  85. return error($th->getMessage());
  86. }
  87. }
  88. /**
  89. * @return array[]
  90. */
  91. protected function getMenu(): array
  92. {
  93. return [
  94. [
  95. "path" => "/dashboard",
  96. "name" => "dashboard",
  97. "component" => "dashboard",
  98. "meta" => [
  99. "icon" => "",
  100. "title" => "总揽",
  101. "type" => "menu",
  102. "color" => "#333"
  103. ],
  104. "hide" => 1
  105. ],[
  106. "path" => "/shop/detail",
  107. "name" => "shop/detail",
  108. "component" => "shop/detail",
  109. "meta" => [
  110. "icon" => "",
  111. "title" => "店铺详情",
  112. "type" => "menu",
  113. "color" => "#333"
  114. ],
  115. "hide" => 1
  116. ],[
  117. "path" => "/shop/change",
  118. "name" => "shop/change",
  119. "component" => "shop/change",
  120. "meta" => [
  121. "icon" => "",
  122. "title" => "切换店铺",
  123. "type" => "menu",
  124. "color" => "#333"
  125. ],
  126. "hide" => 1
  127. ],[
  128. "path" => "/print",
  129. "name" => "print/index",
  130. "component" => "print/index",
  131. "meta" => [
  132. "icon" => "el-icon-printer",
  133. "title" => "打印机",
  134. "type" => "menu",
  135. "color" => "#333"
  136. ],
  137. "hide" => 0
  138. ],[
  139. "path" => "/print/price",
  140. "name" => "print/price",
  141. "component" => "print/price",
  142. "meta" => [
  143. "icon" => "el-icon-shopping-bag",
  144. "title" => "价格设置",
  145. "type" => "menu",
  146. "color" => "#333"
  147. ],
  148. "hide" => 0
  149. ],[
  150. "path" => "/print/discount",
  151. "name" => "print/discount",
  152. "component" => "print/discount",
  153. "meta" => [
  154. "icon" => "el-icon-price-tag",
  155. "title" => "折扣设置",
  156. "type" => "menu",
  157. "color" => "#333"
  158. ],
  159. "hide" => 0
  160. ],[
  161. "path" => "/order",
  162. "name" => "order/index",
  163. "component" => "order/index",
  164. "meta" => [
  165. "icon" => "el-icon-tickets",
  166. "title" => "实时订单",
  167. "type" => "menu",
  168. "color" => "#333"
  169. ],
  170. "hide" => 0
  171. ],[
  172. "path" => "/order/detail",
  173. "name" => "order/detail",
  174. "component" => "order/detail",
  175. "meta" => [
  176. "icon" => "el-icon-tickets",
  177. "title" => "订单详情",
  178. "type" => "menu",
  179. "color" => "#333"
  180. ],
  181. "hide" => 1
  182. ],[
  183. "path" => "/turnover/index",
  184. "name" => "turnover/index",
  185. "component" => "turnover/index",
  186. "meta" => [
  187. "icon" => "el-icon-money",
  188. "title" => "营业额",
  189. "type" => "menu",
  190. "color" => "#333"
  191. ],
  192. "hide" => 0
  193. ],[
  194. "path" => "/member/index",
  195. "name" => "member/index",
  196. "component" => "member/index",
  197. "meta" => [
  198. "icon" => "el-icon-credit-card",
  199. "title" => "会员卡",
  200. "type" => "menu",
  201. "color" => "#333"
  202. ],
  203. "hide" => 0
  204. ],[
  205. "path" => "/member/list",
  206. "name" => "member/list",
  207. "component" => "member/list",
  208. "meta" => [
  209. "icon" => "el-icon-credit-card",
  210. "title" => "会员列表",
  211. "type" => "menu",
  212. "color" => "#333"
  213. ],
  214. "hide" => 1
  215. ],[
  216. "path" => "/member/recharge",
  217. "name" => "member/recharge",
  218. "component" => "member/recharge",
  219. "meta" => [
  220. "icon" => "el-icon-credit-card",
  221. "title" => "充值套餐",
  222. "type" => "menu",
  223. "color" => "#333"
  224. ],
  225. "hide" => 1
  226. ],[
  227. "path" => "/wallet/index",
  228. "name" => "wallet/index",
  229. "component" => "wallet/index",
  230. "meta" => [
  231. "icon" => "el-icon-wallet",
  232. "title" => "我的钱包",
  233. "type" => "menu",
  234. "color" => "#333"
  235. ],
  236. "hide" => 0
  237. ],[
  238. "path" => "/wallet/log",
  239. "name" => "wallet/log",
  240. "component" => "wallet/log",
  241. "meta" => [
  242. "icon" => "el-icon-wallet",
  243. "title" => "钱包记录",
  244. "type" => "menu",
  245. "color" => "#333"
  246. ],
  247. "hide" => 1
  248. ],[
  249. "path" => "/wallet/card",
  250. "name" => "wallet/card",
  251. "component" => "wallet/card",
  252. "meta" => [
  253. "icon" => "el-icon-postcard",
  254. "title" => "结算卡",
  255. "type" => "menu",
  256. "color" => "#333"
  257. ],
  258. "hide" => 0
  259. ],[
  260. "path" => "/shop/bind",
  261. "name" => "shop/bind",
  262. "component" => "shop/bind",
  263. "meta" => [
  264. "icon" => "el-icon-connection",
  265. "title" => "绑定门店",
  266. "type" => "menu",
  267. "color" => "#333"
  268. ],
  269. "hide" => 0
  270. ],[
  271. "path" => "/shop/msg",
  272. "name" => "shop/msg",
  273. "component" => "shop/msg",
  274. "meta" => [
  275. "icon" => "el-icon-notification",
  276. "title" => "消息开关",
  277. "type" => "menu",
  278. "color" => "#333"
  279. ],
  280. "hide" => 0
  281. ]
  282. ];
  283. }
  284. /**
  285. * 获取当前请求的完整 URL
  286. * @param bool $withQuery 是否包含查询参数
  287. * @return string
  288. */
  289. protected function getFullUrl($withQuery = true) {
  290. // 1. 获取协议(支持代理)
  291. $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off')
  292. || (isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == 443)
  293. ? 'https' : 'http';
  294. // 2. 获取主机名(支持代理)
  295. $host = $_SERVER['HTTP_X_FORWARDED_HOST']
  296. ?? $_SERVER['HTTP_HOST']
  297. ?? $_SERVER['SERVER_NAME']
  298. ?? 'localhost';
  299. // 3. 获取端口(支持代理)
  300. $port = $_SERVER['HTTP_X_FORWARDED_PORT']
  301. ?? $_SERVER['SERVER_PORT']
  302. ?? 80;
  303. // 4. 获取请求URI(包含路径和查询参数)
  304. $requestUri = $_SERVER['REQUEST_URI'] ?? '/';
  305. // 5. 构建基础URL
  306. $baseUrl = $protocol . '://' . $host;
  307. // 6. 如果不是标准端口,添加端口号
  308. $isStandardPort = ($protocol === 'http' && $port == 80)
  309. || ($protocol === 'https' && $port == 443);
  310. if (!$isStandardPort) {
  311. $baseUrl .= ':' . $port;
  312. }
  313. // 7. 返回完整URL
  314. return $baseUrl . $requestUri;
  315. }
  316. }