session_helper.php 18 KB

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