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

前言

现在小程序审核是越来越严格了,真的是苦了我们程序猿了,尤其是你想做全局都是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的 至于获取用户的头像和名字,直接用微擎自带的方法

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 160,444评论 4 365
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,867评论 1 298
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 110,157评论 0 248
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,312评论 0 214
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,673评论 3 289
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,802评论 1 223
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 32,010评论 2 315
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,743评论 0 204
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,470评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,696评论 2 250
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,187评论 1 262
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,538评论 3 258
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,188评论 3 240
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,127评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,902评论 0 198
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,889评论 2 283
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,741评论 2 274

推荐阅读更多精彩内容