bob体育平台康健应用Yii2Wechat后台开拓的数不清总计_php技术_脚本之家

2019-12-17 19:12栏目:编程
TAG:

功能上,本程序可以实现无限级留言与回复,即对留言回复,对回复的留言回复。当然你也可以作有限制的控制,使其只对留言回复,关键是在模板代码中去掉回复的留言中的“回复该留言”即可。欢迎去拍砖!

网上有很多关于YII2.0微信开发教程,但是太过复杂凌乱,所以今天在这里给大家整理总结利用Yii2微信后台开发的系列了,给需要的小伙伴们参考。

完整源码点击此处本站下载。

2.获取config参数接口

复制代码 代码如下:gclist("id,username,inputtime,pid,url,content,path,concat as bpath"); $this->assign('Gklist', $garr['list']); $this->assign; $this->display(); } // +---------------------------------------------------------------------- // | 添加留言 // +---------------------------------------------------------------------- public function add(){ $this->adddata; } // +---------------------------------------------------------------------- // | 网址跳转。如在表单url添加网址的话,点击会跳转到相关网站 // +---------------------------------------------------------------------- public function tourl(){ $this->gettourl; } } ?> 10){ return $data= msubstr($_POST['username'],0,5); }else{ return $data= $_POST['username']; } } } // +---------------------------------------------------------------------- // | path()返回子类的path,父类的path的值为0 // +---------------------------------------------------------------------- public function path(){ $pid=isset?$_POST['pid']:0; $id=$_POST['id']; if{ return 0; } $fat=$this->where->find(); $data=$fat['path'].'-'.$fat['id']; return $data; } // +---------------------------------------------------------------------- // | content()过滤留言内容 // +---------------------------------------------------------------------- public function content() { if (isset && !empty { $data =deleteHtmlTags; $data =safeHtml; if { $data = msubstr; } return $data; } } // +---------------------------------------------------------------------- // | content()过滤URL // +---------------------------------------------------------------------- public function geturl(){ if ) { $data = deleteHtmlTags; $data = safeHtml; return $data=$data?$data:""; } } // +---------------------------------------------------------------------- // |gclist($field,$where='',$pagesize=30)留言列表 // +---------------------------------------------------------------------- // |$field,字段 // +---------------------------------------------------------------------- // |$where查询条件,默认为空 // +---------------------------------------------------------------------- // |$pagesize分页记录,默认为30 // +---------------------------------------------------------------------- // |使用方法,看上面的控制器调用 // +---------------------------------------------------------------------- public function gclist($field,$where='',$pagesize=30) { import; $count = $this->field->count(); $P = new Page; $list=$this->field->where->order->limit($P->firstRow . ',' . $P->listRows)->select(); foreach { $list[$k]['count'] = count(explode; $list[$k]['tousername']=$this->where(array->getField; $str = ''; if { for ($i = 0; $i < $list[$k]['count'] * 2; $i++) { $str .= ''; } $str .= ' '; } $list[$k]['space'] = $str; } $P->setConfig; $P->setConfig; $P->setConfig; $P->setConfig; $P->setConfig; $page = $P->show(); $arr=array('page'=>$page,'list'=>$list); return $arr; } } ?>

2.接收微信发送的异步支付结果通知

希望本文所述对大家的ThinkPHP框架程序设计有所帮助。

所有需要使用JS-SDK的页面必须先注入配置信息,否则将无法调用(同一个url仅需调用一次,对于变化url的SPA的web app可在每次url变化时进行调用,目前Android微信客户端不支持pushState的H5新特性,所以使用pushState来实现web app的页面会导致签名失败,此问题会在Android6.2中修复)。

本文所述留言板程序使用了无限级分类的原理,可以实现无限级留言与回复。留言列表gclist保留了留言层次空格,使留言--回复层次分明。分享给大家供大家参考。具体分析如下:

1.在app/config/params.php中配置token参数

复制代码 代码如下:-- ---------------------------- -- Table structure for `wb_guestbook` -- ---------------------------- DROP TABLE IF EXISTS `wb_guestbook`; CREATE TABLE `eway_guestbook` unsigned NOT NULL AUTO_INCREMENT, `pid` int NOT NULL, `email` varchar NOT NULL, `path` varchar NOT NULL, `username` varchar NOT NULL, `updatetime` int NOT NULL, `ip` varchar NOT NULL, `url` varchar NOT NULL, `inputtime` int NOT NULL, `content` text NOT NULL, `verify` varchar NOT NULL, `isreply` tinyint NOT NULL, `status` tinyint NOT NULL, PRIMARY KEY ENGINE=MyISAM AUTO_INCREMENT=42 DEFAULT CHARSET=utf8;

1.微信支付类 WechatPay.php

复制代码 代码如下:CREATE TABLE `wechat_user` NOT NULL, `openid` varchar COLLATE utf8_unicode_ci NOT NULL, `nickname` varchar COLLATE utf8_unicode_ci NOT NULL COMMENT '微信昵称', `sex` tinyint NOT NULL COMMENT '性别', `headimgurl` varchar COLLATE utf8_unicode_ci NOT NULL COMMENT '头像', `country` varchar COLLATE utf8_unicode_ci NOT NULL COMMENT '国家', `province` varchar COLLATE utf8_unicode_ci NOT NULL COMMENT '省份', `city` varchar COLLATE utf8_unicode_ci NOT NULL COMMENT '城市', `access_token` varchar COLLATE utf8_unicode_ci NOT NULL, `refresh_token` varchar COLLATE utf8_unicode_ci NOT NULL, `created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;ALTER TABLE `wechat_user` ADD PRIMARY KEY ;

获取用户信息的相关接口

Yii2后台配置

复制代码 代码如下:public function actionAccesstoken(){ $code = $_GET["code"]; $state = $_GET["state"]; $appid = Yii::$app->params['wechat']['appid']; $appsecret = Yii::$app->params['wechat']['appsecret']; $request_url = ''; //初始化一个curl会话 $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $request_url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $result = curl_exec; $result = $this->response; //获取token和openid成功,数据解析 $access_token = $result['access_token']; $refresh_token = $result['refresh_token']; $openid = $result['openid']; //请求微信接口,获取用户信息 $userInfo = $this->getUserInfo($access_token,$openid); $user_check = WechatUser::find()->where->one { //更新用户资料 } else { //保存用户资料 } //前端网页的重定向 if { return $this->redirect; } else { return $this->redirect; }}

return [ //微信接入 'wechat' =>[ 'token' => 'your token', ],];

根据微信公众平台开发者文档:

复制代码 代码如下:mchid = $mchid; $this->appid = $appid; $this->key = $key; } public function createJsBizPackage($openid, $totalFee, $outTradeNo, $orderName, $notifyUrl, $timestamp){ $config = array( 'mch_id' => $this->mchid, 'appid' => $this->appid, 'key' => $this->key, ); $unified = array( 'appid' => $config['appid'], 'attach' => '支付', 'body' => $orderName, 'mch_id' => $config['mch_id'], 'nonce_str' => self::createNonceStr(), 'notify_url' => $notifyUrl, 'openid' => $openid, 'out_trade_no' => $outTradeNo, 'spbill_create_ip' => '127.0.0.1', 'total_fee' => intval, 'trade_type' => 'JSAPI', ); $unified['sign'] = self::getSign($unified, $config['key']); $responseXml = self::curlPost('', self::arrayToXml; $unifiedOrder = simplexml_load_string($responseXml, 'SimpleXMLElement', LIBXML_NOCDATA); if ($unifiedOrder === false) { die; } if ($unifiedOrder->return_code != 'SUCCESS') { die($unifiedOrder->return_msg); } if ($unifiedOrder->result_code != 'SUCCESS') { die($unifiedOrder->err_code); } $arr = array( "appId" => $config['appid'], "timeStamp" => $timestamp, "nonceStr" => self::createNonceStr(), "package" => "prepay_signType" => 'MD5', ); $arr['paySign'] = self::getSign; return $arr; } public static function curlGet($url = '', $options = array{ $ch = curl_init; curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_TIMEOUT, 30); if { curl_setopt_array; } //https请求 不验证证书和host curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); $data = curl_exec; return $data; } public static function curlPost($url = '', $postData = '', $options = array{ if { $postData = http_build_query; } $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt; curl_setopt($ch, CURLOPT_POSTFIELDS, $postData); curl_setopt($ch, CURLOPT_TIMEOUT, 30); //设置cURL允许执行的最长秒数 if { curl_setopt_array; } //https请求 不验证证书和host curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); $data = curl_exec; return $data; } public static function createNonceStr{ $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; $str = ''; for ($i = 0; $i"; foreach { if { $xml .= "" . $val . "" . $key . ">"; } else { $xml .= "" . $key . ">"; } } $xml .= ""; return $xml; } public static function getSign{ ksort; $unSignParaString = self::formatQueryParaMap; $signStr = strtoupper(md5($unSignParaString . "&key=" . $key)); return $signStr; } protected static function formatQueryParaMap($paraMap, $urlEncode = false){ $buff = ""; ksort; foreach { if (null != $v && "null" != $v) { if { $v = urlencode; } $buff .= $k . "=" . $v . "&"; } } $reqPar = ''; if { $reqPar = substr($buff, 0, strlen; } return $reqPar; }}

3.微信支付类 WechatPay.php

checkSignature($signature,$timestamp,$nonce)){ echo $echoStr; } } private function checkSignature($signature,$timestamp,$nonce) { // you must define TOKEN by yourself $token = Yii::$app->params['wechat']['token']; if  { echo 'TOKEN is not defined!'; } else { $tmpArr = array($token, $timestamp, $nonce); // use SORT_STRING rule sort; $tmpStr = implode; $tmpStr = sha1; if( $tmpStr == $signature ){ return true; }else{ return false; } } }}
public function actionUserinfo(){ if(isset{ $openid = $_REQUEST["openid"]; $user = WechatUser::find()->where->one { $result['error'] = 0; $result['msg'] = '获取成功'; $result['user'] = $user; } else { $result['error'] = 1; $result['msg'] = '没有该用户'; } } else { $result['error'] = 1; $result['msg'] = 'openid为空'; } return $result;}

复制代码 代码如下:public function actionPay(){ if(isset&&isset&&isset($_REQUEST["totalFee"])){ //uid、oid、totalFee $uid = $_REQUEST["uid"]; $oid = $_REQUEST["oid"]; $totalFee = $_REQUEST["totalFee"]; $timestamp = time(); //微信支付参数 $appid = Yii::$app->params['wechat']['appid']; $mchid = Yii::$app->params['wechat']['mchid']; $key = Yii::$app->params['wechat']['key']; $notifyUrl = Yii::$app->params['wechat']['notifyUrl']; //支付打包 $wx_pay = new WechatPay; $package = $wx_pay->createJsBizPackage($uid, $totalFee, $oid, $notifyUrl, $timestamp); $result['error'] = 0; $result['msg'] = '支付打包成功'; $result['package'] = $package; return $result; }else{ $result['error'] = 1; $result['msg'] = '请求参数错误'; } return $result;}

2.在app/config/main.php中配置路由

版权声明:本文由bob体育app发布于编程,转载请注明出处:bob体育平台康健应用Yii2Wechat后台开拓的数不清总计_php技术_脚本之家