Number.php 1.19 KB
<?php

/**
 * 订单号:
 * 140705  89  8738 2394 379
 * 年月日  渠道  用户ID   随机数
 * User: ziy
 * Date: 14-8-3
 * Time: 下午1:51
 */
class YHMCart_Library_Number
{
    /**
     * 年月日  渠道  用户ID   随机数
     * 140803-01-02993932-402
     * @param $uid
     * @param int $channel
     * @param int $randLen
     * @return string
     */
    static function getOrderCode($uid, $channel = 1, $randLen = 99999)
    {
        if (!in_array($channel, YHMCart_Config::$channelCodeList)) {
            #TODO Error
        }
         $mc = YHMUtils_Mc::getMc();
        #TODO 获取标记的增量数字
         $i=0;
        while ($i < $randLen) {
            $format = "%0" . strlen($randLen) . "d";
             $randNumber = sprintf($format, mt_rand(0, $randLen));
            $retData = $mc->get('order_' . $randNumber . '_uid_' . $uid);
            if ($retData == "") {
                $mc->set('order_' . $randNumber . '_uid_' . $uid, $randNumber, 84600);
                break ;
            } 
        }
       
        $uidNumber = sprintf("%08d", $uid);
        $channelNumber = sprintf("%02d", $channel);
        return (int)(date('ymd')  . $uidNumber . $randNumber);
    }
}