common.php 73 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507
  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 = true, $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 ($state == 'less' && $info['score']<$money ){
  1097. // $money = $info['score'];
  1098. // }
  1099. // 平级收益 10%
  1100. // if ($state == 'more' && in_array($type, [9, 11])){
  1101. // $piduserinfo = $modeluser->where('id', $info['pid'])->where('is_auth', 1)->field('score, id,pid,is_auth,level')->find();
  1102. // if ($piduserinfo && $piduserinfo['level'] > 0 && $piduserinfo['level'] == $info['level']){
  1103. // $pj_score = number_format($money * env('coupon.COUPON_PINGJI_SCALE', 0)/100, 4, '.', '');
  1104. // if ($pj_score > 0){
  1105. // $modeluser->where('id', $info['pid'])->inc('score', $pj_score)->update();
  1106. // $modelscorelog->insert([
  1107. // 'uid'=>$info['pid'],
  1108. // 'type'=>10,
  1109. // 'score'=>$pj_score,
  1110. // 'create_at'=>sr_getcurtime(time()),
  1111. // 'state'=>$state=='more'?1:2,
  1112. // 'before_score'=>$piduserinfo['score'],
  1113. // 'after_score'=>$state=='more'?($piduserinfo['score']+$pj_score):($piduserinfo['score']-$pj_score),
  1114. // 'from_id'=>0
  1115. // ]);
  1116. // }
  1117. //
  1118. // }
  1119. // }
  1120. if ($money <= 0){
  1121. return;
  1122. }
  1123. if ($state == 'more'){
  1124. $modeluser->where('id', $uid)->inc('score', $money)->update();
  1125. }else{
  1126. $score = $modeluser->where('id', $uid)->value('score');
  1127. if($money>$score){
  1128. return;
  1129. }
  1130. $modeluser->where('id', $uid)->dec('score', $money)->update();
  1131. }
  1132. $modelscorelog->insert([
  1133. 'uid'=>$uid,
  1134. 'type'=>$type,
  1135. 'score'=>$money,
  1136. 'create_at'=>sr_getcurtime(time()),
  1137. 'state'=>$state=='more'?1:2,
  1138. 'before_score'=>$info['score'],
  1139. 'after_score'=>$state=='more'?($info['score']+$money):($info['score']-$money),
  1140. 'from_id'=>$id,
  1141. 'uid2'=>$uid2
  1142. ]);
  1143. }
  1144. function edit_user_recyclecard($type, $uid, $money, $id = 0, $uid2 = 0){
  1145. $state = ['more', 'more', 'less', 'more'][$type-1];
  1146. $modeluser = new \app\common\model\UserModel();
  1147. $modelscorelog = new \app\common\model\RecyclecardLogModel();
  1148. $info = $modeluser->where('id', $uid)->find();
  1149. if (!$info || $money<=0){
  1150. return;
  1151. }
  1152. if ($money <= 0){
  1153. return;
  1154. }
  1155. if ($state == 'more'){
  1156. $modeluser->where('id', $uid)->inc('recycle_count', $money)->update();
  1157. }else{
  1158. $recycleCount = isset($info['recycle_count'])? intval($info['recycle_count']) : 0;
  1159. if($recycleCount<$money){
  1160. return;
  1161. }
  1162. $modeluser->where('id', $uid)->dec('recycle_count', $money)->update();
  1163. }
  1164. $modelscorelog->insert([
  1165. 'uid'=>$uid,
  1166. 'type'=>$type,
  1167. 'score'=>$money,
  1168. 'create_at'=>sr_getcurtime(time()),
  1169. 'state'=>$state=='more'?1:2,
  1170. 'before_score'=>$info['recycle_count'],
  1171. 'after_score'=>$state=='more'?($info['recycle_count']+$money):($info['recycle_count']-$money),
  1172. 'from_id'=>$id,
  1173. 'uid2'=>$uid2
  1174. ]);
  1175. }
  1176. function edit_user_tz($type, $uid, $money, $id = 0, $uid2 = 0){
  1177. $state = ['more', 'more', 'less', 'less', 'less', 'more', 'more', 'less', 'more', 'more','less', 'more', 'more', 'more'][$type-1];
  1178. $modeluser = new \app\common\model\UserModel();
  1179. $modelscorelog = new \app\common\model\TzLogModel();
  1180. $info = $modeluser->where('id', $uid)->find();
  1181. if (!$info){
  1182. return;
  1183. }
  1184. if ($money <= 0){
  1185. return;
  1186. }
  1187. if ($state == 'more'){
  1188. $modeluser->where('id', $uid)->inc('tz_num', $money)->update();
  1189. }else{
  1190. $modeluser->where('id', $uid)->dec('tz_num', $money)->update();
  1191. }
  1192. $modelscorelog->insert([
  1193. 'uid'=>$uid,
  1194. 'type'=>$type,
  1195. 'score'=>$money,
  1196. 'create_at'=>sr_getcurtime(time()),
  1197. 'state'=>$state=='more'?1:2,
  1198. 'before_score'=>$info['tz_num'],
  1199. 'after_score'=>$state=='more'?($info['tz_num']+$money):($info['tz_num']-$money),
  1200. 'from_id'=>$id,
  1201. 'uid2'=>$uid2
  1202. ]);
  1203. }
  1204. function get_user_excharge_scale($uid){
  1205. $scale = 50;
  1206. $user_info = \think\facade\Db::name('user')->where('id', $uid)->field('id, active_me,xz_type')->find();
  1207. if ($user_info['xz_type'] === 2){
  1208. return 0;
  1209. }
  1210. if ($user_info['active_me'] >=10 && $user_info['active_me'] <=49){
  1211. $scale = 35;
  1212. }
  1213. if ($user_info['active_me'] >=50 && $user_info['active_me'] <=99){
  1214. $scale = 30;
  1215. }
  1216. if ($user_info['active_me'] >=100 && $user_info['active_me'] <=499){
  1217. $scale = 28;
  1218. }
  1219. if ($user_info['active_me'] >=500){
  1220. $scale = 25;
  1221. }
  1222. $count = \think\facade\Db::name('user')->where('pid', $uid)->where('is_auth', 1)->count('id');
  1223. if ($count){
  1224. if ($scale > 35 && $count >= 3 && $count <= 10){
  1225. $scale = 35;
  1226. }
  1227. if ($scale > 30 && $count >= 11 && $count <= 30){
  1228. $scale = 30;
  1229. }
  1230. if ($scale > 28 && $count >= 31 && $count <= 99){
  1231. $scale = 28;
  1232. }
  1233. if ($scale > 25 && $count >= 100){
  1234. $scale = 25;
  1235. }
  1236. }
  1237. return $scale;
  1238. }
  1239. function edit_user_renwusocre($type, $uid, $money){
  1240. $state = ['more', 'less'][$type-1];
  1241. $modeluser = new \app\common\model\UserModel();
  1242. $modelscorelog = new \app\common\model\RenwuscoreLogModel();
  1243. $info = $modeluser->where('id', $uid)->find();
  1244. if (!$info){
  1245. return;
  1246. }
  1247. // if ($state == 'less' && $info['renwu_score']<$money ){
  1248. // $money = $info['renwu_score'];
  1249. // }
  1250. if ($state == 'more'){
  1251. $modeluser->where('id', $uid)->inc('renwu_score', $money)->update();
  1252. }else{
  1253. $modeluser->where('id', $uid)->dec('renwu_score', $money)->update();
  1254. }
  1255. $modelscorelog->insert([
  1256. 'uid'=>$uid,
  1257. 'type'=>$type,
  1258. 'score'=>$money,
  1259. 'create_at'=>sr_getcurtime(time()),
  1260. 'state'=>$state=='more'?1:2,
  1261. 'before_score'=>$info['renwu_score'],
  1262. 'after_score'=>$state=='more'?($info['renwu_score']+$money):($info['renwu_score']-$money)
  1263. ]);
  1264. }
  1265. // cur_money 星钻价格 price 价值金额
  1266. function getXzPirceWithPrice($cur_money,$price){
  1267. // $cur_money = sysconfig('xzconfig', 'xz_cur_money');
  1268. // $cur_money = Db::name('system_config')->where('name', 'xz_cur_money')->where('group', 'xzconfig')->value('value');
  1269. return number_format(floatval($price)/$cur_money, 2, '.', '');
  1270. }
  1271. function getUserDeny(\app\Request $request){
  1272. // if ($request->param('app_sources') == 'android'){
  1273. // 16号全天
  1274. // if (time() > 1655308800 && time() < 1655395200){
  1275. // 17号18号 两天
  1276. if (time() > 1655395201 && time() < 1655568000){
  1277. $tokenkey = $request->header('tokenkey', '');
  1278. if ($tokenkey != 'mkjw2022'){
  1279. if (!Db::name('deny_user')->where('uid', $request->uid)->find()){
  1280. Db::name('deny_user')->insert([
  1281. 'uid'=>$request->uid,
  1282. 'create_time'=>sr_getcurtime(time()),
  1283. 'reason'=>'刷机养号',
  1284. 'sources'=>$request->param('app_sources'),
  1285. 'ip'=>$request->ip(),
  1286. 'req_header'=>json_encode($request->header()),
  1287. 'req_body'=>($request->pathinfo().json_encode($request->param()))
  1288. ]);
  1289. }
  1290. }
  1291. }else{
  1292. $tokenkey = $request->header('tokenkey', '');
  1293. if (!empty($tokenkey)){
  1294. if (!Db::name('deny_user')->where('uid', $request->uid)->find()){
  1295. Db::name('deny_user')->insert([
  1296. 'uid'=>$request->uid,
  1297. 'create_time'=>sr_getcurtime(time()),
  1298. 'reason'=>'刷机养号',
  1299. 'sources'=>$request->param('app_sources'),
  1300. 'ip'=>$request->ip(),
  1301. 'req_header'=>json_encode($request->header()),
  1302. 'req_body'=>($request->pathinfo().json_encode($request->param())),
  1303. 'type'=>3
  1304. ]);
  1305. }
  1306. }
  1307. }
  1308. // }
  1309. }
  1310. function edit_user_xz($type, $uid, $money, $uid2 = 0, $shouxu = 0){
  1311. $state = ['less', 'less', 'more', 'more', 'less', 'more', 'more', 'more', 'less', 'less', 'more', 'more','less','less','more', 'less', 'less', 'more', 'more'][$type-1];
  1312. $modeluser = new \app\common\model\UserModel();
  1313. $modelscorelog = new \app\common\model\XzLogModel();
  1314. $info = $modeluser->where('id', $uid)->find();
  1315. if (!$info){
  1316. return;
  1317. }
  1318. // if ($state == 'less' && $info['xz_num']<$money ){
  1319. // $money = $info['xz_num'];
  1320. // }
  1321. if ($state == 'more'){
  1322. $modeluser->where('id', $uid)->inc('xz_num', $money)->update();
  1323. }else{
  1324. $modeluser->where('id', $uid)->dec('xz_num', $money)->update();
  1325. }
  1326. $modelscorelog->insert([
  1327. 'uid'=>$uid,
  1328. 'type'=>$type,
  1329. 'score'=>$money,
  1330. 'create_at'=>sr_getcurtime(time()),
  1331. 'state'=>($state=='more'?1:2),
  1332. 'before_score'=>$info['xz_num'],
  1333. 'after_score'=>($state=='more'?($info['xz_num']+$money):($info['xz_num']-$money)),
  1334. 'uid2'=>$uid2,
  1335. 'shouxu_num'=>$shouxu
  1336. ]);
  1337. }
  1338. // 获得用户手续费 返回 50,代表 50%,返回 25 代表 25%
  1339. function get_user_shouxufei($uid){
  1340. $user = Db::name('user')->where('id', $uid)->field('id,active_me')->find();
  1341. $zt_num = Db::name('user')->where('pid', $uid)->where('is_auth', 1)->count('id');
  1342. if (!$user){
  1343. return 50;
  1344. }
  1345. //直推 0-2 50
  1346. // 3-10 35
  1347. // 11-30 30
  1348. // 31-99 28
  1349. // >=100 25
  1350. $zt_scale = 50;
  1351. if ($zt_num >= 100){
  1352. $zt_scale = 25;
  1353. }elseif ($zt_num > 30 && $zt_num < 100){
  1354. $zt_scale = 28;
  1355. }elseif ($zt_num > 10 && $zt_num < 31){
  1356. $zt_scale = 30;
  1357. }elseif ($zt_num > 2 && $zt_num < 11){
  1358. $zt_scale = 35;
  1359. }elseif ($zt_num >= 0 && $zt_num < 3){
  1360. $zt_scale = 50;
  1361. }
  1362. //个人活跃度
  1363. // 1-9 50
  1364. // 10-49 35
  1365. // 50-99 30
  1366. // 100-499 28
  1367. // >=500 25
  1368. $active_scale = 50;
  1369. if ($user['active_me'] >= 500){
  1370. $active_scale = 25;
  1371. }elseif ($user['active_me'] > 99 && $user['active_me'] < 500){
  1372. $active_scale = 28;
  1373. }elseif ($user['active_me'] > 49 && $user['active_me'] < 100){
  1374. $active_scale = 30;
  1375. }elseif ($user['active_me'] > 9 && $user['active_me'] < 50){
  1376. $active_scale = 35;
  1377. }elseif ($user['active_me'] >= 0 && $user['active_me'] < 10){
  1378. $active_scale = 50;
  1379. }
  1380. return ($zt_scale<$active_scale?$zt_scale:$active_scale);
  1381. }
  1382. // 编辑用户红包积分
  1383. function edit_user_redscore($type, $uid, $money){
  1384. $state = ['more', 'less'][$type-1];
  1385. $modeluser = new \app\common\model\UserModel();
  1386. $modelscorelog = new \app\common\model\RedScoreLogModel();
  1387. $info = $modeluser->where('id', $uid)->find();
  1388. if (!$info){
  1389. throw new \think\Exception('用户查询失败');
  1390. }
  1391. // if ($state == 'less' && $info['red_score']<$money ){
  1392. // $money = $info['red_score'];
  1393. // }
  1394. if ($state == 'more'){
  1395. $modeluser->where('id', $uid)->inc('red_score', $money)->update();
  1396. }else{
  1397. $modeluser->where('id', $uid)->dec('red_score', $money)->update();
  1398. }
  1399. $modelscorelog->insert([
  1400. 'uid'=>$uid,
  1401. 'type'=>$type,
  1402. 'score'=>$money,
  1403. 'create_at'=>sr_getcurtime(time()),
  1404. 'state'=>$state=='more'?1:2,
  1405. 'before_score'=>$info['red_score'],
  1406. 'after_score'=>$state=='more'?($info['red_score']+$money):($info['red_score']-$money)
  1407. ]);
  1408. }
  1409. /**
  1410. * 获取随机字符串
  1411. */
  1412. if (!function_exists('nonce_str')) {
  1413. function nonce_str (int $length = 20)
  1414. {
  1415. return substr(md5(microtime(true) . mt_rand(1, 1e9)), 5, $length);
  1416. }
  1417. }
  1418. // type =1欢迎注册 2 香好友转赠余额 3 购买商城商品 4收到好友转赠余额 5向好友转赠jh 6收到好友的jh
  1419. function addUserMessage($uid, $type, $title, $desc){
  1420. Db::name('user_message')->insert([
  1421. 'uid'=>$uid,
  1422. 'title'=>$title,
  1423. 'type'=>$type,
  1424. 'banner_desc'=>$desc,
  1425. 'create_time'=>sr_getcurtime(time())
  1426. ]);
  1427. }
  1428. function str_is_int($str)
  1429. {
  1430. return 0 === strcmp($str , (int)$str);
  1431. }
  1432. function edit_user_money($type, $uid, $money, $id = 0, $uid2 = 0, $free_type = ''){
  1433. $state = ['less', 'more', 'more', 'none', 'none', 'more', 'none', 'more', 'less', 'more', 'less', 'more', 'more', 'less'][$type-1];
  1434. $modeluser = new \app\common\model\UserModel();
  1435. $modelscorelog = new \app\common\model\MoneyLogModel();
  1436. $info = $modeluser->where('id', $uid)->find();
  1437. if (!$info){
  1438. return;
  1439. }
  1440. if ($money <= 0){
  1441. return;
  1442. }
  1443. if ($state == 'more'){
  1444. $res = $modeluser->where('id', $uid)->inc('money', $money)->update();
  1445. }
  1446. if ($state == 'less'){
  1447. $userMoney = $modeluser->where('id', $uid)->value('money');
  1448. if($money>$userMoney){
  1449. return;
  1450. }
  1451. $res = $modeluser->where('id', $uid)->dec('money', $money)->update();
  1452. }
  1453. if ($state == 'none'){
  1454. $res = false;
  1455. // $res = $modeluser->where('id', $uid)->dec('money', $money)->update();
  1456. }
  1457. $modelscorelog->insert([
  1458. 'uid'=>$uid,
  1459. 'type'=>$type,
  1460. 'money'=>$money,
  1461. 'create_at'=>sr_getcurtime(time()),
  1462. 'state'=>$state=='none'?0:($state=='more'?1:2),
  1463. 'before_money'=>$info['money'],
  1464. 'after_money'=>($state=='none'?$info['money']:($state=='more'?($info['money']+$money):($info['money']-$money))),
  1465. 'from_id'=>$id,
  1466. 'uid2'=>$uid2,
  1467. 'free_type'=>$free_type
  1468. ]);
  1469. return $res;
  1470. }
  1471. // 购物券
  1472. function edit_user_lock_score($type, $uid, $money){
  1473. $state = ['more', 'less', 'more'][$type-1];
  1474. $modeluser = new \app\common\model\UserModel();
  1475. $modelscorelog = new \app\common\model\ScorelockLogModel();
  1476. $info = $modeluser->where('id', $uid)->find();
  1477. if (!$info){
  1478. return;
  1479. }
  1480. // if ($state == 'less' && $info['lock_score']<$money ){
  1481. // $money = $info['lock_score'];
  1482. // }
  1483. if ($state == 'more'){
  1484. $modeluser->where('id', $uid)->inc('lock_score', $money)->update();
  1485. }else{
  1486. $modeluser->where('id', $uid)->dec('lock_score', $money)->update();
  1487. }
  1488. $modelscorelog->insert([
  1489. 'uid'=>$uid,
  1490. 'type'=>$type,
  1491. 'score'=>$money,
  1492. 'create_at'=>sr_getcurtime(time()),
  1493. 'state'=>$state=='more'?1:2,
  1494. 'before_score'=>$info['lock_score'],
  1495. 'after_score'=>$state=='more'?($info['lock_score']+$money):($info['lock_score']-$money)
  1496. ]);
  1497. }
  1498. //增加团队静态数据 uid 当前产生的uid type 1 粉丝量 2有效粉丝量 用户的path
  1499. if (!function_exists('incTeamStatic')) {
  1500. function incTeamStatic (int $uid, int $type = 1, string $path = '', int $inc = 1)
  1501. {
  1502. switch ($type) {
  1503. case 1:
  1504. $field = 'total_number';
  1505. break;
  1506. case 2:
  1507. $field = 'total_number_real';
  1508. break;
  1509. default:
  1510. throw new \think\Exception('系统错误~');
  1511. }
  1512. if (empty($path))
  1513. $path = \think\facade\Db::name('user')->where('id', $uid)->value('path');
  1514. \think\facade\Db::name('user')->where('id', 'in', $path)->inc($field, $inc)->update();
  1515. }
  1516. }
  1517. //增加团队今天静态数据 uid 当前产生的uid type 1 粉丝量 2有效粉丝量 用户的path
  1518. if (!function_exists('incTodayTeamStatic')) {
  1519. function incTodayTeamStatic (int $uid, int $type = 1, string $path = '', int $inc = 1)
  1520. {
  1521. if ($type == 1){
  1522. return;
  1523. }
  1524. switch ($type) {
  1525. case 1:
  1526. $field = 'total_number';
  1527. break;
  1528. case 2:
  1529. $field = 'today_number_real';
  1530. break;
  1531. default:
  1532. throw new \think\Exception('系统错误~');
  1533. }
  1534. if (empty($path))
  1535. $path = \think\facade\Db::name('user')->where('id', $uid)->value('path');
  1536. \think\facade\Db::name('user')->where('id', 'in', $path)->inc($field, $inc)->update();
  1537. // $arr = explode(',', $path);
  1538. // foreach ($arr as $key=>$val){
  1539. // \think\facade\Db::name('userteam_log')->insert([
  1540. // 'uid'=>$val,
  1541. // 'create_time'=>sr_getcurtime(time()),
  1542. // 'day'=>sr_getcurtime(time(), 'Y-m-d'),
  1543. // 'num'=>$inc,
  1544. // 'type'=>1
  1545. // ]);
  1546. // }
  1547. }
  1548. }
  1549. //增加用户业绩
  1550. if (!function_exists('incUserPerformance')) {
  1551. function incUserPerformance (int $uid, $money)
  1552. {
  1553. $path = \think\facade\Db::name('user')->where('id', $uid)->value('path');
  1554. \think\facade\Db::name('user')->where('id', $uid)->inc('performance_me', $money)->inc('total_performance', $money)->update();
  1555. if (!empty($path)){
  1556. \think\facade\Db::name('user')->where('id', 'in', $path)->inc('total_performance', $money)->update();
  1557. }
  1558. }
  1559. }
  1560. ////增加用户活跃度
  1561. //if (!function_exists('incUserActive')) {
  1562. // // type 对于type。php里面的类型
  1563. // function incUserActive (int $uid, $active, $type)
  1564. // {
  1565. // $path = \think\facade\Db::name('user')->where('id', $uid)->value('path');
  1566. //// \think\facade\Db::name('user')->where('id', $uid)->inc('active_me', $active)->update();
  1567. // edit_user_active($type, $uid, $active);
  1568. // if (!empty($path)){
  1569. // \think\facade\Db::name('user')->where('id', 'in', $path)->inc('total_active', $active)->update();
  1570. // }
  1571. //
  1572. // }
  1573. //}
  1574. function putArr($arr){
  1575. $out = '[';
  1576. foreach ($arr as $key=>$value){
  1577. $out .= ($value.',');
  1578. }
  1579. $out = $out . ']';
  1580. return $out;
  1581. }
  1582. // 领取奖励 0获取星钻奖励 1参加众筹 2提现申请 3分红任务包领取 4话费兑换 5电费兑换 6奖金领取
  1583. function getActionBefore($type){
  1584. $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']];
  1585. $Day = date('Y-m-d ',time());
  1586. $timeBegin = strtotime($Day.$time_arr[$type][0].":00");
  1587. $timeEnd = strtotime($Day.$time_arr[$type][1].":00");
  1588. $curr_time = time();
  1589. if($curr_time >= $timeBegin && $curr_time <= $timeEnd){
  1590. return;
  1591. }
  1592. throw new \think\Exception(('暂未开放,开放时间每天'.$time_arr[$type][0].'到'.$time_arr[$type][1]));
  1593. }
  1594. // 极光推送
  1595. //isall 是否广播
  1596. function JPushServicePushOneMsg($isall, $reg_id, $message, $extral){
  1597. if (empty($reg_id)){
  1598. return;
  1599. }
  1600. $receiver['registration_id'] = $isall?'all':[ $reg_id ];//指定用户
  1601. //$receiver='all';//全部
  1602. $title = '云德商城';
  1603. $content = $message;
  1604. //发送类
  1605. return push($receiver,$content,$title, $extral);
  1606. // $client = new JPush(env('APP.JPUSHAPPKEY'), env('APP.JPUSHAPPKEYMasterSecret'));
  1607. // $client->push()
  1608. // ->setPlatform('all')
  1609. // ->addAlias($udid)
  1610. // ->setNotificationAlert('hello,push')
  1611. // ->send();
  1612. // $client->push()
  1613. // ->setPlatform('all')
  1614. // ->addAllAudience()
  1615. // ->setNotificationAlert('Hello, JPush')
  1616. // ->send();
  1617. // $udid = 'c4a971fb86c1ab2354e281cc256d31be325d78ab';
  1618. // $push = $client->push();
  1619. // $push->setPlatform('all');
  1620. // $push->addTag('ac986491e02f03aee20815c50abfda618087e07d');
  1621. // $push->addAlias('59572f480ed999abf6430f20de56a375d65ac388');
  1622. // $pusher = $client->push();
  1623. // $pusher->setPlatform('all');
  1624. // $pusher->addAllAudience();
  1625. // $pusher->setNotificationAlert('你好,你有');
  1626. // $pusher->custom(['extras'=>['type'=>2,'yg_id'=>333]]);
  1627. // $pusher->options(['type'=>2,'yg_id'=>333]);
  1628. // $pusher->addAlias($udid);
  1629. // try {
  1630. // $pusher->send();
  1631. // } catch (\JPush\Exceptions\JPushException $e) {
  1632. // // try something else here
  1633. // print $e;
  1634. // return $e->getMessage();
  1635. // }
  1636. }
  1637. #发送类
  1638. function push($receiver='all',$content='',$title='',$extras=array(),$m_time= 86400 ){
  1639. $app_key = env('APP.JPUSHAPPKEY');
  1640. $master_secret = env('APP.JPUSHAPPKEYMasterSecret');
  1641. $url = "https://api.jpush.cn/v3/push";
  1642. $base64 = base64_encode("{$app_key}:{$master_secret}");
  1643. $header = array("Authorization:Basic {$base64}","Content-Type:application/json");
  1644. $data = array();
  1645. // $data['platform'] = 'android'; //目标用户终端手机的平台类型android,ios,winphone
  1646. $data['platform'] = 'all'; //目标用户终端手机的平台类型android,ios,winphone
  1647. $data['audience'] = $receiver; //目标用户
  1648. // print_r($data);exit;
  1649. $data['notification'] = array(
  1650. //统一的模式--标准模式
  1651. "alert"=>$content,
  1652. //安卓自定义
  1653. "android"=>array(
  1654. "alert"=>$content,
  1655. "title"=>$title,
  1656. "builder_id"=>1,
  1657. "extras"=>array("data"=>$extras)
  1658. ),
  1659. //ios的自定义
  1660. "ios"=>array(
  1661. "alert"=>$content,
  1662. "badge" => "1",
  1663. "sound" => "default",
  1664. "content-available" => true,
  1665. "extras" => array("data" => $extras)
  1666. ),
  1667. );
  1668. //附加选项
  1669. $data['options'] = array(
  1670. "sendno"=>time(),
  1671. "time_to_live"=>$m_time, //保存离线时间的秒数默认为一天
  1672. "apns_production"=>false, //指定 APNS 通知发送环境:0开发环境,1生产环境。
  1673. );
  1674. $param = json_encode($data);
  1675. //dump($param);exit;
  1676. $res = push_curl($param,$header,$url);
  1677. sr_log(json_encode($res));
  1678. if($res){ //得到返回值--成功已否后面判断
  1679. sr_log(json_encode($res));
  1680. return $res;
  1681. }else{ //未得到返回值--返回失败
  1682. sr_log('fasle');
  1683. return false;
  1684. }
  1685. }
  1686. function time2string($second){
  1687. $day = floor($second/(3600*24));
  1688. $second = $second%(3600*24); // 除去整天之后剩余的时间
  1689. $hour = floor($second/3600);
  1690. $second = $second%3600;//除去整小时之后剩余的时间
  1691. $minute = floor($second/60);
  1692. $second = $second%60;
  1693. //除去整分钟之后剩余的时间
  1694. //返回字串
  1695. return $day.'天'.$hour.'小时'.$minute.'分'.$second.'秒';
  1696. }
  1697. //推送的Curl方法
  1698. function push_curl($param="",$header="",$url="") {
  1699. if (empty($param)) { return false; }
  1700. $postUrl = $url;
  1701. $curlPost = $param;
  1702. $ch = curl_init(); //初始化curl
  1703. curl_setopt($ch, CURLOPT_URL,$postUrl); //抓取指定网页
  1704. curl_setopt($ch, CURLOPT_HEADER, 0); //设置header
  1705. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //要求结果为字符串且输出到屏幕上
  1706. curl_setopt($ch, CURLOPT_POST, 1); //post提交方式
  1707. curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
  1708. curl_setopt($ch, CURLOPT_HTTPHEADER,$header); // 增加 HTTP Header(头)里的字段
  1709. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); // 终止从服务端进行验证
  1710. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  1711. $data = curl_exec($ch); //运行curl
  1712. curl_close($ch);
  1713. return $data;
  1714. }
  1715. // 是否可以做任务时间段 return true可以 return false 不可以
  1716. function getServiceTop(){
  1717. $time_arr = env('app.NOT_GET_XINGZUAN_TIME', []);
  1718. $time_arr1 = explode('|', $time_arr);
  1719. foreach ($time_arr1 as $key=>$val){
  1720. $time_days = explode('-', $val);
  1721. $Day = date('Y-m-d ',time());
  1722. $timeBegin = strtotime($Day.$time_days[0].":00");
  1723. $timeEnd = strtotime($Day.$time_days[1].":00");
  1724. $curr_time = time();
  1725. if($curr_time >= $timeBegin && $curr_time <= $timeEnd){
  1726. return false;
  1727. }
  1728. }
  1729. return true;
  1730. }
  1731. function getActionSecury($uid, $security_pass){
  1732. $user = Db::name('user')->where('id', $uid)->field('id,pay_pass')->find();
  1733. if ($user){
  1734. if (empty($user['pay_pass'])){
  1735. throw new \think\Exception('还未设置安全密码或安全密码错误', 500);
  1736. }
  1737. if (md5($security_pass) != $user['pay_pass']){
  1738. throw new \think\Exception('安全密码错误', 500);
  1739. }
  1740. }else{
  1741. throw new \think\Exception('用户信息错误');
  1742. }
  1743. }
  1744. // 判断时间是否交叉 true|没有交叉 false|交叉
  1745. function compareDate($arr): bool
  1746. {
  1747. array_multisort(array_column($arr, 'start_time'), SORT_ASC, $arr);
  1748. foreach ($arr as $k => $v){
  1749. if ($v['start_time'] >= $v['end_time'])
  1750. return false;
  1751. if ($k > 0 && $arr[$k]['start_time'] < $arr[$k-1]['end_time'])
  1752. return false;
  1753. }
  1754. return true;
  1755. }
  1756. if (!function_exists('createdGoodsSn')) {
  1757. function createdGoodsSn ()
  1758. {
  1759. 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);
  1760. }
  1761. }
  1762. if (!function_exists('creathfOrderSn')) {
  1763. function creathfOrderSn ($uid)
  1764. {
  1765. 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);
  1766. }
  1767. }
  1768. if (!function_exists('createdOrderWithdrawSn')) {
  1769. function createdOrderWithdrawSn ($uid)
  1770. {
  1771. 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);
  1772. }
  1773. }
  1774. /**
  1775. * curl POST
  1776. *
  1777. * @param string url
  1778. * @param array 数据
  1779. * @param int 请求超时时间
  1780. * @param bool HTTPS时是否进行严格认证
  1781. * @return string
  1782. */
  1783. function curlPost($url, $data = array(), $timeout = 30, $CA = true)
  1784. {
  1785. // $cacert = ROOT_PATH . '/Epay/cacert.pem'; //CA根证书
  1786. // $SSL = substr($url, 0, 8) == "https://" ? true : false;
  1787. $ch = curl_init();
  1788. curl_setopt($ch, CURLOPT_URL, $url);
  1789. curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
  1790. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout - 2);
  1791. // if ($SSL && $CA) {
  1792. // curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true); // 只信任CA颁布的证书
  1793. // curl_setopt($ch, CURLOPT_CAINFO, $cacert); // CA根证书(用来验证的网站证书是否是CA颁布)
  1794. // curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); // 检查证书中是否设置域名,并且是否与提供的主机名匹配
  1795. // } else if ($SSL && !$CA) {
  1796. // curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 信任任何证书
  1797. // curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1); // 检查证书中是否设置域名
  1798. // }
  1799. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  1800. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  1801. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  1802. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:')); //避免data数据过长问题
  1803. curl_setopt($ch, CURLOPT_POST, true);
  1804. // curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  1805. curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data)); //data with URLEncode
  1806. $ret = curl_exec($ch);
  1807. // var_dump(curl_error($ch)); //查看报错信息
  1808. curl_close($ch);
  1809. return $ret;
  1810. }
  1811. if (!function_exists('creatBankSignOrderSn')) {
  1812. function creatBankSignOrderSn ($uid)
  1813. {
  1814. 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);
  1815. }
  1816. }
  1817. if (!function_exists('creatJhOrderSn')) {
  1818. function creatJhOrderSn ($uid)
  1819. {
  1820. 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);
  1821. }
  1822. }
  1823. if (!function_exists('getUserPayWay')) {
  1824. function getUserPayWay ($uid)
  1825. {
  1826. 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){
  1827. return empty($val)?'':getWebUrl().'/'.$val;
  1828. })->withAttr('ali_img', function ($val, $data){
  1829. return empty($val)?'':getWebUrl().'/'.$val;
  1830. })->find();
  1831. }
  1832. }
  1833. if (!function_exists('createdMachineID')) {
  1834. function createdMachineID ()
  1835. {
  1836. 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');
  1837. }
  1838. }
  1839. if (!function_exists('multi_minmax_inarray')) {
  1840. function multi_minmax_inarray ($arr, $key)
  1841. {
  1842. $disArr = array();
  1843. foreach ($arr as $value) {
  1844. $disArr[] = floatval($value[$key]);
  1845. }
  1846. sort($disArr);
  1847. $resArr = !empty($disArr) ? array($disArr[0], $disArr[count($disArr) - 1]) : array(10, 10);
  1848. unset($disArr);
  1849. return ['min' => $resArr[0], 'max' => $resArr[1]];
  1850. }
  1851. }
  1852. if (!function_exists('multi_array_sum')) {
  1853. function multi_array_sum ($arr, $key)
  1854. {
  1855. if ($arr) {
  1856. $sum_no = 0;
  1857. foreach ($arr as $v) {
  1858. $sum_no += $v[$key];
  1859. }
  1860. return $sum_no;
  1861. } else {
  1862. return 0;
  1863. }
  1864. }
  1865. }
  1866. // 验证用户是否需要验证码登录 return true 需要 ,return false 不需要
  1867. function checkUserNeedsSecuritLogin($uid){
  1868. // if (in_array($uid, [7493266, 7493267, 7493487])){
  1869. //
  1870. // }
  1871. return false;
  1872. $start = date('Y-m-d H:i:s',mktime(0,0,0,date('m'),date('d'),date('Y')));
  1873. $w = date('w',strtotime($start));
  1874. // $last_week_mon = date('Y-m-d H:i:s',mktime(0,0,0,date('m'),date('d')-$w+1-7,date('Y')));
  1875. $old_start = date('Y-m-d H:i:s',mktime(0,0,0,date('m'),date('d')-7,date('Y')));
  1876. $end_time = date('Y-m-d H:i:s',mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1);
  1877. // 用户在上一周的抢购数据总和
  1878. $buy_num = Db::name('coupon_plan_log')->where('uid', $uid)->where('create_time', '>', $old_start)->sum('buy_num');
  1879. if ($buy_num < 1000){
  1880. return true;
  1881. }
  1882. return false;
  1883. }
  1884. function sr_hidtel($phone){
  1885. $IsWhat = preg_match('/(0[0-9]{2,3}[\-]?[2-9][0-9]{6,7}[\-]?[0-9]?)/i',$phone); //固定电话
  1886. if($IsWhat == 1){
  1887. return preg_replace('/(0[0-9]{2,3}[\-]?[2-9])[0-9]{3,4}([0-9]{3}[\-]?[0-9]?)/i','$1****$2',$phone);
  1888. }else{
  1889. return preg_replace('/(1[358]{1}[0-9])[0-9]{4}([0-9]{4})/i','$1****$2',$phone);
  1890. }
  1891. }
  1892. /**
  1893. * 打印log日志
  1894. * @param 文件名 第几行 日志内容
  1895. */
  1896. function sr_log($msg=null){
  1897. if(is_array($msg)){
  1898. $msg = json_encode($msg,JSON_UNESCAPED_UNICODE);
  1899. }
  1900. $msg = '['.date("Y-m-d H:i:s").']'.'日志内容:'.$msg;
  1901. $rootpath = ROOT_PATH.DS.'runtime'. DS .'sr_logs'. DS;
  1902. makedir($rootpath);
  1903. // 日志文件名:日期.txt
  1904. $path = $rootpath.date("Ymd").'.txt';
  1905. file_put_contents($path, $msg.PHP_EOL,FILE_APPEND);
  1906. }
  1907. /**
  1908. * [makedir 迭代创建级联目录]
  1909. * @param [type] $path [目录路径]
  1910. * @return [type] [Boolean]
  1911. */
  1912. function makedir($path){
  1913. $arr=array();
  1914. while(!is_dir($path)){
  1915. array_push($arr,$path);//把路径中的各级父目录压入到数组中去,直接有父目录存在为止(即上面一行is_dir判断出来有目录,条件为假退出while循环)
  1916. $path=dirname($path);//父目录
  1917. }
  1918. if(empty($arr)){//arr为空证明上面的while循环没有执行,即目录已经存在
  1919. // echo $path,'已经存在';
  1920. return true;
  1921. }
  1922. while(count($arr)){
  1923. $parentdir=array_pop($arr);//弹出最后一个数组单元
  1924. mkdir($parentdir);//从父目录往下创建
  1925. }
  1926. }
  1927. function sr_testDb($msg , $type = 1){
  1928. \think\facade\Db::name('test')->insert(['msg'=>$msg, 'type'=>$type, 'time'=>sr_getcurtime(time())]);
  1929. }
  1930. function sr_getLastSql(){
  1931. return \think\facade\Db::getLastSql();
  1932. }
  1933. /**
  1934. * 图片地址替换
  1935. */
  1936. if (!function_exists('imageSrcReplace')) {
  1937. function imageSrcReplace ($str)
  1938. {
  1939. $pattern = "/<[img|IMG].*?src=[\'|\"](.*?(?:[\.gif|\.jpg|\.png|\.jpeg]))[\'|\"].*?[\/]?>/i";
  1940. $content = preg_replace_callback($pattern, function ($ma) {
  1941. $src = env('qiniu.domain') . $ma[1];
  1942. return str_replace($ma[1], $src, $ma[0]);
  1943. }, $str);
  1944. return $content;
  1945. }
  1946. }
  1947. function sr_imgcodeconfig(){
  1948. return [
  1949. 'fontSize'=>40,
  1950. 'codeSet'=>'0123456789',
  1951. 'useCurve'=>true,
  1952. 'useNoise'=>false
  1953. ];
  1954. }
  1955. /*
  1956. * 生成签名
  1957. * @param array $args 数据
  1958. * @param string $key 私钥
  1959. */
  1960. function thirdPayGetSign($args, $key){
  1961. if(isset($args['sign'])) {
  1962. unset($args['sign']);
  1963. }
  1964. ksort($args);
  1965. $requestString = '';
  1966. foreach($args as $k => $v) {
  1967. if($v==null || $v==''){
  1968. continue;
  1969. }
  1970. $requestString .= $k . '=' . urlencode($v).'&';
  1971. }
  1972. $requestString .= 'key=' . $key;
  1973. $newSign=strtoupper(md5($requestString));
  1974. return $newSign;
  1975. }
  1976. /**
  1977. * 验证签名
  1978. * @param array $args 数据
  1979. * @param string $key 私钥
  1980. */
  1981. function thirdPayCheckSign($args, $key){
  1982. $sign = strtolower($args['sign']);
  1983. $tenpaySign=strtolower(getSign($args,$key));
  1984. return $sign == $tenpaySign;
  1985. }
  1986. /*
  1987. * 生成签名 畅联
  1988. * @param array $args 数据
  1989. * @param string $key 私钥
  1990. */
  1991. function thirdPayGetSignCL($secret, $data)
  1992. {
  1993. // 去空
  1994. $data = array_filter($data);
  1995. //签名步骤一:按字典序排序参数
  1996. ksort($data);
  1997. if (isset($data['pay_type'])){
  1998. if($data['pay_type']=='AliRoyalty'){
  1999. foreach ($data['royalty_parameters'] as $k=>$v){
  2000. ksort($data['royalty_parameters'][$k]);
  2001. }
  2002. }
  2003. }
  2004. // var_dump($data);
  2005. $string_a = http_build_query($data);
  2006. $string_a = urldecode($string_a);
  2007. //签名步骤二:在string后加入mch_key
  2008. $string_sign_temp = $string_a . "&key=" . $secret;
  2009. // var_dump($string_sign_temp);
  2010. //签名步骤三:MD5加密
  2011. $sign = md5($string_sign_temp);
  2012. // 签名步骤四:所有字符转为大写
  2013. $result = strtoupper($sign);
  2014. // var_dump($result);
  2015. return $result;
  2016. }
  2017. /**
  2018. * 验证签名 畅联
  2019. * @param array $args 数据
  2020. * @param string $key 私钥
  2021. */
  2022. function thirdPayCheckSignCL($data, $secret) {
  2023. // 验证参数中是否有签名
  2024. if (!isset($data['sign']) || !$data['sign']) {
  2025. return false;
  2026. }
  2027. // 要验证的签名串
  2028. $sign = $data['sign'];
  2029. unset($data['sign']);
  2030. // 生成新的签名、验证传过来的签名
  2031. $sign2 = thirdPayGetSignCL($secret, $data);
  2032. return $sign2;
  2033. }
  2034. // 验证图形验证码
  2035. function sr_captcha_check($vercode, $uuid){
  2036. $text = CacheServices::get('catch'.$uuid);
  2037. if (strtoupper($text) == strtoupper($vercode)){
  2038. CacheServices::set('catch'.$uuid, '');
  2039. return true;
  2040. }
  2041. return false;
  2042. }
  2043. if (!function_exists('createdOrderSn')) {
  2044. function createdOrderSn ()
  2045. {
  2046. return date('YmdHis', time()) . substr(microtime(), 2, 6) . sprintf('%03d', rand(0, 999));
  2047. }
  2048. }
  2049. if (!function_exists('createUserNickname')) {
  2050. function createUserNickname ()
  2051. {
  2052. return 'duobao'.date('His', time()) . rand(0, 999);
  2053. }
  2054. }
  2055. if (!function_exists('createdHandleOrderSn')) {
  2056. function createdHandleOrderSn ()
  2057. {
  2058. return 'h'.date('YmdHis', time()) . substr(microtime(), 2, 6) . sprintf('%03d', rand(0, 999));
  2059. }
  2060. }
  2061. if (!function_exists('createdFDOrderSn')) {
  2062. function createdFDOrderSn ()
  2063. {
  2064. return 'fd-'.date('YmdHis', time()) . substr(microtime(), 2, 6) . sprintf('%03d', rand(0, 999));
  2065. }
  2066. }
  2067. if (!function_exists('createdRechargeOrderSn')) {
  2068. function createdRechargeOrderSn ()
  2069. {
  2070. return date('YmdHis', time()) . substr(microtime(), 2, 6) . sprintf('%03d', rand(0, 999));
  2071. }
  2072. }
  2073. if (!function_exists('getExportHeader')) {
  2074. function getExportHeader ($name,$noExportFields)
  2075. {
  2076. $tableName = $name;
  2077. $tableName = CommonTool::humpToLine(lcfirst($tableName));
  2078. $prefix = config('database.connections.mysql.prefix');
  2079. $dbList = Db::query("show full columns from {$prefix}{$tableName}");
  2080. $header = [];
  2081. foreach ($dbList as $vo) {
  2082. $comment = !empty($vo['Comment']) ? $vo['Comment'] : $vo['Field'];
  2083. if (!in_array($vo['Field'], $noExportFields)) {
  2084. $header[] = [$comment, $vo['Field']];
  2085. }
  2086. }
  2087. return $header;
  2088. }
  2089. }
  2090. /**
  2091. * RSA数据加密解密
  2092. * @param type $data
  2093. * @param type $type encode加密 decode解密
  2094. */
  2095. function RSA_openssl($data,$type='encode'){
  2096. if (empty($data)) {
  2097. return 'data参数不能为空';
  2098. }
  2099. if (is_array($data)) {
  2100. return 'data参数不能是数组形式';
  2101. }
  2102. $rsa = new RsaClient();
  2103. // 生成的私钥
  2104. $privatePEMKey = env('api.PAY_FUYOU_PRIVATE_KEY');
  2105. // 生成的公钥
  2106. $publicPEMKey = env('api.PAY_FUYOU_PUBLIC_KEY');
  2107. $rsa->rsaPublicKey =$publicPEMKey;
  2108. $rsa->rsaPrivateKey = $privatePEMKey;
  2109. //私钥解密
  2110. if ($type=='decode') {
  2111. $decrypted = $rsa->privateDecryptRsa($data);
  2112. sr_log('4444444444'.'---'.$decrypted);
  2113. return $decrypted;
  2114. }
  2115. //公钥加密
  2116. if ($type=='encode') {
  2117. $decrypted = $rsa->publicEncryptRsa($data);
  2118. return $decrypted;
  2119. }
  2120. }