easywechat升级到5.0

wechat
lqmac 8 months ago
parent bcf188b1b4
commit 4e7a9d25e6
  1. 2
      app/admin/controller/Store.php
  2. 22
      app/api/controller/Goods.php
  3. 30
      app/api/controller/Passport.php
  4. 47
      app/api/service/passport/Login.php
  5. 20
      app/api/service/passport/Party.php
  6. 11
      app/common/model/Goods.php
  7. 30
      app/common/service/qrcode/BaseQRcode.php

@ -230,7 +230,7 @@ class Store extends Controller
if ($channel->isEmpty()) { if ($channel->isEmpty()) {
return $this->renderError('当前渠道不存在'); return $this->renderError('当前渠道不存在');
} }
$user_name = $channel->code.$storeId; $user_name = $channel->alias.$storeId;
//当前账号是否创建 //当前账号是否创建
$merchant = Merchant::where('channel_id', $channel->id)->where('store_id', $storeId)->where('is_delete', 0)->find(); $merchant = Merchant::where('channel_id', $channel->id)->where('store_id', $storeId)->where('is_delete', 0)->find();
if ($merchant) { if ($merchant) {

@ -25,6 +25,8 @@ use think\db\exception\DbException;
use think\response\Json; use think\response\Json;
use app\common\enum\goods\GoodsDeliveryTime; use app\common\enum\goods\GoodsDeliveryTime;
use think\facade\Cache; use think\facade\Cache;
use EasyWeChat\Factory;
/** /**
* 商品控制器 * 商品控制器
* Class Goods * Class Goods
@ -410,9 +412,25 @@ class Goods extends Controller
} }
$store_id = GoodsModel::$storeId; $store_id = GoodsModel::$storeId;
$wxConfig = WxappSettingModel::getConfigBasic($store_id); $wxConfig = WxappSettingModel::getConfigBasic($store_id);
$config = [
'app_id' => $wxConfig['app_id'],
'secret' => $wxConfig['app_secret'],
'response_type' => 'array',// 指定 API 调用返回结果的类型:array(default)/collection/object/raw/自定义类名
'log' => [
'level' => 'debug',
'file' => app()->getRuntimePath().'wechat.log',
],
];
$app = Factory::miniProgram($config);
$data = $app->short_link->getShortLink($page_url, "", false);
$url = "";
if ($data['errcode'] == 0) {
$url = $data['link'];
}
// 请求api获取短连接 // 请求api获取短连接
$link_obj = new ShortLink($wxConfig['app_id'], $wxConfig['app_secret']); // $link_obj = new ShortLink($wxConfig['app_id'], $wxConfig['app_secret']);
$url = $link_obj->shortUrl($page_url, ""); // $url = $link_obj->shortUrl($page_url, "");
return $this->renderSuccess(['url' => $url]); return $this->renderSuccess(['url' => $url]);
} }

@ -140,9 +140,36 @@ class Passport extends Controller
*/ */
public function loginMpWxMobile(): Json public function loginMpWxMobile(): Json
{ {
$params = $this->postForm();
$params['version'] = 1;
// 微信小程序一键登录 // 微信小程序一键登录
$LoginService = new LoginService; $LoginService = new LoginService;
if (!$LoginService->loginMpWxMobile($this->postForm())) { if (!$LoginService->loginMpWxMobile($params)) {
return $this->renderError($LoginService->getError());
}
// 获取登录成功后的用户信息
$userInfo = $LoginService->getUserInfo();
return $this->renderSuccess([
'userId' => (int)$userInfo['user_id'],
'token' => $LoginService->getToken((int)$userInfo['user_id'])
], '登录成功');
}
/**
* 快捷登录: 微信小程序授权手机号登录-新版,基于easywechat5.0
* @return Json
* @throws BaseException
* @throws \think\Exception
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
*/
public function auth(): Json
{
$params = $this->postForm();
$params['version'] = 2;
// 微信小程序一键登录
$LoginService = new LoginService;
if (!$LoginService->auth($params)) {
return $this->renderError($LoginService->getError()); return $this->renderError($LoginService->getError());
} }
// 获取登录成功后的用户信息 // 获取登录成功后的用户信息
@ -152,7 +179,6 @@ class Passport extends Controller
'token' => $LoginService->getToken((int)$userInfo['user_id']) 'token' => $LoginService->getToken((int)$userInfo['user_id'])
], '登录成功'); ], '登录成功');
} }
/** /**
* 是否需要填写昵称头像 (微信小程序端) * 是否需要填写昵称头像 (微信小程序端)
* @param string $code * @param string $code

@ -25,6 +25,8 @@ use app\common\service\BaseService;
use cores\exception\BaseException; use cores\exception\BaseException;
use think\facade\Cache; use think\facade\Cache;
use yiovo\captcha\facade\CaptchaApi; use yiovo\captcha\facade\CaptchaApi;
use EasyWeChat\Factory;
use app\api\model\wxapp\Setting as WxappSettingModel;
/** /**
* 服务类:用户登录 * 服务类:用户登录
@ -201,6 +203,7 @@ class Login extends BaseService
if (empty($form['partyData']['oauth'])) { if (empty($form['partyData']['oauth'])) {
$form['partyData']['oauth'] = 'MP-WEIXIN'; $form['partyData']['oauth'] = 'MP-WEIXIN';
$form['partyData']['code'] = $form['code']; $form['partyData']['code'] = $form['code'];
$form['partyData']['version'] = $form['version'];
} }
$loginData = [ $loginData = [
'mobile' => $plainData['purePhoneNumber'], 'mobile' => $plainData['purePhoneNumber'],
@ -215,7 +218,49 @@ class Login extends BaseService
// 记录登录态 // 记录登录态
return $this->setSession(); return $this->setSession();
} }
/**
* 快捷登录:微信小程序用户-5.0
* @param array $form
* @return bool
* @throws BaseException
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\Exception
*/
public function auth(array $form): bool
{
$wxSetting = WxappSettingModel::getConfigBasic();
$config = [
'app_id' => $wxSetting['app_id'],
'secret' => $wxSetting['app_secret'],
'response_type' => 'array',// 指定 API 调用返回结果的类型:array(default)/collection/object/raw/自定义类名
'log' => [
'level' => 'debug',
'file' => app()->getRuntimePath().'wechat.log',
],
];
$app = Factory::miniProgram($config);
$phoneData = $app->phone_number->getUserPhoneNumber($form['pcode']);
// 整理登录注册数据
if (empty($form['partyData']['oauth'])) {
$form['partyData']['oauth'] = 'MP-WEIXIN';
$form['partyData']['code'] = $form['ucode'];
$form['partyData']['version'] = $form['version'];
}
$loginData = [
'mobile' => $phoneData['phone_info']['purePhoneNumber'] ?? "",
'isParty' => $form['isParty'],
'partyData' => $form['partyData'],
'refereeId' => $form['refereeId'] ?? null,
];
// 自动登录注册
$this->register($loginData);
// 保存第三方用户信息
$this->createUserOauth($this->getUserId(), $loginData['isParty'], $loginData['partyData']);
// 记录登录态
return $this->setSession();
}
/** /**
* 快捷登录:支付宝小程序用户 * 快捷登录:支付宝小程序用户
* @param array $form * @param array $form

@ -19,6 +19,8 @@ use app\common\service\BaseService;
use app\common\enum\Client as ClientEnum; use app\common\enum\Client as ClientEnum;
use cores\exception\BaseException; use cores\exception\BaseException;
use think\Exception; use think\Exception;
use app\api\model\wxapp\Setting as WxappSettingModel;
use EasyWeChat\Factory;
/** /**
* 第三方用户注册登录服务 * 第三方用户注册登录服务
@ -159,7 +161,23 @@ class Party extends BaseService
private function getOauthInfo(array $partyData): ?array private function getOauthInfo(array $partyData): ?array
{ {
if ($partyData['oauth'] === ClientEnum::MP_WEIXIN) { if ($partyData['oauth'] === ClientEnum::MP_WEIXIN) {
$wxSession = static::getMpWxSession($partyData['code']); if ($partyData['version'] == 1) {
$wxSession = static::getMpWxSession($partyData['code']);
} else {
$wxSetting = WxappSettingModel::getConfigBasic();
$config = [
'app_id' => $wxSetting['app_id'],
'secret' => $wxSetting['app_secret'],
'response_type' => 'array',// 指定 API 调用返回结果的类型:array(default)/collection/object/raw/自定义类名
'log' => [
'level' => 'debug',
'file' => app()->getRuntimePath().'wechat.log',
],
];
$app = Factory::miniProgram($config);
$wxSession = $app->auth->session($partyData['code']);
}
return ['oauth_id' => $wxSession['openid'], 'unionid' => $wxSession['unionid'] ?? null]; return ['oauth_id' => $wxSession['openid'], 'unionid' => $wxSession['unionid'] ?? null];
} }
if ($partyData['oauth'] === ClientEnum::WXOFFICIAL) { if ($partyData['oauth'] === ClientEnum::WXOFFICIAL) {

@ -496,6 +496,11 @@ class Goods extends BaseModel
$filter[] = ['is_new', '=', intval($param['is_new'])]; $filter[] = ['is_new', '=', intval($param['is_new'])];
} }
//gj
if (isset($param['merchantId']) && $param['merchantId']) {
$filter[] = ['goods.merchant_id', '=', $param['merchantId']];
}
if (!empty($param['paihang'])) { if (!empty($param['paihang'])) {
$filter[] = ['paihang', '>', 0]; $filter[] = ['paihang', '>', 0];
$query->order('paihang asc'); $query->order('paihang asc');
@ -574,9 +579,9 @@ class Goods extends BaseModel
if (isset($param['is_has_detail']) && $param['is_has_detail'] !== '') { if (isset($param['is_has_detail']) && $param['is_has_detail'] !== '') {
$filter[] = ['goods.is_has_detail', '=', $params['is_has_detail']]; $filter[] = ['goods.is_has_detail', '=', $params['is_has_detail']];
} }
if (isset($param['merchantId']) && $param['merchantId'] !== '') { // if (isset($param['merchant_id']) && $param['merchant_id'] !== '') {
$filter[] = ['goods.merchant_id', '=', $params['merchantId']]; // $filter[] = ['goods.merchant_id', '=', $params['merchant_id']];
} // }
if (isset($param['is_jd_remove']) && $param['is_jd_remove'] !== '') { if (isset($param['is_jd_remove']) && $param['is_jd_remove'] !== '') {
$filter[] = ['goods.is_jd_remove', '=', $params['is_jd_remove']]; $filter[] = ['goods.is_jd_remove', '=', $params['is_jd_remove']];
} }

@ -19,6 +19,7 @@ use app\common\model\h5\Setting as H5SettingModel;
use app\common\model\wxapp\Setting as WxappSettingModel; use app\common\model\wxapp\Setting as WxappSettingModel;
use app\common\service\BaseService; use app\common\service\BaseService;
use cores\exception\BaseException; use cores\exception\BaseException;
use EasyWeChat\Factory;
/** /**
* 二维码服务基类 * 二维码服务基类
@ -78,14 +79,35 @@ class BaseQRcode extends BaseService
if (file_exists($savePath)) { if (file_exists($savePath)) {
return $savePath; return $savePath;
} }
$pathInfo = pathinfo($savePath);
// 小程序配置信息 // 小程序配置信息
$wxConfig = WxappSettingModel::getConfigBasic($storeId); $wxConfig = WxappSettingModel::getConfigBasic($storeId);
$config = [
'app_id' => $wxConfig['app_id'],
'secret' => $wxConfig['app_secret'],
'response_type' => 'array',// 指定 API 调用返回结果的类型:array(default)/collection/object/raw/自定义类名
'log' => [
'level' => 'debug',
'file' => app()->getRuntimePath().'wechat.log',
],
];
$app = Factory::miniProgram($config);
$response = $app->app_code->getUnlimit($scene, [
'page' => $page,
'width' => 430,
]);
if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
$filename = $response->saveAs($pathInfo['dirname'], $pathInfo['basename']);
}
return $savePath;
// var_dump($content);
// exit();
// 请求api获取小程序码 // 请求api获取小程序码
$Qrcode = new WechatQrcode($wxConfig['app_id'], $wxConfig['app_secret']); // $Qrcode = new WechatQrcode($wxConfig['app_id'], $wxConfig['app_secret']);
$content = $Qrcode->getQrcode($scene, $page); // $content = $Qrcode->getQrcode($scene, $page);
// 保存到文件 // 保存到文件
file_put_contents($savePath, $content); // file_put_contents($savePath, $content);
return $savePath; // return $savePath;
} }
/** /**

Loading…
Cancel
Save