session_helper.php 20 KB

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