session_helper.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 16/4/17
  6. * Time: 下午6:46
  7. */
  8. require_once(BASE_HELPER_PATH . '/algorithm.php');
  9. require_once(BASE_HELPER_PATH . '/relation_helper.php');
  10. require_once(BASE_HELPER_PATH . '/account_helper.php');
  11. require_once(BASE_HELPER_PATH . '/push_helper.php');
  12. require_once(BASE_HELPER_PATH . '/login_helper.php');
  13. require_once(BASE_HELPER_PATH . '/bonus_helper.php');
  14. require_once(BASE_HELPER_PATH . '/util_helper.php');
  15. require_once(BASE_HELPER_PATH . '/url_helper.php');
  16. require_once(BASE_HELPER_PATH . '/mcard/mcard.php');
  17. require_once(BASE_HELPER_PATH . '/mtopcard/mtopcard.php');
  18. class session_helper
  19. {
  20. const mobile_login = 1;
  21. const wxopen_login = 2;
  22. const wxunion_login = 3;
  23. const mobile_len = 11;
  24. const device_ios = 1;
  25. const device_android = 2;
  26. const device_mini = 3;
  27. const device_wap = 4;
  28. public static function mobile_valid($mobile)
  29. {
  30. if (!isset($mobile) || empty($mobile) || strlen($mobile) < self::mobile_len) {
  31. return false;
  32. }
  33. $ret = preg_match('/^1(?:3[0-9]|5[012356789]|8[0256789]|7[0678])(-?)\d{4}\1\d{4}$/', $mobile, $arr);
  34. if ($ret == false || count($arr) <= 0) {
  35. return false;
  36. } else {
  37. $mobile = $arr[0];
  38. }
  39. $mobile = str_replace('-', '', $mobile);
  40. $mobile = substr($mobile, -self::mobile_len);
  41. $validate = new Validator();
  42. $validate->setValidate(Validator::verify_mobile($mobile));
  43. $err = $validate->validate();
  44. if (empty($err)) {
  45. return $mobile;
  46. } else {
  47. return false;
  48. }
  49. }
  50. public static function memberid() : int
  51. {
  52. return intval($_SESSION['member_id']);
  53. }
  54. public static function nickname()
  55. {
  56. if (isset($_SESSION['member_nickname']) && !empty($_SESSION['member_nickname'])) {
  57. return $_SESSION['member_nickname'];
  58. }
  59. if (array_key_exists('wx_author', $_SESSION)) {
  60. if (array_key_exists('handled', $_SESSION['wx_author'])) {
  61. $user_info = $_SESSION['wx_author']['user_info'];
  62. return $user_info['nickname'];
  63. }
  64. }
  65. if (isset($_SESSION['member_truename']) && !empty($_SESSION['member_truename'])) {
  66. return $_SESSION['member_truename'];
  67. }
  68. if (isset($_SESSION['member_mobile']) && !empty($_SESSION['member_mobile'])) {
  69. $mobile = $_SESSION['member_mobile'];
  70. if (strlen($mobile) == 11) {
  71. return substr_replace($mobile, '****', 3, 4);
  72. }
  73. }
  74. return '';
  75. }
  76. static public function logined()
  77. {
  78. return ($_SESSION['is_login'] == 1);
  79. }
  80. static public function isapp()
  81. {
  82. if (isset($_SESSION['is_app'])) {
  83. return ($_SESSION['is_app'] == true);
  84. } else {
  85. return false;
  86. }
  87. }
  88. static public function isVerfiyMobile()
  89. {
  90. if (self::logined()) {
  91. return true;
  92. } elseif (isset($_SESSION['member_mobile']) && !empty($_SESSION['member_mobile'])) {
  93. return true;
  94. } else {
  95. return false;
  96. }
  97. }
  98. static public function is_man()
  99. {
  100. return (intval($_SESSION['member_sex']) == 1);
  101. }
  102. static public function sex()
  103. {
  104. $sex = intval($_SESSION['member_sex']);
  105. if ($sex == 1) {
  106. return $sex;
  107. } else {
  108. return 0;
  109. }
  110. }
  111. static function avatar()
  112. {
  113. if (empty($_SESSION['member_avatar']))
  114. {
  115. if (array_key_exists('wx_author', $_SESSION)) {
  116. if (array_key_exists('handled', $_SESSION['wx_author'])) {
  117. $user_info = $_SESSION['wx_author']['user_info'];
  118. return $user_info['headimgurl'];
  119. }
  120. }
  121. if (self::is_man()) {
  122. $img = 'male.png';
  123. } else {
  124. $img = 'female.png';
  125. }
  126. return RESOURCE_SITE_URL . '/mobile/defimg/' . $img;
  127. }
  128. else {
  129. $path = $_SESSION['member_avatar'];
  130. if (util::ishttp($path)) {
  131. $url = $path;
  132. } else {
  133. $url = UPLOAD_SITE_URL . "/shop/avatar{$path}";
  134. }
  135. return $url;
  136. }
  137. }
  138. static public function qrcode()
  139. {
  140. $passwd = util::passwd;
  141. $member_id = self::memberid();
  142. $path = DS.ATTACH_MQRCODE.DS;
  143. $en_name = md5("{$member_id}.{$passwd}");
  144. $name = "{$en_name}.png";
  145. $filename = BASE_UPLOAD_PATH . $path . $name;
  146. if (!file_exists($filename)) {
  147. $url = url_helper::member_index($member_id);
  148. util::qrcode($url,$name);
  149. }
  150. $qurl = UPLOAD_SITE_URL . $path . $name;
  151. $tm = time();
  152. $qurl = "{$qurl}?tm={$tm}";
  153. return $qurl;
  154. }
  155. static public function isVip()
  156. {
  157. $member_id = self::memberid();
  158. return mcard\isVip($member_id);
  159. }
  160. static public function mobile()
  161. {
  162. if (self::isVerfiyMobile()) {
  163. return $_SESSION['member_mobile'];
  164. } else {
  165. return '';
  166. }
  167. }
  168. static public function parase_wxinfo($wxinfo)
  169. {
  170. $user = urldecode($wxinfo);
  171. if (empty($user)) return false;
  172. $user = json_decode($user, true);
  173. if ($user == false) return false;
  174. $info = [];
  175. $info['member_nickname'] = $user['nickname'];
  176. $info['member_avatar'] = $user['headimgurl'];
  177. $info['member_sex'] = intval($user['sex']);
  178. return $info;
  179. }
  180. static public function filter_info($info)
  181. {
  182. $member_info = [];
  183. if (isset($info['member_nickname']) && !empty($info['member_nickname'])) {
  184. $member_info['member_nickname'] = $info['member_nickname'];
  185. }
  186. if (isset($info['member_truename']) && !empty($info['member_truename'])) {
  187. if (!isset($member_info['member_nickname'])) {
  188. $member_info['member_nickname'] = $info['member_truename'];
  189. }
  190. $member_info['member_truename'] = $info['member_truename'];
  191. }
  192. if (isset($info['member_mobile']) && !empty($info['member_mobile'])) {
  193. $mobile = $info['member_mobile'];
  194. if (!isset($member_info['member_nickname'])) {
  195. $member_info['member_nickname'] = substr_replace($mobile, '****', 3, 4);
  196. }
  197. $member_info['member_mobile'] = $info['member_mobile'];
  198. }
  199. return $member_info;
  200. }
  201. static public function filter_member_info($member_info, $openid)
  202. {
  203. field_helper::validate_null_string($member_info, 'member_mobile,member_wxopenid,member_name,' .
  204. 'member_truename,member_signname,member_avatar,member_email,member_birthday');
  205. field_helper::validate_null_string($member_info, 'member_sex', '0');
  206. field_helper::validate_null_string($member_info, 'member_nickname', substr_replace($openid, '****', 3, 4));
  207. return $member_info;
  208. }
  209. static public function parse_contacts($contacts)
  210. {
  211. if (!isset($contacts)) {
  212. return false;
  213. }
  214. $contacts = json_decode(urldecode($contacts));
  215. if ($contacts == false) {
  216. return false;
  217. }
  218. $ar_contact = [];
  219. foreach ($contacts as $mobile)
  220. {
  221. $mobile = session_helper::mobile_valid($mobile);
  222. if ($mobile == false) {
  223. continue;
  224. }
  225. if (algorithm::bsearch($mobile, $ar_contact) != -1) {
  226. continue;
  227. }
  228. array_push($ar_contact, $mobile);
  229. sort($ar_contact);
  230. }
  231. return $ar_contact;
  232. }
  233. static public function session_id() {
  234. return empty($_SESSION['MPHPSESSID']) ? "" : $_SESSION['MPHPSESSID'];
  235. }
  236. static public function binded_mobile() {
  237. return intval($_SESSION['member_mobile_bind']) == 1;
  238. }
  239. static public function binded_wechat() {
  240. return intval($_SESSION['member_wechat_bind']) == 1;
  241. }
  242. static public function need_wechat_author()
  243. {
  244. wechat_helper::clear_origin_url();
  245. if (util::from_wechat() == false || session_helper::client_type() == session_helper::device_mini) {
  246. return false;
  247. }
  248. else
  249. {
  250. $relay_id = session_helper::relay_id();
  251. if (wechat_helper::has_userinfo())
  252. {
  253. $user_info = wechat_helper::get_userinfo();
  254. if(session_helper::logined())
  255. {
  256. $id_logginer = new login\memberid_log(session_helper::memberid());
  257. if(session_helper::binded_wechat())
  258. {
  259. $wechat_loginer = new login\open_userinfo_log($user_info);
  260. if($wechat_loginer->ismember()) {
  261. $wechat_loginer->login();
  262. }
  263. else {
  264. $id_logginer->bind($user_info);
  265. $id_logginer->login();
  266. }
  267. }
  268. else {
  269. $id_logginer->bind($user_info);
  270. $id_logginer->login();
  271. }
  272. }
  273. else
  274. {
  275. $wechat_loginer = new login\open_userinfo_log($user_info);
  276. if ($wechat_loginer->ismember()) {
  277. $wechat_loginer->login();
  278. }
  279. else {
  280. $wechat_loginer->register($user_info,$relay_id,session_helper::mobile());
  281. $wechat_loginer->login();
  282. account_helper::onRegister(session_helper::memberid(),$relay_id);
  283. wechat_helper::set_invite_bonus(); //如果用户从扫二维码来的,需要领红包
  284. }
  285. }
  286. account_helper::onLogin(session_helper::memberid());
  287. wechat_helper::clear_userinfo();
  288. return false;
  289. }
  290. elseif (session_helper::binded_wechat())
  291. {
  292. $author_time = $_SESSION['wxauthor_time'];
  293. $max_time = 86400;
  294. if (time() - $author_time > $max_time) {
  295. return true;
  296. } else {
  297. return false;
  298. }
  299. }
  300. else {
  301. return true;
  302. }
  303. }
  304. }
  305. static public function pub_openid()
  306. {
  307. if (empty($_SESSION['member_wxopenid'])) {
  308. return false;
  309. } else {
  310. return $_SESSION['member_wxopenid'];
  311. }
  312. }
  313. static public function unionid()
  314. {
  315. if (!empty($_SESSION['member_wxunionid'])) {
  316. return $_SESSION['member_wxunionid'];
  317. }
  318. if (array_key_exists('wx_author', $_SESSION))
  319. {
  320. if (array_key_exists('handled', $_SESSION['wx_author'])) {
  321. $user_info = $_SESSION['wx_author']['user_info'];
  322. return $user_info['unionid'];
  323. }
  324. }
  325. return false;
  326. }
  327. static public function thief($fromid, &$err)
  328. {
  329. $thief = new bonus\thief_vilator($fromid);
  330. return $thief->thief($err);
  331. }
  332. static public function first_order()
  333. {
  334. if (array_key_exists('order_num', $_SESSION)) {
  335. $order_num = $_SESSION['order_num'];
  336. }
  337. else {
  338. $mod_member = Model('member');
  339. $minfo = $mod_member->getMemberInfoByID(self::memberid());
  340. if (empty($minfo)) return false;
  341. $order_num = intval($minfo['order_num']);
  342. if ($order_num > 0) {
  343. $_SESSION['order_num'] = $order_num;
  344. }
  345. }
  346. if ($order_num == 0) {
  347. return true;
  348. } else {
  349. return false;
  350. }
  351. }
  352. static public function can_send()
  353. {
  354. if(noBonusRate()) return true;
  355. if (array_key_exists('order_num', $_SESSION)) {
  356. $order_num = $_SESSION['order_num'];
  357. }
  358. else
  359. {
  360. $mod_member = Model('member');
  361. $minfo = $mod_member->getMemberInfoByID(self::memberid());
  362. if (empty($minfo)) return false;
  363. $order_num = intval($minfo['order_num']);
  364. if ($order_num > 0) {
  365. $_SESSION['order_num'] = $order_num;
  366. }
  367. }
  368. return $order_num > 0;
  369. }
  370. static public function add_order()
  371. {
  372. if (!array_key_exists('order_num', $_SESSION)) {
  373. $mod_member = Model('member');
  374. $mod_member->editMember(['member_id' => self::memberid()],['order_num' => ['exp', 'order_num+1'],'lastest_order' => time()]);
  375. $minfo = $mod_member->getMemberInfoByID(self::memberid());
  376. $_SESSION['order_num'] = intval($minfo['order_num']);
  377. } else {
  378. $_SESSION['order_num'] += 1;
  379. }
  380. }
  381. public static function share_id()
  382. {
  383. if(self::logined()) {
  384. return self::memberid();
  385. }
  386. elseif(array_key_exists('relay_id',$_SESSION)) {
  387. return $_SESSION['relay_id'];
  388. } else {
  389. return 0;
  390. }
  391. }
  392. public static function relay_id()
  393. {
  394. if(array_key_exists('relay_id',$_SESSION)) {
  395. return $_SESSION['relay_id'];
  396. } else {
  397. return 0;
  398. }
  399. }
  400. public static function set_relay($relay_id)
  401. {
  402. $relay_id = intval($relay_id);
  403. if($relay_id > 0) {
  404. $_SESSION['relay_id'] = $relay_id;
  405. }
  406. }
  407. public static function client_type()
  408. {
  409. if($_SESSION['client_type'] == 'android') {
  410. return self::device_android;
  411. }
  412. elseif($_SESSION['client_type'] == 'ios') {
  413. return self::device_ios;
  414. }
  415. elseif($_SESSION['client_type'] == 'mini') {
  416. return self::device_mini;
  417. }
  418. elseif($_SESSION['client_type'] == 'ajax' || $_SESSION['client_type'] == 'wap') {
  419. return self::device_wap;
  420. }
  421. else {
  422. return 0;
  423. }
  424. }
  425. public static function version_code()
  426. {
  427. if(self::client_type() == self::device_android) {
  428. return $_SESSION['client_version'];
  429. }
  430. elseif(self::client_type() == self::device_ios) {
  431. return $_SESSION['client_version'] * 100;
  432. }
  433. elseif(self::client_type() == self::device_mini) {
  434. return intval($_SESSION['client_version']);
  435. }
  436. else {
  437. return 0;
  438. }
  439. }
  440. //缓存数据给个人页面使用
  441. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  442. static public function address_num()
  443. {
  444. if(array_key_exists('address_num',$_SESSION)) {
  445. return $_SESSION['address_num'];
  446. }
  447. else
  448. {
  449. $mod_addr = Model('address');
  450. $_SESSION['address_num'] = $mod_addr->getAddressCount(['member_id' => self::memberid()]);
  451. return $_SESSION['address_num'];
  452. }
  453. }
  454. static public function clear_addr()
  455. {
  456. if (array_key_exists('address_num', $_SESSION)) {
  457. unset($_SESSION['address_num']);
  458. }
  459. }
  460. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  461. static public function favorate_num()
  462. {
  463. if(array_key_exists('favorates_num',$_SESSION)) {
  464. return $_SESSION['favorates_num'];
  465. }
  466. else
  467. {
  468. $mod_favorites = Model('favorites');
  469. $_SESSION['favorates_num'] = $mod_favorites->getFavoritesCount(['member_id' => self::memberid()]);
  470. return $_SESSION['favorates_num'];
  471. }
  472. }
  473. static public function clear_favorate()
  474. {
  475. if (array_key_exists('favorates_num', $_SESSION)) {
  476. unset($_SESSION['favorates_num']);
  477. }
  478. }
  479. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  480. static public function fcode_num()
  481. {
  482. $mod_favorites = Model('goods_fcode');
  483. $_SESSION['fcode_num'] = $mod_favorites->getUsableFcodeCount(self::mobile());
  484. return $_SESSION['fcode_num'];
  485. }
  486. static public function clear_fcode()
  487. {
  488. if (array_key_exists('fcode_num', $_SESSION)) {
  489. unset($_SESSION['fcode_num']);
  490. }
  491. }
  492. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  493. static public function has_regmobile() {
  494. return !empty(self::get_regmobile());
  495. }
  496. static public function get_regmobile()
  497. {
  498. if(array_key_exists('regmobile',$_SESSION)) {
  499. return $_SESSION['regmobile'];
  500. } else {
  501. return "";
  502. }
  503. }
  504. static public function set_regmobile($mobile) {
  505. $_SESSION['regmobile'] = $mobile;
  506. }
  507. static public function clear_regmobile()
  508. {
  509. if(array_key_exists('regmobile',$_SESSION)) {
  510. unset($_SESSION['regmobile']);
  511. }
  512. }
  513. }
  514. class wechat_helper
  515. {
  516. static public function has_userinfo() {
  517. return !empty(self::get_userinfo());
  518. }
  519. static public function get_userinfo()
  520. {
  521. if(array_key_exists('wx_author',$_SESSION)) {
  522. return $_SESSION['wx_author']['user_info'];
  523. } else {
  524. return "";
  525. }
  526. }
  527. static public function set_userinfo($userinfo) {
  528. $_SESSION['wx_author']['user_info'] = $userinfo;
  529. }
  530. static public function clear_userinfo()
  531. {
  532. if(array_key_exists('wx_author',$_SESSION)) {
  533. unset($_SESSION['wx_author']);
  534. }
  535. }
  536. static public function has_origin_url()
  537. {
  538. return array_key_exists('author_orgin_url',$_SESSION);
  539. }
  540. static public function set_origin_url($url) {
  541. $_SESSION['author_orgin_url'] = $url;
  542. }
  543. static public function get_origin_url() {
  544. if(array_key_exists('author_orgin_url',$_SESSION)) {
  545. return $_SESSION['author_orgin_url'];
  546. } else {
  547. return "";
  548. }
  549. }
  550. static public function clear_origin_url()
  551. {
  552. if(array_key_exists('author_orgin_url',$_SESSION)) {
  553. unset($_SESSION['author_orgin_url']);
  554. }
  555. }
  556. static public function set_invite_bonus()
  557. {
  558. $_SESSION['invite_bonus_qrcode'] = true;
  559. }
  560. static public function clear_invite_bonus()
  561. {
  562. if(array_key_exists('invite_bonus_qrcode',$_SESSION)) {
  563. unset($_SESSION['invite_bonus_qrcode']);
  564. }
  565. }
  566. static public function invite_bonus_tag()
  567. {
  568. if(array_key_exists('invite_bonus_qrcode',$_SESSION)) {
  569. return $_SESSION['invite_bonus_qrcode'];
  570. }
  571. else {
  572. return false;
  573. }
  574. }
  575. static public function setmini_sesskey($sesskey) {
  576. $_SESSION['mini_sesskey'] = $sesskey;
  577. }
  578. static public function mini_sesskey() {
  579. return $_SESSION['mini_sesskey'];
  580. }
  581. }