| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- use app\constants\OptionSetting;
- use app\models\Option;
- use app\models\SharingReceiverCustom;
- use app\models\StoreCloud;
- use app\models\Supplier;
- use app\models\WechatConfig;
- use app\utils\Wechat\WechatMini;
- if (! function_exists('dump')) {
- /**
- * 调试打印
- * @param $var
- * @param int $depth
- * @param bool $highlight
- * @param bool $exit
- */
- function dump($var, $depth = 10, $highlight = false, $exit = true)
- {
- echo "<pre>";
- yii\helpers\VarDumper::dump($var, $depth, $highlight);
- $exit and exit();
- }
- }
- if (! function_exists('camelize')) {
- /**
- * 下划线转驼峰
- */
- function camelize($uncamelized_words, $separator = '_', $lcfirst = 0)
- {
- $res = str_replace(" ", "", ucwords(str_replace($separator, " ", strtolower($uncamelized_words))));
- return $lcfirst ? lcfirst($res) : $res;
- }
- }
- if (! function_exists('get_header')) {
- /**
- * 获取header参数
- * @param null $key
- * @return array|mixed|null
- */
- function get_header($key = null)
- {
- if (Yii::$app instanceof \yii\console\Application){
- return null;
- }
- $headers = Yii::$app->request->getHeaders();
- if($key === null){
- return $headers->toArray();
- }
- return $headers[$key];
- }
- }
- if (! function_exists('get_params')) {
- /**
- * 获取get参数
- * @param null $key
- * @param null $default
- * @return array|mixed|null
- */
- function get_params($key = null, $default = null)
- {
- if (Yii::$app instanceof \yii\console\Application){
- return null;
- }
- return Yii::$app->request->get($key, $default);
- }
- }
- if (! function_exists('post_params')) {
- /**
- * 获取post参数
- * @param null $key
- * @param null $default
- * @return array|mixed|null
- */
- function post_params($key = null, $default = null)
- {
- if (Yii::$app instanceof \yii\console\Application){
- return null;
- }
- $post = Yii::$app->request->post($key);
- if (! $post) {
- $params = json_decode(Yii::$app->request->getRawBody(), true);
- $post = $key ? (isset($params[$key]) ? $params[$key] : $default) : $params;
- }
- return $post;
- }
- }
- if (! function_exists('input_params')) {
- /**
- * 获取input参数
- * @param $key
- * @param null $default
- * @return array|mixed|null
- */
- function input_params($key, $default = null)
- {
- $post = post_params($key);
- if (!is_null($post)) {
- return $post;
- }
- return get_params($key, $default);
- }
- }
- if (! function_exists('all_params')) {
- /**
- * 获取所有参数
- * @return array|mixed|null
- */
- function all_params()
- {
- $get = get_params() ?: [];
- $post = post_params() ?: [];
- if (isset($get['store_id'])) {
- unset($post['store_id']);
- }
- return array_merge($get, $post);
- }
- }
- if (! function_exists('input_params_only')) {
- /**
- * 获取参数中的部分元素
- * @param array $keys
- * @return array
- */
- function input_params_only(array $keys)
- {
- $params = all_params();
- $result = [];
- foreach ($keys as $key) {
- if (isset($params[$key])) {
- $result[$key] = $params[$key];
- }
- }
- return $result;
- }
- }
- if (! function_exists('input_params_except')) {
- /**
- * 排除参数中的部分元素
- * @param array $keys
- * @return array|mixed|null
- */
- function input_params_except(array $keys)
- {
- $params = all_params();
- foreach ($keys as $key) {
- unset($params[$key]);
- }
- return $params;
- }
- }
- if (! function_exists('pagination_make')) {
- /**
- * 构建分页
- * @param \yii\db\ActiveQuery $query
- * @param bool $asArray
- * @return array
- */
- function pagination_make(yii\db\ActiveQuery $query, $asArray = true, $orderBy = null)
- {
- $countQuery = clone $query;
- $count = $countQuery->count();
- $pageNo = input_params('pageNo', input_params('page', 1)) ?: 1;
- $pageSize = input_params('pageSize', Yii::$app->params['pageSize']) ?: Yii::$app->params['pageSize'];
- $query->offset(($pageNo - 1) * $pageSize)->limit($pageSize);
- if ($orderBy) {
- $query->orderBy($orderBy);
- }
- if ($asArray) {
- $query->asArray();
- }
- $list = $query->all();
- return [
- 'totalCount' => (int)$count,
- 'list' => $list,
- 'pageNo' => (int)$pageNo,
- 'pageSize' => (int)$pageSize,
- ];
- }
- }
- if (! function_exists('event_on')) {
- /**
- * 绑定事件
- * @param string $name
- * @param callable $handler
- * @param mixed $data
- * @param bool $append
- */
- function event_on($name, $handler, $data = null, $append = true)
- {
- Yii::$app->on($name, $handler, $data, $append);
- }
- }
- if (! function_exists('event_trigger')) {
- /**
- * 触发事件
- * @param $name
- * @param \yii\base\Event|null $event
- */
- function event_trigger($name, yii\base\Event $event = null)
- {
- Yii::$app->trigger($name, $event);
- }
- }
- if (! function_exists('event_off')) {
- /**
- * 解除事件
- * @param $name
- * @param null $handler
- * @return bool
- */
- function event_off($name, $handler = null)
- {
- return Yii::$app->off($name, $handler);
- }
- }
- if (! function_exists('event_has')) {
- /**
- * 判断事件是否有绑定处理程序
- * @param $name
- * @return bool
- */
- function event_has($name)
- {
- return Yii::$app->hasEventHandlers($name);
- }
- }
- if (! function_exists('cache')) {
- /**
- * 返回cache组件
- * @return \yii\caching\CacheInterface
- */
- function cache()
- {
- return Yii::$app->cache;
- }
- }
- if (! function_exists('cacheLock')) {
- /**
- * 返回cache组件
- * @return \yii\caching\CacheInterface
- */
- function cacheLock()
- {
- return Yii::$app->cacheLock;
- }
- }
- if (! function_exists('cache_lock')) {
- /**
- * cache缓存锁
- */
- function cache_lock($key, $ttl)
- {
- if(is_array($key)){
- $key = implode('_', $key);
- }
- $cacheV = cache()->get($key);
- if($cacheV !== false){
- return $cacheV;
- }
- cache()->set($key, 1, $ttl);
- return null;
- }
- function cache_lock_del($key)
- {
- if(is_array($key)){
- $key = implode('_', $key);
- }
- cache()->delete($key);
- }
- }
- if (! function_exists('http_client')) {
- /**
- * 返回http客户端
- * @param array $config
- * @return \GuzzleHttp\Client
- */
- function http_client($config = [])
- {
- $config = array_merge([
- 'verify' => false, // 不验证sll
- ], $config);
- return new GuzzleHttp\Client($config);
- }
- }
- if (! function_exists('http_request')) {
- /**
- * 返回http请求对象
- * @param $method
- * @param string $uri
- * @param array $options
- * @return \Psr\Http\Message\ResponseInterface
- * @throws \GuzzleHttp\Exception\GuzzleException
- */
- function http_request($method, $uri = '', array $options = [])
- {
- $client = http_client();
- return $client->request($method, $uri, $options);
- }
- }
- if (! function_exists('http_get')) {
- /**
- * get请求
- * @param string $uri
- * @param array $options
- * @return \Psr\Http\Message\ResponseInterface
- * @throws \GuzzleHttp\Exception\GuzzleException
- */
- function http_get($uri = '', array $options = [])
- {
- return http_request('GET', $uri, $options);
- }
- }
- if (! function_exists('http_post')) {
- /**
- * post请求
- * @param string $uri
- * @param array $options
- * @return \Psr\Http\Message\ResponseInterface
- * @throws \GuzzleHttp\Exception\GuzzleException
- */
- function http_post($uri = '', array $options = [])
- {
- return http_request('POST', $uri, $options);
- }
- }
- if (! function_exists('get_admin')) {
- /**
- * 获取Admin对象
- * @return app\models\Admin|null
- */
- function get_admin()
- {
- return Yii::$app->jwt->getAdmin();
- }
- }
- if (! function_exists('get_saas_purchase_store_cloud')) {
- /**
- * 获取saas用户采购店铺前台store_cloud
- * @return Object|null
- */
- function get_saas_purchase_store_cloud($saas_id = 0)
- {
- // $saas_id = $saas_id ? $saas_id : get_saas_user_id();
- // $item = app\models\StoreCloud::findOne(['saas_user_id' => $saas_id, 'is_delete' => 0, 'is_enable' => 1]);
- if (!$saas_id) {
- try {
- $payload = \Yii::$app->jwt->getPayload();
- $store_admin_id = $payload['store_admin_id'];
- if ($store_admin_id > 0) {
- $StoreMiniAdmin = \app\models\StoreAdmin::findOne(['id' => $store_admin_id, 'status' => 1, 'is_delete' => 0]);
- $store_id = $StoreMiniAdmin->store_id;
- } else {
- $admin = \app\models\Admin::findOne(['id' => $payload['admin_id']]);
- $store_id = $admin->type_id;
- }
- $item = \app\models\StoreCloud::findOne(['store_id' => $store_id, 'is_delete' => 0, 'is_enable' => 1]);
- } catch (\Exception $e) {
- $item = null;
- }
- } else {
- $item = app\models\StoreCloud::findOne(['saas_user_id' => $saas_id, 'is_delete' => 0, 'is_enable' => 1]);
- }
- return $item;
- }
- }
- if (! function_exists('get_saas_purchase_store_id')) {
- /**
- * 获取saas用户采购店铺前台store_id
- * @return int
- */
- function get_saas_purchase_store_id($saas_id = 0)
- {
- $item = get_saas_purchase_store_cloud($saas_id);
- if ($item) {
- return $item->store_id;
- }
- return 0;
- }
- }
- if (! function_exists('get_store_id')) {
- /**
- * 获取前后台store_id
- * @return int
- */
- function get_store_id()
- {
- $params = all_params();
- if (isset($params['store_id'])) {
- return $params['store_id'];
- }
-
- $admin = get_admin();
- return $admin ? $admin->store_id : DEFAULT_STORE_ID;
- }
- }
- if (! function_exists('get_mini_id')) {
- /**
- * 获取前后台store_id
- * @return int
- */
- function get_mini_id()
- {
- $mini_id = get_params('mini_id', post_params('mini_id'));
- if ($mini_id) {
- return $mini_id;
- }else{
- return 0;
- }
- }
- }
- if (! function_exists('get_md_id')) {
- /**
- * 获取md_id
- * @return int
- */
- function get_md_id()
- {
- if (Yii::$app instanceof \yii\console\Application){
- return 0;
- }
- $md_id = input_params('md_id');
- if ((int)$md_id === 0) {
- return 0;
- }
- if ($md_id !== null) {
- $md = \app\models\Md::findOne(['id' => $md_id, 'is_delete' => 0]);
- if ($md && $md->store_id == get_store_id()) {
- return $md_id;
- }
- return -1;
- }
- $admin = get_admin();
- return $admin && $admin->type == \app\models\Admin::ADMIN_TYPE_MD ? $admin->type_id : 0;
- }
- }
- if (! function_exists('get_mch_brands_id')) {
- /**
- * 获取md_id
- * @return int
- */
- function get_mch_brands_id()
- {
- if (Yii::$app instanceof \yii\console\Application){
- return 0;
- }
- $brands_id = input_params('brands_id');
- if ((int)$brands_id === 0) {
- return 0;
- }
- if ($brands_id !== null) {
- $mchBrands = \app\models\MchBrands::findOne(['id' => $brands_id, 'is_delete' => 0]);
- if ($mchBrands && $mchBrands->store_id == get_store_id()) {
- return $brands_id;
- }
- return -1;
- }
- $admin = get_admin();
- return $admin && $admin->type == \app\models\Admin::ADMIN_TYPE_MCH_BRANDS ? $admin->type_id : 0;
- }
- }
- if (! function_exists('current_platform')) {
- /**
- * 获取当前平台
- * @return string
- */
- function current_platform()
- {
- return get_params('platform');
- }
- }
- if (! function_exists('is_platform')) {
- /**
- * 是否为平台小程序
- * @return bool
- */
- function is_platform()
- {
- return isset($_GET['is_platform']) && $_GET['is_platform'] === 'yes' ? true : false;
- }
- }
- if (! function_exists('is_single_store')) {
- /**
- * 是否为单店铺
- * @return bool
- */
- function is_single_store()
- {
- return isset($_GET['is_single_store']) && $_GET['is_single_store'] === 'yes' ? true : false;
- }
- }
- if (! function_exists('self_mini')) {
- /**
- * 是否为单店铺独立小程序
- * @return bool
- */
- function self_mini()
- {
- return get_params('self_mini', false);
- }
- }
- if (! function_exists('show_shenhe')) {
- /**
- * 是否要显示审核数据
- * @return bool
- */
- function show_shenhe()
- {
- $data = app\models\Store::mpAudit(get_store_id());
- return $data['status'];
- // if(client_version() == 18.1) return true;
- // return isset($_GET['show_shenhe']) && $_GET['show_shenhe'] ? true : false;
- }
- }
- if (! function_exists('is_isv')) {
- /**
- * 是否为平台小程序
- * @return bool
- */
- function is_isv()
- {
- return isset($_GET['is_isv']) && $_GET['is_isv'] === 'yes' ? true : false;
- }
- }
- if (! function_exists('is_app_platform')) {
- /**
- * 是否为app
- * @return bool
- */
- function is_app_platform()
- {
- return get_params('platform') === 'app';
- }
- }
- if (! function_exists('is_alipay_platform')) {
- /**
- * 是否为支付宝小程序
- * @return bool
- */
- function is_alipay_platform()
- {
- return get_params('platform') === 'alipay';
- }
- }
- if (! function_exists('is_toutiao_platform')) {
- /**
- * 是否为头条小程序
- * @return bool
- */
- function is_toutiao_platform()
- {
- return get_params('platform') === 'bytedance';
- }
- }
- if (! function_exists('is_wechat_platform')) {
- /**
- * 是否为微信小程序
- * @return bool
- */
- function is_wechat_platform()
- {
- return get_params('platform', 'wechat') === 'wechat';
- }
- }
- if (! function_exists('is_merchant')) {
- /**
- * 是否为批发端小程序
- * @return bool
- */
- function is_merchant()
- {
- return isset($_GET['mini_type']) && $_GET['mini_type'] === 'merchant' ? true : false;
- }
- }
- if (! function_exists('get_mch_id')) {
- /**
- * 获取后台mch_id
- * @return int
- */
- function get_mch_id()
- {
- $mch_id = get_params('mch_id', post_params('mch_id'));
- if ($mch_id != 0) {
- return $mch_id;
- }
- $admin = get_admin();
- return $admin && $admin->type === \app\models\Admin::ADMIN_TYPE_MCH ? $admin->type_id : 0;
- }
- }
- if (! function_exists('get_supplier_id')) {
- /**
- * 获取后台supplier_id
- * @return int
- */
- function get_supplier_id()
- {
- $admin = get_admin();
- $supplier_id = $admin && $admin->type === \app\models\Admin::ADMIN_TYPE_SUPPLIER ? $admin->type_id : 0;
- return input_params('supplier_id', $supplier_id);
- }
- }
- if (! function_exists('get_bd_id')) {
- /**
- * 获取后台推广代理id
- * @return int
- */
- function get_bd_id()
- {
- $admin = get_admin();
- return $admin && $admin->type === \app\models\Admin::ADMIN_TYPE_BD_AGENT ? $admin->id : 0;
- }
- }
- if (! function_exists('get_plugin')) {
- function get_plugin()
- {
- $route = \Yii::$app->requestedRoute;
- $route = str_replace('\\', '/', $route);
- $match = explode('/', $route);
- return $match;
- }
- }
- if (! function_exists('get_plugin_type')) {
- function get_plugin_type()
- {
- $plugin = get_plugin();
- $list = [
- 'goods' => 0,
- 'pond' => 1,
- 'bargain' => 2,
- 'lottery' => 4,
- 'step' => 5,
- 'scanCodePay' => 6, // 当面付
- ];
- if (isset($list[$plugin[1]])) {
- $type = $list[$plugin[1]];
- } else {
- $type = 0;
- }
- return $type;
- }
- }
- if (! function_exists('cyy_version')) {
- function cyy_version()
- {
- static $version = null;
- if ($version) {
- return $version;
- }
- $file = __DIR__ . '/../version.json';
- if (!file_exists($file)) {
- throw new Exception('Version not found');
- }
- $res = json_decode(file_get_contents($file), true);
- if (!is_array($res)) {
- throw new Exception('Version cannot be decoded');
- }
- return $version = $res['version'];
- }
- }
- if (!function_exists('pay_notify_url')) {
- /**
- * 拼接支付回调地址
- *
- * @param string $suffix
- * @return string
- */
- function pay_notify_url($suffix, $store_id = 0, $is_platform = false)
- {
- try {
- $hostInfo = \Yii::$app->request->hostInfo;
- $store_id = $store_id ?: get_store_id();
- $hostInfo = str_replace('http:', 'https:', $hostInfo);
- // if (\Yii::$app->request->getIsSecureConnection()) {
- // $hostInfo = str_replace('http:', 'https:', $hostInfo);
- // } else {
- // $hostInfo = str_replace('https:', 'http:', $hostInfo);
- // }
- if ($is_platform) {
- $store_id = 0;
- }
- $hostInfo .= '/index.php/' . $suffix . '/' . $store_id;
- } catch (\Exception $e) {
- $hostInfo = '';
- }
- return $hostInfo;
- }
- }
- if (!function_exists('pay_return_url')) {
- function path_h5()
- {
- $path = 'h5';
- if (\Yii::$app->prod_is_dandianpu()) {
- $path = \Yii::$app->params['uri_path_dandianpuh5'];
- }
- return $path;
- }
- /**
- * 拼接支付回跳地址
- *
- * @param string $suffix
- * @return string
- */
- function pay_return_url($suffix)
- {
- $hostInfo = \Yii::$app->request->hostInfo;
- $hostInfo = str_replace('http:', 'https:', $hostInfo);
- // if (\Yii::$app->request->getIsSecureConnection()) {
- // $hostInfo = str_replace('http:', 'https:', $hostInfo);
- // } else {
- // $hostInfo = str_replace('https:', 'http:', $hostInfo);
- // }
- $hostInfo .= '/' . path_h5() . '/#/' . $suffix;
- return $hostInfo;
- }
- }
- if (! function_exists('get_user')) {
- /**
- * 获取前端用户对象
- * @return app\models\User|null
- */
- function get_user()
- {
- return Yii::$app->jwt->getUser();
- }
- }
- if (! function_exists('get_saas_user')) {
- /**
- * 获取前端用户对象
- * @return app\models\SaasUser|null
- */
- function get_saas_user()
- {
- return Yii::$app->jwt->getSaasUser();
- }
- }
- if (! function_exists('get_saas_user_id')) {
- /**
- * 获取前端用户对象id
- * @return app\models\SaasUser|null
- */
- function get_saas_user_id()
- {
- $saasUser = get_saas_user();
- return $saasUser ? $saasUser->id : 0;
- }
- }
- if (! function_exists('get_user_id')) {
- /**
- * 获取前端用户对象id
- * @return int
- */
- function get_user_id()
- {
- $user = get_user();
- return $user ? $user->id : 0;
- }
- }
- if (!function_exists('env')) {
- /**
- * Gets the value of an environment variable.
- *
- * @param string $key
- * @param mixed $default
- * @param string $delimiter
- * @return mixed
- */
- function env($key, $default = null, $delimiter = '')
- {
- $value = getenv($key);
- if ($value === false) {
- return value($default);
- }
- switch (strtolower($value)) {
- case 'true':
- case '(true)':
- return true;
- case 'false':
- case '(false)':
- return false;
- case 'empty':
- case '(empty)':
- return '';
- case 'null':
- case '(null)':
- return;
- }
- if (strlen($value) > 1 && str_starts_with($value, '"') && str_ends_with($value, '"')) {
- $value = substr($value, 1, -1);
- }
- if (strlen($delimiter) > 0) {
- if (strlen($value) == 0) {
- $value = $default;
- } else {
- $value = explode($delimiter, $value);
- }
- }
- return $value;
- }
- }
- if (!function_exists('value')) {
- /**
- * Return the default value of the given value.
- *
- * @param mixed $value
- * @return mixed
- */
- function value($value)
- {
- return $value instanceof Closure ? $value() : $value;
- }
- }
- if (! function_exists('queue_push')) {
- /**
- * 消息队列push
- * @param yii\queue\JobInterface|mixed $job
- * @param int 延迟时间,单位秒
- * @return string|null id of a job message
- */
- function queue_push($job, $delay = 0, $checkReplay = 0)
- {
- if($checkReplay){
- // debug_log(serialize($job) . $delay);
- $cache = cacheLock();
- $k = [serialize($job), $delay];
- $id = $cache->get($k);
- if($id){
- // debug_log('cache value:' . $id);
- if(!queue_is_done($id)){
- return $id;
- }
- $cache->delete($k);
- }
- }
- $queue = \Yii::$app->queue;
- if ($delay > 0) {
- $id = $queue->delay($delay)->push($job);
- }else{
- $id = $queue->push($job);
- }
-
- if($checkReplay){
- $cache->set($k, $id, 86400);
- // debug_log($id);
- }
- return $id;
- }
- }
- if (! function_exists('queue_remove')) {
- /**
- * 消息队列移除
- * @param string $id
- * @return bool
- */
- function queue_remove($id)
- {
- return \Yii::$app->queue->remove($id);
- }
- }
- if (! function_exists('queue_is_done')) {
- /**
- * 消息队列是否完成
- * @param string $id
- * @return bool
- */
- function queue_is_done($id)
- {
- return \Yii::$app->queue->isDone($id);
- }
- }
- if (! function_exists('queue_is_waiting')) {
- /**
- * 消息队列是否等待中
- * @param string $id
- * @return bool
- */
- function queue_is_waiting($id)
- {
- return \Yii::$app->queue->isWaiting($id);
- }
- }
- if (! function_exists('is_profit_sharing')) {
- /**
- * 是否走分账
- * wx 微信 ali 支付宝
- * @return bool
- */
- function is_profit_sharing()
- {
- $transfer_profit = \app\models\Store::findOne(get_store_id())->transfer_profit;
- return \Yii::$app->isSaas() && $transfer_profit > 0;
- }
- }
- if (! function_exists('is_profit_pay')) {
- /**
- * 是否走分账服务商支付
- * wx 微信 ali 支付宝
- * @return bool
- */
- function is_profit_pay($type = 'wx', $store_id = 0)
- {
- if (\Yii::$app->isSaas()) {
- return true;
- }else{
- return false;
- }
- // if (\Yii::$app->isSaas()) {
- // $store_id = $store_id ? $store_id : get_store_id();
- // //会员卡支付给平台部分账
- // if($store_id == -1){
- // return false;
- // }
- // $transfer_profit = \app\models\Store::findOne($store_id)->transfer_profit;
- // if ($transfer_profit > 0) {
- // return true;
- // }
- // if ($type == 'wx') {
- // $wechatConfig = \app\models\WechatConfig::find()->where(['store_id' => $store_id, 'type' => 1])->one();
- // if (!$wechatConfig->pay_key) {
- // return true;
- // }
- // } elseif ($type == 'ali') {
- // $aliConfig = \app\models\Option::get(\app\models\Option::OPTOPN_KEY, $store_id, 'alipay');
- // if (!$aliConfig) {
- // return false;
- // }
- // $aliConfig = json_decode($aliConfig['value'], true);
- // if (!$aliConfig['alipay_public_key']) {
- // return true;
- // }
- // }
- // return false;
- // } else {
- // return false;
- // }
-
- }
- }
- if (! function_exists('is_open_platform')) {
- /**
- * 判断小程序是否授权第三方
- * wx 微信 ali 支付宝
- * @return bool
- */
- function is_open_platform($type = 'wx', $store_id = 0)
- {
- if (\Yii::$app->isSaas()) {
- return true;
- }else{
- return false;
- }
- // $store_id = $store_id ? $store_id : get_store_id();
- // if ($type == 'wx') {
- // $wechatConfig = \app\models\WechatConfig::find()->where(['store_id' => $store_id, 'type' => 1])->one();
- // if (!empty($wechatConfig) && !$wechatConfig->app_secret) {
- // return true;
- // }
- // } elseif ($type == 'ali') {
- // return true;
- // }
- // return false;
-
- }
- }
- if (! function_exists('market_alicloudapi_com')) {
- /**
- * 阿里云市场接口
- */
- function market_alicloudapi_com($appcode, $url, $querys = '', $method = "GET") {
- $host = $url;
- $headers = array();
- array_push($headers, "Authorization:APPCODE " . $appcode);
- $url = $url . "?" . $querys;
- $curl = curl_init();
- if($method == 'POST'){
- array_push($headers, "Content-Type".":"."application/x-www-form-urlencoded; charset=UTF-8");
- curl_setopt($curl, CURLOPT_POSTFIELDS, $querys);
- }
- curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
- curl_setopt($curl, CURLOPT_URL, $url);
- curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
- curl_setopt($curl, CURLOPT_FAILONERROR, false);
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($curl, CURLOPT_HEADER, false);
- if (1 == strpos("$".$host, "https://"))
- {
- curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
- }
- return (curl_exec($curl));
- }
- }
- if (! function_exists('cloud_post')) {
- /**
- * 云仓post请求
- */
- function cloud_post($url, $data_string) {
- if(is_array($data_string)) {
- $data_string['version'] = cyy_version();
- $data_string = json_encode($data_string);
- }
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
- curl_setopt($ch, CURLOPT_HTTPHEADER, array(
- 'Content-Type: application/json; charset=utf-8',
- 'Content-Length: ' . strlen($data_string))
- );
- ob_start();
- curl_exec($ch);
- $return_content = ob_get_contents();
- ob_end_clean();
- return $return_content;
- //$return_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
- //return array($return_code, $return_content);
- }
- }
- if (! function_exists('cloud_get')) {
- /**
- * 云仓post请求
- */
- function cloud_get($url,$type=1) {
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- ob_start();
- curl_exec($ch);
- $return_content = ob_get_contents();
- ob_end_clean();
- return $return_content;
- }
- }
- if (!function_exists('union_share_scale_check')) {
- /**
- * 联盟分销比例检查是否超过100
- * @param mixed $store
- * @param bool $exclude_referral
- * @return mixed
- * @throws \yii\base\InvalidConfigException
- * @throws \yii\base\NotSupportedException
- * @throws \yii\db\Exception
- * @throws \yii\base\InvalidArgumentException
- * @throws \Exception
- * @author Syan mzsongyan@gmail.com
- * @date 2022-05-14
- */
- function union_share_scale_check($store, $exclude_referral = false)
- {
- $scale = 0;
- $defaultSet = Option::get(OptionSetting::SHARE_STORE_DEFAULT_SETTING, -1, OptionSetting::SHARE_GROUP_NAME, '{}');
- $defaultSet = json_decode($defaultSet['value'], true);
- if(intval($store->store_share_switch) && $store->store_share_value == 0){
- if(!empty($defaultSet['store_share_switch']) && !empty($defaultSet['store_share_value'])){
- $store->store_share_value = $defaultSet['store_share_value'];
- }
- }
- // 店铺比例
- $scale += $store->store_share_value;
- // 联盟分销
- $union_option = json_decode($store->share_setting, true);
- if(intval($union_option['level']) && $union_option['level_one'] == 0 && $union_option['level_two'] == 0 && $union_option['level_three'] == 0){
- $defaultSet = Option::get(OptionSetting::SHARE_SAAS_DEFAULT_SETTING, -1, OptionSetting::SHARE_GROUP_NAME, '{}');
- $defaultSet = json_decode($defaultSet['value'], true);
- if(!empty($defaultSet['level']) &&
- (!empty($defaultSet['level_one']) || !empty($defaultSet['level_two']) || !empty($defaultSet['level_three']))){
- $union_option['level_one'] = $defaultSet['level_one'];
- $union_option['level_two'] = $defaultSet['level_two'];
- $union_option['level_three'] = $defaultSet['level_three'];
- }
- }
- if ($union_option && is_array($union_option)) {
- $scale = $scale +
- $union_option['level_one'] +
- $union_option['level_two'] +
- $union_option['level_three'];
- }
- if (!$exclude_referral) {
- // // 推荐人
- $referral = app\models\SaasStoreReferral::findOne(['store_id' => $store->id, 'type' => 1]);
- if(intval($referral->is_enable) && $referral->referral_rebate == 0){
- $defaultSet = Option::get(OptionSetting::SHARE_SAAS_REFERRAL_DEFAULT_SETTING, -1, OptionSetting::SHARE_GROUP_NAME, '{}');
- $defaultSet = json_decode($defaultSet['value'], true);
- if(!empty($defaultSet['is_enable']) && !empty($defaultSet['referral_rebate'])){
- $referral->referral_rebate = $defaultSet['referral_rebate'];
- }
- }
- // // 推荐人
- $scale += $referral->referral_rebate;
- }
- if(intval($store->self_rebate_switch) && $store->self_rebate_value == 0) {
- $defaultSet = Option::get(OptionSetting::SHARE_SELF_REBATE_DEFAULT_SETTING, -1, OptionSetting::SHARE_GROUP_NAME, '{}');
- $defaultSet = json_decode($defaultSet['value'], true);
- if (!empty($defaultSet['self_rebate_switch']) && !empty($defaultSet['self_rebate_value'])) {
- $store->self_rebate_value = $defaultSet['self_rebate_value'];
- }
- }
- // 消费返利
- $scale += $store->self_rebate_value;
- $store_total_profit = SharingReceiverCustom::find()->where([
- 'sharing_way' => SharingReceiverCustom::SHARING_WAY_STORE,
- 'sharing_store_id' => $store->id,
- 'is_delete' => SharingReceiverCustom::IS_DELETE_NO
- ])->sum('sharing_profit') ?: 0;
- if ($store_total_profit + $scale > 100) {
- return false;
- }
- return union_share_scale($store, $exclude_referral) <= 100;
- }
- }
- if (!function_exists('union_share_scale')) {
- /**
- * 联盟分销比例
- * @param mixed $store
- * @param bool $exclude_referral
- */
- function union_share_scale($store, $exclude_referral = false, $custom_sharing_id = 0)
- {
- $scale = 0;
- $defaultSet = Option::get(OptionSetting::SHARE_STORE_DEFAULT_SETTING, -1, OptionSetting::SHARE_GROUP_NAME, '{}');
- $defaultSet = json_decode($defaultSet['value'], true);
- if(intval($store->store_share_switch) && $store->store_share_value == 0){
- if(!empty($defaultSet['store_share_switch']) && !empty($defaultSet['store_share_value'])){
- $store->store_share_value = $defaultSet['store_share_value'];
- }
- }
- // 店铺比例
- $scale += $store->store_share_value;
- $union_option = json_decode($store->share_setting, true);
- if(intval($union_option['level']) && $union_option['level_one'] == 0 && $union_option['level_two'] == 0 && $union_option['level_three'] == 0){
- $defaultSet = Option::get(OptionSetting::SHARE_SAAS_DEFAULT_SETTING, -1, OptionSetting::SHARE_GROUP_NAME, '{}');
- $defaultSet = json_decode($defaultSet['value'], true);
- if(!empty($defaultSet['level']) &&
- (!empty($defaultSet['level_one']) || !empty($defaultSet['level_two']) || !empty($defaultSet['level_three']))){
- $union_option['level_one'] = $defaultSet['level_one'];
- $union_option['level_two'] = $defaultSet['level_two'];
- $union_option['level_three'] = $defaultSet['level_three'];
- }
- }
- // 联盟分销
- if ($union_option && is_array($union_option)) {
- $scale = $scale +
- $union_option['level_one'] +
- $union_option['level_two'] +
- $union_option['level_three'];
- }
- if(intval($store->self_rebate_switch) && $store->self_rebate_value == 0) {
- $defaultSet = Option::get(OptionSetting::SHARE_SELF_REBATE_DEFAULT_SETTING, -1, OptionSetting::SHARE_GROUP_NAME, '{}');
- $defaultSet = json_decode($defaultSet['value'], true);
- if (!empty($defaultSet['self_rebate_switch']) && !empty($defaultSet['self_rebate_value'])) {
- $store->self_rebate_value = $defaultSet['self_rebate_value'];
- }
- }
- // 消费返利
- $scale += $store->self_rebate_value;
- if (!$exclude_referral) {
- $referral = app\models\SaasStoreReferral::findOne(['store_id' => $store->id, 'type' => 1]);
- if(intval($referral->is_enable) && $referral->referral_rebate == 0){
- $defaultSet = Option::get(OptionSetting::SHARE_SAAS_REFERRAL_DEFAULT_SETTING, -1, OptionSetting::SHARE_GROUP_NAME, '{}');
- $defaultSet = json_decode($defaultSet['value'], true);
- if(!empty($defaultSet['is_enable']) && !empty($defaultSet['referral_rebate'])){
- $referral->referral_rebate = $defaultSet['referral_rebate'];
- }
- }
- // // 推荐人
- $scale += $referral->referral_rebate;
- }
- $store_total_profit = SharingReceiverCustom::find()->where([
- 'sharing_way' => SharingReceiverCustom::SHARING_WAY_STORE,
- 'sharing_store_id' => $store->id,
- 'is_delete' => SharingReceiverCustom::IS_DELETE_NO
- ])->andWhere(['<>', 'id', $custom_sharing_id])->sum('sharing_profit') ?: 0;
- return $store_total_profit + $scale;
- }
- }
- if (!function_exists('is_h5')) {
- /**
- * 判断是否为h5端
- * @return bool
- * @author Syan mzsongyan@gmail.com
- * @date 2022-05-23
- */
- function is_h5()
- {
- return in_array(get_params('_form'), ['h5', 'official']);
- }
- }
- /**
- * 向下取小数点后n
- */
- if (!function_exists('floor_num')) {
- function floor_num($number, $floor = 2)
- {
- $floor_num = pow(10, $floor);
- return floor($number * $floor_num) / $floor_num;
- }
- }
- // 调试函数
- if (! function_exists('debug_log')) {
- function debug_log($msg, $file = 'debug.log')
- {
- date_default_timezone_set('PRC');
- $file = \Yii::$app->basePath . '/runtime/logs/' . str_replace('\\', '_', $file);
- if (is_array($msg)) {
- $msg = json_encode($msg, JSON_UNESCAPED_UNICODE);
- }
- $message = date('Y-m-d H:i:s') . ' ' . $msg . PHP_EOL;
- $log = error_log($message, 3, $file);
- @chmod($file, 0777);
- return $log;
- }
- }
- if (! function_exists('stbz_client')) {
- /**
- * 胜天半子client
- * @return \Stbz\Api\SupplyClient
- * @author Syan mzsongyan@gmail.com
- * @date 2022-09-13
- */
- function stbz_client($url = '')
- {
- $option = \app\models\Option::get('stbz_account', 0, 'saas', \json_encode([
- 'key' => '',
- 'secret' => '',
- 'url' => ''
- ]));
- $option = json_decode($option['value'], true);
- $SupplyClient = new \Stbz\Api\SupplyClient($option['key'], $option['secret']);
- if (!empty($option['url'])) {
- $SupplyClient->serverRoot = $option['url'];
- }
- if (!empty($url)) {
- $SupplyClient->serverRoot = $url;
- }
- return $SupplyClient;
- }
- }
- if (! function_exists('get_cloud_token')) {
- /**
- * 获取云仓token
- * @return \Stbz\Api\SupplyClient
- * @author Syan mzsongyan@gmail.com
- * @date 2022-09-13
- */
- function get_cloud_token($name, $password, $is_use_platform = 0)
- {
- $url = "/user/getToken";
- $data = [];
- $data['name'] = $name;
- $data['pwd'] = $password;
- if ($is_use_platform) {
- $data['token'] = get_platform_token();
- }
- $domain = (new OptionSetting)->getCloudDomainName();
- $response = cloud_post($domain . $url, $data);
- //\Yii::error('token', $response);
- return json_decode($response,true);
- }
- }
- if (! function_exists('get_platform_token')) {
- /**
- * 生成保存云仓token
- * @return \Stbz\Api\SupplyClient
- * @author Syan mzsongyan@gmail.com
- * @date 2022-09-13
- */
- function get_platform_token()
- {
- // $content = \Yii::$app->cache->get('cloud_admin_cache_token');
- // if ($content) {
- // return $content;
- // }
- $content = Option::get('cloud', 0, 'saas');
- $params = json_decode($content['value'], true);
- $response = get_cloud_token($params['name'], $params['pwd']);
- if ($response['code'] == 0 && $response['taken']) {
- \Yii::$app->cache->set('cloud_admin_cache_token', $response['taken'], 29 * 3600);
- return $response['taken'];
- }
- return '';
- }
- }
- if (! function_exists('get_supplier_token')) {
- /**
- * 生成保存云仓token
- * @return \Stbz\Api\SupplyClient
- * @author Syan mzsongyan@gmail.com
- * @date 2022-09-13
- */
- function get_supplier_token($token_supplier_id = 0, $supplier_id = 0)
- {
- if ($token_supplier_id && $token_supplier_id > 0) {
- $supplier = Supplier::findOne(['cloud_supplier_id' => $token_supplier_id]);
- $supplier_id = $supplier->id;
- } else {
- $supplier_id = $supplier_id ? $supplier_id : get_supplier_id();
- }
- $supplier = Supplier::findOne($supplier_id);
-
- if (!$supplier) {
- return '';
- }
- $response = get_cloud_token($supplier->name, $supplier->password, 1);
- if ($response['code'] == 0 && $response['taken']) {
- \Yii::$app->cache->set('cloud_supplier_cache_token_' . $supplier_id, $response['taken'], 29 * 3600);
- return $response['taken'];
- }
- return '';
- }
- }
- if (! function_exists('get_merchant_token')) {
- /**
- * 生成保存云仓token
- * @return \Stbz\Api\SupplyClient
- * @author Syan mzsongyan@gmail.com
- * @date 2022-09-13
- */
- function get_merchant_token($token_stroe_cloud_id = 0, $store_id = 0, &$message = null)
- {
- if($token_stroe_cloud_id){
- $storeCloud = StoreCloud::find()->where(['id' => $token_stroe_cloud_id, 'is_delete' => 0])->one();
- if ($storeCloud) {
- $access_token = \Yii::$app->cache->get('cloud_mch_cache_token_' . $storeCloud->id);
- if ($access_token) {
- return $access_token;
- }
- $response = get_cloud_token($storeCloud->name, $storeCloud->password, 1);
- if ($response['code'] == 0 && $response['taken']) {
- \Yii::$app->cache->set('cloud_mch_cache_token_' . $storeCloud->id, $response['taken'], 29 * 3600);
- return $response['taken'];
- }
- }
- $message = isset($response) ? $response : null;
- return '';
- }
- $store_id = $store_id ?: get_store_id();
- $storeCloud = StoreCloud::find()->where(['store_id' => $store_id,'is_delete'=>0])->one();
- if (!$storeCloud) {
- $message = [
- 'code' => 1,
- 'msg' => '未查到云仓用户'
- ];
- return '';
- }
-
- $response = get_cloud_token($storeCloud->name, $storeCloud->password, 1);
- if ($response['code'] == 0 && $response['taken']) {
- \Yii::$app->cache->set('cloud_mch_cache_token_' . $storeCloud->id, $response['taken'], 29 * 3600);
- return $response['taken'];
- }
- $message = isset($response) ? $response : null;
-
- return '';
- }
- }
- if (!function_exists('checkReplay')) {
- /**
- * 检查并拦截重复请求
- */
- function checkReplay($params = [], $time = 10)
- {
- $trace = debug_backtrace();
- $class = $trace[1]['class'] ?? '';
- $function = $trace[1]['function'] ?? '';
- $key = [$class, $function, $params];
- $exists = cache()->exists($key);
- // if(1 || $exists){
- if($exists){
- \Yii::$app->response->format = \Yii\web\Response::FORMAT_JSON;
- \Yii::$app->response->data = [
- 'code' => 1,
- 'msg' => '重复请求,请稍后重试',
- '$exists' => $exists,
- ];
- \Yii::$app->end();
- return false;
- }
- cache()->set($key, 1, $time);
- return true;
- }
- }
- if (! function_exists('client_version')) {
- /**
- * 获取客户端版本
- * @return mixed
- * @author Syan mzsongyan@gmail.com
- * @date 2022-12-16
- */
- function client_version()
- {
- return isset($_GET['version']) ? $_GET['version'] : cyy_version();
- }
- }
- if (! function_exists('getAuthLink')) {
- /**
- * 获取客户端版本
- * @return mixed
- * @author Syan mzsongyan@gmail.com
- * @date 2022-12-16
- */
- /**
- * 微信浏览器h5授权链接
- */
- function getAuthLink($store_id, $redirect_url, int $scope_type) {
- $store_wechat = WechatConfig::findOne(['store_id' => $store_id, 'type' => WechatConfig::TYPE_CONFIG_MP, 'is_delete' => 0]);
- $redirect_url = urlencode($redirect_url);
- $other = '';
- if ($scope_type === 0) {
- $type = "snsapi_base";
- } else {
- $type = "snsapi_userinfo";
- $other = "&connect_redirect=1";
- }
- $app = WechatMini::getWechatConfig($store_id, 0, WechatMini::TYPE_OFFICIAL);
- if (!$app) {
- return '';
- }
- $app_id = $app->getConfig()['app_id'];
- return 'https://open.weixin.qq.com/connect/oauth2/authorize?appid=' .$app_id . '&redirect_uri=' . $redirect_url . '&response_type=code&scope=' . $type . '&state=' . $store_id . $other. '#wechat_redirect';
- }
- }
- function buildTree(array $elements, $parentId = 0) {
- $branch = array();
- foreach ($elements as $element) {
- if ($element['parent_id'] == $parentId) {
- $children = buildTree($elements, $element['id']);
- // 如果该元素有子元素,则将它们添加到children键中
- if ($children) {
- $element['children'] = $children;
- }
- $branch[] = $element;
- }
- }
- return $branch;
- }
- function cpu_count(): int
- {
- // Windows does not support the number of processes setting.
- if (\DIRECTORY_SEPARATOR === '\\') {
- return 1;
- }
- $count = 4;
- if (\is_callable('shell_exec')) {
- if (\strtolower(PHP_OS) === 'darwin') {
- $count = (int)\shell_exec('sysctl -n machdep.cpu.core_count');
- } else {
- $count = (int)\shell_exec('nproc');
- }
- }
- return $count > 0 ? $count : 4;
- }
- function timeAgo($time)
- {
- $currentTime = time();
- $diff = $currentTime - $time;
- $intervals = array(
- '年' => 31536000,
- '月' => 2592000,
- '周' => 604800,
- '天' => 86400,
- '小时' => 3600,
- '分钟' => 60,
- '秒' => 1
- );
- foreach ($intervals as $name => $seconds) {
- $value = floor($diff / $seconds);
- if ($value > 0) {
- return $value . ' ' . $name . '前';
- }
- }
- return '刚刚';
- }
- // 获取服务端微信支付对象
- function getServiceClientWxConfig()
- {
- $keys = [
- 'platform_mch_appid',
- 'platform_mch_key',
- 'platform_mch_id',
- 'platform_apiclient_cert',
- 'platform_apiclient_key',
- 'platform_pay_key',
- ];
- $data = Option::get($keys, 0, 'saas');
- if (empty($data)) {
- $data = [
- 'platform_mch_appid' => '',
- 'platform_mch_key' => '',
- 'platform_mch_id' => '',
- 'platform_apiclient_cert' => '',
- 'platform_apiclient_key' => '',
- 'platform_pay_key' => '',
- ];
- } else {
- $arr = [];
- foreach ($data as $value) {
- $index = array_search($value['name'], $keys);
- unset($keys[$index]);
- $arr[$value['name']] = $value['value'];
- }
- foreach ($keys as $key) {
- $arr[$key] = '';
- }
- $data = $arr;
- }
- // 证书
- if (!is_dir(\Yii::$app->runtimePath . '/pem')) {
- mkdir(\Yii::$app->runtimePath . '/pem');
- file_put_contents(\Yii::$app->runtimePath . '/pem/index.html', '');
- }
- $cert_pem_file = null;
- if (isset($data['platform_apiclient_cert']) && $data['platform_apiclient_cert']) {
- $cert_pem_file = \Yii::$app->runtimePath . '/pem/' . md5($data['platform_apiclient_cert']);
- if (!file_exists($cert_pem_file)) {
- file_put_contents($cert_pem_file, $data['platform_apiclient_cert']);
- }
- }
- $key_pem_file = null;
- if (isset($data['platform_apiclient_key']) && $data['platform_apiclient_key']) {
- $key_pem_file = \Yii::$app->runtimePath . '/pem/' . md5($data['platform_apiclient_key']);
- if (!file_exists($key_pem_file)) {
- file_put_contents($key_pem_file, $data['platform_apiclient_key']);
- }
- }
- return \EasyWeChat\Factory::payment([
- 'app_id' => $data['platform_mch_appid'] ?? '',
- 'secret' => $data['platform_mch_key'] ?? '',
- 'key' => $data['platform_pay_key'] ?? '',
- 'mch_id' => $data['platform_mch_id'] ?? '',
- 'cert_path' => $cert_pem_file,
- 'key_path' => $key_pem_file,
- 'response_type' => 'array'
- ]);
- }
- // 中文字符串处理
- function func_substr_replace($str, $replacement = '*', $start = 1, $length = 3)
- {
- $len = mb_strlen($str,'utf-8');
- if ($len > intval($start+$length)) {
- $str1 = mb_substr($str,0,$start,'utf-8');
- $str2 = mb_substr($str,intval($start+$length),NULL,'utf-8');
- } else {
- $str1 = mb_substr($str,0,1,'utf-8');
- $str2 = mb_substr($str,$len-1,1,'utf-8');
- }
- $new_str = $str1;
- $new_str .= $replacement;
- $new_str .= $replacement;
- $new_str .= $str2;
- return $new_str;
- }
- //获取网络图片到临时目录
- function saveTempFile($url, $type = 'pfx')
- {
- if (strpos($url, 'http') === false) {
- $url = 'http:' . trim($url);
- }
- if (!is_dir(\Yii::$app->runtimePath . '/' . $type)) {
- mkdir(\Yii::$app->runtimePath . '/' . $type);
- }
- $save_path = \Yii::$app->runtimePath . '/'.$type.'/' . md5($url) . '.' . $type;
- \app\utils\CurlHelper::download($url, $save_path);
- return $save_path;
- }
- if (!function_exists('has_table')) {
- // 判断表是否存在
- function has_table($table)
- {
- $has = \Yii::$app->db->createCommand('SHOW TABLES LIKE "' . $table . '" ')->queryOne();
- return (bool)$has;
- }
- }
- if (!function_exists('has_column')) {
- // 判断字段是否存在
- function has_column($table, $column)
- {
- $has = \Yii::$app->db->createCommand('SHOW COLUMNS FROM `' . $table . '` LIKE "' . $column . '" ')->queryOne();
- return (bool)$has;
- }
- }
|