common.php 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479
  1. <?php
  2. // 应用公共文件
  3. use app\common\service\AuthService;
  4. use Rsa\RsaClient;
  5. use services\CacheServices;
  6. use think\facade\Cache;
  7. use think\facade\Db;
  8. use utils\Json;
  9. use JPush\Client as JPush;
  10. if (!function_exists('__url')) {
  11. /**
  12. * 构建URL地址
  13. * @param string $url
  14. * @param array $vars
  15. * @param bool $suffix
  16. * @param bool $domain
  17. * @return string
  18. */
  19. function __url(string $url = '', array $vars = [], $suffix = true, $domain = false)
  20. {
  21. return url($url, $vars, $suffix, $domain)->build();
  22. }
  23. }
  24. if (!function_exists('system_box_typesetting')) {
  25. /**
  26. * 构建URL地址
  27. * @param string $url
  28. * @param array $vars
  29. * @param bool $suffix
  30. * @param bool $domain
  31. * @return string
  32. */
  33. function system_box_typesetting(string $url = '', array $vars = [], $suffix = true, $domain = false)
  34. {
  35. return [
  36. ['id'=>10, 'name'=>'普通'],
  37. ['id'=>20, 'name'=>'稀有'],
  38. ['id'=>30, 'name'=>'史诗'],
  39. ['id'=>40, 'name'=>'传说']
  40. ];
  41. }
  42. }
  43. /**
  44. * 请求
  45. */
  46. if (!function_exists('_curlrequest')) {
  47. function _curlrequest (string $url, $data = null, string $type = 'get', array $header = null)
  48. {
  49. // 拼装url
  50. if (!empty($data) && $type == 'get') {
  51. $url = $url . '?' . http_build_query($data);
  52. }
  53. $ch = curl_init(); // 初始化curl
  54. // 设置选项
  55. curl_setopt($ch, CURLOPT_URL, $url);
  56. curl_setopt($ch, CURLOPT_HEADER, false);
  57. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  58. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 这个是重点
  59. if ($type == 'post') {
  60. // post
  61. curl_setopt($ch, CURLOPT_POST, 1); // 设置为post
  62. curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  63. }
  64. //设置header头
  65. if (!empty($header)) {
  66. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  67. }
  68. // 执行并获取内容
  69. $output = curl_exec($ch);
  70. // 释放curl句柄
  71. curl_close($ch);
  72. return $output;
  73. }
  74. }
  75. if (!function_exists('shumei_event')) {
  76. // enevtId 事件 比如 register login tokenId 用户的id deviceId前端的id
  77. function shumei_event($eventId, $tokenId, $deviceId)
  78. {
  79. return ;
  80. if (env('APP.CUR_SYS_PARAMS') != 1){
  81. $ip = get_real_ip();
  82. if ($ip=='127.0.0.1'){
  83. $ip = '123.45.32.55';
  84. }
  85. $params = array(
  86. 'accessKey'=>env('app.SM_accessKey'),
  87. 'appId'=>env('app.SM_appId'),
  88. 'eventId'=>$eventId,
  89. 'data'=>array('tokenId'=>strval($tokenId), 'ip'=>$ip,'timestamp'=>intval(time().'000'), 'deviceId'=>$deviceId)
  90. );
  91. $res = _curlrequest('http://api-skynet-bj.fengkongcloud.com/v4/event', json_encode($params),'post');
  92. // sr_log(json_encode($res));
  93. }
  94. }
  95. }
  96. if (!function_exists('password')) {
  97. /**
  98. * 密码加密算法
  99. * @param $value 需要加密的值
  100. * @param $type 加密类型,默认为md5 (md5, hash)
  101. * @return mixed
  102. */
  103. function password($value)
  104. {
  105. $value = sha1('blog_') . md5($value) . md5('_encrypt') . sha1($value);
  106. return sha1($value);
  107. }
  108. }
  109. /**
  110. * debug调试
  111. * @deprecated 不建议使用,建议直接使用框架自带的log组件
  112. * @param string|array $data 打印信息
  113. * @param string $type 类型
  114. * @param string $suffix 文件后缀名
  115. * @param bool $force
  116. * @param null $file
  117. */
  118. //if (!function_exists('xdebug')) {
  119. // function xdebug($data, $type = 'xdebug', $suffix = null, $force = false, $file = null)
  120. // {
  121. // !is_dir(runtime_path() . 'xdebug/') && mkdir(runtime_path() . 'xdebug/');
  122. // if (is_null($file)) {
  123. // $file = is_null($suffix) ? runtime_path() . 'xdebug/' . date('Ymd') . '.txt' : runtime_path() . 'xdebug/' . date('Ymd') . "_{$suffix}" . '.txt';
  124. // }
  125. // file_put_contents($file, "[" . date('Y-m-d H:i:s') . "] " . "========================= {$type} ===========================" . PHP_EOL, FILE_APPEND);
  126. // $str = (is_string($data) ? $data : (is_array($data) || is_object($data)) ? print_r($data, true) : var_export($data, true)) . PHP_EOL;
  127. // $force ? file_put_contents($file, $str) : file_put_contents($file, $str, FILE_APPEND);
  128. // }
  129. //}
  130. function sr_randFloat($min=0, $max=1){
  131. return $min + mt_rand()/mt_getrandmax() * ($max-$min);
  132. }
  133. if (!function_exists('sysconfig')) {
  134. /**
  135. * 获取系统配置信息
  136. * @param $group
  137. * @param null $name
  138. * @return array|mixed
  139. */
  140. function sysconfig($group, $name = null)
  141. {
  142. $where = ['group' => $group];
  143. $value = empty($name) ? Cache::get("sysconfig_{$group}") : Cache::get("sysconfig_{$group}_{$name}");
  144. if (empty($value)) {
  145. if (!empty($name)) {
  146. $where['name'] = $name;
  147. $value = \app\common\model\SystemConfig::where($where)->value('value');
  148. Cache::tag('sysconfig')->set("sysconfig_{$group}_{$name}", $value, 3600);
  149. } else {
  150. $value = \app\common\model\SystemConfig::where($where)->column('value', 'name');
  151. Cache::tag('sysconfig')->set("sysconfig_{$group}", $value, 3600);
  152. }
  153. }
  154. return $value;
  155. }
  156. }
  157. if (!function_exists('array_format_key')) {
  158. /**
  159. * 二位数组重新组合数据
  160. * @param $array
  161. * @param $key
  162. * @return array
  163. */
  164. function array_format_key($array, $key)
  165. {
  166. $newArray = [];
  167. foreach ($array as $vo) {
  168. $newArray[$vo[$key]] = $vo;
  169. }
  170. return $newArray;
  171. }
  172. }
  173. if (!function_exists('auth')) {
  174. /**
  175. * auth权限验证
  176. * @param $node
  177. * @return bool
  178. * @throws \think\db\exception\DataNotFoundException
  179. * @throws \think\db\exception\DbException
  180. * @throws \think\db\exception\ModelNotFoundException
  181. */
  182. function auth($node = null)
  183. {
  184. $authService = new AuthService(session('admin.id'));
  185. $check = $authService->checkNode($node);
  186. return $check;
  187. }
  188. }
  189. if (!function_exists('sr_getcurtime')) {
  190. // 获取当前时间字符串
  191. function sr_getcurtime($time, $format='Y-m-d H:i:s')
  192. {
  193. return date($format, $time);
  194. }
  195. }
  196. /*
  197. * json 返回失败
  198. */
  199. if (!function_exists('api_error_return')){
  200. function api_error_return($msg){
  201. $json = new Json();
  202. return $json->json_error($msg);
  203. }
  204. }
  205. /*
  206. * json 返回成功
  207. */
  208. if (!function_exists('api_succ_return')){
  209. function api_succ_return($msg){
  210. $json = new Json();
  211. return $json->json_success($msg);
  212. }
  213. }
  214. /**
  215. * 对象转数组
  216. */
  217. if (!function_exists('object_array')) {
  218. function object_array ($array)
  219. {
  220. if (is_object($array)) {
  221. $array = (array)$array;
  222. }
  223. if (is_array($array)) {
  224. foreach ($array as $key => $value) {
  225. $array[$key] = object_array($value);
  226. }
  227. }
  228. return $array;
  229. }
  230. }
  231. /*
  232. * 校验是否满足公共参数 return true不满足 returnfalse 有公共参数
  233. * */
  234. function ISNOTREQUESTPASS($data)
  235. {
  236. if (!isset($data['app_sources'])){
  237. return true;
  238. }
  239. // sys_model 系统型号
  240. if (in_array($data['app_sources'],['ios', 'android'] )){
  241. if (!isset($data['app_version']) || !isset($data['app_sources']) || !isset($data['timestamp']) || !isset($data['sys_version']) || !isset($data['sys_model']) || !isset($data['udid'])){
  242. return true;
  243. }
  244. }
  245. if (isset($data['app_sources']) && !in_array($data['app_sources'], ['ios', 'android', 'miniapp'])){
  246. return true;
  247. }
  248. return false;
  249. }
  250. function toUrlParams ($params)
  251. {
  252. $string = '';
  253. if (!empty($params)) {
  254. $array = array();
  255. foreach ($params as $key => $value) {
  256. $array[] = $key . '=' . $value;
  257. }
  258. $string = implode("&", $array);
  259. }
  260. return $string;
  261. }
  262. function getUrlParams($url){
  263. $arr = explode('?', $url);
  264. if (isset($arr[1])){
  265. $arr1 = explode('&', $arr[1]);
  266. $res = [];
  267. foreach ($arr1 as $key=>$val){
  268. $arr3 = explode('=', $val);
  269. $res[$arr3[0]] = $arr3[1];
  270. }
  271. return $res;
  272. }else{
  273. return [];
  274. }
  275. }
  276. // 验证密码是否合规
  277. if (!function_exists('vertifyPass')) {
  278. function vertifyPass (string $pass)
  279. {
  280. if (strlen($pass) < 8){
  281. return false;
  282. }
  283. // 包含字母 并且包含数字
  284. if (preg_match('/\d+/', $pass) && preg_match('/[a-zA-Z]+/', $pass)){
  285. return true;
  286. }
  287. return false;
  288. }
  289. }
  290. // 验证用户名是否合规
  291. if (!function_exists('vertifyUserName')) {
  292. function vertifyUserName (string $pass)
  293. {
  294. if (strlen($pass) < 7 || strlen($pass) > 15){
  295. return false;
  296. }
  297. // 包含字母 并且包含数字
  298. if (preg_match('/\d+/', $pass) && preg_match('/[a-zA-Z]+/', $pass)){
  299. return true;
  300. }
  301. return false;
  302. }
  303. }
  304. // 验证支付密码是否合规 必须是六位数字
  305. if (!function_exists('vertifySecurityPass')) {
  306. function vertifySecurityPass (string $pass)
  307. {
  308. if (strlen($pass) != 6){
  309. return false;
  310. }
  311. // 包含字母 并且包含数字
  312. if (preg_match("/^\d*$/", $pass)){
  313. return true;
  314. }
  315. return false;
  316. }
  317. }
  318. // 解密
  319. function AESjiemi($str){
  320. return \utils\AES::decrypt($str);
  321. }
  322. // 加密
  323. function AESjiami($str){
  324. return \utils\AES::encrypt($str);
  325. }
  326. function weibao_posturl($url,$headerArray,$data){
  327. // $data = json_encode($data);
  328. $curl = curl_init();
  329. curl_setopt($curl, CURLOPT_URL, $url);
  330. curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
  331. curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,FALSE);
  332. curl_setopt($curl, CURLOPT_POST, 1);
  333. curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
  334. curl_setopt($curl,CURLOPT_HTTPHEADER,$headerArray);
  335. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  336. $output = curl_exec($curl);
  337. curl_close($curl);
  338. return json_decode($output,true);
  339. }
  340. /*
  341. * 前端品味数娱签名
  342. * */
  343. if (!function_exists('createPwApiSign')) {
  344. function createApiSigncreatePwApiSign (array $params): string
  345. {
  346. //第一步、参数名ASCII码从小到大排序(字典序),区分大小写,参数为空也参与签名。
  347. ksort($params);
  348. //第二步、将排序好的参数使用URI参数的方式拼接密钥,得到字符串 waitSignParamsStr。
  349. $waitSignParamsStr = http_build_query($params);
  350. //第三步、使用 '&key=apiKey’方式拼接到waitSignParamsStr字符结尾,得到字符串waitSign。
  351. $app_key = env('xzthird.PINWEIKEY', '');
  352. $waitSign = $waitSignParamsStr.'&key='.$app_key;
  353. // 此时$waitSign = 'appid=WBSGTRTMR6&key=D5W7AsARzGWAiznSNKnjqeg61cU44w51'
  354. //第四步、对waitSign字符串MD5摘要,并转换为全大写。
  355. $sign = strtoupper(md5($waitSign));
  356. //此时$sign = '2323481C19EDAE9560705A321072EBF0';
  357. return $sign;
  358. // unset($params['sign']);
  359. // //签名步骤一:按字典序排序数组参数
  360. // ksort($params);
  361. // $string = toUrlParams($params);
  362. //
  363. //
  364. // //签名步骤二:在string后加入KEY
  365. // $app_key = env('xzthird.PINWEIKEY', '');
  366. // $string = trim($string . "&key=" . $app_key);
  367. //
  368. //// $string = str_replace(' ', '', $string);
  369. // // 防止前端传数组字符串的 双引号被编码了
  370. // $string = htmlspecialchars_decode($string);
  371. ////return $string;
  372. // //签名步骤三:MD5加密
  373. // $string = md5($string);
  374. // //签名步骤四:所有字符转为大写
  375. // $result = strtoupper($string);
  376. // return $result;
  377. }
  378. }
  379. /*
  380. * 前端生成签名
  381. * */
  382. if (!function_exists('createApiSign')) {
  383. function createApiSign (array $params): string
  384. {
  385. unset($params['sign']);
  386. //签名步骤一:按字典序排序数组参数
  387. ksort($params);
  388. $string = toUrlParams($params);
  389. //签名步骤二:在string后加入KEY
  390. $app_key = env('api.APP_KEY');
  391. $string = trim($string . "&key=" . $app_key);
  392. // sr_log('addKey:'.$string);
  393. // $string = str_replace(' ', '', $string);
  394. // 防止前端传数组字符串的 双引号被编码了
  395. $string = htmlspecialchars_decode($string);
  396. // sr_log('addTrim:'.$string);
  397. //签名步骤三:MD5加密
  398. // $string = md5($string);
  399. $string = AESjiami($string);
  400. // sr_log('sign:'.$string);
  401. //签名步骤四:所有字符转为大写
  402. $result = strtoupper($string);
  403. return $result;
  404. }
  405. }
  406. /**
  407. * 手机验证码验证
  408. */
  409. if (!function_exists('sms_code_verify')) {
  410. function sms_code_verify ($param, $type = '')
  411. {
  412. /** @var \sms\Sms $sms */
  413. $sms = app()->make(\sms\Sms::class);
  414. if (!$sms->scene($type)->check($param['mobile'], $param['code'])) { // 验证手机验证码
  415. header('Content-Type:application/json; charset=utf-8');
  416. echo json_encode(['msg' => $sms->getErrorMsg(), 'code' => 500, 'data' => []]);
  417. exit;
  418. }
  419. }
  420. }
  421. /**
  422. * 生成随机不重复的六位数
  423. */
  424. if (!function_exists('create_invite_code')) {
  425. function create_invite_code ()
  426. {
  427. $count = 0;
  428. do {
  429. $d = date('his', time()).sprintf('%03d', rand(0, 99));
  430. $count = Db::name('user')->where('code|px_code', $d)->count(); //保证生成的邀请码不会重复,查询表是否存在此邀请码
  431. } while ($count > 0);
  432. return $d;
  433. // 生成八位英文跟字母 保证不会重复
  434. // if (true){
  435. // $count = 0;
  436. // do {
  437. // $code = 'ABCDEFGHJKLMNPQRSTUVWXYZ';
  438. // $rand = $code[rand(0,23)]
  439. // .strtoupper(dechex(date('m')))
  440. // .date('d').substr(time(),-5)
  441. // .substr(microtime(),2,5)
  442. // .sprintf('%02d',rand(0,99));
  443. // for(
  444. // $a = md5( $rand, true ),
  445. // $s = '0123456789ABCDEFGHIJKLMNOPQRSTUV',
  446. // $d = '',
  447. // $f = 0;
  448. // $f < 8;
  449. // $g = ord( $a[ $f ] ),
  450. // $d .= $s[ ( $g ^ ord( $a[ $f + 8 ] ) ) - $g & 0x1F ],
  451. // $f++
  452. // );
  453. //
  454. // $count = Db::name('user')->where('code', $d)->count(); //保证生成的邀请码不会重复,查询表是否存在此邀请码
  455. // } while ($count > 0);
  456. //
  457. // return $d;
  458. // }
  459. // 生成六位的编码 数字跟英文
  460. // if (true){
  461. // $v0 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  462. // $v1 = $v0[rand(0, 25)] . strtoupper(dechex(date('m'))) . date('d') . substr(time(), -5) . substr(microtime(), 2, 5) . sprintf('%02d', rand(0, 99));
  463. // for ($v2 = md5($v1, !0), $v3 = '0123456789ABCDEFGHIJKLMNOPQRSTUV', $v4 = '', $v5 = 0; $v5 < 6; $v6 = ord($v2[$v5]), $v4 .= $v3[($v6 ^ ord($v2[$v5 + 6])) - $v6 & 31], $v5++) {
  464. // }
  465. // return $v4;
  466. // }
  467. }
  468. }
  469. /**
  470. * 获取网站的url
  471. */
  472. function getWebUrl()
  473. {
  474. $http_type = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https' : 'http';
  475. return $http_type.'://' . input('server.HTTP_HOST') ;
  476. }
  477. if (!function_exists('__HTTPLOCALIMG')) {
  478. function __HTTPLOCALIMG($img_path)
  479. {
  480. if (empty($img_path)){
  481. return '';
  482. }
  483. return getWebUrl() .'/'.$img_path;
  484. }
  485. }
  486. if (!function_exists('__HTTPIMG')) {
  487. function __HTTPIMG($img_path)
  488. {
  489. if (empty($img_path)){
  490. return '';
  491. }
  492. return getWebUrl() .'/'.$img_path;
  493. }
  494. }
  495. if (!function_exists('__HTTPSAVEIMGADMIN')) {
  496. function __HTTPSAVEIMGADMIN($img_path)
  497. {
  498. $arr = explode('upload/', $img_path);
  499. return 'upload/'.$arr[1];
  500. }
  501. }
  502. if (!function_exists('__HTTPGETIMGADMIN')) {
  503. function __HTTPGETIMGADMIN($img_path)
  504. {
  505. return env('APP.API_WEB_URL').'/'.$img_path;
  506. }
  507. }
  508. // 加密 :encrypt('str','E','nowamagic');
  509. // 解密 :encrypt('被加密过的字符串','D','nowamagic');
  510. // 参数说明:
  511. // $string :需要加密解密的字符串
  512. // $operation:判断是加密还是解密:E:加密 D:解密
  513. // $key :加密的钥匙(密匙);
  514. if (!function_exists('cryption')) {
  515. function cryption($string, $operation, $key = '')
  516. {
  517. $key = md5($key);
  518. $key_length = strlen($key);
  519. $string = $operation == 'D' ? base64_decode($string) : substr(md5($string . $key), 0, 8) . $string;
  520. $string_length = strlen($string);
  521. $rndkey = $box = array();
  522. $result = '';
  523. for ($i = 0; $i <= 255; $i++) {
  524. $rndkey[$i] = ord($key[$i % $key_length]);
  525. $box[$i] = $i;
  526. }
  527. for ($j = $i = 0; $i < 256; $i++) {
  528. $j = ($j + $box[$i] + $rndkey[$i]) % 256;
  529. $tmp = $box[$i];
  530. $box[$i] = $box[$j];
  531. $box[$j] = $tmp;
  532. }
  533. for ($a = $j = $i = 0; $i < $string_length; $i++) {
  534. $a = ($a + 1) % 256;
  535. $j = ($j + $box[$a]) % 256;
  536. $tmp = $box[$a];
  537. $box[$a] = $box[$j];
  538. $box[$j] = $tmp;
  539. $result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
  540. }
  541. if ($operation == 'D') {
  542. if (substr($result, 0, 8) == substr(md5(substr($result, 8) . $key), 0, 8)) {
  543. return substr($result, 8);
  544. } else {
  545. return '';
  546. }
  547. } else {
  548. return str_replace('=', '', base64_encode($result));
  549. }
  550. }
  551. }
  552. /**
  553. * 删除字符串中的所有空格
  554. */
  555. if (!function_exists('trim_string')) {
  556. function trim_string (string $str): string
  557. {
  558. $q = array(" ", " ", "\t", "\n", "\r");
  559. $h = array("", "", "", "", "");
  560. return str_replace($q, $h, $str);
  561. }
  562. }
  563. /**
  564. * 获取系统配置
  565. */
  566. if (!function_exists('get_config')) {
  567. function get_config (int $type = 1, $name = 'default')
  568. {
  569. $key = md5("system_configs:{$type}");
  570. if (($result = Cache($key)) === false || empty($result) || true) {
  571. $result = \think\facade\Db::name('configs')->where('type', $type)->column('value', 'name');
  572. if (empty($result)) return null;
  573. \cache($key, $result);
  574. }
  575. if ($name != 'default') {
  576. if (!isset($result[$name])) return null;
  577. if (!is_null(json_decode($result[$name], true)))
  578. $result = json_decode($result[$name], true);
  579. else
  580. $result = $result[$name];
  581. } else {
  582. foreach ($result as $key => $value) {
  583. if (!is_null(json_decode($result[$key], true)))
  584. $result[$key] = json_decode($result[$key], true);
  585. }
  586. }
  587. return $result;
  588. }
  589. }
  590. /**
  591. * 加密
  592. */
  593. if (!function_exists('encode')) {
  594. function encode ($str = '')
  595. {
  596. $spool = array(
  597. 0 => 'm047gl8f1j',
  598. 1 => 'wg3h0ca22l',
  599. 2 => 'j6oyw8kc3h',
  600. 3 => 'tods62c44i',
  601. 4 => 'c9f5aeb65v',
  602. 5 => 'kim7plty6w',
  603. 6 => 'pkneclga7z',
  604. 7 => '3kf46cxa8h',
  605. 8 => 'qo83ezx544',
  606. 9 => 'ivbondxc37',
  607. 10 => 's8pw43u2hf',
  608. 11 => '09xzmjr163',
  609. 12 => 'wbyn6ha23d',
  610. 13 => 'q7v92643jl',
  611. 14 => 'gh6jxkd4bt',
  612. 15 => 'kpo0zt156a',
  613. );
  614. $len_mask = 'd6a45fe94c35211';
  615. $str = $str . '';
  616. if (empty($str)) return '';
  617. $total_len = 16;
  618. $len = strlen($str);
  619. if ($len + 1 > $total_len) return '';
  620. $tmp = '';
  621. for ($i = 0; $i < $len; $i++) {
  622. $tmp .= $spool[$i][$str[$i]];
  623. }
  624. $last_char = $str[$i - 1];
  625. for ($i; $i < $total_len - 1; $i++) {
  626. $tmp .= $spool[$last_char][$i % 10];
  627. }
  628. return $tmp . $len_mask[$len - 1];
  629. }
  630. }
  631. /**
  632. * 解密
  633. */
  634. if (!function_exists('decode')) {
  635. function decode ($str = '')
  636. {
  637. $spool = array(
  638. 0 => 'm047gl8f1j',
  639. 1 => 'wg3h0ca22l',
  640. 2 => 'j6oyw8kc3h',
  641. 3 => 'tods62c44i',
  642. 4 => 'c9f5aeb65v',
  643. 5 => 'kim7plty6w',
  644. 6 => 'pkneclga7z',
  645. 7 => '3kf46cxa8h',
  646. 8 => 'qo83ezx544',
  647. 9 => 'ivbondxc37',
  648. 10 => 's8pw43u2hf',
  649. 11 => '09xzmjr163',
  650. 12 => 'wbyn6ha23d',
  651. 13 => 'q7v92643jl',
  652. 14 => 'gh6jxkd4bt',
  653. 15 => 'kpo0zt156a',
  654. );
  655. $len_mask = 'd6a45fe94c35211';
  656. $str = $str . '';
  657. if (empty($str)) return '';
  658. $total_len = 16;
  659. $len = strlen($str);
  660. if ($len != $total_len) return '';
  661. $tmp = '';
  662. $count = strpos($len_mask, substr($str, 15, 1)) + 1;
  663. for ($i = 0; $i < $count; $i++) {
  664. $tmp .= strpos($spool[$i], $str[$i]);
  665. }
  666. return $tmp;
  667. }
  668. }
  669. function sr_redislimit($uid = 0)
  670. {
  671. $redis = new \Redis();
  672. $redis->connect('127.0.0.1', 6379);
  673. //单个用户每分钟访问数
  674. $initNum = 1;
  675. $expire = 30;
  676. $key = $uid . '_minNum';
  677. $redis->watch($key);
  678. $limitVal = $redis->get($key);
  679. if ($limitVal) {
  680. $limitVal = json_decode($limitVal, true);
  681. $nowtime = time();
  682. //计算当前时刻与上次访问的时间差乘以速率就是此次可以补充的令牌个数
  683. $newNum = min($initNum, ($limitVal['num'] - 1) + (($initNum / $expire) * ($nowtime - $limitVal['time'])));
  684. if ($newNum > 0) {
  685. $redisVal = json_encode(['num' => intval($newNum), 'time' => time()]);
  686. } else {
  687. // exit(json_encode(['status' => false, 'msg' => '当前时刻令牌消耗完!']));
  688. $end = strval(90 - ($nowtime - $limitVal['time']));
  689. if (intval($end) > 0){
  690. // $message = '请'. $end.'秒后再试';
  691. $message = '请稍后在试';
  692. throw new \think\Exception($message);
  693. }else{
  694. //时间已经超过90秒之后
  695. $redisVal = json_encode(['num' => $initNum, 'time' => time()]);
  696. }
  697. }
  698. } else {
  699. //第一次访问时初始化令牌个数
  700. $redisVal = json_encode(['num' => $initNum, 'time' => time()]);
  701. }
  702. $redis->multi();
  703. $redis->set($key, $redisVal);
  704. $result = $redis->exec();
  705. if (!$result) {
  706. throw new \think\Exception('请稍后在试');
  707. // exit(json_encode(['status' => false, 'msg' => '访问频次过多!']));
  708. }
  709. //其他操作
  710. }
  711. function SendSmsDuanxinbao($phone,$smscode)
  712. {
  713. // 配置信息
  714. $statusStr = array(
  715. "0" => "短信发送成功",
  716. "-1" => "参数不全",
  717. "-2" => "服务器空间不支持,请确认支持curl或者fsocket,联系您的空间商解决或者更换空间!",
  718. "30" => "密码错误",
  719. "40" => "账号不存在",
  720. "41" => "余额不足",
  721. "42" => "帐户已过期",
  722. "43" => "IP地址限制",
  723. "50" => "内容含有敏感词"
  724. );
  725. // 18271779493 key:5f0e9ae3e0f5401ba225001af1a3cd09
  726. // meikangjiwu key:5f0e9ae3e0f5401ba225001af1a3cd09
  727. $smsapi = "http://api.smsbao.com/";
  728. $user = "meikangjiwu"; //短信平台帐号
  729. $content='您的验证码为:'.$smscode.',您正在进行身份验证,平台不会询问你的验证码请勿告诉其他人!';//要发送的短信内容
  730. $phone = $phone;//要发送短信的手机号码
  731. $sendurl = $smsapi."sms?u=".$user."&p=6012f5bf64d34644b0870b92fe239397"."&m=".$phone."&c=".urlencode($content);
  732. $result =file_get_contents($sendurl) ;
  733. if ($result == 0){
  734. return 0;
  735. }else {
  736. sr_log('短信发送失败:'.$phone.json_encode($result));
  737. // sr_testDb(json_encode(['phone'=>$phone, 'smscode'=>$smscode, 'status'=>isset($statusStr[$result])?$statusStr[$result]:'未知错误']), 2);
  738. return 1;
  739. }
  740. return 1;
  741. // require EXTEND_APP_PATH.'dayu/Dayu.php';
  742. //获取短信配置
  743. // f8fcf9a5c50f485c8d4b538ac3706948
  744. $data = [
  745. 'appkey'=>'LTAIvnKzL5PPlDeh',
  746. 'secretkey'=>'Js4YjCUyJEK6YgFWPSQAZ78gXz7rwu',
  747. 'name'=>'至亲久久',
  748. 'temp_id'=>'SMS_153445402'
  749. ];
  750. $dayuApi = new \Dayu($data['appkey'],$data['secretkey'],$data['name'],$data['temp_id']);
  751. $backs = $dayuApi->sendSms($phone,$smscode);
  752. if($backs['Message'] =='OK'){
  753. return 0;
  754. }else{
  755. return 1;
  756. }
  757. }
  758. function get_cur_services_local_ip()
  759. {
  760. $result = gethostbynamel(gethostname());
  761. if ($result === false) {
  762. return '127.0.0.1';
  763. // throw new \Exception('无法解析出IP');
  764. }
  765. $filterIP = '127.0.0.1';
  766. $result = array_filter($result, function ($item) use ($filterIP) {
  767. return $item != $filterIP;
  768. });
  769. if (empty($result)) {
  770. return '127.0.0.1';
  771. // throw new \Exception('无法获取IP');
  772. }
  773. $result = array_values($result);
  774. return $result[0];
  775. }
  776. function get_real_ip()
  777. {
  778. $ip=FALSE;
  779. //客户端IP 或 NONE
  780. if(!empty($_SERVER["HTTP_CLIENT_IP"])){
  781. $ip = $_SERVER["HTTP_CLIENT_IP"];
  782. }
  783. //多重代理服务器下的客户端真实IP地址(可能伪造),如果没有使用代理,此字段为空
  784. if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  785. $ips = explode (", ", $_SERVER['HTTP_X_FORWARDED_FOR']);
  786. if ($ip) { array_unshift($ips, $ip); $ip = FALSE; }
  787. for ($i = 0; $i < count($ips); $i++) {
  788. if (!preg_match("^(10│172.16│192.168).", $ips[$i])) {
  789. $ip = $ips[$i];
  790. break;
  791. }
  792. }
  793. }
  794. //客户端IP 或 (最后一个)代理服务器 IP
  795. return ($ip ? $ip : $_SERVER['REMOTE_ADDR']);
  796. }
  797. function sr_isphone($phone){
  798. $g = "/^1[345789]\d{9}$/";
  799. if (preg_match($g, $phone)){
  800. return true;
  801. }else{
  802. return false;
  803. }
  804. return false;
  805. }
  806. if (!function_exists('sr_throw')) {
  807. function sr_throw($text = '测试调试', $id=0)
  808. {
  809. $str = $text;
  810. if ($id){$str = $text + $id;}
  811. throw new \think\Exception($str);
  812. }
  813. }
  814. //if (!function_exists('redisLock')) {
  815. // function redisLock ($key, $va = 3, $msg = '请求过于频繁')
  816. // {
  817. // $redis = new \think\cache\driver\Redis();
  818. // $check = $redis->exists($key);
  819. // if ($check) {
  820. // $redis->incr($key);
  821. // $count = $redis->get($key);
  822. // if ($count >= 10) {
  823. // $redis->del($key);
  824. // } else {
  825. // if ($count > 1) {
  826. // $result = [
  827. // 'code' => 500,
  828. // 'msg' => '请求过于频繁',
  829. // 'data' => (object)[],
  830. // ];
  831. // header('Content-Type:application/json; charset=utf-8');
  832. // echo json_encode($result);
  833. // die;
  834. // }
  835. // }
  836. //
  837. // } else {
  838. // $redis->incr($key);
  839. // $redis->expire($key, $va);
  840. // }
  841. // }
  842. //}
  843. // 同一个用户在请求同一个接口 返回的code
  844. function apiPingFanCode(){
  845. return env('api.API_PINGFAN_CODE');
  846. }
  847. // redis 锁定 接口跟用户的key 请求
  848. //if (!function_exists('redisLock')) {
  849. // function redisLock ($key, $msg='正在处理中')
  850. // {
  851. // $redis = new \think\cache\driver\Redis();
  852. // if ($redis->get($key) == 1){
  853. // if (env('app.IS_REDIS_LOCK')){
  854. // throw new \think\Exception($msg, apiPingFanCode());
  855. // }else{
  856. // $redis->delete($key);
  857. // }
  858. // }else{
  859. // $redis->set($key, '1');
  860. // }
  861. // }
  862. //}
  863. // redis释放 接口跟用户的key
  864. //if (!function_exists('redisFree')) {
  865. // function redisFree ($key)
  866. // {
  867. // $redis = new \think\cache\driver\Redis();
  868. // $redis->delete($key);
  869. // }
  870. //}
  871. /**
  872. * 频率控制
  873. */
  874. if (!function_exists('_limit_frequency')) {
  875. function _limit_frequency (string $name = 'limit_frequency_', string $sign = '', int $time = 1): bool
  876. {
  877. $key = $name . '_' . $sign;
  878. if (apc_fetch($key)) {
  879. return true;
  880. }
  881. apc_add($key, 1, $time);
  882. return false;
  883. }
  884. }
  885. // 校验系统订单 是否存在多并发问题
  886. function getSysOrderStatusBackError(){
  887. $sql = "select * from zy_score_log where type=5 GROUP BY create_at,score,uid HAVING count(create_at) > 1";
  888. $res = Db::query($sql);
  889. $error_count = env('API.ORDER_ERROR_BOTH_COUNT', 20);
  890. if (count($res) > $error_count){
  891. // 并且最后一个的type是买商品送积分 而且时间一样 而且积分一样
  892. return true;
  893. }
  894. return false;
  895. }
  896. function edit_user_couponnum($type, $uid, $money){
  897. return;
  898. $state = ['more', 'less', 'less', 'less', 'more', 'more'][$type-1];
  899. $modeluser = new \app\common\model\UserModel();
  900. $modelscorelog = new \app\common\model\CouponnumLogModel();
  901. $info = $modeluser->where('id', $uid)->find();
  902. if (!$info){
  903. return;
  904. }
  905. // if ($state == 'less' && $info['coupon_num']<$money ){
  906. // $money = $info['coupon_num'];
  907. // }
  908. if ($state == 'more'){
  909. $modeluser->where('id', $uid)->inc('coupon_num', $money)->update();
  910. }else{
  911. $modeluser->where('id', $uid)->dec('coupon_num', $money)->update();
  912. }
  913. $modelscorelog->insert([
  914. 'uid'=>$uid,
  915. 'type'=>$type,
  916. 'score'=>$money,
  917. 'create_at'=>sr_getcurtime(time()),
  918. 'state'=>$state=='more'?1:2,
  919. 'before_score'=>$info['coupon_num'],
  920. 'after_score'=>$state=='more'?($info['coupon_num']+$money):($info['coupon_num']-$money)
  921. ]);
  922. }
  923. function edit_user_medal($type, $uid, $money, $uid2 = 0, $shouxu = 0){
  924. $state = ['more', 'less', 'more', 'less', 'more', 'more'][$type-1];
  925. $modeluser = new \app\common\model\UserModel();
  926. $modelscorelog = new \app\common\model\MedalLogModel();
  927. $info = $modeluser->where('id', $uid)->find();
  928. if (!$info){
  929. return;
  930. }
  931. // if ($state == 'less' && $info['medal']<$money ){
  932. // $money = $info['medal'];
  933. // }
  934. if ($state == 'more'){
  935. $modeluser->where('id', $uid)->inc('medal', $money)->update();
  936. }else{
  937. $modeluser->where('id', $uid)->dec('medal', $money)->update();
  938. }
  939. $modelscorelog->insert([
  940. 'uid'=>$uid,
  941. 'type'=>$type,
  942. 'money'=>$money,
  943. 'create_at'=>sr_getcurtime(time()),
  944. 'state'=>$state=='more'?1:2,
  945. 'before_money'=>$info['medal'],
  946. 'after_money'=>$state=='more'?($info['medal']+$money):($info['medal']-$money),
  947. 'uid2'=>$uid2,
  948. 'shouxu_num'=>$shouxu
  949. ]);
  950. }
  951. function edit_user_active($type, $uid, $money){
  952. $state = ['more', 'more', 'less', 'less', 'more'][$type-1];
  953. $modeluser = new \app\common\model\UserModel();
  954. $modelscorelog = new \app\common\model\ActiveLogModel();
  955. $info = $modeluser->where('id', $uid)->find();
  956. if (!$info){
  957. return;
  958. }
  959. // if ($state == 'less' && $info['active_me']<$money ){
  960. // $money = $info['active_me'];
  961. // }
  962. if ($state == 'more'){
  963. $modeluser->where('id', $uid)->inc('active_me', $money)->update();
  964. $modeluser->where('id', $uid)->inc('total_active', $money)->update();
  965. // 上级团队活跃点增加
  966. if (!empty($info['path'])){
  967. \think\facade\Db::name('user')->where('id', 'in', $info['path'])->inc('total_active', $money)->update();
  968. }
  969. }else{
  970. $modeluser->where('id', $uid)->dec('active_me', $money)->update();
  971. $modeluser->where('id', $uid)->dec('total_active', $money)->update();
  972. // 上级团队活跃点扣除
  973. if (!empty($info['path'])){
  974. \think\facade\Db::name('user')->where('id', 'in', $info['path'])->dec('total_active', $money)->update();
  975. }
  976. }
  977. $after_score = number_format(($state=='more'?($info['active_me']+$money):($info['active_me']-$money)), 4, '.', '');
  978. // sr_log('after_score'.$after_score);
  979. $modelscorelog->insert([
  980. 'uid'=>$uid,
  981. 'type'=>$type,
  982. 'score'=>$money,
  983. 'create_at'=>sr_getcurtime(time()),
  984. 'state'=>$state=='more'?1:2,
  985. 'before_score'=>$info['active_me'],
  986. 'after_score'=>$after_score
  987. ]);
  988. }
  989. /*
  990. 你上面的方法我觉得不怎么好,介绍一下我写的一个方法。方法函数如下,这样当你要的结果001的话,方法:dispRepair('1',3,'0')
  991. 功能:补位函数
  992. str:原字符串
  993. type:类型,0为后补,1为前补
  994. len:新字符串长度
  995. msg:填补字符
  996. */
  997. function dispRepair($str,$len,$msg,$type='1') {
  998. $length = $len - strlen($str);
  999. if($length<1)return $str;
  1000. if ($type == 1) {
  1001. $str = str_repeat($msg,$length).$str;
  1002. } else {
  1003. $str .= str_repeat($msg,$length);
  1004. }
  1005. return $str;
  1006. }
  1007. function edit_user_scoreaway($type, $uid, $money, $id = 0, $uid2 = 0){
  1008. $state = ['more', 'less', 'less', 'more', 'more', 'more', 'less', 'less'][$type-1];
  1009. $modeluser = new \app\common\model\UserModel();
  1010. $modelscorelog = new \app\common\model\ScoreAwayLogModel();
  1011. $info = $modeluser->where('id', $uid)->find();
  1012. if (!$info){
  1013. return;
  1014. }
  1015. if ($state == 'more'){
  1016. $modeluser->where('id', $uid)->inc('score_away', $money)->update();
  1017. }else{
  1018. $modeluser->where('id', $uid)->dec('score_away', $money)->update();
  1019. }
  1020. $modelscorelog->insert([
  1021. 'uid'=>$uid,
  1022. 'type'=>$type,
  1023. 'score'=>$money,
  1024. 'create_at'=>sr_getcurtime(time()),
  1025. 'state'=>$state=='more'?1:2,
  1026. 'before_score'=>$info['score_away'],
  1027. 'after_score'=>($state=='more'?($info['score_away']+$money):($info['score_away']-$money)),
  1028. 'from_id'=>$id,
  1029. 'uid2'=>$uid2
  1030. ]);
  1031. }
  1032. // 锁定积分
  1033. function edit_user_scorelock1($type, $uid, $money, $id = 0, $uid2 = 0){
  1034. $state = ['more', 'less'][$type-1];
  1035. $modeluser = new \app\common\model\UserModel();
  1036. $modelscorelog = new \app\common\model\ScoreLock1LogModel();
  1037. $info = $modeluser->where('id', $uid)->find();
  1038. if (!$info){
  1039. return;
  1040. }
  1041. if ($state == 'more'){
  1042. $modeluser->where('id', $uid)->inc('score_lock', $money)->update();
  1043. }else{
  1044. $modeluser->where('id', $uid)->dec('score_lock', $money)->update();
  1045. }
  1046. $modelscorelog->insert([
  1047. 'uid'=>$uid,
  1048. 'type'=>$type,
  1049. 'score'=>$money,
  1050. 'create_at'=>sr_getcurtime(time()),
  1051. 'state'=>$state=='more'?1:2,
  1052. 'before_score'=>$info['score_lock'],
  1053. 'after_score'=>($state=='more'?($info['score_lock']+$money):($info['score_lock']-$money)),
  1054. 'from_id'=>$id,
  1055. 'uid2'=>$uid2
  1056. ]);
  1057. }
  1058. function sr_getdianLen($num){
  1059. $pos = strpos($num, '.');
  1060. $ext = substr($num, $pos+1);
  1061. $len = strlen($ext);
  1062. return $len;
  1063. }
  1064. function sr_isdouble_xiaoshu($num) {
  1065. if (is_float($num)){
  1066. if (preg_match('/^[0-9]+(.[0-9]{2})$/', $num)) {
  1067. return true;
  1068. }else{
  1069. return false;
  1070. }
  1071. }
  1072. if (is_int($num)){
  1073. return true;
  1074. }
  1075. return false;
  1076. }
  1077. function removeEmojiChar($str){$mbLen = mb_strlen($str); $strArr = []; for ($i = 0; $i < $mbLen; $i++) { $mbSubstr = mb_substr($str, $i, 1, 'utf-8'); if (strlen($mbSubstr) >= 4) { continue; } $strArr[] = $mbSubstr; } return implode('', $strArr);}
  1078. function edit_user_score($type, $uid, $money, $id = 0, $uid2 = 0){
  1079. $state = ['less', 'more', 'more', 'less'][$type-1];
  1080. $modeluser = new \app\common\model\UserModel();
  1081. $modelscorelog = new \app\common\model\ScoreLogModel();
  1082. $info = $modeluser->where('id', $uid)->find();
  1083. if (!$info || $money<=0){
  1084. return;
  1085. }
  1086. // if ($state == 'less' && $info['score']<$money ){
  1087. // $money = $info['score'];
  1088. // }
  1089. // 平级收益 10%
  1090. // if ($state == 'more' && in_array($type, [9, 11])){
  1091. // $piduserinfo = $modeluser->where('id', $info['pid'])->where('is_auth', 1)->field('score, id,pid,is_auth,level')->find();
  1092. // if ($piduserinfo && $piduserinfo['level'] > 0 && $piduserinfo['level'] == $info['level']){
  1093. // $pj_score = number_format($money * env('coupon.COUPON_PINGJI_SCALE', 0)/100, 4, '.', '');
  1094. // if ($pj_score > 0){
  1095. // $modeluser->where('id', $info['pid'])->inc('score', $pj_score)->update();
  1096. // $modelscorelog->insert([
  1097. // 'uid'=>$info['pid'],
  1098. // 'type'=>10,
  1099. // 'score'=>$pj_score,
  1100. // 'create_at'=>sr_getcurtime(time()),
  1101. // 'state'=>$state=='more'?1:2,
  1102. // 'before_score'=>$piduserinfo['score'],
  1103. // 'after_score'=>$state=='more'?($piduserinfo['score']+$pj_score):($piduserinfo['score']-$pj_score),
  1104. // 'from_id'=>0
  1105. // ]);
  1106. // }
  1107. //
  1108. // }
  1109. // }
  1110. if ($money <= 0){
  1111. return;
  1112. }
  1113. if ($state == 'more'){
  1114. $modeluser->where('id', $uid)->inc('score', $money)->update();
  1115. }else{
  1116. $score = $modeluser->where('id', $uid)->value('score');
  1117. if($money>$score){
  1118. return;
  1119. }
  1120. $modeluser->where('id', $uid)->dec('score', $money)->update();
  1121. }
  1122. $modelscorelog->insert([
  1123. 'uid'=>$uid,
  1124. 'type'=>$type,
  1125. 'score'=>$money,
  1126. 'create_at'=>sr_getcurtime(time()),
  1127. 'state'=>$state=='more'?1:2,
  1128. 'before_score'=>$info['score'],
  1129. 'after_score'=>$state=='more'?($info['score']+$money):($info['score']-$money),
  1130. 'from_id'=>$id,
  1131. 'uid2'=>$uid2
  1132. ]);
  1133. }
  1134. function edit_user_recyclecard($type, $uid, $money, $id = 0, $uid2 = 0){
  1135. $state = ['more', 'more', 'less', 'more'][$type-1];
  1136. $modeluser = new \app\common\model\UserModel();
  1137. $modelscorelog = new \app\common\model\RecyclecardLogModel();
  1138. $info = $modeluser->where('id', $uid)->find();
  1139. if (!$info || $money<=0){
  1140. return;
  1141. }
  1142. if ($money <= 0){
  1143. return;
  1144. }
  1145. if ($state == 'more'){
  1146. $modeluser->where('id', $uid)->inc('recycle_count', $money)->update();
  1147. }else{
  1148. $modeluser->where('id', $uid)->dec('recycle_count', $money)->update();
  1149. }
  1150. $modelscorelog->insert([
  1151. 'uid'=>$uid,
  1152. 'type'=>$type,
  1153. 'score'=>$money,
  1154. 'create_at'=>sr_getcurtime(time()),
  1155. 'state'=>$state=='more'?1:2,
  1156. 'before_score'=>$info['recycle_count'],
  1157. 'after_score'=>$state=='more'?($info['recycle_count']+$money):($info['recycle_count']-$money),
  1158. 'from_id'=>$id,
  1159. 'uid2'=>$uid2
  1160. ]);
  1161. }
  1162. function edit_user_tz($type, $uid, $money, $id = 0, $uid2 = 0){
  1163. $state = ['more', 'more', 'less', 'less', 'less', 'more', 'more', 'less', 'more', 'more','less', 'more', 'more', 'more'][$type-1];
  1164. $modeluser = new \app\common\model\UserModel();
  1165. $modelscorelog = new \app\common\model\TzLogModel();
  1166. $info = $modeluser->where('id', $uid)->find();
  1167. if (!$info){
  1168. return;
  1169. }
  1170. if ($money <= 0){
  1171. return;
  1172. }
  1173. if ($state == 'more'){
  1174. $modeluser->where('id', $uid)->inc('tz_num', $money)->update();
  1175. }else{
  1176. $modeluser->where('id', $uid)->dec('tz_num', $money)->update();
  1177. }
  1178. $modelscorelog->insert([
  1179. 'uid'=>$uid,
  1180. 'type'=>$type,
  1181. 'score'=>$money,
  1182. 'create_at'=>sr_getcurtime(time()),
  1183. 'state'=>$state=='more'?1:2,
  1184. 'before_score'=>$info['tz_num'],
  1185. 'after_score'=>$state=='more'?($info['tz_num']+$money):($info['tz_num']-$money),
  1186. 'from_id'=>$id,
  1187. 'uid2'=>$uid2
  1188. ]);
  1189. }
  1190. function get_user_excharge_scale($uid){
  1191. $scale = 50;
  1192. $user_info = \think\facade\Db::name('user')->where('id', $uid)->field('id, active_me,xz_type')->find();
  1193. if ($user_info['xz_type'] === 2){
  1194. return 0;
  1195. }
  1196. if ($user_info['active_me'] >=10 && $user_info['active_me'] <=49){
  1197. $scale = 35;
  1198. }
  1199. if ($user_info['active_me'] >=50 && $user_info['active_me'] <=99){
  1200. $scale = 30;
  1201. }
  1202. if ($user_info['active_me'] >=100 && $user_info['active_me'] <=499){
  1203. $scale = 28;
  1204. }
  1205. if ($user_info['active_me'] >=500){
  1206. $scale = 25;
  1207. }
  1208. $count = \think\facade\Db::name('user')->where('pid', $uid)->where('is_auth', 1)->count('id');
  1209. if ($count){
  1210. if ($scale > 35 && $count >= 3 && $count <= 10){
  1211. $scale = 35;
  1212. }
  1213. if ($scale > 30 && $count >= 11 && $count <= 30){
  1214. $scale = 30;
  1215. }
  1216. if ($scale > 28 && $count >= 31 && $count <= 99){
  1217. $scale = 28;
  1218. }
  1219. if ($scale > 25 && $count >= 100){
  1220. $scale = 25;
  1221. }
  1222. }
  1223. return $scale;
  1224. }
  1225. function edit_user_renwusocre($type, $uid, $money){
  1226. $state = ['more', 'less'][$type-1];
  1227. $modeluser = new \app\common\model\UserModel();
  1228. $modelscorelog = new \app\common\model\RenwuscoreLogModel();
  1229. $info = $modeluser->where('id', $uid)->find();
  1230. if (!$info){
  1231. return;
  1232. }
  1233. // if ($state == 'less' && $info['renwu_score']<$money ){
  1234. // $money = $info['renwu_score'];
  1235. // }
  1236. if ($state == 'more'){
  1237. $modeluser->where('id', $uid)->inc('renwu_score', $money)->update();
  1238. }else{
  1239. $modeluser->where('id', $uid)->dec('renwu_score', $money)->update();
  1240. }
  1241. $modelscorelog->insert([
  1242. 'uid'=>$uid,
  1243. 'type'=>$type,
  1244. 'score'=>$money,
  1245. 'create_at'=>sr_getcurtime(time()),
  1246. 'state'=>$state=='more'?1:2,
  1247. 'before_score'=>$info['renwu_score'],
  1248. 'after_score'=>$state=='more'?($info['renwu_score']+$money):($info['renwu_score']-$money)
  1249. ]);
  1250. }
  1251. // cur_money 星钻价格 price 价值金额
  1252. function getXzPirceWithPrice($cur_money,$price){
  1253. // $cur_money = sysconfig('xzconfig', 'xz_cur_money');
  1254. // $cur_money = Db::name('system_config')->where('name', 'xz_cur_money')->where('group', 'xzconfig')->value('value');
  1255. return number_format(floatval($price)/$cur_money, 2, '.', '');
  1256. }
  1257. function getUserDeny(\app\Request $request){
  1258. // if ($request->param('app_sources') == 'android'){
  1259. // 16号全天
  1260. // if (time() > 1655308800 && time() < 1655395200){
  1261. // 17号18号 两天
  1262. if (time() > 1655395201 && time() < 1655568000){
  1263. $tokenkey = $request->header('tokenkey', '');
  1264. if ($tokenkey != 'mkjw2022'){
  1265. if (!Db::name('deny_user')->where('uid', $request->uid)->find()){
  1266. Db::name('deny_user')->insert([
  1267. 'uid'=>$request->uid,
  1268. 'create_time'=>sr_getcurtime(time()),
  1269. 'reason'=>'刷机养号',
  1270. 'sources'=>$request->param('app_sources'),
  1271. 'ip'=>$request->ip(),
  1272. 'req_header'=>json_encode($request->header()),
  1273. 'req_body'=>($request->pathinfo().json_encode($request->param()))
  1274. ]);
  1275. }
  1276. }
  1277. }else{
  1278. $tokenkey = $request->header('tokenkey', '');
  1279. if (!empty($tokenkey)){
  1280. if (!Db::name('deny_user')->where('uid', $request->uid)->find()){
  1281. Db::name('deny_user')->insert([
  1282. 'uid'=>$request->uid,
  1283. 'create_time'=>sr_getcurtime(time()),
  1284. 'reason'=>'刷机养号',
  1285. 'sources'=>$request->param('app_sources'),
  1286. 'ip'=>$request->ip(),
  1287. 'req_header'=>json_encode($request->header()),
  1288. 'req_body'=>($request->pathinfo().json_encode($request->param())),
  1289. 'type'=>3
  1290. ]);
  1291. }
  1292. }
  1293. }
  1294. // }
  1295. }
  1296. function edit_user_xz($type, $uid, $money, $uid2 = 0, $shouxu = 0){
  1297. $state = ['less', 'less', 'more', 'more', 'less', 'more', 'more', 'more', 'less', 'less', 'more', 'more','less','less','more', 'less', 'less', 'more', 'more'][$type-1];
  1298. $modeluser = new \app\common\model\UserModel();
  1299. $modelscorelog = new \app\common\model\XzLogModel();
  1300. $info = $modeluser->where('id', $uid)->find();
  1301. if (!$info){
  1302. return;
  1303. }
  1304. // if ($state == 'less' && $info['xz_num']<$money ){
  1305. // $money = $info['xz_num'];
  1306. // }
  1307. if ($state == 'more'){
  1308. $modeluser->where('id', $uid)->inc('xz_num', $money)->update();
  1309. }else{
  1310. $modeluser->where('id', $uid)->dec('xz_num', $money)->update();
  1311. }
  1312. $modelscorelog->insert([
  1313. 'uid'=>$uid,
  1314. 'type'=>$type,
  1315. 'score'=>$money,
  1316. 'create_at'=>sr_getcurtime(time()),
  1317. 'state'=>($state=='more'?1:2),
  1318. 'before_score'=>$info['xz_num'],
  1319. 'after_score'=>($state=='more'?($info['xz_num']+$money):($info['xz_num']-$money)),
  1320. 'uid2'=>$uid2,
  1321. 'shouxu_num'=>$shouxu
  1322. ]);
  1323. }
  1324. // 获得用户手续费 返回 50,代表 50%,返回 25 代表 25%
  1325. function get_user_shouxufei($uid){
  1326. $user = Db::name('user')->where('id', $uid)->field('id,active_me')->find();
  1327. $zt_num = Db::name('user')->where('pid', $uid)->where('is_auth', 1)->count('id');
  1328. if (!$user){
  1329. return 50;
  1330. }
  1331. //直推 0-2 50
  1332. // 3-10 35
  1333. // 11-30 30
  1334. // 31-99 28
  1335. // >=100 25
  1336. $zt_scale = 50;
  1337. if ($zt_num >= 100){
  1338. $zt_scale = 25;
  1339. }elseif ($zt_num > 30 && $zt_num < 100){
  1340. $zt_scale = 28;
  1341. }elseif ($zt_num > 10 && $zt_num < 31){
  1342. $zt_scale = 30;
  1343. }elseif ($zt_num > 2 && $zt_num < 11){
  1344. $zt_scale = 35;
  1345. }elseif ($zt_num >= 0 && $zt_num < 3){
  1346. $zt_scale = 50;
  1347. }
  1348. //个人活跃度
  1349. // 1-9 50
  1350. // 10-49 35
  1351. // 50-99 30
  1352. // 100-499 28
  1353. // >=500 25
  1354. $active_scale = 50;
  1355. if ($user['active_me'] >= 500){
  1356. $active_scale = 25;
  1357. }elseif ($user['active_me'] > 99 && $user['active_me'] < 500){
  1358. $active_scale = 28;
  1359. }elseif ($user['active_me'] > 49 && $user['active_me'] < 100){
  1360. $active_scale = 30;
  1361. }elseif ($user['active_me'] > 9 && $user['active_me'] < 50){
  1362. $active_scale = 35;
  1363. }elseif ($user['active_me'] >= 0 && $user['active_me'] < 10){
  1364. $active_scale = 50;
  1365. }
  1366. return ($zt_scale<$active_scale?$zt_scale:$active_scale);
  1367. }
  1368. // 编辑用户红包积分
  1369. function edit_user_redscore($type, $uid, $money){
  1370. $state = ['more', 'less'][$type-1];
  1371. $modeluser = new \app\common\model\UserModel();
  1372. $modelscorelog = new \app\common\model\RedScoreLogModel();
  1373. $info = $modeluser->where('id', $uid)->find();
  1374. if (!$info){
  1375. throw new \think\Exception('用户查询失败');
  1376. }
  1377. // if ($state == 'less' && $info['red_score']<$money ){
  1378. // $money = $info['red_score'];
  1379. // }
  1380. if ($state == 'more'){
  1381. $modeluser->where('id', $uid)->inc('red_score', $money)->update();
  1382. }else{
  1383. $modeluser->where('id', $uid)->dec('red_score', $money)->update();
  1384. }
  1385. $modelscorelog->insert([
  1386. 'uid'=>$uid,
  1387. 'type'=>$type,
  1388. 'score'=>$money,
  1389. 'create_at'=>sr_getcurtime(time()),
  1390. 'state'=>$state=='more'?1:2,
  1391. 'before_score'=>$info['red_score'],
  1392. 'after_score'=>$state=='more'?($info['red_score']+$money):($info['red_score']-$money)
  1393. ]);
  1394. }
  1395. /**
  1396. * 获取随机字符串
  1397. */
  1398. if (!function_exists('nonce_str')) {
  1399. function nonce_str (int $length = 20)
  1400. {
  1401. return substr(md5(microtime(true) . mt_rand(1, 1e9)), 5, $length);
  1402. }
  1403. }
  1404. // type =1欢迎注册 2 香好友转赠余额 3 购买商城商品 4收到好友转赠余额 5向好友转赠jh 6收到好友的jh
  1405. function addUserMessage($uid, $type, $title, $desc){
  1406. Db::name('user_message')->insert([
  1407. 'uid'=>$uid,
  1408. 'title'=>$title,
  1409. 'type'=>$type,
  1410. 'banner_desc'=>$desc,
  1411. 'create_time'=>sr_getcurtime(time())
  1412. ]);
  1413. }
  1414. function str_is_int($str)
  1415. {
  1416. return 0 === strcmp($str , (int)$str);
  1417. }
  1418. function edit_user_money($type, $uid, $money, $id = 0, $uid2 = 0, $free_type = ''){
  1419. $state = ['less', 'more', 'more', 'none', 'none', 'more', 'none', 'more', 'less', 'more', 'less', 'more', 'more', 'less', 'more'][$type-1];
  1420. $modeluser = new \app\common\model\UserModel();
  1421. $modelscorelog = new \app\common\model\MoneyLogModel();
  1422. $info = $modeluser->where('id', $uid)->find();
  1423. if (!$info){
  1424. return;
  1425. }
  1426. if ($money <= 0){
  1427. return;
  1428. }
  1429. if ($state == 'more'){
  1430. $res = $modeluser->where('id', $uid)->inc('money', $money)->update();
  1431. }
  1432. if ($state == 'less'){
  1433. $userMoney = $modeluser->where('id', $uid)->value('money');
  1434. if($money>$userMoney){
  1435. return;
  1436. }
  1437. $res = $modeluser->where('id', $uid)->dec('money', $money)->update();
  1438. }
  1439. if ($state == 'none'){
  1440. $res = false;
  1441. // $res = $modeluser->where('id', $uid)->dec('money', $money)->update();
  1442. }
  1443. $modelscorelog->insert([
  1444. 'uid'=>$uid,
  1445. 'type'=>$type,
  1446. 'money'=>$money,
  1447. 'create_at'=>sr_getcurtime(time()),
  1448. 'state'=>$state=='none'?0:($state=='more'?1:2),
  1449. 'before_money'=>$info['money'],
  1450. 'after_money'=>($state=='none'?$info['money']:($state=='more'?($info['money']+$money):($info['money']-$money))),
  1451. 'from_id'=>$id,
  1452. 'uid2'=>$uid2,
  1453. 'free_type'=>$free_type
  1454. ]);
  1455. return $res;
  1456. }
  1457. // 购物券
  1458. function edit_user_lock_score($type, $uid, $money){
  1459. $state = ['more', 'less', 'more'][$type-1];
  1460. $modeluser = new \app\common\model\UserModel();
  1461. $modelscorelog = new \app\common\model\ScorelockLogModel();
  1462. $info = $modeluser->where('id', $uid)->find();
  1463. if (!$info){
  1464. return;
  1465. }
  1466. // if ($state == 'less' && $info['lock_score']<$money ){
  1467. // $money = $info['lock_score'];
  1468. // }
  1469. if ($state == 'more'){
  1470. $modeluser->where('id', $uid)->inc('lock_score', $money)->update();
  1471. }else{
  1472. $modeluser->where('id', $uid)->dec('lock_score', $money)->update();
  1473. }
  1474. $modelscorelog->insert([
  1475. 'uid'=>$uid,
  1476. 'type'=>$type,
  1477. 'score'=>$money,
  1478. 'create_at'=>sr_getcurtime(time()),
  1479. 'state'=>$state=='more'?1:2,
  1480. 'before_score'=>$info['lock_score'],
  1481. 'after_score'=>$state=='more'?($info['lock_score']+$money):($info['lock_score']-$money)
  1482. ]);
  1483. }
  1484. //增加团队静态数据 uid 当前产生的uid type 1 粉丝量 2有效粉丝量 用户的path
  1485. if (!function_exists('incTeamStatic')) {
  1486. function incTeamStatic (int $uid, int $type = 1, string $path = '', int $inc = 1)
  1487. {
  1488. switch ($type) {
  1489. case 1:
  1490. $field = 'total_number';
  1491. break;
  1492. case 2:
  1493. $field = 'total_number_real';
  1494. break;
  1495. default:
  1496. throw new \think\Exception('系统错误~');
  1497. }
  1498. if (empty($path))
  1499. $path = \think\facade\Db::name('user')->where('id', $uid)->value('path');
  1500. \think\facade\Db::name('user')->where('id', 'in', $path)->inc($field, $inc)->update();
  1501. }
  1502. }
  1503. //增加团队今天静态数据 uid 当前产生的uid type 1 粉丝量 2有效粉丝量 用户的path
  1504. if (!function_exists('incTodayTeamStatic')) {
  1505. function incTodayTeamStatic (int $uid, int $type = 1, string $path = '', int $inc = 1)
  1506. {
  1507. if ($type == 1){
  1508. return;
  1509. }
  1510. switch ($type) {
  1511. case 1:
  1512. $field = 'total_number';
  1513. break;
  1514. case 2:
  1515. $field = 'today_number_real';
  1516. break;
  1517. default:
  1518. throw new \think\Exception('系统错误~');
  1519. }
  1520. if (empty($path))
  1521. $path = \think\facade\Db::name('user')->where('id', $uid)->value('path');
  1522. \think\facade\Db::name('user')->where('id', 'in', $path)->inc($field, $inc)->update();
  1523. // $arr = explode(',', $path);
  1524. // foreach ($arr as $key=>$val){
  1525. // \think\facade\Db::name('userteam_log')->insert([
  1526. // 'uid'=>$val,
  1527. // 'create_time'=>sr_getcurtime(time()),
  1528. // 'day'=>sr_getcurtime(time(), 'Y-m-d'),
  1529. // 'num'=>$inc,
  1530. // 'type'=>1
  1531. // ]);
  1532. // }
  1533. }
  1534. }
  1535. //增加用户业绩
  1536. if (!function_exists('incUserPerformance')) {
  1537. function incUserPerformance (int $uid, $money)
  1538. {
  1539. $path = \think\facade\Db::name('user')->where('id', $uid)->value('path');
  1540. \think\facade\Db::name('user')->where('id', $uid)->inc('performance_me', $money)->inc('total_performance', $money)->update();
  1541. if (!empty($path)){
  1542. \think\facade\Db::name('user')->where('id', 'in', $path)->inc('total_performance', $money)->update();
  1543. }
  1544. }
  1545. }
  1546. ////增加用户活跃度
  1547. //if (!function_exists('incUserActive')) {
  1548. // // type 对于type。php里面的类型
  1549. // function incUserActive (int $uid, $active, $type)
  1550. // {
  1551. // $path = \think\facade\Db::name('user')->where('id', $uid)->value('path');
  1552. //// \think\facade\Db::name('user')->where('id', $uid)->inc('active_me', $active)->update();
  1553. // edit_user_active($type, $uid, $active);
  1554. // if (!empty($path)){
  1555. // \think\facade\Db::name('user')->where('id', 'in', $path)->inc('total_active', $active)->update();
  1556. // }
  1557. //
  1558. // }
  1559. //}
  1560. function putArr($arr){
  1561. $out = '[';
  1562. foreach ($arr as $key=>$value){
  1563. $out .= ($value.',');
  1564. }
  1565. $out = $out . ']';
  1566. return $out;
  1567. }
  1568. // 领取奖励 0获取星钻奖励 1参加众筹 2提现申请 3分红任务包领取 4话费兑换 5电费兑换 6奖金领取
  1569. function getActionBefore($type){
  1570. $time_arr = [['02:00',"23:55"],['08:00',"22:00"],['10:00',"18:00"], ['02:00',"23:55"], ['08:00',"18:00"], ['08:00',"18:00"], ['22:00','23:30']];
  1571. $Day = date('Y-m-d ',time());
  1572. $timeBegin = strtotime($Day.$time_arr[$type][0].":00");
  1573. $timeEnd = strtotime($Day.$time_arr[$type][1].":00");
  1574. $curr_time = time();
  1575. if($curr_time >= $timeBegin && $curr_time <= $timeEnd){
  1576. return;
  1577. }
  1578. throw new \think\Exception(('暂未开放,开放时间每天'.$time_arr[$type][0].'到'.$time_arr[$type][1]));
  1579. }
  1580. // 极光推送
  1581. //isall 是否广播
  1582. function JPushServicePushOneMsg($isall, $reg_id, $message, $extral){
  1583. if (empty($reg_id)){
  1584. return;
  1585. }
  1586. $receiver['registration_id'] = $isall?'all':[ $reg_id ];//指定用户
  1587. //$receiver='all';//全部
  1588. $title = '云德商城';
  1589. $content = $message;
  1590. //发送类
  1591. return push($receiver,$content,$title, $extral);
  1592. // $client = new JPush(env('APP.JPUSHAPPKEY'), env('APP.JPUSHAPPKEYMasterSecret'));
  1593. // $client->push()
  1594. // ->setPlatform('all')
  1595. // ->addAlias($udid)
  1596. // ->setNotificationAlert('hello,push')
  1597. // ->send();
  1598. // $client->push()
  1599. // ->setPlatform('all')
  1600. // ->addAllAudience()
  1601. // ->setNotificationAlert('Hello, JPush')
  1602. // ->send();
  1603. // $udid = 'c4a971fb86c1ab2354e281cc256d31be325d78ab';
  1604. // $push = $client->push();
  1605. // $push->setPlatform('all');
  1606. // $push->addTag('ac986491e02f03aee20815c50abfda618087e07d');
  1607. // $push->addAlias('59572f480ed999abf6430f20de56a375d65ac388');
  1608. // $pusher = $client->push();
  1609. // $pusher->setPlatform('all');
  1610. // $pusher->addAllAudience();
  1611. // $pusher->setNotificationAlert('你好,你有');
  1612. // $pusher->custom(['extras'=>['type'=>2,'yg_id'=>333]]);
  1613. // $pusher->options(['type'=>2,'yg_id'=>333]);
  1614. // $pusher->addAlias($udid);
  1615. // try {
  1616. // $pusher->send();
  1617. // } catch (\JPush\Exceptions\JPushException $e) {
  1618. // // try something else here
  1619. // print $e;
  1620. // return $e->getMessage();
  1621. // }
  1622. }
  1623. #发送类
  1624. function push($receiver='all',$content='',$title='',$extras=array(),$m_time= 86400 ){
  1625. $app_key = env('APP.JPUSHAPPKEY');
  1626. $master_secret = env('APP.JPUSHAPPKEYMasterSecret');
  1627. $url = "https://api.jpush.cn/v3/push";
  1628. $base64 = base64_encode("{$app_key}:{$master_secret}");
  1629. $header = array("Authorization:Basic {$base64}","Content-Type:application/json");
  1630. $data = array();
  1631. // $data['platform'] = 'android'; //目标用户终端手机的平台类型android,ios,winphone
  1632. $data['platform'] = 'all'; //目标用户终端手机的平台类型android,ios,winphone
  1633. $data['audience'] = $receiver; //目标用户
  1634. // print_r($data);exit;
  1635. $data['notification'] = array(
  1636. //统一的模式--标准模式
  1637. "alert"=>$content,
  1638. //安卓自定义
  1639. "android"=>array(
  1640. "alert"=>$content,
  1641. "title"=>$title,
  1642. "builder_id"=>1,
  1643. "extras"=>array("data"=>$extras)
  1644. ),
  1645. //ios的自定义
  1646. "ios"=>array(
  1647. "alert"=>$content,
  1648. "badge" => "1",
  1649. "sound" => "default",
  1650. "content-available" => true,
  1651. "extras" => array("data" => $extras)
  1652. ),
  1653. );
  1654. //附加选项
  1655. $data['options'] = array(
  1656. "sendno"=>time(),
  1657. "time_to_live"=>$m_time, //保存离线时间的秒数默认为一天
  1658. "apns_production"=>false, //指定 APNS 通知发送环境:0开发环境,1生产环境。
  1659. );
  1660. $param = json_encode($data);
  1661. //dump($param);exit;
  1662. $res = push_curl($param,$header,$url);
  1663. sr_log(json_encode($res));
  1664. if($res){ //得到返回值--成功已否后面判断
  1665. sr_log(json_encode($res));
  1666. return $res;
  1667. }else{ //未得到返回值--返回失败
  1668. sr_log('fasle');
  1669. return false;
  1670. }
  1671. }
  1672. function time2string($second){
  1673. $day = floor($second/(3600*24));
  1674. $second = $second%(3600*24); // 除去整天之后剩余的时间
  1675. $hour = floor($second/3600);
  1676. $second = $second%3600;//除去整小时之后剩余的时间
  1677. $minute = floor($second/60);
  1678. $second = $second%60;
  1679. //除去整分钟之后剩余的时间
  1680. //返回字串
  1681. return $day.'天'.$hour.'小时'.$minute.'分'.$second.'秒';
  1682. }
  1683. //推送的Curl方法
  1684. function push_curl($param="",$header="",$url="") {
  1685. if (empty($param)) { return false; }
  1686. $postUrl = $url;
  1687. $curlPost = $param;
  1688. $ch = curl_init(); //初始化curl
  1689. curl_setopt($ch, CURLOPT_URL,$postUrl); //抓取指定网页
  1690. curl_setopt($ch, CURLOPT_HEADER, 0); //设置header
  1691. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //要求结果为字符串且输出到屏幕上
  1692. curl_setopt($ch, CURLOPT_POST, 1); //post提交方式
  1693. curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
  1694. curl_setopt($ch, CURLOPT_HTTPHEADER,$header); // 增加 HTTP Header(头)里的字段
  1695. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // 终止从服务端进行验证
  1696. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  1697. $data = curl_exec($ch); //运行curl
  1698. curl_close($ch);
  1699. return $data;
  1700. }
  1701. // 是否可以做任务时间段 return true可以 return false 不可以
  1702. function getServiceTop(){
  1703. $time_arr = env('app.NOT_GET_XINGZUAN_TIME', []);
  1704. $time_arr1 = explode('|', $time_arr);
  1705. foreach ($time_arr1 as $key=>$val){
  1706. $time_days = explode('-', $val);
  1707. $Day = date('Y-m-d ',time());
  1708. $timeBegin = strtotime($Day.$time_days[0].":00");
  1709. $timeEnd = strtotime($Day.$time_days[1].":00");
  1710. $curr_time = time();
  1711. if($curr_time >= $timeBegin && $curr_time <= $timeEnd){
  1712. return false;
  1713. }
  1714. }
  1715. return true;
  1716. }
  1717. function getActionSecury($uid, $security_pass){
  1718. $user = Db::name('user')->where('id', $uid)->field('id,pay_pass')->find();
  1719. if ($user){
  1720. if (empty($user['pay_pass'])){
  1721. throw new \think\Exception('还未设置安全密码或安全密码错误', 500);
  1722. }
  1723. if (md5($security_pass) != $user['pay_pass']){
  1724. throw new \think\Exception('安全密码错误', 500);
  1725. }
  1726. }else{
  1727. throw new \think\Exception('用户信息错误');
  1728. }
  1729. }
  1730. // 判断时间是否交叉 true|没有交叉 false|交叉
  1731. function compareDate($arr): bool
  1732. {
  1733. array_multisort(array_column($arr, 'start_time'), SORT_ASC, $arr);
  1734. foreach ($arr as $k => $v){
  1735. if ($v['start_time'] >= $v['end_time'])
  1736. return false;
  1737. if ($k > 0 && $arr[$k]['start_time'] < $arr[$k-1]['end_time'])
  1738. return false;
  1739. }
  1740. return true;
  1741. }
  1742. if (!function_exists('createdGoodsSn')) {
  1743. function createdGoodsSn ()
  1744. {
  1745. return '6' . substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))) . str_pad(mt_rand(1, 9999999), 7, '0', STR_PAD_LEFT), 0, 11);
  1746. }
  1747. }
  1748. if (!function_exists('creathfOrderSn')) {
  1749. function creathfOrderSn ($uid)
  1750. {
  1751. return 'HF'.$uid. substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))) . str_pad(mt_rand(1, 9999999), 7, '0', STR_PAD_LEFT), 0, 11);
  1752. }
  1753. }
  1754. if (!function_exists('createdOrderWithdrawSn')) {
  1755. function createdOrderWithdrawSn ($uid)
  1756. {
  1757. return 'WD'.$uid. substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))) . str_pad(mt_rand(1, 9999999), 7, '0', STR_PAD_LEFT), 0, 11);
  1758. }
  1759. }
  1760. /**
  1761. * curl POST
  1762. *
  1763. * @param string url
  1764. * @param array 数据
  1765. * @param int 请求超时时间
  1766. * @param bool HTTPS时是否进行严格认证
  1767. * @return string
  1768. */
  1769. function curlPost($url, $data = array(), $timeout = 30, $CA = true)
  1770. {
  1771. // $cacert = ROOT_PATH . '/Epay/cacert.pem'; //CA根证书
  1772. // $SSL = substr($url, 0, 8) == "https://" ? true : false;
  1773. $ch = curl_init();
  1774. curl_setopt($ch, CURLOPT_URL, $url);
  1775. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  1776. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout - 2);
  1777. // if ($SSL && $CA) {
  1778. // curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); // 只信任CA颁布的证书
  1779. // curl_setopt($ch, CURLOPT_CAINFO, $cacert); // CA根证书(用来验证的网站证书是否是CA颁布)
  1780. // curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); // 检查证书中是否设置域名,并且是否与提供的主机名匹配
  1781. // } else if ($SSL && !$CA) {
  1782. // curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 信任任何证书
  1783. // curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1); // 检查证书中是否设置域名
  1784. // }
  1785. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  1786. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  1787. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  1788. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:')); //避免data数据过长问题
  1789. curl_setopt($ch, CURLOPT_POST, true);
  1790. // curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  1791. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data)); //data with URLEncode
  1792. $ret = curl_exec($ch);
  1793. // var_dump(curl_error($ch)); //查看报错信息
  1794. curl_close($ch);
  1795. return $ret;
  1796. }
  1797. if (!function_exists('creatBankSignOrderSn')) {
  1798. function creatBankSignOrderSn ($uid)
  1799. {
  1800. return 'signbank'.$uid. substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))) . str_pad(mt_rand(1, 9999999), 7, '0', STR_PAD_LEFT), 0, 11);
  1801. }
  1802. }
  1803. if (!function_exists('creatJhOrderSn')) {
  1804. function creatJhOrderSn ($uid)
  1805. {
  1806. return 'JH'.$uid. substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))) . str_pad(mt_rand(1, 9999999), 7, '0', STR_PAD_LEFT), 0, 11);
  1807. }
  1808. }
  1809. if (!function_exists('getUserPayWay')) {
  1810. function getUserPayWay ($uid)
  1811. {
  1812. return Db::name('user_data')->where('uid', $uid)->field('wx_img,wx_pay,wx_pay_name,ali_img,ali_pay,ali_pay_name,bank_no,bank_subname')->withAttr('wx_img', function ($val, $data){
  1813. return empty($val)?'':getWebUrl().'/'.$val;
  1814. })->withAttr('ali_img', function ($val, $data){
  1815. return empty($val)?'':getWebUrl().'/'.$val;
  1816. })->find();
  1817. }
  1818. }
  1819. if (!function_exists('createdMachineID')) {
  1820. function createdMachineID ()
  1821. {
  1822. return 'MA-' . md5('4dr'.substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))) . str_pad(mt_rand(1, 9999999), 7, '0', STR_PAD_LEFT), 0, 11).'DF');
  1823. }
  1824. }
  1825. if (!function_exists('multi_minmax_inarray')) {
  1826. function multi_minmax_inarray ($arr, $key)
  1827. {
  1828. $disArr = array();
  1829. foreach ($arr as $value) {
  1830. $disArr[] = floatval($value[$key]);
  1831. }
  1832. sort($disArr);
  1833. $resArr = !empty($disArr) ? array($disArr[0], $disArr[count($disArr) - 1]) : array(10, 10);
  1834. unset($disArr);
  1835. return ['min' => $resArr[0], 'max' => $resArr[1]];
  1836. }
  1837. }
  1838. if (!function_exists('multi_array_sum')) {
  1839. function multi_array_sum ($arr, $key)
  1840. {
  1841. if ($arr) {
  1842. $sum_no = 0;
  1843. foreach ($arr as $v) {
  1844. $sum_no += $v[$key];
  1845. }
  1846. return $sum_no;
  1847. } else {
  1848. return 0;
  1849. }
  1850. }
  1851. }
  1852. // 验证用户是否需要验证码登录 return true 需要 ,return false 不需要
  1853. function checkUserNeedsSecuritLogin($uid){
  1854. // if (in_array($uid, [7493266, 7493267, 7493487])){
  1855. //
  1856. // }
  1857. return false;
  1858. $start = date('Y-m-d H:i:s',mktime(0,0,0,date('m'),date('d'),date('Y')));
  1859. $w = date('w',strtotime($start));
  1860. // $last_week_mon = date('Y-m-d H:i:s',mktime(0,0,0,date('m'),date('d')-$w+1-7,date('Y')));
  1861. $old_start = date('Y-m-d H:i:s',mktime(0,0,0,date('m'),date('d')-7,date('Y')));
  1862. $end_time = date('Y-m-d H:i:s',mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1);
  1863. // 用户在上一周的抢购数据总和
  1864. $buy_num = Db::name('coupon_plan_log')->where('uid', $uid)->where('create_time', '>', $old_start)->sum('buy_num');
  1865. if ($buy_num < 1000){
  1866. return true;
  1867. }
  1868. return false;
  1869. }
  1870. function sr_hidtel($phone){
  1871. $IsWhat = preg_match('/(0[0-9]{2,3}[\-]?[2-9][0-9]{6,7}[\-]?[0-9]?)/i',$phone); //固定电话
  1872. if($IsWhat == 1){
  1873. return preg_replace('/(0[0-9]{2,3}[\-]?[2-9])[0-9]{3,4}([0-9]{3}[\-]?[0-9]?)/i','$1****$2',$phone);
  1874. }else{
  1875. return preg_replace('/(1[358]{1}[0-9])[0-9]{4}([0-9]{4})/i','$1****$2',$phone);
  1876. }
  1877. }
  1878. /**
  1879. * 打印log日志
  1880. * @param 文件名 第几行 日志内容
  1881. */
  1882. function sr_log($msg=null){
  1883. if(is_array($msg)){
  1884. $msg = json_encode($msg,JSON_UNESCAPED_UNICODE);
  1885. }
  1886. $msg = '['.date("Y-m-d H:i:s").']'.'日志内容:'.$msg;
  1887. $rootpath = ROOT_PATH.DS.'runtime'. DS .'sr_logs'. DS;
  1888. makedir($rootpath);
  1889. // 日志文件名:日期.txt
  1890. $path = $rootpath.date("Ymd").'.txt';
  1891. file_put_contents($path, $msg.PHP_EOL,FILE_APPEND);
  1892. }
  1893. /**
  1894. * [makedir 迭代创建级联目录]
  1895. * @param [type] $path [目录路径]
  1896. * @return [type] [Boolean]
  1897. */
  1898. function makedir($path){
  1899. $arr=array();
  1900. while(!is_dir($path)){
  1901. array_push($arr,$path);//把路径中的各级父目录压入到数组中去,直接有父目录存在为止(即上面一行is_dir判断出来有目录,条件为假退出while循环)
  1902. $path=dirname($path);//父目录
  1903. }
  1904. if(empty($arr)){//arr为空证明上面的while循环没有执行,即目录已经存在
  1905. // echo $path,'已经存在';
  1906. return true;
  1907. }
  1908. while(count($arr)){
  1909. $parentdir=array_pop($arr);//弹出最后一个数组单元
  1910. mkdir($parentdir);//从父目录往下创建
  1911. }
  1912. }
  1913. function sr_testDb($msg , $type = 1){
  1914. \think\facade\Db::name('test')->insert(['msg'=>$msg, 'type'=>$type, 'time'=>sr_getcurtime(time())]);
  1915. }
  1916. function sr_getLastSql(){
  1917. return \think\facade\Db::getLastSql();
  1918. }
  1919. /**
  1920. * 图片地址替换
  1921. */
  1922. if (!function_exists('imageSrcReplace')) {
  1923. function imageSrcReplace ($str)
  1924. {
  1925. $pattern = "/<[img|IMG].*?src=[\'|\"](.*?(?:[\.gif|\.jpg|\.png|\.jpeg]))[\'|\"].*?[\/]?>/i";
  1926. $content = preg_replace_callback($pattern, function ($ma) {
  1927. $src = env('qiniu.domain') . $ma[1];
  1928. return str_replace($ma[1], $src, $ma[0]);
  1929. }, $str);
  1930. return $content;
  1931. }
  1932. }
  1933. function sr_imgcodeconfig(){
  1934. return [
  1935. 'fontSize'=>40,
  1936. 'codeSet'=>'0123456789',
  1937. 'useCurve'=>true,
  1938. 'useNoise'=>false
  1939. ];
  1940. }
  1941. /*
  1942. * 生成签名
  1943. * @param array $args 数据
  1944. * @param string $key 私钥
  1945. */
  1946. function thirdPayGetSign($args, $key){
  1947. if(isset($args['sign'])) {
  1948. unset($args['sign']);
  1949. }
  1950. ksort($args);
  1951. $requestString = '';
  1952. foreach($args as $k => $v) {
  1953. if($v==null || $v==''){
  1954. continue;
  1955. }
  1956. $requestString .= $k . '=' . urlencode($v).'&';
  1957. }
  1958. $requestString .= 'key=' . $key;
  1959. $newSign=strtoupper(md5($requestString));
  1960. return $newSign;
  1961. }
  1962. /**
  1963. * 验证签名
  1964. * @param array $args 数据
  1965. * @param string $key 私钥
  1966. */
  1967. function thirdPayCheckSign($args, $key){
  1968. $sign = strtolower($args['sign']);
  1969. $tenpaySign=strtolower(getSign($args,$key));
  1970. return $sign == $tenpaySign;
  1971. }
  1972. /*
  1973. * 生成签名 畅联
  1974. * @param array $args 数据
  1975. * @param string $key 私钥
  1976. */
  1977. function thirdPayGetSignCL($secret, $data)
  1978. {
  1979. // 去空
  1980. $data = array_filter($data);
  1981. //签名步骤一:按字典序排序参数
  1982. ksort($data);
  1983. if (isset($data['pay_type'])){
  1984. if($data['pay_type']=='AliRoyalty'){
  1985. foreach ($data['royalty_parameters'] as $k=>$v){
  1986. ksort($data['royalty_parameters'][$k]);
  1987. }
  1988. }
  1989. }
  1990. // var_dump($data);
  1991. $string_a = http_build_query($data);
  1992. $string_a = urldecode($string_a);
  1993. //签名步骤二:在string后加入mch_key
  1994. $string_sign_temp = $string_a . "&key=" . $secret;
  1995. // var_dump($string_sign_temp);
  1996. //签名步骤三:MD5加密
  1997. $sign = md5($string_sign_temp);
  1998. // 签名步骤四:所有字符转为大写
  1999. $result = strtoupper($sign);
  2000. // var_dump($result);
  2001. return $result;
  2002. }
  2003. /**
  2004. * 验证签名 畅联
  2005. * @param array $args 数据
  2006. * @param string $key 私钥
  2007. */
  2008. function thirdPayCheckSignCL($data, $secret) {
  2009. // 验证参数中是否有签名
  2010. if (!isset($data['sign']) || !$data['sign']) {
  2011. return false;
  2012. }
  2013. // 要验证的签名串
  2014. $sign = $data['sign'];
  2015. unset($data['sign']);
  2016. // 生成新的签名、验证传过来的签名
  2017. $sign2 = thirdPayGetSignCL($secret, $data);
  2018. return $sign2;
  2019. }
  2020. // 验证图形验证码
  2021. function sr_captcha_check($vercode, $uuid){
  2022. $text = CacheServices::get('catch'.$uuid);
  2023. if (strtoupper($text) == strtoupper($vercode)){
  2024. CacheServices::set('catch'.$uuid, '');
  2025. return true;
  2026. }
  2027. return false;
  2028. }
  2029. if (!function_exists('createdOrderSn')) {
  2030. function createdOrderSn ()
  2031. {
  2032. return date('YmdHis', time()) . substr(microtime(), 2, 6) . sprintf('%03d', rand(0, 999));
  2033. }
  2034. }
  2035. if (!function_exists('createUserNickname')) {
  2036. function createUserNickname ()
  2037. {
  2038. return 'duobao'.date('His', time()) . rand(0, 999);
  2039. }
  2040. }
  2041. if (!function_exists('createdHandleOrderSn')) {
  2042. function createdHandleOrderSn ()
  2043. {
  2044. return 'h'.date('YmdHis', time()) . substr(microtime(), 2, 6) . sprintf('%03d', rand(0, 999));
  2045. }
  2046. }
  2047. if (!function_exists('createdFDOrderSn')) {
  2048. function createdFDOrderSn ()
  2049. {
  2050. return 'fd-'.date('YmdHis', time()) . substr(microtime(), 2, 6) . sprintf('%03d', rand(0, 999));
  2051. }
  2052. }
  2053. if (!function_exists('createdRechargeOrderSn')) {
  2054. function createdRechargeOrderSn ()
  2055. {
  2056. return date('YmdHis', time()) . substr(microtime(), 2, 6) . sprintf('%03d', rand(0, 999));
  2057. }
  2058. }
  2059. /**
  2060. * RSA数据加密解密
  2061. * @param type $data
  2062. * @param type $type encode加密 decode解密
  2063. */
  2064. function RSA_openssl($data,$type='encode'){
  2065. if (empty($data)) {
  2066. return 'data参数不能为空';
  2067. }
  2068. if (is_array($data)) {
  2069. return 'data参数不能是数组形式';
  2070. }
  2071. $rsa = new RsaClient();
  2072. // 生成的私钥
  2073. $privatePEMKey = env('api.PAY_FUYOU_PRIVATE_KEY');
  2074. // 生成的公钥
  2075. $publicPEMKey = env('api.PAY_FUYOU_PUBLIC_KEY');
  2076. $rsa->rsaPublicKey =$publicPEMKey;
  2077. $rsa->rsaPrivateKey = $privatePEMKey;
  2078. //私钥解密
  2079. if ($type=='decode') {
  2080. $decrypted = $rsa->privateDecryptRsa($data);
  2081. sr_log('4444444444'.'---'.$decrypted);
  2082. return $decrypted;
  2083. }
  2084. //公钥加密
  2085. if ($type=='encode') {
  2086. $decrypted = $rsa->publicEncryptRsa($data);
  2087. return $decrypted;
  2088. }
  2089. }