Base.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. namespace app\extra\basic;
  3. use app\extra\tools\CodeExtend;
  4. use app\model\system\SystemUser;
  5. use Overtrue\EasySms\Strategies\OrderStrategy;
  6. use think\Validate;
  7. class Base
  8. {
  9. protected function getParent(array $data)
  10. {
  11. if ($data['parent_id'] > 0) {
  12. return $data['parent_id'];
  13. }
  14. return $data['user_id'];
  15. }
  16. public function getShopId()
  17. {
  18. return request()->header("shop",0);
  19. }
  20. public function getAccountId(array $data)
  21. {
  22. return $data['account_id'];
  23. }
  24. protected function getDyConfig(): array
  25. {
  26. return ["appid" => sConf('dy.appid'),'secret' => sConf('dy.secret')];
  27. }
  28. /**
  29. * 写入新用户
  30. * @param array $param
  31. * @return bool
  32. */
  33. protected function sceneUser(array $param = [],int $type = 1,$field = "agent_id"): bool
  34. {
  35. if (!isset($param['id']))
  36. {
  37. $param['salt'] = strtoupper(CodeExtend::random(10,3));
  38. $param['password'] = md5($param['password'].$param['salt']);
  39. $param['create_ip'] = request()->getRealIp() ?: '127.0.0.1';
  40. $param['type'] = $type;
  41. }
  42. return (new SystemUser)->setAutoData($param,$field);
  43. }
  44. protected function getSmsChannel(): array
  45. {
  46. return [
  47. [
  48. "name" => "阿里云",
  49. "type" => "aliyun",
  50. "url" => "https://dysms.console.aliyun.com/dysms.htm"
  51. ],
  52. [
  53. "name" => "腾讯云",
  54. "type" => "qcloud",
  55. "url" => "https://console.cloud.tencent.com/smsv2"
  56. ],
  57. [
  58. "name" => "七牛云",
  59. "type" => "qiniu",
  60. "url" => "https://portal.qiniu.com/sms/dashboard"
  61. ]
  62. ];
  63. }
  64. /**
  65. * 快捷输入并验证( 支持 规则 # 别名 )
  66. * @param array $rules 验证规则( 验证信息数组 )
  67. * @param array|string $input 输入方式 ( post. 或 get. )
  68. * @param callable|null $callable 异常处理操作
  69. */
  70. protected function _valid(array $rules, array|string $input = '', ?callable $callable = null)
  71. {
  72. if (is_string($input)) {
  73. $type = trim($input, '.') ?: 'get';
  74. $input = request()->$type();
  75. }
  76. [$data, $rule, $info] = [[], [], []];
  77. foreach ($rules as $name => $message) if (is_numeric($name)) {
  78. [$name, $alias] = explode('#', $message . '#');
  79. $data[$name] = $input[($alias ?: $name)] ?? null;
  80. } elseif (!str_contains($name, '.')) {
  81. $data[$name] = $message;
  82. } elseif (preg_match('|^(.*?)\.(.*?)#(.*?)#?$|', $name . '#', $matches)) {
  83. [, $_key, $_rule, $alias] = $matches;
  84. if (in_array($_rule, ['value', 'default'])) {
  85. if ($_rule === 'value') $data[$_key] = $message;
  86. elseif ($_rule === 'default') $data[$_key] = $input[($alias ?: $_key)] ?? $message;
  87. } else {
  88. $info[explode(':', $name)[0]] = $message;
  89. $data[$_key] = $data[$_key] ?? ($input[($alias ?: $_key)] ?? null);
  90. $rule[$_key] = isset($rule[$_key]) ? ($rule[$_key] . '|' . $_rule) : $_rule;
  91. }
  92. }
  93. $validate = new Validate();
  94. if ($validate->rule($rule)->message($info)->check($data)) {
  95. return $data;
  96. } elseif (is_callable($callable)) {
  97. return call_user_func($callable, $validate->getError(), $data);
  98. } else {
  99. return $validate->getError();
  100. }
  101. }
  102. /**
  103. * 菜单信息格式化
  104. * @param array $menus
  105. * @param int $type
  106. * @param int $mch
  107. * @return array
  108. */
  109. protected function filterMenu(array $menus,int $type,int $mch = 0): array
  110. {
  111. foreach ($menus as &$menu) {
  112. $menu['meta'] = [
  113. "title" => $menu['title'],
  114. "icon" => $menu['icon']??'',
  115. "type" => $menu['type']
  116. ];
  117. if (!empty($menu['children'])) {
  118. $menu['children'] = $this->filterMenu($menu['children'],$type,$mch);
  119. }
  120. if ($mch > 0) {
  121. $menu['component'] = ($mch==1?'merchant/':'store/').$menu['name'];
  122. } else {
  123. $menu['component'] = $menu['name'];
  124. }
  125. $menu['isMenu'] = $menu['status'];
  126. if($type == 1) unset($menu['title'],$menu['icon'],$menu['type'],$menu['pid'],$menu['id']);
  127. }
  128. return $menus;
  129. }
  130. }