Base.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. namespace app\extra\basic;
  3. use app\model\saas\SaasStore;
  4. use support\Response;
  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. public function getShopId()
  14. {
  15. return request()->header("shop",0);
  16. }
  17. /**
  18. * 保证金检测
  19. */
  20. protected function checkDeposit(string $poiId = ""): array
  21. {
  22. if ($poiId == 0) return [1,'ok'];
  23. $deposit = (new SaasStore)->where("poi_id",$poiId)->findOrEmpty();
  24. if ($deposit->isEmpty()) return [0,"非法操作"];
  25. if ($deposit['deposit'] == 0) return [0,'抖音来客保证金未缴纳,请联系渠道进行保证金缴纳,充值成功后自动开放权限'];
  26. return [1,'ok'];
  27. }
  28. protected function getDyConfig(): array
  29. {
  30. return ["appid" => sConf('wechat.mini_appid'),'secret' => sConf('wechat.mini_secret')];
  31. }
  32. /**
  33. * 短信通道
  34. * @return array[]
  35. */
  36. protected function getSmsChannel(): array
  37. {
  38. return [
  39. [
  40. "name" => "阿里云",
  41. "type" => "aliyun",
  42. "url" => "https://dysms.console.aliyun.com/dysms.htm"
  43. ],
  44. [
  45. "name" => "腾讯云",
  46. "type" => "qcloud",
  47. "url" => "https://console.cloud.tencent.com/smsv2"
  48. ],
  49. [
  50. "name" => "七牛云",
  51. "type" => "qiniu",
  52. "url" => "https://portal.qiniu.com/sms/dashboard"
  53. ]
  54. ];
  55. }
  56. /**
  57. * 快捷输入并验证( 支持 规则 # 别名 )
  58. * @param array $rules 验证规则( 验证信息数组 )
  59. * @param array|string $input 输入方式 ( post. 或 get. )
  60. * @param callable|null $callable 异常处理操作
  61. */
  62. protected function _valid(array $rules, array|string $input = '', ?callable $callable = null)
  63. {
  64. if (is_string($input)) {
  65. $type = trim($input, '.') ?: 'get';
  66. $input = request()->$type();
  67. }
  68. [$data, $rule, $info] = [[], [], []];
  69. foreach ($rules as $name => $message) if (is_numeric($name)) {
  70. [$name, $alias] = explode('#', $message . '#');
  71. $data[$name] = $input[($alias ?: $name)] ?? null;
  72. } elseif (!str_contains($name, '.')) {
  73. $data[$name] = $message;
  74. } elseif (preg_match('|^(.*?)\.(.*?)#(.*?)#?$|', $name . '#', $matches)) {
  75. [, $_key, $_rule, $alias] = $matches;
  76. if (in_array($_rule, ['value', 'default'])) {
  77. if ($_rule === 'value') $data[$_key] = $message;
  78. elseif ($_rule === 'default') $data[$_key] = $input[($alias ?: $_key)] ?? $message;
  79. } else {
  80. $info[explode(':', $name)[0]] = $message;
  81. $data[$_key] = $data[$_key] ?? ($input[($alias ?: $_key)] ?? null);
  82. $rule[$_key] = isset($rule[$_key]) ? ($rule[$_key] . '|' . $_rule) : $_rule;
  83. }
  84. }
  85. $validate = new Validate();
  86. if ($validate->rule($rule)->message($info)->check($data)) {
  87. return $data;
  88. } elseif (is_callable($callable)) {
  89. return call_user_func($callable, $validate->getError(), $data);
  90. } else {
  91. return $validate->getError();
  92. }
  93. }
  94. /**
  95. * @param $data
  96. * @return array|string|null
  97. */
  98. private function parseData($data)
  99. {
  100. if ($data instanceof Arrayable)
  101. return $data->toArray();
  102. else
  103. return $data;
  104. }
  105. /**
  106. * @param string $message
  107. * @param array|null $data
  108. * @return Response
  109. */
  110. public function success(string $message = self::DEFAULT_SUCCESS_MESSAGE, array $data = null): Response
  111. {
  112. $message = $this->parseData($message);
  113. if (is_array($message)) {
  114. $data = $message;
  115. $message = self::DEFAULT_SUCCESS_MESSAGE;
  116. } else {
  117. $data = $this->parseData($data);
  118. }
  119. return $this->make(self::DEFAULT_SUCCESS_CODE, $message, $data);
  120. }
  121. /**
  122. * @param int $status
  123. * @param string $msg
  124. * @param null $data
  125. * @return Response
  126. */
  127. public function make(int $status, string $msg, $data = null)
  128. {
  129. $content = compact('status', 'msg');
  130. if (!is_null($data))
  131. $content['data'] = $this->parseData($data);
  132. return json($content);
  133. }
  134. public function encode($msg = self::DEFAULT_SUCCESS_MESSAGE, $data = null)
  135. {
  136. $msg = $this->parseData($msg);
  137. if (is_array($msg)) {
  138. $data = $msg;
  139. $msg = self::DEFAULT_SUCCESS_MESSAGE;
  140. } else {
  141. $data = $this->parseData($data);
  142. }
  143. $status = self::DEFAULT_SUCCESS_CODE;
  144. $encode = true;
  145. $content = compact('status','encode', 'msg');
  146. if (is_null($data) || env('APP_DEBUG', false))
  147. return $this->success($msg, $data['data'] ?? $data);
  148. if (!is_array($data) || !isset($data['data'])) {
  149. $res['data'] = $data;
  150. } else {
  151. $res = $data;
  152. }
  153. $res['status'] = $status;
  154. $res['msg'] = $msg;
  155. $content['data'] = base64_encode(gzdeflate(json_encode($res,JSON_UNESCAPED_UNICODE),9));
  156. return json($content);
  157. }
  158. /**
  159. * 菜单信息格式化
  160. * @param array $menus
  161. * @param int $type
  162. * @param int $mch
  163. * @return array
  164. */
  165. protected function filterMenu(array $menus,int $type,int $mch = 0): array
  166. {
  167. foreach ($menus as &$menu) {
  168. $menu['meta'] = [
  169. "title" => $menu['title'],
  170. "icon" => $menu['icon']??'',
  171. "type" => $menu['type'],
  172. "hidden" => !($menu['hide'] == 1),
  173. ];
  174. if (!empty($menu['children'])) {
  175. $menu['children'] = $this->filterMenu($menu['children'],$type,$mch);
  176. }
  177. if ($mch > 0) {
  178. $menu['component'] = ($mch==1?'merchant/':'store/').$menu['name'];
  179. } else {
  180. $menu['component'] = $menu['name'];
  181. }
  182. $menu['isMenu'] = $menu['status'];
  183. if($type == 1) unset($menu['title'],$menu['icon'],$menu['type'],$menu['pid'],$menu['id']);
  184. }
  185. return $menus;
  186. }
  187. }