common.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. <?php
  2. use think\Db;
  3. /**
  4. * 秒转换为天
  5. */
  6. function get_stay_time($remain_time, $is_hour = 1, $is_minutes = 1)
  7. {
  8. $day = floor($remain_time / (3600*24));
  9. $day = $day > 0 ? $day.'天' : '';
  10. $hour = floor(($remain_time % (3600*24)) / 3600);
  11. $hour = $hour > 0 ? $hour.'小时' : '';
  12. if($is_hour && $is_minutes) {
  13. $minutes = floor((($remain_time % (3600*24)) % 3600) / 60);
  14. $minutes = $minutes > 0 ? $minutes.'分钟' : '';
  15. return $day.$hour.$minutes;
  16. }
  17. if($hour) {
  18. return $day.$hour;
  19. }
  20. return $day;
  21. }
  22. //获取全图片地址 $image_data
  23. function image_path($image_data){
  24. if(empty($image_data)){
  25. return $image_data;
  26. }
  27. if (strpos($image_data,'|')!==false){
  28. $image_res = explode('|',$image_data);
  29. }elseif(strpos($image_data,',')!==false){
  30. $image_res = explode(',',$image_data);
  31. }else{
  32. $image_res = array($image_data);
  33. }
  34. return $image_res;
  35. }
  36. // 获取模板区域
  37. function get_city_area()
  38. {
  39. $field=['id','pid','name'];
  40. $list=Db::table('store_area')->where('pid',0)->field($field)->select();
  41. foreach ($list as $k=>&$v){
  42. $v['children']= Db::table('store_area')->where('pid',$v['id'])->field($field)->select();
  43. }
  44. return $list;
  45. }
  46. function http_curl($url,$type='get',$res='json',$arr=''){
  47. $headers = array();
  48. //根据API的要求,定义相对应的Content-Type
  49. array_push($headers, "Content-Type".":"."application/x-www-form-urlencoded; charset=UTF-8;application/json");
  50. $curl = curl_init();
  51. curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $type);
  52. curl_setopt($curl, CURLOPT_URL, $url);
  53. curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
  54. curl_setopt($curl, CURLOPT_FAILONERROR, false);
  55. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  56. curl_setopt($curl, CURLOPT_HEADER, false);
  57. $output = curl_exec($curl);
  58. curl_close($curl);
  59. if($res=='json'){
  60. if($output === false){
  61. //请求失败,返回错误信息
  62. return curl_error($curl);
  63. }else{
  64. //请求成功,返回信息
  65. return json_decode($output,true);
  66. }
  67. }
  68. }
  69. // 获取物流信息
  70. function get_delivery($send_no = 'JD0053309649641',$express_code=''){
  71. error_reporting(E_ALL || ~E_NOTICE);
  72. $AppKey = 204008273;
  73. $AppSecret ='t9PavvfCeK5v2XidwyK5pWDp8b0hzMq4';
  74. $AppCode ='47f640e3529d43e28365311a530db2b7';//开通服务后 买家中心-查看AppCode
  75. $host = "https://wuliu.market.alicloudapi.com";//api访问链接
  76. $path = "/kdi";//API访问后缀
  77. $method = "GET";
  78. $body ='';
  79. $headers = array();
  80. array_push($headers, "Authorization:APPCODE " . $AppCode);
  81. $querys = "no={$send_no}&type={$express_code}"; //参数写在这里
  82. $url = $host . $path . "?" . $querys;
  83. $curl = curl_init();
  84. curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
  85. curl_setopt($curl, CURLOPT_URL, $url);
  86. curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
  87. curl_setopt($curl, CURLOPT_FAILONERROR, false);
  88. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  89. curl_setopt($curl, CURLOPT_HEADER, true);
  90. if (1 == strpos("$" . $host, "https://")) {
  91. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  92. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  93. }
  94. $out_put = curl_exec($curl);
  95. $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
  96. list($header, $body) = explode("\r\n\r\n", $out_put, 2);
  97. if ($httpCode == 200) {
  98. return json_decode($body,true)['result'];
  99. } else {
  100. return [];
  101. }
  102. }
  103. // 元石日志
  104. function crystal_log($user_id,$crystal,$desc,$type,$rel_id=0)
  105. {
  106. $log_data = [
  107. 'user_id' => $user_id,
  108. 'create_at' => date('Y-m-d H:i:s'),
  109. 'crystal' => $crystal,
  110. 'desc' => $desc,
  111. 'type' => $type,
  112. 'rel_id' => $rel_id,
  113. ];
  114. Db::table('crystal_info')->insert($log_data);
  115. }
  116. /**
  117. * 判断是否为合法的身份证号码
  118. * @param $mobile
  119. * @return int
  120. */
  121. function isCreditNo($vStr){
  122. $vCity = array(
  123. '11','12','13','14','15','21','22',
  124. '23','31','32','33','34','35','36',
  125. '37','41','42','43','44','45','46',
  126. '50','51','52','53','54','61','62',
  127. '63','64','65','71','81','82','91'
  128. );
  129. if (!preg_match('/^([\d]{17}[xX\d]|[\d]{15})$/', $vStr)) return false;
  130. if (!in_array(substr($vStr, 0, 2), $vCity)) return false;
  131. $vStr = preg_replace('/[xX]$/i', 'a', $vStr);
  132. $vLength = strlen($vStr);
  133. if ($vLength == 18) {
  134. $vBirthday = substr($vStr, 6, 4) . '-' . substr($vStr, 10, 2) . '-' . substr($vStr, 12, 2);
  135. } else {
  136. $vBirthday = '19' . substr($vStr, 6, 2) . '-' . substr($vStr, 8, 2) . '-' . substr($vStr, 10, 2);
  137. }
  138. if (date('Y-m-d', strtotime($vBirthday)) != $vBirthday) return false;
  139. if ($vLength == 18) {
  140. $vSum = 0;
  141. for ($i = 17 ; $i >= 0 ; $i--) {
  142. $vSubStr = substr($vStr, 17 - $i, 1);
  143. $vSum += (pow(2, $i) % 11) * (($vSubStr == 'a') ? 10 : intval($vSubStr , 11));
  144. }
  145. if($vSum % 11 != 1) return false;
  146. }
  147. return true;
  148. }
  149. // 实名认证
  150. function user_certification($id_card,$name){
  151. $host = "http://checkone.market.alicloudapi.com";
  152. $path = "/chinadatapay/1882";
  153. $method = "POST";
  154. $appcode = "30be8bdcc65842919980a8276ffc4995";
  155. $headers = array();
  156. array_push($headers, "Authorization:APPCODE " . $appcode);
  157. //根据API的要求,定义相对应的Content-Type
  158. array_push($headers, "Content-Type".":"."application/x-www-form-urlencoded; charset=UTF-8");
  159. $bodys = "idcard=".$id_card."&name=".$name;
  160. $url = $host . $path;
  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. dump($res);die;
  176. if($res){
  177. $res = json_decode($res,true);
  178. if($res['data']['result'] == 1){
  179. return 1;
  180. }else{
  181. return 0;
  182. }
  183. }else{
  184. return 0;
  185. }
  186. }
  187. function shoucang(){
  188. $host = "http://180.76.141.31:8888/cz/call";
  189. $method = "POST";
  190. $headers = array();
  191. array_push($headers, "Content-Type".":"."application/json; charset=UTF-8");
  192. $bodys = array(
  193. 'key' => 'test2',
  194. 'userkey' => '12344',
  195. 'product' => json_encode(array('name'=>'test','count'=>7))
  196. );
  197. $curl = curl_init();
  198. curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
  199. curl_setopt($curl, CURLOPT_URL, $host);
  200. curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
  201. curl_setopt($curl, CURLOPT_FAILONERROR, false);
  202. curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  203. curl_setopt($curl, CURLOPT_HEADER, false);
  204. if (1 == strpos("$".$host, "https://"))
  205. {
  206. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
  207. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
  208. }
  209. curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($bodys));
  210. $res = curl_exec($curl);
  211. var_dump($res);exit();
  212. }
  213. /**
  214. * 获取大师提前分钟数
  215. */
  216. function getAdvanceMinutes(){
  217. return Db::name('system_config')->where('name','advance_minutes')->value('value');
  218. }
  219. /**
  220. * 获取自动取消分钟数
  221. */
  222. function getCancelTime(){
  223. return Db::name('system_config')->where('name','cancel_time')->value('value');
  224. }
  225. /**
  226. * 用户信息存入redis hash
  227. */
  228. function setMemberInfoHash($member_id){
  229. $info = Db::name('store_member')->where('id',$member_id)->find();
  230. $redis = new \think\cache\driver\Redis();
  231. $array = ['member'.$member_id=>json_encode($info)];
  232. $redis->hMSet('memberInfo',$array);
  233. }
  234. /**
  235. * redis获取用户信息
  236. */
  237. function getMemberInfoHash($member_id){
  238. $redis = new \think\cache\driver\Redis();
  239. $info = $redis->hGet('memberInfo','member'.$member_id);
  240. if (!$info){
  241. $info = Db::name('store_member')->where('id',$member_id)->find();
  242. $array = ['member'.$member_id=>json_encode($info)];
  243. $redis->hMSet('memberInfo',$array);
  244. }else{
  245. $info = json_decode($info,true);
  246. }
  247. return $info;
  248. }
  249. /**
  250. * 用户抢购卡数量存入redis 字符串
  251. */
  252. function setMembercard($member_id){
  253. $snap_card = Db::name('store_member')->where('id',$member_id)->value('snap_card');
  254. $redis = new \think\cache\driver\Redis();
  255. $redis->set('membercard_'.$member_id,$snap_card);
  256. }
  257. /**
  258. * 获取用户抢购卡数量
  259. */
  260. function getMembercard($member_id){
  261. $redis = new \think\cache\driver\Redis();
  262. $count = $redis->get('membercard_'.$member_id);
  263. if (empty($count) && $count!=0){
  264. $count = Db::name('store_member')->where('id',$member_id)->value('snap_card');
  265. $redis->set('membercard_'.$member_id,$count);
  266. }
  267. return $count;
  268. }
  269. /**
  270. * 获取藏品库存
  271. */
  272. function getCollectionInventory($id){
  273. $redis = new \think\cache\driver\Redis();
  274. $count = $redis->get('collection_count_'.$id);
  275. if (empty($count) && $count!=0){
  276. $count = Db::name('store_collection')->where('id',$id)->value('now_inventory');
  277. $redis->set('collection_count_'.$id,$count);
  278. }
  279. return $count;
  280. }
  281. /**
  282. * 减掉藏品库存
  283. */
  284. function loseCollectionInventory($id,$num){
  285. $redis = new \think\cache\driver\Redis();
  286. $redis->Decrby('collection_count_'.$id,$num);
  287. }
  288. /**
  289. * 加藏品库存
  290. */
  291. function addCollectionInventory($id,$num){
  292. $redis = new \think\cache\driver\Redis();
  293. $redis->Incrby('collection_count_'.$id,$num);
  294. }
  295. /**
  296. * 用户购买藏品排名更新
  297. */
  298. function saveRanking($id){
  299. $redis = new \think\cache\driver\Redis();
  300. $redis->Incr('ranking'.$id);
  301. }
  302. /**
  303. * 获取用户购买藏品排名更新
  304. */
  305. function getRanking($id){
  306. $redis = new \think\cache\driver\Redis();
  307. $count = $redis->get('ranking'.$id);
  308. if (empty($count)){
  309. $redis->set('ranking'.$id,0);
  310. }
  311. $count = empty($count) ? 0 : $count;
  312. return $count;
  313. }
  314. /**
  315. * 藏品信息存入redis hash
  316. */
  317. function setCollectionInfoHash($id){
  318. $info = Db::name('store_collection')->where('id',$id)->find();
  319. $redis = new \think\cache\driver\Redis();
  320. $array = ['collection'.$id=>json_encode($info)];
  321. $redis->hMSet('collectionInfo',$array);
  322. }
  323. /**
  324. * 获取藏品信息
  325. */
  326. function getCollectionInfoHash($id)
  327. {
  328. $redis = new \think\cache\driver\Redis();
  329. $info = $redis->hGet('collectionInfo','collection'.$id);
  330. if (!$info){
  331. $info = Db::name('store_collection')->where('id',$id)->find();
  332. $array = ['collection'.$id=>json_encode($info)];
  333. $redis->hMSet('collectionInfo',$array);
  334. }else{
  335. $info = json_decode($info,true);
  336. }
  337. return $info;
  338. }
  339. function retrunWxConfig(){
  340. $config = [
  341. 'app_id' => 'wxa79190389d57c04b',
  342. 'mch_id' =>'1623066512',
  343. 'key' =>'qwertyuiopASDFGHJKLZXCVBNM123456',
  344. // 'sub_mch_id'=>'1612529165'
  345. ];
  346. return $config;
  347. }
  348. /**
  349. * 获取排名
  350. */
  351. function getTag($id,$now,$num){
  352. $len = strlen($num);
  353. $re = 'XX'.$id.'#';
  354. switch ($len){
  355. case 1:
  356. $re = $re.'0'.$now;
  357. break;
  358. case 2:
  359. if ($now<10){
  360. $re = $re.'0'.$now;
  361. }else{
  362. $re = $re.$now;
  363. }
  364. break;
  365. case 3:
  366. if ($now<10){
  367. $re = $re.'00'.$now;
  368. }elseif ($now>=10 && $now<100){
  369. $re = $re.'0'.$now;
  370. }else{
  371. $re = $re.$now;
  372. }
  373. break;
  374. case 4:
  375. if ($now<10){
  376. $re = $re.'000'.$now;
  377. }elseif ($now>=10 && $now<100){
  378. $re = $re.'00'.$now;
  379. }elseif ($now>=100 && $now<1000){
  380. $re = $re.'0'.$now;
  381. }else{
  382. $re = $re.$now;
  383. }
  384. break;
  385. case 5:
  386. if ($now<10){
  387. $re = $re.'0000'.$now;
  388. }elseif ($now>=10 && $now<100){
  389. $re = $re.'000'.$now;
  390. }elseif ($now>=100 && $now<1000){
  391. $re = $re.'00'.$now;
  392. }elseif ($now>=1000 && $now<10000){
  393. $re = $re.'0'.$now;
  394. }else{
  395. $re = $re.$now;
  396. }
  397. break;
  398. case 6:
  399. if ($now<10){
  400. $re = $re.'00000'.$now;
  401. }elseif ($now>=10 && $now<100){
  402. $re = $re.'0000'.$now;
  403. }elseif ($now>=100 && $now<1000){
  404. $re = $re.'000'.$now;
  405. }elseif ($now>=1000 && $now<10000){
  406. $re = $re.'00'.$now;
  407. }elseif ($now>=10000 && $now<100000){
  408. $re = $re.'0'.$now;
  409. }else{
  410. $re = $re.$now;
  411. }
  412. break;
  413. }
  414. $re = $re.'/'.$num;
  415. return $re;
  416. }
  417. /**
  418. * 变更会员积分等
  419. * @param int $money 余额
  420. * @param int $user_id 会员ID
  421. * @param string $memo 备注
  422. */
  423. function memberMoneyChange($num, $type , $m_id, $title, $pm = 0)
  424. {
  425. $member = Db::name('store_member')->where('id',$m_id)->find();
  426. switch ($type){
  427. case 1: //积分
  428. $before = $member['integral'];
  429. if ($pm==1)
  430. $after = $member['integral']+$num;
  431. else
  432. $after = $member['integral']-$num;
  433. $update_data = ['integral'=>$after];
  434. break;
  435. case 2: //抢购卡
  436. $before = $member['snap_card'];
  437. if ($pm==1)
  438. $after = $member['snap_card']+$num;
  439. else
  440. $after = $member['snap_card']-$num;
  441. $update_data = ['snap_card'=>$after];
  442. break;
  443. }
  444. Db::startTrans();
  445. try {
  446. Db::name('store_member')->where('id',$m_id)->update($update_data);
  447. $data = [
  448. 'm_id'=>$m_id,
  449. 'type'=>$type,
  450. 'pm'=>$pm,
  451. 'title'=>$title,
  452. 'change_money'=>$num,
  453. 'before'=>$before,
  454. 'after'=>$after
  455. ];
  456. Db::name('store_member_log')->insert($data);
  457. Db::commit();
  458. return true;
  459. }catch (\Exception $e){
  460. Db::rollback();
  461. return false;
  462. }
  463. }