SoftController.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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. echo $result->body;
  230. die();
  231. } else {
  232. Log::info("调用失败,原因:");
  233. return view("home/soft/aliPay", ['success' => false, 'msg' => "支付失败,请稍后再试!"]);
  234. }
  235. } catch (\Exception $e) {
  236. Log::info("调用失败," . $e->getMessage());
  237. return view("home/soft/aliPay", ['success' => false, 'msg' => $e->getMessage()]);
  238. }
  239. }
  240. /**
  241. * 根据订单号,获取订单服务
  242. *
  243. * @param string $productType
  244. * @param int $userId
  245. * @return ComboOrderService|GoodsOrderService|null
  246. */
  247. public function getOrderService(string $productType, int $userId): GoodsOrderService|ComboOrderService|null
  248. {
  249. if ($productType == 1) {
  250. return new GoodsOrderService($userId);
  251. } else {
  252. return new ComboOrderService($userId);
  253. }
  254. }
  255. /**
  256. * 登录框
  257. */
  258. public function login($pmid)
  259. {
  260. return view("home/soft/login");
  261. }
  262. /**
  263. * 不是会员的提示信息
  264. */
  265. public function reminder($pmid)
  266. {
  267. //判断该产品是否存在
  268. $productInfo = DB::table('product')->where('mid', $pmid)->where('is_delete', 0)->first();
  269. if (empty($productInfo)) {
  270. abort(404);
  271. }
  272. // 根据当前的参数,获取登录信息
  273. $loginToken = new LoginTokenService($this->siteInfo['token_table']);
  274. // token 放到了userAgent里面,后期优化
  275. $tokenInfo = $loginToken->findToken(\Illuminate\Support\Facades\Request::userAgent());
  276. $isVip = false;
  277. if ($tokenInfo && $tokenInfo->user_id) {
  278. $billInfo = $this->getUserBill($productInfo->id, $tokenInfo->user_id);
  279. if (isset($billInfo) && $billInfo['validity_type'] != 0) {
  280. $isVip = true;
  281. }
  282. }
  283. // 获取图片广告
  284. $ad = DB::table("position_list")->where("group_id", 1)->where('tag', 'no_vip_reminder')->first();
  285. // 商品绑定的广告
  286. return view("home/soft/reminder", ["isLogin" => (bool)$tokenInfo, "isVip" => $isVip, "ad" => $ad]);
  287. }
  288. /**
  289. * 商品套餐信息框
  290. *
  291. * @param $pmid
  292. * @return Application|\Illuminate\Contracts\View\Factory|View
  293. */
  294. public function buy($pmid)
  295. {
  296. $agentMid = Request::input('agent_mid');
  297. $token = \Illuminate\Support\Facades\Request::userAgent();
  298. if (empty($token)) {
  299. return view("home/soft/login");
  300. }
  301. //判断该产品是否存在
  302. $productInfo = DB::table('product')->where('mid', $pmid)->where('is_delete', 0)->first();
  303. if (empty($productInfo)) {
  304. abort(404);
  305. }
  306. // 根据当前的参数,获取登录信息
  307. $loginToken = new LoginTokenService($this->siteInfo['token_table']);
  308. // token 放到了userAgent里面,后期优化
  309. $tokenInfo = $loginToken->findToken($token);
  310. if ($tokenInfo) {
  311. $this->userId = $tokenInfo->user_id;
  312. // 获取图片广告
  313. $ad = DB::table("position_list")
  314. ->where("group_id", 2)
  315. ->where('tag', 'buy_box_top_ad')->first();
  316. // 获取用户信息
  317. $userInfo = DB::table('user')->find($this->userId);
  318. unset($userInfo->unionid);
  319. $billInfo = $this->getUserBill($productInfo->id, $this->userId);
  320. return view("home/soft/buy", ["agentMid" => $agentMid, "token" => $token, "billInfo" => $billInfo, "userInfo" => $userInfo, "productInfo" => $productInfo, 'ad' => $ad]);
  321. } else {
  322. return view("home/soft/login");
  323. }
  324. }
  325. ////////////////////////////////////////////////////////
  326. //// electron ////////////////////////////////////////
  327. ////////////////////////////////////////////////////////
  328. public function login2($pmid)
  329. {
  330. return view("home/soft/electron/login");
  331. }
  332. /**
  333. * 不是会员的提示信息
  334. */
  335. public function reminder2($pmid)
  336. {
  337. //判断该产品是否存在
  338. $productInfo = DB::table('product')->where('mid', $pmid)->where('is_delete', 0)->first();
  339. if (empty($productInfo)) {
  340. abort(404);
  341. }
  342. // 根据当前的参数,获取登录信息
  343. $loginToken = new LoginTokenService($this->siteInfo['token_table']);
  344. // token 放到了userAgent里面,后期优化
  345. $tokenInfo = $loginToken->findToken(\Illuminate\Support\Facades\Request::userAgent());
  346. $isVip = false;
  347. if ($tokenInfo && $tokenInfo->user_id) {
  348. $billInfo = $this->getUserBill($productInfo->id, $tokenInfo->user_id);
  349. if (isset($billInfo) && $billInfo['validity_type'] != 0) {
  350. $isVip = true;
  351. }
  352. }
  353. // 获取图片广告
  354. $ad = DB::table("position_list")->where("product_id", $productInfo->id)->where('tag', 'no_vip_reminder')->first();
  355. // 商品绑定的广告
  356. return view("home/soft/electron/reminder", ["isLogin" => (bool)$tokenInfo, "isVip" => $isVip, "ad" => $ad]);
  357. }
  358. /**
  359. * 商品套餐信息框
  360. *
  361. * @param $pmid
  362. * @return Application|\Illuminate\Contracts\View\Factory|View
  363. */
  364. public function buy2($pmid)
  365. {
  366. $agentMid = Request::input('agent_mid');
  367. $token = Request::input('token');
  368. // $token = \Illuminate\Support\Facades\request::header();
  369. // dd($_REQUEST,$_GET,$token, $_COOKIE);
  370. if (empty($token)) {
  371. return view("home/soft/electron/login");
  372. }
  373. //判断该产品是否存在
  374. $productInfo = DB::table('product')->where('mid', $pmid)->where('is_delete', 0)->first();
  375. if (empty($productInfo)) {
  376. abort(404);
  377. }
  378. // 根据当前的参数,获取登录信息
  379. $loginToken = new LoginTokenService($this->siteInfo['token_table']);
  380. // token 放到了userAgent里面,后期优化
  381. $tokenInfo = $loginToken->findToken($token);
  382. if ($tokenInfo) {
  383. $this->userId = $tokenInfo->user_id;
  384. // 获取图片广告
  385. $ad = DB::table("position_list")
  386. ->where("product_id", $productInfo->id)
  387. ->where('tag', 'buy_box_top_ad')->first();
  388. // 获取用户信息
  389. $userInfo = DB::table('user')->find($this->userId);
  390. unset($userInfo->unionid);
  391. $billInfo = $this->getUserBill($productInfo->id, $this->userId);
  392. return view("home/soft/electron/buy", ["agentMid" => $agentMid, "token" => $token, "billInfo" => $billInfo, "userInfo" => $userInfo, "productInfo" => $productInfo, 'ad' => $ad]);
  393. } else {
  394. return view("home/soft/electron/login");
  395. }
  396. }
  397. /**
  398. * 获取购买的产品的账单信息
  399. */
  400. private function getUserBill($productId, $userId)
  401. {
  402. // 获取该产品的支付信息
  403. $validity_type = 0;
  404. $validity_end_time = "";
  405. $billInfo = DB::table("user_buy_bill")->where("product_id", $productId)->where("user_id", $userId)->where("is_delete", 0)->first();
  406. if ($billInfo) {
  407. if ($billInfo->validity_type == 2) {
  408. // 永久有效
  409. $validity_type = 2;
  410. } else if ($billInfo->validity_type == 1) {
  411. // // 时间有效期
  412. $end_time = $billInfo->validity_end_time;
  413. if (time() < $end_time) {
  414. $validity_type = 1;
  415. $validity_end_time = date('Y-m-d H:i:s', $end_time);
  416. }
  417. }
  418. }
  419. return ["validity_type" => $validity_type, "validity_end_time" => $validity_end_time];
  420. }
  421. /**
  422. * 卸载后,优惠弹窗
  423. */
  424. public function uninstallWeb($pmid)
  425. {
  426. //判断该产品是否存在
  427. $productInfo = DB::table('product')->where('mid', $pmid)->where('is_delete', 0)->first();
  428. if (empty($productInfo)) {
  429. abort(404);
  430. }
  431. return view("home/soft/login", ['productInfo' => $productInfo]);
  432. }
  433. }