common.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988
  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 user_certification($id_card,$name){
  152. $host = "http://checkone.market.alicloudapi.com";
  153. $path = "/chinadatapay/1882";
  154. $method = "POST";
  155. $appcode = "30be8bdcc65842919980a8276ffc4995";
  156. $headers = array();
  157. array_push($headers, "Authorization:APPCODE " . $appcode);
  158. //根据API的要求,定义相对应的Content-Type
  159. array_push($headers, "Content-Type".":"."application/x-www-form-urlencoded; charset=UTF-8");
  160. $bodys = "idcard=".$id_card."&name=".$name;
  161. $url = $host . $path;
  162. $curl = curl_init();
  163. curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
  164. curl_setopt($curl, CURLOPT_URL, $url);
  165. curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
  166. curl_setopt($curl, CURLOPT_FAILONERROR, false);
  167. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  168. curl_setopt($curl, CURLOPT_HEADER, false);
  169. if (1 == strpos("$".$host, "https://"))
  170. {
  171. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  172. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  173. }
  174. curl_setopt($curl, CURLOPT_POSTFIELDS, $bodys);
  175. $res = curl_exec($curl);
  176. dump($res);die;
  177. if($res){
  178. $res = json_decode($res,true);
  179. if($res['data']['result'] == 1){
  180. return 1;
  181. }else{
  182. return 0;
  183. }
  184. }else{
  185. return 0;
  186. }
  187. }
  188. function shoucang(){
  189. $host = "http://180.76.141.31:8888/cz/call";
  190. $method = "POST";
  191. $headers = array();
  192. array_push($headers, "Content-Type".":"."application/json; charset=UTF-8");
  193. $bodys = array(
  194. 'key' => 'test2',
  195. 'userkey' => '12344',
  196. 'product' => json_encode(array('name'=>'test','count'=>7))
  197. );
  198. $curl = curl_init();
  199. curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
  200. curl_setopt($curl, CURLOPT_URL, $host);
  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, json_encode($bodys));
  211. $res = curl_exec($curl);
  212. var_dump($res);exit();
  213. }
  214. /**
  215. * 大师提前分钟数存入redis
  216. */
  217. function setAdvanceMinutes(){
  218. $redis = new \think\cache\driver\Redis();
  219. $value = Db::name('system_config')->where('name','advance_minutes')->value('value');
  220. $redis->set('advance_minutes',$value);
  221. }
  222. /**
  223. * 获取大师提前分钟数
  224. */
  225. function getAdvanceMinutes(){
  226. $redis = new \think\cache\driver\Redis();
  227. $value = $redis->get('advance_minutes');
  228. if (empty($value)){
  229. $value = Db::name('system_config')->where('name','advance_minutes')->value('value');
  230. $redis->set('advance_minutes',$value);
  231. }
  232. return $value;
  233. }
  234. /**
  235. * 获取自动取消分钟数
  236. */
  237. function getCancelTime(){
  238. return Db::name('system_config')->where('name','cancel_time')->value('value');
  239. }
  240. /**
  241. * 用户信息存入redis hash
  242. */
  243. function setMemberInfoHash($member_id){
  244. $info = Db::name('store_member')->where('id',$member_id)->find();
  245. $redis = new \think\cache\driver\Redis();
  246. $array = ['member'.$member_id=>json_encode($info)];
  247. $redis->hMSet('memberInfo',$array);
  248. }
  249. /**
  250. * redis获取用户信息
  251. */
  252. function getMemberInfoHash($member_id){
  253. $redis = new \think\cache\driver\Redis();
  254. $info = $redis->hGet('memberInfo','member'.$member_id);
  255. if (!$info){
  256. $info = Db::name('store_member')->where('id',$member_id)->find();
  257. $array = ['member'.$member_id=>json_encode($info)];
  258. $redis->hMSet('memberInfo',$array);
  259. }else{
  260. $info = json_decode($info,true);
  261. }
  262. return $info;
  263. }
  264. /**
  265. * 用户抢购卡数量存入redis 字符串
  266. */
  267. function setMembercard($member_id){
  268. $snap_card = Db::name('store_member')->where('id',$member_id)->value('snap_card');
  269. $redis = new \think\cache\driver\Redis();
  270. $redis->set('membercard_'.$member_id,$snap_card);
  271. }
  272. /**
  273. * 获取用户抢购卡数量
  274. */
  275. function getMembercard($member_id){
  276. $redis = new \think\cache\driver\Redis();
  277. $count = $redis->get('membercard_'.$member_id);
  278. if (empty($count)){
  279. $count = Db::name('store_member')->where('id',$member_id)->value('snap_card');
  280. $redis->set('membercard_'.$member_id,$count);
  281. }
  282. return $count;
  283. }
  284. /**
  285. * 减掉用户抢购卡数量(减1)
  286. */
  287. function loseMembercard($id){
  288. $redis = new \think\cache\driver\Redis();
  289. $redis->Decr('membercard_'.$id);
  290. }
  291. /**
  292. * 增加用户抢购卡数量(加1)
  293. */
  294. function addMembercard($id){
  295. $redis = new \think\cache\driver\Redis();
  296. $redis->Incr('membercard_'.$id);
  297. }
  298. /**
  299. * 获取藏品库存
  300. */
  301. function getCollectionInventory($id){
  302. $redis = new \think\cache\driver\Redis();
  303. $count = $redis->get('collection_count_'.$id);
  304. if (empty($count)){
  305. $count = Db::name('store_collection')->where('id',$id)->value('now_inventory');
  306. $redis->set('collection_count_'.$id,$count);
  307. }
  308. return $count;
  309. }
  310. /**
  311. * 减掉藏品库存
  312. */
  313. function loseCollectionInventory($id,$num){
  314. $redis = new \think\cache\driver\Redis();
  315. $redis->Decrby('collection_count_'.$id,$num);
  316. }
  317. /**
  318. * 加藏品库存
  319. */
  320. function addCollectionInventory($id,$num){
  321. $redis = new \think\cache\driver\Redis();
  322. $redis->Incrby('collection_count_'.$id,$num);
  323. }
  324. /**
  325. * 用户购买藏品排名更新
  326. */
  327. function saveRanking($id){
  328. $redis = new \think\cache\driver\Redis();
  329. $redis->Incr('ranking'.$id);
  330. }
  331. /**
  332. * 获取用户购买藏品排名更新
  333. */
  334. function getRanking($id){
  335. $redis = new \think\cache\driver\Redis();
  336. $count = $redis->get('ranking'.$id);
  337. if (empty($count)){
  338. $redis->set('ranking'.$id,0);
  339. }
  340. $count = empty($count) ? 0 : $count;
  341. return $count;
  342. }
  343. /**
  344. * 藏品信息存入redis hash
  345. */
  346. function setCollectionInfoHash($id){
  347. $info = Db::name('store_collection')->where('id',$id)->find();
  348. $redis = new \think\cache\driver\Redis();
  349. $array = ['collection'.$id=>json_encode($info)];
  350. $redis->hMSet('collectionInfo',$array);
  351. }
  352. /**
  353. * 获取用户购买数量
  354. */
  355. function getByCount($mid,$id){
  356. $redis = new \think\cache\driver\Redis();
  357. $count = $redis->get('UserByCount_'.$mid.$id);
  358. if (empty($count) && $count!=0){
  359. // $count = Db::name('store_order')
  360. // ->where('mid',$mid)
  361. // ->where('c_id',$id)
  362. // ->whereIn('status','0,1')
  363. // ->sum('num');
  364. // $count = $count ? $count : 0;
  365. $count = 0;
  366. $redis->Incrby('UserByCount_'.$mid.$id,$count);
  367. }
  368. return $count;
  369. }
  370. /**
  371. * 用户购买数量增加
  372. */
  373. function IncrByCount($mid,$id,$number){
  374. $redis = new \think\cache\driver\Redis();
  375. $redis->Incrby('UserByCount_'.$mid.$id,$number);
  376. }
  377. /**
  378. * 用户购买数量减少
  379. */
  380. function DecrByCount($mid,$id,$number){
  381. $redis = new \think\cache\driver\Redis();
  382. $redis->Decrby('UserByCount_'.$mid.$id,$number);
  383. }
  384. /**
  385. * 获取藏品hash redis长度
  386. */
  387. function getLenCollection($id){
  388. $redis = new \think\cache\driver\Redis();
  389. $count = $redis->Llen('collectionHash_'.$id);
  390. if (!$count){
  391. $count = Db::name('hash')
  392. ->where('goods_id',$id)
  393. ->where('success',1)
  394. ->where('status',0)
  395. ->count();
  396. }
  397. return $count;
  398. }
  399. /**
  400. * 获取藏品信息
  401. */
  402. function getCollectionInfoHash($id)
  403. {
  404. $redis = new \think\cache\driver\Redis();
  405. $info = $redis->hGet('collectionInfo','collection'.$id);
  406. if (!$info){
  407. $info = Db::name('store_collection')->where('id',$id)->find();
  408. $array = ['collection'.$id=>json_encode($info)];
  409. $redis->hMSet('collectionInfo',$array);
  410. }else{
  411. $info = json_decode($info,true);
  412. }
  413. return $info;
  414. }
  415. /*
  416. * 微信支付账户
  417. */
  418. function retrunWxConfig(){
  419. $config = [
  420. 'app_id' => 'wxa79190389d57c04b',
  421. 'mch_id' =>'1623066512',
  422. 'key' =>'qwertyuiopASDFGHJKLZXCVBNM123456',
  423. ];
  424. return $config;
  425. }
  426. /**
  427. * 获取离线账户
  428. */
  429. function getOfflineAccount(){
  430. $url = 'http://192.144.219.204:8083/ddc/createAccount';
  431. return file_get_contents($url);
  432. }
  433. /**
  434. * 获取链账户
  435. */
  436. function getWalletAddress($phone,$address){
  437. $phone .= rand(10000,99999);
  438. $url = "http://192.144.219.204:8083/ddc/createAddress?name=".$phone."&account=".$address;
  439. $res=curlRequest($url);
  440. return json_decode($res,true);
  441. }
  442. /**
  443. * 获取公司生成的hash
  444. */
  445. function getCompanyHash($id){
  446. $redis = new \think\cache\driver\Redis();
  447. $hash = $redis->lPop('collectionHash_'.$id);
  448. if (!$hash){
  449. $hash = Db::name('hash')->where('goods_id',$id)
  450. ->where('status',0)
  451. ->order('id asc')
  452. ->field('hash,create_at')
  453. ->limit(1)
  454. ->find();
  455. }else{
  456. $hash = json_decode($hash,true);
  457. }
  458. return $hash;
  459. }
  460. /**
  461. * 获取排名
  462. */
  463. function getTag($id,$now,$num){
  464. $len = strlen($num);
  465. $re = 'XX'.$id.'#';
  466. switch ($len){
  467. case 1:
  468. $re = $re.'0'.$now;
  469. break;
  470. case 2:
  471. if ($now<10){
  472. $re = $re.'0'.$now;
  473. }else{
  474. $re = $re.$now;
  475. }
  476. break;
  477. case 3:
  478. if ($now<10){
  479. $re = $re.'00'.$now;
  480. }elseif ($now>=10 && $now<100){
  481. $re = $re.'0'.$now;
  482. }else{
  483. $re = $re.$now;
  484. }
  485. break;
  486. case 4:
  487. if ($now<10){
  488. $re = $re.'000'.$now;
  489. }elseif ($now>=10 && $now<100){
  490. $re = $re.'00'.$now;
  491. }elseif ($now>=100 && $now<1000){
  492. $re = $re.'0'.$now;
  493. }else{
  494. $re = $re.$now;
  495. }
  496. break;
  497. case 5:
  498. if ($now<10){
  499. $re = $re.'0000'.$now;
  500. }elseif ($now>=10 && $now<100){
  501. $re = $re.'000'.$now;
  502. }elseif ($now>=100 && $now<1000){
  503. $re = $re.'00'.$now;
  504. }elseif ($now>=1000 && $now<10000){
  505. $re = $re.'0'.$now;
  506. }else{
  507. $re = $re.$now;
  508. }
  509. break;
  510. case 6:
  511. if ($now<10){
  512. $re = $re.'00000'.$now;
  513. }elseif ($now>=10 && $now<100){
  514. $re = $re.'0000'.$now;
  515. }elseif ($now>=100 && $now<1000){
  516. $re = $re.'000'.$now;
  517. }elseif ($now>=1000 && $now<10000){
  518. $re = $re.'00'.$now;
  519. }elseif ($now>=10000 && $now<100000){
  520. $re = $re.'0'.$now;
  521. }else{
  522. $re = $re.$now;
  523. }
  524. break;
  525. }
  526. $re = $re.'/'.$num;
  527. return $re;
  528. }
  529. /**
  530. * 判断藏品是否设置过提醒
  531. */
  532. function getRemind($mid,$c_id){
  533. return Db::name('store_collection_remind')->where('mid',$mid)->where('c_id',$c_id)->count();
  534. }
  535. /**
  536. * 变更会员积分等
  537. * @param int $money 余额
  538. * @param int $user_id 会员ID
  539. * @param string $memo 备注
  540. */
  541. function memberMoneyChange($num, $type , $m_id, $title, $pm = 0,$link_id=0)
  542. {
  543. $member = Db::name('store_member')->where('id',$m_id)->find();
  544. switch ($type){
  545. case 1: //积分
  546. $before = $member['integral'];
  547. if ($pm==1)
  548. $after = $member['integral']+$num;
  549. else
  550. $after = $member['integral']-$num;
  551. $update_data = ['integral'=>$after];
  552. break;
  553. case 2: //抢购卡
  554. $before = $member['snap_card'];
  555. if ($pm==1)
  556. $after = $member['snap_card']+$num;
  557. else
  558. $after = $member['snap_card']-$num;
  559. $update_data = ['snap_card'=>$after];
  560. break;
  561. }
  562. Db::startTrans();
  563. try {
  564. Db::name('store_member')->where('id',$m_id)->update($update_data);
  565. $data = [
  566. 'm_id'=>$m_id,
  567. 'type'=>$type,
  568. 'pm'=>$pm,
  569. 'title'=>$title,
  570. 'change'=>$num,
  571. 'before'=>$before,
  572. 'after'=>$after,
  573. 'link_id'=>$link_id
  574. ];
  575. Db::name('store_member_log')->insert($data);
  576. Db::commit();
  577. return true;
  578. }catch (\Exception $e){
  579. Db::rollback();
  580. return false;
  581. }
  582. }
  583. /**
  584. * 获取单个系统配置信息
  585. */
  586. function getConfigValue($name){
  587. return Db::name('system_config')->where('name',$name)->value('value');
  588. }
  589. /**
  590. * 判断藏品状态
  591. */
  592. function checkCollectionState($id=''){
  593. if ($id){
  594. $info = Db::name('store_collection')->where('id',$id)->find();
  595. if ($info['state']==1){
  596. if ($info['now_inventory']==0){
  597. Db::name('store_collection')->where('id',$info['id'])->update(['state'=>3]);
  598. }
  599. }else if($info['state']==2){
  600. if (strtotime($info['sell_time'])<=time()){
  601. Db::name('store_collection')->where('id',$info['id'])->update(['state'=>1]);
  602. }
  603. }else if($info['state']==3){
  604. if ($info['now_inventory']>0){
  605. Db::name('store_collection')->where('id',$info['id'])->update(['state'=>1]);
  606. }
  607. }
  608. }else{
  609. $list = Db::name('store_collection')
  610. ->whereIn('state','1,2')
  611. ->where('is_deleted',0)
  612. ->select();
  613. foreach ($list as &$v){
  614. if ($v['state']==1){
  615. if ($v['now_inventory']==0){
  616. Db::name('store_collection')->where('id',$v['id'])->update(['state'=>3]);
  617. }
  618. }else{
  619. if (strtotime($v['sell_time'])<=time()){
  620. Db::name('store_collection')->where('id',$v['id'])->update(['state'=>1]);
  621. }
  622. }
  623. }
  624. }
  625. }
  626. /**
  627. * redis加锁
  628. */
  629. function redisSetNx($id){
  630. $redis = new \think\cache\driver\Redis();
  631. $key = 'stock_'.$id;
  632. $is_lock = $redis->setnx($key,time()+5);
  633. if ($is_lock){
  634. return true;
  635. }else{
  636. //加锁失败的情况下,判断锁是否已经存在,如果存在切已经过期,删除锁,重新加锁
  637. $val = $redis->get($key);
  638. if ($val && $val<time()){
  639. $redis->del($key);
  640. }
  641. return $redis->setnx($key,time()+5);
  642. }
  643. }
  644. /**
  645. * 删除锁
  646. */
  647. function DelRedisSetNx($id){
  648. $redis = new \think\cache\driver\Redis();
  649. $key = 'stock_'.$id;
  650. $redis->del($key);
  651. }
  652. /***********************************************人脸识别*****************************************************************/
  653. /**
  654. * 获取accesstoken
  655. * @return mixed
  656. */
  657. function getAccessToken(){
  658. $appid_id =getFaceInfo()['appid'];
  659. $secret = getFaceInfo()['secret'];
  660. $grant_type = 'client_credential';
  661. $version = '1.0.0';
  662. $url = "https://miniprogram-kyc.tencentcloudapi.com/api/oauth2/access_token?app_id=".$appid_id."&secret=".$secret."&grant_type=".$grant_type."&version=".$version;
  663. $result = curlRequest($url);
  664. $result = json_decode($result,true);
  665. if ($result['code']==0){
  666. return $result;
  667. }
  668. echo $result['msg'];die;
  669. }
  670. /**
  671. * 获取api_ticket
  672. */
  673. function getApiTicket($type,$userId=''){
  674. $appid_id =getFaceInfo()['appid'];
  675. $access_token = getAccessToken()['access_token'];
  676. $version = '1.0.0';
  677. $url = "https://miniprogram-kyc.tencentcloudapi.com/api/oauth2/api_ticket?app_id=".$appid_id."&access_token=".$access_token."&type=".$type."&version=".$version;
  678. if ($type=='NONCE'){
  679. $url .= "&user_id=".$userId;
  680. }
  681. $result = curlRequest($url);
  682. $result = json_decode($result,true);
  683. if ($result['code']==0){
  684. return $result;
  685. }
  686. echo $result['msg'];die;
  687. }
  688. /**
  689. * 生成签名
  690. */
  691. function generateSign($userId){
  692. $wbappid = getFaceInfo()['appid'];
  693. $version = '1.0.0';
  694. $ticket = getApiTicket('SIGN')['tickets'][0]['value'];
  695. $nonce = get32Str();
  696. // $array = ['wbappid'=>$wbappid, 'userId'=>"$userId",'nonce'=>$nonce, 'version'=>$version, 'ticket'=>$ticket];
  697. $array = [$wbappid, "$userId",$nonce, $version, $ticket];
  698. file_put_contents("signLog.txt", json_encode($array) . "\n" . "\n", FILE_APPEND);
  699. sort($array);
  700. $str='';
  701. foreach ($array as $kk=>$vv){
  702. $str .=$vv;
  703. }
  704. // echo $str;die;
  705. $result = strtoupper(sha1($str));
  706. $res['sign'] = $result;
  707. $res['nonce'] = $nonce;
  708. $res['appid'] = $wbappid;
  709. return $res;
  710. }
  711. /**
  712. * 合作方后台上送身份信息
  713. */
  714. function getfaceid($name,$idNo,$userId){
  715. $webankAppId = getFaceInfo()['appid'];
  716. $orderNo = get32Str();
  717. $sign = generateSign($userId);
  718. $data = [
  719. 'webankAppId'=>$webankAppId,
  720. 'orderNo'=>$orderNo,
  721. 'name'=>$name,
  722. 'idNo'=>$idNo,
  723. 'userId'=>"$userId",
  724. 'version'=>'1.0.0',
  725. 'sign'=>$sign['sign'],
  726. 'nonce'=>$sign['nonce']
  727. ];
  728. $headers = [];
  729. array_push($headers, "Content-Type".":"."application/json; charset=UTF-8");
  730. $url = 'https://miniprogram-kyc.tencentcloudapi.com/api/server/getfaceid?orderNo='.$orderNo;
  731. $result = curlRequest($url,$headers,json_encode($data),'POST');
  732. $result = json_decode($result,true);
  733. if ($result['code']==0){
  734. $data = [
  735. 'appid'=>$sign['appid'],
  736. 'userID'=>"$userId",
  737. 'sign'=>$sign['sign'],
  738. 'nonce'=>$sign['nonce'],
  739. 'sdksign'=>SdkSign($userId,$sign['nonce']),
  740. 'result'=>$result['result'],
  741. ];
  742. return $data;
  743. }
  744. echo $result['msg'];die;
  745. }
  746. /**
  747. * 生成 SDK 接口调用步骤使用签名
  748. */
  749. function SdkSign($userId,$nonce){
  750. $wbappid = getFaceInfo()['appid'];
  751. $version = '1.0.0';
  752. $ticket = getApiTicket('NONCE',$userId)['tickets'][0]['value'];
  753. //$nonce = get32Str();
  754. $array = [$wbappid, "$userId",$nonce, $version, $ticket];
  755. file_put_contents("sdksignLog.txt", json_encode($array) . "\n" . "\n", FILE_APPEND);
  756. sort($array);
  757. $str='';
  758. foreach ($array as $kk=>$vv){
  759. $str .=$vv;
  760. }
  761. $result = strtoupper(sha1($str));
  762. return $result;
  763. }
  764. function getFaceInfo(){
  765. //正式
  766. $date = [
  767. 'appid'=>'IDAKcvCn',
  768. 'secret'=>'L1id3z5zROK8SVXg72ZyZXc8ziniYLKpOnIzN1gecrBaEMaKIlKrreyRGCLU2z4l'
  769. ];
  770. //测试
  771. // $date = [
  772. // 'appid'=>'TIDA3uGO',
  773. // 'secret'=>'z41IysbEhRy5oR4Qs2kFfVkrufXIJMtsJOQ3eulkWwXwdKG772LSSiZsKKwfsv9n'
  774. // ];
  775. return $date;
  776. }
  777. //将数组转成uri字符串
  778. function formatBizQueryParaMap($paraMap, $urlencode)
  779. {
  780. $buff = "";
  781. ksort($paraMap);
  782. foreach ($paraMap as $k => $v)
  783. {
  784. if($urlencode)
  785. {
  786. $v = urlencode($v);
  787. }
  788. // $buff .= strtolower($k) . "=" . $v . "&";
  789. $buff .= $v;
  790. }
  791. if (strlen($buff) > 0)
  792. {
  793. $reqPar = substr($buff, 0, strlen($buff)-1);
  794. }
  795. return $reqPar;
  796. }
  797. /**
  798. * 生成32位随机数
  799. */
  800. function get32Str(){
  801. $length = 32;
  802. $str = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
  803. $len = strlen($str)-1;
  804. $randstr = '';
  805. for ($i=0;$i<$length;$i++) {
  806. $num=mt_rand(0,$len);
  807. $randstr .= $str[$num];
  808. }
  809. return $randstr;
  810. }
  811. function get_order_sn(){
  812. $order_id_main = date('YmdHis') . rand(10000000,99999999);
  813. $order_id_len = strlen($order_id_main);
  814. $order_id_sum = 0;
  815. for($i=0; $i<$order_id_len; $i++){
  816. $order_id_sum += (int)(substr($order_id_main,$i,1));
  817. }
  818. $osn = $order_id_main . str_pad((100 - $order_id_sum % 100) % 100,2,'0',STR_PAD_LEFT);
  819. return $osn;
  820. }
  821. /**
  822. * 生成二维码
  823. */
  824. function setqrcode($value,$name){
  825. $dir = dirname(realpath(dirname($_SERVER['SCRIPT_FILENAME']))) . '/public/user';
  826. if(!file_exists($dir)){
  827. //检查是否有该文件夹,如果没有就创建,并给予最高权限
  828. mkdir($dir, 0700,true);
  829. }
  830. $filename = $dir.'/'.$name.'.png';
  831. QRcode::png($value,$filename,QR_ECLEVEL_L,7);
  832. return 'http://'.$_SERVER['SERVER_NAME']."/user/".$name.'.png';
  833. }
  834. //生成邀请二维码
  835. function setintivecode($user_id){
  836. $name = $user_id."_".time();
  837. $address = 'http://'.$_SERVER['SERVER_NAME'].'/h5/login.html?invite_code='.$user_id;
  838. $url = setqrcode($address,$name);
  839. return $url;
  840. }
  841. //邀请地址
  842. function getintiveaddress($user_id){
  843. $address = 'http://'.$_SERVER['SERVER_NAME'].'/h5/login.html?invite_code='.$user_id;
  844. return $address;
  845. }
  846. /**
  847. * @param $url
  848. * @param array $headers
  849. * @param array $body
  850. * @param string $method
  851. * @return bool|string
  852. */
  853. function curlRequest($url, $headers = [], $body = [], $method = "GET")
  854. {
  855. $ch = curl_init();
  856. curl_setopt($ch, CURLOPT_URL, $url);
  857. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);//设置请求头
  858. curl_setopt($ch, CURLOPT_POSTFIELDS, $body);//设置请求体
  859. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); //定义请求类型
  860. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  861. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  862. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  863. curl_setopt($ch, CURLOPT_HTTP_VERSION, 'CURL_HTTP_VERSION_1_1');
  864. $output = curl_exec($ch);
  865. curl_close($ch);
  866. return $output;
  867. }