微信第三方平台(服务商)

对接微信第三方平台服务商,成为服务商后,可以让公众号和小程序授权管理,代替小程序公众号实现业务

1.发起授权,使用微信第三方平台账号信息对接

//发起授权,返回url地址,用户点击url跳转授权页面,用户扫描授权
public function Authorization()
{
    $config = [
        'app_id' => config('config.app_id'),
        'secret' => config('config.secret'),
        'token' => config('config.token'),
        'aes_key' => config('config.aes_key')
    ];
    $openPlatform = Factory::openPlatform($config);
    //查询是否授权
    $re = $openPlatform->getPreAuthorizationUrl('https://api.jcgxkj.cn/api/v2/setting/callback?type=3&from_uu_id=112'); // 传入回调URI即可
    #直接显示一个链接
    return view('authorization', ['src' => $re]);
}

//用户授权成功回调,将授权的程序记录到数据库中, appid,authorizer_access_token
public function callback(Request $request)
{
    $openPlatform = OpenPlat::init();
    $info = $openPlatform->handleAuthorize($request->get('auth_code'));
    //添加更新账号
    (new Account())->add($info['authorization_info']);
    return view('callback', ['appid' => $info['authorization_info']['authorizer_appid'],'type'=>$request->type]);
}
//添加更新账号
public function add($data){
    $account = Account::query()->where(['appid'=>$data['authorizer_appid']])->first();
    if ($account){
        $account->authorizer_access_token = $data['authorizer_access_token'];
        $account->authorizer_refresh_token =$data['authorizer_refresh_token'];
        $account->expire = $data['expires_in']+time();
        $account->save();
    }else{
        Account::create([
            'appid'=>$data['authorizer_appid'], //授权程序的appid
            'authorizer_access_token'=>$data['authorizer_access_token'], //代实现业务需要
            'authorizer_refresh_token'=>$data['authorizer_refresh_token'], //刷新token
            'expire'=>$data['expires_in']+time(), //access_token过期时间
        ]);
    }
}

//实现业务| 具体操作业务
public function doAction(Request $request)
{
    $action = $request->action;
    $appid = $request->appid;
    $form_uu_id = $request->form_uu_id;
    if (!$action || !$appid) {
        echo '<script>alert("参数错误!");history.back();</script>';
    }
    $mini = new Miniprogram($appid);
    if ($action == 'auth_bind') {
        //设置小程序绑定平台
        if ($mini->bind($form_uu_id)) {
            echo '<script>alert("绑定平台操作成功");history.back();</script>';
        } else {
            $data=[
                -1=>'系统错误',
                40013=>'appid 或 open_appid 无效',
                89000=>'该公众号/小程序已经绑定了开放平台帐号',
                89001=>'Authorizer 与开放平台帐号主体不相同',
                89004=>'该开放平台帐号所绑定的公众号/小程序已达上限(100 个)',
            ];
            $message = '绑定失败';
            if (isset($data[$mini->code])){
                $message = $data[$mini->code];
            }
            echo '<script>alert("绑定平台操作失败:错误码 ' .$message . '");history.back();</script>';
        }
    } elseif ($action == 'setServerDomain') {
        //设置小程序服务器域名地址
        if ($mini->setServerDomain()) {
            echo '<script>alert("设置小程序服务器域名操作成功");history.back();</script>';
        } else {
            echo '<script>alert("服务器域名域名已设置,无需重复设置");history.back();</script>';
        }
    } elseif ($action == 'setBusinessDomain') {
        //设置业务域名
        if ($mini->setBusinessDomain()) {
            echo '<script>alert("设置小程序业务域名操作成功");history.back();</script>';

        } else {
            echo '<script>alert("设置小程序业务域名操作失败");history.back();</script>';
        }
    } elseif ($action == 'bind') {
        //绑定小程序体验者
        $wechatid = $request->wechatid;
        if ($wechatid) {
            if ($mini->bindMember($wechatid)) {
                echo '<script>alert("绑定小程序体验者操作成功");history.back();</script>';
            } else {
                echo '<script>alert("绑定小程序体验者操作失败");history.back();</script>';
            }
        }
    } elseif ($action == 'uploadCode') {
        //上传小程序代码
        if ($mini->uploadCode($request->template_id)) {
            echo '<script>alert("上传小程序代码操作成功");history.back();</script>';
        } else {
            echo '<script>alert("上传小程序代码操作失败");history.back();</script>';
        }
    } elseif ($action == 'getExpVersion') {
        //获取体验小程序的体验二维码
        $qrcode = $mini->getExpVersion();
        if ($qrcode) {
            $host = host($request->getHost());
            $url = $host.'/'.$qrcode;
            Header("Location:$url");exit;
        } else {
            echo '<script>alert("获取体验小程序的体验二维码操作失败");history.back();</script>';
        }
    } elseif ($action == 'review') {
        //提交审核
        $auditid = Wxminiprogram_audit::query()->where(['appid' => $appid])->where('status', '>=', 2)->orderBy('id', 'desc')->first();
        if ($auditid) {
            echo '<script>alert("有待处理的版本,请先处理该版本相关事项再提交新的审核。审核ID:' . $auditid['auditid'] . '");history.back();</script>';
        } else {
            if ($mini->submitReview()) {
                echo '<script>alert("小程序提交审核操作成功");history.back();</script>';
            } else {
                echo '<script>alert("小程序提交审核操作失败");history.back();</script>';
            }
        }
    } elseif ($action == 'getAudit') {
        //查询指定版本的审核状态
        $auditid = $request->auditid;
        if ($auditid) {
            if ($mini->getAuditStatus($auditid)) {
                $audit = Wxminiprogram_audit::query()->where(['appid' => $appid, 'auditid' => $auditid])->first();
                if ($audit['status'] == 0) {
                    echo '<script>alert("该版本审核已通过");history.back();</script>';
                } elseif ($audit['status'] == 1) {
                    echo '<script>alert("该版本审核失败,原因' . str_replace('"',' ',$audit['reason']). '");history.back();</script>';

                } elseif ($audit['status'] == 2) {
                    echo '<script>alert("该版本小程序正在审核中......");history.back();</script>';
                } else {
                    echo '<script>alert("未知状态......");history.back();</script>';
                }
            } else {
                echo '<script>alert("查询指定版本的审核状态操作失败");history.back();</script>';
            }
        }
    } elseif ($action == 'lastAudit') {
        //查询最新一次提交的审核状态
        $auditid = $mini->getLastAudit();
        if ($auditid) {
            $audit = Wxminiprogram_audit::query()->where(['appid' => $appid, 'auditid' => $auditid])->orderBy('id','desc')->first();
            if ($audit['status'] == 0) {
                echo '<script>alert("该版本审核已通过");history.back();</script>';

            } elseif ($audit['status'] == 1) {
                echo '<script>alert("该版本审核失败,原因' . str_replace('"',' ',$audit['reason']) . '");history.back();</script>';

            } elseif ($audit['status'] == 2) {
                echo '<script>alert("该版本小程序正在审核中......");history.back();</script>';

            } else {
                echo '<script>alert("未知状态......");history.back();</script>';
            }
        } else {
            echo '<script>alert("查询最新一次提交的审核状态操作失败,请查看日志");history.back();</script>';

        }
    } elseif ($action == 'release') {
        //发布已通过审核的小程序
        $auditid = Wxminiprogram_audit::query()->where(['appid' => $appid])->where('status', '>=', 2)->orderBy('id','desc')->first();
        if ($auditid) {
            echo '<script>alert("有待处理的版本,请先处理该版本相关事项再发布版本。审核ID:' . $auditid['auditid'] . '");history.back();</script>';
        } else {
            $errcode = $mini->release();
            if ($errcode) {
                echo '<script>alert("已发布");history.back();</script>';
            } else {
                echo '<script>alert("发布失败,错误代码:' . $errcode . '");history.back();</script>';
            }
        }
    }
}

//视图
<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">

        <title>代程序实现业务</title>

        <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.0/dist/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
    </head>
    <body>
    <div class="card-header">
        微信授权小程序appid: {!! $appid !!}
 </div>
    <div class="card-body" style="text-align: center">
        <div class="container" style="margin-top: 30px">
            <div class="row">
                <form class="form-inline" method="post" action="{{url('/api/v2/setting/do_action')}}">
                    <input type="hidden" name="appid" value=" {!!$appid!!}">
                    <input type="hidden" name="form_uu_id" value=" {!!$form_uu_id!!}">
                    <input type="hidden" name="action" value="auth_bind">
                    <div class="form-group mx-sm-10">
                        <label for="inputEmail3" class="col-form-label">第一步:</label>
                        <button type="submit" class="btn btn-primary">绑定平台</button>
                    </div>
                </form>
            </div>
        </div>
        @if($type!=3)
 <div class="container" style="margin-top: 30px">
                <div class="row">
                    <form class="form-inline" method="post" action="{{url('/api/v2/setting/do_action')}}">
                        <input type="hidden" name="appid" value=" {!!$appid!!}">
                        <input type="hidden" name="action" value="setServerDomain">
                        <div class="form-group mx-sm-10">
                            <label for="inputEmail3" class="col-form-label">第二步:</label>
                            <button type="submit" class="btn btn-primary">设置服务域名</button>
                        </div>
                    </form>
                </div>
            </div>
            <div class="container" style="margin-top: 30px">
                <div class="row">
                    <form class="form-inline" method="post" action="{{url('/api/v2/setting/do_action')}}">
                        <input type="hidden" name="appid" value=" {!!$appid!!}">
                        <input type="hidden" name="action" value="bind">
                        <div class="form-group mx-sm-10">
                            <label for="inputEmail3" class="col-form-label">第三步:</label>
                            <input type="text" class="form-control" id="yuming" name="wechatid" placeholder="绑定小程序体验者" required>
                        </div>
                        <button type="submit" class="btn btn-primary">绑定</button>
                    </form>
                </div>
            </div>
            <div class="container" style="margin-top: 30px">
                <div class="row">
                    <form class="form-inline" method="post" action="{{url('/api/v2/setting/do_action')}}">
                        <input type="hidden" name="appid" value=" {!!$appid!!}">
                        <input type="hidden" name="action" value="uploadCode">
                        <div class="form-group mx-sm-10">
                            <label for="inputEmail3" class="col-form-label">第四步:</label>
                            <input type="number" class="form-control" id="yuming" name="template_id" placeholder="模板ID,微信开放平台获取" required>
                        </div>
                        <button type="submit" class="btn btn-primary">上传小程序代码</button>
                    </form>
                </div>
            </div>
            <div class="container" style="margin-top: 30px">
                <div class="row">
                    <form class="form-inline" method="post" action="{{url('/api/v2/setting/do_action')}}">
                        <input type="hidden" name="appid" value=" {!!$appid!!}">
                        <input type="hidden" name="action" value="getExpVersion">
                        <div class="form-group mx-sm-10">
                            <label for="inputEmail3" class="col-form-label">第五步:</label>
                            <button type="submit" class="btn btn-primary">获取体验小程序的体验二维码</button>
                        </div>
                    </form>
                </div>
            </div>
            <div class="container" style="margin-top: 30px">
                <div class="row">
                    <form class="form-inline" method="post" action="{{url('/api/v2/setting/do_action')}}">
                        <input type="hidden" name="appid" value=" {!!$appid!!}">
                        <input type="hidden" name="action" value="review">
                        <div class="form-group mx-sm-10">
                            <label for="inputEmail3" class="col-form-label">第六步:</label>
                            <button type="submit" class="btn btn-primary">提交审核</button>
                        </div>
                    </form>
                </div>
            </div>
            <div class="container" style="margin-top: 30px">
                <div class="row">
                    <form class="form-inline" method="post" action="{{url('/api/v2/setting/do_action')}}">
                        <input type="hidden" name="appid" value=" {!!$appid!!}">
                        <input type="hidden" name="action" value="lastAudit">
                        <div class="form-group mx-sm-10">
                            <label for="inputEmail3" class="col-form-label">第七步:</label>
                            <button type="submit" class="btn btn-primary">查询最新一次提交的审核状态</button>
                        </div>
                    </form>
                </div>
            </div>
            <div class="container" style="margin-top: 30px">
                <div class="row">
                    <form class="form-inline" method="post" action="{{url('/api/v2/setting/do_action')}}">
                        <input type="hidden" name="appid" value=" {!!$appid!!}">
                        <input type="hidden" name="action" value="getAudit">
                        <div class="form-group mx-sm-10">
                            <label for="inputEmail3" class="col-form-label">第八步:</label>
                            <input type="text" class="form-control" name="auditid" id="yuming" placeholder="审核ID" required>
                        </div>
                        <button type="submit" class="btn btn-primary">查询指定版本的审核状态</button>
                    </form>
                </div>
            </div>
            <div class="container" style="margin-top: 30px">
                <div class="row">
                    <form class="form-inline" method="post" action="{{url('/api/v2/setting/do_action')}}">
                        <input type="hidden" name="appid" value=" {!!$appid!!}">
                        <input type="hidden" name="action" value="release">
                        <div class="form-group mx-sm-10">
                            <label for="inputEmail3" class="col-form-label">第九步:</label>
                            <button type="submit" class="btn btn-primary">发布已通过审核的小程序</button>
                        </div>
                    </form>
                </div>
            </div>
        @endif


 </div>

    </body>
</html>
<?php

namespace App\Helper;
//代小程序实现业务逻辑
use App\Http\Controllers\Api\v2\Model\Account;
use App\Http\Controllers\Api\v2\Model\OpenPlatform;
use App\Http\Controllers\Api\v2\Model\Wechat_config;
use App\Http\Controllers\Api\v2\Model\Wxminiprogram_audit;

class Miniprogram
{
    const domain = 'api.jcgxkj.cn'; //业务域名
    protected $account; //小程序账号信息
    public $code;

    //初始化
    public function __construct($appid)
    {
        //查询小程序信息
        $this->account = Account::query()->where(['appid' => $appid])->first();
        if ($this->account) {
            //判断token是否过期
            if (time() > $this->account['expire']) {
                //更新token
                $miniapp = $this->update_authorizer_access_token($appid, $this->account['authorizer_refresh_token']);
                if ($miniapp) {
                    $this->account->authorizer_access_token = $miniapp->authorizer_access_token;
                    $this->account->authorizer_refresh_token = $miniapp->authorizer_refresh_token;
                    $this->account->expire = time() + 7200;
                    if ($this->account->save()) {
                        $this->errorLog("更新小程序account_token成功" . $appid, $miniapp);
                    }
                } else {
                    $this->errorLog("更新小程序access_token失败,appid:" . $appid, '');
                    exit();
                }
            }
        } else {
            $this->errorLog('小程序不存在,appid:' . $appid, '');
            exit();
        }
    }

    /**
     * @param $appid  小程序appid
     * @param $refresh_token 小程序refresh_token
     * @param $component_appid 开发平台appid
     * @param $component_access_token 开发平台票据
     */
    private function update_authorizer_access_token($appid, $refresh_token)
    {
        $url = 'https://api.weixin.qq.com/cgi-bin/component/api_authorizer_token?component_access_token=' . OpenPlat::init()->access_token->getToken()['component_access_token'];
        $data = '{"component_appid":"' . config('config.app_id') . '","authorizer_appid":"' . $appid . '","authorizer_refresh_token":"' . $refresh_token . '"}';
        $ret = json_decode($this->https_post($url, $data));
        if (isset($ret->authorizer_access_token)) {
            return $ret;
        } else {
            $this->errorLog("更新授权小程序的authorizer_access_token操作失败,appid:" . $appid, $ret);
            return null;
        }
    }

    //小程序绑定|开放平台,先利用接口创建开放平台用于绑定,如多个程序需要绑定到同一个平台下,需要绑定的程序主体一致
    public function bind($form_uu_id)
    {
        //判断是否有open_appid
        $open_appid = Wechat_config::query()->where(['form_uu_id' => $form_uu_id])->first();
        if ($open_appid) {
            if ($open_appid['open_appid']) {
                $url = "https://api.weixin.qq.com/cgi-bin/open/bind?access_token=" . $this->account['authorizer_access_token'];
                $data = '{"appid":"' . $this->account->appid . '" ,"open_appid": "' . $open_appid['open_appid'] . '"}';
            } else {
                $url = "https://api.weixin.qq.com/cgi-bin/open/create?access_token=" . $this->account['authorizer_access_token'];
                $data = '{"appid":"' . $this->account->appid . '"}';
            }
            $ret = json_decode($this->https_post($url, $data));
            if ($ret->errcode == 0) {
                if (!$open_appid['open_appid']) {
                    $open_appid->open_appid = $ret->open_appid;
                    $open_appid->save();
                }
                $this->errorLog('小程序绑定开发平台成功:' . $this->account['appid'], '');
                return true;
            } else {
                $this->code = $ret->errcode;
                $this->errorLog('小程序绑定开发平台失败:' . $this->account['appid'], $ret);
                return false;
            }
        } else {
            $this->errorLog('小程序绑定开发平台信息不存在', '');
            return false;
        }

    }

    /**
     * 设置小程序域名| 域名必须在第三方平台添加
     * @param $authorizer_access_token
     * @return bool
     */
    public function setServerDomain()
    {
        $url = "https://api.weixin.qq.com/wxa/modify_domain?access_token=" . $this->account['authorizer_access_token'];
        $domain = self::domain;
        if (is_array($domain)) {
            $https = '';
            $wss = '';
            foreach ($domain as $key => $value) {
                $https .= '"https://' . $value . '",';
                $wss .= '"wss://' . $value . '",';
            }
            $https = rtrim($https, ',');
            $wss = rtrim($wss, ',');
            $data = '{
                "action":"add",
                "requestdomain":[' . $https . '],
                "wsrequestdomain":[' . $wss . '],
                "uploaddomain":[' . $https . '],
                "downloaddomain":[' . $https . ']
            }';
        } else {
            $data = '{
                "action":"add",
                "requestdomain":"https://' . $domain . '",
                "wsrequestdomain":"wss://' . $domain . '",
                "uploaddomain":"https://' . $domain . '",
                "downloaddomain":"https://' . $domain . '"
            }';
        }
        $ret = json_decode($this->https_post($url, $data));
        if ($ret->errcode == 0) {
            $this->errorLog('小程序业务设置成功:' . $this->account['appid'], '');
            return true;
        } else {
            $this->errorLog('小程序域名设置失败:' . $this->account['appid'], $ret);
            return false;
        }
    }


    /*
    * 设置小程序业务域名,无需加https前缀,但域名必须可以通过https访问
    * @params string / array $domains : 域名地址。只接收一维数组。
    * */
    public function setBusinessDomain()
    {
        $domain = self::domain;
        $url = "https://api.weixin.qq.com/wxa/setwebviewdomain?access_token=" . $this->account->authorizer_access_token;
        if (is_array($domain)) {
            $https = '';
            foreach ($domain as $key => $value) {
                $https .= '"https://' . $value . '",';
            }
            $https = rtrim($https, ',');
            $data = '{
               "action":"add",
               "webviewdomain":[' . $https . ']
            }';
        } else {
            $data = '{
                "action":"add",
                "webviewdomain":"https://' . $domain . '"
            }';
        }
        $ret = json_decode($this->https_post($url, $data));
        if ($ret->errcode == 0) {
            return true;
        } else {
            $this->errorLog("设置小程序业务域名失败,appid:" . $this->account->appid, $ret);
            return false;
        }

    }


    /**
     * 成员管理,绑定小程序体验者
     * @param $wechatid 体验者的微信号
     * @return bool
     */
    public function bindMember($wechatid)
    {
        $url = "https://api.weixin.qq.com/wxa/bind_tester?access_token=" . $this->account['authorizer_access_token'];
        $data = '{"wechatid":"' . $wechatid . '"}';
        $ret = json_decode($this->https_post($url, $data));
        if ($ret->errcode == 0) {
            $this->errorLog("绑定小程序体验者操作成功,appid:" . $this->account['appid'] . ' 体验者的微信号' . $wechatid, $ret);
            return true;
        } else {
            $this->errorLog("绑定小程序体验者操作失败,appid:" . $this->account['appid'], $ret);
            return false;
        }

    }

    /**
     * 成员管理,解绑定小程序体验者
     * @params string $wechatid : 体验者的微信号
     */
    public function unBindMember($wechatid)
    {
        $url = "https://api.weixin.qq.com/wxa/unbind_tester?access_token=" . $this->account['authorizer_access_token'];
        $data = '{"wechatid":"' . $wechatid . '"}';
        $ret = json_decode($this->https_post($url, $data));
        if ($ret->errcode == 0) {
            $this->errorLog("解绑定小程序体验者操作成功,appid:" . $this->account['appid'] . ' 体验者的微信号' . $wechatid, $ret);
            return true;
        } else {
            $this->errorLog("解绑定小程序体验者操作失败,appid:" . $this->account['appid'], $ret);
            return false;
        }
    }


    /**
     * 成员管理,获取小程序体验者列表
     * @return bool
     */
    public function listMember()
    {
        $url = "https://api.weixin.qq.com/wxa/memberauth?access_token=" . $this->account['authorizer_access_token'];
        $data = '{"action":"get_experiencer"}';
        $ret = json_decode($this->https_post($url, $data));
        if ($ret->errcode == 0) {
            $this->errorLog("获取小程序体验者列表操作成功,appid:" . $this->account['appid'], $ret);
            return $ret->members;
        } else {
            $this->errorLog("获取小程序体验者列表操作失败,appid:" . $this->account['appid'], $ret);
            return false;
        }
    }


    /*
    * 为授权的小程序帐号上传小程序代码
    * @params int $template_id : 模板ID
    * @params json $ext_json : 小程序配置文件,json格式
    * @params string $user_version : 代码版本号
    * @params string $user_desc : 代码描述
    */
    public function uploadCode($template_id = 0, $user_version = 'v1.0.0', $user_desc = "匠厨科技小程序模板库")
    {
        $ext_json = json_encode('{"extEnable": true,"extAppid": "' . $this->account['appid'] . '","ext":{"appid": "' . $this->account['appid'] . '"}}');
        $url = "https://api.weixin.qq.com/wxa/commit?access_token=" . $this->account->authorizer_access_token;
        $data = '{"template_id":"' . $template_id . '","ext_json":' . $ext_json . ',"user_version":"' . $user_version . '","user_desc":"' . $user_desc . '"}';
        $ret = json_decode($this->https_post($url, $data));
        if ($ret->errcode == 0) {

            $this->errorLog("为授权的小程序帐号上传小程序代码操作成功,appid:" . $this->account->appid, $ret);
            return true;
        } else {
            $this->errorLog("为授权的小程序帐号上传小程序代码操作失败,appid:" . $this->account->appid, $ret);
            return false;
        }
    }


    /*
    * 获取体验小程序的体验二维码
    * @params string $path : 指定体验版二维码跳转到某个具体页面
    */
    public function getExpVersion($path = '')
    {
        if ($path) {
            $url = "https://api.weixin.qq.com/wxa/get_qrcode?access_token=" . $this->account->authorizer_access_token . "&path=" . urlencode($path);
        } else {
            $url = "https://api.weixin.qq.com/wxa/get_qrcode?access_token=" . $this->account->authorizer_access_token;
        }
        $ret = $this->https_get($url);
        $select = strstr($ret, 'errcode');
        if ($select) {
            $this->errorLog("获取体验小程序的体验二维码操作失败,appid:" . $this->account->appid, $ret);
            return false;
        } else {
            //保存地址
            $imgDir = 'storage/';   //必须是项目所在的绝对路径
            //要生成的图片名字
            $filename = 'experience/' . md5(time() . mt_rand(10, 99)) . ".png"; //新图片名称
            $newFilePath = $imgDir . $filename;
            $data = $ret;
            $newFile = fopen($newFilePath, "w"); //打开文件准备写入
            fwrite($newFile, $data); //写入二进制流到文件
            fclose($newFile); //关闭文件
            $this->errorLog("获取体验小程序的体验二维码操作成功,appid:" . $this->account->appid, $ret);
            return $filename;
        }

    }

    /*
    * 提交审核
    * @params string $tag : 小程序标签,多个标签以空格分开
    * @params strint $title : 小程序页面标题,长度不超过32
    */
    public function submitReview($tag = "餐饮", $title = "餐饮营销小程序开发")
    {
        $first_class = '';
        $second_class = '';
        $first_id = 0;
        $second_id = 0;
        $address = "pages/index/index";
        $category = $this->getCategory();
        if (!empty($category)) {
            $first_class = $category[0]->first_class ? $category[0]->first_class : '';
            $second_class = $category[0]->second_class ? $category[0]->second_class : '';
            $first_id = $category[0]->first_id ? $category[0]->first_id : 0;
            $second_id = $category[0]->second_id ? $category[0]->second_id : 0;
        }
        $getpage = $this->getPage();
        if (!empty($getpage) && isset($getpage[0])) {
            $address = $getpage[0];
        }
        $url = "https://api.weixin.qq.com/wxa/submit_audit?access_token=" . $this->account->authorizer_access_token;
        $data = '{
            "item_list":[{
            "address":"' . $address . '",
            "tag":"' . $tag . '",
            "title":"' . $title . '",
            "first_class":"' . $first_class . '",
            "second_class":"' . $second_class . '",
            "first_id":"' . $first_id . '",
            "second_id":"' . $second_id . '"
            }]
         }';
        $ret = json_decode($this->https_post($url, $data));
        if ($ret->errcode == 0) {
            (new Wxminiprogram_audit())->add(['appid' => $this->account->appid, 'auditid' => $ret->auditid]);
            $this->errorLog("小程序提交审核操作成功,appid:" . $this->account->appid, $ret);
            return true;
        } else {
            $this->errorLog("小程序提交审核操作失败,appid:" . $this->account->appid, $ret);
            return false;
        }

    }


    /*
    * 小程序审核撤回
    * 单个帐号每天审核撤回次数最多不超过1次,一个月不超过10次。
    * */
    public function unDoCodeAudit()
    {
        $url = "https://api.weixin.qq.com/wxa/undocodeaudit?access_token=" . $this->account->authorizer_access_token;
        $ret = json_decode($this->https_get($url));
        if ($ret->errcode == 0) {
            return true;
        } else {
            $this->errorLog("小程序审核撤回操作失败,appid:" . $this->account->appid, $ret);
            return false;
        }
    }

    /*
    * 查询指定版本的审核状态
    * @params string $auditid : 提交审核时获得的审核id
    */
    public function getAuditStatus($auditid)
    {
        $url = "https://api.weixin.qq.com/wxa/get_auditstatus?access_token=" . $this->account->authorizer_access_token;
        $data = '{"auditid":"' . $auditid . '"}';
        $ret = json_decode($this->https_post($url, $data));
        if ($ret->errcode == 0) {
            $reason = isset($ret->reason) ? $ret->reason : '';
            Wxminiprogram_audit::query()->where(['appid' => $this->account->appid, 'auditid' => $auditid])->update([
                'status' => $ret->status,
                'reason' => $reason
            ]);
            $this->errorLog("查询指定版本的审核状态操作成功,appid:" . $this->account->appid, $ret);
            return true;
        } else {
            $this->errorLog("查询指定版本的审核状态操作失败,appid:" . $this->account->appid, $ret);
            return false;
        }

    }

    /*
    * 查询最新一次提交的审核状态
    */
    public function getLastAudit()
    {
        $url = "https://api.weixin.qq.com/wxa/get_latest_auditstatus?access_token=" . $this->account->authorizer_access_token;
        $ret = json_decode($this->https_get($url));
        if ($ret->errcode == 0) {
            $reason = isset($ret->reason) ? $ret->reason : '';
            Wxminiprogram_audit::query()->where(['appid' => $this->account->appid, 'auditid' => $ret->auditid])->update([
                'status' => $ret->status,
                'reason' => $reason
            ]);
            $this->errorLog("查询最新一次提交的审核状态操作成功,appid:" . $this->account->appid, $ret);
            return $ret->auditid;
        } else {
            $this->errorLog("查询最新一次提交的审核状态操作失败,appid:" . $this->account->appid, $ret);
            return false;
        }

    }

    /*
    * 发布已通过审核的小程序
    */
    public function release()
    {
        $url = "https://api.weixin.qq.com/wxa/release?access_token=" . $this->account->authorizer_access_token;
        $data = '{}';
        $ret = json_decode($this->https_post($url, $data));
        if ($ret->errcode == 0) {
            $this->errorLog("发布已通过审核的小程序操作成功,appid:" . $this->account->appid, $ret);
            return true;
        } else {
            $this->errorLog("发布已通过审核的小程序操作失败,appid:" . $this->account->appid, $ret);
            return $ret->errcode;
        }

    }

    /*
    * 获取授权小程序帐号的可选类目
    */
    private function getCategory()
    {
        $url = "https://api.weixin.qq.com/wxa/get_category?access_token=" . $this->account->authorizer_access_token;
        $ret = json_decode($this->https_get($url));
        if ($ret->errcode == 0) {
            return $ret->category_list;
        } else {
            $this->errorLog("获取授权小程序帐号的可选类目操作失败,appid:" . $this->account->appid, $ret);
            return false;
        }
    }

    /*
    * 获取小程序的第三方提交代码的页面配置
    */
    private function getPage()
    {
        $url = "https://api.weixin.qq.com/wxa/get_page?access_token=" . $this->account->authorizer_access_token;
        $ret = json_decode($this->https_get($url));
        if ($ret->errcode == 0) {
            return $ret->page_list;
        } else {
            $this->errorLog("获取小程序的第三方提交代码的页面配置失败,appid:" . $this->account->appid, $ret);
            return false;
        }
    }


    //记录错误消息
    private function errorLog($msg, $ret)
    {
        addLog(['msg' => $msg, 'ret' => $ret]);

    }

    /*
    * 发起POST网络提交
    * @params string $url : 网络地址
    * @params json $data : 发送的json格式数据
    */
    private function https_post($url, $data)
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        if (!empty($data)) {
            curl_setopt($curl, CURLOPT_POST, 1);
            curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        }
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($curl);
        curl_close($curl);
        return $output;
    }

    /*
     *
    * 发起GET网络提交
    * @params string $url : 网络地址
    */
    private function https_get($url)
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($curl, CURLOPT_HEADER, FALSE);
        curl_setopt($curl, CURLOPT_TIMEOUT, 60);
        if (curl_errno($curl)) {
            return 'Errno' . curl_error($curl);
        } else {
            $result = curl_exec($curl);
        }
        curl_close($curl);
        return $result;
    }


}

具体流程如下

发起授权->用户扫描授权->授权回调保存授权信息=可以进行代替实现业务

绑定开放平台(打通用户身份)->设置程序业务域名->绑定小程序体验者->上传小程序代码->获取体验二维码->提交审核->发布


上传小程序代码流程

1.在小程序根目录中创建文件ext.json文件

{
"extEnable": true,
"extAppid": "wx11035e0b985ceb36",  //和项目appid一致,需要在第三方平台绑定才能提交
"ext": {
}

}

MyAnswer博客

2.使用开发者工具上传代码到微信第三方草稿箱,再将草稿箱添加到模板库,然后就可以使用模板库的模板id进行上传代码了

MyAnswer博客

MyAnswer博客



MyAnswer博客
请先登录后发表评论
  • 最新评论
  • 总共0条评论