helpers.php 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. use app\constants\OptionSetting;
  8. use app\models\Option;
  9. use app\models\SharingReceiverCustom;
  10. use app\models\StoreCloud;
  11. use app\models\Supplier;
  12. use app\models\WechatConfig;
  13. use app\utils\Wechat\WechatMini;
  14. if (! function_exists('dump')) {
  15. /**
  16. * 调试打印
  17. * @param $var
  18. * @param int $depth
  19. * @param bool $highlight
  20. * @param bool $exit
  21. */
  22. function dump($var, $depth = 10, $highlight = false, $exit = true)
  23. {
  24. echo "<pre>";
  25. yii\helpers\VarDumper::dump($var, $depth, $highlight);
  26. $exit and exit();
  27. }
  28. }
  29. if (! function_exists('camelize')) {
  30. /**
  31. * 下划线转驼峰
  32. */
  33. function camelize($uncamelized_words, $separator = '_', $lcfirst = 0)
  34. {
  35. $res = str_replace(" ", "", ucwords(str_replace($separator, " ", strtolower($uncamelized_words))));
  36. return $lcfirst ? lcfirst($res) : $res;
  37. }
  38. }
  39. if (! function_exists('get_header')) {
  40. /**
  41. * 获取header参数
  42. * @param null $key
  43. * @return array|mixed|null
  44. */
  45. function get_header($key = null)
  46. {
  47. if (Yii::$app instanceof \yii\console\Application){
  48. return null;
  49. }
  50. $headers = Yii::$app->request->getHeaders();
  51. if($key === null){
  52. return $headers->toArray();
  53. }
  54. return $headers[$key];
  55. }
  56. }
  57. if (! function_exists('get_params')) {
  58. /**
  59. * 获取get参数
  60. * @param null $key
  61. * @param null $default
  62. * @return array|mixed|null
  63. */
  64. function get_params($key = null, $default = null)
  65. {
  66. if (Yii::$app instanceof \yii\console\Application){
  67. return null;
  68. }
  69. return Yii::$app->request->get($key, $default);
  70. }
  71. }
  72. if (! function_exists('post_params')) {
  73. /**
  74. * 获取post参数
  75. * @param null $key
  76. * @param null $default
  77. * @return array|mixed|null
  78. */
  79. function post_params($key = null, $default = null)
  80. {
  81. if (Yii::$app instanceof \yii\console\Application){
  82. return null;
  83. }
  84. $post = Yii::$app->request->post($key);
  85. if (! $post) {
  86. $params = json_decode(Yii::$app->request->getRawBody(), true);
  87. $post = $key ? (isset($params[$key]) ? $params[$key] : $default) : $params;
  88. }
  89. return $post;
  90. }
  91. }
  92. if (! function_exists('input_params')) {
  93. /**
  94. * 获取input参数
  95. * @param $key
  96. * @param null $default
  97. * @return array|mixed|null
  98. */
  99. function input_params($key, $default = null)
  100. {
  101. $post = post_params($key);
  102. if (!is_null($post)) {
  103. return $post;
  104. }
  105. return get_params($key, $default);
  106. }
  107. }
  108. if (! function_exists('all_params')) {
  109. /**
  110. * 获取所有参数
  111. * @return array|mixed|null
  112. */
  113. function all_params()
  114. {
  115. $get = get_params() ?: [];
  116. $post = post_params() ?: [];
  117. if (isset($get['store_id'])) {
  118. unset($post['store_id']);
  119. }
  120. return array_merge($get, $post);
  121. }
  122. }
  123. if (! function_exists('input_params_only')) {
  124. /**
  125. * 获取参数中的部分元素
  126. * @param array $keys
  127. * @return array
  128. */
  129. function input_params_only(array $keys)
  130. {
  131. $params = all_params();
  132. $result = [];
  133. foreach ($keys as $key) {
  134. if (isset($params[$key])) {
  135. $result[$key] = $params[$key];
  136. }
  137. }
  138. return $result;
  139. }
  140. }
  141. if (! function_exists('input_params_except')) {
  142. /**
  143. * 排除参数中的部分元素
  144. * @param array $keys
  145. * @return array|mixed|null
  146. */
  147. function input_params_except(array $keys)
  148. {
  149. $params = all_params();
  150. foreach ($keys as $key) {
  151. unset($params[$key]);
  152. }
  153. return $params;
  154. }
  155. }
  156. if (! function_exists('pagination_make')) {
  157. /**
  158. * 构建分页
  159. * @param \yii\db\ActiveQuery $query
  160. * @param bool $asArray
  161. * @return array
  162. */
  163. function pagination_make(yii\db\ActiveQuery $query, $asArray = true, $orderBy = null)
  164. {
  165. $countQuery = clone $query;
  166. $count = $countQuery->count();
  167. $pageNo = input_params('pageNo', input_params('page', 1)) ?: 1;
  168. $pageSize = input_params('pageSize', Yii::$app->params['pageSize']) ?: Yii::$app->params['pageSize'];
  169. $query->offset(($pageNo - 1) * $pageSize)->limit($pageSize);
  170. if ($orderBy) {
  171. $query->orderBy($orderBy);
  172. }
  173. if ($asArray) {
  174. $query->asArray();
  175. }
  176. $list = $query->all();
  177. return [
  178. 'totalCount' => (int)$count,
  179. 'list' => $list,
  180. 'pageNo' => (int)$pageNo,
  181. 'pageSize' => (int)$pageSize,
  182. ];
  183. }
  184. }
  185. if (! function_exists('event_on')) {
  186. /**
  187. * 绑定事件
  188. * @param string $name
  189. * @param callable $handler
  190. * @param mixed $data
  191. * @param bool $append
  192. */
  193. function event_on($name, $handler, $data = null, $append = true)
  194. {
  195. Yii::$app->on($name, $handler, $data, $append);
  196. }
  197. }
  198. if (! function_exists('event_trigger')) {
  199. /**
  200. * 触发事件
  201. * @param $name
  202. * @param \yii\base\Event|null $event
  203. */
  204. function event_trigger($name, yii\base\Event $event = null)
  205. {
  206. Yii::$app->trigger($name, $event);
  207. }
  208. }
  209. if (! function_exists('event_off')) {
  210. /**
  211. * 解除事件
  212. * @param $name
  213. * @param null $handler
  214. * @return bool
  215. */
  216. function event_off($name, $handler = null)
  217. {
  218. return Yii::$app->off($name, $handler);
  219. }
  220. }
  221. if (! function_exists('event_has')) {
  222. /**
  223. * 判断事件是否有绑定处理程序
  224. * @param $name
  225. * @return bool
  226. */
  227. function event_has($name)
  228. {
  229. return Yii::$app->hasEventHandlers($name);
  230. }
  231. }
  232. if (! function_exists('cache')) {
  233. /**
  234. * 返回cache组件
  235. * @return \yii\caching\CacheInterface
  236. */
  237. function cache()
  238. {
  239. return Yii::$app->cache;
  240. }
  241. }
  242. if (! function_exists('cacheLock')) {
  243. /**
  244. * 返回cache组件
  245. * @return \yii\caching\CacheInterface
  246. */
  247. function cacheLock()
  248. {
  249. return Yii::$app->cacheLock;
  250. }
  251. }
  252. if (! function_exists('cache_lock')) {
  253. /**
  254. * cache缓存锁
  255. */
  256. function cache_lock($key, $ttl)
  257. {
  258. if(is_array($key)){
  259. $key = implode('_', $key);
  260. }
  261. $cacheV = cache()->get($key);
  262. if($cacheV !== false){
  263. return $cacheV;
  264. }
  265. cache()->set($key, 1, $ttl);
  266. return null;
  267. }
  268. function cache_lock_del($key)
  269. {
  270. if(is_array($key)){
  271. $key = implode('_', $key);
  272. }
  273. cache()->delete($key);
  274. }
  275. }
  276. if (! function_exists('http_client')) {
  277. /**
  278. * 返回http客户端
  279. * @param array $config
  280. * @return \GuzzleHttp\Client
  281. */
  282. function http_client($config = [])
  283. {
  284. $config = array_merge([
  285. 'verify' => false, // 不验证sll
  286. ], $config);
  287. return new GuzzleHttp\Client($config);
  288. }
  289. }
  290. if (! function_exists('http_request')) {
  291. /**
  292. * 返回http请求对象
  293. * @param $method
  294. * @param string $uri
  295. * @param array $options
  296. * @return \Psr\Http\Message\ResponseInterface
  297. * @throws \GuzzleHttp\Exception\GuzzleException
  298. */
  299. function http_request($method, $uri = '', array $options = [])
  300. {
  301. $client = http_client();
  302. return $client->request($method, $uri, $options);
  303. }
  304. }
  305. if (! function_exists('http_get')) {
  306. /**
  307. * get请求
  308. * @param string $uri
  309. * @param array $options
  310. * @return \Psr\Http\Message\ResponseInterface
  311. * @throws \GuzzleHttp\Exception\GuzzleException
  312. */
  313. function http_get($uri = '', array $options = [])
  314. {
  315. return http_request('GET', $uri, $options);
  316. }
  317. }
  318. if (! function_exists('http_post')) {
  319. /**
  320. * post请求
  321. * @param string $uri
  322. * @param array $options
  323. * @return \Psr\Http\Message\ResponseInterface
  324. * @throws \GuzzleHttp\Exception\GuzzleException
  325. */
  326. function http_post($uri = '', array $options = [])
  327. {
  328. return http_request('POST', $uri, $options);
  329. }
  330. }
  331. if (! function_exists('get_admin')) {
  332. /**
  333. * 获取Admin对象
  334. * @return app\models\Admin|null
  335. */
  336. function get_admin()
  337. {
  338. return Yii::$app->jwt->getAdmin();
  339. }
  340. }
  341. if (! function_exists('get_saas_purchase_store_cloud')) {
  342. /**
  343. * 获取saas用户采购店铺前台store_cloud
  344. * @return Object|null
  345. */
  346. function get_saas_purchase_store_cloud($saas_id = 0)
  347. {
  348. // $saas_id = $saas_id ? $saas_id : get_saas_user_id();
  349. // $item = app\models\StoreCloud::findOne(['saas_user_id' => $saas_id, 'is_delete' => 0, 'is_enable' => 1]);
  350. if (!$saas_id) {
  351. try {
  352. $payload = \Yii::$app->jwt->getPayload();
  353. $store_admin_id = $payload['store_admin_id'];
  354. if ($store_admin_id > 0) {
  355. $StoreMiniAdmin = \app\models\StoreAdmin::findOne(['id' => $store_admin_id, 'status' => 1, 'is_delete' => 0]);
  356. $store_id = $StoreMiniAdmin->store_id;
  357. } else {
  358. $admin = \app\models\Admin::findOne(['id' => $payload['admin_id']]);
  359. $store_id = $admin->type_id;
  360. }
  361. $item = \app\models\StoreCloud::findOne(['store_id' => $store_id, 'is_delete' => 0, 'is_enable' => 1]);
  362. } catch (\Exception $e) {
  363. $item = null;
  364. }
  365. } else {
  366. $item = app\models\StoreCloud::findOne(['saas_user_id' => $saas_id, 'is_delete' => 0, 'is_enable' => 1]);
  367. }
  368. return $item;
  369. }
  370. }
  371. if (! function_exists('get_saas_purchase_store_id')) {
  372. /**
  373. * 获取saas用户采购店铺前台store_id
  374. * @return int
  375. */
  376. function get_saas_purchase_store_id($saas_id = 0)
  377. {
  378. $item = get_saas_purchase_store_cloud($saas_id);
  379. if ($item) {
  380. return $item->store_id;
  381. }
  382. return 0;
  383. }
  384. }
  385. if (! function_exists('get_store_id')) {
  386. /**
  387. * 获取前后台store_id
  388. * @return int
  389. */
  390. function get_store_id()
  391. {
  392. $params = all_params();
  393. if (isset($params['store_id'])) {
  394. return $params['store_id'];
  395. }
  396. $admin = get_admin();
  397. return $admin ? $admin->store_id : DEFAULT_STORE_ID;
  398. }
  399. }
  400. if (! function_exists('get_mini_id')) {
  401. /**
  402. * 获取前后台store_id
  403. * @return int
  404. */
  405. function get_mini_id()
  406. {
  407. $mini_id = get_params('mini_id', post_params('mini_id'));
  408. if ($mini_id) {
  409. return $mini_id;
  410. }else{
  411. return 0;
  412. }
  413. }
  414. }
  415. if (! function_exists('get_md_id')) {
  416. /**
  417. * 获取md_id
  418. * @return int
  419. */
  420. function get_md_id()
  421. {
  422. if (Yii::$app instanceof \yii\console\Application){
  423. return 0;
  424. }
  425. $md_id = input_params('md_id');
  426. if ((int)$md_id === 0) {
  427. return 0;
  428. }
  429. if ($md_id !== null) {
  430. $md = \app\models\Md::findOne(['id' => $md_id, 'is_delete' => 0]);
  431. if ($md && $md->store_id == get_store_id()) {
  432. return $md_id;
  433. }
  434. return -1;
  435. }
  436. $admin = get_admin();
  437. return $admin && $admin->type == \app\models\Admin::ADMIN_TYPE_MD ? $admin->type_id : 0;
  438. }
  439. }
  440. if (! function_exists('get_mch_brands_id')) {
  441. /**
  442. * 获取md_id
  443. * @return int
  444. */
  445. function get_mch_brands_id()
  446. {
  447. if (Yii::$app instanceof \yii\console\Application){
  448. return 0;
  449. }
  450. $brands_id = input_params('brands_id');
  451. if ((int)$brands_id === 0) {
  452. return 0;
  453. }
  454. if ($brands_id !== null) {
  455. $mchBrands = \app\models\MchBrands::findOne(['id' => $brands_id, 'is_delete' => 0]);
  456. if ($mchBrands && $mchBrands->store_id == get_store_id()) {
  457. return $brands_id;
  458. }
  459. return -1;
  460. }
  461. $admin = get_admin();
  462. return $admin && $admin->type == \app\models\Admin::ADMIN_TYPE_MCH_BRANDS ? $admin->type_id : 0;
  463. }
  464. }
  465. if (! function_exists('current_platform')) {
  466. /**
  467. * 获取当前平台
  468. * @return string
  469. */
  470. function current_platform()
  471. {
  472. return get_params('platform');
  473. }
  474. }
  475. if (! function_exists('is_platform')) {
  476. /**
  477. * 是否为平台小程序
  478. * @return bool
  479. */
  480. function is_platform()
  481. {
  482. return isset($_GET['is_platform']) && $_GET['is_platform'] === 'yes' ? true : false;
  483. }
  484. }
  485. if (! function_exists('is_single_store')) {
  486. /**
  487. * 是否为单店铺
  488. * @return bool
  489. */
  490. function is_single_store()
  491. {
  492. return isset($_GET['is_single_store']) && $_GET['is_single_store'] === 'yes' ? true : false;
  493. }
  494. }
  495. if (! function_exists('self_mini')) {
  496. /**
  497. * 是否为单店铺独立小程序
  498. * @return bool
  499. */
  500. function self_mini()
  501. {
  502. return get_params('self_mini', false);
  503. }
  504. }
  505. if (! function_exists('show_shenhe')) {
  506. /**
  507. * 是否要显示审核数据
  508. * @return bool
  509. */
  510. function show_shenhe()
  511. {
  512. $data = app\models\Store::mpAudit(get_store_id());
  513. return $data['status'];
  514. // if(client_version() == 18.1) return true;
  515. // return isset($_GET['show_shenhe']) && $_GET['show_shenhe'] ? true : false;
  516. }
  517. }
  518. if (! function_exists('is_isv')) {
  519. /**
  520. * 是否为平台小程序
  521. * @return bool
  522. */
  523. function is_isv()
  524. {
  525. return isset($_GET['is_isv']) && $_GET['is_isv'] === 'yes' ? true : false;
  526. }
  527. }
  528. if (! function_exists('is_app_platform')) {
  529. /**
  530. * 是否为app
  531. * @return bool
  532. */
  533. function is_app_platform()
  534. {
  535. return get_params('platform') === 'app';
  536. }
  537. }
  538. if (! function_exists('is_alipay_platform')) {
  539. /**
  540. * 是否为支付宝小程序
  541. * @return bool
  542. */
  543. function is_alipay_platform()
  544. {
  545. return get_params('platform') === 'alipay';
  546. }
  547. }
  548. if (! function_exists('is_toutiao_platform')) {
  549. /**
  550. * 是否为头条小程序
  551. * @return bool
  552. */
  553. function is_toutiao_platform()
  554. {
  555. return get_params('platform') === 'bytedance';
  556. }
  557. }
  558. if (! function_exists('is_wechat_platform')) {
  559. /**
  560. * 是否为微信小程序
  561. * @return bool
  562. */
  563. function is_wechat_platform()
  564. {
  565. return get_params('platform', 'wechat') === 'wechat';
  566. }
  567. }
  568. if (! function_exists('is_merchant')) {
  569. /**
  570. * 是否为批发端小程序
  571. * @return bool
  572. */
  573. function is_merchant()
  574. {
  575. return isset($_GET['mini_type']) && $_GET['mini_type'] === 'merchant' ? true : false;
  576. }
  577. }
  578. if (! function_exists('get_mch_id')) {
  579. /**
  580. * 获取后台mch_id
  581. * @return int
  582. */
  583. function get_mch_id()
  584. {
  585. $mch_id = get_params('mch_id', post_params('mch_id'));
  586. if ($mch_id != 0) {
  587. return $mch_id;
  588. }
  589. $admin = get_admin();
  590. return $admin && $admin->type === \app\models\Admin::ADMIN_TYPE_MCH ? $admin->type_id : 0;
  591. }
  592. }
  593. if (! function_exists('get_supplier_id')) {
  594. /**
  595. * 获取后台supplier_id
  596. * @return int
  597. */
  598. function get_supplier_id()
  599. {
  600. $admin = get_admin();
  601. $supplier_id = $admin && $admin->type === \app\models\Admin::ADMIN_TYPE_SUPPLIER ? $admin->type_id : 0;
  602. return input_params('supplier_id', $supplier_id);
  603. }
  604. }
  605. if (! function_exists('get_bd_id')) {
  606. /**
  607. * 获取后台推广代理id
  608. * @return int
  609. */
  610. function get_bd_id()
  611. {
  612. $admin = get_admin();
  613. return $admin && $admin->type === \app\models\Admin::ADMIN_TYPE_BD_AGENT ? $admin->id : 0;
  614. }
  615. }
  616. if (! function_exists('get_plugin')) {
  617. function get_plugin()
  618. {
  619. $route = \Yii::$app->requestedRoute;
  620. $route = str_replace('\\', '/', $route);
  621. $match = explode('/', $route);
  622. return $match;
  623. }
  624. }
  625. if (! function_exists('get_plugin_type')) {
  626. function get_plugin_type()
  627. {
  628. $plugin = get_plugin();
  629. $list = [
  630. 'goods' => 0,
  631. 'pond' => 1,
  632. 'bargain' => 2,
  633. 'lottery' => 4,
  634. 'step' => 5,
  635. 'scanCodePay' => 6, // 当面付
  636. ];
  637. if (isset($list[$plugin[1]])) {
  638. $type = $list[$plugin[1]];
  639. } else {
  640. $type = 0;
  641. }
  642. return $type;
  643. }
  644. }
  645. if (! function_exists('cyy_version')) {
  646. function cyy_version()
  647. {
  648. static $version = null;
  649. if ($version) {
  650. return $version;
  651. }
  652. $file = __DIR__ . '/../version.json';
  653. if (!file_exists($file)) {
  654. throw new Exception('Version not found');
  655. }
  656. $res = json_decode(file_get_contents($file), true);
  657. if (!is_array($res)) {
  658. throw new Exception('Version cannot be decoded');
  659. }
  660. return $version = $res['version'];
  661. }
  662. }
  663. if (!function_exists('pay_notify_url')) {
  664. /**
  665. * 拼接支付回调地址
  666. *
  667. * @param string $suffix
  668. * @return string
  669. */
  670. function pay_notify_url($suffix, $store_id = 0, $is_platform = false)
  671. {
  672. try {
  673. $hostInfo = \Yii::$app->request->hostInfo;
  674. $store_id = $store_id ?: get_store_id();
  675. $hostInfo = str_replace('http:', 'https:', $hostInfo);
  676. // if (\Yii::$app->request->getIsSecureConnection()) {
  677. // $hostInfo = str_replace('http:', 'https:', $hostInfo);
  678. // } else {
  679. // $hostInfo = str_replace('https:', 'http:', $hostInfo);
  680. // }
  681. if ($is_platform) {
  682. $store_id = 0;
  683. }
  684. $hostInfo .= '/index.php/' . $suffix . '/' . $store_id;
  685. } catch (\Exception $e) {
  686. $hostInfo = '';
  687. }
  688. return $hostInfo;
  689. }
  690. }
  691. if (!function_exists('pay_return_url')) {
  692. function path_h5()
  693. {
  694. $path = 'h5';
  695. if (\Yii::$app->prod_is_dandianpu()) {
  696. $path = \Yii::$app->params['uri_path_dandianpuh5'];
  697. }
  698. return $path;
  699. }
  700. /**
  701. * 拼接支付回跳地址
  702. *
  703. * @param string $suffix
  704. * @return string
  705. */
  706. function pay_return_url($suffix)
  707. {
  708. $hostInfo = \Yii::$app->request->hostInfo;
  709. $hostInfo = str_replace('http:', 'https:', $hostInfo);
  710. // if (\Yii::$app->request->getIsSecureConnection()) {
  711. // $hostInfo = str_replace('http:', 'https:', $hostInfo);
  712. // } else {
  713. // $hostInfo = str_replace('https:', 'http:', $hostInfo);
  714. // }
  715. $hostInfo .= '/' . path_h5() . '/#/' . $suffix;
  716. return $hostInfo;
  717. }
  718. }
  719. if (! function_exists('get_user')) {
  720. /**
  721. * 获取前端用户对象
  722. * @return app\models\User|null
  723. */
  724. function get_user()
  725. {
  726. return Yii::$app->jwt->getUser();
  727. }
  728. }
  729. if (! function_exists('get_saas_user')) {
  730. /**
  731. * 获取前端用户对象
  732. * @return app\models\SaasUser|null
  733. */
  734. function get_saas_user()
  735. {
  736. return Yii::$app->jwt->getSaasUser();
  737. }
  738. }
  739. if (! function_exists('get_saas_user_id')) {
  740. /**
  741. * 获取前端用户对象id
  742. * @return app\models\SaasUser|null
  743. */
  744. function get_saas_user_id()
  745. {
  746. $saasUser = get_saas_user();
  747. return $saasUser ? $saasUser->id : 0;
  748. }
  749. }
  750. if (! function_exists('get_user_id')) {
  751. /**
  752. * 获取前端用户对象id
  753. * @return int
  754. */
  755. function get_user_id()
  756. {
  757. $user = get_user();
  758. return $user ? $user->id : 0;
  759. }
  760. }
  761. if (!function_exists('env')) {
  762. /**
  763. * Gets the value of an environment variable.
  764. *
  765. * @param string $key
  766. * @param mixed $default
  767. * @param string $delimiter
  768. * @return mixed
  769. */
  770. function env($key, $default = null, $delimiter = '')
  771. {
  772. $value = getenv($key);
  773. if ($value === false) {
  774. return value($default);
  775. }
  776. switch (strtolower($value)) {
  777. case 'true':
  778. case '(true)':
  779. return true;
  780. case 'false':
  781. case '(false)':
  782. return false;
  783. case 'empty':
  784. case '(empty)':
  785. return '';
  786. case 'null':
  787. case '(null)':
  788. return;
  789. }
  790. if (strlen($value) > 1 && str_starts_with($value, '"') && str_ends_with($value, '"')) {
  791. $value = substr($value, 1, -1);
  792. }
  793. if (strlen($delimiter) > 0) {
  794. if (strlen($value) == 0) {
  795. $value = $default;
  796. } else {
  797. $value = explode($delimiter, $value);
  798. }
  799. }
  800. return $value;
  801. }
  802. }
  803. if (!function_exists('value')) {
  804. /**
  805. * Return the default value of the given value.
  806. *
  807. * @param mixed $value
  808. * @return mixed
  809. */
  810. function value($value)
  811. {
  812. return $value instanceof Closure ? $value() : $value;
  813. }
  814. }
  815. if (! function_exists('queue_push')) {
  816. /**
  817. * 消息队列push
  818. * @param yii\queue\JobInterface|mixed $job
  819. * @param int 延迟时间,单位秒
  820. * @return string|null id of a job message
  821. */
  822. function queue_push($job, $delay = 0, $checkReplay = 0)
  823. {
  824. if($checkReplay){
  825. // debug_log(serialize($job) . $delay);
  826. $cache = cacheLock();
  827. $k = [serialize($job), $delay];
  828. $id = $cache->get($k);
  829. if($id){
  830. // debug_log('cache value:' . $id);
  831. if(!queue_is_done($id)){
  832. return $id;
  833. }
  834. $cache->delete($k);
  835. }
  836. }
  837. $queue = \Yii::$app->queue;
  838. if ($delay > 0) {
  839. $id = $queue->delay($delay)->push($job);
  840. }else{
  841. $id = $queue->push($job);
  842. }
  843. if($checkReplay){
  844. $cache->set($k, $id, 86400);
  845. // debug_log($id);
  846. }
  847. return $id;
  848. }
  849. }
  850. if (! function_exists('queue_remove')) {
  851. /**
  852. * 消息队列移除
  853. * @param string $id
  854. * @return bool
  855. */
  856. function queue_remove($id)
  857. {
  858. return \Yii::$app->queue->remove($id);
  859. }
  860. }
  861. if (! function_exists('queue_is_done')) {
  862. /**
  863. * 消息队列是否完成
  864. * @param string $id
  865. * @return bool
  866. */
  867. function queue_is_done($id)
  868. {
  869. return \Yii::$app->queue->isDone($id);
  870. }
  871. }
  872. if (! function_exists('queue_is_waiting')) {
  873. /**
  874. * 消息队列是否等待中
  875. * @param string $id
  876. * @return bool
  877. */
  878. function queue_is_waiting($id)
  879. {
  880. return \Yii::$app->queue->isWaiting($id);
  881. }
  882. }
  883. if (! function_exists('is_profit_sharing')) {
  884. /**
  885. * 是否走分账
  886. * wx 微信 ali 支付宝
  887. * @return bool
  888. */
  889. function is_profit_sharing()
  890. {
  891. $transfer_profit = \app\models\Store::findOne(get_store_id())->transfer_profit;
  892. return \Yii::$app->isSaas() && $transfer_profit > 0;
  893. }
  894. }
  895. if (! function_exists('is_profit_pay')) {
  896. /**
  897. * 是否走分账服务商支付
  898. * wx 微信 ali 支付宝
  899. * @return bool
  900. */
  901. function is_profit_pay($type = 'wx', $store_id = 0)
  902. {
  903. if (\Yii::$app->isSaas()) {
  904. return true;
  905. }else{
  906. return false;
  907. }
  908. // if (\Yii::$app->isSaas()) {
  909. // $store_id = $store_id ? $store_id : get_store_id();
  910. // //会员卡支付给平台部分账
  911. // if($store_id == -1){
  912. // return false;
  913. // }
  914. // $transfer_profit = \app\models\Store::findOne($store_id)->transfer_profit;
  915. // if ($transfer_profit > 0) {
  916. // return true;
  917. // }
  918. // if ($type == 'wx') {
  919. // $wechatConfig = \app\models\WechatConfig::find()->where(['store_id' => $store_id, 'type' => 1])->one();
  920. // if (!$wechatConfig->pay_key) {
  921. // return true;
  922. // }
  923. // } elseif ($type == 'ali') {
  924. // $aliConfig = \app\models\Option::get(\app\models\Option::OPTOPN_KEY, $store_id, 'alipay');
  925. // if (!$aliConfig) {
  926. // return false;
  927. // }
  928. // $aliConfig = json_decode($aliConfig['value'], true);
  929. // if (!$aliConfig['alipay_public_key']) {
  930. // return true;
  931. // }
  932. // }
  933. // return false;
  934. // } else {
  935. // return false;
  936. // }
  937. }
  938. }
  939. if (! function_exists('is_open_platform')) {
  940. /**
  941. * 判断小程序是否授权第三方
  942. * wx 微信 ali 支付宝
  943. * @return bool
  944. */
  945. function is_open_platform($type = 'wx', $store_id = 0)
  946. {
  947. if (\Yii::$app->isSaas()) {
  948. return true;
  949. }else{
  950. return false;
  951. }
  952. // $store_id = $store_id ? $store_id : get_store_id();
  953. // if ($type == 'wx') {
  954. // $wechatConfig = \app\models\WechatConfig::find()->where(['store_id' => $store_id, 'type' => 1])->one();
  955. // if (!empty($wechatConfig) && !$wechatConfig->app_secret) {
  956. // return true;
  957. // }
  958. // } elseif ($type == 'ali') {
  959. // return true;
  960. // }
  961. // return false;
  962. }
  963. }
  964. if (! function_exists('market_alicloudapi_com')) {
  965. /**
  966. * 阿里云市场接口
  967. */
  968. function market_alicloudapi_com($appcode, $url, $querys = '', $method = "GET") {
  969. $host = $url;
  970. $headers = array();
  971. array_push($headers, "Authorization:APPCODE " . $appcode);
  972. $url = $url . "?" . $querys;
  973. $curl = curl_init();
  974. if($method == 'POST'){
  975. array_push($headers, "Content-Type".":"."application/x-www-form-urlencoded; charset=UTF-8");
  976. curl_setopt($curl, CURLOPT_POSTFIELDS, $querys);
  977. }
  978. curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
  979. curl_setopt($curl, CURLOPT_URL, $url);
  980. curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
  981. curl_setopt($curl, CURLOPT_FAILONERROR, false);
  982. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  983. curl_setopt($curl, CURLOPT_HEADER, false);
  984. if (1 == strpos("$".$host, "https://"))
  985. {
  986. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  987. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  988. }
  989. return (curl_exec($curl));
  990. }
  991. }
  992. if (! function_exists('cloud_post')) {
  993. /**
  994. * 云仓post请求
  995. */
  996. function cloud_post($url, $data_string) {
  997. if(is_array($data_string)) {
  998. $data_string['version'] = cyy_version();
  999. $data_string = json_encode($data_string);
  1000. }
  1001. $ch = curl_init();
  1002. curl_setopt($ch, CURLOPT_POST, 1);
  1003. curl_setopt($ch, CURLOPT_URL, $url);
  1004. curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
  1005. curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  1006. 'Content-Type: application/json; charset=utf-8',
  1007. 'Content-Length: ' . strlen($data_string))
  1008. );
  1009. ob_start();
  1010. curl_exec($ch);
  1011. $return_content = ob_get_contents();
  1012. ob_end_clean();
  1013. return $return_content;
  1014. //$return_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  1015. //return array($return_code, $return_content);
  1016. }
  1017. }
  1018. if (! function_exists('cloud_get')) {
  1019. /**
  1020. * 云仓post请求
  1021. */
  1022. function cloud_get($url,$type=1) {
  1023. $ch = curl_init();
  1024. curl_setopt($ch, CURLOPT_URL, $url);
  1025. ob_start();
  1026. curl_exec($ch);
  1027. $return_content = ob_get_contents();
  1028. ob_end_clean();
  1029. return $return_content;
  1030. }
  1031. }
  1032. if (!function_exists('union_share_scale_check')) {
  1033. /**
  1034. * 联盟分销比例检查是否超过100
  1035. * @param mixed $store
  1036. * @param bool $exclude_referral
  1037. * @return mixed
  1038. * @throws \yii\base\InvalidConfigException
  1039. * @throws \yii\base\NotSupportedException
  1040. * @throws \yii\db\Exception
  1041. * @throws \yii\base\InvalidArgumentException
  1042. * @throws \Exception
  1043. * @author Syan mzsongyan@gmail.com
  1044. * @date 2022-05-14
  1045. */
  1046. function union_share_scale_check($store, $exclude_referral = false)
  1047. {
  1048. $scale = 0;
  1049. $defaultSet = Option::get(OptionSetting::SHARE_STORE_DEFAULT_SETTING, -1, OptionSetting::SHARE_GROUP_NAME, '{}');
  1050. $defaultSet = json_decode($defaultSet['value'], true);
  1051. if(intval($store->store_share_switch) && $store->store_share_value == 0){
  1052. if(!empty($defaultSet['store_share_switch']) && !empty($defaultSet['store_share_value'])){
  1053. $store->store_share_value = $defaultSet['store_share_value'];
  1054. }
  1055. }
  1056. // 店铺比例
  1057. $scale += $store->store_share_value;
  1058. // 联盟分销
  1059. $union_option = json_decode($store->share_setting, true);
  1060. if(intval($union_option['level']) && $union_option['level_one'] == 0 && $union_option['level_two'] == 0 && $union_option['level_three'] == 0){
  1061. $defaultSet = Option::get(OptionSetting::SHARE_SAAS_DEFAULT_SETTING, -1, OptionSetting::SHARE_GROUP_NAME, '{}');
  1062. $defaultSet = json_decode($defaultSet['value'], true);
  1063. if(!empty($defaultSet['level']) &&
  1064. (!empty($defaultSet['level_one']) || !empty($defaultSet['level_two']) || !empty($defaultSet['level_three']))){
  1065. $union_option['level_one'] = $defaultSet['level_one'];
  1066. $union_option['level_two'] = $defaultSet['level_two'];
  1067. $union_option['level_three'] = $defaultSet['level_three'];
  1068. }
  1069. }
  1070. if ($union_option && is_array($union_option)) {
  1071. $scale = $scale +
  1072. $union_option['level_one'] +
  1073. $union_option['level_two'] +
  1074. $union_option['level_three'];
  1075. }
  1076. if (!$exclude_referral) {
  1077. // // 推荐人
  1078. $referral = app\models\SaasStoreReferral::findOne(['store_id' => $store->id, 'type' => 1]);
  1079. if(intval($referral->is_enable) && $referral->referral_rebate == 0){
  1080. $defaultSet = Option::get(OptionSetting::SHARE_SAAS_REFERRAL_DEFAULT_SETTING, -1, OptionSetting::SHARE_GROUP_NAME, '{}');
  1081. $defaultSet = json_decode($defaultSet['value'], true);
  1082. if(!empty($defaultSet['is_enable']) && !empty($defaultSet['referral_rebate'])){
  1083. $referral->referral_rebate = $defaultSet['referral_rebate'];
  1084. }
  1085. }
  1086. // // 推荐人
  1087. $scale += $referral->referral_rebate;
  1088. }
  1089. if(intval($store->self_rebate_switch) && $store->self_rebate_value == 0) {
  1090. $defaultSet = Option::get(OptionSetting::SHARE_SELF_REBATE_DEFAULT_SETTING, -1, OptionSetting::SHARE_GROUP_NAME, '{}');
  1091. $defaultSet = json_decode($defaultSet['value'], true);
  1092. if (!empty($defaultSet['self_rebate_switch']) && !empty($defaultSet['self_rebate_value'])) {
  1093. $store->self_rebate_value = $defaultSet['self_rebate_value'];
  1094. }
  1095. }
  1096. // 消费返利
  1097. $scale += $store->self_rebate_value;
  1098. $store_total_profit = SharingReceiverCustom::find()->where([
  1099. 'sharing_way' => SharingReceiverCustom::SHARING_WAY_STORE,
  1100. 'sharing_store_id' => $store->id,
  1101. 'is_delete' => SharingReceiverCustom::IS_DELETE_NO
  1102. ])->sum('sharing_profit') ?: 0;
  1103. if ($store_total_profit + $scale > 100) {
  1104. return false;
  1105. }
  1106. return union_share_scale($store, $exclude_referral) <= 100;
  1107. }
  1108. }
  1109. if (!function_exists('union_share_scale')) {
  1110. /**
  1111. * 联盟分销比例
  1112. * @param mixed $store
  1113. * @param bool $exclude_referral
  1114. */
  1115. function union_share_scale($store, $exclude_referral = false, $custom_sharing_id = 0)
  1116. {
  1117. $scale = 0;
  1118. $defaultSet = Option::get(OptionSetting::SHARE_STORE_DEFAULT_SETTING, -1, OptionSetting::SHARE_GROUP_NAME, '{}');
  1119. $defaultSet = json_decode($defaultSet['value'], true);
  1120. if(intval($store->store_share_switch) && $store->store_share_value == 0){
  1121. if(!empty($defaultSet['store_share_switch']) && !empty($defaultSet['store_share_value'])){
  1122. $store->store_share_value = $defaultSet['store_share_value'];
  1123. }
  1124. }
  1125. // 店铺比例
  1126. $scale += $store->store_share_value;
  1127. $union_option = json_decode($store->share_setting, true);
  1128. if(intval($union_option['level']) && $union_option['level_one'] == 0 && $union_option['level_two'] == 0 && $union_option['level_three'] == 0){
  1129. $defaultSet = Option::get(OptionSetting::SHARE_SAAS_DEFAULT_SETTING, -1, OptionSetting::SHARE_GROUP_NAME, '{}');
  1130. $defaultSet = json_decode($defaultSet['value'], true);
  1131. if(!empty($defaultSet['level']) &&
  1132. (!empty($defaultSet['level_one']) || !empty($defaultSet['level_two']) || !empty($defaultSet['level_three']))){
  1133. $union_option['level_one'] = $defaultSet['level_one'];
  1134. $union_option['level_two'] = $defaultSet['level_two'];
  1135. $union_option['level_three'] = $defaultSet['level_three'];
  1136. }
  1137. }
  1138. // 联盟分销
  1139. if ($union_option && is_array($union_option)) {
  1140. $scale = $scale +
  1141. $union_option['level_one'] +
  1142. $union_option['level_two'] +
  1143. $union_option['level_three'];
  1144. }
  1145. if(intval($store->self_rebate_switch) && $store->self_rebate_value == 0) {
  1146. $defaultSet = Option::get(OptionSetting::SHARE_SELF_REBATE_DEFAULT_SETTING, -1, OptionSetting::SHARE_GROUP_NAME, '{}');
  1147. $defaultSet = json_decode($defaultSet['value'], true);
  1148. if (!empty($defaultSet['self_rebate_switch']) && !empty($defaultSet['self_rebate_value'])) {
  1149. $store->self_rebate_value = $defaultSet['self_rebate_value'];
  1150. }
  1151. }
  1152. // 消费返利
  1153. $scale += $store->self_rebate_value;
  1154. if (!$exclude_referral) {
  1155. $referral = app\models\SaasStoreReferral::findOne(['store_id' => $store->id, 'type' => 1]);
  1156. if(intval($referral->is_enable) && $referral->referral_rebate == 0){
  1157. $defaultSet = Option::get(OptionSetting::SHARE_SAAS_REFERRAL_DEFAULT_SETTING, -1, OptionSetting::SHARE_GROUP_NAME, '{}');
  1158. $defaultSet = json_decode($defaultSet['value'], true);
  1159. if(!empty($defaultSet['is_enable']) && !empty($defaultSet['referral_rebate'])){
  1160. $referral->referral_rebate = $defaultSet['referral_rebate'];
  1161. }
  1162. }
  1163. // // 推荐人
  1164. $scale += $referral->referral_rebate;
  1165. }
  1166. $store_total_profit = SharingReceiverCustom::find()->where([
  1167. 'sharing_way' => SharingReceiverCustom::SHARING_WAY_STORE,
  1168. 'sharing_store_id' => $store->id,
  1169. 'is_delete' => SharingReceiverCustom::IS_DELETE_NO
  1170. ])->andWhere(['<>', 'id', $custom_sharing_id])->sum('sharing_profit') ?: 0;
  1171. return $store_total_profit + $scale;
  1172. }
  1173. }
  1174. if (!function_exists('is_h5')) {
  1175. /**
  1176. * 判断是否为h5端
  1177. * @return bool
  1178. * @author Syan mzsongyan@gmail.com
  1179. * @date 2022-05-23
  1180. */
  1181. function is_h5()
  1182. {
  1183. return in_array(get_params('_form'), ['h5', 'official']);
  1184. }
  1185. }
  1186. /**
  1187. * 向下取小数点后n
  1188. */
  1189. if (!function_exists('floor_num')) {
  1190. function floor_num($number, $floor = 2)
  1191. {
  1192. $floor_num = pow(10, $floor);
  1193. return floor($number * $floor_num) / $floor_num;
  1194. }
  1195. }
  1196. // 调试函数
  1197. if (! function_exists('debug_log')) {
  1198. function debug_log($msg, $file = 'debug.log')
  1199. {
  1200. date_default_timezone_set('PRC');
  1201. $file = \Yii::$app->basePath . '/runtime/logs/' . str_replace('\\', '_', $file);
  1202. if (is_array($msg)) {
  1203. $msg = json_encode($msg, JSON_UNESCAPED_UNICODE);
  1204. }
  1205. $message = date('Y-m-d H:i:s') . ' ' . $msg . PHP_EOL;
  1206. $log = error_log($message, 3, $file);
  1207. @chmod($file, 0777);
  1208. return $log;
  1209. }
  1210. }
  1211. if (! function_exists('stbz_client')) {
  1212. /**
  1213. * 胜天半子client
  1214. * @return \Stbz\Api\SupplyClient
  1215. * @author Syan mzsongyan@gmail.com
  1216. * @date 2022-09-13
  1217. */
  1218. function stbz_client($url = '')
  1219. {
  1220. $option = \app\models\Option::get('stbz_account', 0, 'saas', \json_encode([
  1221. 'key' => '',
  1222. 'secret' => '',
  1223. 'url' => ''
  1224. ]));
  1225. $option = json_decode($option['value'], true);
  1226. $SupplyClient = new \Stbz\Api\SupplyClient($option['key'], $option['secret']);
  1227. if (!empty($option['url'])) {
  1228. $SupplyClient->serverRoot = $option['url'];
  1229. }
  1230. if (!empty($url)) {
  1231. $SupplyClient->serverRoot = $url;
  1232. }
  1233. return $SupplyClient;
  1234. }
  1235. }
  1236. if (! function_exists('get_cloud_token')) {
  1237. /**
  1238. * 获取云仓token
  1239. * @return \Stbz\Api\SupplyClient
  1240. * @author Syan mzsongyan@gmail.com
  1241. * @date 2022-09-13
  1242. */
  1243. function get_cloud_token($name, $password, $is_use_platform = 0)
  1244. {
  1245. $url = "/user/getToken";
  1246. $data = [];
  1247. $data['name'] = $name;
  1248. $data['pwd'] = $password;
  1249. if ($is_use_platform) {
  1250. $data['token'] = get_platform_token();
  1251. }
  1252. $domain = (new OptionSetting)->getCloudDomainName();
  1253. $response = cloud_post($domain . $url, $data);
  1254. //\Yii::error('token', $response);
  1255. return json_decode($response,true);
  1256. }
  1257. }
  1258. if (! function_exists('get_platform_token')) {
  1259. /**
  1260. * 生成保存云仓token
  1261. * @return \Stbz\Api\SupplyClient
  1262. * @author Syan mzsongyan@gmail.com
  1263. * @date 2022-09-13
  1264. */
  1265. function get_platform_token()
  1266. {
  1267. // $content = \Yii::$app->cache->get('cloud_admin_cache_token');
  1268. // if ($content) {
  1269. // return $content;
  1270. // }
  1271. $content = Option::get('cloud', 0, 'saas');
  1272. $params = json_decode($content['value'], true);
  1273. $response = get_cloud_token($params['name'], $params['pwd']);
  1274. if ($response['code'] == 0 && $response['taken']) {
  1275. \Yii::$app->cache->set('cloud_admin_cache_token', $response['taken'], 29 * 3600);
  1276. return $response['taken'];
  1277. }
  1278. return '';
  1279. }
  1280. }
  1281. if (! function_exists('get_supplier_token')) {
  1282. /**
  1283. * 生成保存云仓token
  1284. * @return \Stbz\Api\SupplyClient
  1285. * @author Syan mzsongyan@gmail.com
  1286. * @date 2022-09-13
  1287. */
  1288. function get_supplier_token($token_supplier_id = 0, $supplier_id = 0)
  1289. {
  1290. if ($token_supplier_id && $token_supplier_id > 0) {
  1291. $supplier = Supplier::findOne(['cloud_supplier_id' => $token_supplier_id]);
  1292. $supplier_id = $supplier->id;
  1293. } else {
  1294. $supplier_id = $supplier_id ? $supplier_id : get_supplier_id();
  1295. }
  1296. $supplier = Supplier::findOne($supplier_id);
  1297. if (!$supplier) {
  1298. return '';
  1299. }
  1300. $response = get_cloud_token($supplier->name, $supplier->password, 1);
  1301. if ($response['code'] == 0 && $response['taken']) {
  1302. \Yii::$app->cache->set('cloud_supplier_cache_token_' . $supplier_id, $response['taken'], 29 * 3600);
  1303. return $response['taken'];
  1304. }
  1305. return '';
  1306. }
  1307. }
  1308. if (! function_exists('get_merchant_token')) {
  1309. /**
  1310. * 生成保存云仓token
  1311. * @return \Stbz\Api\SupplyClient
  1312. * @author Syan mzsongyan@gmail.com
  1313. * @date 2022-09-13
  1314. */
  1315. function get_merchant_token($token_stroe_cloud_id = 0, $store_id = 0, &$message = null)
  1316. {
  1317. if($token_stroe_cloud_id){
  1318. $storeCloud = StoreCloud::find()->where(['id' => $token_stroe_cloud_id, 'is_delete' => 0])->one();
  1319. if ($storeCloud) {
  1320. $access_token = \Yii::$app->cache->get('cloud_mch_cache_token_' . $storeCloud->id);
  1321. if ($access_token) {
  1322. return $access_token;
  1323. }
  1324. $response = get_cloud_token($storeCloud->name, $storeCloud->password, 1);
  1325. if ($response['code'] == 0 && $response['taken']) {
  1326. \Yii::$app->cache->set('cloud_mch_cache_token_' . $storeCloud->id, $response['taken'], 29 * 3600);
  1327. return $response['taken'];
  1328. }
  1329. }
  1330. $message = isset($response) ? $response : null;
  1331. return '';
  1332. }
  1333. $store_id = $store_id ?: get_store_id();
  1334. $storeCloud = StoreCloud::find()->where(['store_id' => $store_id,'is_delete'=>0])->one();
  1335. if (!$storeCloud) {
  1336. $message = [
  1337. 'code' => 1,
  1338. 'msg' => '未查到云仓用户'
  1339. ];
  1340. return '';
  1341. }
  1342. $response = get_cloud_token($storeCloud->name, $storeCloud->password, 1);
  1343. if ($response['code'] == 0 && $response['taken']) {
  1344. \Yii::$app->cache->set('cloud_mch_cache_token_' . $storeCloud->id, $response['taken'], 29 * 3600);
  1345. return $response['taken'];
  1346. }
  1347. $message = isset($response) ? $response : null;
  1348. return '';
  1349. }
  1350. }
  1351. if (!function_exists('checkReplay')) {
  1352. /**
  1353. * 检查并拦截重复请求
  1354. */
  1355. function checkReplay($params = [], $time = 10)
  1356. {
  1357. $trace = debug_backtrace();
  1358. $class = $trace[1]['class'] ?? '';
  1359. $function = $trace[1]['function'] ?? '';
  1360. $key = [$class, $function, $params];
  1361. $exists = cache()->exists($key);
  1362. // if(1 || $exists){
  1363. if($exists){
  1364. \Yii::$app->response->format = \Yii\web\Response::FORMAT_JSON;
  1365. \Yii::$app->response->data = [
  1366. 'code' => 1,
  1367. 'msg' => '重复请求,请稍后重试',
  1368. '$exists' => $exists,
  1369. ];
  1370. \Yii::$app->end();
  1371. return false;
  1372. }
  1373. cache()->set($key, 1, $time);
  1374. return true;
  1375. }
  1376. }
  1377. if (! function_exists('client_version')) {
  1378. /**
  1379. * 获取客户端版本
  1380. * @return mixed
  1381. * @author Syan mzsongyan@gmail.com
  1382. * @date 2022-12-16
  1383. */
  1384. function client_version()
  1385. {
  1386. return isset($_GET['version']) ? $_GET['version'] : cyy_version();
  1387. }
  1388. }
  1389. if (! function_exists('getAuthLink')) {
  1390. /**
  1391. * 获取客户端版本
  1392. * @return mixed
  1393. * @author Syan mzsongyan@gmail.com
  1394. * @date 2022-12-16
  1395. */
  1396. /**
  1397. * 微信浏览器h5授权链接
  1398. */
  1399. function getAuthLink($store_id, $redirect_url, int $scope_type) {
  1400. $store_wechat = WechatConfig::findOne(['store_id' => $store_id, 'type' => WechatConfig::TYPE_CONFIG_MP, 'is_delete' => 0]);
  1401. $redirect_url = urlencode($redirect_url);
  1402. $other = '';
  1403. if ($scope_type === 0) {
  1404. $type = "snsapi_base";
  1405. } else {
  1406. $type = "snsapi_userinfo";
  1407. $other = "&connect_redirect=1";
  1408. }
  1409. $app = WechatMini::getWechatConfig($store_id, 0, WechatMini::TYPE_OFFICIAL);
  1410. if (!$app) {
  1411. return '';
  1412. }
  1413. $app_id = $app->getConfig()['app_id'];
  1414. 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';
  1415. }
  1416. }
  1417. function buildTree(array $elements, $parentId = 0) {
  1418. $branch = array();
  1419. foreach ($elements as $element) {
  1420. if ($element['parent_id'] == $parentId) {
  1421. $children = buildTree($elements, $element['id']);
  1422. // 如果该元素有子元素,则将它们添加到children键中
  1423. if ($children) {
  1424. $element['children'] = $children;
  1425. }
  1426. $branch[] = $element;
  1427. }
  1428. }
  1429. return $branch;
  1430. }
  1431. function cpu_count(): int
  1432. {
  1433. // Windows does not support the number of processes setting.
  1434. if (\DIRECTORY_SEPARATOR === '\\') {
  1435. return 1;
  1436. }
  1437. $count = 4;
  1438. if (\is_callable('shell_exec')) {
  1439. if (\strtolower(PHP_OS) === 'darwin') {
  1440. $count = (int)\shell_exec('sysctl -n machdep.cpu.core_count');
  1441. } else {
  1442. $count = (int)\shell_exec('nproc');
  1443. }
  1444. }
  1445. return $count > 0 ? $count : 4;
  1446. }
  1447. function timeAgo($time)
  1448. {
  1449. $currentTime = time();
  1450. $diff = $currentTime - $time;
  1451. $intervals = array(
  1452. '年' => 31536000,
  1453. '月' => 2592000,
  1454. '周' => 604800,
  1455. '天' => 86400,
  1456. '小时' => 3600,
  1457. '分钟' => 60,
  1458. '秒' => 1
  1459. );
  1460. foreach ($intervals as $name => $seconds) {
  1461. $value = floor($diff / $seconds);
  1462. if ($value > 0) {
  1463. return $value . ' ' . $name . '前';
  1464. }
  1465. }
  1466. return '刚刚';
  1467. }
  1468. // 获取服务端微信支付对象
  1469. function getServiceClientWxConfig()
  1470. {
  1471. $keys = [
  1472. 'platform_mch_appid',
  1473. 'platform_mch_key',
  1474. 'platform_mch_id',
  1475. 'platform_apiclient_cert',
  1476. 'platform_apiclient_key',
  1477. 'platform_pay_key',
  1478. ];
  1479. $data = Option::get($keys, 0, 'saas');
  1480. if (empty($data)) {
  1481. $data = [
  1482. 'platform_mch_appid' => '',
  1483. 'platform_mch_key' => '',
  1484. 'platform_mch_id' => '',
  1485. 'platform_apiclient_cert' => '',
  1486. 'platform_apiclient_key' => '',
  1487. 'platform_pay_key' => '',
  1488. ];
  1489. } else {
  1490. $arr = [];
  1491. foreach ($data as $value) {
  1492. $index = array_search($value['name'], $keys);
  1493. unset($keys[$index]);
  1494. $arr[$value['name']] = $value['value'];
  1495. }
  1496. foreach ($keys as $key) {
  1497. $arr[$key] = '';
  1498. }
  1499. $data = $arr;
  1500. }
  1501. // 证书
  1502. if (!is_dir(\Yii::$app->runtimePath . '/pem')) {
  1503. mkdir(\Yii::$app->runtimePath . '/pem');
  1504. file_put_contents(\Yii::$app->runtimePath . '/pem/index.html', '');
  1505. }
  1506. $cert_pem_file = null;
  1507. if (isset($data['platform_apiclient_cert']) && $data['platform_apiclient_cert']) {
  1508. $cert_pem_file = \Yii::$app->runtimePath . '/pem/' . md5($data['platform_apiclient_cert']);
  1509. if (!file_exists($cert_pem_file)) {
  1510. file_put_contents($cert_pem_file, $data['platform_apiclient_cert']);
  1511. }
  1512. }
  1513. $key_pem_file = null;
  1514. if (isset($data['platform_apiclient_key']) && $data['platform_apiclient_key']) {
  1515. $key_pem_file = \Yii::$app->runtimePath . '/pem/' . md5($data['platform_apiclient_key']);
  1516. if (!file_exists($key_pem_file)) {
  1517. file_put_contents($key_pem_file, $data['platform_apiclient_key']);
  1518. }
  1519. }
  1520. return \EasyWeChat\Factory::payment([
  1521. 'app_id' => $data['platform_mch_appid'] ?? '',
  1522. 'secret' => $data['platform_mch_key'] ?? '',
  1523. 'key' => $data['platform_pay_key'] ?? '',
  1524. 'mch_id' => $data['platform_mch_id'] ?? '',
  1525. 'cert_path' => $cert_pem_file,
  1526. 'key_path' => $key_pem_file,
  1527. 'response_type' => 'array'
  1528. ]);
  1529. }
  1530. // 中文字符串处理
  1531. function func_substr_replace($str, $replacement = '*', $start = 1, $length = 3)
  1532. {
  1533. $len = mb_strlen($str,'utf-8');
  1534. if ($len > intval($start+$length)) {
  1535. $str1 = mb_substr($str,0,$start,'utf-8');
  1536. $str2 = mb_substr($str,intval($start+$length),NULL,'utf-8');
  1537. } else {
  1538. $str1 = mb_substr($str,0,1,'utf-8');
  1539. $str2 = mb_substr($str,$len-1,1,'utf-8');
  1540. }
  1541. $new_str = $str1;
  1542. $new_str .= $replacement;
  1543. $new_str .= $replacement;
  1544. $new_str .= $str2;
  1545. return $new_str;
  1546. }
  1547. //获取网络图片到临时目录
  1548. function saveTempFile($url, $type = 'pfx')
  1549. {
  1550. if (strpos($url, 'http') === false) {
  1551. $url = 'http:' . trim($url);
  1552. }
  1553. if (!is_dir(\Yii::$app->runtimePath . '/' . $type)) {
  1554. mkdir(\Yii::$app->runtimePath . '/' . $type);
  1555. }
  1556. $save_path = \Yii::$app->runtimePath . '/'.$type.'/' . md5($url) . '.' . $type;
  1557. \app\utils\CurlHelper::download($url, $save_path);
  1558. return $save_path;
  1559. }
  1560. if (!function_exists('has_table')) {
  1561. // 判断表是否存在
  1562. function has_table($table)
  1563. {
  1564. $has = \Yii::$app->db->createCommand('SHOW TABLES LIKE "' . $table . '" ')->queryOne();
  1565. return (bool)$has;
  1566. }
  1567. }
  1568. if (!function_exists('has_column')) {
  1569. // 判断字段是否存在
  1570. function has_column($table, $column)
  1571. {
  1572. $has = \Yii::$app->db->createCommand('SHOW COLUMNS FROM `' . $table . '` LIKE "' . $column . '" ')->queryOne();
  1573. return (bool)$has;
  1574. }
  1575. }