Base.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. namespace app\extra\basic;
  3. use app\extra\douyin\Client;
  4. use app\extra\tools\CodeExtend;
  5. use app\model\saas\SaasOrderLife;
  6. use app\model\saas\SaasOrderLog;
  7. use app\model\saas\SaasStore;
  8. use app\model\system\SystemUser;
  9. use Overtrue\EasySms\Strategies\OrderStrategy;
  10. use think\Validate;
  11. class Base
  12. {
  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 string $account
  35. * @param string $openId
  36. * @return void
  37. */
  38. protected function asyncDyOrder(string $account = "",string $openId = "")
  39. {
  40. // 同步已经下单的订单
  41. if (!empty($account)) {
  42. $resp = (new Client)->config($this->getDyConfig())->token()->queryOrder($account,$openId);
  43. print_r($resp);
  44. $store = (new SaasStore)->where("store_id",$account)->findOrEmpty();
  45. if (!empty($resp['data']['orders'])) {
  46. $orderData = $orderLog = [];
  47. foreach ($resp['data']['orders'] as $key=>$val) {
  48. if ($val['can_use']) {
  49. $orderEx = (new SaasOrderLife)->where("order_id",$val['order_id'])->findOrEmpty();
  50. $certificates = $val['certificates'][0];
  51. if ($orderEx->isEmpty() && !isset($certificates['lock_info_extra'])) {
  52. $orderData[$key] = [
  53. "open_id" => $openId,
  54. "agent_id" => $store['agent_id']??'',
  55. "store_id" => $store['store_id']??'',
  56. "order_id" => $val['order_id'],
  57. "pay_amount" => $certificates['amount']['pay_amount'],
  58. "order_amount" => $certificates['amount']['original_amount'],
  59. "expire_at" => date("Y-m-d H:i:s",$certificates['expire_time']),
  60. "out_id" => $certificates['sku_info']['sku_id'],
  61. "product_name" => $certificates['sku_info']['title'],
  62. "groupon_type" => $certificates['sku_info']['groupon_type'],
  63. "certificate_id" => $certificates['certificate_id']??'',
  64. "start_time" => date("Y-m-d H:i:s",$certificates['start_time']),
  65. "status" => 1
  66. ];
  67. $orderLog[$key] = [
  68. "order_id" => $val['order_id'],
  69. "title" => "发起订单",
  70. "remark" => "下单并完成支付"
  71. ];
  72. }
  73. }
  74. }
  75. if (!empty($orderData)) {
  76. (new SaasOrderLife)->insertAll(array_values($orderData));
  77. (new SaasOrderLog)->insertAll(array_values($orderLog));
  78. }
  79. }
  80. }
  81. }
  82. /**
  83. * 写入新用户
  84. * @param array $param
  85. * @return bool
  86. */
  87. protected function sceneUser(array $param = [],int $type = 1,$field = "agent_id"): bool
  88. {
  89. if (!isset($param['id']))
  90. {
  91. $param['salt'] = strtoupper(CodeExtend::random(10,3));
  92. $param['password'] = md5($param['password'].$param['salt']);
  93. $param['create_ip'] = request()->getRealIp() ?: '127.0.0.1';
  94. $param['type'] = $type;
  95. }
  96. return (new SystemUser)->setAutoData($param,$field);
  97. }
  98. protected function getSmsChannel(): array
  99. {
  100. return [
  101. [
  102. "name" => "阿里云",
  103. "type" => "aliyun",
  104. "url" => "https://dysms.console.aliyun.com/dysms.htm"
  105. ],
  106. [
  107. "name" => "腾讯云",
  108. "type" => "qcloud",
  109. "url" => "https://console.cloud.tencent.com/smsv2"
  110. ],
  111. [
  112. "name" => "七牛云",
  113. "type" => "qiniu",
  114. "url" => "https://portal.qiniu.com/sms/dashboard"
  115. ]
  116. ];
  117. }
  118. /**
  119. * 快捷输入并验证( 支持 规则 # 别名 )
  120. * @param array $rules 验证规则( 验证信息数组 )
  121. * @param array|string $input 输入方式 ( post. 或 get. )
  122. * @param callable|null $callable 异常处理操作
  123. */
  124. protected function _valid(array $rules, array|string $input = '', ?callable $callable = null)
  125. {
  126. if (is_string($input)) {
  127. $type = trim($input, '.') ?: 'get';
  128. $input = request()->$type();
  129. }
  130. [$data, $rule, $info] = [[], [], []];
  131. foreach ($rules as $name => $message) if (is_numeric($name)) {
  132. [$name, $alias] = explode('#', $message . '#');
  133. $data[$name] = $input[($alias ?: $name)] ?? null;
  134. } elseif (!str_contains($name, '.')) {
  135. $data[$name] = $message;
  136. } elseif (preg_match('|^(.*?)\.(.*?)#(.*?)#?$|', $name . '#', $matches)) {
  137. [, $_key, $_rule, $alias] = $matches;
  138. if (in_array($_rule, ['value', 'default'])) {
  139. if ($_rule === 'value') $data[$_key] = $message;
  140. elseif ($_rule === 'default') $data[$_key] = $input[($alias ?: $_key)] ?? $message;
  141. } else {
  142. $info[explode(':', $name)[0]] = $message;
  143. $data[$_key] = $data[$_key] ?? ($input[($alias ?: $_key)] ?? null);
  144. $rule[$_key] = isset($rule[$_key]) ? ($rule[$_key] . '|' . $_rule) : $_rule;
  145. }
  146. }
  147. $validate = new Validate();
  148. if ($validate->rule($rule)->message($info)->check($data)) {
  149. return $data;
  150. } elseif (is_callable($callable)) {
  151. return call_user_func($callable, $validate->getError(), $data);
  152. } else {
  153. return $validate->getError();
  154. }
  155. }
  156. /**
  157. * 菜单信息格式化
  158. * @param array $menus
  159. * @param int $type
  160. * @param int $mch
  161. * @return array
  162. */
  163. protected function filterMenu(array $menus,int $type,int $mch = 0): array
  164. {
  165. foreach ($menus as &$menu) {
  166. $menu['meta'] = [
  167. "title" => $menu['title'],
  168. "icon" => $menu['icon']??'',
  169. "type" => $menu['type']
  170. ];
  171. if (!empty($menu['children'])) {
  172. $menu['children'] = $this->filterMenu($menu['children'],$type,$mch);
  173. }
  174. if ($mch > 0) {
  175. $menu['component'] = ($mch==1?'merchant/':'store/').$menu['name'];
  176. } else {
  177. $menu['component'] = $menu['name'];
  178. }
  179. $menu['isMenu'] = $menu['status'];
  180. if($type == 1) unset($menu['title'],$menu['icon'],$menu['type'],$menu['pid'],$menu['id']);
  181. }
  182. return $menus;
  183. }
  184. }