session_helper.php 19 KB

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