之前给客户用了一套独立的单商户微商城系统CRMEB,功能比较薄弱,不能满足客户的一些分销、积分兑换的需求,二次开发成本太高,所以决定还是换到人人商城中,做为程序员懒是一种美德,写代码导入商品信息,又有趣又能拜托人工录入商品的痛苦,之前也对微擎有过点研究做过一次修改扩展,不过好久没再弄也忘记差不多了,再次快速查看微擎的开发文档,主要是如何连接外部数据库,下载远程文件,再就是根据微擎的路由规则找到人人商城快速添加商品goods.create的程序位置,添加断点输出json,看人人商城添加商品添加的参数,只需要对某些字段进行修改处理 再入库就可以了。

还有就是要从一个合作商那的APP接口中直接采集最新的产品,合作商的APP接口我们用FIDDLER抓https包的方式获得,需要设置手机代理到开发机器中,并在手机安装相关证书文件,合作商的APP其实是用APICLOUD制作的可以很容易看到源码。有接口就用微擎框架自带的http相关方法获得json数据并解析,处理入库。

<?php
public function importold()
    {
        exit("已经导入");
        error_reporting(E_ALL);
        set_time_limit(0);
        ignore_user_abort();
        load()->func('file');


        //导入商品 为编程的方式解决问题
        $data = json_decode('{"uniacid":3,"title":"\u6563\u517b\u9ed1\u732a\u4e94\u82b1\u8089","unit":"\u4ef6","keywords":"","type":1,"thumb_first":0,"isrecommand":0,"isnew":0,"ishot":0,"issendfree":0,"isnodiscount":0,"marketprice":100,"minprice":100,"maxprice":100,"productprice":"100","costprice":"100","virtualsend":0,"virtualsendcontent":"","virtual":0,"cash":0,"cashier":0,"invoice":0,"dispatchtype":0,"dispatchprice":"","dispatchid":0,"status":0,"goodssn":"","productsn":"","weight":"","total":0,"showtotal":0,"totalcnf":0,"hasoption":0,"subtitle":"","shorttitle":"","content":"<p><img src=\"http:\/\/wx.xunza.com\/web\/index.php?c=utility&a=wxcode&do=image&attach=http%3A%2F%2Fmmbiz.qpic.cn%2Fmmbiz_jpg%2FMoL5lIhnW9tpHAq13hGLbEVhxZxuK4zM9amH5atS9YyWSChOvOcgl5kGSHXtN1uUc1HnKKtqDcLwGVyRyGOVyw%2F0%3Fwx_fmt%3Djpeg\" width=\"100%\"\/><\/p>","createtime":1545794595,"video":"","discounts":"{\"type\":0,\"default\":\"\",\"default_pay\":\"\"}","pcate":"3","ccate":"9","tcate":0,"cates":"9","pcates":"","ccates":"9","tcates":"","thumb":"images\/3\/2018\/12\/kCbCcZiEccDBEy8Wz4WWlqwD9Ifmjx.jpg","thumb_url":"a:0:{}","isstatustime":0,"statustimestart":1545794220,"statustimeend":1548472620}',true);
        //var_dump($data);die();

        //遍历以前数据库 重置数组相关参数 下载远程文件 保存商品
        $other_database = array(
            'host' => 'www.linsen18.com', //数据库IP或是域名
            'username' => '', // 数据库连接用户名
            'password' => '', // 数据库连接密码
            'database' => 'wx_zchszxx_com', // 数据库名
            'port' => 3306, // 数据库连接端口
            'tablepre' => 'eb_', // 表前缀,如果没有前缀留空即可
            'charset' => 'utf8', // 数据库默认编码
            'pconnect' => 0, // 是否使用长连接
        );
        $other_db = new DB($other_database);
//查询uid为1的会员信息
        $products = $other_db->getall('store_product');

        foreach ($products as $product)
        {
            if($product["id"] > 43 && $product["id"] < 46 ) {
                $data["title"] = $product['store_name'];
                $data["unit"] = $product['unit_name'];
                $data["marketprice"] = $product['price'];
                $data["minprice"] = $product['price'];
                $data["maxprice"] = $product['price'];
                $data["productprice"] = $product['price'];
                $data["costprice"] = $product['price'];
                $data["content"] = $this->replaceimg($product['description']);
                $data["createtime"] = $product['add_time'];
                $data["thumb"] = $product['image'];

                $file = file_remote_attach_fetch($product['image']);
                if (!is_error($file)) {
                    $data->thumb = $file;
                }
                //var_dump($data);
                //远程文件保存
                pdo_insert("ewei_shop_goods", $data);
                echo $data["title"] . "导入成功<br />";

            }
        }
        
    }


    public function importkl()
    {
        exit();
        error_reporting(E_ALL);
        set_time_limit(0);
        ignore_user_abort();
        load()->func('file');
        load()->func("communication");
        $data = json_decode('{"uniacid":3,"title":"\u6563\u517b\u9ed1\u732a\u4e94\u82b1\u8089","unit":"\u4ef6","keywords":"","type":1,"thumb_first":0,"isrecommand":0,"isnew":0,"ishot":0,"issendfree":0,"isnodiscount":0,"marketprice":100,"minprice":100,"maxprice":100,"productprice":"100","costprice":"100","virtualsend":0,"virtualsendcontent":"","virtual":0,"cash":0,"cashier":0,"invoice":0,"dispatchtype":0,"dispatchprice":"","dispatchid":0,"status":0,"goodssn":"","productsn":"","weight":"","total":0,"showtotal":0,"totalcnf":0,"hasoption":0,"subtitle":"","shorttitle":"","content":"<p><img src=\"http:\/\/wx.xunza.com\/web\/index.php?c=utility&a=wxcode&do=image&attach=http%3A%2F%2Fmmbiz.qpic.cn%2Fmmbiz_jpg%2FMoL5lIhnW9tpHAq13hGLbEVhxZxuK4zM9amH5atS9YyWSChOvOcgl5kGSHXtN1uUc1HnKKtqDcLwGVyRyGOVyw%2F0%3Fwx_fmt%3Djpeg\" width=\"100%\"\/><\/p>","createtime":1545794595,"video":"","discounts":"{\"type\":0,\"default\":\"\",\"default_pay\":\"\"}","pcate":"3","ccate":"9","tcate":0,"cates":"9","pcates":"","ccates":"9","tcates":"","thumb":"images\/3\/2018\/12\/kCbCcZiEccDBEy8Wz4WWlqwD9Ifmjx.jpg","thumb_url":"a:0:{}","isstatustime":0,"statustimestart":1545794220,"statustimeend":1548472620}',true);
        //var_dump($data);die();

        $resp = ihttp_get("https://odoapi.kangerys.com:8443/odoapi/product/list/c270a2babe5946c8b82a09a8eb25ad00?pageSize=100&pageOffset=1&phone=undefined&version=1.0");
        $json = $resp['content'];

        foreach (json_decode($json)->data->datas as $key => $pro) {
            //存入实体
            //var_dump($pro);

            if($key > 20) {
                //打开详情页
                $info = ihttp_get("https://odoapi.kangerys.com:8443/odoapi/product/get/" . $pro->productId . "?version=1.0");

                //下载缩略图
                //正则匹配内容中的图片 下载到本地并替换成新地址  下载不能同时下载多个?文章重名
                $proinfo = json_decode($info['content'])->data;
                $data["title"] = $proinfo->name;
                $file = file_remote_attach_fetch($proinfo->thumbUrl);
                if (!is_error($file)) {
                    $data["thumb"] = $file;
                }

                $data["marketprice"] = $proinfo->retailPrice / 100;
                $data["minprice"] = $proinfo->retailPrice / 100;
                $data["maxprice"] = $proinfo->retailPrice / 100;
                $data["productprice"] = $proinfo->retailPrice / 100;
                $data["costprice"] = $proinfo->retailPrice / 100;
                $data["content"] = $this->replaceimg($proinfo->details);
                $data["createtime"] = time();
                $data["total"] = $proinfo->stock;
                $data["sales"] = $proinfo->sales;

                //var_dump($data);
                //远程文件保存
                pdo_insert("ewei_shop_goods", $data);
                echo $data["title"] . "导入成功<br />";

            }


        }



    }

有几点注意就是json解码后生成对象还是数组的问题,微擎的数据库入库要求是数组,所以在json_decode($data,true)要加true

标签: none

添加新评论