IndexController.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <?php
  2. namespace App\Http\Api;
  3. use Illuminate\Support\Facades\DB;
  4. use Illuminate\Support\Facades\Request;
  5. use Illuminate\Support\Str;
  6. use Endroid\QrCode\QrCode;
  7. /**
  8. * 接口,手机端
  9. */
  10. class IndexController extends HttpBaseController
  11. {
  12. public function __construct()
  13. {
  14. parent::__construct();
  15. $this->setWebsite(__NAMESPACE__);
  16. }
  17. /**
  18. * 首页
  19. */
  20. public function index()
  21. {
  22. }
  23. /**
  24. * 创建客服二维码,qt创建二维码太麻烦
  25. */
  26. public function createCustomerQrcode($pmid)
  27. {
  28. // todo 在qml中,暂时无法获取,以后再改进
  29. $token = Request::input("token"); // token
  30. $name = Request::input("name"); // 软件对应的网站名称
  31. $statistics_flag = Request::input("flag");
  32. $product_version = Request::input("version"); // 产品版本
  33. $url = "https://www.qasimblog.com/api/index/toCustomer/" . $pmid . "?" . http_build_query([
  34. "flag" => $statistics_flag,
  35. "version" => $product_version,
  36. "name" => $name,
  37. "token" => $token,
  38. ]);
  39. $qrCode = new QrCode($url);
  40. header('Content-Type: ' . $qrCode->getContentType());
  41. echo $qrCode->writeString();
  42. }
  43. /**
  44. * 企业微信客服跳转
  45. */
  46. public function toCustomer($pmid)
  47. {
  48. // 获取产品信息
  49. return response()->redirectTo("https://work.weixin.qq.com/kfid/kfcbcbd9a513832ff76");
  50. }
  51. /**
  52. * 版本升级检测
  53. */
  54. public function upgrade($osType, $pmid)
  55. {
  56. $version = Request::input('version');
  57. $agent_mid = Request::input('agent_mid');
  58. if (empty($version)) {
  59. return responseMessage(2001, "参数错误,请重试!");
  60. }
  61. if (!in_array($osType, ['windows', 'linux', 'android', 'ios'])) {
  62. return responseMessage(2002, "参数错误,请重试!");
  63. }
  64. //判断该产品是否存在
  65. $productInfo = DB::table('product')->where('mid', $pmid)->where('is_delete', 0)->first();
  66. if (empty($productInfo)) {
  67. return responseMessage(2003, "产品不存在");
  68. }
  69. // 获取版本信息
  70. if ($agent_mid) { // 升级代理商版本
  71. $versionInfo = DB::table('product_version', "download_url")
  72. ->select("version", "desc")
  73. ->where('os_type', $osType)
  74. ->where('product_id', $productInfo->id)
  75. ->where("version", ">", $version)
  76. ->where('agent_mid', $agent_mid)
  77. ->where('status', 1)
  78. ->where('is_delete', 0)
  79. ->orderBy('id', "desc")->first();
  80. if (empty($versionInfo)) {
  81. $versionInfo = DB::table('product_version')
  82. ->select("version", "desc", "download_url")
  83. ->where('os_type', $osType)
  84. ->where('product_id', $productInfo->id)
  85. ->where("version", ">", $version)
  86. ->where('status', 1)
  87. ->where('is_delete', 0)
  88. ->orderBy('id', "desc")->first();
  89. }
  90. } else {
  91. $versionInfo = DB::table('product_version')
  92. ->select("version", "desc", "download_url")
  93. ->where('os_type', $osType)
  94. ->where('product_id', $productInfo->id)
  95. ->where("version", ">", $version)
  96. ->where('status', 1)
  97. ->where('is_delete', 0)
  98. ->orderBy('id', "desc")->first();
  99. }
  100. if ($versionInfo) {
  101. return responseMessage(1001, "", $versionInfo);
  102. } else {
  103. return responseMessage(2004, "当前版本已经是最新版本了!");
  104. }
  105. }
  106. /**
  107. * 软件的安装及操作统计
  108. */
  109. public function statistics($pmid)
  110. {
  111. $this->isLogin();
  112. //判断该产品是否存在
  113. $productInfo = DB::table('product')->where('mid', $pmid)->where('is_delete', 0)->first();
  114. if (empty($productInfo)) {
  115. return responseMessage(2000, "产品不存在");
  116. }
  117. $type = Request::input("type");
  118. $tag = Request::input("tag");
  119. $product_version = Request::input("product_version"); // 产品版本
  120. $windows_uuid = Request::input("windows_uuid");
  121. $os_platform = Request::input("os_platform");
  122. $os_version = Request::input("os_version");
  123. $cpu = Request::input("cpu");
  124. $statistics_flag = Request::input("statistics_flag");
  125. $from = Request::input("from");
  126. if (empty($type)) {
  127. return responseMessage(2001, "参数错误!");
  128. }
  129. if (empty($os_platform)) {
  130. return responseMessage(2002, "参数错误!");
  131. }
  132. if (empty($windows_uuid)) {
  133. return responseMessage(2003, "参数错误!");
  134. }
  135. $data = [
  136. "user_id" => $this->userId,
  137. "product_id" => $productInfo->id,
  138. "product_version" => $product_version,
  139. "type" => $type, // 0 下载 1 安装 2 卸载 3 启动 4 操作
  140. "statistics_flag" => $statistics_flag, // 代理商mid
  141. "tag" => $tag, //点击的标签,可以统
  142. "windows_uuid" => $windows_uuid,
  143. "os_platform" => $os_platform, //windows,linux,android,ios,mac
  144. "os_version" => $os_version, // 系统版本
  145. "cpu" => $cpu, //cpu信息32位,64位
  146. "from" => $from, //refer来源
  147. "ip" => Request::getClientIp(), //IP地址
  148. "year" => date("Y"),
  149. "month" => date("m"),
  150. "day" => date("d"),
  151. 'mid' => Str::random(12),
  152. 'created_at' => time(),
  153. 'updated_at' => time(),
  154. ];
  155. $isSuccess = DB::table("user_use_log")->insert($data);
  156. if ($isSuccess) {
  157. return responseMessage(1001, "");
  158. } else {
  159. return responseMessage(2006, "保存失败");
  160. }
  161. }
  162. /**
  163. * 获取客户端IP地址
  164. * @param int $type 返回类型 0 返回IP地址 1 返回IPV4地址数字
  165. * @param bool $adv 是否进行高级模式获取(有可能被伪装)
  166. * @return mixed
  167. */
  168. /**
  169. * 获取ip
  170. * @return mixed
  171. */
  172. public function getIp()
  173. {
  174. if (isset($_SERVER["HTTP_X_FORWARDED_FOR"])) {
  175. //优先使用 HTTP_X_FORWARDED_FOR,此值是一个逗号分割的多个IP
  176. //注意:我这里没做处理,是因为运维在入口处禁止了伪造请求头,HTTP_X_FORWARDED_FOR是可信的,不能代表所有业务场景
  177. //todo 没有禁止伪造请求头下的特殊处理
  178. $ipStr = $_SERVER["HTTP_X_FORWARDED_FOR"];
  179. $ipArr = explode(',', $ipStr);
  180. $client_ip = $ipArr[0] ?? '';
  181. } else {
  182. $client_ip = $_SERVER["HTTP_CLIENT_IP"] ?? $_SERVER["REMOTE_ADDR"];
  183. }
  184. //过滤无效IP
  185. if (filter_var($client_ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) !== false || filter_var($client_ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) !== false) {
  186. return $client_ip;
  187. } else {
  188. return $_SERVER["REMOTE_ADDR"];
  189. }
  190. }
  191. }