helpers.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Mead
  5. * Date: 2019/8/10
  6. * Time: 2:09 PM
  7. */
  8. function route_class()
  9. {
  10. return strtolower(str_replace([
  11. '.',
  12. '::'
  13. ], '-', \Illuminate\Support\Facades\Route::currentRouteName()));
  14. }
  15. function dda(...$args)
  16. {
  17. foreach ($args as &$x) {
  18. if (method_exists($x, 'toArray')) {
  19. $x = $x->toArray();
  20. }
  21. }
  22. dd(...$args);
  23. }
  24. if (!function_exists('request')) {
  25. /**
  26. * Get an instance of the current request or an input item from the request.
  27. *
  28. * @param array|string|null $key
  29. * @param mixed $default
  30. * @return \Illuminate\Http\Request|string|array
  31. */
  32. function request($key = null, $default = null)
  33. {
  34. if (is_null($key)) {
  35. return app('request');
  36. }
  37. if (is_array($key)) {
  38. return app('request')->only($key);
  39. }
  40. $value = app('request')->__get($key);
  41. return is_null($value) ? value($default) : $value;
  42. }
  43. }
  44. if (!function_exists('wechat_mini_config')) {
  45. /**
  46. * 获取微信小程序配置
  47. *
  48. * @param array|string|null $key
  49. * @param mixed $default
  50. * @return \Illuminate\Http\Request|string|array
  51. */
  52. function wechat_mini_config($merchant)
  53. {
  54. $config = [
  55. 'app_id' => $merchant['wxapp_app_id'],
  56. 'secret' => $merchant['wxapp_app_secret'],
  57. // 下面为可选项
  58. // 指定 API 调用返回结果的类型:array(default)/collection/object/raw/自定义类名
  59. 'response_type' => 'array',
  60. 'log' => [
  61. 'level' => 'debug',
  62. 'file' => __DIR__ . '/' . $merchant['id'] . '/wechat.log',
  63. ],
  64. ];
  65. return $config;
  66. }
  67. }
  68. if (!function_exists('wechat_pay_config')) {
  69. /**
  70. * 获取微信小程序配置
  71. *
  72. * @param array|string|null $key
  73. * @param mixed $default
  74. * @return \Illuminate\Http\Request|string|array
  75. */
  76. function wechat_pay_config($merchant)
  77. {
  78. $config = [
  79. 'app_id' => $merchant['wxapp_app_id'],
  80. 'mch_id' => $merchant['pay_mch_id'],
  81. 'key' => $merchant['pay_key'], // API 密钥
  82. // 如需使用敏感接口(如退款、发送红包等)需要配置 API 证书路径(登录商户平台下载 API 证书)
  83. 'cert_path' => base_path() . '/storage/app/public/merchant/' . $merchant['pay_cert_path'], // XXX: 绝对路径!!!!
  84. 'key_path' => base_path() . '/storage/app/public/merchant/' . $merchant['pay_key_path'], // XXX: 绝对路径!!!!
  85. 'notify_url' => config('app.url') . '/api/payments/wechat-notify/' . $merchant['id'], // 默认支付结果通知地址
  86. ];
  87. return $config;
  88. }
  89. }
  90. if (!function_exists('alipay_mini_config')) {
  91. /**
  92. * 获取支付宝小程序配置
  93. *
  94. * @param array|string|null $key
  95. * @param mixed $default
  96. * @return \Illuminate\Http\Request|string|array
  97. */
  98. function alipay_mini_config($merchant, $notifyUrl = '')
  99. {
  100. $options = new Alipay\EasySDK\Kernel\Config();
  101. $options->protocol = 'https';
  102. $options->gatewayHost = 'openapi.alipay.com';
  103. $options->signType = 'RSA2';
  104. $options->appId = $merchant['alipaymini_appId'];
  105. // 为避免私钥随源码泄露,推荐从文件中读取私钥字符串而不是写入源码中
  106. $options->merchantPrivateKey = $merchant['alipaymini_merchantPrivateKey'];
  107. // $options->alipayCertPath = base_path() . '/storage/app/public/merchant/' . $merchant['alipaymini_alipayCertPath'];
  108. // $options->alipayRootCertPath = base_path() . '/storage/app/public/merchant/' . $merchant['alipaymini_alipayRootCertPath'];
  109. // $options->merchantCertPath = base_path() . '/storage/app/public/merchant/' . $merchant['alipaymini_merchantCertPath'];
  110. $options->alipayCertPath = base_path() . '/database/zhifubao/' . $merchant['alipaymini_alipayCertPath'];
  111. $options->alipayRootCertPath = base_path() . '/database/zhifubao/' . $merchant['alipaymini_alipayRootCertPath'];
  112. $options->merchantCertPath = base_path() . '/database/zhifubao/' . $merchant['alipaymini_merchantCertPath'];
  113. if ($notifyUrl == '') {
  114. //可设置异步通知接收服务地址(可选)
  115. $options->notifyUrl = config('app.url') . '/api/alipay_notify/' . $merchant['id'];
  116. } else {
  117. $options->notifyUrl = $notifyUrl;
  118. }
  119. return Alipay\EasySDK\Kernel\Factory::setOptions($options);
  120. }
  121. }
  122. if (!function_exists('alipay_mini_pay')) {
  123. /**
  124. * 支付宝支付
  125. */
  126. function alipay_mini_pay($merchant, $desc, $no, $money, $alibuyer)
  127. {
  128. $msg = [
  129. 'code' => 0,
  130. 'msg' => '',
  131. 'data' => [],
  132. ];
  133. $result = alipay_mini_config($merchant)->payment()->common()->create($desc, $no, $money, $alibuyer);
  134. $responseChecker = new Alipay\EasySDK\Kernel\Util\ResponseChecker();
  135. if (!($responseChecker->success($result))) {
  136. $msg['msg'] = "调用失败,原因:" . $result->msg . "," . $result->subMsg;
  137. $msg['data'] = $result;
  138. return $msg;
  139. }
  140. return $msg = [
  141. 'code' => 1,
  142. 'msg' => '',
  143. 'data' => $result,
  144. ];
  145. }
  146. }
  147. if (!function_exists('userAuthinfo')) {
  148. /**
  149. * 解决一个用户对应两个auth 对应auth
  150. * $type 0 微信小程序授权 1 支付宝小程序授权
  151. */
  152. function userAuthinfo($merchant, $user_id, $type = false)
  153. {
  154. $msg = [
  155. 'code' => 0,
  156. 'msg' => '',
  157. 'data' => [],
  158. ];
  159. if ($type === false) {
  160. switch (source_type()) {
  161. case \App\Http\Controllers\Controller::SOURCE_TYPE_WECHAT:
  162. $type = \App\Models\Auth::TYPE_WEAPP;
  163. break;
  164. case \App\Http\Controllers\Controller::SOURCE_TYPE_ALIPAY:
  165. $type = \App\Models\Auth::TYPE_ALIPAY;
  166. break;
  167. }
  168. }
  169. if (!in_array($type, [App\Models\Auth::TYPE_WEAPP, App\Models\Auth::TYPE_ALIPAY])) {
  170. return $msg = [
  171. 'code' => 0,
  172. 'msg' => 'type参数不对',
  173. 'data' => '',
  174. ];
  175. }
  176. $auth = App\Models\Auth::where('merchant_id', $merchant['id'])->where('type', $type)->where('user_id', $user_id)->first();
  177. if (!$auth) return $msg = [
  178. 'code' => 0,
  179. 'msg' => '登录信息不存在',
  180. 'data' => '',
  181. ];
  182. return $msg = [
  183. 'code' => 1,
  184. 'data' => $auth,
  185. ];
  186. }
  187. }
  188. //支付宝预授权解冻参数拼接-公共参数 老版本SDK
  189. function AlipayYushouquanJiedong($merchant, $order_no, $order_rno)
  190. {
  191. $data = [
  192. //公共参数
  193. 'appId' => $merchant['alipaymini_appId'],
  194. 'rsaPrivateKey' => $merchant['alipaymini_merchantPrivateKey'],
  195. 'appCertPath' => base_path() . '/storage/app/public/merchant/' . $merchant['alipaymini_merchantCertPath'],
  196. 'alipayCertPath' => base_path() . '/storage/app/public/merchant/' . $merchant['alipaymini_alipayCertPath'],
  197. 'rootCertPath' => base_path() . '/storage/app/public/merchant/' . $merchant['alipaymini_alipayRootCertPath'],
  198. //查询预授权请求参数
  199. 'out_order_no' => $order_no, //商户授权资金订单号 , - 查询AlipayQueryYushouquan
  200. 'out_request_no' => $order_rno,//商户本次资金操作的请求流水号,用于标示请求流水的唯一性 - 查询AlipayQueryYushouquan
  201. ];
  202. return $data;
  203. }
  204. //支付宝预授权解冻参数拼接-请求参数 老版本SDK
  205. function AlipayYushouquanJiedongQingqiu($data, $auth_no, $out_request_no, $amount)
  206. {
  207. $dataarr = [
  208. //解冻请求参数
  209. 'auth_no' => $auth_no,
  210. 'out_request_no' => $out_request_no,//解冻后查询不能跟未解冻之前押金订单out_request_no为同一个 这里刷新一次记为解冻订单号为$refund->no
  211. 'amount' => $amount,
  212. 'remark' => '用户骑行押金解冻'
  213. ];
  214. $data = array_merge($data, $dataarr);
  215. return $data;
  216. }
  217. if (!function_exists('AlipayMiniOpenSign')) {
  218. //获取手机号解密
  219. function AlipayMiniOpenSign($merchant, $response_mi)
  220. {
  221. $aesKey = $merchant['alipaymini_aesKey'];//"UsA9af1KDBfQ485it1m+Sg==";//AES密钥
  222. $content = $response_mi;
  223. $result = openssl_decrypt(base64_decode($content), 'AES-128-CBC', base64_decode($aesKey), OPENSSL_RAW_DATA);
  224. $v = json_decode($result, true);
  225. return $v;
  226. }
  227. }
  228. function merchant_id()
  229. {
  230. return request()->header('merchant-id', request()->get('merchant_id', 0));
  231. }
  232. function source_type()
  233. {
  234. return request()->header('source-type', request()->get('source_type', 'wechat'));
  235. }
  236. function return_json($msg, $code = 500)
  237. {
  238. header('content-type:application/json;charset=utf-8');
  239. $msg = ['message' => $msg, 'status_code' => $code];
  240. echo json_encode($msg, true);
  241. die();
  242. }
  243. function path_to_url($path, $disk = 'public')
  244. {
  245. // 如果 image 字段本身就已经是完整的 url 就直接返回
  246. if (\Illuminate\Support\Str::startsWith($path, ['http://', 'https://'])) {
  247. return $path;
  248. }
  249. return \Illuminate\Support\Facades\Storage::disk($disk)->url($path);
  250. }
  251. function api_route($name, $v = 'v1')
  252. {
  253. return app('Dingo\Api\Routing\UrlGenerator')->version($v)->route($name);
  254. }
  255. function f_money($money, $sed = ',')
  256. {
  257. return number_format($money, 2, '.', $sed);
  258. }
  259. function php2js($arr)
  260. {
  261. return json_encode($arr, true);
  262. }
  263. function js2php($json)
  264. {
  265. return json_decode($json, true);
  266. }
  267. function arr2option($arr)
  268. {
  269. return collect($arr)->map(function ($key, $val) {
  270. return [
  271. 'label' => $key,
  272. 'value' => $val,
  273. 's_value' => (string)$val
  274. ];
  275. })->values();
  276. }
  277. function str2arr($str, $glue = ',')
  278. {
  279. return explode($glue, $str);
  280. }
  281. function arr2str($arr, $glue = ',')
  282. {
  283. return implode($glue, array_wrap($arr));
  284. }
  285. function wechat_fee($money)
  286. {
  287. return ($money * 100);
  288. }
  289. /**
  290. * 通过身份证号获取年龄
  291. * @param $id_card
  292. * @return bool|false|string
  293. * User: Mead
  294. */
  295. function idCard2age($id_card)
  296. {
  297. $year = substr($id_card, 6, 4);
  298. $monthDay = substr($id_card, 10, 4);
  299. $age = date('Y') - $year;
  300. if ($monthDay > date('md')) {
  301. $age--;
  302. }
  303. return $age;
  304. }
  305. function now()
  306. {
  307. return date('Y-m-d H:i:s');
  308. }
  309. /**
  310. * 计算两点地理坐标之间的距离
  311. * @param float $longitude1 起点经度
  312. * @param float $latitude1 起点纬度
  313. * @param float $longitude2 终点经度
  314. * @param float $latitude2 终点纬度
  315. * @param Int $unit 单位 1:米 2:千米
  316. * @param Int $decimal 精度 保留小数位数
  317. * @return float|int
  318. */
  319. function getDistance($longitude1, $latitude1, $longitude2, $latitude2, $unit = 2, $decimal = 2)
  320. {
  321. $EARTH_RADIUS = 6370.996; // 地球半径系数
  322. $PI = 3.1415926;
  323. $radLat1 = $latitude1 * $PI / 180.0;
  324. $radLat2 = $latitude2 * $PI / 180.0;
  325. $radLng1 = $longitude1 * $PI / 180.0;
  326. $radLng2 = $longitude2 * $PI / 180.0;
  327. $a = $radLat1 - $radLat2;
  328. $b = $radLng1 - $radLng2;
  329. $distance = 2 * asin(sqrt(pow(sin($a / 2), 2) + cos($radLat1) * cos($radLat2) * pow(sin($b / 2), 2)));
  330. $distance = $distance * $EARTH_RADIUS * 1000;
  331. if ($unit == 2) {
  332. $distance = $distance / 1000;
  333. }
  334. $distance = round($distance, $decimal);
  335. return $distance;
  336. }
  337. /**
  338. * 手机号中间四位隐藏
  339. * @param $mobile
  340. * @return mixed
  341. * User: Mead
  342. */
  343. function mobile_hidden($mobile)
  344. {
  345. return substr_replace($mobile, '****', 3, 4);
  346. }
  347. function format_mileage($km)
  348. {
  349. if ($km >= 1) {
  350. return "{$km}km";
  351. } else {
  352. $m = $km * 1000;
  353. return "{$m}m";
  354. }
  355. }
  356. /**
  357. * 格式化分钟
  358. * @param $min
  359. * @return string
  360. * User: Mead
  361. */
  362. function format_minutes($min)
  363. {
  364. $hours = 0;
  365. if ($min >= 60) {
  366. $hours = floor($min / 60);
  367. }
  368. $minute = $min % 60;
  369. $str = '';
  370. if ($hours) {
  371. $str .= "{$hours}小时";
  372. }
  373. if ($minute) {
  374. $str .= "{$minute}分";
  375. }
  376. return $str;
  377. }
  378. /**
  379. * 重新生成订单号
  380. * @param $no
  381. * User: Mead
  382. */
  383. function order_repeat($no)
  384. {
  385. $order = str2arr($no, '-')[0];
  386. return $order . '-' . rand(1, 9);
  387. }
  388. function curl_get($url)
  389. {
  390. $curl = curl_init();
  391. //设置抓取的url
  392. curl_setopt($curl, CURLOPT_URL, $url);
  393. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);//设置获取的信息以文件流的形式返回,而不是直接输出
  394. $data = curl_exec($curl); //执行命令
  395. curl_close($curl); //关闭URL请求
  396. return js2php($data);
  397. }
  398. function makeNoTag($tag)
  399. {
  400. return config('bike.no_tag') . $tag;
  401. }