Tools.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\utils;
  8. use app\constants\OptionSetting;
  9. use app\models\Option;
  10. use app\models\Order;
  11. use app\models\OrderRefund;
  12. use app\models\VerifyCard;
  13. use app\models\VerifyCardAccount;
  14. use app\models\VerifyCardSale;
  15. use yii\helpers\Json;
  16. class Tools
  17. {
  18. /**
  19. * 计算两点之间的距离
  20. * @param $lat1
  21. * @param $lng1
  22. * @param $lat2
  23. * @param $lng2
  24. * @return string
  25. */
  26. public static function getDistance($lat1, $lng1, $lat2, $lng2) {
  27. if (empty($lat1) || empty($lng1) || empty($lat2) || empty($lng2)) {
  28. return '未知距离';
  29. }
  30. $from = [$lat1, $lng1];
  31. $to = [$lat2, $lng2];
  32. sort($from);
  33. sort($to);
  34. $EARTH_RADIUS = 6370.996; // 地球半径系数
  35. $distance = $EARTH_RADIUS * 2 * asin(sqrt(pow(sin(($from[0] * pi() / 180 - $to[0] * pi() / 180) / 2), 2) + cos($from[0] * pi() / 180) * cos($to[0] * pi() / 180) * pow(sin(($from[1] * pi() / 180 - $to[1] * pi() / 180) / 2), 2))) * 1000;
  36. $distance = round($distance, 2);
  37. if ($distance > 1000) {
  38. $distance = round($distance / 1000, 2) . 'km';
  39. } else {
  40. $distance .= 'm';
  41. }
  42. return $distance;
  43. }
  44. /**
  45. * 获取未过期且有使用次数的核销卡
  46. * @param $ids
  47. * @return array
  48. */
  49. public static function getVerifyList($ids, $order = null, $goods_model=null) {
  50. $arr = [];
  51. $verify_card = VerifyCard::find()
  52. ->where(['in', 'id', $ids])
  53. ->asArray()
  54. ->all();
  55. if (!empty($verify_card) && is_array($verify_card)) {
  56. foreach ($verify_card as &$val) {
  57. $is_add = true;
  58. $val['account'] = '';
  59. $val['password'] = '';
  60. $account = VerifyCardAccount::find()->where(['store_id' => $val['store_id'], 'card_id' => $val['id'], 'status' => 0])->limit(1)->one();
  61. if ($account) {
  62. $val['account'] = $account->account;
  63. $val['password'] = $account->password;
  64. }
  65. if ($val['type'] > 1) {
  66. $is_add = false;
  67. if ($account && $account->status == 0) {
  68. $is_add = true;
  69. }
  70. }
  71. $val['sale_status'] = 0;
  72. $val['sale_id'] = 0;
  73. $val['is_use'] = 0;
  74. if ($order) {
  75. if ($val['type'] == 1) {
  76. //判断时间
  77. $after_sale_time = Option::get(OptionSetting::STORE_AFTER_SALE_TIME, get_store_id())['value'];
  78. $sale_time = time() - ($after_sale_time * 86400);
  79. //判断订单状态已经完成
  80. if ($order->confirm_time <= $sale_time && $order->trade_status == 3 && $order->is_sale == 0) {
  81. $val['is_use'] = 1;
  82. }
  83. }
  84. $sale_info = VerifyCardSale::find()->where([
  85. 'verify_card_id' => $val['id'],
  86. 'user_id' => $order->user_id,
  87. 'store_id' => $val['store_id'],
  88. 'goods_id' => $goods_model->id,
  89. ])->orderBy('id desc')->asArray()->one();
  90. if ($sale_info) {
  91. $is_add = true;
  92. $val['sale_status'] = $sale_info['status'];
  93. $val['sale_id'] = $sale_info['id'];
  94. $val['video_status'] = $sale_info['video_status'];
  95. }
  96. }
  97. $dbcount = VerifyCardAccount::find()->where(['card_id' => $val['id']])->count();
  98. if (($dbcount > $val['send_times'] || $val['num'] == 0) && $is_add) {
  99. $arr[] = $val;
  100. }
  101. }
  102. if (!empty($arr)) {
  103. foreach ($arr as $key => $value) {
  104. if ($value['date_type'] == 2) {
  105. if (intval($value['end_time'] < time())) {
  106. unset($arr[$key]);
  107. }
  108. }
  109. if (intval($value['is_delete']) == 1) {
  110. unset($arr[$key]);
  111. }
  112. }
  113. }
  114. return array_values($arr);
  115. } else {
  116. return [];
  117. }
  118. }
  119. public static function getDate() {
  120. // 当前日期
  121. $defaultDate = date("Y-m-d");
  122. // $first = 1 表示每周星期一为开始日期 0表示每周日为开始日期
  123. $first = 1;
  124. // 获取当前周的第几天 周日是 0 周一到周六是 1 - 6
  125. $w = date('w', strtotime($defaultDate));
  126. // 获取本周开始日期,如果$w是0,则表示周日,减去 6 天
  127. $week_start = date('Y-m-d', strtotime("$defaultDate -" . ($w ? $w - $first : 6) . ' days'));
  128. // 本周结束日期
  129. $week_end = date('Y-m-d', strtotime("$week_start +6 days"));
  130. return [
  131. 'start' => $week_start,
  132. 'end' => $week_end,
  133. 'date' => self::getDateFromRange($week_start, $week_end)
  134. ];
  135. }
  136. /**
  137. * 获取时间段内的日期
  138. * @param $start_date
  139. * @param $end_date
  140. * @return array
  141. */
  142. public static function getDateFromRange($start_date, $end_date){
  143. $s_timestamp = strtotime($start_date);
  144. $e_timestamp = strtotime($end_date);
  145. // 计算日期段内有多少天
  146. $days = ($e_timestamp - $s_timestamp) / 86400 + 1;
  147. // 保存每天日期
  148. $date = [];
  149. for ($i = 0; $i < $days; $i++){
  150. $date[] = date('Y-m-d', $s_timestamp + (86400 * $i));
  151. }
  152. return $date;
  153. }
  154. /**
  155. * 将用户名进行处理,中间用星号表示
  156. * @param $user_name
  157. * @return string
  158. */
  159. public static function substr_cut($user_name){
  160. // 获取字符串长度
  161. $strlen = mb_strlen($user_name, 'utf-8');
  162. // 如果字符创长度小于2,不做任何处理
  163. if ($strlen < 2){
  164. return $user_name . '**';
  165. } else {
  166. // mb_substr — 获取字符串的部分
  167. $firstStr = mb_substr($user_name, 0, 1, 'utf-8');
  168. $lastStr = mb_substr($user_name, -1, 1, 'utf-8');
  169. // str_repeat — 重复一个字符串
  170. return $strlen == 2 ? $firstStr . str_repeat('*', mb_strlen($user_name, 'utf-8') - 1) : $firstStr . str_repeat("*", $strlen - 2) . $lastStr;
  171. }
  172. }
  173. /**
  174. * 格式化数字
  175. */
  176. public static function float_number($number) {
  177. $length = strlen($number); // 数字长度
  178. if ($length > 8) { // 亿单位
  179. $str = substr_replace(strstr($number, substr($number, -7), ' '), '.', -1, 0) . "亿";
  180. } else if ($length > 4) { // 万单位
  181. // 截取前俩为
  182. $str = substr_replace(strstr($number, substr($number, -3), ' '), '.', -1, 0) . "万";
  183. } else {
  184. return $number;
  185. }
  186. return $str;
  187. }
  188. /**
  189. * 获取特殊时间
  190. */
  191. public static function getTime($time, $now_time){
  192. $times = $now_time - $time;
  193. if ($times <= 60) {
  194. $str_time = '刚刚';
  195. } else if ($times <= 60 * 60) {
  196. $str_time = round($times / 60) . '分钟前';
  197. } elseif ($times <= 60 * 60 * 24) {
  198. $str_time = round($times / (60 * 60)) . '小时前';
  199. } elseif ($times <= 60 * 60 * 48){
  200. $str_time = '昨天';
  201. } else {
  202. $str_time = date('m-d', $time);
  203. }
  204. return $str_time;
  205. }
  206. /**
  207. * 获得几天前,几小时前,几月前
  208. * @param int $time 时间戳
  209. * @param int $now_time 当前时间
  210. * @param array $unit 时间单位
  211. * @return bool|string
  212. */
  213. public static function date_before($time, $now_time, $unit = null) {
  214. $time = intval($time);
  215. $unit = is_null($unit) ? ["年", "月", "星期", "天", "小时", "分钟", "秒"] : $unit;
  216. switch (true) {
  217. case $time < ($now_time - 31536000) :
  218. return floor(($now_time - $time) / 31536000) . $unit[0] . '前';
  219. case $time < ($now_time - 2592000) :
  220. return floor(($now_time - $time) / 2592000) . $unit[1] . '前';
  221. case $time < ($now_time - 604800) :
  222. return floor(($now_time - $time) / 604800) . $unit[2] . '前';
  223. case $time < ($now_time - 86400) :
  224. return floor(($now_time - $time) / 86400) . $unit[3] . '前';
  225. case $time < ($now_time - 3600) :
  226. return floor(($now_time - $time) / 3600) . $unit[4] . '前';
  227. case $time < ($now_time - 60) :
  228. return floor(($now_time - $time) / 60) . $unit[5] . '前';
  229. default :
  230. return floor($now_time - $time) . $unit[6] . '前';
  231. }
  232. }
  233. }