123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- <?php
- /**
- * Created by PhpStorm.
- * User: stanley-king
- * Date: 2017/4/7
- * Time: 下午5:04
- */
- namespace user_session;
- require_once (BASE_ROOT_PATH . '/helper/algorithm.php');
- require_once (BASE_ROOT_PATH . '/helper/session_helper.php');
- require_once (BASE_ROOT_PATH . '/helper/goods/commonid_helper.php');
- require_once (BASE_ROOT_PATH . '/helper/fcode/mfcode.php');
- use fcode\mfcode;
- use session_helper;
- use commonid_helper;
- use Log;
- class fcode
- {
- private $mCodes;
- private $mDirty;
- private $mFClear;
- public function __construct($clear = false)
- {
- $this->mCodes = [];
- $this->mDirty = false;
- $this->mFClear = $clear;
- if(isset($_SESSION['fcodes'])) {
- $this->mCodes = $_SESSION['fcodes'];
- }
- else {
- $this->onStatus();
- }
- }
- public function __destruct()
- {
- if($this->mFClear)
- {
- Log::record("fcode __destruct clear all fcodes.",Log::DEBUG);
- if(isset($_SESSION['fcodes'])) {
- unset($_SESSION['fcodes']);
- }
- $this->mDirty = false;
- return;
- }
- if($this->mDirty) {
- $_SESSION['fcodes'] = $this->mCodes;
- }
- $this->mDirty = false;
- }
- public function onStatus()
- {
- $this->mCodes = [];
- $this->mDirty = true;
- if(session_helper::logined() == false || empty(session_helper::mobile())) {
- return;
- }
- $mod_fcode = Model('goods_fcode');
- $fcodes = $mod_fcode->getFcodeList(['mobile' => session_helper::mobile()]);
- foreach ($fcodes as $item)
- {
- $fcoder = new mfcode($item);
- if($fcoder->un_used()) {
- $this->mCodes[$fcoder->commonid()][] = $item;
- }
- }
- }
- public function goods_has_code($goods_id,&$lock_num)
- {
- $lock_num = 0;
- $common_id = commonid_helper::instance()->common_id($goods_id);
- if($common_id == false) {
- return false;
- }
- return $this->usable_num($common_id,$lock_num);
- }
- public function usable_num($common_id,&$lock_num)
- {
- $lock_num = 0;
- if(session_helper::logined() == false) {
- return false;
- }
- if(array_key_exists($common_id,$this->mCodes))
- {
- $total = 0;
- $fcodes = $this->mCodes[$common_id];
- foreach ($fcodes as $item)
- {
- $fcoder = new mfcode($item);
- if($fcoder->can_use()) {
- $total += 1;
- }
- if($fcoder->locked()) {
- $lock_num += 1;
- }
- }
- return ($total == 0 ? false : $total);
- }
- else
- {
- return false;
- }
- }
- public function fetch($commonid,$num)
- {
- if($num <= 0) return false;
- if(array_key_exists($commonid,$this->mCodes))
- {
- $result = [];
- $fcodes = $this->mCodes[$commonid];
- foreach ($fcodes as $item)
- {
- $fcoder = new mfcode($item);
- if($fcoder->can_use())
- {
- $result[] = $fcoder->fc_id();
- if(--$num == 0) {
- $this->mFClear = true;
- return $result;
- }
- }
- }
- return false;
- }
- else
- {
- return false;
- }
- }
- }
|