Base.php 8.2 KB

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