Helpers.php 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045
  1. <?php
  2. /**
  3. * 获取当前域名
  4. */
  5. function _domain()
  6. {
  7. return str_replace('https://', '', str_replace('http://', '', url('/')));
  8. }
  9. /**
  10. * 数组转xml
  11. */
  12. function arrayToXml($array)
  13. {
  14. $xml = '<xml>';
  15. foreach ($array as $k => $v) {
  16. $xml .= '<' . $k . '><![CDATA[' . $v . ']]></' . $k . '>';
  17. }
  18. $xml .= '</xml>';
  19. return $xml;
  20. }
  21. /**
  22. * 签名生成
  23. */
  24. function _sign($params, $key)
  25. {
  26. $signPars = "";
  27. ksort($params);
  28. foreach ($params as $k => $v) {
  29. if ("" != $v && "sign" != $k) {
  30. $signPars .= $k . "=" . $v . "&";
  31. }
  32. }
  33. $signPars .= "key=" . $key;
  34. return md5($signPars);
  35. }
  36. /**
  37. * 普通对称校验签名
  38. */
  39. function get_sign($params)
  40. {
  41. $url = arr_to_url($params, false);
  42. $url = $url . '&key=' . env('SECRET_KEY');
  43. // v('get_zw_notify_sign_url:'.$url);
  44. $sign = md5($url);
  45. return $sign;
  46. }
  47. function arr_to_url($array, $has_sign = false)
  48. {
  49. ksort($array);
  50. reset($array);
  51. $arg = "";
  52. while (list($name, $val) = each($array)) {
  53. if ($name == 'sign' && !$has_sign) continue;
  54. if (strpos($name, "_") === 0)
  55. continue;
  56. if (is_array($val))
  57. $val = join(',', $val);
  58. if ($val === "")
  59. continue;
  60. $arg .= $name . "=" . $val . "&";
  61. }
  62. $arg = substr($arg, 0, count($arg) - 2);
  63. return $arg;
  64. }
  65. /**
  66. * 获取真实IP
  67. */
  68. function _getIp()
  69. {
  70. if (getenv('HTTP_X_FORWARDED_FOR')) {
  71. $ip = getenv('HTTP_X_FORWARDED_FOR');
  72. } else if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown"))
  73. $ip = getenv("HTTP_CLIENT_IP");
  74. else if (getenv("HTTP_X_FORWARD_FOR") && strcasecmp(getenv("HTTP_X_FORWARD_FOR"), "unknown"))
  75. $ip = getenv("HTTP_X_FORWARD_FOR");
  76. else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown"))
  77. $ip = getenv("REMOTE_ADDR");
  78. else if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown"))
  79. $ip = $_SERVER['REMOTE_ADDR'];
  80. else
  81. $ip = "unknown";
  82. return ($ip);
  83. }
  84. /**
  85. * 数组 转 对象
  86. * @param array $arr 数组
  87. * @return object
  88. */
  89. function array_to_object($arr)
  90. {
  91. if (gettype($arr) != 'array') {
  92. return;
  93. }
  94. foreach ($arr as $k => $v) {
  95. if (gettype($v) == 'array' || getType($v) == 'object') {
  96. $arr[$k] = (object) array_to_object($v);
  97. }
  98. }
  99. return (object) $arr;
  100. }
  101. /**
  102. * 对象 转 数组
  103. * @param object $obj 对象
  104. * @return array
  105. */
  106. function object_to_array($obj)
  107. {
  108. $obj = (array) $obj;
  109. foreach ($obj as $k => $v) {
  110. if (gettype($v) == 'resource') {
  111. return;
  112. }
  113. if (gettype($v) == 'object' || gettype($v) == 'array') {
  114. $obj[$k] = (array) object_to_array($v);
  115. }
  116. }
  117. return $obj;
  118. }
  119. /**
  120. * 检查是否为手机号码
  121. */
  122. function _isPhone($number)
  123. {
  124. return preg_match("/^1[34578][0-9]{9}$/", $number);
  125. }
  126. /**
  127. * 获取渠道域名
  128. */
  129. function get_channel_domain($distribution_channel_id)
  130. {
  131. return env('PROTOCOL') . '://' . env('CHANNEL_SITE_PREFIX') . "{$distribution_channel_id}." . env('CHANNEL_MAIN_DOMAIN');
  132. }
  133. /**
  134. * 判断所传的参数是否缺少,如果缺少返回渠道的字段,正确返回0
  135. * @param array $param
  136. * @param array $must
  137. * @return int|mixed
  138. */
  139. function checkParam(array $param, array $must)
  140. {
  141. foreach ($must as $item) {
  142. if (array_key_exists($item, $param) && $param[$item] != '') {
  143. } else {
  144. return $item;
  145. }
  146. }
  147. return 0;
  148. }
  149. /**
  150. * 获取省份城市列表
  151. */
  152. function _cities()
  153. {
  154. $cities_json = '{"北京市":["北京市"],"天津市":["天津市"],"河北省":["石家庄市","唐山市","秦皇岛市","邯郸市","邢台市","保定市","张家口市","承德市","沧州市","廊坊市","衡水市"],"山西省":["太原市","大同市","阳泉市","长治市","晋城市","朔州市","晋中市","运城市","忻州市","临汾市","吕梁市"],"内蒙古自治区":["呼和浩特市","包头市","乌海市","赤峰市","通辽市","鄂尔多斯市","呼伦贝尔市","巴彦淖尔市","乌兰察布市","兴安盟","锡林郭勒盟","阿拉善盟"],"辽宁省":["沈阳市","大连市","鞍山市","抚顺市","本溪市","丹东市","锦州市","营口市","阜新市","辽阳市","盘锦市","铁岭市","朝阳市","葫芦岛市"],"吉林省":["长春市","吉林市","四平市","辽源市","通化市","白山市","松原市","白城市","延边朝鲜族自治州"],"黑龙江省":["哈尔滨市","齐齐哈尔市","鸡西市","鹤岗市","双鸭山市","大庆市","伊春市","佳木斯市","七台河市","牡丹江市","黑河市","绥化市","大兴安岭地区"],"上海市":["上海市"],"江苏省":["南京市","无锡市","徐州市","常州市","苏州市","南通市","连云港市","淮安市","盐城市","扬州市","镇江市","泰州市","宿迁市"],"浙江省":["杭州市","宁波市","温州市","嘉兴市","湖州市","绍兴市","金华市","衢州市","舟山市","台州市","丽水市"],"安徽省":["合肥市","芜湖市","蚌埠市","淮南市","马鞍山市","淮北市","铜陵市","安庆市","黄山市","滁州市","阜阳市","宿州市","六安市","亳州市","池州市","宣城市"],"福建省":["福州市","厦门市","莆田市","三明市","泉州市","漳州市","南平市","龙岩市","宁德市"],"江西省":["南昌市","景德镇市","萍乡市","九江市","新余市","鹰潭市","赣州市","吉安市","宜春市","抚州市","上饶市"],"山东省":["济南市","青岛市","淄博市","枣庄市","东营市","烟台市","潍坊市","济宁市","泰安市","威海市","日照市","莱芜市","临沂市","德州市","聊城市","滨州市","菏泽市"],"河南省":["郑州市","开封市","洛阳市","平顶山市","安阳市","鹤壁市","新乡市","焦作市","濮阳市","许昌市","漯河市","三门峡市","南阳市","商丘市","信阳市","周口市","驻马店市"],"湖北省":["武汉市","黄石市","十堰市","宜昌市","襄阳市","鄂州市","荆门市","孝感市","荆州市","黄冈市","咸宁市","随州市","恩施土家族苗族自治州"],"湖南省":["长沙市","株洲市","湘潭市","衡阳市","邵阳市","岳阳市","常德市","张家界市","益阳市","郴州市","永州市","怀化市","娄底市","湘西土家族苗族自治州"],"广东省":["广州市","韶关市","深圳市","珠海市","汕头市","佛山市","江门市","湛江市","茂名市","肇庆市","惠州市","梅州市","汕尾市","河源市","阳江市","清远市","东莞市","中山市","潮州市","揭阳市","云浮市"],"广西壮族自治区":["南宁市","柳州市","桂林市","梧州市","北海市","防城港市","钦州市","贵港市","玉林市","百色市","贺州市","河池市","来宾市","崇左市"],"海南省":["海口市","三亚市","三沙市","儋州市"],"重庆市":["重庆市"],"四川省":["成都市","自贡市","攀枝花市","泸州市","德阳市","绵阳市","广元市","遂宁市","内江市","乐山市","南充市","眉山市","宜宾市","广安市","达州市","雅安市","巴中市","资阳市","阿坝藏族羌族自治州","甘孜藏族自治州","凉山彝族自治州"],"贵州省":["贵阳市","六盘水市","遵义市","安顺市","毕节市","铜仁市","黔西南布依族苗族自治州","黔东南苗族侗族自治州","黔南布依族苗族自治州"],"云南省":["昆明市","曲靖市","玉溪市","保山市","昭通市","丽江市","普洱市","临沧市","楚雄彝族自治州","红河哈尼族彝族自治州","文山壮族苗族自治州","西双版纳傣族自治州","大理白族自治州","德宏傣族景颇族自治州","怒江傈僳族自治州","迪庆藏族自治州"],"西藏自治区":["拉萨市","日喀则市","昌都市","林芝市","山南市","那曲地区","阿里地区"],"陕西省":["西安市","铜川市","宝鸡市","咸阳市","渭南市","延安市","汉中市","榆林市","安康市","商洛市"],"甘肃省":["兰州市","嘉峪关市","金昌市","白银市","天水市","武威市","张掖市","平凉市","酒泉市","庆阳市","定西市","陇南市","临夏回族自治州","甘南藏族自治州"],"青海省":["西宁市","海东市","海北藏族自治州","黄南藏族自治州","海南藏族自治州","果洛藏族自治州","玉树藏族自治州","海西蒙古族藏族自治州"],"宁夏回族自治区":["银川市","石嘴山市","吴忠市","固原市","中卫市"],"新疆维吾尔自治区":["乌鲁木齐市","克拉玛依市","吐鲁番市","哈密市","昌吉回族自治州","博尔塔拉蒙古自治州","巴音郭楞蒙古自治州","阿克苏地区","克孜勒苏柯尔克孜自治州","喀什地区","和田地区","伊犁哈萨克自治州","塔城地区","阿勒泰地区"],"台湾省":[],"香港特别行政区":[],"澳门特别行政区":[]}';
  155. return json_decode($cities_json, 1);
  156. }
  157. /**
  158. * 对象 转 数组
  159. * @param object $obj 对象
  160. * @return array
  161. */
  162. function ignoreKeyInArray($targetArray, $delete_keys = [], $changes = [])
  163. {
  164. $change_keys = array_keys($changes);
  165. foreach ($targetArray as $key => $value) {
  166. if (in_array($key, $delete_keys) && isset($targetArray[$key])) unset($targetArray[$key]);
  167. if (in_array($key, $change_keys) && isset($targetArray[$key])) $targetArray[$key] = $changes[$key];
  168. if (is_array($value)) ignoreKeyInArray($value, $delete_keys, $change_keys);
  169. }
  170. return $targetArray;
  171. }
  172. function itemTransform($trans, $data)
  173. {
  174. if ($data) {
  175. return $trans->transform($data);
  176. } else {
  177. return [];
  178. }
  179. }
  180. function collectionTransform($trans, $data)
  181. {
  182. $ret_data = [];
  183. if ($data) {
  184. foreach ($data as $item) {
  185. $ret_data[] = $trans->transform($item);
  186. }
  187. }
  188. return $ret_data;
  189. }
  190. function paginationTransform($trans, $paginator)
  191. {
  192. $ret = [];
  193. $ret['list'] = [];
  194. if ($paginator) {
  195. foreach ($paginator as $item) {
  196. $ret['list'][] = $trans->transform($item);
  197. }
  198. $ret['meta'] = [
  199. 'total' => (int) $paginator->total(),
  200. 'per_page' => (int) $paginator->perPage(),
  201. 'current_page' => (int) $paginator->currentPage(),
  202. 'last_page' => (int) $paginator->lastPage(),
  203. 'next_page_url' => (string) $paginator->nextPageUrl(),
  204. 'prev_page_url' => (string) $paginator->previousPageUrl()
  205. ];
  206. }
  207. return $ret;
  208. }
  209. function ImageNewsToArray($datas)
  210. {
  211. if (empty($datas)) return null;
  212. if (!is_array($datas)) {
  213. $datas = json_decode($datas);
  214. }
  215. $send_data = array();
  216. foreach ($datas as $no => $data) {
  217. foreach ($data as $_data) {
  218. foreach ($_data as $key => $one_data) {
  219. $send_data[$no][$key] = $one_data;
  220. }
  221. }
  222. }
  223. return $send_data;
  224. }
  225. /**
  226. * 加密site id
  227. */
  228. function encodeDistributionChannelId($id)
  229. {
  230. $encrypt_pool = ['14' => 'xyvz5mexll52mzn4', '13' => 'laosiji', '4372' => 'qhyeyue', '365' => 'vciam5tg71', '384' => 'sdxisd', '5795' => 'gyp23fzu'];
  231. if (isset($encrypt_pool[$id])) {
  232. return $encrypt_pool[$id];
  233. }
  234. /*$db_encrypt_info = \DB::table('distribution_channel_id_encrypt')
  235. ->where('distribution_channel_id',$id)
  236. ->where('is_enable',1)
  237. ->select('en_distribution_channel_id')
  238. ->first();
  239. if($db_encrypt_info && $db_encrypt_info->en_distribution_channel_id){
  240. return $db_encrypt_info->en_distribution_channel_id;
  241. }*/
  242. $hashids = new \Hashids\Hashids('', 16, 'abcdefghjklmnopqrstuvwxyz1234567890');
  243. return $hashids->encode($id);
  244. }
  245. /**
  246. * 解密密site id
  247. */
  248. function decodeDistributionChannelId($code)
  249. {
  250. $encrypt_pool = ['xyvz5mexll52mzn4' => '14', 'laosiji' => '13', 'qhyeyue' => '4372', 'vciam5tg71' => '365', 'sdxisd' => '384', 'gyp23fzu' => '5795'];
  251. if (isset($encrypt_pool[$code])) {
  252. return $encrypt_pool[$code];
  253. }
  254. /*$db_encrypt_info = \DB::table('distribution_channel_id_encrypt')
  255. ->where('en_distribution_channel_id',$code)
  256. ->where('is_enable',1)
  257. ->select('distribution_channel_id')
  258. ->first();
  259. if($db_encrypt_info && $db_encrypt_info->distribution_channel_id){
  260. return $db_encrypt_info->distribution_channel_id;
  261. }*/
  262. $hashids = new \Hashids\Hashids('', 16, 'abcdefghjklmnopqrstuvwxyz1234567890');
  263. $res = $hashids->decode($code);
  264. if ($res && isset($res[0])) {
  265. return $res[0];
  266. }
  267. return null;
  268. }
  269. //bid加密
  270. function book_hash_encode($bid)
  271. {
  272. return Vinkla\Hashids\Facades\Hashids::encode($bid);
  273. }
  274. //bid解密
  275. function book_hash_decode($encrypt_bid)
  276. {
  277. return Vinkla\Hashids\Facades\Hashids::decode($encrypt_bid);
  278. }
  279. /**
  280. * 保存Excel格式数据表 需要composer require phpoffice/phpspreadsheet
  281. * @param $header
  282. * @param $data
  283. * @param $path eg. storage_path('app/excel.xlsx')
  284. * @param $remark array $remark['cell']备注单元格 ¥remark['contents']备注内容
  285. */
  286. function saveExcelData($header, $data, $path, $remark = '')
  287. {
  288. $objPHPExcel = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
  289. // Set document properties
  290. // Rename worksheet
  291. $objPHPExcel->getActiveSheet()->setTitle('Phpmarker-' . date('Y-m-d'));
  292. // Set active sheet index to the first sheet, so Excel opens this as the first sheet
  293. $objPHPExcel->setActiveSheetIndex(0);
  294. //设置header
  295. if (!is_array($header)) {
  296. return false;
  297. }
  298. $ini = 65; //A的acsii码
  299. foreach ($header as $value) {
  300. $objPHPExcel->getActiveSheet()->getColumnDimension(chr($ini))->setAutoSize(true);
  301. $objPHPExcel->getActiveSheet()->setCellValue(chr($ini++) . '1', $value);
  302. }
  303. if (!is_array($data)) {
  304. return false;
  305. }
  306. $i = 2;
  307. foreach ($data as $val) {
  308. $ini = 65;
  309. foreach ($val as $item) {
  310. $objPHPExcel->getActiveSheet()->setCellValue(chr($ini++) . $i, $item);
  311. }
  312. ++$i;
  313. }
  314. if ($remark) {
  315. $conditional1 = new \PhpOffice\PhpSpreadsheet\Style\Conditional();
  316. $conditional1->setConditionType(\PhpOffice\PhpSpreadsheet\Style\Conditional::CONDITION_CELLIS)
  317. ->setOperatorType(\PhpOffice\PhpSpreadsheet\Style\Conditional::OPERATOR_BETWEEN)
  318. ->addCondition('200')
  319. ->addCondition('400');
  320. $conditional1->getStyle()->getFont()->getColor()->setARGB(\PhpOffice\PhpSpreadsheet\Style\Color::COLOR_YELLOW);
  321. foreach ($remark as $key => $value) {
  322. $conditionalStyles = $objPHPExcel->getActiveSheet()->getStyle($value['cell'])->getConditionalStyles();
  323. $conditionalStyles[] = $conditional1;
  324. $objPHPExcel->getActiveSheet()->getStyle($value['cell'])->setConditionalStyles($conditionalStyles);
  325. $objPHPExcel->getActiveSheet()->setCellValue($value['cell'], $value['contents']);
  326. }
  327. }
  328. if (!$path) {
  329. // Redirect output to a client’s web browser (Xlsx)
  330. header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  331. header('Content-Disposition: attachment;filename="01simple.xlsx"');
  332. header('Cache-Control: max-age=0');
  333. // If you're serving to IE 9, then the following may be needed
  334. header('Cache-Control: max-age=1');
  335. // If you're serving to IE over SSL, then the following may be needed
  336. header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  337. header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
  338. header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
  339. header('Pragma: public'); // HTTP/1.0
  340. $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($objPHPExcel, 'Xlsx');
  341. $writer->save('php://output');
  342. //return true;
  343. } else {
  344. $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($objPHPExcel, 'Xlsx');
  345. $writer->save($path);
  346. }
  347. }
  348. function saveExcelDataToMultiSheet(array $param, $path)
  349. {
  350. $sheet_num = count($param);
  351. //\Log::info($sheet_num);
  352. //\Log::info($param);
  353. $objPHPExcel = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
  354. $new_sheet = null;
  355. for ($j = 0; $j < $sheet_num; $j++) {
  356. // Set active sheet index to the first sheet, so Excel opens this as the first sheet
  357. if ($j > 0) {
  358. \Log::info('created_sheet');
  359. $objPHPExcel->createSheet($j);
  360. //\Log::info($new_sheet);
  361. }
  362. $objPHPExcel->setActiveSheetIndex($j);
  363. // Rename worksheet
  364. $objPHPExcel->getActiveSheet()->setTitle($param[$j]['title']);
  365. //设置header
  366. if (!is_array($param[$j]['header'])) {
  367. return false;
  368. }
  369. $header = $param[$j]['header'];
  370. $ini = 65; //A的acsii码
  371. foreach ($header as $value) {
  372. $objPHPExcel->getActiveSheet()->getColumnDimension(chr($ini))->setAutoSize(true);
  373. $objPHPExcel->getActiveSheet()->setCellValue(chr($ini++) . '1', $value);
  374. }
  375. if (!is_array($param[$j]['data'])) {
  376. return false;
  377. }
  378. $data = $param[$j]['data'];
  379. $i = 2;
  380. foreach ($data as $val) {
  381. $ini = 65;
  382. foreach ($val as $item) {
  383. $objPHPExcel->getActiveSheet()->setCellValue(chr($ini++) . $i, $item);
  384. }
  385. ++$i;
  386. }
  387. }
  388. $writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($objPHPExcel, 'Xlsx');
  389. $writer->save($path);
  390. }
  391. function myLog($name, $filename = '')
  392. {
  393. if (!$filename) {
  394. $filename = $name;
  395. }
  396. $filename = $filename . '.log';
  397. $logger = new \Monolog\Logger($name);
  398. $writer = new \Illuminate\Log\Writer($logger);
  399. $writer->useDailyFiles(storage_path('logs/' . $filename),3);
  400. return $writer;
  401. }
  402. function redisEnv($key, $default = '')
  403. {
  404. static $result = [];
  405. if (isset($result[$key])) return $result[$key];
  406. $value = \Redis::hget('env', $key);
  407. if ($value) {
  408. $result[$key] = $value;
  409. return $value;
  410. }
  411. return $default;
  412. }
  413. function redisEnvMulti(...$key)
  414. {
  415. $value = \Redis::hmget('env', $key);
  416. return $value;
  417. }
  418. function get_client_ip($type = 0, $adv = false)
  419. {
  420. $type = $type ? 1 : 0;
  421. static $ip = null;
  422. if (null !== $ip) {
  423. return $ip[$type];
  424. }
  425. if ($adv) {
  426. if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  427. $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
  428. $pos = array_search('unknown', $arr);
  429. if (false !== $pos) {
  430. unset($arr[$pos]);
  431. }
  432. $ip = trim($arr[0]);
  433. } elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
  434. $ip = $_SERVER['HTTP_CLIENT_IP'];
  435. } elseif (isset($_SERVER['REMOTE_ADDR'])) {
  436. $ip = $_SERVER['REMOTE_ADDR'];
  437. }
  438. } elseif (isset($_SERVER['REMOTE_ADDR'])) {
  439. $ip = $_SERVER['REMOTE_ADDR'];
  440. }
  441. // IP地址合法验证
  442. $long = sprintf("%u", ip2long($ip));
  443. $ip = $long ? array($ip, $long) : array('0.0.0.0', 0);
  444. return $ip[$type];
  445. }
  446. //授权两次
  447. function specialChannelAuthInfo()
  448. {
  449. return [
  450. '4404' => 'wx1d618e3cf988e2e6',
  451. '4395' => 'wx5135dcdbb6043872',
  452. '4396' => 'wx8f365ca550b8bf3c',
  453. '4397' => 'wx83b5939831c13515',
  454. '4398' => 'wx50dd61b654bbbb9e',
  455. '4399' => 'wxe01b0f5e3a456d94',
  456. '4400' => 'wxb98203d36e526fcb',
  457. '4401' => 'wx4badede056f2e776',
  458. '4402' => 'wx9a941e981acdeb74',
  459. '4403' => 'wxef3feefc54f1c6a1',
  460. '4405' => 'wx2f2c0b2faa0978c9',
  461. '4406' => 'wx9e911047fc2554d0',
  462. '4413' => 'wx6e662e3154cc49bd',
  463. '4414' => 'wx2983fdb0e80431ba',
  464. '4415' => 'wx4876d3c9ccd07ad6',
  465. '4416' => 'wx7e51e2825f5aad16',
  466. //'4433'=>'wx274eb60e7a1fc953',
  467. //'4434'=>'wx7e06ff61885e50bb',
  468. '4435' => 'wxe3035fd2e77b7cc8',
  469. '4436' => 'wx79dbfc1ed7397c28',
  470. //'4438'=>'wx7af4f5ee84c8b097',
  471. '4439' => 'wx0789cce3a1699ce8',
  472. //'4440'=>'wx7f8907c009d3908d',
  473. '4441' => 'wx01f9731df15a7888',
  474. '4442' => 'wxb7192d7171ffa3c4',
  475. '4443' => 'wx3355f5be92b92350',
  476. '4444' => 'wx431a3a692700b63a',
  477. '4445' => 'wx3c53a641ee53b984',
  478. '4456' => 'wxc8c1cfb70e568f0c',
  479. '976' => 'wx21506dfd22a0dc9c',
  480. '928' => 'wx76da3531773c1f39',
  481. '157' => 'wx5ebe6187c0fb0bd5',
  482. '4175' => 'wx64cf3051ceb145ae',
  483. '4604' => 'wxc177995c55b5e75e'
  484. ];
  485. }
  486. //授权一次
  487. function specialChannelAuthInfoV2($distribution_channel_id)
  488. {
  489. $info = \Redis::hget('specialChannelAuthInfoV2', $distribution_channel_id);
  490. $data = [];
  491. if ($info) {
  492. $data[$distribution_channel_id] = $info;
  493. }
  494. return $data;
  495. //$info = \Redis::hgetall('specialChannelAuthInfoV2');
  496. //return $info;
  497. /*return [
  498. '4483'=>'wx4651668ca7f6be51',
  499. '4567'=>'wx2285b54ee5e6e752',
  500. '4568'=>'wx4cfa12302780e654',
  501. '4569'=>'wxa25b19c509ba7db5',
  502. '4578'=>'wx3beda81dba0b450b',
  503. '4580'=>'wxb4bb620409a96ed7',
  504. '4603'=>'wxf9df2ce5ea94fa4e',
  505. //'4604'=>'wxb479d38891b14286',
  506. '4605'=>'wx72569a8f18599cdb',
  507. '4606'=>'wxb17c22779a821271',
  508. '4607'=>'wx2bc3eaf8840f81a8',
  509. '4608'=>'wx3723054b39225b9d',
  510. '4609'=>'wx504c20ebf5cfcd60',
  511. '4617'=>'wxf45639677a2bb97c',
  512. '4624'=>'wx552c4500db26c627',
  513. '4658'=>'wx680b1e557a2e811d',
  514. '4666'=>'wx8136bdaa80a6dda0',
  515. '4667'=>'wx26608e6bb0705656',
  516. '4763'=>'wxdd100beddec262c9',
  517. '4779'=>'wx6f9cd0fcb96d9d9d',
  518. '4778'=>'wx4e1d53afd5e73b33',
  519. '4433'=>'wx449a65a9bbc7d50a',
  520. '4434'=>'wx7ea67b55ac102fd3',
  521. '4440'=>'wxddd622a5c195a3a9',
  522. '4438'=>'wx007ef65244b47f7c',
  523. '4815'=>'wx33f6cc190b194b3e',
  524. '4839'=>'wx27b946dad6c3aa91',
  525. '4905'=>'wxe1a06f73c723c2a6',
  526. '4906'=>'wxf03275b6863c2806',
  527. '4907'=>'wxadc68453a4500191',
  528. '4908'=>'wx8d46d69ab9d76c55',
  529. '4909'=>'wx2c62f7f4a02176d7',
  530. '4910'=>'wx7ee2ad6685e3d5b3',
  531. '4820'=>'wx30ecb35d13959f8d',
  532. '4980'=>'wx002be80fb65d808e',
  533. '5148'=>'wx8e9ff7a97fa67675',
  534. '5189'=>'wx475125bc6dd942f9',
  535. '4941'=>'wxe7f83fba4596cb03',
  536. '5319'=>'wxa697fd62760dfc53',
  537. '5365'=>'wx40c098d5ef384b2d',
  538. '5366'=>'wx1fd33388075778fe',
  539. '5427'=>'wxb54db864aae35e43',
  540. '5428'=>'wx46d1c3dc66bb08c6',
  541. '5429'=>'wx953c1a61bd82a358',
  542. '5430'=>'wx956fd17a5d81c122',
  543. '5444'=>'wx3f11071d2c51fe3c',
  544. '5464'=>'wxe06f1e737730cda8',
  545. '5225'=>'wx25d970a3f95ebebe',
  546. '4217'=>'wxdb15f8db194bf6f4',
  547. '5617'=>'wx94e7c0d2e805ba5e',
  548. '527'=>'wxbfa2ba3dccf76921',
  549. '5623'=>'wxe6fa23bd8c26e4a1',
  550. '5644'=>'wx379246ab67f5ba9b',
  551. '5645'=>'wx6723f21df29e0eb1',
  552. '5646'=>'wx28ab08db5717a16d',
  553. '5647'=>'wx14acfd6563f46ca9',
  554. '5648'=>'wx3d9d9118751b6caf',
  555. '5649'=>'wx8b1f11fcc7ba18da',
  556. '5650'=>'wx5fa6c4a6aa4bdf2a',
  557. '5651'=>'wxe5ab0cc5f3765329',
  558. '5652'=>'wx4990a38d72a02577',
  559. '5653'=>'wxdc09e7629d529a69',
  560. '5654'=>'wx03f0aa164ea3b4d9',
  561. '5655'=>'wxd9e763876c55864a',
  562. '5656'=>'wxd9816239598d8fdb',
  563. '5657'=>'wxb280a352c0a0ed65',
  564. '5658'=>'wxd9a6a8b2749717c3',
  565. '5660'=>'wx2c252d71dce324cb',
  566. '5661'=>'wxcc024129cf943eda',
  567. '5631'=>'wxc2282173679eca9f',
  568. '5632'=>'wxb6cfac3a8618d758',
  569. '5633'=>'wx7e4038e693ff47b8',
  570. '5634'=>'wx0521f0de4dfea155',
  571. '5635'=>'wx32ab656b849bd3b0',
  572. '5636'=>'wx524790df543980f3',
  573. '5637'=>'wx52ad84ebd80039fb',
  574. '5643'=>'wx72037519796b1531',
  575. '5664'=>'wx9265b52a75ebb042',
  576. '5638'=>'wx8384f5fb7ab1b19c',
  577. '5639'=>'wx7f470f3758928718',
  578. '5640'=>'wxc565bc7bc4a42074',
  579. '5641'=>'wx7e9d88af5216eedd',
  580. '5642'=>'wx4618cda729138d15',
  581. '5662'=>'wx44b8626c8ad9247e',
  582. '5663'=>'wx8653757301ed2591',
  583. '5690'=>'wxbdbb2b13eea80db9',
  584. '5691'=>'wx8b2e50da113c673c',
  585. '5692'=>'wxb51a74837aa2f1c4',
  586. '5764'=>'wxdb47c48bbfa19cb7',
  587. '5808'=>'wx99b70748c46d0533'
  588. ];*/
  589. }
  590. function generateMonthOrderUrl($user_id)
  591. {
  592. $app_id = env('MONTH_ORDER_APPID');
  593. $app_secret = env('MONTH_ORDER_APP_SECRET');
  594. $key = env('MONTH_ORDER_KEY');
  595. $plan_id = env('MONTH_ORDER_PLAN_ID');
  596. $ip = _getIp();
  597. $sign = _sign(compact('app_id', 'app_secret', 'plan_id', 'user_id', 'ip'), $key . $key);
  598. $sign = strtoupper($sign);
  599. $return_web = 1;
  600. $url = 'http://pap.manyuedu.org/?' . http_build_query(compact('app_id', 'app_secret', 'plan_id', 'user_id', 'ip', 'sign', 'return_web'));
  601. return $url;
  602. }
  603. function generateMonthOrderUrlV2($user_id, $plan_id, $ip)
  604. {
  605. $app_id = env('MONTH_ORDER_APPID');
  606. $app_secret = env('MONTH_ORDER_APP_SECRET');
  607. $key = env('MONTH_ORDER_KEY');
  608. $sign = _sign(compact('app_id', 'app_secret', 'plan_id', 'user_id', 'ip'), $key . $key);
  609. $sign = strtoupper($sign);
  610. $return_web = 1;
  611. $url = 'http://pap.manyuedu.org/?' . http_build_query(compact('app_id', 'app_secret', 'plan_id', 'user_id', 'ip', 'sign', 'return_web'));
  612. return $url;
  613. }
  614. function getMillisecond()
  615. {
  616. list($microsecond, $time) = explode(' ', microtime());
  617. return (float) sprintf('%.0f', (floatval($microsecond) + floatval($time)) * 1000);
  618. }
  619. /**
  620. * CURL发送post请求
  621. * @param $url
  622. * @param null $data
  623. * @param bool $json
  624. * @return bool|string
  625. */
  626. function httpPostRequest($url, $data = null, $json = FALSE)
  627. {
  628. //创建了一个curl会话资源,成功返回一个句柄;
  629. $curl = curl_init();
  630. //设置url
  631. curl_setopt($curl, CURLOPT_URL, $url);
  632. //设置为FALSE 禁止 cURL 验证对等证书(peer’s certificate)
  633. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
  634. //设置为 1 是检查服务器SSL证书中是否存在一个公用名(common name)
  635. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
  636. if (!empty($data)) {
  637. //设置请求为POST
  638. curl_setopt($curl, CURLOPT_POST, 1);
  639. //curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 60); //最长的可忍受的连接时间
  640. //设置POST的数据域
  641. curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  642. if ($json) {
  643. curl_setopt($curl, CURLOPT_HEADER, 0);
  644. curl_setopt(
  645. $curl,
  646. CURLOPT_HTTPHEADER,
  647. array(
  648. 'Content-Type: application/json; charset=utf-8',
  649. 'Content-Length: ' . strlen($data)
  650. )
  651. );
  652. }
  653. }
  654. //设置是否将响应结果存入变量,1是存入,0是直接输出
  655. curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
  656. //然后将响应结果存入变量
  657. $output = curl_exec($curl);
  658. //关闭这个curl会话资源
  659. curl_close($curl);
  660. return $output;
  661. }
  662. /**
  663. * 获取对象或数组的属性值
  664. * @param $param
  665. * @param $key
  666. * @param string $default
  667. * @return mixed|string
  668. */
  669. function getProp($param, $key, $default = '')
  670. {
  671. $result = $default;
  672. if (is_object($param) && isset($param->$key)) {
  673. $result = $param->$key;
  674. }
  675. if (is_array($param) && isset($param[$key])) {
  676. $result = $param[$key];
  677. }
  678. return $result;
  679. }
  680. /**
  681. * 求最大公约数 Greatest Common Divisor(GCD)
  682. * @param $a
  683. * @param $b
  684. * @return int
  685. */
  686. function gcd($a, $b)
  687. {
  688. // 防止因除数为0而崩溃
  689. if ($a === 0 || $b === 0) {
  690. return 1;
  691. }
  692. if ($a % $b === 0) {
  693. return $b;
  694. }
  695. return gcd($b, $a % $b);
  696. }
  697. /**
  698. * 格式化比例值
  699. * @param $value1
  700. * @param $value2
  701. * @param $gcd
  702. * @return array|float[]|int[]
  703. */
  704. function proportion($value1, $value2, $gcd)
  705. {
  706. if ($gcd === 0) {
  707. return [$value1, $value2];
  708. }
  709. return [$value1 / $gcd, $value2 / $gcd];
  710. }
  711. /**
  712. * 钉钉通知异常
  713. * @param $message
  714. */
  715. function sendNotice($message)
  716. {
  717. $webHook = config('common.dingTalk');
  718. $data = [
  719. 'msgtype' => 'text',
  720. 'text' => [
  721. 'content' => $message
  722. ],
  723. 'at' => [
  724. // 'atMobiles' => [
  725. // '13127819373'
  726. // ],
  727. 'isAll' => true
  728. ]
  729. ];
  730. httpPostRequest($webHook, json_encode($data), true);
  731. }
  732. /**
  733. * 获取隐藏cp
  734. * @param string $package 包名/包id/站点id
  735. * @param $type 0 是包名 ;1 是包id;2是站点id
  736. * @return false|string[]
  737. */
  738. function getHiddenCp($package = "",$type = 0)
  739. {
  740. if (empty($package)){
  741. return array_filter(explode(',',env('HIDDEN_CP_SOURCE')));
  742. }
  743. if ($type == 0){
  744. $data = \DB::table('qapp_package_info')->where('package','=',$package)->value('reject_cp');
  745. }else if($type == 1){
  746. $data = \DB::table('qapp_package_info')->where('id','=',$package)->value('reject_cp');
  747. }else if($type == 2) {
  748. $data = \DB::table('qapp_package_info')->where('channel_id','=',$package)->value('reject_cp');
  749. }else{
  750. $data = null;
  751. }
  752. if (empty($data)){
  753. return array_filter(explode(',',env('HIDDEN_CP_SOURCE')));
  754. }
  755. return explode(',',$data);
  756. }
  757. /**
  758. * 包名替换
  759. * @param $name
  760. * @return string
  761. */
  762. function get_real_package($name)
  763. {
  764. switch($name)
  765. {
  766. case 'com.beidao.kuaiying.haohan':
  767. $package = 'com.beidao.kuaiying.haitian';
  768. break;
  769. case 'com.beidao.kuaiying.haohannew':
  770. $package = 'com.beidao.kuaiying.haohan';
  771. break;
  772. default:
  773. $package = $name;
  774. break;
  775. }
  776. return $package;
  777. }
  778. if (!function_exists("check_qapp_auth")){
  779. function check_qapp_auth($package = "",$isPackageId = 0)
  780. {
  781. if (empty($package)){
  782. return false;
  783. }
  784. if ($isPackageId == 0){
  785. $data = \DB::table('qapp_package_info')->where('package','=',$package)->value('cp_author_status');
  786. }else{
  787. $data = \DB::table('qapp_package_info')->where('id','=',$package)->value('cp_author_status');
  788. }
  789. if ($data == 1){
  790. return true;
  791. }
  792. return false;
  793. }
  794. }
  795. /**
  796. *
  797. * name: check_qapp_send_order_id
  798. * 判断派单id是否属于所访问的站点
  799. * @param mixed $channelId 站点id
  800. * @param mixed $sendOrderId 派单id
  801. * @return bool
  802. * date 2022/09/09 10:50
  803. */
  804. if (!function_exists("check_qapp_send_order_id)")){
  805. function check_qapp_send_order_id($channelId = 0 ,$sendOrderId = 0){
  806. if ($sendOrderId < 1 || $channelId < 1){
  807. return false;
  808. }
  809. $sendOrderChannelId = Redis::hGet("qapp:send_order:distribution_channel",$sendOrderId);
  810. if ($sendOrderChannelId < 1 ){
  811. $sendOrderChannelId = \DB::table('qapp_send_orders')->where('send_order_id','=',$sendOrderId)->value('distribution_channel_id');
  812. if ($sendOrderChannelId > 0 ){
  813. Redis::hSet("qapp:send_order:distribution_channel",$sendOrderId,$sendOrderChannelId);
  814. }
  815. }
  816. if (trim($channelId) !== trim($sendOrderChannelId)){
  817. myLog("QuickAppSendOrderError")->info("派单渠道和包名不匹配: send_order_id = {$sendOrderId};sned_order_id_channel_id= {$sendOrderChannelId};channel_id= {$channelId} ");
  818. return false;
  819. }
  820. return true;
  821. }
  822. }
  823. if(!function_exists("str_decode")){
  824. /**
  825. * 字符解密
  826. * name: str_decode
  827. * @param $str
  828. * @return int
  829. * date 2022/09/27 11:38
  830. */
  831. function str_decode($str){
  832. $decode = Hashids::decode($str);
  833. $decode = is_array($decode) ? array_shift($decode) : $decode;
  834. if(empty($decodeBid)){
  835. myLog("StrDecodeError")->info("str : {$str}, decode : ".var_export($decode,true));
  836. }
  837. return intval($decode);
  838. }
  839. }
  840. if (!function_exists('str_encode')){
  841. /***
  842. * 字符加密
  843. * name: str_decode
  844. * @param mixed $str
  845. * @return mixed
  846. * date 2022/09/27 11:38
  847. */
  848. function str_encode($str)
  849. {
  850. return Hashids::encode($str);
  851. }
  852. }
  853. if(!function_exists('get_special_bid')){
  854. function get_special_bid()
  855. {
  856. return [];
  857. // return [7742,7743,7748,8721,8749,64870,64871,64872,64873,64874,64875,64876,64877,64878,64879,64880,64881,64882,64883,64884,64885,64886,64887,64888,64889,64890,64891,64892,64893,64894,64895,64896,64897,64900,64899,64898];
  858. }
  859. }
  860. /**
  861. * 判断包是否是公共包
  862. * name: is_public_package
  863. * @param string $packageName
  864. * @return bool
  865. * date 2022/09/22 16:53
  866. */
  867. if(!function_exists("is_public_package")){
  868. function is_public_package($packageName = "")
  869. {
  870. if (empty($packageName)){
  871. return false;
  872. }
  873. $publicPackageName = get_public_packages();
  874. if (in_array($packageName,$publicPackageName)){
  875. return true;
  876. }
  877. return false;
  878. }
  879. }
  880. /**
  881. * 判断包是否是公共包站点id
  882. * name: is_public_package
  883. * @param string $packageName
  884. * @return bool
  885. * date 2022/09/22 16:53
  886. */
  887. if(!function_exists("is_public_package_channel_id")){
  888. function is_public_package_channel_id($channelId = 0)
  889. {
  890. if (empty($$channelId)){
  891. return false;
  892. }
  893. $publicPackageName = get_public_package_channel_ids();
  894. if (in_array($channelId,$publicPackageName)){
  895. return true;
  896. }
  897. return false;
  898. }
  899. }
  900. /**
  901. * 所有获取公共包
  902. * name: get_public_packages
  903. * @return \Illuminate\Config\Repository|\Illuminate\Foundation\Application|mixed
  904. * date 2022/09/22 17:24
  905. */
  906. if(!function_exists('get_public_packages')){
  907. function get_public_packages(){
  908. return config("package.package_name");
  909. }
  910. }
  911. /**
  912. * 所有获取公共包站点id
  913. * name: get_public_packages
  914. * @return \Illuminate\Config\Repository|\Illuminate\Foundation\Application|mixed
  915. * date 2022/09/22 17:24
  916. */
  917. if(!function_exists('get_public_package_channel_ids')){
  918. function get_public_package_channel_ids(){
  919. return config("package.channel_id");
  920. }
  921. }
  922. /***
  923. * 获取默认公共包站点id
  924. * name: get_default_public_channel_id
  925. * @return int|mixed
  926. * date 2022/09/23 14:51
  927. */
  928. if (!function_exists("get_default_public_channel_id")){
  929. function get_default_public_channel_id()
  930. {
  931. $package = get_default_public_package_info();
  932. return $package['channel_id'] ?? 0;
  933. }
  934. }
  935. /***
  936. * 获取默认公共包站点id和包名
  937. * name: get_default_public_package_info
  938. * @return \Illuminate\Config\Repository|\Illuminate\Foundation\Application|mixed
  939. * date 2022/09/23 14:51
  940. */
  941. if (!function_exists("get_default_public_channel_id")){
  942. function get_default_public_package_info()
  943. {
  944. return config("package.default");
  945. }
  946. }
  947. if(function_exists('get_rejcet_cp_by_package_name')){
  948. function get_rejcet_cp($package = "",$isPackageId = 0){
  949. if (empty($package)){
  950. return false;
  951. }
  952. if ($isPackageId == 0){
  953. $data = \DB::table('qapp_package_info')->where('package','=',$package)->value('cp_author_status');
  954. }else{
  955. $data = \DB::table('qapp_package_info')->where('id','=',$package)->value('cp_author_status');
  956. }
  957. }
  958. }