Base.php 7.6 KB

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