Base.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. namespace app\extra\basic;
  3. use app\extra\tools\CodeExtend;
  4. use app\model\system\SystemUser;
  5. use think\contract\Arrayable;
  6. use think\Validate;
  7. class Base
  8. {
  9. const DEFAULT_SUCCESS_MESSAGE = 'success';
  10. const DEFAULT_FAIL_MESSAGE = 'fail';
  11. const DEFAULT_SUCCESS_CODE = 200;
  12. const DEFAULT_FAIL_CODE = 400;
  13. protected function getParent(array $data)
  14. {
  15. if ($data['parent_id'] > 0) {
  16. return $data['parent_id'];
  17. }
  18. return $data['user_id'];
  19. }
  20. public function getShopId()
  21. {
  22. return request()->header("shop",0);
  23. }
  24. public function getAccountId(array $data)
  25. {
  26. return $data['account_id'];
  27. }
  28. protected function getDyConfig(): array
  29. {
  30. return ["appid" => sConf('dy.appid'),'secret' => sConf('dy.secret')];
  31. }
  32. /**
  33. * 写入新用户
  34. * @param array $param
  35. * @return bool
  36. */
  37. protected function sceneUser(array $param = [],int $type = 1,$field = "agent_id"): bool
  38. {
  39. if (!isset($param['id']))
  40. {
  41. $param['salt'] = strtoupper(CodeExtend::random(10,3));
  42. $param['password'] = md5($param['password'].$param['salt']);
  43. $param['create_ip'] = request()->getRealIp() ?: '127.0.0.1';
  44. $param['type'] = $type;
  45. }
  46. return (new SystemUser)->setAutoData($param,$field);
  47. }
  48. protected function getSmsChannel(): array
  49. {
  50. return [
  51. [
  52. "name" => "阿里云",
  53. "type" => "aliyun",
  54. "url" => "https://dysms.console.aliyun.com/dysms.htm"
  55. ],
  56. [
  57. "name" => "腾讯云",
  58. "type" => "qcloud",
  59. "url" => "https://console.cloud.tencent.com/smsv2"
  60. ],
  61. [
  62. "name" => "七牛云",
  63. "type" => "qiniu",
  64. "url" => "https://portal.qiniu.com/sms/dashboard"
  65. ]
  66. ];
  67. }
  68. /**
  69. * 快捷输入并验证( 支持 规则 # 别名 )
  70. * @param array $rules 验证规则( 验证信息数组 )
  71. * @param array|string $input 输入方式 ( post. 或 get. )
  72. * @param callable|null $callable 异常处理操作
  73. */
  74. protected function _valid(array $rules, array|string $input = '', ?callable $callable = null)
  75. {
  76. if (is_string($input)) {
  77. $type = trim($input, '.') ?: 'get';
  78. $input = request()->$type();
  79. }
  80. [$data, $rule, $info] = [[], [], []];
  81. foreach ($rules as $name => $message) if (is_numeric($name)) {
  82. [$name, $alias] = explode('#', $message . '#');
  83. $data[$name] = $input[($alias ?: $name)] ?? null;
  84. } elseif (!str_contains($name, '.')) {
  85. $data[$name] = $message;
  86. } elseif (preg_match('|^(.*?)\.(.*?)#(.*?)#?$|', $name . '#', $matches)) {
  87. [, $_key, $_rule, $alias] = $matches;
  88. if (in_array($_rule, ['value', 'default'])) {
  89. if ($_rule === 'value') $data[$_key] = $message;
  90. elseif ($_rule === 'default') $data[$_key] = $input[($alias ?: $_key)] ?? $message;
  91. } else {
  92. $info[explode(':', $name)[0]] = $message;
  93. $data[$_key] = $data[$_key] ?? ($input[($alias ?: $_key)] ?? null);
  94. $rule[$_key] = isset($rule[$_key]) ? ($rule[$_key] . '|' . $_rule) : $_rule;
  95. }
  96. }
  97. $validate = new Validate();
  98. if ($validate->rule($rule)->message($info)->check($data)) {
  99. return $data;
  100. } elseif (is_callable($callable)) {
  101. return call_user_func($callable, $validate->getError(), $data);
  102. } else {
  103. return $validate->getError();
  104. }
  105. }
  106. /**
  107. * @param $data
  108. * @return array|string|null
  109. */
  110. private function parseData($data)
  111. {
  112. if ($data instanceof Arrayable)
  113. return $data->toArray();
  114. else
  115. return $data;
  116. }
  117. /**
  118. * @param string|array|Arrayable $message
  119. * @param array|Arrayable|null $data
  120. * @return Json
  121. */
  122. public function success($message = self::DEFAULT_SUCCESS_MESSAGE, $data = null)
  123. {
  124. $message = $this->parseData($message);
  125. if (is_array($message)) {
  126. $data = $message;
  127. $message = self::DEFAULT_SUCCESS_MESSAGE;
  128. } else {
  129. $data = $this->parseData($data);
  130. }
  131. return $this->make(self::DEFAULT_SUCCESS_CODE, $message, $data);
  132. }
  133. /**
  134. * @param int $status
  135. * @param string $message
  136. * @param array|Arrayable|null $data
  137. */
  138. public function make(int $status, string $message, $data = null)
  139. {
  140. $content = compact('status', 'message');
  141. if (!is_null($data))
  142. $content['data'] = $this->parseData($data);
  143. return json($content);
  144. }
  145. public function encode($message = self::DEFAULT_SUCCESS_MESSAGE, $data = null)
  146. {
  147. $message = $this->parseData($message);
  148. if (is_array($message)) {
  149. $data = $message;
  150. $message = self::DEFAULT_SUCCESS_MESSAGE;
  151. } else {
  152. $data = $this->parseData($data);
  153. }
  154. $status = self::DEFAULT_SUCCESS_CODE;
  155. $encode = true;
  156. $content = compact('status','encode', 'message');
  157. if (is_null($data) || env('APP_DEBUG', false))
  158. return $this->success($message, $data['data'] ?? $data);
  159. if (!is_array($data) || !isset($data['data'])) {
  160. $res['data'] = $data;
  161. } else {
  162. $res = $data;
  163. }
  164. $res['status'] = $status;
  165. $res['message'] = $message;
  166. $content['data'] = base64_encode(gzdeflate(json_encode($res,JSON_UNESCAPED_UNICODE),9));
  167. return json($content);
  168. }
  169. /**
  170. * 菜单信息格式化
  171. * @param array $menus
  172. * @param int $type
  173. * @param int $mch
  174. * @return array
  175. */
  176. protected function filterMenu(array $menus,int $type,int $mch = 0): array
  177. {
  178. foreach ($menus as &$menu) {
  179. $menu['meta'] = [
  180. "title" => $menu['title'],
  181. "icon" => $menu['icon']??'',
  182. "type" => $menu['type']
  183. ];
  184. if (!empty($menu['children'])) {
  185. $menu['children'] = $this->filterMenu($menu['children'],$type,$mch);
  186. }
  187. if ($mch > 0) {
  188. $menu['component'] = ($mch==1?'merchant/':'store/').$menu['name'];
  189. } else {
  190. $menu['component'] = $menu['name'];
  191. }
  192. $menu['isMenu'] = $menu['status'];
  193. if($type == 1) unset($menu['title'],$menu['icon'],$menu['type'],$menu['pid'],$menu['id']);
  194. }
  195. return $menus;
  196. }
  197. }