'未上架', 5 => '上架中', 11 => '已下架', 13 => '违规下架' ]; /** * 审核状态 * @var string[] */ private $editStatus = [ 0 => '审核中', 1 => '编辑中', 2 => '审核中', 3 => '审核失败', 4 => '审核成功', ]; private $mime = [ 'image/jpeg' => '.jpg', 'image/png' => '.png', 'image/gif' => '.gif', 'image/bmp' => '.bmp', 'image/webp' => '.webp', ]; public $image_service; public $driver; public function getBillList($condition = [], $page = 1, $site_id) { $aliapp = new MinCode($site_id); $config = new Config(); $config_info = $config->getAppConfig($site_id)['data']['value']; $data = [ 'mini_app_id' => $config_info['appid'], 'page_num' => 1, 'page_size' => 10, 'order_status' => 'PAID', ]; $res = $aliapp->requestApi('alipay.open.mini.order.itembill.batchquery', array_merge($data, $condition))['alipay_open_mini_order_itembill_batchquery_response']; if ($res['code'] != 10000 || !isset($res['order_list'])) { $res = [ 'code' => 1, 'msg' => '暂无数据', 'page_num' => 1, 'page_size' => 10, 'total' => 0, 'order_list' => [] ]; } else { if (count($res['order_list'])) { $out_trade_no = array_column($res['order_list'], 'out_trade_no'); $pay_out_trade_no = model('pay', 0)->getColumn([['out_trade_no', 'in', $out_trade_no]], '*', 'out_trade_no'); $order_info = model('order', 0)->getColumn([['out_trade_no', 'in', $out_trade_no]], '*', 'out_trade_no'); foreach ($res['order_list'] as &$item) { if (isset($pay_out_trade_no[$item['out_trade_no']]) && isset($order_info[$item['out_trade_no']]) && $order_info[$item['out_trade_no']]['order_status'] != -1) { $item['out_order_id'] = $order_info[$item['out_trade_no']]['order_id']; $item['is_sync_bill'] = 1; $item['out_order_status'] = $order_info[$item['out_trade_no']]['order_status']; $item['is_sync_delivery'] = $order_info[$item['out_trade_no']]['order_status']; } else { $item['is_sync_bill'] = 0; $item['is_sync_delivery'] = 0; $item['out_order_id'] = 0; $item['out_order_status'] = ''; } } } $res['code'] = 0; } return $res; } /** * 获取商品列表 * @param array $condition * @param bool $field * @param string $order * @param int $page * @param int $list_rows * @param string $alias * @param array $join * @return array */ public function getGoodsPageList($condition = [], $field = true, $order = '', $page = 1, $list_rows = PAGE_LIST_ROWS, $alias = 'a', $join = []) { $field = 'sg.*,g.goods_name,g.goods_image,g.price,g.goods_stock,g.recommend_way'; $alias = 'sg'; $join = [ ['goods g', 'g.goods_id = sg.out_item_id', 'inner'], ]; $data = model('shopalilife_goods')->pageList($condition, $field, $order, $page, $list_rows, $alias, $join); if (!empty($data['list'])) { foreach ($data['list'] as $k => $item) { $data['list'][$k]['goods_stock'] = numberFormat($data['list'][$k]['goods_stock']); $arr_img = explode(',', $item['goods_image']); $data['list'][$k]['cover_img'] = $arr_img[0] ?? ''; $data['list'][$k]['create_time'] = $item['create_time'] > 0 ? time_to_date($item['create_time']) : '--'; $data['list'][$k]['audit_time'] = $item['audit_time'] > 0 ? time_to_date($item['audit_time']) : '--'; if ($item['reject_reason']) { $reject_reason = json_decode($item['reject_reason'], true); if ($reject_reason) { $data['list'][$k]['reject_reason'] = implode('', array_column($reject_reason, 'remark')); } } } } return $this->success($data); } /*** * 推送普通商品 * @return void */ public function PushPTSyncGoodsInfo($goods_id, $data) { $join = [ ['goods g', 'g.goods_id=a.goods_id', 'right'] ]; $where = [ ['g.goods_id', '=', $goods_id] ]; $goods_info = $this->getGoodsInfo($where, 'g.*,a.goods_id as gid,a.spu_data,a.spu_status,a.item_type,a.api_type,a.category_id,a.skus', 'a', $join); $sku_list = model('goods_sku')->getList([['goods_id', '=', $goods_id]], 'sku_id,sku_name,sku_no,sku_image,discount_price,market_price,stock,sku_spec_format'); if (!empty($goods_info['gid'])) { $skus = json_decode($goods_info['skus'], true); $spu_data = json_decode($goods_info['spu_data'], true); $spu_data_names = []; if ($spu_data) { $spu_data_names = array_column($spu_data, 'sku_name'); } $out_sku_ids = array_column($skus, 'out_sku_id'); $sku_ids = array_column($sku_list, 'sku_id'); $sku_names = array_column($sku_list, 'sku_name'); if (array_diff($out_sku_ids, $sku_ids) || array_diff($sku_names, $spu_data_names)) { $param['goods_id'] = $goods_id; $param['site_id'] = $goods_info['site_id']; $api = 'alipay.open.app.item.modify'; $response = 'alipay_open_app_item_modify_response'; return $this->SyncAlipayGoods($param, $api, $response, $data['alipaycategory_id'] ?? ''); } else { return $this->syncDtockPrice($goods_id, $goods_info['site_id']); } } else { $api = 'alipay.open.app.item.create'; $key = 'alipay_open_app_item_create_response'; $data['site_id'] = $goods_info['site_id']; $data['goods_id'] = $goods_id; $data['goods_name'] = $goods_info['goods_name']; return $this->SyncAlipayGoods($data, $api, $key, $data['alipaycategory_id']); } } public function PushLocaSyncGoodsInfo($goods_id, $data) { } /** * 同步商品库商品 */ public function syncGoods($start = 1, $limit = 20, $site_id, $type = 'allcategory') { if ($type == 'allcategory') { $api = 'alipay.open.app.item.list.query'; $key = 'alipay_open_app_item_list_query_response'; } else { $api = 'alipay.open.app.localitem.allcategory.query'; $key = 'alipay_open_app_localitem_allcategory_query_response'; } $aliapp = new MinCode($site_id); $res = $aliapp->requestApi($api, ['page_num' => $start, 'page_size' => $limit]); $sync_res = $res[$key]; if ($sync_res['code'] != 10000) return $sync_res; if (!empty($sync_res['items'])) { foreach ($sync_res['items'] as $goods_item) { $count = model('shopalilife_goods')->getCount([['out_item_id', '=', $goods_item['out_item_id']], ['site_id', '=', $site_id]]); $is_sync = model('goods')->getCount([['goods_id', '=', $goods_item['out_item_id']], ['site_id', '=', $site_id]]); if ($count) { model('shopalilife_goods')->update( [ 'spu_status' => $goods_item['spu_status'], 'stock_num' => $goods_item['stock_num'], 'category_id' => $goods_item['category_id'], 'item_id' => $goods_item['item_id'] ], [ ['out_item_id', '=', $goods_item['out_item_id']] ] ); } else { $category = (new Category())->getCategoryInfo($goods_item['category_id'], $site_id); $temp = [ 'site_id' => $site_id, 'is_sync' => $is_sync, 'out_item_id' => $goods_item['out_item_id'], 'item_id' => $goods_item['item_id'], 'stock_num' => $goods_item['stock_num'], 'category_id' => $goods_item['category_id'], 'api_type' => $type, 'spu_status' => $goods_item['spu_status'], 'create_time' => time(), 'update_time' => time(), 'cat_name' => $category['cat_name'] ]; model('shopalilife_goods')->add($temp); } } $total_page = ceil($sync_res['total'] / $limit); return $this->success(['page' => $start, 'total_page' => $total_page]); } else { return $this->success(['page' => $start, 'total_page' => 1]); } } /*** * 支付宝状态更新 * @param $param * @return array */ public function StatusNotify($param) { $biz_content = $param['biz_content'] ?? ''; if ($biz_content) { $goods_data = [ 'spu_status' => $biz_content['spu_status'], 'is_sync' => 1, 'reject_reason' => json_encode($biz_content['reasons'] ?? '') ?? '' ]; model('shopalilife_goods')->update($goods_data, [['out_item_id', '=', $biz_content['out_item_id']]]); } return $this->success(); } /*** * 同步线上价格 * @param $goods_id * @param $site_id * @param $type * @return array */ public function syncDtockPrice($goods_id, $site_id) { $api = 'alipay.open.app.item.direct.modify'; $key = 'alipay_open_app_item_direct_modify_response'; $goods_info = model('goods')->getInfo([['goods_id', '=', $goods_id], ['site_id', '=', $site_id]], 'goods_id,goods_name,goods_image,sku_id,goods_content'); $miniapp = new MinCode($site_id); // 同步商品所需数据 $spu_data = [ 'out_item_id' => $goods_info['goods_id'], 'skus' => [] ]; $sku_list = model('goods_sku')->getList([['goods_id', '=', $goods_info['goods_id']]], 'sku_id,sku_no,sku_image,discount_price,market_price,stock,sku_spec_format'); if (count($sku_list) > 1) { foreach ($sku_list as $sku_item) { if ($sku_item['market_price'] <= $sku_item['discount_price']) { $sku_item['market_price'] = $sku_item['discount_price'] + $sku_item['discount_price'] * 0.5; } $sku_data = [ 'out_sku_id' => $sku_item['sku_id'], 'sale_status' => 'AVAILABLE', 'stock_num' => numberFormat($sku_item['stock']), 'sale_price' => $sku_item['discount_price'] * 100, 'original_price' => $sku_item['market_price'] * 100, ]; array_push($spu_data['skus'], $sku_data); } } else { $sku_item = $sku_list[0]; $spu_data['sale_price'] = $sku_item['discount_price'] * 100; $spu_data['original_price'] = $sku_item['market_price'] * 100; $spu_data['stock_num'] = numberFormat($sku_item['stock']); $spu_data['sale_status'] = 'AVAILABLE'; unset($spu_data['skus']); if ($spu_data['original_price'] <= $spu_data['sale_price']) { $spu_data['original_price'] = $spu_data['sale_price'] + $spu_data['sale_price'] * 0.5; } } $add_res = $miniapp->requestApi($api, $spu_data)[$key]; if ($add_res['code'] != 10000) return $this->error('', $add_res['sub_msg']); $goods_data['item_id'] = $add_res['item_id']; $goods_data['update_time'] = time(); model('shopalilife_goods')->update($goods_data, [['out_item_id', '=', $goods_id], ['site_id', '=', $site_id]]); return $this->success(); } /*** * 同步本地商品价格 * @param $goods_id * @param $site_id * @param $type * @return array|void */ public function syncLocalitemPrice($goods_id, $site_id) { $api = 'alipay.open.app.localitem.direct.modify'; $key = 'alipay_open_app_localitem_direct_modify_response'; $goods_info = model('goods')->getInfo([['goods_id', '=', $goods_id], ['site_id', '=', $site_id]], 'goods_id,goods_name,goods_image,sku_id,goods_content'); $miniapp = new MinCode($site_id); // 同步商品所需数据 $groupboy = new \addon\groupbuy\model\Groupbuy(); $gwhere = [ ['g.goods_id', '=', $goods_id] ]; $groupboyInfo = $groupboy->getGroupbuyInfo($gwhere)['data']; $spu_data = [ 'out_item_id' => $goods_info['goods_id'], 'sold_time' => [ 'start_time' => date('Y-m-d H:i:s', $groupboyInfo['start_time']), 'end_time' => date('Y-m-d H:i:s', $groupboyInfo['end_time']), ], 'skus' => [ [ 'sale_price' => $groupboyInfo['groupbuy_price'] * 100, 'original_price' => $groupboyInfo['price'] * 100, 'sale_status' => 'AVAILABLE', 'stock_num' => numberFormat($groupboyInfo['goods_stock']), ] ] ]; $add_res = $miniapp->requestApi($api, $spu_data)[$key]; if ($add_res['code'] != 10000) return $this->error('', $add_res['sub_msg']); $goods_data['item_id'] = $add_res['item_id']; $goods_data['update_time'] = time(); model('shopalilife_goods')->update($goods_data, [['out_item_id', '=', $goods_id], ['site_id', '=', $site_id]]); return $this->success(); } /*** * 查询商品信息 * @param $site_id * @param $item_id * @param $type * @return mixed */ public function goodsQuery($site_id, $item_id, $type = 'allcategory') { $aliapp = new MinCode($site_id); if ($type == 'allcategory') { $api = 'alipay.open.app.item.query'; $key = 'alipay_open_app_item_modify_response'; } else { $api = 'alipay.open.app.localitem.modify'; $key = 'alipay_open_app_localitem_modify_response'; } $res = $aliapp->requestApi('alipay.open.app.item.query', ['item_id' => $item_id]); $query = $res['alipay_open_app_item_query_response']; return $query; } /*** * 获取单品信息 * @param $param * @return mixed */ public function getGoodsInfo($param, $field = '*', $alias = 'a', $join = null, $data = null) { $goodsInfo = model('shopalilife_goods')->getInfo($param, $field, $alias, $join); $goodsInfo['apptype'] = 'aliapp'; return $goodsInfo; } /** * 添加商品 * @param $param */ public function addGoods($param, $type = 'allcategory') { $goods_list = model('goods')->getList([['goods_id', 'in', explode(',', $param['goods_ids'])], ['site_id', '=', $param['site_id']]], 'goods_id,goods_name,goods_image,sku_id,goods_content'); if (!empty($goods_list)) { $category = (new Category())->getCategoryInfo($param['cat_id'], $param['site_id']); $miniapp = new MinCode($param['site_id']); if ($type == 'allcategory') { $api = 'alipay.open.app.item.create'; $key = 'alipay_open_app_item_create_response'; } else { $api = 'alipay.open.app.localitem.create'; $key = 'alipay_open_app_localitem_create_response'; } $miniConfig = $miniapp->getConfig(); foreach ($goods_list as $goods_item) { // 需加到库中的商品数据 $goods_data = [ 'out_item_id' => $goods_item['goods_id'], 'category_id' => $param['cat_id'] ]; //处理图片 $goods_image_arr = $this->handleImg($goods_item['goods_image'], 'BIG'); $image_list = []; $image_urls = []; $this->driver = config('upload')['driver'] ?? 'gd'; $this->image_service = new ImageService($this->driver); foreach ($goods_image_arr as $img_k => $img_y) { if (strpos($img_y, 'http://') !== false || strpos($img_y, 'https://') !== false) { $image = $this->image_service->open(img($img_y))->thumb(750, 750); $file = root_path() . 'runtime/temp/' . $goods_item['goods_id'] . $this->mime[$image->image->mime]; $image->save($file, 100); $image = $miniapp->imageUpload($img_k . $goods_item['goods_name'], $file, true); $res = $image['alipay_offline_material_image_upload_response']; if ($res['code'] == 10000) { if (count($image_list) < 3) { $image_list[] = $res['image_id']; $image_urls[] = $res['image_url']; } } } else { $image_urls[] = 'https://oalipay-dl-django.alicdn.com/rest/1.0/image?fileIds=' . $img_y; if (count($image_list) < 3) { $image_list[] = $img_y; } } } $url = urlencode("pages_market/goods/detail?goods_id={$goods_item[ 'goods_id' ]}"); // 同步商品所需数据 $spu_data = [ 'title' => $goods_item['goods_name'], 'path' => "alipays://platformapi/startApp?appId={$miniConfig['appid']}&page={$url}", 'head_img' => $image_list[0], 'image_list' => $image_list, 'desc_info' => [ 'desc' => $goods_item['goods_name'] ], 'skus' => [] ]; $sku_list = model('goods_sku')->getList([['goods_id', '=', $goods_item['goods_id']]], 'sku_id,sku_no,sku_image,discount_price,market_price,stock,sku_spec_format'); if (count($sku_list) > 1) { foreach ($sku_list as $keys => $sku_item) { if ($keys >= 50) { continue; } if ($sku_item['market_price'] <= $sku_item['discount_price']) { $sku_item['market_price'] = $sku_item['discount_price'] + $sku_item['discount_price'] * 0.5; } $sku_image = $this->handleImg($sku_item['sku_image'], 'MID')[0]; if (strpos($img_y, 'http://') !== false || strpos($img_y, 'https://') !== false) { //图片处理 $image = $this->image_service->open(img($this->handleImg($sku_item['sku_image'])[0]))->thumb(400, 400); $file = root_path() . 'runtime/temp/' . $sku_item['sku_id'] . $this->mime[$image->image->mime]; $image->save($file, 100); $sku_res = $miniapp->imageUpload($sku_item['sku_id'], $file, true)['alipay_offline_material_image_upload_response']; if ($sku_res['code'] == 10000) { $sku_image = $sku_res['image_id']; } } $sku_data = [ 'out_sku_id' => $sku_item['sku_id'], 'thumb_img' => $sku_image, 'sale_status' => 'AVAILABLE', 'stock_num' => numberFormat($sku_item['stock']), 'sale_price' => $sku_item['discount_price'] * 100, 'original_price' => $sku_item['market_price'] * 100, 'item_details_page_model' => 0, 'sku_attrs' => [], ]; if (!empty($sku_item['sku_spec_format'])) { foreach (json_decode($sku_item['sku_spec_format'], true) as $spec_item) { array_push($sku_data['sku_attrs'], [ 'attr_key' => $spec_item['spec_name'], 'attr_value' => $spec_item['spec_value_name'] ]); } } else { $sku_data['sku_attrs'] = [ [ 'attr_key' => $sku_item['sku_id'], 'attr_value' => $sku_item['sku_id'] ] ]; } array_push($spu_data['skus'], $sku_data); } } else { $sku_item = $sku_list[0]; $spu_data['sale_price'] = $sku_item['discount_price'] * 100; $spu_data['original_price'] = $sku_item['market_price'] * 100; $spu_data['stock_num'] = numberFormat($sku_item['stock']); $spu_data['sale_status'] = 'AVAILABLE'; $spu_data['item_details_page_model'] = 0; unset($spu_data['skus']); if ($spu_data['original_price'] <= $spu_data['sale_price']) { return $this->error('', '销售价格必须小于划线价'); } } $add_res = $miniapp->requestApi($api, array_merge($goods_data, $spu_data))[$key]; if ($add_res['code'] != 10000) return $this->error('', $add_res['sub_msg']); $goods_data['is_sync'] = 1; $goods_data['api_type'] = $type; $goods_data['spu_status'] = 'AUDITING'; $goods_data['goods_id'] = $goods_item['goods_id']; $goods_data['item_id'] = $add_res['item_id']; $goods_data['goods_image'] = implode(',', $image_urls); $goods_data['goods_content'] = $this->getGoodsContent($param['site_id'], $goods_item); $goods_data['skus'] = json_encode($add_res['skus']); $goods_data['spu_data'] = json_encode($spu_data); $goods_data['create_time'] = time(); $goods_data['site_id'] = $param['site_id']; $goods_data['cat_name'] = $category['cat_name']; model('shopalilife_goods')->add($goods_data); } return $this->success(); } else { return $this->error('', '未获取到要添加的商品'); } } /**** * 添加新商品任务 * @param $param * @param $type * @return array|void */ public function newAddGoods($param, $type = 'allcategory', $item_type = 1) { if (isset($param['alipay_cat_id']) && !$param['alipay_cat_id']) return $this->error(); $category = (new Category())->getCategoryInfo($param['alipay_cat_id'], $param['site_id']); if ($category) { $goods_data['is_sync'] = 0; $goods_data['out_item_id'] = $param['goods_id']; $goods_data['goods_id'] = $param['goods_id']; $goods_data['goods_name'] = $param['goods_name']; $goods_data['item_id'] = 0; $goods_data['item_type'] = $item_type; $goods_data['edit_status'] = 1; $goods_data['api_type'] = $type; $goods_data['category_id'] = $param['alipay_cat_id']; $goods_data['spu_status'] = 'NOTSYNC'; $goods_data['skus'] = '{}'; $goods_data['create_time'] = time(); $goods_data['site_id'] = $param['site_id']; $goods_data['cat_name'] = $category['cat_name']; $res = model('shopalilife_goods')->add($goods_data); return $this->success($res); } return $this->error(); } /*** * 自动同步同步商品支付宝 * @return void */ public function AoutSyncGoodsAlipay() { $where = [ ['l.is_sync', '=', 0] ]; $join = [ ['goods g', 'l.goods_id=g.goods_id', 'inner'] ]; $field = 'l.*'; $goodsList = model('shopalilife_goods')->getList($where, $field, 'create_time desc', 'l', $join, '', 1); if ($goodsList) { foreach ($goodsList as $item) { $item['cat_id'] = $item['category_id']; if ($item['spu_status'] == 'NOTSYNC' && $item['api_type'] == 'allcategory') { $api = 'alipay.open.app.item.create'; $key = 'alipay_open_app_item_create_response'; $this->SyncAlipayGoods($item, $api, $key); } else if ($item['edit_status'] == 1 && $item['api_type'] == 'allcategory') { $this->SyncAlipayGoods($item); } else if ($item['spu_status'] == 'NOTSYNC' && $item['api_type'] != 'allcategory') { $this->localitemAddGoods($item, $item['item_type']); } else if ($item['edit_status'] == 0 && $item['api_type'] != 'allcategory') { $this->updateGoodslocalitemGoods($item['goods_id'], $item, $item['item_type']); } else { echo date('Y-m-d', time()); } } } return $this->success(); } /*** * 同步支付宝修改支付宝 * @param $param * @param $api * @param $response * @return array * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ public function SyncAlipayGoods($param, $api = 'alipay.open.app.item.modify', $response = 'alipay_open_app_item_modify_response', $category_id = '') { $field = 'sg.*,g.goods_id,g.goods_name,g.goods_image,g.sku_id,g.goods_content'; $alias = 'sg'; $join = [ ['goods g', 'g.goods_id = sg.out_item_id', 'inner'], ]; $condition = [ ['g.goods_id', '=', $param['goods_id']], ['g.site_id', '=', $param['site_id']], ]; $goods_info = model('shopalilife_goods')->getInfo($condition, $field, $alias, $join); $error = ''; if (!empty($goods_info)) { $cat_id = $category_id ?: $goods_info['category_id']; $category = (new Category())->getCategoryInfo($cat_id, $param['site_id']); $miniapp = new MinCode($param['site_id']); //处理图片 $goods_image_arr = $this->handleImg($goods_info['goods_image'], 'BIG'); $image_list = []; $image_urls = []; $this->driver = config('upload')['driver'] ?? 'gd'; $this->image_service = new ImageService($this->driver); foreach ($goods_image_arr as $img_k => $img_y) { if (strpos($img_y, 'http://') !== false || strpos($img_y, 'https://') !== false) { $image = $this->image_service->open(img($img_y))->thumb(750, 750); $file = root_path() . 'runtime/temp/' . $goods_info['goods_id'] . $this->mime[$image->image->mime]; $image->save($file, 100); $image = $miniapp->imageUpload($goods_info['goods_id'], $file, true); $res = $image['alipay_offline_material_image_upload_response']; if ($res['code'] == 10000) { if (count($image_list) < 3) { $image_list[] = $res['image_id']; } $image_urls[] = $res['image_url']; } } else { if (count($image_list) < 3) { $image_list[] = $res['image_id']; } } } $url = urlencode("pages_market/goods/detail?goods_id={$goods_info[ 'goods_id' ]}"); $miniConfig = $miniapp->getConfig(); // 同步商品所需数据 $spu_data = [ 'out_item_id' => $goods_info['goods_id'], 'category_id' => $cat_id, 'title' => $goods_info['goods_name'], 'path' => "alipays://platformapi/startApp?appId={$miniConfig['appid']}&page={$url}", 'head_img' => $image_list[0], 'desc_info' => [ 'desc' => $goods_info['goods_name'] ], 'skus' => [] ]; $sku_list = Db::name('goods_sku')->where([['goods_id', '=', $goods_info['goods_id']]])->field('sku_id,sku_no,sku_image,discount_price,market_price,stock,sku_spec_format')->select(); if (count($sku_list) > 1) { foreach ($sku_list as $keys => $sku_item) { if ($keys > 50) { $error = 'SKU大于50个部分属性丢失'; continue; } if ($sku_item['market_price'] <= $sku_item['discount_price']) { $sku_item['market_price'] = $sku_item['discount_price'] + $sku_item['discount_price'] * 0.5; } $sku_image = $this->handleImg($sku_item['sku_image'], 'MID')[0]; //图片处理 if (strpos($sku_image, 'http://') || strpos($sku_image, 'https://')) { $image = $this->image_service->open(img($this->handleImg($sku_item['sku_image'])[0]))->thumb(400, 400); $file = root_path() . 'runtime/temp/' . $sku_item['sku_id'] . $this->mime[$image->image->mime]; $image->save($file, 100); $sku_res = $miniapp->imageUpload($sku_item['sku_id'], $file, true)['alipay_offline_material_image_upload_response']; $sku_image = ""; if ($sku_res['code'] == 10000) { $sku_image = $sku_res['image_id']; } } $sku_data = [ 'out_sku_id' => $sku_item['sku_id'], 'thumb_img' => $sku_image, 'sale_status' => 'AVAILABLE', 'stock_num' => numberFormat($sku_item['stock']), 'sale_price' => $sku_item['discount_price'] * 100, 'original_price' => $sku_item['market_price'] * 100, 'item_details_page_model' => 0, 'sku_attrs' => [], ]; if (!empty($sku_item['sku_spec_format'])) { foreach (json_decode($sku_item['sku_spec_format'], true) as $spec_item) { array_push($sku_data['sku_attrs'], [ 'attr_key' => $spec_item['spec_name'], 'attr_value' => $spec_item['spec_value_name'] ]); } } array_push($spu_data['skus'], $sku_data); } } else { $sku_item = $sku_list[0]; $spu_data['sale_price'] = $sku_item['discount_price'] * 100; $spu_data['original_price'] = $sku_item['market_price'] * 100; $spu_data['stock_num'] = numberFormat($sku_item['stock']); $spu_data['image_list'] = $image_list; $spu_data['sale_status'] = 'AVAILABLE'; $spu_data['item_details_page_model'] = 0; if ($spu_data['original_price'] <= $spu_data['sale_price']) { $spu_data['original_price'] = $spu_data['sale_price'] + $spu_data['sale_price'] * 0.5; } unset($spu_data['skus']); } $add_res = $miniapp->requestApi($api, $spu_data)[$response]; if ($add_res['code'] != 10000) { $error = $add_res['sub_msg']; } else { $goods_data['item_id'] = $add_res['item_id']; $goods_data['skus'] = json_encode($add_res['skus']); $goods_data['spu_status'] = 'AUDITING'; } $goods_data['goods_image'] = implode(',', $image_urls); $goods_data['goods_content'] = $this->getGoodsContent($param['site_id'], $goods_info); $goods_data['update_time'] = time(); $goods_data['edit_status'] = 2; $goods_data['reject_reason'] = $error; $goods_data['cat_name'] = $category['cat_name']; $goods_data['spu_data'] = json_encode($spu_data); model('shopalilife_goods')->update($goods_data, [['out_item_id', '=', $param['goods_id']], ['site_id', '=', $param['site_id']]]); if ($error) return $this->error($error); return $this->success(); } else { model('shopalilife_goods')->delete([['out_item_id', '=', $param['goods_id']], ['site_id', '=', $param['site_id']]]); $param['alipay_cat_id'] = $category_id; return $this->newAddGoods($param); } } /*** * 同步商城图片 * @return void * @throws \think\db\exception\DbException */ public function SyncGoodsImage() { $field = 'sg.id,sg.site_id,sg.item_id,sg.api_type,sg.out_item_id,sg.goods_image as image,g.goods_id,g.goods_image,g.goods_content'; $alias = 'sg'; $join = [ ['goods g', 'g.goods_id = sg.out_item_id', 'inner'] ]; $condition = [ ['sg.reject_reason', '<>', 'imgerr'], ['sg.spu_status', '<>', 'NOTSYNC'], ['sg.goods_id', '>', 0], ['sg.goods_image', '=', ''] ]; $goods_info = model('shopalilife_goods')->getList($condition, $field, '', $alias, $join, '', 10); if ($goods_info) { foreach ($goods_info as $item) { $goodsInfo = $this->goodsQuery($item['site_id'], $item['item_id'], $item['api_type']); if ($goodsInfo['code'] == '10000' && isset($goodsInfo['image_list'])) { $data['goods_image'] = implode(',', $goodsInfo['image_list']); $data['goods_content'] = $this->getGoodsContent($item['site_id'], $item); model('shopalilife_goods')->update($data, ['goods_id' => $item['goods_id'], 'site_id' => $item['site_id']]); } else { $data['reject_reason'] = 'imgerr'; } model('shopalilife_goods')->update($data, ['goods_id' => $item['goods_id'], 'site_id' => $item['site_id']]); } return $this->success('成功'); } return $this->error('无数据'); } /*** * 上传产品图片到支付宝 * @param $id * @return array|void */ public function UploadImage($id = '') { try { $albumInfo = Db::name('album_pic')->where('pic_id', '=', $id)->field('pic_id,pic_path,site_id,thumb_value')->find(); if ($albumInfo && !$albumInfo['thumb_value']) { $config_model = new Config(); $config_info = $config_model->getAppConfig($albumInfo['site_id'])['data']['value']; if (isset($config_info['account'])) { $this->driver = config('upload')['driver'] ?? 'gd'; $this->image_service = new ImageService($this->driver); $miniapp = new MinCode($albumInfo['site_id']); $ali_pic_value = []; $ali_pic_path = ''; //图片处理 $image = $this->image_service->open(img($albumInfo['pic_path'])); $file1 = root_path() . 'runtime/temp/' . md5($albumInfo['pic_path']) . $this->mime[$image->image->mime]; if (strpos($albumInfo['pic_path'], 'alicdn.com') !== false && (strpos($albumInfo['pic_path'], 'http://') !== false || strpos($albumInfo['pic_path'], 'https://') !== false)) { $image->save($file1, 100); $pic_path = $miniapp->imageUpload(md5($albumInfo['pic_path']), $file1, true)['alipay_offline_material_image_upload_response']; if ($pic_path['code'] == 10000) { $ali_pic_path = $pic_path['image_url']; } } $image = $this->image_service->open($file1)->thumb(400, 400); $file = root_path() . 'runtime/temp/' . md5($albumInfo['pic_path']) . '_400' . $this->mime[$image->image->mime]; $image->save($file, 100); $img_400 = $miniapp->imageUpload(md5($albumInfo['pic_path']) . '_400', $file, true)['alipay_offline_material_image_upload_response']; if ($img_400['code'] == 10000) { $ali_pic_value['MID'] = [ 'size' => 'BIG', "width" => 400, "height" => 400, 'image_id' => $img_400['image_id'], 'thumb_name' => $img_400['image_url'], ]; } $image = $this->image_service->open($file1)->thumb(750, 750); $file = root_path() . 'runtime/temp/' . md5($albumInfo['pic_path']) . '_750' . $this->mime[$image->image->mime]; $image->save($file, 100); $img_750 = $miniapp->imageUpload(md5($albumInfo['pic_path']) . '_750', $file, true)['alipay_offline_material_image_upload_response']; if ($img_750['code'] == 10000) { $ali_pic_value['BIG'] = [ 'size' => 'BIG', "width" => 750, "height" => 750, 'image_id' => $img_750['image_id'], 'thumb_name' => $img_750['image_url'], ]; } $data = []; if ($ali_pic_path) { $data['ali_pic_path'] = $ali_pic_path; } if ($ali_pic_value) { $data['thumb_value'] = json_encode($ali_pic_value); } if ($data) { $res = Db::name('album_pic')->where('pic_id', '=', $id)->update($data); return $this->success($res); } } } return $this->success(); } catch (\Exception $e) { return $this->error($e->getMessage()); } } /*** * 添加团购商品 * @return void */ public function localitemAddGoods($param, $item_type = '', $merchant_name = '', $phone_number = '') { $goods_ids = $param['goods_ids']; $goods_list = model('goods')->getList([['goods_id', 'in', $goods_ids], ['site_id', '=', $param['site_id']]], 'goods_id,goods_name,goods_image,sku_id,goods_content,unit'); //保存本地团购信息获取商品信息 $groupbuy_data = [ 'site_id' => $param['site_id'], 'start_time' => strtotime($param['start_time']), 'end_time' => strtotime($param['end_time']), 'rule' => $param['notification'] ]; $groupbuy_model = new GroupbuyModel(); $groupbuy_model->addGroupbuy($groupbuy_data, $param['goods_list'], $goods_ids); if (!empty($goods_list)) { $category = (new Category())->getCategoryInfo($param['cat_id'], $param['site_id']); $miniapp = new MinCode($param['site_id']); $api = 'alipay.open.app.localitem.create'; $key = 'alipay_open_app_localitem_create_response'; $miniConfig = $miniapp->getConfig(); foreach ($goods_list as $goods_item) { // 需加到库中的商品数据 $goods_data = [ 'out_item_id' => $goods_item['goods_id'], 'category_id' => $param['cat_id'] ]; $groupboy = new \addon\groupbuy\model\Groupbuy(); $gwhere = [ ['g.goods_id', '=', $goods_item['goods_id']] ]; $groupboyInfo = $groupboy->getGroupbuyInfo($gwhere)['data']; if (empty($groupboyInfo)) return $this->error('', '团购活动不存在'); //处理图片 $goods_image_arr = $this->handleImg($goods_item['goods_image'], 'BIG'); $image_list = []; $this->driver = config('upload')['driver'] ?? 'gd'; $this->image_service = new ImageService($this->driver); $chat = urlencode('pages_tool/chat/room'); foreach ($goods_image_arr as $img_k => $img_y) { if (strpos($img_y, 'http://') !== false || strpos($img_y, 'https://') !== false) { $image = $this->image_service->open(img($img_y))->thumb(750, 750); $file = root_path() . 'runtime/temp/' . $goods_item['goods_id'] . $this->mime[$image->image->mime]; $image->save($file, 100); $image = $miniapp->imageUpload($img_k . $goods_item['goods_name'], $file, true); $res = $image['alipay_offline_material_image_upload_response']; if ($res['code'] == 10000) { if (count($image_list) < 3) { $image_list[] = $res['image_id']; } } else { return $this->error('', $res['sub_msg'] . '或图片格式不正确'); } } else { if (count($image_list) < 3) { $image_list[] = $img_y; } } } $groupboy_url = urlencode("pages_promotion/groupbuy/detail?groupbuy_id={$groupboyInfo[ 'groupbuy_id']}"); // 同步商品所需数据 $spu_data = [ 'title' => $goods_item['goods_name'], 'path' => "alipays://platformapi/startApp?appId={$miniConfig['appid']}&page={$groupboy_url}", 'merchant_name' => $merchant_name, 'head_img' => $image_list[0], 'image_list' => $image_list, 'item_type' => $item_type, 'item_details_page_model' => 0, 'sold_time' => [ 'start_time' => $param['start_time'], 'end_time' => $param['end_time'], ], 'customer_service_mobile' => [ 'phone_type' => isMobileNo($phone_number) ? 1 : 2, 'phone_number' => $phone_number, 'customer_link' => "alipays://platformapi/startApp?appId={$miniConfig['appid']}&page={$chat}", ], 'skus' => [] ]; $sku_list = model('goods_sku')->getList([['goods_id', '=', $goods_item['goods_id']]], 'sku_id,sku_name,discount_price,market_price,stock,sku_spec_format'); if (count($sku_list) > 50) { return $this->error('', '规格属性不得超过50个'); } $commodity = [ 'group_name' => '商品组', 'total_count' => '', 'option_count' => 1, 'item_list' => [] ]; $unit = $goods_item['unit'] ? $goods_item['unit'] : '人'; $stock = 0; if (count($sku_list) > 1) { foreach ($sku_list as $keys => $sku_item) { if ($keys >= 50) { continue; } if ($sku_item['market_price'] <= $sku_item['discount_price']) { return $this->error('', '销售价格必须小于划线价'); } $stock += numberFormat($sku_item['stock']); $sku_spec_format = json_decode($sku_item['sku_spec_format'], true)[0]; $item_list = [ 'name' => $sku_spec_format['spec_value_name'], 'count' => 1, 'count_unit' => $unit, 'price' => $sku_item['discount_price'] * 100, 'unit' => '元', ]; array_push($commodity['item_list'], $item_list); } } else { $sku_item = $sku_list[0]; $commodity['item_list'][] = [ 'name' => $goods_item['goods_name'], 'count' => 1, 'count_unit' => $unit, 'price' => $groupboyInfo['groupbuy_price'] * 100, 'unit' => '元', ]; $stock = numberFormat($sku_item['stock']); if ($sku_item['market_price'] <= $groupboyInfo['groupbuy_price']) { return $this->error('', '销售价格必须小于划线价'); } } $skus = [ 'sale_price' => $groupboyInfo['groupbuy_price'] * 100, 'original_price' => $groupboyInfo['price'] * 100, 'sale_status' => 'AVAILABLE', 'stock_num' => numberFormat($sku_item['stock']), ]; $spu_data['skus'][] = $skus; $commodity['total_count'] = count($commodity['item_list']); $commodity['option_count'] = 1; $commodityArr[] = $commodity; $use_url = urlencode("pages/member/index"); $use_path = "alipays://platformapi/startApp?appId={$miniConfig['appid']}&page={$use_url}"; $spu_data['attrs'] = [ [ //库存设置 'attr_key' => 'limit_stock_rule', 'attr_value' => [ 'limit' => 1, 'num' => $stock, 'unit' => $unit, ], ], [ 'attr_key' => 'original_price_source',//原价配置 'attr_value' => 1, ], [ 'attr_key' => 'commodity',//套餐 'attr_value' => $commodityArr, ], [ 'attr_key' => 'notification',//套餐 'attr_value' => $param['notification'], ], [ 'attr_key' => 'select_all_shop',//蚂蚁门店 'attr_value' => 1, ], [ 'attr_key' => 'use_date',//可使用日期 'attr_value' => [ 'use_date_type' => 1, 'use_start_date' => date('Y-m-d', strtotime($param['start_time'])), 'use_end_date' => date('Y-m-d', strtotime($param['end_time'])), ], ], [ 'attr_key' => 'use_limit',//使用时间限制 'attr_value' => [ 'use_time_type' => 1 ], ], [ 'attr_key' => 'limit_buy_rule',//限购 'attr_value' => [ 'limit' => 0, 'num' => '' ] ], [ 'attr_key' => 'refund_rule',//退费规则 'attr_value' => [ 'refund_policy' => [1, 2] ],//使用时间限制 ], [ 'attr_key' => 'merchant_refund_confirm',//由商家确认退款 'attr_value' => 2,//使用时间限制1 商家确认,2无需商家确认 ], [ 'attr_key' => 'usage_type',//使用方式 'attr_value' => [ 'usage_list' => [ ['use_type' => 1], [ 'use_type' => 2, 'use_path' => $use_path, ], ] ], ], [ 'attr_key' => 'verify_type',//核销方式 'attr_value' => ['1', '2'], ], [ 'attr_key' => 'verify_type',//核销方式 'attr_value' => ['1', '2'], ], [ 'attr_key' => 'code_source_type',//核销码 1三方核销码 2支付宝核销码 'attr_value' => '2', ], [ 'attr_key' => 'settle_type',//结算方式 'attr_value' => [ 'settle_type' => '1' ] ], [ 'attr_key' => 'preorder_rule',//是否预约 'attr_value' => [ 'need_appointment' => false, 'appointment_instruction' => '',//预约说明 ], ], [ 'attr_key' => 'contact_info_type',//顾客预留手机信息 'attr_value' => '1', ], [ 'attr_key' => 'order_settle_rule',//结算规则 1默认核销结算 'attr_value' => ['order_settle_rule' => 1], ], // [ // 'attr_key' => 'detail_rich_desc',//富文本消息 // 'attr_value' =>[ // 'type'=>'1', // 'content'=>$goods_item['goods_content'], // ] // ], [ 'attr_key' => 'coupon_usage_rule',//是否可以使用优惠券 1不可使用 2可使用 'attr_value' => '1', ], [ 'attr_key' => 'verify_tool',//核销工具 'attr_value' => '接口API', ] ]; $add_res = $miniapp->requestApi($api, array_merge($goods_data, $spu_data))[$key]; // 添加商品到小程序 if ($add_res['code'] != 10000) return $this->error('', $add_res['sub_msg']); $goods_data['is_sync'] = 1; $goods_data['api_type'] = 'localitem'; $goods_data['spu_status'] = 'AUDITING'; $goods_data['goods_id'] = $goods_item['goods_id']; $goods_data['item_id'] = $add_res['item_id']; $goods_data['skus'] = json_encode($add_res['skus']); $goods_data['create_time'] = time(); $goods_data['site_id'] = $param['site_id']; $goods_data['cat_name'] = $category['cat_name']; $goods_data['site_name'] = $param['merchant_name']; $goods_data['phone_number'] = $param['phone_number']; $goods_data['spu_data'] = json_encode($spu_data); model('shopalilife_goods')->add($goods_data); } return $this->success(); } else { return $this->error('', '未获取到要添加的商品'); } } /*** * 更新本地生活 * @param $param * @return void */ public function updateGoodslocalitemGoods($goods_id, $param, $item_type) { //保存本地团购信息获取商品信息 $goods_model = new GoodsModel(); $goods_info = $goods_model->getGoodsInfo([['goods_id', '=', $goods_id]], 'site_id,goods_name,goods_image,price,unit')['data']; if (!empty($goods_info)) { $groupbuy_data = [ 'goods_id' => $goods_id, 'goods_name' => $goods_info['goods_name'], 'goods_image' => $goods_info['goods_image'], 'goods_price' => $goods_info['price'], 'groupbuy_price' => $param['groupbuy_price'], 'buy_num' => $param['buy_num'], 'start_time' => strtotime($param['start_time']), 'end_time' => strtotime($param['end_time']), 'rule' => $param['notification'], ]; $groupbuy_id = $param['groupbuy_id']; $groupbuy_model = new GroupbuyModel(); $groupbuy_model->editGroupbuy($groupbuy_id, $param['site_id'], $groupbuy_data);//编辑团购 $category = (new Category())->getCategoryInfo($param['cat_id'], $goods_info['site_id']); $miniapp = new MinCode($goods_info['site_id']); $api = 'alipay.open.app.localitem.modify'; $key = 'alipay_open_app_localitem_modify_response'; $miniConfig = $miniapp->getConfig(); // 需加到库中的商品数据 $goods_data = [ 'out_item_id' => $goods_id, 'category_id' => $param['cat_id'] ]; //处理图片 $goods_image_arr = $this->handleImg($goods_info['goods_image'], 'BIG'); $image_list = []; $this->driver = config('upload')['driver'] ?? 'gd'; $this->image_service = new ImageService($this->driver); $chat = urlencode('pages_tool/chat/room'); foreach ($goods_image_arr as $img_k => $img_y) { if (strpos($img_y, 'http://') !== false || strpos($img_y, 'https://') !== false) { $image = $this->image_service->open(img($img_y))->thumb(800, 800); $file = root_path() . 'runtime/temp/' . $goods_id . $this->mime[$image->image->mime]; $image->save($file, 100); $image = $miniapp->imageUpload($img_k . $goods_info['goods_name'], $file, true); $res = $image['alipay_offline_material_image_upload_response']; if ($res['code'] == 10000) { if (count($image_list) < 3) { $image_list[] = $res['image_id']; } } } else { if (count($image_list) < 3) { $image_list[] = $img_y; } } } $groupboy_url = urlencode("pages_promotion/groupbuy/detail?groupbuy_id={$groupbuy_id}"); // 同步商品所需数据 $spu_data = [ 'title' => $goods_info['goods_name'], 'path' => "alipays://platformapi/startApp?appId={$miniConfig['appid']}&page={$groupboy_url}", 'merchant_name' => $param['merchant_name'], 'head_img' => $image_list[0], 'image_list' => $image_list, 'item_type' => $item_type, 'item_details_page_model' => 0, 'sold_time' => [ 'start_time' => $param['start_time'], 'end_time' => $param['end_time'], ], 'customer_service_mobile' => [ 'phone_type' => isMobileNo($param['phone_number']) ? 1 : 2, 'phone_number' => $param['phone_number'], 'customer_link' => "alipays://platformapi/startApp?appId={$miniConfig['appid']}&page={$chat}", ], 'skus' => [] ]; $sku_list = model('goods_sku')->getList([['goods_id', '=', $goods_id]], 'sku_id,sku_name,discount_price,market_price,stock,sku_spec_format'); if (count($sku_list) > 25) { return $this->error('', '规格属性不得超过25个'); } $commodity = [ 'group_name' => '商品组', 'total_count' => '', 'option_count' => 1, 'item_list' => [] ]; $unit = $goods_info['unit'] ? $goods_info['unit'] : '人'; $stock = 0; if (count($sku_list) > 1) { foreach ($sku_list as $keys => $sku_item) { if ($keys >= 25) { continue; } if ($sku_item['market_price'] <= $sku_item['discount_price']) { return $this->error('', '销售价格必须小于划线价'); } $stock += numberFormat($sku_item['stock']); $sku_spec_format = json_decode($sku_item['sku_spec_format'], true)[0]; $item_list = [ 'name' => $sku_spec_format['spec_value_name'], 'count' => 1, 'count_unit' => $unit, 'price' => $sku_item['discount_price'] * 100, 'unit' => '元', ]; array_push($commodity['item_list'], $item_list); } } else { $sku_item = $sku_list[0]; $commodity['item_list'][] = [ 'name' => $goods_info['goods_name'], 'count' => 1, 'count_unit' => $unit, 'price' => $param['groupbuy_price'] * 100, 'unit' => '元', ]; $stock = numberFormat($sku_item['stock']); if ($sku_item['market_price'] <= $param['groupbuy_price']) { return $this->error('', '销售价格必须小于划线价'); } } $skus = [ 'sale_price' => $param['groupbuy_price'] * 100, 'original_price' => $goods_info['price'] * 100, 'sale_status' => 'AVAILABLE', 'stock_num' => numberFormat($sku_item['stock']), ]; $spu_data['skus'][] = $skus; $commodity['total_count'] = count($commodity['item_list']); $commodity['option_count'] = 1; $commodityArr[] = $commodity; $use_url = urlencode("pages/member/index"); $use_path = "alipays://platformapi/startApp?appId={$miniConfig['appid']}&page={$use_url}"; $spu_data['attrs'] = [ [ //库存设置 'attr_key' => 'limit_stock_rule', 'attr_value' => [ 'limit' => 1, 'num' => $stock, 'unit' => $unit, ], ], [ 'attr_key' => 'original_price_source',//原价配置 'attr_value' => 1, ], [ 'attr_key' => 'commodity',//套餐 'attr_value' => $commodityArr, ], [ 'attr_key' => 'notification',//套餐 'attr_value' => $param['notification'], ], [ 'attr_key' => 'select_all_shop',//蚂蚁门店 'attr_value' => 1, ], [ 'attr_key' => 'use_date',//可使用日期 'attr_value' => [ 'use_date_type' => 1, 'use_start_date' => date('Y-m-d', strtotime($param['start_time'])), 'use_end_date' => date('Y-m-d', strtotime($param['end_time'])), ], ], [ 'attr_key' => 'use_limit',//使用时间限制 'attr_value' => [ 'use_time_type' => 1 ], ], [ 'attr_key' => 'limit_buy_rule',//限购 'attr_value' => [ 'limit' => 0, 'num' => '' ] ], [ 'attr_key' => 'refund_rule',//退费规则 'attr_value' => [ 'refund_policy' => [1, 2] ],//使用时间限制 ], [ 'attr_key' => 'merchant_refund_confirm',//由商家确认退款 'attr_value' => 2,//使用时间限制1 商家确认,2无需商家确认 ], [ 'attr_key' => 'usage_type',//使用方式 'attr_value' => [ 'usage_list' => [ ['use_type' => 1], [ 'use_type' => 2, 'use_path' => $use_path, ], ] ], ], [ 'attr_key' => 'verify_type',//核销方式 'attr_value' => ['1', '2'], ], [ 'attr_key' => 'verify_type',//核销方式 'attr_value' => ['1', '2'], ], [ 'attr_key' => 'code_source_type',//核销码 1三方核销码 2支付宝核销码 'attr_value' => '2', ], [ 'attr_key' => 'settle_type',//结算方式 'attr_value' => [ 'settle_type' => '1' ] ], [ 'attr_key' => 'preorder_rule',//是否预约 'attr_value' => [ 'need_appointment' => false, 'appointment_instruction' => '',//预约说明 ], ], [ 'attr_key' => 'contact_info_type',//顾客预留手机信息 'attr_value' => '1', ], [ 'attr_key' => 'order_settle_rule',//结算规则 1默认核销结算 'attr_value' => ['order_settle_rule' => 1], ], [ 'attr_key' => 'coupon_usage_rule',//是否可以使用优惠券 1不可使用 2可使用 'attr_value' => '1', ], [ 'attr_key' => 'verify_tool',//核销工具 'attr_value' => '接口API', ] ]; $add_res = $miniapp->requestApi($api, array_merge($goods_data, $spu_data))[$key]; // 添加商品到小程序 if ($add_res['code'] != 10000) return $this->error('', $add_res['sub_msg']); $goods_data['is_sync'] = 1; $goods_data['api_type'] = 'localitem'; $goods_data['spu_status'] = 'AUDITING'; $goods_data['goods_id'] = $goods_id; $goods_data['item_id'] = $add_res['item_id']; $goods_data['skus'] = json_encode($add_res['skus']); $goods_data['create_time'] = time(); $goods_data['site_id'] = $goods_info['site_id']; $goods_data['cat_name'] = $category['cat_name']; $goods_data['site_name'] = $param['merchant_name']; $goods_data['phone_number'] = $param['phone_number']; $goods_data['spu_data'] = json_encode($spu_data); model('shopalilife_goods')->update($goods_data, ['goods_id' => $goods_id]); return $this->success(); } else { return $this->error('', '未获取到要添加的商品'); } } /** * 更新商品 * @param $param * @return array */ public function updateGoods($param, $type = 'allcategory') { $shopcompoent_goods_info = Db::name('shopalilife_goods')->where([['out_item_id', '=', $param['goods_id']], ['site_id', '=', $param['site_id']]])->find(); $goods_info = Db::name('goods')->where([['goods_id', '=', $param['goods_id']], ['site_id', '=', $param['site_id']]])->field('goods_id,goods_name,goods_image,sku_id,goods_content')->find(); $error = ''; if (!empty($shopcompoent_goods_info) && !empty($goods_info)) { $cat_id = $param['cat_id'] ?? $shopcompoent_goods_info['category_id']; $category = (new Category())->getCategoryInfo($cat_id, $param['site_id']); $miniapp = new MinCode($param['site_id']); //处理图片 $goods_image_arr = $this->handleImg($goods_info['goods_image'], 'BIG'); $image_list = []; $image_urls = []; $this->driver = config('upload')['driver'] ?? 'gd'; $this->image_service = new ImageService($this->driver); foreach ($goods_image_arr as $img_k => $img_y) { if (strpos($img_y, 'http://') !== false || strpos($img_y, 'https://') !== false) { $image = $this->image_service->open(img($img_y))->thumb(750, 750); $file = root_path() . 'runtime/temp/' . $goods_info['goods_id'] . '_750' . $this->mime[$image->image->mime]; $image->save($file, 100); $image = $miniapp->imageUpload($goods_info['goods_id'] . '_750', $file, true); $res = $image['alipay_offline_material_image_upload_response']; if ($res['code'] == 10000) { $image_urls[] = $res['image_url']; if (count($image_list) < 3) { $image_list[] = $res['image_id']; } } } else { $image_list[] = $img_y; $image_urls[] = 'https://oalipay-dl-django.alicdn.com/rest/1.0/image?fileIds=' . $img_y; } } $url = urlencode("pages_market/goods/detail?goods_id={$goods_info[ 'goods_id' ]}"); $miniConfig = $miniapp->getConfig(); // 同步商品所需数据 $spu_data = [ 'out_item_id' => $goods_info['goods_id'], 'category_id' => $cat_id, 'title' => $goods_info['goods_name'], 'path' => "alipays://platformapi/startApp?appId={$miniConfig['appid']}&page={$url}", 'head_img' => $image_list[0], 'desc_info' => [ 'desc' => $goods_info['goods_name'] ], 'skus' => [] ]; $sku_list = Db::name('goods_sku')->where([['goods_id', '=', $goods_info['goods_id']]])->field('sku_id,sku_no,sku_image,discount_price,market_price,stock,sku_spec_format')->select(); if (count($sku_list) > 1) { foreach ($sku_list as $keys => $sku_item) { if ($keys > 25) { $error = 'SKU大于25个部分属性丢失'; continue; } if ($sku_item['market_price'] <= $sku_item['discount_price']) { $sku_item['market_price'] = $sku_item['discount_price'] + $sku_item['discount_price'] * 0.5; } $sku_image = $this->handleImg($sku_item['sku_image'], 'MID')[0]; if (strpos($sku_image, 'http://') || strpos($sku_image, 'https://')) { //图片处理 $image = $this->image_service->open(img($this->handleImg($sku_item['sku_image'])[0]))->thumb(400, 400); $file = root_path() . 'runtime/temp/' . $sku_item['sku_id'] . $this->mime[$image->image->mime]; $image->save($file, 100); $sku_res = $miniapp->imageUpload($sku_item['sku_id'], $file, true)['alipay_offline_material_image_upload_response']; $sku_image = ""; if ($sku_res['code'] == 10000) { $sku_image = $sku_res['image_id']; } } $sku_data = [ 'out_sku_id' => $sku_item['sku_id'], 'thumb_img' => $sku_image, 'sale_status' => 'AVAILABLE', 'stock_num' => numberFormat($sku_item['stock']), 'sale_price' => $sku_item['discount_price'] * 100, 'original_price' => $sku_item['market_price'] * 100, 'item_details_page_model' => 0, 'sku_attrs' => [], ]; if (!empty($sku_item['sku_spec_format'])) { foreach (json_decode($sku_item['sku_spec_format'], true) as $spec_item) { array_push($sku_data['sku_attrs'], [ 'attr_key' => $spec_item['spec_name'], 'attr_value' => $spec_item['spec_value_name'] ]); } } array_push($spu_data['skus'], $sku_data); } } else { $sku_item = $sku_list[0]; $spu_data['sale_price'] = $sku_item['discount_price'] * 100; $spu_data['original_price'] = $sku_item['market_price'] * 100; $spu_data['stock_num'] = numberFormat($sku_item['stock']); $spu_data['image_list'] = $image_list; $spu_data['sale_status'] = 'AVAILABLE'; $spu_data['item_details_page_model'] = 0; if ($spu_data['original_price'] <= $spu_data['sale_price']) { $spu_data['original_price'] = $spu_data['sale_price'] + $spu_data['sale_price'] * 0.5; } unset($spu_data['skus']); } $add_res = $miniapp->requestApi('alipay.open.app.item.modify', $spu_data)['alipay_open_app_item_modify_response']; if ($add_res['code'] != 10000) { $error = $add_res['sub_msg']; } else { $goods_data['item_id'] = $add_res['item_id']; $goods_data['skus'] = json_encode($add_res['skus']); $goods_data['spu_status'] = 'AUDITING'; } $goods_data['goods_image'] = implode(',', $image_urls); $goods_data['goods_content'] = $this->getGoodsContent($param['site_id'], $goods_info); $goods_data['update_time'] = time(); $goods_data['edit_status'] = 2; $goods_data['reject_reason'] = $error; $goods_data['cat_name'] = $category['cat_name']; $goods_data['spu_data'] = json_encode($spu_data); model('shopalilife_goods')->update($goods_data, [['out_item_id', '=', $param['goods_id']], ['site_id', '=', $param['site_id']]]); if ($error) return $this->error($error); return $this->success(); } else { return $this->error('', '未获取到要更新的商品'); } } /*** * 替换支付宝图片富文本 * @param $site_id * @param $goods_info * @return string|string[] * @throws \think\db\exception\DbException */ public function getGoodsContent($site_id, $goods_info) { $pattern = '/src=\"(.+?)\".*?/'; $content = $goods_info['goods_content']; preg_match_all($pattern, $content, $match); if ($match) { //处理图片 $newArr = []; $images = $match[1]; $album_pic = Db::name('album_pic')->where('pic_path', 'in', $images)->column('pic_id,pic_path,ali_pic_path', 'pic_path'); $this->driver = config('upload')['driver'] ?? 'gd'; $this->image_service = new ImageService($this->driver); $miniapp = new MinCode($site_id); foreach ($images as $img_path) { if (strpos($img_path, 'alicdn.com') || (!isset($album_pic[$img_path]) && strpos($img_path, 'https://') || strpos($img_path, 'http://'))) { $newArr[] = $img_path; continue; } if (isset($album_pic[$img_path]) && $album_pic[$img_path]['ali_pic_path']) { $newArr[] = $album_pic[$img_path]['ali_pic_path']; } else { $image = $this->image_service->open(img($img_path)); $file = root_path() . 'runtime/temp/' . $goods_info['goods_id'] . $this->mime[$image->image->mime]; $image->save($file, 100); $image = $miniapp->imageUpload($img_path, $file, true); $res = $image['alipay_offline_material_image_upload_response']; if ($res['code'] == 10000) { $newArr[] = $res['image_url']; Db::name('album_pic')->where('pic_path', '=', $img_path)->update(['ali_pic_path' => $res['image_url']]); } else { $newArr[] = $img_path; } } } return str_replace($images, $newArr, $content); } return ''; } /*** * 编辑商品信息 * @param $param * @return void */ public function GoodsEditupdate($param) { $where = [ 'site_id' => $param['site_id'], 'goods_id' => $param['goods_id'], ]; $param['cat_id'] = $param['alipay_cat_id']; $goodsInfo = model('shopalilife_goods')->getInfo($where); if ($goodsInfo) { $sku_info = json_decode($goodsInfo['skus'], true); $out_sku_ids = array_column($sku_info, 'out_sku_id'); $sku_list = model('goods_sku')->getColumn([['goods_id', '=', $param['goods_id']]], 'sku_id'); $result = array_udiff($sku_list, $out_sku_ids, function ($a, $b) { return $a == $b ? 0 : 1; //查询是否有更新SKU }); if (empty($result) && $goodsInfo['category_id'] == $param['alipay_cat_id']) { $this->syncDtockPrice($param['goods_id'], $param['site_id']); } else { model('shopalilife_goods')->update(['edit_status' => 1, 'is_sync' => 0, 'reject_reason' => ''], [['out_item_id', '=', $param['goods_id']], ['site_id', '=', $param['site_id']]]); } } else { $this->newAddGoods($param); } return $this->success(); } /** * 处理分割图片 * @param $images * @return false|string[] */ private function handleImg($images, $size = '') { $img_arr = explode(',', $images); if ($size) { $thumb_value = model('album_pic')->getColumn(['pic_path' => $img_arr], 'thumb_value', 'pic_path'); $img_arr = array_map(function ($v) use ($thumb_value, $size) { if (array_key_exists($v, $thumb_value)) { $value = json_decode($thumb_value[$v], true); if (array_key_exists($size, $value)) { $img_path = $value[$size]; $thumb_name = $img_path['thumb_name']; parse_str($thumb_name, $srt); $v = array_values($srt); if ($v) { return $v[0]; } else { return img($v); } } } return img($v); }, $img_arr); } else { $img_arr = array_map('img', $img_arr); } return $img_arr; } /** * 删除商品 * @param $id * @param $site_id */ public function deleteGoods($goods_ids, $site_id) { if (!empty($goods_ids)) { $array_goodsIds = explode(',', $goods_ids); } $res = (new MinCode($site_id))->requestApi('alipay.open.app.item.delete', ['out_item_id_list' => $array_goodsIds]); model('shopalilife_goods')->delete([['site_id', '=', $site_id], ['out_item_id', 'in', $goods_ids]]); return $this->success($res); } /** * 商品上架 * @param $goods_ids * @param $site_id * @return array|mixed * @throws \GuzzleHttp\Exception\GuzzleException */ public function goodsListing($goods_ids, $site_id) { if (!empty($goods_ids)) { $array_goodsIds = explode(',', $goods_ids); } foreach ($array_goodsIds as $k => $goods_id) { $api_type = model('shopalilife_goods')->getValue([['out_item_id', '=', $goods_id], ['site_id', '=', $site_id]], 'api_type'); if ($api_type == 'allcategory') { $api = 'alipay.open.app.item.direct.modify'; } else { $api = 'alipay.open.app.localitem.direct.modify'; } $sku_list = model('goods_sku')->getList([['goods_id', '=', $goods_id]], 'sku_id'); $spu_data = [ 'out_item_id' => $goods_id, 'skus' => [] ]; if (count($sku_list) > 1) { foreach ($sku_list as $sku_item) { $sku_data = [ 'out_sku_id' => $sku_item['sku_id'], 'sale_status' => 'AVAILABLE', ]; array_push($spu_data['skus'], $sku_data); } } else { $spu_data['sale_status'] = 'AVAILABLE'; } $res[$k] = (new MinCode($site_id))->requestApi($api, $spu_data); if ($res[$k]['code'] != 0) return $res[$k]; } model('shopcompoent_goods')->update(['spu_status' => 'DELISTING'], [['site_id', '=', $site_id], ['out_item_id', 'in', $goods_ids]]); return $this->success(); } /** * 商品下架 * @param $goods_ids * @param $site_id * @return array|mixed * @throws \GuzzleHttp\Exception\GuzzleException */ public function goodsDelisting($goods_ids, $site_id) { if (!empty($goods_ids)) { $array_goodsIds = explode(',', $goods_ids); } foreach ($array_goodsIds as $k => $goods_id) { $api_type = model('shopalilife_goods')->getValue([['out_item_id', '=', $goods_id], ['site_id', '=', $site_id]], 'api_type'); if ($api_type == 'allcategory') { $api = 'alipay.open.app.item.direct.modify'; } else { $api = 'alipay.open.app.localitem.direct.modify'; } $sku_list = model('goods_sku')->getList([['goods_id', '=', $goods_id]], 'sku_id'); $spu_data = [ 'out_item_id' => $goods_id, 'skus' => [] ]; if (count($sku_list) > 1) { foreach ($sku_list as $sku_item) { $sku_data = [ 'out_sku_id' => $sku_item['sku_id'], 'sale_status' => 'DELISTING', ]; array_push($spu_data['skus'], $sku_data); } } else { $spu_data['sale_status'] = 'DELISTING'; } $res[$k] = (new MinCode($site_id))->requestApi($api, $spu_data); if ($res[$k]['code'] != 0) return $res[$k]; } model('shopcompoent_goods')->update(['spu_status' => 'DELISTING'], [['site_id', '=', $site_id], ['out_item_id', 'in', $goods_ids]]); return $this->success(); } }