Base.php 7.6 KB

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