common.php 73 KB

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