common.php 72 KB

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