common.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. // 应用公共文件
  3. use think\facade\Db;
  4. if(!function_exists("json_show")){
  5. function json_show(int $code = 0, string $message = '请求成功', array $data = [])
  6. {
  7. return json(['code' => $code, 'message' => $message, 'data' => $data]);
  8. }
  9. }
  10. /**手机号验证
  11. * @param $mobile
  12. * @return bool
  13. */
  14. if(!function_exists("checkMobile")){
  15. function checkMobile($mobile){
  16. if (!is_numeric($mobile)) {
  17. return false;
  18. }
  19. return preg_match('#^1[3,4,5,6,7,8,9]{1}[\d]{9}$#', $mobile) ? true : false;
  20. }
  21. }
  22. /**邮箱验证
  23. * @param $email
  24. * @return bool
  25. */
  26. if(!function_exists("checkEmail")){
  27. function checkEmail($email){
  28. if (!$email) {
  29. return false;
  30. }
  31. return preg_match('#[a-z0-9&\-_.]+@[\w\-_]+([\w\-.]+)?\.[\w\-]+#is', $email) ? true : false;
  32. }
  33. }
  34. /**
  35. * @param
  36. * @return int
  37. */
  38. if(!function_exists("makeSalt")){
  39. function makeSalt(){
  40. $salt = rand(10000000,99999999);
  41. return $salt;
  42. }
  43. }
  44. /**
  45. * @param $account
  46. * @param float|int $expire
  47. * @return string
  48. */
  49. if(!function_exists("makeToken")){
  50. function makeToken($account){
  51. return sha1($account['username'].$account['mobile'].time());
  52. }
  53. }
  54. if(!function_exists("checkToken")){
  55. /**
  56. * @param $token
  57. * @param int $expire
  58. * @return array|bool|float|int|mixed|\stdClass|string|null
  59. * @throws \Psr\SimpleCache\InvalidArgumentException
  60. */
  61. function checkToken($token,$expire=0){
  62. $getToken = think\facade\Cache::store("redis")->get("user:info:{$token}");
  63. if($getToken!=false){
  64. $cache=think\facade\Cache::store("redis")->set("user:info:{$token}",$getToken,$expire);
  65. if($cache!=false) return $getToken;
  66. }
  67. return false;
  68. }
  69. }
  70. //获取部门的层级名称
  71. if (!function_exists('get_part')) {
  72. function get_part(int $item_id = 0,array $data=[])
  73. {
  74. $db = Db::name("company_item")
  75. ->field('id,name,pid')
  76. ->where(['id' => $item_id, 'is_del' => 0])
  77. ->findOrEmpty();
  78. if (empty($db)) return [];
  79. // $tem=[];
  80. // $tem['id']=$db['id'];
  81. // $tem['name']=$db['name'];
  82. array_unshift($data, $db);
  83. if ($db['pid'] == 0) return $data;
  84. else return get_part($db['pid'], $data);
  85. }
  86. }
  87. //获取组织架构的层级列表
  88. //@param $data 顶级列表的数据集合
  89. //@param $vio 是否展示部门下的账号
  90. if (!function_exists('crea')) {
  91. function crea($data, $vio = 0)
  92. {
  93. $db = Db::name("company_item")
  94. ->field(true)
  95. ->where(['pid' => $data['id'], 'is_del' => 0])
  96. ->select()
  97. ->toArray();
  98. if ($vio == 1) {
  99. $d = Db::name("sys_account_item")
  100. ->alias('a')
  101. ->field('a.account_id,b.username,c.nickname,c.mobile,b.status')
  102. ->leftJoin('account b','b.id=a.account_id AND b.is_del=0')
  103. ->leftJoin('user c','c.account_id=a.account_id')
  104. ->where(['itemid' => $data['id'], 'is_del' => 0])
  105. ->select()
  106. ->toArray();
  107. $data['item'] = $d;
  108. }
  109. if (empty($db)) {
  110. $data['child'] = [];
  111. return $data;
  112. }
  113. //var_dump($db);
  114. foreach ($db as $p) {
  115. $data['child'][] = crea($p, $vio);
  116. }
  117. return $data;
  118. }
  119. }
  120. //生成随机字符串
  121. if (function_exists('makeNo') == false) {
  122. function makeNo(string $str = '')
  123. {
  124. $date = date("ymdHis");
  125. // $year = date("Y")-2000;
  126. $msec = rand(1000, 9999);
  127. return $str . $date . $msec;
  128. }
  129. }
  130. //获取三级架构
  131. if (!function_exists('get_top_customer_org')) {
  132. function get_top_customer_org($var, $data = [])
  133. {
  134. $str = Db::name('customer_org1')
  135. ->field('id,name,pid,level')
  136. ->where(['id' => $var])
  137. ->findOrEmpty();
  138. if (empty($str)) return [];
  139. $vmn = [];
  140. $vmn['id'] = $str['id'];
  141. $vmn['name'] = $str['name'];
  142. $vmn['level'] = $str['level'];
  143. array_unshift($data, $vmn);
  144. // $var['id']=made();
  145. if ($str['pid'] == 0) return $data;
  146. else return get_top_customer_org($str['pid'], $data);
  147. }
  148. }