common.php 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102
  1. <?php
  2. use app\common\library\QRcode;
  3. use think\Db;
  4. /**
  5. * 秒转换为天
  6. */
  7. function get_stay_time($remain_time, $is_hour = 1, $is_minutes = 1)
  8. {
  9. $day = floor($remain_time / (3600*24));
  10. $day = $day > 0 ? $day.'天' : '';
  11. $hour = floor(($remain_time % (3600*24)) / 3600);
  12. $hour = $hour > 0 ? $hour.'小时' : '';
  13. if($is_hour && $is_minutes) {
  14. $minutes = floor((($remain_time % (3600*24)) % 3600) / 60);
  15. $minutes = $minutes > 0 ? $minutes.'分钟' : '';
  16. return $day.$hour.$minutes;
  17. }
  18. if($hour) {
  19. return $day.$hour;
  20. }
  21. return $day;
  22. }
  23. //获取全图片地址 $image_data
  24. function image_path($image_data){
  25. if(empty($image_data)){
  26. return $image_data;
  27. }
  28. if (strpos($image_data,'|')!==false){
  29. $image_res = explode('|',$image_data);
  30. }elseif(strpos($image_data,',')!==false){
  31. $image_res = explode(',',$image_data);
  32. }else{
  33. $image_res = array($image_data);
  34. }
  35. return $image_res;
  36. }
  37. // 获取模板区域
  38. function get_city_area()
  39. {
  40. $field=['id','pid','name'];
  41. $list=Db::table('store_area')->where('pid',0)->field($field)->select();
  42. foreach ($list as $k=>&$v){
  43. $v['children']= Db::table('store_area')->where('pid',$v['id'])->field($field)->select();
  44. }
  45. return $list;
  46. }
  47. function http_curl($url,$type='get',$res='json',$arr=''){
  48. $headers = array();
  49. //根据API的要求,定义相对应的Content-Type
  50. array_push($headers, "Content-Type".":"."application/x-www-form-urlencoded; charset=UTF-8;application/json");
  51. $curl = curl_init();
  52. curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $type);
  53. curl_setopt($curl, CURLOPT_URL, $url);
  54. curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
  55. curl_setopt($curl, CURLOPT_FAILONERROR, false);
  56. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  57. curl_setopt($curl, CURLOPT_HEADER, false);
  58. $output = curl_exec($curl);
  59. curl_close($curl);
  60. if($res=='json'){
  61. if($output === false){
  62. //请求失败,返回错误信息
  63. return curl_error($curl);
  64. }else{
  65. //请求成功,返回信息
  66. return json_decode($output,true);
  67. }
  68. }
  69. }
  70. // 获取物流信息
  71. function get_delivery($send_no = 'JD0053309649641',$express_code=''){
  72. error_reporting(E_ALL || ~E_NOTICE);
  73. $AppKey = 204008273;
  74. $AppSecret ='t9PavvfCeK5v2XidwyK5pWDp8b0hzMq4';
  75. $AppCode ='47f640e3529d43e28365311a530db2b7';//开通服务后 买家中心-查看AppCode
  76. $host = "https://wuliu.market.alicloudapi.com";//api访问链接
  77. $path = "/kdi";//API访问后缀
  78. $method = "GET";
  79. $body ='';
  80. $headers = array();
  81. array_push($headers, "Authorization:APPCODE " . $AppCode);
  82. $querys = "no={$send_no}&type={$express_code}"; //参数写在这里
  83. $url = $host . $path . "?" . $querys;
  84. $curl = curl_init();
  85. curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
  86. curl_setopt($curl, CURLOPT_URL, $url);
  87. curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
  88. curl_setopt($curl, CURLOPT_FAILONERROR, false);
  89. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  90. curl_setopt($curl, CURLOPT_HEADER, true);
  91. if (1 == strpos("$" . $host, "https://")) {
  92. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  93. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  94. }
  95. $out_put = curl_exec($curl);
  96. $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
  97. list($header, $body) = explode("\r\n\r\n", $out_put, 2);
  98. if ($httpCode == 200) {
  99. return json_decode($body,true)['result'];
  100. } else {
  101. return [];
  102. }
  103. }
  104. // 元石日志
  105. function crystal_log($user_id,$crystal,$desc,$type,$rel_id=0)
  106. {
  107. $log_data = [
  108. 'user_id' => $user_id,
  109. 'create_at' => date('Y-m-d H:i:s'),
  110. 'crystal' => $crystal,
  111. 'desc' => $desc,
  112. 'type' => $type,
  113. 'rel_id' => $rel_id,
  114. ];
  115. Db::table('crystal_info')->insert($log_data);
  116. }
  117. /**
  118. * 判断是否为合法的身份证号码
  119. * @param $mobile
  120. * @return int
  121. */
  122. function isCreditNo($vStr){
  123. $vCity = array(
  124. '11','12','13','14','15','21','22',
  125. '23','31','32','33','34','35','36',
  126. '37','41','42','43','44','45','46',
  127. '50','51','52','53','54','61','62',
  128. '63','64','65','71','81','82','91'
  129. );
  130. if (!preg_match('/^([\d]{17}[xX\d]|[\d]{15})$/', $vStr)) return false;
  131. if (!in_array(substr($vStr, 0, 2), $vCity)) return false;
  132. $vStr = preg_replace('/[xX]$/i', 'a', $vStr);
  133. $vLength = strlen($vStr);
  134. if ($vLength == 18) {
  135. $vBirthday = substr($vStr, 6, 4) . '-' . substr($vStr, 10, 2) . '-' . substr($vStr, 12, 2);
  136. } else {
  137. $vBirthday = '19' . substr($vStr, 6, 2) . '-' . substr($vStr, 8, 2) . '-' . substr($vStr, 10, 2);
  138. }
  139. if (date('Y-m-d', strtotime($vBirthday)) != $vBirthday) return false;
  140. if ($vLength == 18) {
  141. $vSum = 0;
  142. for ($i = 17 ; $i >= 0 ; $i--) {
  143. $vSubStr = substr($vStr, 17 - $i, 1);
  144. $vSum += (pow(2, $i) % 11) * (($vSubStr == 'a') ? 10 : intval($vSubStr , 11));
  145. }
  146. if($vSum % 11 != 1) return false;
  147. }
  148. return true;
  149. }
  150. //实名认证(云盾身份认证(身份证二要素核验)
  151. function identifyCertification($id_card,$name){
  152. $host = "https://safrvcert.market.alicloudapi.com";
  153. $path = "/safrv_2meta_id_name/";
  154. $method = "GET";
  155. $appcode = "95ec5cd8c54d4ced9220caebdaa5d750";
  156. $headers = array();
  157. array_push($headers, "Authorization:APPCODE " . $appcode);
  158. $querys = "__userId=__userId&identifyNum=$id_card&userName=$name&verifyKey=verifyKey";
  159. $bodys = "";
  160. $url = $host . $path . "?" . $querys;
  161. $curl = curl_init();
  162. curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
  163. curl_setopt($curl, CURLOPT_URL, $url);
  164. curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
  165. curl_setopt($curl, CURLOPT_FAILONERROR, false);
  166. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  167. curl_setopt($curl, CURLOPT_HEADER, false);
  168. if (1 == strpos("$".$host, "https://"))
  169. {
  170. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  171. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  172. }
  173. curl_setopt($curl, CURLOPT_POSTFIELDS, $bodys);
  174. $res = curl_exec($curl);
  175. if($res){
  176. $res = json_decode($res,true);
  177. if($res['code'] == 200 && $res['message']=='success'){
  178. return 1;
  179. }else{
  180. return 0;
  181. }
  182. }else{
  183. return 0;
  184. }
  185. }
  186. // 实名认证
  187. function user_certification($id_card,$name){
  188. $host = "http://checkone.market.alicloudapi.com";
  189. $path = "/chinadatapay/1882";
  190. $method = "POST";
  191. $appcode = "30be8bdcc65842919980a8276ffc4995";
  192. $headers = array();
  193. array_push($headers, "Authorization:APPCODE " . $appcode);
  194. //根据API的要求,定义相对应的Content-Type
  195. array_push($headers, "Content-Type".":"."application/x-www-form-urlencoded; charset=UTF-8");
  196. $bodys = "idcard=".$id_card."&name=".$name;
  197. $url = $host . $path;
  198. $curl = curl_init();
  199. curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
  200. curl_setopt($curl, CURLOPT_URL, $url);
  201. curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
  202. curl_setopt($curl, CURLOPT_FAILONERROR, false);
  203. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  204. curl_setopt($curl, CURLOPT_HEADER, false);
  205. if (1 == strpos("$".$host, "https://"))
  206. {
  207. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  208. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  209. }
  210. curl_setopt($curl, CURLOPT_POSTFIELDS, $bodys);
  211. $res = curl_exec($curl);
  212. dump($res);die;
  213. if($res){
  214. $res = json_decode($res,true);
  215. if($res['data']['result'] == 1){
  216. return 1;
  217. }else{
  218. return 0;
  219. }
  220. }else{
  221. return 0;
  222. }
  223. }
  224. function shoucang(){
  225. $host = "http://180.76.141.31:8888/cz/call";
  226. $method = "POST";
  227. $headers = array();
  228. array_push($headers, "Content-Type".":"."application/json; charset=UTF-8");
  229. $bodys = array(
  230. 'key' => 'test2',
  231. 'userkey' => '12344',
  232. 'product' => json_encode(array('name'=>'test','count'=>7))
  233. );
  234. $curl = curl_init();
  235. curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
  236. curl_setopt($curl, CURLOPT_URL, $host);
  237. curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
  238. curl_setopt($curl, CURLOPT_FAILONERROR, false);
  239. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  240. curl_setopt($curl, CURLOPT_HEADER, false);
  241. if (1 == strpos("$".$host, "https://"))
  242. {
  243. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  244. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  245. }
  246. curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($bodys));
  247. $res = curl_exec($curl);
  248. var_dump($res);exit();
  249. }
  250. /**
  251. * 大师提前分钟数存入redis
  252. */
  253. function setAdvanceMinutes(){
  254. $redis = new \think\cache\driver\Redis();
  255. $value = Db::name('system_config')->where('name','advance_minutes')->value('value');
  256. $redis->set('advance_minutes',$value);
  257. }
  258. /**
  259. * 获取大师提前分钟数
  260. */
  261. function getAdvanceMinutes(){
  262. $redis = new \think\cache\driver\Redis();
  263. $value = $redis->get('advance_minutes');
  264. if (empty($value)){
  265. $value = Db::name('system_config')->where('name','advance_minutes')->value('value');
  266. $redis->set('advance_minutes',$value);
  267. }
  268. return $value;
  269. }
  270. /**
  271. * 获取自动取消分钟数
  272. */
  273. function getCancelTime(){
  274. return Db::name('system_config')->where('name','cancel_time')->value('value');
  275. }
  276. /**
  277. * 用户信息存入redis hash
  278. */
  279. function setMemberInfoHash($member_id){
  280. $info = Db::name('store_member')->where('id',$member_id)->find();
  281. $redis = new \think\cache\driver\Redis();
  282. $array = ['member'.$member_id=>json_encode($info)];
  283. $redis->hMSet('memberInfo',$array);
  284. }
  285. /**
  286. * redis获取用户信息
  287. */
  288. function getMemberInfoHash($member_id){
  289. $redis = new \think\cache\driver\Redis();
  290. $info = $redis->hGet('memberInfo','member'.$member_id);
  291. if (!$info){
  292. $info = Db::name('store_member')->where('id',$member_id)->find();
  293. $array = ['member'.$member_id=>json_encode($info)];
  294. $redis->hMSet('memberInfo',$array);
  295. }else{
  296. $info = json_decode($info,true);
  297. }
  298. return $info;
  299. }
  300. /**
  301. * 用户抢购卡数量存入redis 字符串
  302. */
  303. function setMembercard($member_id){
  304. $snap_card = Db::name('store_member')->where('id',$member_id)->value('snap_card');
  305. $redis = new \think\cache\driver\Redis();
  306. $redis->set('membercard_'.$member_id,$snap_card);
  307. }
  308. /**
  309. * 获取用户抢购卡数量
  310. */
  311. function getMembercard($member_id){
  312. $redis = new \think\cache\driver\Redis();
  313. $count = $redis->get('membercard_'.$member_id);
  314. if (empty($count)){
  315. $count = Db::name('store_member')->where('id',$member_id)->value('snap_card');
  316. $redis->set('membercard_'.$member_id,$count);
  317. }
  318. return $count;
  319. }
  320. /**
  321. * 减掉用户抢购卡数量(减1)
  322. */
  323. function loseMembercard($id){
  324. $redis = new \think\cache\driver\Redis();
  325. $redis->Decr('membercard_'.$id);
  326. }
  327. /**
  328. * 增加用户抢购卡数量(加1)
  329. */
  330. function addMembercard($id){
  331. $redis = new \think\cache\driver\Redis();
  332. $redis->Incr('membercard_'.$id);
  333. }
  334. /**
  335. * 获取藏品库存
  336. */
  337. function getCollectionInventory($id){
  338. $redis = new \think\cache\driver\Redis();
  339. $count = $redis->get('collection_count_'.$id);
  340. if (empty($count)){
  341. $redis->set('collection_count_'.$id,0);
  342. // $count = Db::name('store_collection')->where('id',$id)->value('now_inventory');
  343. // $redis->set('collection_count_'.$id,$count);
  344. }
  345. return $count;
  346. }
  347. /**
  348. * 减掉藏品库存
  349. */
  350. function loseCollectionInventory($id,$num){
  351. $redis = new \think\cache\driver\Redis();
  352. $redis->Decrby('collection_count_'.$id,$num);
  353. }
  354. /**
  355. * 加藏品库存
  356. */
  357. function addCollectionInventory($id,$num){
  358. $redis = new \think\cache\driver\Redis();
  359. $redis->Incrby('collection_count_'.$id,$num);
  360. }
  361. /**
  362. * 用户购买藏品排名更新
  363. */
  364. function saveRanking($id){
  365. $redis = new \think\cache\driver\Redis();
  366. $redis->Incr('ranking'.$id);
  367. }
  368. /**
  369. * 获取用户购买藏品排名更新
  370. */
  371. function getRanking($id){
  372. $redis = new \think\cache\driver\Redis();
  373. $count = $redis->get('ranking'.$id);
  374. if (empty($count)){
  375. $redis->set('ranking'.$id,0);
  376. }
  377. $count = empty($count) ? 0 : $count;
  378. return $count;
  379. }
  380. /**
  381. * 藏品信息存入redis hash
  382. */
  383. function setCollectionInfoHash($id){
  384. $info = Db::name('store_collection')->where('id',$id)->find();
  385. $redis = new \think\cache\driver\Redis();
  386. $array = ['collection'.$id=>json_encode($info)];
  387. $redis->hMSet('collectionInfo',$array);
  388. }
  389. /**
  390. * 获取用户购买数量
  391. */
  392. function getByCount($mid,$id){
  393. $redis = new \think\cache\driver\Redis();
  394. $count = $redis->get('UserByCount_'.$mid.$id);
  395. if (empty($count) && $count!=0){
  396. // $count = Db::name('store_order')
  397. // ->where('mid',$mid)
  398. // ->where('c_id',$id)
  399. // ->whereIn('status','0,1')
  400. // ->sum('num');
  401. // $count = $count ? $count : 0;
  402. $count = 0;
  403. $redis->Incrby('UserByCount_'.$mid.$id,$count);
  404. }
  405. return $count;
  406. }
  407. /**
  408. * 用户购买数量增加
  409. */
  410. function IncrByCount($mid,$id,$number){
  411. $redis = new \think\cache\driver\Redis();
  412. $redis->Incrby('UserByCount_'.$mid.$id,$number);
  413. }
  414. /**
  415. * 用户购买数量减少
  416. */
  417. function DecrByCount($mid,$id,$number){
  418. $redis = new \think\cache\driver\Redis();
  419. $redis->Decrby('UserByCount_'.$mid.$id,$number);
  420. }
  421. /**
  422. * 获取藏品hash redis长度
  423. */
  424. function getLenCollection($id){
  425. $redis = new \think\cache\driver\Redis();
  426. $count = $redis->Llen('collectionHash_'.$id);
  427. if (!$count){
  428. $count = Db::name('hash')
  429. ->where('goods_id',$id)
  430. ->where('status',0)
  431. ->count();
  432. }
  433. return $count;
  434. }
  435. /**
  436. * 获取藏品信息
  437. */
  438. function getCollectionInfoHash($id)
  439. {
  440. $redis = new \think\cache\driver\Redis();
  441. $info = $redis->hGet('collectionInfo','collection'.$id);
  442. if (!$info){
  443. $info = Db::name('store_collection')->where('id',$id)->find();
  444. $array = ['collection'.$id=>json_encode($info)];
  445. $redis->hMSet('collectionInfo',$array);
  446. }else{
  447. $info = json_decode($info,true);
  448. }
  449. return $info;
  450. }
  451. /**
  452. * 收集下单用户id
  453. */
  454. function setCollectionBuyUser($mid){
  455. $redis = new \think\cache\driver\Redis();
  456. $array = [$mid=>1];
  457. $redis->hMSet('buyUserInfo',$array);
  458. }
  459. /*
  460. * 微信支付账户
  461. */
  462. function retrunWxConfig(){
  463. $config = [
  464. 'app_id' => 'wxa6980453638c9c78',
  465. 'mch_id' =>'1623128602',
  466. 'key' =>'qwertyuiopASDFGHJKLZXCVBNM123456',
  467. ];
  468. return $config;
  469. }
  470. /**
  471. * 获取离线账户
  472. */
  473. function getOfflineAccount(){
  474. $url = 'http://192.144.219.204:8083/ddc/createAccount';
  475. return file_get_contents($url);
  476. }
  477. /**
  478. * 获取链账户
  479. */
  480. function getWalletAddress($phone){
  481. $url = 'http://47.111.246.47:8088/register?accountName='.$phone;
  482. $res=curlRequest($url);
  483. if ($res){
  484. $res = json_decode($res,true);
  485. if ($res['code']=='200'){
  486. return $res['result']['userAddressStr'];
  487. }
  488. }
  489. return false;
  490. }
  491. /**
  492. * 获取公司生成的hash
  493. */
  494. function getCompanyHash($id){
  495. $redis = new \think\cache\driver\Redis();
  496. $hash = $redis->lPop('collectionHash_'.$id);
  497. if (!$hash){
  498. $hash = Db::name('hash')->where('goods_id',$id)
  499. ->where('status',0)
  500. ->order('id asc')
  501. ->field('hash,create_at')
  502. ->limit(1)
  503. ->find();
  504. }else{
  505. $hash = json_decode($hash,true);
  506. }
  507. return $hash;
  508. }
  509. /**
  510. * 获取排名
  511. */
  512. function getTag($id,$now,$num){
  513. $len = strlen($num);
  514. $re = 'JYBL'.$id.'#';
  515. switch ($len){
  516. case 1:
  517. $re = $re.'0'.$now;
  518. break;
  519. case 2:
  520. if ($now<10){
  521. $re = $re.'0'.$now;
  522. }else{
  523. $re = $re.$now;
  524. }
  525. break;
  526. case 3:
  527. if ($now<10){
  528. $re = $re.'00'.$now;
  529. }elseif ($now>=10 && $now<100){
  530. $re = $re.'0'.$now;
  531. }else{
  532. $re = $re.$now;
  533. }
  534. break;
  535. case 4:
  536. if ($now<10){
  537. $re = $re.'000'.$now;
  538. }elseif ($now>=10 && $now<100){
  539. $re = $re.'00'.$now;
  540. }elseif ($now>=100 && $now<1000){
  541. $re = $re.'0'.$now;
  542. }else{
  543. $re = $re.$now;
  544. }
  545. break;
  546. case 5:
  547. if ($now<10){
  548. $re = $re.'0000'.$now;
  549. }elseif ($now>=10 && $now<100){
  550. $re = $re.'000'.$now;
  551. }elseif ($now>=100 && $now<1000){
  552. $re = $re.'00'.$now;
  553. }elseif ($now>=1000 && $now<10000){
  554. $re = $re.'0'.$now;
  555. }else{
  556. $re = $re.$now;
  557. }
  558. break;
  559. case 6:
  560. if ($now<10){
  561. $re = $re.'00000'.$now;
  562. }elseif ($now>=10 && $now<100){
  563. $re = $re.'0000'.$now;
  564. }elseif ($now>=100 && $now<1000){
  565. $re = $re.'000'.$now;
  566. }elseif ($now>=1000 && $now<10000){
  567. $re = $re.'00'.$now;
  568. }elseif ($now>=10000 && $now<100000){
  569. $re = $re.'0'.$now;
  570. }else{
  571. $re = $re.$now;
  572. }
  573. break;
  574. }
  575. $re = $re.'/'.$num;
  576. return $re;
  577. }
  578. /**
  579. * 判断藏品是否设置过提醒
  580. */
  581. function getRemind($mid,$c_id){
  582. return Db::name('store_collection_remind')->where('mid',$mid)->where('c_id',$c_id)->count();
  583. }
  584. /**
  585. * 变更会员积分等
  586. * @param int $money 余额
  587. * @param int $user_id 会员ID
  588. * @param string $memo 备注
  589. */
  590. function memberMoneyChange($num, $type , $m_id, $title, $pm = 0,$link_id=0)
  591. {
  592. $member = Db::name('store_member')->where('id',$m_id)->find();
  593. switch ($type){
  594. case 1: //积分
  595. $before = $member['integral'];
  596. if ($pm==1)
  597. $after = $member['integral']+$num;
  598. else
  599. $after = $member['integral']-$num;
  600. $update_data = ['integral'=>$after];
  601. break;
  602. case 2: //抢购卡
  603. $before = $member['snap_card'];
  604. if ($pm==1)
  605. $after = $member['snap_card']+$num;
  606. else
  607. $after = $member['snap_card']-$num;
  608. $update_data = ['snap_card'=>$after];
  609. break;
  610. }
  611. Db::startTrans();
  612. try {
  613. Db::name('store_member')->where('id',$m_id)->update($update_data);
  614. $data = [
  615. 'm_id'=>$m_id,
  616. 'type'=>$type,
  617. 'pm'=>$pm,
  618. 'title'=>$title,
  619. 'change'=>$num,
  620. 'before'=>$before,
  621. 'after'=>$after,
  622. 'link_id'=>$link_id
  623. ];
  624. Db::name('store_member_log')->insert($data);
  625. Db::commit();
  626. return true;
  627. }catch (\Exception $e){
  628. Db::rollback();
  629. return false;
  630. }
  631. }
  632. /**
  633. * 获取单个系统配置信息
  634. */
  635. function getConfigValue($name){
  636. return Db::name('system_config')->where('name',$name)->value('value');
  637. }
  638. /**
  639. * 判断藏品状态
  640. */
  641. function checkCollectionState($id=''){
  642. if ($id){
  643. $info = Db::name('store_collection')->where('id',$id)->find();
  644. $now_inventory = getCollectionInventory($id);
  645. if ($info['state']==1){
  646. if ($now_inventory<=0){
  647. Db::name('store_collection')->where('id',$info['id'])->update(['state'=>3]);
  648. }
  649. }else if($info['state']==2){
  650. if (strtotime($info['sell_time'])<=time()){
  651. Db::name('store_collection')->where('id',$info['id'])->update(['state'=>1]);
  652. }
  653. }else if($info['state']==3){
  654. if ($now_inventory>0){
  655. Db::name('store_collection')->where('id',$info['id'])->update(['state'=>1]);
  656. }
  657. }
  658. }else{
  659. $list = Db::name('store_collection')
  660. ->whereIn('state','1,2')
  661. ->where('is_deleted',0)
  662. ->select();
  663. foreach ($list as &$v){
  664. $now_inventory = getCollectionInventory($v['id']);
  665. if ($v['state']==1){
  666. if ($now_inventory<=0){
  667. Db::name('store_collection')->where('id',$v['id'])->update(['state'=>3]);
  668. }
  669. }elseif ($v['state']==2){
  670. if (strtotime($v['sell_time'])<=time()){
  671. Db::name('store_collection')->where('id',$v['id'])->update(['state'=>1]);
  672. }
  673. }elseif ($v['state']==3){
  674. if ($now_inventory>0){
  675. Db::name('store_collection')->where('id',$v['id'])->update(['state'=>1]);
  676. }
  677. }
  678. }
  679. }
  680. }
  681. /**
  682. * redis加锁
  683. */
  684. function redisSetNx($id){
  685. $redis = new \think\cache\driver\Redis();
  686. $key = 'stock_'.$id;
  687. $exptime = 10;
  688. $is_lock = $redis->setnx($key,time()+$exptime);
  689. if ($is_lock){
  690. return true;
  691. }else{
  692. //加锁失败的情况下,判断锁是否已经存在,如果存在切已经过期,删除锁,重新加锁
  693. $val = $redis->get($key);
  694. if ($val && $val<time()){
  695. $redis->del($key);
  696. }
  697. return $redis->setnx($key,time()+$exptime);
  698. }
  699. }
  700. /**
  701. * 删除锁
  702. */
  703. function DelRedisSetNx($id){
  704. $redis = new \think\cache\driver\Redis();
  705. $key = 'stock_'.$id;
  706. $redis->del($key);
  707. }
  708. /**
  709. * 盲盒信息存入redis hash
  710. */
  711. function setBoxInfoHash($id){
  712. $info = Db::name('store_collection')->where('id',$id)->find();
  713. $redis = new \think\cache\driver\Redis();
  714. $array = ['collection'.$id=>json_encode($info)];
  715. $redis->hMSet('collectionInfo',$array);
  716. }
  717. /***********************************************人脸识别*****************************************************************/
  718. /**
  719. * 获取accesstoken
  720. * @return mixed
  721. */
  722. function getAccessToken(){
  723. $appid_id =getFaceInfo()['appid'];
  724. $secret = getFaceInfo()['secret'];
  725. $grant_type = 'client_credential';
  726. $version = '1.0.0';
  727. $url = "https://miniprogram-kyc.tencentcloudapi.com/api/oauth2/access_token?app_id=".$appid_id."&secret=".$secret."&grant_type=".$grant_type."&version=".$version;
  728. $result = curlRequest($url);
  729. $result = json_decode($result,true);
  730. if ($result['code']==0){
  731. return $result;
  732. }
  733. echo $result['msg'];die;
  734. }
  735. /**
  736. * 获取api_ticket
  737. */
  738. function getApiTicket($type,$userId=''){
  739. $appid_id =getFaceInfo()['appid'];
  740. $access_token = getAccessToken()['access_token'];
  741. $version = '1.0.0';
  742. $url = "https://miniprogram-kyc.tencentcloudapi.com/api/oauth2/api_ticket?app_id=".$appid_id."&access_token=".$access_token."&type=".$type."&version=".$version;
  743. if ($type=='NONCE'){
  744. $url .= "&user_id=".$userId;
  745. }
  746. $result = curlRequest($url);
  747. $result = json_decode($result,true);
  748. if ($result['code']==0){
  749. return $result;
  750. }
  751. echo $result['msg'];die;
  752. }
  753. /**
  754. * 生成签名
  755. */
  756. function generateSign($userId){
  757. $wbappid = getFaceInfo()['appid'];
  758. $version = '1.0.0';
  759. $ticket = getApiTicket('SIGN')['tickets'][0]['value'];
  760. $nonce = get32Str();
  761. // $array = ['wbappid'=>$wbappid, 'userId'=>"$userId",'nonce'=>$nonce, 'version'=>$version, 'ticket'=>$ticket];
  762. $array = [$wbappid, "$userId",$nonce, $version, $ticket];
  763. file_put_contents("signLog.txt", json_encode($array) . "\n" . "\n", FILE_APPEND);
  764. sort($array);
  765. $str='';
  766. foreach ($array as $kk=>$vv){
  767. $str .=$vv;
  768. }
  769. // echo $str;die;
  770. $result = strtoupper(sha1($str));
  771. $res['sign'] = $result;
  772. $res['nonce'] = $nonce;
  773. $res['appid'] = $wbappid;
  774. return $res;
  775. }
  776. /**
  777. * 合作方后台上送身份信息
  778. */
  779. function getfaceid($name,$idNo,$userId){
  780. $webankAppId = getFaceInfo()['appid'];
  781. $orderNo = get32Str();
  782. $sign = generateSign($userId);
  783. $data = [
  784. 'webankAppId'=>$webankAppId,
  785. 'orderNo'=>$orderNo,
  786. 'name'=>$name,
  787. 'idNo'=>$idNo,
  788. 'userId'=>"$userId",
  789. 'version'=>'1.0.0',
  790. 'sign'=>$sign['sign'],
  791. 'nonce'=>$sign['nonce']
  792. ];
  793. $headers = [];
  794. array_push($headers, "Content-Type".":"."application/json; charset=UTF-8");
  795. $url = 'https://miniprogram-kyc.tencentcloudapi.com/api/server/getfaceid?orderNo='.$orderNo;
  796. $result = curlRequest($url,$headers,json_encode($data),'POST');
  797. $result = json_decode($result,true);
  798. if ($result['code']==0){
  799. $data = [
  800. 'appid'=>$sign['appid'],
  801. 'userID'=>"$userId",
  802. 'sign'=>$sign['sign'],
  803. 'nonce'=>$sign['nonce'],
  804. 'sdksign'=>SdkSign($userId,$sign['nonce']),
  805. 'result'=>$result['result'],
  806. ];
  807. return $data;
  808. }
  809. echo $result['msg'];die;
  810. }
  811. /**
  812. * 生成 SDK 接口调用步骤使用签名
  813. */
  814. function SdkSign($userId,$nonce){
  815. $wbappid = getFaceInfo()['appid'];
  816. $version = '1.0.0';
  817. $ticket = getApiTicket('NONCE',$userId)['tickets'][0]['value'];
  818. //$nonce = get32Str();
  819. $array = [$wbappid, "$userId",$nonce, $version, $ticket];
  820. file_put_contents("sdksignLog.txt", json_encode($array) . "\n" . "\n", FILE_APPEND);
  821. sort($array);
  822. $str='';
  823. foreach ($array as $kk=>$vv){
  824. $str .=$vv;
  825. }
  826. $result = strtoupper(sha1($str));
  827. return $result;
  828. }
  829. function getFaceInfo(){
  830. //正式
  831. $date = [
  832. 'appid'=>'IDAKcvCn',
  833. 'secret'=>'L1id3z5zROK8SVXg72ZyZXc8ziniYLKpOnIzN1gecrBaEMaKIlKrreyRGCLU2z4l'
  834. ];
  835. //测试
  836. // $date = [
  837. // 'appid'=>'TIDA3uGO',
  838. // 'secret'=>'z41IysbEhRy5oR4Qs2kFfVkrufXIJMtsJOQ3eulkWwXwdKG772LSSiZsKKwfsv9n'
  839. // ];
  840. return $date;
  841. }
  842. //将数组转成uri字符串
  843. function formatBizQueryParaMap($paraMap, $urlencode)
  844. {
  845. $buff = "";
  846. ksort($paraMap);
  847. foreach ($paraMap as $k => $v)
  848. {
  849. if($urlencode)
  850. {
  851. $v = urlencode($v);
  852. }
  853. // $buff .= strtolower($k) . "=" . $v . "&";
  854. $buff .= $v;
  855. }
  856. if (strlen($buff) > 0)
  857. {
  858. $reqPar = substr($buff, 0, strlen($buff)-1);
  859. }
  860. return $reqPar;
  861. }
  862. /**
  863. * 生成32位随机数
  864. */
  865. function get32Str($length='32'){
  866. $str = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
  867. $len = strlen($str)-1;
  868. $randstr = '';
  869. for ($i=0;$i<$length;$i++) {
  870. $num=mt_rand(0,$len);
  871. $randstr .= $str[$num];
  872. }
  873. return $randstr;
  874. }
  875. function get_order_sn(){
  876. $order_id_main = date('YmdHis') . rand(10000000,99999999);
  877. $order_id_len = strlen($order_id_main);
  878. $order_id_sum = 0;
  879. for($i=0; $i<$order_id_len; $i++){
  880. $order_id_sum += (int)(substr($order_id_main,$i,1));
  881. }
  882. $osn = $order_id_main . str_pad((100 - $order_id_sum % 100) % 100,2,'0',STR_PAD_LEFT);
  883. return $osn;
  884. }
  885. /**
  886. * 生成二维码
  887. */
  888. function setqrcode($value,$name){
  889. $dir = dirname(realpath(dirname($_SERVER['SCRIPT_FILENAME']))) . '/public/user';
  890. if(!file_exists($dir)){
  891. //检查是否有该文件夹,如果没有就创建,并给予最高权限
  892. mkdir($dir, 0700,true);
  893. }
  894. $filename = $dir.'/'.$name.'.png';
  895. QRcode::png($value,$filename,QR_ECLEVEL_L,7);
  896. return 'http://'.$_SERVER['SERVER_NAME']."/user/".$name.'.png';
  897. }
  898. //生成邀请二维码
  899. function setintivecode($user_id){
  900. $name = $user_id."_".time();
  901. $address = 'http://'.$_SERVER['SERVER_NAME'].'/web/h5/pages/mine/zhuce?invite_code='.$user_id;
  902. $url = setqrcode($address,$name);
  903. return $url;
  904. }
  905. //邀请地址
  906. function getintiveaddress($user_id){
  907. $address = 'http://'.$_SERVER['SERVER_NAME'].'/web/h5/pages/mine/zhuce?invite_code='.$user_id;
  908. return $address;
  909. }
  910. /**
  911. * @param $url
  912. * @param array $headers
  913. * @param array $body
  914. * @param string $method
  915. * @return bool|string
  916. */
  917. function curlRequest($url, $headers = [], $body = [], $method = "GET")
  918. {
  919. $ch = curl_init();
  920. curl_setopt($ch, CURLOPT_URL, $url);
  921. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);//设置请求头
  922. curl_setopt($ch, CURLOPT_POSTFIELDS, $body);//设置请求体
  923. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); //定义请求类型
  924. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  925. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  926. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  927. curl_setopt($ch, CURLOPT_HTTP_VERSION, 'CURL_HTTP_VERSION_1_1');
  928. $output = curl_exec($ch);
  929. curl_close($ch);
  930. return $output;
  931. }
  932. /**
  933. * 盲盒藏品库存和藏品状态判断
  934. * @param $proArr
  935. * @return int|string
  936. */
  937. function checkBox(){
  938. $list = Db::name('store_blind_box')
  939. ->where('status',1)
  940. ->where('is_del',1)
  941. ->field('id,is_prize,prize')
  942. ->select();
  943. foreach ($list as &$v){
  944. if ($v['is_prize']==1){
  945. $info = Db::name('store_collection')->where('id',$v['prize'])->find();
  946. if ($info['now_inventory']<=0 || $info['status']==0 || $info['is_deleted']==1){
  947. Db::name('store_blind_box')->where('id',$v['id'])->update(['status'=>0]);
  948. }
  949. }
  950. }
  951. }
  952. //计算中奖概率
  953. function get_rand($proArr) {
  954. $result = '';
  955. //概率数组的总概率精度
  956. $proSum = array_sum($proArr);
  957. // var_dump($proSum);
  958. //概率数组循环
  959. foreach ($proArr as $key => $proCur) {
  960. $randNum = mt_rand(1, $proSum); //返回随机整数
  961. if ($randNum <= $proCur) {
  962. $result = $key;
  963. break;
  964. } else {
  965. $proSum -= $proCur;
  966. }
  967. }
  968. unset ($proArr);
  969. return $result;
  970. }