common.php 71 KB

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