common.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types = 1);
  12. // 应用公共函数库文件
  13. use app\api\model\card\UserRiceCard;
  14. use app\api\model\card\UserRiceCard as UserRiceCardModel;
  15. use app\api\model\card\UserRiceCardConsume;
  16. use app\store\service\Auth;
  17. use app\common\service\store\User as StoreUserService;
  18. /**
  19. * 验证指定url是否有访问权限
  20. * @param string|array $url
  21. * @param bool $strict 严格模式
  22. * @return bool
  23. */
  24. function checkPrivilege($url, $strict = true)
  25. {
  26. try {
  27. return Auth::getInstance()->checkPrivilege($url, $strict);
  28. } catch (\Exception $e) {
  29. return false;
  30. }
  31. }
  32. /**
  33. * 日期转换时间戳(不保留时间)
  34. * 例如: 2020-04-01 08:15:08 => 1585670400
  35. * @param string $date
  36. * @return false|int
  37. */
  38. function str2time_date(string $date)
  39. {
  40. return strtotime(date('Y-m-d', strtotime($date)));
  41. }
  42. /**
  43. * 日期转换时间戳(保留最晚时间)
  44. * 例如: 2020-04-01 08:15:08 => 1585670400
  45. * @param string $date
  46. * @return false|int
  47. */
  48. function str2time_latest(string $date)
  49. {
  50. return strtotime(date('Y-m-d 23:59:59', strtotime($date)));
  51. }
  52. /**
  53. * 格式化起止时间(为了兼容前端RangePicker组件)
  54. * 2020-04-01T08:15:08.891Z => 1585670400
  55. * @param array $times
  56. * @return array
  57. */
  58. function between_time(array $times)
  59. {
  60. foreach ($times as &$time) {
  61. $time = trim($time, '&quot;');
  62. $time = str2time_date($time);
  63. }
  64. return ['start_time' => current($times), 'end_time' => next($times)];
  65. }
  66. /**
  67. * 日期转换时间戳(保留时分秒时间)
  68. * 例如: 2020-04-01 08:15:08 => 1585670400
  69. * @param string $date
  70. * @return false|int
  71. */
  72. function str2time_date_format(string $date)
  73. {
  74. return strtotime(date('Y-m-d H:i:s', strtotime($date)));
  75. }
  76. /**
  77. * 格式化起止时间(为了兼容前端RangePicker组件)
  78. * 2020-04-01T08:15:08.891Z => 1585670400
  79. * @param array $times
  80. * @return array
  81. */
  82. function between_time_format(array $times)
  83. {
  84. foreach ($times as &$time) {
  85. $time = trim($time, '&quot;');
  86. $time = str2time_date_format($time);
  87. }
  88. return ['start_time' => current($times), 'end_time' => next($times)];
  89. }
  90. /**
  91. * 格式化起止日期(为了兼容前端RangePicker组件)
  92. * 2020-04-01T08:15:08.891Z => 2020-04-01 08:15:08
  93. * @param array $times
  94. * @return array
  95. */
  96. function between_date(array $times)
  97. {
  98. foreach ($times as &$time) {
  99. $time = trim($time, '&quot;');
  100. $time = str2time_date($time);
  101. }
  102. return ['start_date' => date('Y-m-d H:i:s',current($times)), 'end_date' => date('Y-m-d H:i:s',next($times)+86399)];
  103. }
  104. /**
  105. * 格式化起止日期(为了兼容前端RangePicker组件),保留时间
  106. * 2020-04-01T08:15:08.891Z => 2020-04-01 08:15:08
  107. * @param array $times
  108. * @return array
  109. */
  110. function between_date_format(array $times): array
  111. {
  112. foreach ($times as &$time) {
  113. $time = trim($time, '&quot;');
  114. $time = str2time_date_format($time);
  115. }
  116. return ['start_date' => date('Y-m-d H:i:s',current($times)), 'end_date' => date('Y-m-d H:i:s',next($times)+86399)];
  117. }
  118. /**
  119. * 获取8位字符串(6位数字+2位大写字母)
  120. * @param $amount
  121. * @return array
  122. */
  123. function gen_rand8($amount){
  124. $codes = [];
  125. $numSeeds = '23456789';
  126. //字符组合
  127. //$strSeeds = ['TWEOCDUBZHMXJNARFPKGYVSQ','ZGTUNQAWEBVRPMKYDOFJCXHS','ANYERQDVFMBPOHGCZXKUTJWS'];
  128. $strSeeds = 'ABCDEFGHJKMNPQRSTUVWXYZ';
  129. for ($count=0;$count<$amount;$count++){
  130. $randStr = $randNum = '';
  131. for ($i=0;$i<6;$i++) {
  132. //$num = mt_rand(0,7);
  133. $randStr .= $numSeeds[mt_rand(0,7)];
  134. }
  135. //随机取两位数字
  136. $randStr .= $strSeeds[mt_rand(0,22)].$strSeeds[mt_rand(0,22)];
  137. $codes[] = str_shuffle($randStr);
  138. }
  139. return $codes;
  140. }
  141. function gen_number8($amount,$startWith='2'){
  142. $codes = [];
  143. for ($count=0;$count<$amount;$count++){
  144. //$codes[] = $numSeeds[rand(0,4)].rand(1000000,9999999);
  145. $codes[] = $startWith.rand(1000000,9999999);
  146. }
  147. return $codes;
  148. }
  149. /**
  150. * N位自由数字
  151. * @param $amount
  152. * @return array
  153. */
  154. function gen_rand_number8($amount){
  155. $codes = [];
  156. $numSeeds = '0123456789';
  157. for ($count=0;$count<$amount;$count++){
  158. $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)];
  159. }
  160. return $codes;
  161. }
  162. //退还米卡
  163. function riceCardReturn(int $rice_card_id,$rice_card_money,$order_no,$pay_money){
  164. $riceCard = UserRiceCardModel::getValidRiceCardDetail($rice_card_id);
  165. if ($rice_card_id) { // 订单商品有使用现金卡
  166. // 验证余额
  167. if ($rice_card_money + $riceCard['balance'] > $riceCard['face_value']) {
  168. return false;
  169. }
  170. // 写入现金卡流水明细
  171. UserRiceCardConsume::add($riceCard, $order_no, $pay_money, $rice_card_money, 1, '商品退款');
  172. // 退还现金卡金额
  173. UserRiceCard::setIncByField($rice_card_id, 'balance', (float)$rice_card_money);
  174. $riceCard->effect_state = 1; // 改为 生效
  175. $riceCard->save();
  176. }
  177. return true;
  178. }