session_helper.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  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. static public function mobile_valid($mobile)
  26. {
  27. if (!isset($mobile) || empty($mobile) || strlen($mobile) < self::mobile_len) {
  28. return false;
  29. }
  30. $ret = preg_match('/^1(?:3[0-9]|5[012356789]|8[0256789]|7[0678])(-?)\d{4}\1\d{4}$/', $mobile, $arr);
  31. if ($ret == false || count($arr) <= 0) {
  32. return false;
  33. } else {
  34. $mobile = $arr[0];
  35. }
  36. $mobile = str_replace('-', '', $mobile);
  37. $mobile = substr($mobile, -self::mobile_len);
  38. $validate = new Validator();
  39. $validate->setValidate(Validator::verify_mobile($mobile));
  40. $err = $validate->validate();
  41. if (empty($err)) {
  42. return $mobile;
  43. } else {
  44. return false;
  45. }
  46. }
  47. static public function memberid()
  48. {
  49. return intval($_SESSION['member_id']);
  50. }
  51. static public function nickname()
  52. {
  53. if (isset($_SESSION['member_nickname']) && !empty($_SESSION['member_nickname'])) {
  54. return $_SESSION['member_nickname'];
  55. }
  56. if (array_key_exists('wx_author', $_SESSION)) {
  57. if (array_key_exists('handled', $_SESSION['wx_author'])) {
  58. $user_info = $_SESSION['wx_author']['user_info'];
  59. return $user_info['nickname'];
  60. }
  61. }
  62. if (isset($_SESSION['member_truename']) && !empty($_SESSION['member_truename'])) {
  63. return $_SESSION['member_truename'];
  64. }
  65. if (isset($_SESSION['member_mobile']) && !empty($_SESSION['member_mobile'])) {
  66. $mobile = $_SESSION['member_mobile'];
  67. if (strlen($mobile) == 11) {
  68. return substr_replace($mobile, '****', 3, 4);
  69. }
  70. }
  71. return '';
  72. }
  73. static public function logined()
  74. {
  75. return ($_SESSION['is_login'] == 1);
  76. }
  77. static public function isapp()
  78. {
  79. if (isset($_SESSION['is_app'])) {
  80. return ($_SESSION['is_app'] == true);
  81. } else {
  82. return false;
  83. }
  84. }
  85. static public function isVerfiyMobile()
  86. {
  87. if (self::logined()) {
  88. return true;
  89. } else {
  90. if (isset($_SESSION['member_mobile']) && !empty($_SESSION['member_mobile'])) {
  91. return true;
  92. } else {
  93. return false;
  94. }
  95. }
  96. }
  97. static public function is_man()
  98. {
  99. return (intval($_SESSION['member_sex']) == 1);
  100. }
  101. static public function sex()
  102. {
  103. $sex = intval($_SESSION['member_sex']);
  104. if ($sex == 1) {
  105. return $sex;
  106. } else {
  107. return 0;
  108. }
  109. }
  110. static function avatar()
  111. {
  112. if (empty($_SESSION['member_avatar'])) {
  113. if (array_key_exists('wx_author', $_SESSION)) {
  114. if (array_key_exists('handled', $_SESSION['wx_author'])) {
  115. $user_info = $_SESSION['wx_author']['user_info'];
  116. return $user_info['headimgurl'];
  117. }
  118. }
  119. if (self::is_man()) {
  120. $img = 'male.png';
  121. } else {
  122. $img = 'female.png';
  123. }
  124. return RESOURCE_SITE_URL . '/mobile/defimg/' . $img;
  125. } else {
  126. $path = $_SESSION['member_avatar'];
  127. if (util::ishttp($path)) {
  128. $url = $path;
  129. } else {
  130. $url = UPLOAD_SITE_URL . "/shop/avatar{$path}";
  131. }
  132. return $url;
  133. }
  134. }
  135. static public function qrcode()
  136. {
  137. $passwd = util::passwd;
  138. $member_id = self::memberid();
  139. $path = DS.ATTACH_MQRCODE.DS;
  140. $en_name = md5("{$member_id}.{$passwd}");
  141. $name = "{$en_name}.png";
  142. $filename = BASE_UPLOAD_PATH . $path . $name;
  143. if (!file_exists($filename)) {
  144. $url = url_helper::member_index($member_id);
  145. util::qrcode($url,$name);
  146. }
  147. $qurl = UPLOAD_SITE_URL . $path . $name;
  148. $tm = time();
  149. $qurl = "{$qurl}?tm={$tm}";
  150. return $qurl;
  151. }
  152. static public function mobile()
  153. {
  154. if (self::isVerfiyMobile()) {
  155. return $_SESSION['member_mobile'];
  156. } else {
  157. return '';
  158. }
  159. }
  160. static public function parase_wxinfo($wxinfo)
  161. {
  162. $user = urldecode($wxinfo);
  163. if (empty($user)) return false;
  164. $user = json_decode($user, true);
  165. if ($user == false) return false;
  166. $info = array();
  167. $info['member_nickname'] = $user['nickname'];
  168. $info['member_avatar'] = $user['headimgurl'];
  169. $info['member_sex'] = intval($user['sex']);
  170. return $info;
  171. }
  172. static public function filter_info($info)
  173. {
  174. $member_info = array();
  175. if (isset($info['member_nickname']) && !empty($info['member_nickname'])) {
  176. $member_info['member_nickname'] = $info['member_nickname'];
  177. }
  178. if (isset($info['member_truename']) && !empty($info['member_truename'])) {
  179. if (!isset($member_info['member_nickname'])) {
  180. $member_info['member_nickname'] = $info['member_truename'];
  181. }
  182. $member_info['member_truename'] = $info['member_truename'];
  183. }
  184. if (isset($info['member_mobile']) && !empty($info['member_mobile'])) {
  185. $mobile = $info['member_mobile'];
  186. if (!isset($member_info['member_nickname'])) {
  187. $member_info['member_nickname'] = substr_replace($mobile, '****', 3, 4);
  188. }
  189. $member_info['member_mobile'] = $info['member_mobile'];
  190. }
  191. return $member_info;
  192. }
  193. static public function filter_member_info($member_info, $openid)
  194. {
  195. field_helper::validate_null_string($member_info, 'member_mobile,member_wxopenid,member_name,' .
  196. 'member_truename,member_signname,member_avatar,member_email,member_birthday');
  197. field_helper::validate_null_string($member_info, 'member_sex', '0');
  198. field_helper::validate_null_string($member_info, 'member_nickname', substr_replace($openid, '****', 3, 4));
  199. return $member_info;
  200. }
  201. static public function parse_contacts($contacts)
  202. {
  203. if (!isset($contacts)) {
  204. return false;
  205. }
  206. $contacts = json_decode(urldecode($contacts));
  207. if ($contacts == false) {
  208. return false;
  209. }
  210. $ar_contact = array();
  211. foreach ($contacts as $mobile)
  212. {
  213. $mobile = session_helper::mobile_valid($mobile);
  214. if ($mobile == false) {
  215. continue;
  216. }
  217. if (algorithm::bsearch($mobile, $ar_contact) != -1) {
  218. continue;
  219. }
  220. array_push($ar_contact, $mobile);
  221. sort($ar_contact);
  222. }
  223. return $ar_contact;
  224. }
  225. static public function session_id() {
  226. return empty($_SESSION['MPHPSESSID']) ? "" : $_SESSION['MPHPSESSID'];
  227. }
  228. static public function binded_mobile() {
  229. return intval($_SESSION['member_mobile_bind']) == 1;
  230. }
  231. static public function binded_wechat() {
  232. return intval($_SESSION['member_wechat_bind']) == 1;
  233. }
  234. static public function need_wechat_author()
  235. {
  236. wechat_helper::clear_origin_url();
  237. if (util::from_wechat() == false || session_helper::client_type() == session_helper::device_mini) {
  238. return false;
  239. }
  240. else
  241. {
  242. $relay_id = session_helper::relay_id();
  243. if (wechat_helper::has_userinfo())
  244. {
  245. $user_info = wechat_helper::get_userinfo();
  246. if(session_helper::logined())
  247. {
  248. $id_logginer = new login\memberid_log(session_helper::memberid());
  249. if(session_helper::binded_wechat())
  250. {
  251. $wechat_loginer = new login\open_userinfo_log($user_info);
  252. if($wechat_loginer->ismember()) {
  253. $wechat_loginer->login();
  254. }
  255. else {
  256. $id_logginer->unbind_wechat();
  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. else {
  418. return 0;
  419. }
  420. }
  421. static public function version_code()
  422. {
  423. if(self::client_type() == self::device_android) {
  424. return $_SESSION['client_version'];
  425. }
  426. elseif(self::client_type() == self::device_ios) {
  427. return $_SESSION['client_version'] * 100;
  428. }
  429. else {
  430. return 0;
  431. }
  432. }
  433. //缓存数据给个人页面使用
  434. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  435. static public function address_num()
  436. {
  437. if(array_key_exists('address_num',$_SESSION)) {
  438. return $_SESSION['address_num'];
  439. }
  440. else
  441. {
  442. $mod_addr = Model('address');
  443. $_SESSION['address_num'] = $mod_addr->getAddressCount(['member_id' => self::memberid()]);
  444. return $_SESSION['address_num'];
  445. }
  446. }
  447. static public function clear_addr()
  448. {
  449. if (array_key_exists('address_num', $_SESSION)) {
  450. unset($_SESSION['address_num']);
  451. }
  452. }
  453. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  454. static public function favorate_num()
  455. {
  456. if(array_key_exists('favorates_num',$_SESSION)) {
  457. return $_SESSION['favorates_num'];
  458. }
  459. else
  460. {
  461. $mod_favorites = Model('favorites');
  462. $_SESSION['favorates_num'] = $mod_favorites->getFavoritesCount(['member_id' => self::memberid()]);
  463. return $_SESSION['favorates_num'];
  464. }
  465. }
  466. static public function clear_favorate()
  467. {
  468. if (array_key_exists('favorates_num', $_SESSION)) {
  469. unset($_SESSION['favorates_num']);
  470. }
  471. }
  472. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  473. static public function fcode_num()
  474. {
  475. $mod_favorites = Model('goods_fcode');
  476. $_SESSION['fcode_num'] = $mod_favorites->getUsableFcodeCount(self::mobile());
  477. return $_SESSION['fcode_num'];
  478. }
  479. static public function clear_fcode()
  480. {
  481. if (array_key_exists('fcode_num', $_SESSION)) {
  482. unset($_SESSION['fcode_num']);
  483. }
  484. }
  485. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  486. static public function has_regmobile() {
  487. return !empty(self::get_regmobile());
  488. }
  489. static public function get_regmobile()
  490. {
  491. if(array_key_exists('regmobile',$_SESSION)) {
  492. return $_SESSION['regmobile'];
  493. } else {
  494. return "";
  495. }
  496. }
  497. static public function set_regmobile($mobile) {
  498. $_SESSION['regmobile'] = $mobile;
  499. }
  500. static public function clear_regmobile()
  501. {
  502. if(array_key_exists('regmobile',$_SESSION)) {
  503. unset($_SESSION['regmobile']);
  504. }
  505. }
  506. }
  507. class wechat_helper
  508. {
  509. static public function has_userinfo() {
  510. return !empty(self::get_userinfo());
  511. }
  512. static public function get_userinfo()
  513. {
  514. if(array_key_exists('wx_author',$_SESSION)) {
  515. return $_SESSION['wx_author']['user_info'];
  516. } else {
  517. return "";
  518. }
  519. }
  520. static public function set_userinfo($userinfo) {
  521. $_SESSION['wx_author']['user_info'] = $userinfo;
  522. }
  523. static public function clear_userinfo()
  524. {
  525. if(array_key_exists('wx_author',$_SESSION)) {
  526. unset($_SESSION['wx_author']);
  527. }
  528. }
  529. static public function has_origin_url()
  530. {
  531. return array_key_exists('author_orgin_url',$_SESSION);
  532. }
  533. static public function set_origin_url($url) {
  534. $_SESSION['author_orgin_url'] = $url;
  535. }
  536. static public function get_origin_url() {
  537. if(array_key_exists('author_orgin_url',$_SESSION)) {
  538. return $_SESSION['author_orgin_url'];
  539. } else {
  540. return "";
  541. }
  542. }
  543. static public function clear_origin_url()
  544. {
  545. if(array_key_exists('author_orgin_url',$_SESSION)) {
  546. unset($_SESSION['author_orgin_url']);
  547. }
  548. }
  549. static public function set_invite_bonus()
  550. {
  551. $_SESSION['invite_bonus_qrcode'] = true;
  552. }
  553. static public function clear_invite_bonus()
  554. {
  555. if(array_key_exists('invite_bonus_qrcode',$_SESSION)) {
  556. unset($_SESSION['invite_bonus_qrcode']);
  557. }
  558. }
  559. static public function invite_bonus_tag()
  560. {
  561. if(array_key_exists('invite_bonus_qrcode',$_SESSION)) {
  562. return $_SESSION['invite_bonus_qrcode'];
  563. }
  564. else {
  565. return false;
  566. }
  567. }
  568. static public function setmini_sesskey($sesskey) {
  569. $_SESSION['mini_sesskey'] = $sesskey;
  570. }
  571. static public function mini_sesskey() {
  572. return $_SESSION['mini_sesskey'];
  573. }
  574. }