美文网首页
【通用】小程序webview封装微信公众号网页,解决支付问题

【通用】小程序webview封装微信公众号网页,解决支付问题

作者: 侯蛋蛋_ | 来源:发表于2020-03-09 10:46 被阅读0次

前言

现在小程序审核是越来越严格了,真的是苦了我们程序猿了,尤其是你想做全局都是webview封装的小程序更是难上加难,就建个index,然后webview真的是很难通过的,所以您不建个几十个有用的文件还真不好通过

最近正好做了微课堂的二次开发,正好拿他的全局webview正好做封装,完美

image.png

第一步

首先先看一下您根目录是否有wxapp.php这个文件,如果没有的话,新建一个,然后把下面内容复制进去

<?php 

defined('IN_IA') or exit('Access Denied');
class Tyzm_diamondvoteModuleWxapp extends WeModuleWxapp {
    


    /* 读取设置缓存
     * $type 读取缓存类型 1.全局设置表 2.分销设置表
     */
    private function readCache($type){
        global $_W;

        if($type==1){
            $setting = cache_load('fy_lesson_'.$_W['uniacid'].'_setting');
            if(empty($setting)){
                $setting = $this->getSetting();
                cache_write('fy_lesson_'.$_W['uniacid'].'_setting', $setting);
            }
            return $setting;

        }elseif($type==2){
            $comsetting = cache_load('fy_lesson_'.$_W['uniacid'].'_commission_setting');
            if(empty($comsetting)){
                $comsetting = $this->getComsetting();
                cache_write('fy_lesson_'.$_W['uniacid'].'_commission_setting', $comsetting);
            }
            return $comsetting;
        }
    }

    /* 获取基本设置参数 */
    private function getSetting(){
        global $_W;
        return pdo_get($this->table_setting, array('uniacid'=>$_W['uniacid']));
    }

    /* 获取分销设置参数 */
    private function getComsetting(){
        global $_W;
        return pdo_get($this->table_commission_setting, array('uniacid'=>$_W['uniacid']));
    }

/***************************** 接口数据(实际使用) *********************************/

    /* 分销设置页面 */
    public function doPageShareInfo(){
        global $_W, $_GPC;

        $setting = $this->readCache(1);
        $comsetting = $this->readCache(2);
        $shareinfo = unserialize($comsetting['sharelink']);
        $shareinfo['images'] = $shareinfo['images'] ? $_W['attachurl'].$shareinfo['images'] : $shareinfo['images'];
        
        $wxapp_uniacid = trim($_GPC['wxapp_uniacid']); /* 小程序uniacid */
        $wxapp_version = trim($_GPC['wxapp_version']); /* 小程序版本号 */
        $navigationBar = cache_load('fy_lessonv2_wxapp_navigationBar'.$wxapp_uniacid);
        if(empty($navigationBar)){
            $wxapp_versions = pdo_get('wxapp_versions', array('uniacid'=>$wxapp_uniacid,'version'=>$wxapp_version));
            $appjson = unserialize($wxapp_versions['appjson']);
            $navigationBar = array(
                'navigationBarTextStyle' => $appjson['window']['navigationBarTextStyle'] ? $appjson['window']['navigationBarTextStyle'] : '#000000',
                'navigationBarBackgroundColor' => $appjson['window']['navigationBarBackgroundColor'] ? $appjson['window']['navigationBarBackgroundColor'] : '#ffffff',
            );
            cache_write('fy_lessonv2_wxapp_navigationBar'.$wxapp_uniacid, $navigationBar);
        }

        $data = array(
            'attachurl'  => $_W['attachurl'],
            'avatar'     => $_W['account']['avatar'],
            'setting'    => $setting,
            'shareinfo'  => $shareinfo,
            'hidden_sale'=> $comsetting['hidden_sale'] ? true : false,
            'navigationBar' => $navigationBar,
        );

        return $this->result(0, '', $data);
    }
        
    /* 首页 */
    public function doPageIndex() {
        global $_W;

        $comsetting = $this->readCache(2);
        if($comsetting['hidden_sale']){
            $url = "";
        }else{
            $url = "https://wxg.succeessman.com/app/./index.php?i=3&c=entry&rid=1&isopenlink=first&do=index&m=tyzm_diamondvote";
        }

        return $this->result(0, '', $url);
    }



    /* 获取关注页面 */
    public function doPageFollowQrcode(){
        global $_W, $_GPC;

        /* 检查目录是否存在 */
        $dirpath = "../attachment/images/{$_W['uniacid']}/fy_lessonv2/";
        if (!file_exists($dirpath)) {
            mkdir($dirpath, 0777);
        }

        $imagepath = $dirpath."wxapp_follow.png";
        if(!file_exists($imagepath) || filectime($imagepath) > time()+86400){
            load()->classs('wxapp.account');
            $account_api = WeAccount::create();
            $response = $account_api->getCodeUnlimit('1011', 'fy_lessonv2/pages/follow/index', 450, array(
                'auto_color' => true,
                'line_color' => array(
                    'r' => '#ABABAB',
                    'g' => '#ABABAC',
                    'b' => '#ABABAD',
                ),
            ));
            file_put_contents($imagepath, $response);

            return $this->result(0, '', $_W['uniacid']);
        }
    }

    public function doPayGetopenid() {
        global $_W,$_GPC;

        $ordersn = $_GPC['ordersn'];
        $order_type = substr($ordersn, 0, 1);

        if(is_numeric($_W['openid']) && $_W['openid']>0){
            load()->model('mc');
            $openid = mc_uid2openid($_W['openid']);
        }

        $data = array(
            'openid' => $openid ? $openid : $_W['openid'],
        );

        return $this->result(0, '', $data);
    }

    /* 支付参数 */
    public function doPagePay() {
        global $_GPC, $_W;

        $title = $_GPC['title'];
        $ordersn = $_GPC['ordersn'];
        $order_type = substr($ordersn, 0, 1);

        /* 购买VIP订单 */
        if($order_type=='V'){
            $viporder = pdo_get($this->table_member_order, array('ordersn'=>$ordersn));
            $fee = $viporder['vipmoney'];
        }
        /* 购买VIP订单 */   
        if($order_type=='L'){
            $lessonorder = pdo_get($this->table_order, array('ordersn'=>$ordersn));
            $fee = $lessonorder['price'];
        }
        /* 购买讲师订单 */
        if($order_type=='T'){
            $teacherorder = pdo_get($this->table_teacher_order, array('ordersn'=>$ordersn));
            $fee = $teacherorder['price'];
        }

        if(is_numeric($_W['openid'])){
            load()->model('mc');
            $openid = mc_uid2openid($_W['openid']);
        }

        $paylog = pdo_get($this->table_core_paylog, array('tid' => $ordersn, 'status'=>0));
        if(!empty($paylog)){
            pdo_delete($this->table_core_paylog, array('tid' => $ordersn));
        }

        $order = array(
            'tid'   => $ordersn,
            'user'  => $openid ? $openid : $_W['openid'],
            'fee'   => floatval($fee),
            'title' => $title,
        );

        $pay_params = $this->pay($order);
        if (is_error($pay_params)) {
            return $this->result(1, $pay_params);
        }

        /* 订单类型 1.vip订单 2.课程订单 3.讲师订单 */
        if(!empty($viporder)){
            $pay_params['order_type'] = 1;
        }elseif(!empty($lessonorder)){
            $pay_params['order_type'] = 2;
        }elseif(!empty($teacherorder)){
            $pay_params['order_type'] = 3;
        }
        return $this->result(0, '', $pay_params);
    }

/***************************** 支付返回结果处理开始 *******************************/
    
    /* 支付返回确认 */
    public function payResult($params) {
        global $_W, $_GPC;

        $setting = $this->readCache(1);   /* 基本设置 */
        $comsetting = $this->readCache(2);/* 分销设置 */

        include_once dirname(__FILE__).'/inc/core/Payresult.php';
        $pay_result = new Payresult();
        $pay_result->dealResult($params, $setting, $comsetting, $wechat_type='wxapp');
    }


/***************************** 接口数据(审核使用) *********************************/
    /* 基本设置参数 */
    public function doPageSetting() {
        global $_W;
        $setting = $this->readCache(1);

        $data = array(
            'setting' => $setting
        );

        return $this->result(0, '', $data);
    }
    
    /* 轮播图 */
    public function doPageBanner() {
        global $_W;

        $condition = array(
            'uniacid' => $_W['uniacid'],
            'banner_type' => 0,
            'is_pc'   => 0,
            'is_show' => 1
        );
        $banner = pdo_getall($this->table_banner, $condition);
        foreach($banner as $k=>$v){
            $banner[$k]['img'] = $_W['attachurl'].$v['picture'];
        }
        $this->result(0, '', $banner);
    }
    
    public function doPageGetopenid(){
        global $_W, $_GPC;
        
        if(is_numeric($_W['openid']) && $_W['openid']>0){
            load()->model('mc');
            $openid = mc_uid2openid($_W['openid']);
        }
        $_SESSION['openid']=$openid ? $openid : $_W['openid'];
        $data = array(
            'openid' => $openid ? $openid : $_W['openid'],
            
        );
        $this->result(0, '', $data);
    }


}

第二步改万能小程序的文件

这里也要改成 对应的模块名,然后文件内容里面也要将Xiaof_toupiao批量替换成自己的模块名字

Xiaof_toupiaoModuleWxapp把内容里面的这个Xiaof_toupiao改成对应的模块名
doPageIndex里面的地址改变一下就可以了

image.png
image.png

第三步改site.php

建一个通用的全局变量_W,基本都知道_W其实就是_SESSION的演变,所以这里只需要改变_SESSION就可以改变微擎的全局变量的值

public function __construct() {
        global $_GPC;
        $_SESSION['openid']=$_GPC['openid'];
        $_SESSION['member']['avatar']=urldecode($_GPC['avatar']);
        $_SESSION['member']['nickname']=$_GPC['nickname'];
        $useragent = addslashes($_SERVER['HTTP_USER_AGENT']);
        if(strpos($useragent, 'MicroMessenger') === false && strpos($useragent, 'Windows Phone') === false ){
            //非微信
        }else{
            $oauthuser = m('user') -> Get_checkoauth();
            $oauthuser['openid']=$_GPC['openid'];
            $oauthuser['member']['avatar']=urldecode($_GPC['avatar']);
            $oauthuser['member']['nickname']=$_GPC['nickname'];
            
            $this->oauthuser=$oauthuser;
        }
        
    }
  • doPageGetopenid是获取用户openid的 至于获取用户的头像和名字,直接用微擎自带的方法

相关文章

网友评论

      本文标题:【通用】小程序webview封装微信公众号网页,解决支付问题

      本文链接:https://www.haomeiwen.com/subject/hmqorhtx.html