common.php 34 KB

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