UtilService.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. namespace liuniu;
  3. use dh2y\qrcode\QRcode;
  4. use think\Request;
  5. class UtilService
  6. {
  7. /**
  8. * 获取POST请求的数据
  9. * @param $params
  10. * @param null $request
  11. * @param bool $suffix
  12. * @return array
  13. */
  14. public static function postMore($params, $request = null, $suffix = false)
  15. {
  16. if ($request === null) $request = Request::instance();
  17. $p = [];
  18. $i = 0;
  19. foreach ($params as $param) {
  20. if (!is_array($param)) {
  21. $p[$suffix == true ? $i++ : $param] = $request->param($param);
  22. } else {
  23. if (!isset($param[1])) $param[1] = null;
  24. if (!isset($param[2])) $param[2] = '';
  25. if (is_array($param[0])) {
  26. $name = is_array($param[1]) ? $param[0][0] . '/a' : $param[0][0] . '/' . $param[0][1];
  27. $keyName = $param[0][0];
  28. } else {
  29. $name = is_array($param[1]) ? $param[0] . '/a' : $param[0];
  30. $keyName = $param[0];
  31. }
  32. $p[$suffix == true ? $i++ : (isset($param[3]) ? $param[3] : $keyName)] = $request->param($name, $param[1], $param[2]);
  33. }
  34. }
  35. return $p;
  36. }
  37. /**
  38. * 获取请求的数据
  39. * @param $params
  40. * @param null $request
  41. * @param bool $suffix
  42. * @return array
  43. */
  44. public static function getMore($params, $request = null, $suffix = false)
  45. {
  46. if ($request === null) $request = Request::instance();
  47. $p = [];
  48. $i = 0;
  49. foreach ($params as $param) {
  50. if (!is_array($param)) {
  51. $p[$suffix == true ? $i++ : $param] = $request->param($param);
  52. } else {
  53. if (!isset($param[1])) $param[1] = null;
  54. if (!isset($param[2])) $param[2] = '';
  55. if (is_array($param[0])) {
  56. $name = is_array($param[1]) ? $param[0][0] . '/a' : $param[0][0] . '/' . $param[0][1];
  57. $keyName = $param[0][0];
  58. } else {
  59. $name = is_array($param[1]) ? $param[0] . '/a' : $param[0];
  60. $keyName = $param[0];
  61. }
  62. $p[$suffix == true ? $i++ : (isset($param[3]) ? $param[3] : $keyName)] = $request->param($name, $param[1], $param[2]);
  63. }
  64. }
  65. return $p;
  66. }
  67. /**
  68. * TODO 获取小程序二维码是否生成
  69. * @param $url
  70. * @return array
  71. */
  72. public static function remoteImage($url)
  73. {
  74. $curl = curl_init();
  75. curl_setopt($curl, CURLOPT_URL, $url);
  76. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  77. $result = curl_exec($curl);
  78. $result = json_decode($result, true);
  79. if (is_array($result)) return ['status' => false, 'msg' => $result['errcode'] . '---' . $result['errmsg']];
  80. return ['status' => true];
  81. }
  82. /**
  83. * TODO 修改 https 和 http 移动到common
  84. * @param $url $url 域名
  85. * @param int $type 0 返回https 1 返回 http
  86. * @return string
  87. */
  88. public static function setHttpType($url, $type = 0)
  89. {
  90. $domainTop = substr($url, 0, 5);
  91. if ($type) {
  92. if ($domainTop == 'https') $url = 'http' . substr($url, 5, strlen($url));
  93. } else {
  94. if ($domainTop != 'https') $url = 'https:' . substr($url, 5, strlen($url));
  95. }
  96. return $url;
  97. }
  98. /**
  99. * 获取二维码
  100. * @param $url
  101. * @param $name
  102. * @return array|bool|string
  103. */
  104. public static function getQRCodePath($url, $name)
  105. {
  106. if (!strlen(trim($url)) || !strlen(trim($name))) return false;
  107. try {
  108. $uploadType = 1;
  109. //TODO 没有选择默认使用本地上传
  110. if (!$uploadType) $uploadType = 1;
  111. $uploadType = (int)$uploadType;
  112. $siteUrl = Request::instance()->domain();
  113. if (!$siteUrl) return '请前往后台设置->系统设置->网站域名 填写您的域名格式为:http://域名';
  114. $info = [];
  115. $outfile = "./uploads/code";
  116. $code = new QRcode();
  117. $wapCodePath = $code->png($url, $outfile . '/' . $name)->getPath(); //获取二维码生成的地址
  118. $content = file_get_contents('.' . $wapCodePath);
  119. if ($uploadType === 1) {
  120. $info["code"] = 200;
  121. $info["name"] = $name;
  122. $info["dir"] = $wapCodePath;
  123. $info["time"] = time();
  124. $info['size'] = 0;
  125. $info['type'] = 'image/png';
  126. $info["image_type"] = 1;
  127. $info['thumb_path'] = $wapCodePath;
  128. return $info;
  129. }
  130. } catch (\Exception $e) {
  131. return $e->getMessage();
  132. }
  133. }
  134. /**
  135. * 获取小程序外连
  136. * @param $cid
  137. * @return mixed
  138. */
  139. public static function getMiniScheme($cid,$param)
  140. {
  141. if (!cache('Scheme_' . $cid)) {
  142. $access = MiniProgramService::getToken($cid);
  143. $url = 'https://api.weixin.qq.com/wxa/generatescheme?access_token=' . $access;
  144. $data = [];
  145. if (isset($param['path']) && $param['path'] != '') $data['jump_wxa']['path'] = $param['path'];
  146. if (isset($param['query']) && $param['query'] != '') $data['jump_wxa']['query'] = $param['query'];
  147. $data['is_expire'] = true;
  148. $data['expire_type'] = 1;
  149. $data['expire_interval'] = 15;
  150. $res = HttpService::postRequest($url, $data);
  151. cache('Scheme_' . $cid,json_decode($res,true));
  152. }
  153. return cache('Scheme_' . $cid);
  154. }
  155. /**
  156. * 获取小程序外连
  157. * @param $cid
  158. * @return mixed
  159. */
  160. public static function getMiniLink($cid,$param)
  161. {
  162. if (!cache('link_' . $cid) || true) {
  163. $access = MiniProgramService::getToken($cid);
  164. $url = 'https://api.weixin.qq.com/wxa/generate_urllink?access_token=' . $access['access_token'];
  165. $data = [];
  166. $data['path'] = $param['path'];
  167. $data['query'] = $param['query'];
  168. $data['is_expire'] = false;
  169. $res = do_request($url,$data,null,true,true);
  170. cache('link_' . $cid,json_decode($res,true));
  171. }
  172. return cache('link_' . $cid);
  173. }
  174. }