123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640 |
- <?php
- /**
- * Created by PhpStorm.
- * User: stanley-king
- * Date: 16/4/17
- * Time: 下午6:46
- */
- require_once(BASE_HELPER_PATH . '/algorithm.php');
- require_once(BASE_HELPER_PATH . '/relation_helper.php');
- require_once(BASE_HELPER_PATH . '/account_helper.php');
- require_once(BASE_HELPER_PATH . '/push_helper.php');
- require_once(BASE_HELPER_PATH . '/login_helper.php');
- require_once(BASE_HELPER_PATH . '/bonus_helper.php');
- require_once(BASE_HELPER_PATH . '/util_helper.php');
- require_once(BASE_HELPER_PATH . '/url_helper.php');
- require_once(BASE_HELPER_PATH . '/mcard/mcard.php');
- require_once(BASE_HELPER_PATH . '/mtopcard/mtopcard.php');
- class session_helper
- {
- const mobile_login = 1;
- const wxopen_login = 2;
- const wxunion_login = 3;
- const mobile_len = 11;
- const device_ios = 1;
- const device_android = 2;
- const device_mini = 3;
- const device_wap = 4;
- public static function mobile_valid($mobile)
- {
- if (!isset($mobile) || empty($mobile) || strlen($mobile) < self::mobile_len) {
- return false;
- }
- $ret = preg_match('/^1(?:3[0-9]|5[012356789]|8[0256789]|7[0678])(-?)\d{4}\1\d{4}$/', $mobile, $arr);
- if ($ret == false || count($arr) <= 0) {
- return false;
- } else {
- $mobile = $arr[0];
- }
- $mobile = str_replace('-', '', $mobile);
- $mobile = substr($mobile, -self::mobile_len);
- $validate = new Validator();
- $validate->setValidate(Validator::verify_mobile($mobile));
- $err = $validate->validate();
- if (empty($err)) {
- return $mobile;
- } else {
- return false;
- }
- }
- public static function memberid() : int
- {
- return intval($_SESSION['member_id']);
- }
- public static function nickname()
- {
- if (isset($_SESSION['member_nickname']) && !empty($_SESSION['member_nickname'])) {
- return $_SESSION['member_nickname'];
- }
- if (array_key_exists('wx_author', $_SESSION)) {
- if (array_key_exists('handled', $_SESSION['wx_author'])) {
- $user_info = $_SESSION['wx_author']['user_info'];
- return $user_info['nickname'];
- }
- }
- if (isset($_SESSION['member_truename']) && !empty($_SESSION['member_truename'])) {
- return $_SESSION['member_truename'];
- }
- if (isset($_SESSION['member_mobile']) && !empty($_SESSION['member_mobile'])) {
- $mobile = $_SESSION['member_mobile'];
- if (strlen($mobile) == 11) {
- return substr_replace($mobile, '****', 3, 4);
- }
- }
- return '';
- }
- static public function logined()
- {
- return ($_SESSION['is_login'] == 1);
- }
- static public function isapp()
- {
- if (isset($_SESSION['is_app'])) {
- return ($_SESSION['is_app'] == true);
- } else {
- return false;
- }
- }
- static public function isVerfiyMobile()
- {
- if (self::logined()) {
- return true;
- } elseif (isset($_SESSION['member_mobile']) && !empty($_SESSION['member_mobile'])) {
- return true;
- } else {
- return false;
- }
- }
- static public function is_man()
- {
- return (intval($_SESSION['member_sex']) == 1);
- }
- static public function sex()
- {
- $sex = intval($_SESSION['member_sex']);
- if ($sex == 1) {
- return $sex;
- } else {
- return 0;
- }
- }
- static function avatar()
- {
- if (empty($_SESSION['member_avatar']))
- {
- if (array_key_exists('wx_author', $_SESSION)) {
- if (array_key_exists('handled', $_SESSION['wx_author'])) {
- $user_info = $_SESSION['wx_author']['user_info'];
- return $user_info['headimgurl'];
- }
- }
- if (self::is_man()) {
- $img = 'male.png';
- } else {
- $img = 'female.png';
- }
- return RESOURCE_SITE_URL . '/mobile/defimg/' . $img;
- }
- else {
- $path = $_SESSION['member_avatar'];
- if (util::ishttp($path)) {
- $url = $path;
- } else {
- $url = UPLOAD_SITE_URL . "/shop/avatar{$path}";
- }
- return $url;
- }
- }
- static public function qrcode()
- {
- $passwd = util::passwd;
- $member_id = self::memberid();
- $path = DS.ATTACH_MQRCODE.DS;
- $en_name = md5("{$member_id}.{$passwd}");
- $name = "{$en_name}.png";
- $filename = BASE_UPLOAD_PATH . $path . $name;
- if (!file_exists($filename)) {
- $url = url_helper::member_index($member_id);
- util::qrcode($url,$name);
- }
- $qurl = UPLOAD_SITE_URL . $path . $name;
- $tm = time();
- $qurl = "{$qurl}?tm={$tm}";
- return $qurl;
- }
- static public function isVip()
- {
- $member_id = self::memberid();
- return mcard\isVip($member_id);
- }
- static public function mobile()
- {
- if (self::isVerfiyMobile()) {
- return $_SESSION['member_mobile'];
- } else {
- return '';
- }
- }
- static public function parase_wxinfo($wxinfo)
- {
- $user = urldecode($wxinfo);
- if (empty($user)) return false;
- $user = json_decode($user, true);
- if ($user == false) return false;
- $info = [];
- $info['member_nickname'] = $user['nickname'];
- $info['member_avatar'] = $user['headimgurl'];
- $info['member_sex'] = intval($user['sex']);
- return $info;
- }
- static public function filter_info($info)
- {
- $member_info = [];
- if (isset($info['member_nickname']) && !empty($info['member_nickname'])) {
- $member_info['member_nickname'] = $info['member_nickname'];
- }
- if (isset($info['member_truename']) && !empty($info['member_truename'])) {
- if (!isset($member_info['member_nickname'])) {
- $member_info['member_nickname'] = $info['member_truename'];
- }
- $member_info['member_truename'] = $info['member_truename'];
- }
- if (isset($info['member_mobile']) && !empty($info['member_mobile'])) {
- $mobile = $info['member_mobile'];
- if (!isset($member_info['member_nickname'])) {
- $member_info['member_nickname'] = substr_replace($mobile, '****', 3, 4);
- }
- $member_info['member_mobile'] = $info['member_mobile'];
- }
- return $member_info;
- }
- static public function filter_member_info($member_info, $openid)
- {
- field_helper::validate_null_string($member_info, 'member_mobile,member_wxopenid,member_name,' .
- 'member_truename,member_signname,member_avatar,member_email,member_birthday');
- field_helper::validate_null_string($member_info, 'member_sex', '0');
- field_helper::validate_null_string($member_info, 'member_nickname', substr_replace($openid, '****', 3, 4));
- return $member_info;
- }
- static public function parse_contacts($contacts)
- {
- if (!isset($contacts)) {
- return false;
- }
- $contacts = json_decode(urldecode($contacts));
- if ($contacts == false) {
- return false;
- }
- $ar_contact = [];
- foreach ($contacts as $mobile)
- {
- $mobile = session_helper::mobile_valid($mobile);
- if ($mobile == false) {
- continue;
- }
- if (algorithm::bsearch($mobile, $ar_contact) != -1) {
- continue;
- }
- array_push($ar_contact, $mobile);
- sort($ar_contact);
- }
- return $ar_contact;
- }
- static public function session_id() {
- return empty($_SESSION['MPHPSESSID']) ? "" : $_SESSION['MPHPSESSID'];
- }
- static public function binded_mobile() {
- return intval($_SESSION['member_mobile_bind']) == 1;
- }
- static public function binded_wechat() {
- return intval($_SESSION['member_wechat_bind']) == 1;
- }
- static public function need_wechat_author()
- {
- wechat_helper::clear_origin_url();
- if (util::from_wechat() == false || session_helper::client_type() == session_helper::device_mini) {
- return false;
- }
- else
- {
- $relay_id = session_helper::relay_id();
- if (wechat_helper::has_userinfo())
- {
- $user_info = wechat_helper::get_userinfo();
- if(session_helper::logined())
- {
- $id_logginer = new login\memberid_log(session_helper::memberid());
- if(session_helper::binded_wechat())
- {
- $wechat_loginer = new login\open_userinfo_log($user_info);
- if($wechat_loginer->ismember()) {
- $wechat_loginer->login();
- }
- else {
- $id_logginer->bind($user_info);
- $id_logginer->login();
- }
- }
- else {
- $id_logginer->bind($user_info);
- $id_logginer->login();
- }
- }
- else
- {
- $wechat_loginer = new login\open_userinfo_log($user_info);
- if ($wechat_loginer->ismember()) {
- $wechat_loginer->login();
- }
- else {
- $wechat_loginer->register($user_info,$relay_id,session_helper::mobile());
- $wechat_loginer->login();
- account_helper::onRegister(session_helper::memberid(),$relay_id);
- wechat_helper::set_invite_bonus(); //如果用户从扫二维码来的,需要领红包
- }
- }
- account_helper::onLogin(session_helper::memberid());
- wechat_helper::clear_userinfo();
- return false;
- }
- elseif (session_helper::binded_wechat())
- {
- $author_time = $_SESSION['wxauthor_time'];
- $max_time = 86400;
- if (time() - $author_time > $max_time) {
- return true;
- } else {
- return false;
- }
- }
- else {
- return true;
- }
- }
- }
- static public function pub_openid()
- {
- if (empty($_SESSION['member_wxopenid'])) {
- return false;
- } else {
- return $_SESSION['member_wxopenid'];
- }
- }
- static public function unionid()
- {
- if (!empty($_SESSION['member_wxunionid'])) {
- return $_SESSION['member_wxunionid'];
- }
- if (array_key_exists('wx_author', $_SESSION))
- {
- if (array_key_exists('handled', $_SESSION['wx_author'])) {
- $user_info = $_SESSION['wx_author']['user_info'];
- return $user_info['unionid'];
- }
- }
- return false;
- }
- static public function thief($fromid, &$err)
- {
- $thief = new bonus\thief_vilator($fromid);
- return $thief->thief($err);
- }
- static public function first_order()
- {
- if (array_key_exists('order_num', $_SESSION)) {
- $order_num = $_SESSION['order_num'];
- }
- else {
- $mod_member = Model('member');
- $minfo = $mod_member->getMemberInfoByID(self::memberid());
- if (empty($minfo)) return false;
- $order_num = intval($minfo['order_num']);
- if ($order_num > 0) {
- $_SESSION['order_num'] = $order_num;
- }
- }
- if ($order_num == 0) {
- return true;
- } else {
- return false;
- }
- }
- static public function can_send()
- {
- if(noBonusRate()) return true;
- if (array_key_exists('order_num', $_SESSION)) {
- $order_num = $_SESSION['order_num'];
- }
- else
- {
- $mod_member = Model('member');
- $minfo = $mod_member->getMemberInfoByID(self::memberid());
- if (empty($minfo)) return false;
- $order_num = intval($minfo['order_num']);
- if ($order_num > 0) {
- $_SESSION['order_num'] = $order_num;
- }
- }
- return $order_num > 0;
- }
- static public function add_order()
- {
- if (!array_key_exists('order_num', $_SESSION)) {
- $mod_member = Model('member');
- $mod_member->editMember(['member_id' => self::memberid()],['order_num' => ['exp', 'order_num+1'],'lastest_order' => time()]);
- $minfo = $mod_member->getMemberInfoByID(self::memberid());
- $_SESSION['order_num'] = intval($minfo['order_num']);
- } else {
- $_SESSION['order_num'] += 1;
- }
- }
- public static function share_id()
- {
- if(self::logined()) {
- return self::memberid();
- }
- elseif(array_key_exists('relay_id',$_SESSION)) {
- return $_SESSION['relay_id'];
- } else {
- return 0;
- }
- }
- public static function relay_id()
- {
- if(array_key_exists('relay_id',$_SESSION)) {
- return $_SESSION['relay_id'];
- } else {
- return 0;
- }
- }
- public static function set_relay($relay_id)
- {
- $relay_id = intval($relay_id);
- if($relay_id > 0) {
- $_SESSION['relay_id'] = $relay_id;
- }
- }
- public static function client_type()
- {
- if($_SESSION['client_type'] == 'android') {
- return self::device_android;
- }
- elseif($_SESSION['client_type'] == 'ios') {
- return self::device_ios;
- }
- elseif($_SESSION['client_type'] == 'mini') {
- return self::device_mini;
- }
- elseif($_SESSION['client_type'] == 'ajax' || $_SESSION['client_type'] == 'wap') {
- return self::device_wap;
- }
- else {
- return 0;
- }
- }
- public static function version_code()
- {
- if(self::client_type() == self::device_android) {
- return $_SESSION['client_version'];
- }
- elseif(self::client_type() == self::device_ios) {
- return $_SESSION['client_version'] * 100;
- }
- elseif(self::client_type() == self::device_mini) {
- return intval($_SESSION['client_version']);
- }
- else {
- return 0;
- }
- }
- //缓存数据给个人页面使用
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- static public function address_num()
- {
- if(array_key_exists('address_num',$_SESSION)) {
- return $_SESSION['address_num'];
- }
- else
- {
- $mod_addr = Model('address');
- $_SESSION['address_num'] = $mod_addr->getAddressCount(['member_id' => self::memberid()]);
- return $_SESSION['address_num'];
- }
- }
- static public function clear_addr()
- {
- if (array_key_exists('address_num', $_SESSION)) {
- unset($_SESSION['address_num']);
- }
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- static public function favorate_num()
- {
- if(array_key_exists('favorates_num',$_SESSION)) {
- return $_SESSION['favorates_num'];
- }
- else
- {
- $mod_favorites = Model('favorites');
- $_SESSION['favorates_num'] = $mod_favorites->getFavoritesCount(['member_id' => self::memberid()]);
- return $_SESSION['favorates_num'];
- }
- }
- static public function clear_favorate()
- {
- if (array_key_exists('favorates_num', $_SESSION)) {
- unset($_SESSION['favorates_num']);
- }
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- static public function fcode_num()
- {
- $mod_favorites = Model('goods_fcode');
- $_SESSION['fcode_num'] = $mod_favorites->getUsableFcodeCount(self::mobile());
- return $_SESSION['fcode_num'];
- }
- static public function clear_fcode()
- {
- if (array_key_exists('fcode_num', $_SESSION)) {
- unset($_SESSION['fcode_num']);
- }
- }
- ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- static public function has_regmobile() {
- return !empty(self::get_regmobile());
- }
- static public function get_regmobile()
- {
- if(array_key_exists('regmobile',$_SESSION)) {
- return $_SESSION['regmobile'];
- } else {
- return "";
- }
- }
- static public function set_regmobile($mobile) {
- $_SESSION['regmobile'] = $mobile;
- }
- static public function clear_regmobile()
- {
- if(array_key_exists('regmobile',$_SESSION)) {
- unset($_SESSION['regmobile']);
- }
- }
- }
- class wechat_helper
- {
- static public function has_userinfo() {
- return !empty(self::get_userinfo());
- }
- static public function get_userinfo()
- {
- if(array_key_exists('wx_author',$_SESSION)) {
- return $_SESSION['wx_author']['user_info'];
- } else {
- return "";
- }
- }
- static public function set_userinfo($userinfo) {
- $_SESSION['wx_author']['user_info'] = $userinfo;
- }
- static public function clear_userinfo()
- {
- if(array_key_exists('wx_author',$_SESSION)) {
- unset($_SESSION['wx_author']);
- }
- }
- static public function has_origin_url()
- {
- return array_key_exists('author_orgin_url',$_SESSION);
- }
- static public function set_origin_url($url) {
- $_SESSION['author_orgin_url'] = $url;
- }
- static public function get_origin_url() {
- if(array_key_exists('author_orgin_url',$_SESSION)) {
- return $_SESSION['author_orgin_url'];
- } else {
- return "";
- }
- }
- static public function clear_origin_url()
- {
- if(array_key_exists('author_orgin_url',$_SESSION)) {
- unset($_SESSION['author_orgin_url']);
- }
- }
- static public function set_invite_bonus()
- {
- $_SESSION['invite_bonus_qrcode'] = true;
- }
- static public function clear_invite_bonus()
- {
- if(array_key_exists('invite_bonus_qrcode',$_SESSION)) {
- unset($_SESSION['invite_bonus_qrcode']);
- }
- }
- static public function invite_bonus_tag()
- {
- if(array_key_exists('invite_bonus_qrcode',$_SESSION)) {
- return $_SESSION['invite_bonus_qrcode'];
- }
- else {
- return false;
- }
- }
- static public function setmini_sesskey($sesskey) {
- $_SESSION['mini_sesskey'] = $sesskey;
- }
- static public function mini_sesskey() {
- return $_SESSION['mini_sesskey'];
- }
- }
|