BaseData.php 13 KB

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