Base.php 7.3 KB

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