SoftController.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. <?php
  2. namespace App\Http\Home;
  3. use Alipay\EasySDK\Kernel\Factory as Alipay;
  4. use Alipay\EasySDK\Kernel\Util\ResponseChecker;
  5. use App\Mail\SysErrorNotice;
  6. use App\Services\Login\LoginTokenService;
  7. use App\Services\OrderService\ComboOrderService;
  8. use App\Services\OrderService\GoodsOrderService;
  9. use EasyWeChat\Factory;
  10. use Illuminate\Contracts\Foundation\Application;
  11. use Illuminate\Contracts\View\View;
  12. use Illuminate\Support\Facades\Cache;
  13. use Illuminate\Support\Facades\DB;
  14. use Illuminate\Support\Facades\Log;
  15. use Illuminate\Support\Facades\Mail;
  16. use Illuminate\Support\Facades\Request;
  17. use Illuminate\Support\Str;
  18. class SoftController extends HttpBaseController
  19. {
  20. public function __construct()
  21. {
  22. parent::__construct();
  23. $this->setWebsite(__NAMESPACE__);
  24. }
  25. /**
  26. * exe安装包下载
  27. *
  28. * @param string $pmid
  29. * @param string $osPlatform
  30. * @return \Illuminate\Http\RedirectResponse|void
  31. */
  32. public function download(string $pmid, string $osPlatform = 'windows')
  33. {
  34. $isSuffix = Str::endsWith($pmid, '.exe');
  35. if($isSuffix){
  36. $pmid = rtrim($pmid,".exe");
  37. }
  38. //判断该产品是否存在
  39. $productInfo = DB::table('product')->where('mid', $pmid)->where('is_delete', 0)->first();
  40. if (empty($productInfo)) {
  41. die();
  42. }
  43. if (!in_array($osPlatform, ['windows', 'linux', 'android', 'ios'])) {
  44. die();
  45. }
  46. // 获取下载地址,然后跳转
  47. $versionInfo = DB::table('product_version')
  48. ->where('os_type', $osPlatform)
  49. ->where('product_id', $productInfo->id)
  50. ->where('status', 1)
  51. ->where("is_delete", 0)
  52. ->first();
  53. if ($versionInfo) {
  54. return (new \Illuminate\Http\RedirectResponse($versionInfo->download_url));
  55. } else {
  56. die();
  57. }
  58. }
  59. /**
  60. * 压缩包程序
  61. * 下载后的安装包,压缩包程序
  62. *
  63. * @param $osPlatform
  64. * @param $pmid
  65. * @return Application|\Illuminate\Http\RedirectResponse|void
  66. */
  67. public function installPackage($osPlatform, $pmid)
  68. {
  69. //判断该产品是否存在
  70. $productInfo = DB::table('product')->where('mid', $pmid)->where('is_delete', 0)->first();
  71. if (empty($productInfo)) {
  72. Log::error("被攻击了 - 产品不存在" . $pmid);
  73. die();
  74. }
  75. if (!in_array($osPlatform, ['windows', 'linux', 'android', 'ios'])) {
  76. Log::error("被攻击了 - 系统类型不存在" . $osPlatform);
  77. die();
  78. }
  79. $version = Request::input('version');
  80. $agent_mid = Request::input('agent_mid');
  81. // 获取版本信息
  82. $find = DB::table('product_version')
  83. ->select("version", "desc", "download_url", "install_package")
  84. ->where('os_type', $osPlatform)
  85. ->where('product_id', $productInfo->id)
  86. ->where('status', 1)
  87. ->where('is_delete', 0)
  88. ->orderBy('id', "desc");
  89. if ($agent_mid) {
  90. $find->where('agent_mid', $agent_mid);
  91. }
  92. if ($version) {
  93. $find->where("version", ">", $version);
  94. }
  95. $versionInfo = $find->first();
  96. if ($versionInfo) {
  97. return (new \Illuminate\Http\RedirectResponse($versionInfo->install_package));
  98. } else {
  99. Log::error("致命错误 - 安装包不存在" . $pmid);
  100. // 邮件通知管理员
  101. Mail::queue(new SysErrorNotice(__FUNCTION__ . " 致命错误 - 安装包不存在!" . json_encode($productInfo)));
  102. die();
  103. }
  104. }
  105. /**
  106. * 支付页面
  107. *
  108. * @param string $code
  109. * @return Application|\Illuminate\Contracts\View\Factory|View|void
  110. */
  111. public function pay(string $code = "")
  112. {
  113. if (empty($code)) {
  114. // 显示错误页面
  115. return view("home/soft/wechatPay", ['success' => false, 'msg' => '参数错误,请重试! -1']);
  116. }
  117. $info = Cache::get($code);
  118. if (empty($info)) {
  119. return view("home/soft/wechatPay", ['success' => false, 'msg' => '二维码已过期,如有问题,请联系官方客服! - 2']);
  120. }
  121. // 触发,扫描完成机制
  122. Cache::put("pay_scan_" . $code, 1, 2 * 3600);
  123. /**
  124. * 'agent_mid' => $agent_mid,
  125. * 'user_id' => $this->userId,
  126. * 'product_type' => $product_type,
  127. * 'payType' => $payType,
  128. * 'sku' => $sku,
  129. * 'product_id' => $productInfo->id,
  130. */
  131. if ($this->isAlipayClient()) {
  132. return $this->alipay($info, $code);
  133. } else {
  134. return $this->wechatPay($info, $code);
  135. }
  136. }
  137. /**
  138. * 微信支付
  139. */
  140. public function wechatPay($requestData, $code)
  141. {
  142. $type = $requestData['sku'] ?? 4; // 1 月价格 2 半年价格 3 年
  143. // 获取服务商品详情
  144. $goodsInfo = DB::table('product')->find($requestData['product_id']);
  145. if (empty($goodsInfo)) {
  146. return view("home/soft/wechatPay", ['success' => false, 'msg' => '该产品可能已删除,请重试!']);
  147. }
  148. if ($goodsInfo->status == 2) {
  149. return view("home/soft/wechatPay", ['success' => false, 'msg' => '对不起,该产品已下架,不能购买!']);
  150. }
  151. // 获取用户信息
  152. $userInfo = DB::table('user_wechat_official_account')->select("openid")->where('user_id', $requestData['user_id'])->where('is_delete', 0)->first();
  153. if (empty($userInfo)) {
  154. return view("home/soft/wechatPay", ['success' => false, 'msg' => '该用户不存在,可能已删除,请重试!']);
  155. }
  156. // 判断
  157. $orderService = $this->getOrderService($requestData['product_type'], $requestData['user_id']);
  158. $orderInfo = $orderService->getOrderInfo($code, ['user_id' => $requestData['user_id'], 'order_status' => 1]); // 判断该订单号,是否已经生成,为了解决支付时连续扫描多次的问题
  159. if (empty($orderInfo)) {
  160. $orderInfo = $orderService->create($type, $goodsInfo, $code);
  161. if (empty($orderInfo)) {
  162. return view("home/soft/wechatPay", ['success' => false, 'msg' => '对不起,订单不存在或者已删除!']);
  163. }
  164. }
  165. $orderNo = $orderInfo['order_no'];
  166. $config = config('easywechat.pay.default');
  167. $app = Factory::payment($config);
  168. $app->scheme($orderNo);
  169. $jssdk = $app->jssdk;
  170. // 生成支付二维码
  171. $result = $app->order->unify([
  172. 'trade_type' => 'JSAPI',
  173. 'product_id' => $orderNo,
  174. 'body' => '订单号:' . $orderNo,
  175. 'out_trade_no' => $orderNo,
  176. 'total_fee' => $orderInfo['order_amount_total'] * 100,
  177. 'openid' => $userInfo->openid,
  178. ]);
  179. $prepay_id = $result['prepay_id'];
  180. $prepayConfig = $jssdk->sdkConfig($prepay_id);
  181. $configStr = $this->getOfficialAccountConfig();
  182. return view("home/soft/wechatPay", ['success' => true, 'configStr' => $configStr, 'prepayConfig' => $prepayConfig]);
  183. }
  184. /**
  185. * 获取公众号的配置信息
  186. */
  187. private function getOfficialAccountConfig()
  188. {
  189. $config = config('easywechat.official_account.default');
  190. $app = Factory::officialAccount($config);
  191. return $app->jssdk->buildConfig(['chooseWXPay'], false);
  192. }
  193. /**
  194. * 支付宝支付
  195. */
  196. private function alipay($requestData, $code)
  197. {
  198. $type = $requestData['sku'] ?? ''; // 1 月价格 2 半年价格 3 年
  199. // 获取服务商品详情
  200. $goodsInfo = DB::table('product')->find($requestData['product_id']);
  201. if (empty($goodsInfo)) {
  202. return view("home/soft/aliPay", ['success' => false, 'msg' => '该产品可能已删除,请重试!']);
  203. }
  204. if ($goodsInfo->status == 2) {
  205. return view("home/soft/aliPay", ['success' => false, 'msg' => '对不起,该产品已下架,不能购买!']);
  206. }
  207. $orderService = $this->getOrderService($requestData['product_type'], $requestData['user_id']);
  208. $orderInfo = $orderService->getOrderInfo($code, ['user_id' => $requestData['user_id'], 'order_status' => 1]); // 判断该订单号,是否已经生成 为了解决支付时连续扫描多次的问题
  209. if (empty($orderInfo)) {
  210. $orderInfo = $orderService->create($type, $goodsInfo, $code);
  211. if (empty($orderInfo)) {
  212. return view("home/soft/aliPay", ['success' => false, 'msg' => '订单不存在或者已删除!']);
  213. }
  214. }
  215. $order = [
  216. 'out_trade_no' => $orderInfo['order_no'],
  217. 'total_amount' => $orderInfo['order_amount_total'],
  218. 'subject' => '订单号:' . $orderInfo['order_no'],
  219. ];
  220. try {
  221. //1. 设置参数(全局只需设置一次)
  222. Alipay::setOptions($this->getAlipayOptions());
  223. //2. 发起API调用
  224. $result = Alipay::payment()->wap()->pay($order['subject'], $order['out_trade_no'], $order['total_amount'], "", ""); //网页支付
  225. $responseChecker = new ResponseChecker();
  226. //3. 处理响应或异常
  227. if ($responseChecker->success($result)) {
  228. Log::info("调用成功");
  229. Log::info("调用成功", $result->body);
  230. echo $result->body;
  231. die();
  232. } else {
  233. Log::info("调用失败,原因:");
  234. return view("home/soft/aliPay", ['success' => false, 'msg' => "支付失败,请稍后再试!"]);
  235. }
  236. } catch (\Exception $e) {
  237. Log::info("调用失败," . $e->getMessage());
  238. return view("home/soft/aliPay", ['success' => false, 'msg' => $e->getMessage()]);
  239. }
  240. }
  241. /**
  242. * 根据订单号,获取订单服务
  243. *
  244. * @param string $productType
  245. * @param int $userId
  246. * @return ComboOrderService|GoodsOrderService|null
  247. */
  248. public function getOrderService(string $productType, int $userId): GoodsOrderService|ComboOrderService|null
  249. {
  250. if ($productType == 1) {
  251. return new GoodsOrderService($userId);
  252. } else {
  253. return new ComboOrderService($userId);
  254. }
  255. }
  256. /**
  257. * 登录框
  258. */
  259. public function login($pmid)
  260. {
  261. return view("home/soft/login");
  262. }
  263. /**
  264. * 不是会员的提示信息
  265. */
  266. public function reminder($pmid)
  267. {
  268. //判断该产品是否存在
  269. $productInfo = DB::table('product')->where('mid', $pmid)->where('is_delete', 0)->first();
  270. if (empty($productInfo)) {
  271. abort(404);
  272. }
  273. // 根据当前的参数,获取登录信息
  274. $loginToken = new LoginTokenService($this->siteInfo['token_table']);
  275. // token 放到了userAgent里面,后期优化
  276. $tokenInfo = $loginToken->findToken(\Illuminate\Support\Facades\Request::userAgent());
  277. $isVip = false;
  278. if ($tokenInfo && $tokenInfo->user_id) {
  279. $billInfo = $this->getUserBill($productInfo->id, $tokenInfo->user_id);
  280. if (isset($billInfo) && $billInfo['validity_type'] != 0) {
  281. $isVip = true;
  282. }
  283. }
  284. // 获取图片广告
  285. $ad = DB::table("position_list")->where("group_id", 1)->where('tag', 'no_vip_reminder')->first();
  286. // 商品绑定的广告
  287. return view("home/soft/reminder", ["isLogin" => (bool)$tokenInfo, "isVip" => $isVip, "ad" => $ad]);
  288. }
  289. /**
  290. * 商品套餐信息框
  291. *
  292. * @param $pmid
  293. * @return Application|\Illuminate\Contracts\View\Factory|View
  294. */
  295. public function buy($pmid)
  296. {
  297. $agentMid = Request::input('agent_mid');
  298. $token = \Illuminate\Support\Facades\Request::userAgent();
  299. if (empty($token)) {
  300. return view("home/soft/login");
  301. }
  302. //判断该产品是否存在
  303. $productInfo = DB::table('product')->where('mid', $pmid)->where('is_delete', 0)->first();
  304. if (empty($productInfo)) {
  305. abort(404);
  306. }
  307. // 根据当前的参数,获取登录信息
  308. $loginToken = new LoginTokenService($this->siteInfo['token_table']);
  309. // token 放到了userAgent里面,后期优化
  310. $tokenInfo = $loginToken->findToken($token);
  311. if ($tokenInfo) {
  312. $this->userId = $tokenInfo->user_id;
  313. // 获取图片广告
  314. $ad = DB::table("position_list")
  315. ->where("group_id", 2)
  316. ->where('tag', 'buy_box_top_ad')->first();
  317. // 获取用户信息
  318. $userInfo = DB::table('user')->find($this->userId);
  319. unset($userInfo->unionid);
  320. $billInfo = $this->getUserBill($productInfo->id, $this->userId);
  321. return view("home/soft/buy", ["agentMid" => $agentMid, "token" => $token, "billInfo" => $billInfo, "userInfo" => $userInfo, "productInfo" => $productInfo, 'ad' => $ad]);
  322. } else {
  323. return view("home/soft/login");
  324. }
  325. }
  326. ////////////////////////////////////////////////////////
  327. //// electron ////////////////////////////////////////
  328. ////////////////////////////////////////////////////////
  329. public function login2($pmid)
  330. {
  331. return view("home/soft/electron/login");
  332. }
  333. /**
  334. * 不是会员的提示信息
  335. */
  336. public function reminder2($pmid)
  337. {
  338. //判断该产品是否存在
  339. $productInfo = DB::table('product')->where('mid', $pmid)->where('is_delete', 0)->first();
  340. if (empty($productInfo)) {
  341. abort(404);
  342. }
  343. // 根据当前的参数,获取登录信息
  344. $loginToken = new LoginTokenService($this->siteInfo['token_table']);
  345. // token 放到了userAgent里面,后期优化
  346. $tokenInfo = $loginToken->findToken(\Illuminate\Support\Facades\Request::userAgent());
  347. $isVip = false;
  348. if ($tokenInfo && $tokenInfo->user_id) {
  349. $billInfo = $this->getUserBill($productInfo->id, $tokenInfo->user_id);
  350. if (isset($billInfo) && $billInfo['validity_type'] != 0) {
  351. $isVip = true;
  352. }
  353. }
  354. // 获取图片广告
  355. $ad = DB::table("position_list")->where("product_id", $productInfo->id)->where('tag', 'no_vip_reminder')->first();
  356. // 商品绑定的广告
  357. return view("home/soft/electron/reminder", ["isLogin" => (bool)$tokenInfo, "isVip" => $isVip, "ad" => $ad]);
  358. }
  359. /**
  360. * 商品套餐信息框
  361. *
  362. * @param $pmid
  363. * @return Application|\Illuminate\Contracts\View\Factory|View
  364. */
  365. public function buy2($pmid)
  366. {
  367. $agentMid = Request::input('agent_mid');
  368. $token = Request::input('token');
  369. // $token = \Illuminate\Support\Facades\request::header();
  370. // dd($_REQUEST,$_GET,$token, $_COOKIE);
  371. if (empty($token)) {
  372. return view("home/soft/electron/login");
  373. }
  374. //判断该产品是否存在
  375. $productInfo = DB::table('product')->where('mid', $pmid)->where('is_delete', 0)->first();
  376. if (empty($productInfo)) {
  377. abort(404);
  378. }
  379. // 根据当前的参数,获取登录信息
  380. $loginToken = new LoginTokenService($this->siteInfo['token_table']);
  381. // token 放到了userAgent里面,后期优化
  382. $tokenInfo = $loginToken->findToken($token);
  383. if ($tokenInfo) {
  384. $this->userId = $tokenInfo->user_id;
  385. // 获取图片广告
  386. $ad = DB::table("position_list")
  387. ->where("product_id", $productInfo->id)
  388. ->where('tag', 'buy_box_top_ad')->first();
  389. // 获取用户信息
  390. $userInfo = DB::table('user')->find($this->userId);
  391. unset($userInfo->unionid);
  392. $billInfo = $this->getUserBill($productInfo->id, $this->userId);
  393. return view("home/soft/electron/buy", ["agentMid" => $agentMid, "token" => $token, "billInfo" => $billInfo, "userInfo" => $userInfo, "productInfo" => $productInfo, 'ad' => $ad]);
  394. } else {
  395. return view("home/soft/electron/login");
  396. }
  397. }
  398. /**
  399. * 获取购买的产品的账单信息
  400. */
  401. private function getUserBill($productId, $userId)
  402. {
  403. // 获取该产品的支付信息
  404. $validity_type = 0;
  405. $validity_end_time = "";
  406. $billInfo = DB::table("user_buy_bill")->where("product_id", $productId)->where("user_id", $userId)->where("is_delete", 0)->first();
  407. if ($billInfo) {
  408. if ($billInfo->validity_type == 2) {
  409. // 永久有效
  410. $validity_type = 2;
  411. } else if ($billInfo->validity_type == 1) {
  412. // // 时间有效期
  413. $end_time = $billInfo->validity_end_time;
  414. if (time() < $end_time) {
  415. $validity_type = 1;
  416. $validity_end_time = date('Y-m-d H:i:s', $end_time);
  417. }
  418. }
  419. }
  420. return ["validity_type" => $validity_type, "validity_end_time" => $validity_end_time];
  421. }
  422. /**
  423. * 卸载后,优惠弹窗
  424. */
  425. public function uninstallWeb($pmid)
  426. {
  427. //判断该产品是否存在
  428. $productInfo = DB::table('product')->where('mid', $pmid)->where('is_delete', 0)->first();
  429. if (empty($productInfo)) {
  430. abort(404);
  431. }
  432. return view("home/soft/login", ['productInfo' => $productInfo]);
  433. }
  434. }