1.自带的Esaywechat的版本是3.x https://www.easywechat.com/docs/3.x/js
2.Esaywechat的路径 在 /vendor/overtrue/wechat 目录下
3.创建微信配置文件 /application/extra/wxconfig.php
<?phpreturn [ /** * Debug 模式,bool 值:true/false * * 当值为 false 时,所有的日志都不会记录 */ 'debug' => true, /** * 账号基本信息,请从微信公众平台/开放平台获取 */ 'app_id' => 'your-app-id', // AppID 'secret' => 'your-app-secret', // AppSecret 'token' => 'your-token', // Token 'aes_key' => '', // EncodingAESKey,安全模式与兼容模式下请一定要填写!!! /** * 日志配置 * * level: 日志级别, 可选为: * debug/info/notice/warning/error/critical/alert/emergency * permission:日志文件权限(可选),默认为null(若为null值,monolog会取0644) * file:日志文件位置(绝对路径!!!),要求可写权限 */ 'log' => [ 'level' => 'debug', 'permission' => 0777, 'file' => '/tmp/easywechat.log', ], /** * OAuth 配置 * * scopes:公众平台(snsapi_userinfo / snsapi_base),开放平台:snsapi_login * callback:OAuth授权完成后的回调页地址 */ 'oauth' => [ 'scopes' => ['snsapi_userinfo'], 'callback' => '/examples/oauth_callback.php', ], /** * 微信支付 */ 'payment' => [ 'merchant_id' => 'your-mch-id', 'key' => 'key-for-signature', 'cert_path' => 'path/to/your/cert.pem', // XXX: 绝对路径!!!! 'key_path' => 'path/to/your/key', // XXX: 绝对路径!!!! // 'device_info' => '013467007045764', // 'sub_app_id' => '', // 'sub_merchant_id' => '', // ... ], /** * Guzzle 全局设置 * * 更多请参考: http://docs.guzzlephp.org/en/latest/request-options.html */ 'guzzle' => [ 'timeout' => 3.0, // 超时时间(秒) //'verify' => false, // 关掉 SSL 认证(强烈不建议!!!) ],];
以上内容放入wxconfig.php
控制器
<?phpnamespace appindexcontroller;use thinkConfig;use EasyWeChatFoundationApplication;use thinkSession;use appcommoncontrollerFrontend;class Index extends Frontend{ protected $noNeedLogin = '*'; protected $noNeedRight = '*'; protected $layout = ''; protected $wechat = null; public function _initialize() { parent::_initialize(); $config = Config::get("wxconfig"); $this->wechat = new Application($config); if(empty(Session::get("wechat_user"))) { Session::set("loadurl", $this->request->url()); $response = $this->wechat->oauth->redirect(); $response->send(); } } // 首页 public function index() { //此处是首页代码 // js -- 微信分享功能 支付 获取位置信息 打开调试模式 $js = $this->wechat->js->config(['updateAppMessageShareData','chooseWXPay', 'getLocation'],true); $this->assign("js", $js); } }
进行微信回调的方法
public function home() { $config = Config::get("wxconfig"); $this->wechat = new Application($config); $userinfo = $this->wechat->oauth->user(); Session::set("wechat_user",$userinfo->toArray()); //微信基本信息 Session::set("user_info",$this->auth->getUserInfo()); // $url = Session::get("loadurl"); Header("Location: $url"); }
如果要使用其他的方法 可以去看Application文件
在Application文件内 有相应的注释
/** * Class Application. * * @property EasyWeChatCoreAccessToken $access_token * @property EasyWeChatServerGuard $server * @property EasyWeChatUserUser $user * @property EasyWeChatUserTag $user_tag * @property EasyWeChatUserGroup $user_group * @property EasyWeChatJsJs $js * @property OvertrueSocialiteProvidersWeChatProvider $oauth * @property EasyWeChatMenuMenu $menu * @property EasyWeChatNoticeNotice $notice * @property EasyWeChatMaterialMaterial $material * @property EasyWeChatMaterialTemporary $material_temporary * @property EasyWeChatStaffStaff $staff * @property EasyWeChatUrlUrl $url * @property EasyWeChatQRCodeQRCode $qrcode * @property EasyWeChatSemanticSemantic $semantic * @property EasyWeChatStatsStats $stats * @property EasyWeChatPaymentMerchant $merchant * @property EasyWeChatPaymentPayment $payment * @property EasyWeChatPaymentLuckyMoneyLuckyMoney $lucky_money * @property EasyWeChatPaymentMerchantPayMerchantPay $merchant_pay * @property EasyWeChatPaymentCashCouponCashCoupon $cash_coupon * @property EasyWeChatReplyReply $reply * @property EasyWeChatBroadcastBroadcast $broadcast * @property EasyWeChatCardCard $card * @property EasyWeChatDeviceDevice $device * @property EasyWeChatCommentComment $comment * @property EasyWeChatShakeAroundShakeAround $shakearound * @property EasyWeChatOpenPlatformOpenPlatform $open_platform * @property EasyWeChatMiniProgramMiniProgram $mini_program * * @method EasyWeChatSupportCollection clearQuota() * @method EasyWeChatSupportCollection getCallbackIp() */
然后根据微信文档查找到需要使用的方法https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/JS-SDK.html#61进行功能写入
前端分享功能代码
<script src="https://res.wx.qq.com/open/js/jweixin-1.6.0.js"></script><script>wx.config({$js});wx.ready(function () { //需在用户可能点击分享按钮前就先调用 wx.updateAppMessageShareData({ title: "{$info.share_title}", // 分享标题 desc: '{$info.share_desc|htmlentities}', // 分享描述 link: "{$domain}/index.php/index/index/index?shareid={$login_user_id['id']}", // 分享链接,该链接域名或路径必须与当前页面对应的公众号JS安全域名一致 imgUrl: '{$info.share_img}', // 分享图标 success: function (res) { } });}); </script>
有不足的地方 希望大佬们指导一下
希望以上内容对你有所帮助!如果还有其他问题,请随时提问。 各类知识收集 拥有多年CMS企业建站经验,对 iCMS, Fastadmin, ClassCMS, LeCMS, PbootCMS, PHPCMS, 易优CMS, YzmCMS, 讯睿CMS, 极致CMS, Wordpress, HkCMS, YznCMS, WellCMS, ThinkCMF, 等各类cms的相互转化,程序开发,网站制作,bug修复,程序杀毒,插件定制都可以提供最佳解决方案。