| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- <?php
- namespace app\controller\api;
- use app\extra\basic\Base;
- use app\extra\dyLife\Crypt;
- use app\extra\dyLife\data\BaseData;
- use app\extra\tools\UploadExtend;
- use app\middleware\AuthMiddleware;
- use app\model\saas\SaasGoods;
- use app\model\saas\SaasStore;
- use LinFly\Annotation\Attributes\Route\Controller;
- use LinFly\Annotation\Attributes\Route\GetMapping;
- use LinFly\Annotation\Attributes\Route\Middleware;
- use LinFly\Annotation\Attributes\Route\PostMapping;
- use support\Request;
- use support\Response;
- use Webman\RedisQueue\Redis;
- #[Controller("/dy/home"),Middleware(AuthMiddleware::class)]
- class Home extends Base
- {
- /**
- * @var array|string[]
- */
- protected array $noNeedLogin = ["getHomeData","getLicense","payBtnMobile"];
- #[GetMapping("data")]
- public function getHomeData(Request $request): Response
- {
- try {
- $param = $this->_valid([
- "store.require" => trans("empty.require")
- ],$request->method());
- if (!is_array($param)) return error($param);
- $banner = [
- [
- "cover" => "https://washmy.oss-cn-guangzhou.aliyuncs.com/storage/dc7fd761c2e2af9c340f20677f8f115d942f0bde.png"
- ]
- ];
- $store = (new SaasStore)->where("poi_id",$param['store'])->field("poi_name,start_at,end_at,longitude,latitude,service_mobile,poi_id")->findOrEmpty();
- $goods = (new SaasGoods)->where("poi_id",$param['store'])->where("status",1)->field("product_id,product_name,image_list,category,price,line_price,sale_stock,id")->limit(10)->order('id','desc')->select()->toArray();
- return success("ok",compact('banner','store','goods'));
- } catch (\Throwable $th) {
- return error($th->getMessage());
- }
- }
- #[GetMapping("license")]
- public function getLicense(): Response
- {
- try {
- return success("ok",['img' => sConf("service.license")]);
- } catch (\Throwable $throwable) {
- return error($throwable->getMessage());
- }
- }
- /**
- * 三级城市信息
- * @param Request $request
- * @return Response
- */
- #[GetMapping("city")]
- public function getCityJson(Request $request): Response
- {
- try {
- $data = json_decode(file_get_contents(base_path()."/city.json"),true);
- return successTrans("success.data",$data);
- } catch (\Throwable $throwable) {
- return error($throwable->getMessage());
- }
- }
- /**
- * 生服组件解密手机号码
- * @param Request $request
- * @return Response
- */
- #[PostMapping("mobile")]
- public function payBtnMobile(Request $request): Response
- {
- try {
- $param = $request->all();
- $mobile = [];
- if (!empty($param['code'])) {
- $sessionKey = (new Crypt)->config($this->getDyConfig())->token()->getSessionKey($param['code']);
- if (!empty($sessionKey)) {
- $mobileStr = $this->decrypt2code($param['encryptedData'], $sessionKey['session_key'],$param['iv']);
- if (!empty($mobileStr)) $mobile = json_decode($mobileStr,true);
- }
- }
- if (empty($mobile)) return error("获取失败");
- return successTrans("success.data",['mobile' => $mobile['purePhoneNumber']]);
- } catch (\Throwable $throwable) {
- return error($throwable->getMessage());
- }
- }
- /**
- * 解密微信用户手机号等加密数据
- *
- * @param string $encrypted_data Base64 编码的密文
- * @param string $session_key Base64 编码的会话密钥
- * @param string $iv Base64 编码的初始向量
- * @return string 解密后的原始字符串(通常为 JSON)
- * @throws Exception
- */
- protected function decrypt2code($encrypted_data, $session_key, $iv)
- {
- // 1. Base64 解码
- $data = base64_decode($encrypted_data, true);
- $key = base64_decode($session_key, true);
- $iv = base64_decode($iv, true);
- if ($data === false || $key === false || $iv === false) {
- throw new \Exception('Base64 解码失败');
- }
- // 2. 根据密钥长度选择 AES 算法
- $key_len = strlen($key);
- switch ($key_len) {
- case 16:
- $method = 'AES-128-CBC';
- break;
- case 24:
- $method = 'AES-192-CBC';
- break;
- case 32:
- $method = 'AES-256-CBC';
- break;
- default:
- throw new \Exception("无效的密钥长度: {$key_len} 字节");
- }
- // 3. 解密(OPENSSL_RAW_DATA 表示返回二进制数据,不进行 Base64 编码)
- $decrypted = openssl_decrypt($data, $method, $key, OPENSSL_RAW_DATA, $iv);
- if ($decrypted === false) {
- throw new \Exception('解密失败: ' . openssl_error_string());
- }
- return $decrypted;
- }
- }
|