session_helper.php 18 KB

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