// +---------------------------------------------------------------------- declare (strict_types = 1); // 应用公共函数库文件 use app\api\model\card\UserRiceCard; use app\api\model\card\UserRiceCard as UserRiceCardModel; use app\api\model\card\UserRiceCardConsume; use app\store\service\Auth; use app\common\service\store\User as StoreUserService; /** * 验证指定url是否有访问权限 * @param string|array $url * @param bool $strict 严格模式 * @return bool */ function checkPrivilege($url, $strict = true) { try { return Auth::getInstance()->checkPrivilege($url, $strict); } catch (\Exception $e) { return false; } } /** * 日期转换时间戳(不保留时间) * 例如: 2020-04-01 08:15:08 => 1585670400 * @param string $date * @return false|int */ function str2time_date(string $date) { return strtotime(date('Y-m-d', strtotime($date))); } /** * 日期转换时间戳(保留最晚时间) * 例如: 2020-04-01 08:15:08 => 1585670400 * @param string $date * @return false|int */ function str2time_latest(string $date) { return strtotime(date('Y-m-d 23:59:59', strtotime($date))); } /** * 格式化起止时间(为了兼容前端RangePicker组件) * 2020-04-01T08:15:08.891Z => 1585670400 * @param array $times * @return array */ function between_time(array $times) { foreach ($times as &$time) { $time = trim($time, '"'); $time = str2time_date($time); } return ['start_time' => current($times), 'end_time' => next($times)]; } /** * 日期转换时间戳(保留时分秒时间) * 例如: 2020-04-01 08:15:08 => 1585670400 * @param string $date * @return false|int */ function str2time_date_format(string $date) { return strtotime(date('Y-m-d H:i:s', strtotime($date))); } /** * 格式化起止时间(为了兼容前端RangePicker组件) * 2020-04-01T08:15:08.891Z => 1585670400 * @param array $times * @return array */ function between_time_format(array $times) { foreach ($times as &$time) { $time = trim($time, '"'); $time = str2time_date_format($time); } return ['start_time' => current($times), 'end_time' => next($times)]; } /** * 格式化起止日期(为了兼容前端RangePicker组件) * 2020-04-01T08:15:08.891Z => 2020-04-01 08:15:08 * @param array $times * @return array */ function between_date(array $times) { foreach ($times as &$time) { $time = trim($time, '"'); $time = str2time_date($time); } return ['start_date' => date('Y-m-d H:i:s',current($times)), 'end_date' => date('Y-m-d H:i:s',next($times)+86399)]; } /** * 格式化起止日期(为了兼容前端RangePicker组件),保留时间 * 2020-04-01T08:15:08.891Z => 2020-04-01 08:15:08 * @param array $times * @return array */ function between_date_format(array $times): array { foreach ($times as &$time) { $time = trim($time, '"'); $time = str2time_date_format($time); } return ['start_date' => date('Y-m-d H:i:s',current($times)), 'end_date' => date('Y-m-d H:i:s',next($times)+86399)]; } /** * 获取8位字符串(6位数字+2位大写字母) * @param $amount * @return array */ function gen_rand8($amount){ $codes = []; $numSeeds = '23456789'; //字符组合 //$strSeeds = ['TWEOCDUBZHMXJNARFPKGYVSQ','ZGTUNQAWEBVRPMKYDOFJCXHS','ANYERQDVFMBPOHGCZXKUTJWS']; $strSeeds = 'ABCDEFGHJKMNPQRSTUVWXYZ'; for ($count=0;$count<$amount;$count++){ $randStr = $randNum = ''; for ($i=0;$i<6;$i++) { //$num = mt_rand(0,7); $randStr .= $numSeeds[mt_rand(0,7)]; } //随机取两位数字 $randStr .= $strSeeds[mt_rand(0,22)].$strSeeds[mt_rand(0,22)]; $codes[] = str_shuffle($randStr); } return $codes; } function gen_number8($amount,$startWith='2'){ $codes = []; for ($count=0;$count<$amount;$count++){ //$codes[] = $numSeeds[rand(0,4)].rand(1000000,9999999); $codes[] = $startWith.rand(1000000,9999999); } return $codes; } /** * N位自由数字 * @param $amount * @return array */ function gen_rand_number8($amount){ $codes = []; $numSeeds = '0123456789'; for ($count=0;$count<$amount;$count++){ $codes[] = $numSeeds[rand(0,9)].$numSeeds[rand(0,9)].$numSeeds[rand(0,9)].$numSeeds[rand(0,9)].$numSeeds[rand(0,9)].$numSeeds[rand(0,9)].$numSeeds[rand(0,9)].$numSeeds[rand(0,9)]; } return $codes; } //退还米卡 function riceCardReturn(int $rice_card_id,$rice_card_money,$order_no,$pay_money){ $riceCard = UserRiceCardModel::getValidRiceCardDetail($rice_card_id); if ($rice_card_id) { // 订单商品有使用现金卡 // 验证余额 if ($rice_card_money + $riceCard['balance'] > $riceCard['face_value']) { return false; } // 写入现金卡流水明细 UserRiceCardConsume::add($riceCard, $order_no, $pay_money, $rice_card_money, 1, '商品退款'); // 退还现金卡金额 UserRiceCard::setIncByField($rice_card_id, 'balance', (float)$rice_card_money); $riceCard->effect_state = 1; // 改为 生效 $riceCard->save(); } return true; }