session_helper.php 19 KB

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