123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Mead
- * Date: 2019/8/10
- * Time: 2:09 PM
- */
- function route_class()
- {
- return strtolower(str_replace([
- '.',
- '::'
- ], '-', \Illuminate\Support\Facades\Route::currentRouteName()));
- }
- function dda(...$args)
- {
- foreach ($args as &$x) {
- if (method_exists($x, 'toArray')) {
- $x = $x->toArray();
- }
- }
- dd(...$args);
- }
- if (!function_exists('request')) {
- /**
- * Get an instance of the current request or an input item from the request.
- *
- * @param array|string|null $key
- * @param mixed $default
- * @return \Illuminate\Http\Request|string|array
- */
- function request($key = null, $default = null)
- {
- if (is_null($key)) {
- return app('request');
- }
- if (is_array($key)) {
- return app('request')->only($key);
- }
- $value = app('request')->__get($key);
- return is_null($value) ? value($default) : $value;
- }
- }
- if (!function_exists('wechat_mini_config')) {
- /**
- * 获取微信小程序配置
- *
- * @param array|string|null $key
- * @param mixed $default
- * @return \Illuminate\Http\Request|string|array
- */
- function wechat_mini_config($merchant)
- {
- $config = [
- 'app_id' => $merchant['wxapp_app_id'],
- 'secret' => $merchant['wxapp_app_secret'],
- // 下面为可选项
- // 指定 API 调用返回结果的类型:array(default)/collection/object/raw/自定义类名
- 'response_type' => 'array',
- 'log' => [
- 'level' => 'debug',
- 'file' => __DIR__ . '/' . $merchant['id'] . '/wechat.log',
- ],
- ];
- return $config;
- }
- }
- if (!function_exists('wechat_pay_config')) {
- /**
- * 获取微信小程序配置
- *
- * @param array|string|null $key
- * @param mixed $default
- * @return \Illuminate\Http\Request|string|array
- */
- function wechat_pay_config($merchant)
- {
- $config = [
- 'app_id' => $merchant['wxapp_app_id'],
- 'mch_id' => $merchant['pay_mch_id'],
- 'key' => $merchant['pay_key'], // API 密钥
- // 如需使用敏感接口(如退款、发送红包等)需要配置 API 证书路径(登录商户平台下载 API 证书)
- 'cert_path' => base_path() . '/storage/app/public/merchant/' . $merchant['pay_cert_path'], // XXX: 绝对路径!!!!
- 'key_path' => base_path() . '/storage/app/public/merchant/' . $merchant['pay_key_path'], // XXX: 绝对路径!!!!
- 'notify_url' => config('app.url') . '/api/payments/wechat-notify/' . $merchant['id'], // 默认支付结果通知地址
- ];
- return $config;
- }
- }
- if (!function_exists('alipay_mini_config')) {
- /**
- * 获取支付宝小程序配置
- *
- * @param array|string|null $key
- * @param mixed $default
- * @return \Illuminate\Http\Request|string|array
- */
- function alipay_mini_config($merchant, $notifyUrl = '')
- {
- $options = new Alipay\EasySDK\Kernel\Config();
- $options->protocol = 'https';
- $options->gatewayHost = 'openapi.alipay.com';
- $options->signType = 'RSA2';
- $options->appId = $merchant['alipaymini_appId'];
- // 为避免私钥随源码泄露,推荐从文件中读取私钥字符串而不是写入源码中
- $options->merchantPrivateKey = $merchant['alipaymini_merchantPrivateKey'];
- // $options->alipayCertPath = base_path() . '/storage/app/public/merchant/' . $merchant['alipaymini_alipayCertPath'];
- // $options->alipayRootCertPath = base_path() . '/storage/app/public/merchant/' . $merchant['alipaymini_alipayRootCertPath'];
- // $options->merchantCertPath = base_path() . '/storage/app/public/merchant/' . $merchant['alipaymini_merchantCertPath'];
- $options->alipayCertPath = base_path() . '/database/zhifubao/' . $merchant['alipaymini_alipayCertPath'];
- $options->alipayRootCertPath = base_path() . '/database/zhifubao/' . $merchant['alipaymini_alipayRootCertPath'];
- $options->merchantCertPath = base_path() . '/database/zhifubao/' . $merchant['alipaymini_merchantCertPath'];
- if ($notifyUrl == '') {
- //可设置异步通知接收服务地址(可选)
- $options->notifyUrl = config('app.url') . '/api/alipay_notify/' . $merchant['id'];
- } else {
- $options->notifyUrl = $notifyUrl;
- }
- return Alipay\EasySDK\Kernel\Factory::setOptions($options);
- }
- }
- if (!function_exists('alipay_mini_pay')) {
- /**
- * 支付宝支付
- */
- function alipay_mini_pay($merchant, $desc, $no, $money, $alibuyer)
- {
- $msg = [
- 'code' => 0,
- 'msg' => '',
- 'data' => [],
- ];
- $result = alipay_mini_config($merchant)->payment()->common()->create($desc, $no, $money, $alibuyer);
- $responseChecker = new Alipay\EasySDK\Kernel\Util\ResponseChecker();
- if (!($responseChecker->success($result))) {
- $msg['msg'] = "调用失败,原因:" . $result->msg . "," . $result->subMsg;
- $msg['data'] = $result;
- return $msg;
- }
- return $msg = [
- 'code' => 1,
- 'msg' => '',
- 'data' => $result,
- ];
- }
- }
- if (!function_exists('userAuthinfo')) {
- /**
- * 解决一个用户对应两个auth 对应auth
- * $type 0 微信小程序授权 1 支付宝小程序授权
- */
- function userAuthinfo($merchant, $user_id, $type = false)
- {
- $msg = [
- 'code' => 0,
- 'msg' => '',
- 'data' => [],
- ];
- if ($type === false) {
- switch (source_type()) {
- case \App\Http\Controllers\Controller::SOURCE_TYPE_WECHAT:
- $type = \App\Models\Auth::TYPE_WEAPP;
- break;
- case \App\Http\Controllers\Controller::SOURCE_TYPE_ALIPAY:
- $type = \App\Models\Auth::TYPE_ALIPAY;
- break;
- }
- }
- if (!in_array($type, [App\Models\Auth::TYPE_WEAPP, App\Models\Auth::TYPE_ALIPAY])) {
- return $msg = [
- 'code' => 0,
- 'msg' => 'type参数不对',
- 'data' => '',
- ];
- }
- $auth = App\Models\Auth::where('merchant_id', $merchant['id'])->where('type', $type)->where('user_id', $user_id)->first();
- if (!$auth) return $msg = [
- 'code' => 0,
- 'msg' => '登录信息不存在',
- 'data' => '',
- ];
- return $msg = [
- 'code' => 1,
- 'data' => $auth,
- ];
- }
- }
- //支付宝预授权解冻参数拼接-公共参数 老版本SDK
- function AlipayYushouquanJiedong($merchant, $order_no, $order_rno)
- {
- $data = [
- //公共参数
- 'appId' => $merchant['alipaymini_appId'],
- 'rsaPrivateKey' => $merchant['alipaymini_merchantPrivateKey'],
- 'appCertPath' => base_path() . '/storage/app/public/merchant/' . $merchant['alipaymini_merchantCertPath'],
- 'alipayCertPath' => base_path() . '/storage/app/public/merchant/' . $merchant['alipaymini_alipayCertPath'],
- 'rootCertPath' => base_path() . '/storage/app/public/merchant/' . $merchant['alipaymini_alipayRootCertPath'],
- //查询预授权请求参数
- 'out_order_no' => $order_no, //商户授权资金订单号 , - 查询AlipayQueryYushouquan
- 'out_request_no' => $order_rno,//商户本次资金操作的请求流水号,用于标示请求流水的唯一性 - 查询AlipayQueryYushouquan
- ];
- return $data;
- }
- //支付宝预授权解冻参数拼接-请求参数 老版本SDK
- function AlipayYushouquanJiedongQingqiu($data, $auth_no, $out_request_no, $amount)
- {
- $dataarr = [
- //解冻请求参数
- 'auth_no' => $auth_no,
- 'out_request_no' => $out_request_no,//解冻后查询不能跟未解冻之前押金订单out_request_no为同一个 这里刷新一次记为解冻订单号为$refund->no
- 'amount' => $amount,
- 'remark' => '用户骑行押金解冻'
- ];
- $data = array_merge($data, $dataarr);
- return $data;
- }
- if (!function_exists('AlipayMiniOpenSign')) {
- //获取手机号解密
- function AlipayMiniOpenSign($merchant, $response_mi)
- {
- $aesKey = $merchant['alipaymini_aesKey'];//"UsA9af1KDBfQ485it1m+Sg==";//AES密钥
- $content = $response_mi;
- $result = openssl_decrypt(base64_decode($content), 'AES-128-CBC', base64_decode($aesKey), OPENSSL_RAW_DATA);
- $v = json_decode($result, true);
- return $v;
- }
- }
- function merchant_id()
- {
- return request()->header('merchant-id', request()->get('merchant_id', 0));
- }
- function source_type()
- {
- return request()->header('source-type', request()->get('source_type', 'wechat'));
- }
- function return_json($msg, $code = 500)
- {
- header('content-type:application/json;charset=utf-8');
- $msg = ['message' => $msg, 'status_code' => $code];
- echo json_encode($msg, true);
- die();
- }
- function path_to_url($path, $disk = 'public')
- {
- // 如果 image 字段本身就已经是完整的 url 就直接返回
- if (\Illuminate\Support\Str::startsWith($path, ['http://', 'https://'])) {
- return $path;
- }
- return \Illuminate\Support\Facades\Storage::disk($disk)->url($path);
- }
- function api_route($name, $v = 'v1')
- {
- return app('Dingo\Api\Routing\UrlGenerator')->version($v)->route($name);
- }
- function f_money($money, $sed = ',')
- {
- return number_format($money, 2, '.', $sed);
- }
- function php2js($arr)
- {
- return json_encode($arr, true);
- }
- function js2php($json)
- {
- return json_decode($json, true);
- }
- function arr2option($arr)
- {
- return collect($arr)->map(function ($key, $val) {
- return [
- 'label' => $key,
- 'value' => $val,
- 's_value' => (string)$val
- ];
- })->values();
- }
- function str2arr($str, $glue = ',')
- {
- return explode($glue, $str);
- }
- function arr2str($arr, $glue = ',')
- {
- return implode($glue, array_wrap($arr));
- }
- function wechat_fee($money)
- {
- return ($money * 100);
- }
- /**
- * 通过身份证号获取年龄
- * @param $id_card
- * @return bool|false|string
- * User: Mead
- */
- function idCard2age($id_card)
- {
- $year = substr($id_card, 6, 4);
- $monthDay = substr($id_card, 10, 4);
- $age = date('Y') - $year;
- if ($monthDay > date('md')) {
- $age--;
- }
- return $age;
- }
- function now()
- {
- return date('Y-m-d H:i:s');
- }
- /**
- * 计算两点地理坐标之间的距离
- * @param float $longitude1 起点经度
- * @param float $latitude1 起点纬度
- * @param float $longitude2 终点经度
- * @param float $latitude2 终点纬度
- * @param Int $unit 单位 1:米 2:千米
- * @param Int $decimal 精度 保留小数位数
- * @return float|int
- */
- function getDistance($longitude1, $latitude1, $longitude2, $latitude2, $unit = 2, $decimal = 2)
- {
- $EARTH_RADIUS = 6370.996; // 地球半径系数
- $PI = 3.1415926;
- $radLat1 = $latitude1 * $PI / 180.0;
- $radLat2 = $latitude2 * $PI / 180.0;
- $radLng1 = $longitude1 * $PI / 180.0;
- $radLng2 = $longitude2 * $PI / 180.0;
- $a = $radLat1 - $radLat2;
- $b = $radLng1 - $radLng2;
- $distance = 2 * asin(sqrt(pow(sin($a / 2), 2) + cos($radLat1) * cos($radLat2) * pow(sin($b / 2), 2)));
- $distance = $distance * $EARTH_RADIUS * 1000;
- if ($unit == 2) {
- $distance = $distance / 1000;
- }
- $distance = round($distance, $decimal);
- return $distance;
- }
- /**
- * 手机号中间四位隐藏
- * @param $mobile
- * @return mixed
- * User: Mead
- */
- function mobile_hidden($mobile)
- {
- return substr_replace($mobile, '****', 3, 4);
- }
- function format_mileage($km)
- {
- if ($km >= 1) {
- return "{$km}km";
- } else {
- $m = $km * 1000;
- return "{$m}m";
- }
- }
- /**
- * 格式化分钟
- * @param $min
- * @return string
- * User: Mead
- */
- function format_minutes($min)
- {
- $hours = 0;
- if ($min >= 60) {
- $hours = floor($min / 60);
- }
- $minute = $min % 60;
- $str = '';
- if ($hours) {
- $str .= "{$hours}小时";
- }
- if ($minute) {
- $str .= "{$minute}分";
- }
- return $str;
- }
- /**
- * 重新生成订单号
- * @param $no
- * User: Mead
- */
- function order_repeat($no)
- {
- $order = str2arr($no, '-')[0];
- return $order . '-' . rand(1, 9);
- }
- function curl_get($url)
- {
- $curl = curl_init();
- //设置抓取的url
- curl_setopt($curl, CURLOPT_URL, $url);
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);//设置获取的信息以文件流的形式返回,而不是直接输出
- $data = curl_exec($curl); //执行命令
- curl_close($curl); //关闭URL请求
- return js2php($data);
- }
- function makeNoTag($tag)
- {
- return config('bike.no_tag') . $tag;
- }
|