session_helper.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  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 (wechat_helper::has_userinfo())
  240. {
  241. $user_info = wechat_helper::userinfo();
  242. if(session_helper::logined())
  243. {
  244. $id_logginer = new login\memberid_log(session_helper::memberid());
  245. if(session_helper::binded_wechat())
  246. {
  247. $wechat_loginer = new login\open_userinfo_log($user_info);
  248. if($wechat_loginer->ismember()) {
  249. $wechat_loginer->login();
  250. }
  251. else {
  252. $id_logginer->unbind_wechat();
  253. $id_logginer->bind($user_info);
  254. $id_logginer->login();
  255. }
  256. }
  257. else {
  258. $id_logginer->bind($user_info);
  259. $id_logginer->login();
  260. }
  261. }
  262. else
  263. {
  264. $wechat_loginer = new login\open_userinfo_log($user_info);
  265. if ($wechat_loginer->ismember()) {
  266. $wechat_loginer->login();
  267. }
  268. else {
  269. $wechat_loginer->register($user_info,$relay_id,session_helper::mobile());
  270. $wechat_loginer->login();
  271. account_helper::onRegister(session_helper::memberid(),$relay_id);
  272. wechat_helper::set_invite_bonus(); //如果用户从扫二维码来的,需要领红包
  273. }
  274. }
  275. account_helper::onLogin(session_helper::memberid());
  276. wechat_helper::clear_wxinfo();
  277. return false;
  278. }
  279. else
  280. {
  281. if (session_helper::binded_wechat())
  282. {
  283. $author_time = $_SESSION['wxauthor_time'];
  284. $max_time = 86400;
  285. if (time() - $author_time > $max_time) {
  286. return true;
  287. } else {
  288. return false;
  289. }
  290. }
  291. else {
  292. return true;
  293. }
  294. }
  295. }
  296. }
  297. static public function pub_openid()
  298. {
  299. if (empty($_SESSION['member_wxopenid'])) {
  300. return false;
  301. } else {
  302. return $_SESSION['member_wxopenid'];
  303. }
  304. }
  305. static public function unionid()
  306. {
  307. if (!empty($_SESSION['member_wxunionid'])) {
  308. return $_SESSION['member_wxunionid'];
  309. }
  310. if (array_key_exists('wx_author', $_SESSION))
  311. {
  312. if (array_key_exists('handled', $_SESSION['wx_author'])) {
  313. $user_info = $_SESSION['wx_author']['user_info'];
  314. return $user_info['unionid'];
  315. }
  316. }
  317. return false;
  318. }
  319. static public function thief($fromid, &$err)
  320. {
  321. $thief = new bonus\thief_vilator($fromid);
  322. return $thief->thief($err);
  323. }
  324. static public function first_order()
  325. {
  326. if (array_key_exists('order_num', $_SESSION)) {
  327. $order_num = $_SESSION['order_num'];
  328. }
  329. else {
  330. $mod_member = Model('member');
  331. $minfo = $mod_member->getMemberInfoByID(self::memberid());
  332. if (empty($minfo)) return false;
  333. $order_num = intval($minfo['order_num']);
  334. if ($order_num > 0) {
  335. $_SESSION['order_num'] = $order_num;
  336. }
  337. }
  338. if ($order_num == 0) {
  339. return true;
  340. } else {
  341. return false;
  342. }
  343. }
  344. static public function can_send()
  345. {
  346. if(noBonusRate()) return true;
  347. if (array_key_exists('order_num', $_SESSION)) {
  348. $order_num = $_SESSION['order_num'];
  349. }
  350. else
  351. {
  352. $mod_member = Model('member');
  353. $minfo = $mod_member->getMemberInfoByID(self::memberid());
  354. if (empty($minfo)) return false;
  355. $order_num = intval($minfo['order_num']);
  356. if ($order_num > 0) {
  357. $_SESSION['order_num'] = $order_num;
  358. }
  359. }
  360. return $order_num > 0;
  361. }
  362. static public function add_order()
  363. {
  364. if (!array_key_exists('order_num', $_SESSION)) {
  365. $mod_member = Model('member');
  366. $mod_member->editMember(['member_id' => self::memberid()],['order_num' => array('exp', 'order_num+1'),'lastest_order' => time()]);
  367. $minfo = $mod_member->getMemberInfoByID(self::memberid());
  368. $_SESSION['order_num'] = intval($minfo['order_num']);
  369. } else {
  370. $_SESSION['order_num'] += 1;
  371. }
  372. }
  373. static public function share_id()
  374. {
  375. if(self::logined()) {
  376. return self::memberid();
  377. }
  378. else
  379. {
  380. if(array_key_exists('relay_id',$_SESSION)) {
  381. return $_SESSION['relay_id'];
  382. } else {
  383. return 0;
  384. }
  385. }
  386. }
  387. static public function relay_id()
  388. {
  389. if(array_key_exists('relay_id',$_SESSION)) {
  390. return $_SESSION['relay_id'];
  391. } else {
  392. return 0;
  393. }
  394. }
  395. static public function set_relay($relay_id)
  396. {
  397. $relay_id = intval($relay_id);
  398. if($relay_id > 0) {
  399. $_SESSION['relay_id'] = $relay_id;
  400. }
  401. }
  402. static public function client_type()
  403. {
  404. if($_SESSION['client_type'] == 'android') {
  405. return self::device_android;
  406. }
  407. elseif($_SESSION['client_type'] == 'ios') {
  408. return self::device_ios;
  409. }
  410. else {
  411. return 0;
  412. }
  413. }
  414. static public function version_code()
  415. {
  416. if(self::client_type() == self::device_android) {
  417. return $_SESSION['client_version'];
  418. }
  419. elseif(self::client_type() == self::device_ios) {
  420. return $_SESSION['client_version'] * 100;
  421. }
  422. else {
  423. return 0;
  424. }
  425. }
  426. //缓存数据给个人页面使用
  427. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  428. static public function address_num()
  429. {
  430. if(array_key_exists('address_num',$_SESSION)) {
  431. return $_SESSION['address_num'];
  432. }
  433. else
  434. {
  435. $mod_addr = Model('address');
  436. $_SESSION['address_num'] = $mod_addr->getAddressCount(['member_id' => self::memberid()]);
  437. return $_SESSION['address_num'];
  438. }
  439. }
  440. static public function clear_addr()
  441. {
  442. if (array_key_exists('address_num', $_SESSION)) {
  443. unset($_SESSION['address_num']);
  444. }
  445. }
  446. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  447. static public function favorate_num()
  448. {
  449. if(array_key_exists('favorates_num',$_SESSION)) {
  450. return $_SESSION['favorates_num'];
  451. }
  452. else
  453. {
  454. $mod_favorites = Model('favorites');
  455. $_SESSION['favorates_num'] = $mod_favorites->getFavoritesCount(['member_id' => self::memberid()]);
  456. return $_SESSION['favorates_num'];
  457. }
  458. }
  459. static public function clear_favorate()
  460. {
  461. if (array_key_exists('favorates_num', $_SESSION)) {
  462. unset($_SESSION['favorates_num']);
  463. }
  464. }
  465. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  466. static public function fcode_num()
  467. {
  468. $mod_favorites = Model('goods_fcode');
  469. $_SESSION['fcode_num'] = $mod_favorites->getUsableFcodeCount(self::mobile());
  470. return $_SESSION['fcode_num'];
  471. }
  472. static public function clear_fcode()
  473. {
  474. if (array_key_exists('fcode_num', $_SESSION)) {
  475. unset($_SESSION['fcode_num']);
  476. }
  477. }
  478. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  479. static public function has_regmobile() {
  480. return !empty(self::get_regmobile());
  481. }
  482. static public function get_regmobile()
  483. {
  484. if(array_key_exists('regmobile',$_SESSION)) {
  485. return $_SESSION['regmobile'];
  486. } else {
  487. return "";
  488. }
  489. }
  490. static public function set_regmobile($mobile) {
  491. $_SESSION['regmobile'] = $mobile;
  492. }
  493. static public function clear_regmobile()
  494. {
  495. if(array_key_exists('regmobile',$_SESSION)) {
  496. unset($_SESSION['regmobile']);
  497. }
  498. }
  499. }
  500. class wechat_helper
  501. {
  502. static public function has_userinfo() {
  503. return !empty(self::userinfo());
  504. }
  505. static public function userinfo()
  506. {
  507. if(array_key_exists('wx_author',$_SESSION)) {
  508. return $_SESSION['wx_author']['user_info'];
  509. } else {
  510. return "";
  511. }
  512. }
  513. static public function set_userinfo($userinfo) {
  514. $_SESSION['wx_author']['user_info'] = $userinfo;
  515. }
  516. static public function clear_wxinfo()
  517. {
  518. if(array_key_exists('wx_author',$_SESSION)) {
  519. unset($_SESSION['wx_author']);
  520. }
  521. }
  522. static public function has_origin_url()
  523. {
  524. return array_key_exists('author_orgin_url',$_SESSION);
  525. }
  526. static public function set_origin_url($url) {
  527. $_SESSION['author_orgin_url'] = $url;
  528. }
  529. static public function get_origin_url() {
  530. if(array_key_exists('author_orgin_url',$_SESSION)) {
  531. return $_SESSION['author_orgin_url'];
  532. } else {
  533. return "";
  534. }
  535. }
  536. static public function clear_origin_url()
  537. {
  538. if(array_key_exists('author_orgin_url',$_SESSION)) {
  539. unset($_SESSION['author_orgin_url']);
  540. }
  541. }
  542. static public function set_invite_bonus()
  543. {
  544. $_SESSION['invite_bonus_qrcode'] = true;
  545. }
  546. static public function clear_invite_bonus()
  547. {
  548. if(array_key_exists('invite_bonus_qrcode',$_SESSION)) {
  549. unset($_SESSION['invite_bonus_qrcode']);
  550. }
  551. }
  552. static public function invite_bonus_tag()
  553. {
  554. if(array_key_exists('invite_bonus_qrcode',$_SESSION)) {
  555. return $_SESSION['invite_bonus_qrcode'];
  556. }
  557. else {
  558. return false;
  559. }
  560. }
  561. }