BaseData.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. <?php
  2. namespace app\extra\dyLife\data;
  3. use app\extra\dyLife\BasicLife;
  4. use yzh52521\EasyHttp\Http;
  5. use function DI\string;
  6. class BaseData extends BasicLife
  7. {
  8. /**
  9. * 创建团购商品
  10. * https://bytedance.larkoffice.com/docx/doxcnnyH289B98IgcPiLWxpSChc
  11. * https://partner.open-douyin.com/docs/resource/zh-CN/local-life/develop/OpenAPI/general-capabilities/goods/save
  12. * https://open.douyin.com/goodlife/v1/goods/product/save/
  13. */
  14. public function createGoodsData(array $data = [],array $sku = [],array $store = [],array $valMap = []): array
  15. {
  16. $product_attrs = $sku_attrs = [];
  17. foreach ($valMap['product_attrs'] as $val) {
  18. $keyVal = $this->getProductAttrs($val['key'],$data);
  19. if (!empty($keyVal)) {
  20. $product_attrs[$val['key']] = $keyVal;
  21. }
  22. }
  23. foreach ($valMap['sku_attrs'] as $val) {
  24. $keyVal = $this->getProductAttrs($val['key'],$data);
  25. if (!empty($keyVal)) {
  26. $sku_attrs[$val['key']] = $keyVal;
  27. }
  28. }
  29. $param = [
  30. "account_id" => $store['store_id'], // 商家id
  31. "product" => [
  32. "account_name" => $store['poi_name'], // 商家名
  33. "biz_line" => 5, // 业务线 固定 5 小程序
  34. "category_id" => $data['category_id']??0, // 品类id
  35. "product_name" => $data['product_name'], // 商品名
  36. "product_type" => $data['product_type'], // 商品类型
  37. "attr_key_value_map" => $product_attrs,
  38. "out_id" => $data['product_id'], // 第三方id
  39. "out_url" => json_encode([
  40. "app_id" => sConf("wechat.mini_appid"),
  41. "params" => json_encode(['goods' => $data['product_id']]),
  42. "path" => "pages/goods/detail"
  43. ]), // 第三方跳转链接,小程序商品必填
  44. "pois" => [
  45. [
  46. "poi_id" => $store['poi_id'], // 门店POI
  47. ]
  48. ],
  49. "sold_end_time" => strtotime($data['sold_start_times'][1]), // 售卖结束时间
  50. "sold_start_time" => strtotime($data['sold_start_times'][0]), // 售卖开始时间
  51. "spu_id" => (string)$data['spu_id'], // spuid
  52. "account_id" => $store['store_id'], // 商家id
  53. ]
  54. ];
  55. if (empty($sku)) { // 单sku
  56. $param['sku'] = [
  57. "attr_key_value_map" => $sku_attrs,
  58. "actual_amount" => $data['price'], // 售价 实际支付价格
  59. "sku_name" => $data['product_name'], // sku名
  60. "origin_amount" => $data['line_price'],
  61. "status" => 1,
  62. "stock" => [
  63. "limit_type" => $data['limit_use_rule'], // 库存上限类型,为2时stock_qty和avail_qty字段无意义 1 有限库存 2 无限库存
  64. "avail_qty" => $data['use_num_per_consume']??0, // 可用库存,limit_type=2时无意义,始终保证stock_qty=avail_qty+frozen_qty+sold_qty
  65. ]
  66. ];
  67. } else { // 多sku
  68. }
  69. return $this->curlPostApi("goodlife/v1/goods/product/save/",$param);
  70. }
  71. /**
  72. * 参数处理
  73. * @param string $key
  74. * @param array $data
  75. * @return string
  76. */
  77. protected function getProductAttrs(string $key = "",array $data = []): string
  78. {
  79. switch ($key)
  80. {
  81. case "trade_url": // 小程序提单页跳转
  82. return json_encode([
  83. "app_id" => sConf("wechat.mini_appid"),
  84. "params" => json_encode(['goods' => $data['product_id']]),
  85. "path" => "pages/goods/detail"
  86. ]);
  87. break;
  88. case "commodity": // 商品搭配
  89. $specs = json_decode($data['specs'],true);
  90. $commodity = [];
  91. foreach ($specs as $key=>$val) {
  92. $commodity[$key] = [
  93. "group_name" => $val['name'],
  94. "total_count" => count($val['list']),
  95. "option_count" => $val['num']==0?count($val['list']):$val['num']
  96. ];
  97. foreach ($val['list'] as $sKey=>$sub) {
  98. $commodity[$key]['item_list'][$sKey] = [
  99. "name" => $sub['name'],
  100. "price" => $sub['price'] * 100,
  101. "count" => 1,
  102. "unit" => "份"
  103. ];
  104. }
  105. }
  106. $return = json_encode($commodity);
  107. break;
  108. case "customer_reserved_info":
  109. if ($data['customer_reserved_info'] == 1) { // 不需要留资
  110. $return = json_encode([
  111. "allow" => false
  112. ]);
  113. } else {
  114. $return = json_encode([
  115. "allow" => true,
  116. "allow_tel" => true,
  117. "require_for_tel" => true
  118. ]);
  119. }
  120. break;
  121. case "appointment": // 预约规则
  122. if ($data['booking_type'] == 1) { // 无需预约
  123. $return = json_encode([
  124. "need_appointment" => false
  125. ]);
  126. } else {
  127. if ($data['booking_unit'] == 1) {
  128. $return = json_encode([
  129. "need_appointment" => true,
  130. "ahead_day_num" => $data['booking_date']
  131. ]);
  132. } else {
  133. $return = json_encode([
  134. "need_appointment" => true,
  135. "ahead_hour_num" => $data['booking_date']
  136. ]);
  137. }
  138. }
  139. break;
  140. case "EntryType": // 入口类型 1:H5 2:小程序 3:抖音 4:lynx
  141. $return = "2";
  142. break;
  143. case "market_price": // 划线价格
  144. $return = (string)$data['line_price'];
  145. break;
  146. case "can_no_use_date": // 不可使用日期
  147. if ($data['can_no_use_date'] == 1) {
  148. $return = json_encode(["enable" => false]);
  149. } else { // 指定日期
  150. $noUse = ["enable" => true];
  151. if ($data['can_no_use_week']) { // 每周几不可用
  152. $noUse['days_of_week'] = $data['no_use_weeks'];
  153. }
  154. if ($data['can_no_use_holiday']) { // 节假日不可用
  155. $noUse['holidays'] = $data['no_use_weeks'];
  156. }
  157. if ($data['can_no_use_day']) { // 指定某天不可用
  158. $noUse['date_list'] = $data['no_use_days'];
  159. }
  160. $return = json_encode($noUse);
  161. }
  162. break;
  163. case "image_list": // 封面图 图片比例:375:280
  164. $image_list = json_decode($data['image_list'],true);
  165. $return = json_encode([
  166. [
  167. "url" => $image_list[0]['url']
  168. ]
  169. ]);
  170. break;
  171. case "limit_use_rule": // 限制使用规则
  172. if ($data['rec_person_type']==1) {
  173. $return = json_encode(["is_limit_use" => false]);
  174. } else {
  175. $return = json_encode(['is_limit_use' => true, 'use_num_per_consume' => $data['rec_person_num_max']??0]);
  176. }
  177. break;
  178. case "show_channel":
  179. $return = (string) $data['show_channel'];
  180. break;
  181. case "use_date": // 使用日期 券码的可以核销日期,履约核销强依赖
  182. if ($data['use_date_type'] == 1) { // 指定天数
  183. $return = json_encode([
  184. "use_date_type" => 2,
  185. "day_duration" => $data['day_duration']
  186. ]);
  187. } else { // 指定日期
  188. $return = json_encode([
  189. "use_date_type" => 1,
  190. "use_start_date" => $data['use_times'][0],
  191. "use_end_date" => $data['use_times'][0]
  192. ]);
  193. }
  194. break;
  195. case "use_time": // 使用时间 用户可以消费的时间
  196. $return = json_encode([
  197. "use_time_type" => 1
  198. ]);
  199. break;
  200. case "Notification": // 使用规则
  201. $return = $data['notification'];
  202. break;
  203. case "RefundPolicy": // 退款政策 1-允许退款 2-不可退款 3-有条件退
  204. $return = "1";
  205. break;
  206. case "refund_need_merchant_confirm": // 退款是否需商家审核
  207. $return = "true";
  208. break;
  209. case "code_source_type": // 券码生成方式 "1-抖音码 2-三方码 3-预导码
  210. $return = (string)$data['code_source_type'];
  211. break;
  212. case "settle_type": // 收款方式 1-总店结算 2-分店结算
  213. $return = (string)$data['code_source_type'];
  214. break;
  215. case "limit_rule": // 限制购买
  216. if ($data['limit_rule_type'] == 2) {
  217. $return = json_encode(['is_limit' => true,"total_buy_num" => $data['limit_rule']]);
  218. } else {
  219. $return = json_encode(['is_limit' => false]);
  220. }
  221. break;
  222. case "auto_renew":
  223. $return = $data['auto_renew']==1?'true':'false';
  224. break;
  225. default:
  226. $return = "";
  227. break;
  228. }
  229. return $return;
  230. }
  231. /**
  232. * 获取来客绑定的所有门店
  233. * 一页最多50条
  234. */
  235. public function getStoreData(string $account,int $page = 1,int $size = 50): array
  236. {
  237. $data = [
  238. "account_id" => $account,
  239. "page" => $page,
  240. "size" => $size
  241. ];
  242. return $this->curlGetApi("goodlife/v1/shop/poi/query/",$data);
  243. }
  244. /**
  245. * 获取来客绑定的所有分类
  246. */
  247. public function getStoreCategoryData(string $account): array
  248. {
  249. $data = [
  250. "account_id" => $account,
  251. "query_category_type" => 1
  252. ];
  253. return $this->curlGetApi("goodlife/v1/goods/category/get/",$data);
  254. }
  255. /**
  256. * 查下商品模板
  257. */
  258. public function getStoreCategoryTemplate(string $category_id,int $product_type = 1): array
  259. {
  260. $data = compact("category_id","product_type");
  261. return $this->curlGetApi("goodlife/v1/goods/template/get/",$data);
  262. }
  263. }