helpers.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: sdf_sky
  5. * Date: 15/10/27
  6. * Time: 下午7:11
  7. */
  8. if (!function_exists('trans_common_bool')) {
  9. function trans_common_bool($value)
  10. {
  11. $map = [
  12. 0 => '否',
  13. 1 => '是',
  14. ];
  15. if ($value === 'all') {
  16. return $map;
  17. }
  18. if (isset($map[$value])) {
  19. return $map[$value];
  20. }
  21. return '';
  22. }
  23. }
  24. /*商品类型字段定义*/
  25. if (!function_exists('trans_goods_post_type')) {
  26. function trans_goods_post_type($post_type)
  27. {
  28. $map = [
  29. 0 => '不需要',
  30. 1 => '需要',
  31. ];
  32. if ($post_type === 'all') {
  33. return $map;
  34. }
  35. if (isset($map[$post_type])) {
  36. return $map[$post_type];
  37. }
  38. return '';
  39. }
  40. }
  41. if (!function_exists('trans_gender_name')) {
  42. function trans_gender_name($post_type)
  43. {
  44. $map = [
  45. 0 => '保密',
  46. 1 => '男',
  47. 2 => '女',
  48. ];
  49. if ($post_type === 'all') {
  50. return $map;
  51. }
  52. if (isset($map[$post_type])) {
  53. return $map[$post_type];
  54. }
  55. return '';
  56. }
  57. }
  58. /*行家认证状态文字定义*/
  59. if (!function_exists('trans_authentication_status')) {
  60. function trans_authentication_status($status)
  61. {
  62. $map = [
  63. 0 => '待审核',
  64. 1 => '审核通过',
  65. 4 => '审核失败',
  66. ];
  67. if ($status === 'all') {
  68. return $map;
  69. }
  70. if (isset($map[$status])) {
  71. return $map[$status];
  72. }
  73. return '';
  74. }
  75. }
  76. /*公告状态文字定义*/
  77. if (!function_exists('trans_exchange_status')) {
  78. function trans_exchange_status($status)
  79. {
  80. $map = [
  81. 0 => '未处理',
  82. 1 => '已处理',
  83. 4 => '兑换失败',
  84. ];
  85. if ($status === 'all') {
  86. return $map;
  87. }
  88. if (isset($map[$status])) {
  89. return $map[$status];
  90. }
  91. return '';
  92. }
  93. }
  94. /*公告状态文字定义*/
  95. if (!function_exists('trans_common_status')) {
  96. function trans_common_status($status)
  97. {
  98. $map = [
  99. 0 => '待审核',
  100. 1 => '已审核',
  101. ];
  102. if ($status === 'all') {
  103. return $map;
  104. }
  105. if (isset($map[$status])) {
  106. return $map[$status];
  107. }
  108. return '';
  109. }
  110. }
  111. /*草稿类型定义*/
  112. if (!function_exists('trans_draft_type')) {
  113. function trans_draft_type($type)
  114. {
  115. $map = [
  116. 'question' => '提问',
  117. 'answer' => '回答',
  118. 'article' => '文章'
  119. ];
  120. if ($type === 'all') {
  121. return $map;
  122. }
  123. if (isset($map[$type])) {
  124. return $map[$type];
  125. }
  126. return '';
  127. }
  128. }
  129. /*举报状态定义*/
  130. if (!function_exists('trans_report_status')) {
  131. function trans_report_status($status)
  132. {
  133. $map = [
  134. 0 => '待处理',
  135. 1 => '已处理',
  136. 4 => '已忽略'
  137. ];
  138. if ($status === 'all') {
  139. return $map;
  140. }
  141. if (isset($map[$status])) {
  142. return $map[$status];
  143. }
  144. return '';
  145. }
  146. }
  147. /*举报原因类型定义*/
  148. if (!function_exists('trans_report_type')) {
  149. function trans_report_type($type)
  150. {
  151. // 读取定义的配置信息
  152. $map = config('tipask.report_type');
  153. if ($type === 'all') {
  154. return $map;
  155. }
  156. if (isset($map[$type])) {
  157. return $map[$type]['subject'];
  158. }
  159. return '';
  160. }
  161. }
  162. /*公告状态文字定义*/
  163. if (!function_exists('trans_pay_status')) {
  164. function trans_pay_status($status)
  165. {
  166. $map = [
  167. 0 => '待支付',
  168. 1 => '已支付',
  169. ];
  170. if ($status === 'all') {
  171. return $map;
  172. }
  173. if (isset($map[$status])) {
  174. return $map[$status];
  175. }
  176. return '';
  177. }
  178. }
  179. /*问题状态文本描述定义*/
  180. if (!function_exists('trans_question_status')) {
  181. function trans_question_status($status)
  182. {
  183. $map = [
  184. 1 => '待解决',
  185. 2 => '已解决',
  186. ];
  187. if ($status === 'all') {
  188. return $map;
  189. }
  190. if (isset($map[$status])) {
  191. return $map[$status];
  192. }
  193. return '';
  194. }
  195. }
  196. /*问题状态文本描述定义*/
  197. if (!function_exists('trans_day_of_week')) {
  198. function trans_day_of_week($day)
  199. {
  200. $map = [
  201. 0 => '星期天',
  202. 1 => '星期一',
  203. 2 => '星期二',
  204. 3 => '星期三',
  205. 4 => '星期四',
  206. 5 => '星期五',
  207. 6 => '星期六',
  208. ];
  209. if ($day === 'all') {
  210. return $map;
  211. }
  212. if (isset($map[$day])) {
  213. return $map[$day];
  214. }
  215. return '';
  216. }
  217. }
  218. if (!function_exists('trans_question_price')) {
  219. function trans_question_price($price = 'all')
  220. {
  221. $map = [0, 5, 10, 15, 20, 30, 50, 80, 100];
  222. return $map;
  223. }
  224. }
  225. /*数据库setting表操作*/
  226. if (!function_exists('Setting')) {
  227. function Setting()
  228. {
  229. return app('App\Models\Setting');
  230. }
  231. }
  232. /*数据库Category表操作*/
  233. if (!function_exists('load_categories')) {
  234. function load_categories($type = 'all')
  235. {
  236. return app('App\Models\Category')->loadFromCache($type);
  237. }
  238. }
  239. /*数据库Category表操作*/
  240. if (!function_exists('make_option_tree')) {
  241. function make_option_tree($type = 'all', $select_id = 0)
  242. {
  243. $categories = app('App\Models\Category')->loadFromCache($type);
  244. return app('App\Models\Category')->makeOptionTree($categories, $select_id);
  245. }
  246. }
  247. /*生成分类Tab下拉数据格式*/
  248. if (!function_exists('get_category_tab_data')) {
  249. function get_category_tab_data($type = 'all', $size = 6)
  250. {
  251. $categories = app('App\Models\Category')->loadFromCache($type);
  252. $rootCategories = [];
  253. foreach ($categories as $category) {
  254. if ($category->parent_id == 0) {
  255. $rootCategories[] = $category;
  256. }
  257. }
  258. $tabData['out_tabs'] = array_slice($rootCategories,0,$size);
  259. $tabData['in_tabs'] = array_slice($rootCategories,$size);
  260. return $tabData;
  261. }
  262. }
  263. /*数据库area地区表操作*/
  264. if (!function_exists('Area')) {
  265. function Area()
  266. {
  267. return app('App\Models\Area');
  268. }
  269. }
  270. /**
  271. * 将正整数转换为带+,例如 10 装换为 +10
  272. * 用户积分显示
  273. */
  274. if (!function_exists('integer_string')) {
  275. function integer_string($value)
  276. {
  277. if ($value >= 0) {
  278. return '+' . $value;
  279. }
  280. return $value;
  281. }
  282. }
  283. if (!function_exists('get_credit_message')) {
  284. function get_credit_message($credits, $coins)
  285. {
  286. $messages = [];
  287. if ($credits != 0) {
  288. $messages[] = '经验 ' . integer_string($credits);
  289. }
  290. if ($coins != 0) {
  291. $messages[] = '金币 ' . integer_string($coins);
  292. }
  293. return implode(",", $messages);
  294. }
  295. }
  296. if (!function_exists('timestamp_format')) {
  297. function timestamp_format($timestamp, $showDateTime = true)
  298. {
  299. $carbon = \Carbon\Carbon::instance(new DateTime($timestamp));
  300. $time_format_string = Setting()->get('date_format');
  301. if ($showDateTime) {
  302. $time_format_string .= ' ' . Setting()->get('time_format');
  303. }
  304. if (Setting()->get('time_friendly') == 1) {
  305. return $carbon->diffInWeeks(\Carbon\Carbon::now()) >= 1 ? $carbon->format($time_format_string) : $carbon->diffForHumans();
  306. }
  307. return $carbon->format($time_format_string);
  308. }
  309. }
  310. if (!function_exists('parse_seo_template')) {
  311. function parse_seo_template($type, $source)
  312. {
  313. $seo_template = Setting()->get($type);
  314. $seo_template = str_replace("{wzmc}", Setting()->get('website_name'), $seo_template);
  315. $seo_template = str_replace("{wzkh}", Setting()->get('website_slogan'), $seo_template);
  316. if (str_contains($type, ['question', 'article'])) {
  317. if ($source->tags) {
  318. $tagList = array_pluck($source->tags->toArray(), 'name');
  319. $seo_template = str_replace("{htlb}", implode(",", $tagList), $seo_template);
  320. }
  321. }
  322. if (str_contains($type, 'question')) {
  323. $seo_template = str_replace("{wtbt}", strip_tags($source->title), $seo_template);
  324. $seo_template = str_replace("{wtms}", str_limit(strip_tags($source->description), 200), $seo_template);
  325. } else {
  326. if (str_contains($type, 'article')) {
  327. $seo_template = str_replace("{wzbt}", strip_tags($source->title), $seo_template);
  328. $seo_template = str_replace("{wzzy}", str_limit($source->summary, 200), $seo_template);
  329. } else {
  330. if (str_contains($type, 'topic')) {
  331. $seo_template = str_replace("{htmc}", $source->name, $seo_template);
  332. $seo_template = str_replace("{htjj}", str_limit($source->summary, 200), $seo_template);
  333. }
  334. }
  335. }
  336. return $seo_template;
  337. }
  338. }
  339. /*生成头像图片地址*/
  340. if (!function_exists('get_user_avatar')) {
  341. function get_user_avatar($user_id, $size = 'middle', $extension = 'jpg', $random = '')
  342. {
  343. $avatarName = $user_id . '_' . $size . '.' . $extension;
  344. if ($random) {
  345. $avatarName .= '?' . str_random(8);
  346. }
  347. return route('website.image.avatar', ['avatar_name' => $avatarName]);
  348. }
  349. }
  350. /*常见的正则判断*/
  351. /*邮箱判断*/
  352. if (!function_exists('is_email')) {
  353. function is_email($email)
  354. {
  355. $reg = "/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/";
  356. if (preg_match($reg, $email)) {
  357. return true;
  358. }
  359. return false;
  360. }
  361. }
  362. /*手机号码判断*/
  363. if (!function_exists('is_mobile')) {
  364. function is_mobile($mobile)
  365. {
  366. $reg = "/^1[3456789]\d{9}$/";
  367. if (!preg_match($reg, $mobile)) {
  368. return false;
  369. }
  370. return true;
  371. }
  372. }
  373. /*创建唯一订单号*/
  374. if (!function_exists('build_order_no')) {
  375. function build_order_no($user_id = 0)
  376. {
  377. $order_no = date('Ymd') . substr(implode(null, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
  378. $salt = md5($user_id);
  379. $star = strtoupper(substr($salt, 1, 5));
  380. return $order_no . $star;
  381. }
  382. }
  383. /*创建唯一订单号*/
  384. if (!function_exists('random_number')) {
  385. function random_number($length = 6)
  386. {
  387. $pool = '0123456789';
  388. return substr(str_shuffle(str_repeat($pool, $length)), 0, $length);
  389. }
  390. }
  391. /*金币转换元*/
  392. if (!function_exists('coins_to_cent')) {
  393. function coins_to_cent($coins)
  394. {
  395. /*获取1元能够吗多少个金币*/
  396. $charge_rage = config('pay.charge_rate');
  397. if ($charge_rage > 0 && $coins > 0) {
  398. $rate = ceil(100 / $charge_rage); //计算单个金币的价格
  399. $result = $coins * $rate;
  400. return $result;
  401. }
  402. return 0;
  403. }
  404. }
  405. if (!function_exists('yuan_to_coins')) {
  406. function yuan_to_coins($yuan)
  407. {
  408. $charge_rage = config('pay.charge_rate');
  409. if ($charge_rage > 0 && $yuan > 0) {
  410. $result = $yuan * $charge_rage;
  411. return $result;
  412. }
  413. return false;
  414. }
  415. }
  416. /*分转元*/
  417. if (!function_exists('cent_to_yuan')) {
  418. function cent_to_yuan($cent)
  419. {
  420. return '¥' . number_format($cent / 100, 2);
  421. }
  422. }
  423. if (!function_exists('format_money')) {
  424. function format_money($money)
  425. {
  426. return number_format($money / 100, 2);
  427. }
  428. }
  429. /*提取html内容中的img标签图片地址*/
  430. if (!function_exists('get_editor_images')) {
  431. function get_editor_images($content)
  432. {
  433. preg_match_all('/<img[^>]+>/i', $content, $imgTags);
  434. $imageUrls = [];
  435. for ($i = 0; $i < count($imgTags[0]); $i++) {
  436. preg_match('/src="([^"]+)/i', $imgTags[0][$i], $imgage);
  437. $imageUrls[] = str_ireplace('src="', '', $imgage[0]);
  438. }
  439. return $imageUrls;
  440. }
  441. }
  442. if (!function_exists('env_cert')) {
  443. function env_cert($env_param)
  444. {
  445. $cert_string = '';
  446. if (file_exists(env($env_param, ''))) {
  447. $cert_string = file_get_contents(env($env_param));
  448. }
  449. return $cert_string;
  450. }
  451. }