From 5f4e5ebd1b4ddaf67eb9241a3a52464a8eb7368e Mon Sep 17 00:00:00 2001 From: Anyon Date: Mon, 14 Dec 2020 10:57:39 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/data/controller/api/Goods.php | 2 +- app/data/controller/api/Login.php | 6 ++-- app/data/controller/api/Notify.php | 8 ++--- app/data/controller/api/Wechat.php | 22 ++++++++++++-- app/data/controller/api/Wxapp.php | 29 +++++++++++++------ app/data/controller/api/auth/Address.php | 3 +- app/data/controller/api/auth/Config.php | 8 +++-- app/data/controller/api/auth/Order.php | 4 +-- app/data/service/PaymentService.php | 14 ++++----- app/data/service/UserService.php | 29 +++++++++++-------- ...nPayService.php => JoinPaymentService.php} | 8 ++--- ...ayService.php => WechatPaymentService.php} | 10 +++---- 12 files changed, 89 insertions(+), 54 deletions(-) rename app/data/service/payment/{JoinPayService.php => JoinPaymentService.php} (96%) rename app/data/service/payment/{WechatPayService.php => WechatPaymentService.php} (92%) diff --git a/app/data/controller/api/Goods.php b/app/data/controller/api/Goods.php index c77536981..6511b2eb3 100644 --- a/app/data/controller/api/Goods.php +++ b/app/data/controller/api/Goods.php @@ -40,7 +40,7 @@ class Goods extends Controller */ public function getGoods() { - if ($code = input('code', null)) { + if ($code = input('code', '')) { $this->app->db->name('ShopGoods')->where(['code' => $code])->update([ 'num_read' => $this->app->db->raw('num_read+1'), ]); diff --git a/app/data/controller/api/Login.php b/app/data/controller/api/Login.php index 5c07d7460..1322a8720 100644 --- a/app/data/controller/api/Login.php +++ b/app/data/controller/api/Login.php @@ -44,7 +44,7 @@ class Login extends Controller if (empty($user)) $this->error('该手机号还没有注册哦!'); if (empty($user['status'])) $this->error('该用户账号状态异常!'); if (md5($data['password']) === $user['password']) { - $this->success('手机登录成功!', UserService::instance()->save($map, [], 'web', true)); + $this->success('手机登录成功!', UserService::instance()->set($map, [], $this->type, true)); } else { $this->error('账号登录失败,请稍候再试!'); } @@ -79,7 +79,7 @@ class Login extends Controller $this->error('手机号已注册,请使用其它手机号!'); } $data['password'] = md5($data['password']); - $user = UserService::instance()->save($map, $data, $this->type, true); + $user = UserService::instance()->set($map, $data, $this->type, true); empty($user) ? $this->error('手机注册失败!') : $this->success('用户注册成功!', $user); } @@ -96,7 +96,7 @@ class Login extends Controller 'phone.require' => '手机号不能为空!', 'secure.require' => '安全码不能为空!', ]); - if ($data['secure'] !== sysconf('zt.secure_code')) $this->error('短信发送安全码错误!'); + if ($data['secure'] !== sysconf('zt.secure_code')) $this->error('接口安全码错误!'); [$state, $message, $data] = MessageService::instance()->sendVerifyCode($data['phone']); $state ? $this->success($message, $data) : $this->error($message, $data); } diff --git a/app/data/controller/api/Notify.php b/app/data/controller/api/Notify.php index 57fa235c6..fbcc7767c 100644 --- a/app/data/controller/api/Notify.php +++ b/app/data/controller/api/Notify.php @@ -2,8 +2,8 @@ namespace app\data\controller\api; -use app\data\service\payment\JoinPayService; -use app\data\service\payment\WechatPayService; +use app\data\service\payment\JoinPaymentService; +use app\data\service\payment\WechatPaymentService; use think\admin\Controller; /** @@ -25,7 +25,7 @@ class Notify extends Controller public function wxpay(string $scene = 'order'): string { if (strtolower($scene) === 'order') { - return WechatPayService::instance()->notify(); + return WechatPaymentService::instance()->notify(); } else { return 'success'; } @@ -42,7 +42,7 @@ class Notify extends Controller public function joinpay(string $scene = 'order'): string { if (strtolower($scene) === 'order') { - return JoinPayService::instance()->notify(); + return JoinPaymentService::instance()->notify(); } else { return 'success'; } diff --git a/app/data/controller/api/Wechat.php b/app/data/controller/api/Wechat.php index 1a3cb59a3..357721060 100644 --- a/app/data/controller/api/Wechat.php +++ b/app/data/controller/api/Wechat.php @@ -26,6 +26,12 @@ class Wechat extends Controller */ private $type = UserService::APITYPE_WECHAT; + /** + * 唯一绑定字段 + * @var string + */ + private $field; + /** * 粉丝OPNEID * @var string @@ -50,6 +56,18 @@ class Wechat extends Controller */ private $userInfo; + /** + * 控制器初始化 + */ + protected function initialize() + { + if (empty(UserService::TYPES[$this->type]['auth'])) { + $this->error("接口类型[{$this->type}]没有定义规则"); + } else { + $this->field = UserService::TYPES[$this->type]['auth']; + } + } + /** * 加载对应JSSDK数据 * @return \think\Response @@ -76,8 +94,8 @@ class Wechat extends Controller $data['openid2'] = $data['openid']; $data['base_sex'] = ['未知', '男', '女'][$data['sex']] ?? '未知'; if (isset($data['headimgurl'])) $data['headimg'] = $data['headimgurl']; - $map = isset($data['unionid']) ? ['unionid' => $data['unionid']] : [UserService::AUTHS[$this->type] => $this->openid]; - $this->userInfo = UserService::instance()->save($map, array_merge($map, $data), $this->type, true); + $map = isset($data['unionid']) ? ['unionid' => $data['unionid']] : [$this->field => $this->openid]; + $this->userInfo = UserService::instance()->set($map, array_merge($map, $data), $this->type, true); $content = $this->_buildContent(); } return Response::create($content)->contentType('application/x-javascript'); diff --git a/app/data/controller/api/Wxapp.php b/app/data/controller/api/Wxapp.php index dfc8ce969..82e44ab36 100644 --- a/app/data/controller/api/Wxapp.php +++ b/app/data/controller/api/Wxapp.php @@ -21,13 +21,19 @@ class Wxapp extends Controller * 接口认证类型 * @var string */ - protected $type = UserService::APITYPE_WXAPP; + private $type = UserService::APITYPE_WXAPP; + + /** + * 唯一绑定字段 + * @var string + */ + private $field; /** * 小程序配置参数 * @var array */ - protected $config; + private $config; /** * 接口服务初始化 @@ -40,8 +46,13 @@ class Wxapp extends Controller $this->config = [ 'appid' => sysconf('data.wxapp_appid'), 'appsecret' => sysconf('data.wxapp_appkey'), - 'cache_path' => $this->app->getRuntimePath() . 'wechat', + 'cache_path' => $this->app->getRootPath() . 'runtime' . DIRECTORY_SEPARATOR . 'wechat', ]; + if (empty(UserService::TYPES[$this->type]['auth'])) { + $this->error("接口类型[{$this->type}]没有定义规则"); + } else { + $this->field = UserService::TYPES[$this->type]['auth']; + } } /** @@ -55,9 +66,9 @@ class Wxapp extends Controller { $input = $this->_vali(['code.require' => '登录凭证code不能为空!']); [$openid, $unionid, $sessionKey] = $this->_getSessionKey($input['code']); - $map = empty($unionid) ? ['openid1' => $openid] : ['unionid' => $unionid]; - $data = array_merge($map, ['openid1' => $openid, 'session_key' => $sessionKey]); - $this->success('授权换取成功!', UserService::instance()->save($map, $data, $this->type, true)); + $map = empty($unionid) ? [$this->field => $openid] : ['unionid' => $unionid]; + $data = array_merge($map, [$this->field => $openid, 'session_key' => $sessionKey]); + $this->success('授权换取成功!', UserService::instance()->set($map, $data, $this->type, true)); } /** @@ -80,9 +91,9 @@ class Wxapp extends Controller $result = Crypt::instance($this->config)->decode($input['iv'], $input['session_key'], $input['encrypted']); if (is_array($result) && isset($result['openId']) && isset($result['avatarUrl']) && isset($result['nickName'])) { $sex = ['未知', '男', '女'][$result['gender']] ?? '未知'; - $map = empty($result['unionId']) ? ['openid1' => $result['openId']] : ['unionid' => $result['unionId']]; - $data = [UserService::AUTHS[$this->type] => $result['openId'], 'headimg' => $result['avatarUrl'], 'nickname' => $result['nickName'], 'base_sex' => $sex]; - $this->success('数据解密成功!', UserService::instance()->save($map, array_merge($map, $data), $this->type, true)); + $map = empty($result['unionId']) ? [$this->field => $result['openId']] : ['unionid' => $result['unionId']]; + $data = [$this->field => $result['openId'], 'headimg' => $result['avatarUrl'], 'nickname' => $result['nickName'], 'base_sex' => $sex]; + $this->success('数据解密成功!', UserService::instance()->set($map, array_merge($map, $data), $this->type, true)); } elseif (is_array($result) && isset($result['phoneNumber'])) { $this->success('数据解密成功!', $result); } else { diff --git a/app/data/controller/api/auth/Address.php b/app/data/controller/api/auth/Address.php index 6955bb356..3f0504983 100644 --- a/app/data/controller/api/auth/Address.php +++ b/app/data/controller/api/auth/Address.php @@ -109,8 +109,7 @@ class Address extends Auth public function remove() { $map = $this->_vali([ - 'uid.value' => $this->uuid, - 'code.require' => '地址编号不能为空!', + 'uid.value' => $this->uuid, 'code.require' => '地址编号不能为空!', ]); $address = $this->app->db->name($this->table)->where($map)->find(); if (empty($address)) $this->error('需要删除的地址不存在!'); diff --git a/app/data/controller/api/auth/Config.php b/app/data/controller/api/auth/Config.php index d1855652e..d3b0ae198 100644 --- a/app/data/controller/api/auth/Config.php +++ b/app/data/controller/api/auth/Config.php @@ -22,11 +22,13 @@ class Config extends Auth { $types = []; foreach (PaymentService::TYPES as $type => $arr) { - if (in_array($this->type, $arr['auth'])) $types[] = $type; + if (isset($arr['auth']) && in_array($this->type, $arr['auth'])) { + $types[] = $type; + } } $map = ['status' => 1, 'deleted' => 0]; $query = $this->app->db->name('DataPayment')->where($map)->whereIn('type', $types); - $result = $query->order('sort desc,id desc')->field('id,name,type,create_at')->select(); - $this->success('获取支付通道数据', $result->toArray()); + $collect = $query->order('sort desc,id desc')->field('id,name,type,create_at')->select(); + $this->success('获取支付通道数据', $collect->toArray()); } } \ No newline at end of file diff --git a/app/data/controller/api/auth/Order.php b/app/data/controller/api/auth/Order.php index dd208fc5c..afd96f70d 100644 --- a/app/data/controller/api/auth/Order.php +++ b/app/data/controller/api/auth/Order.php @@ -192,9 +192,9 @@ class Order extends Auth $order = $this->app->db->name('ShopOrder')->where($map)->find(); if (empty($order)) $this->error('获取订单数据失败,请稍候再试!'); if ($order['status'] != 2) $this->error('该订单不能发起支付哦!'); - if ($order['payment_status']) $this->error('订单已经支付,不需要再次支付哦!'); + if ($order['payment_status'] > 0) $this->error('订单已经完成支付!'); try { - $openid = $this->user[UserService::AUTHS[$this->type]] ?? ''; + $openid = $this->user[UserService::TYPES[$this->type]['auth']] ?? ''; $params = PaymentService::build($data['payid'])->create($openid, $order['order_no'], $order['amount_total'], '商城订单支付', ''); $this->success('获取支付参数成功!', $params); } catch (HttpResponseException $exception) { diff --git a/app/data/service/PaymentService.php b/app/data/service/PaymentService.php index b4a7f1bfe..ce0d186ea 100644 --- a/app/data/service/PaymentService.php +++ b/app/data/service/PaymentService.php @@ -2,8 +2,8 @@ namespace app\data\service; -use app\data\service\payment\JoinPayService; -use app\data\service\payment\WechatPayService; +use app\data\service\payment\JoinPaymentService; +use app\data\service\payment\WechatPaymentService; use think\admin\Service; /** @@ -29,7 +29,7 @@ abstract class PaymentService extends Service ], PaymentService::PAYMENT_JOINPAY_GZH => [ 'name' => '汇聚服务号支付', - 'auth' => 'UserService::APITYPE_WECHAT', + 'auth' => [UserService::APITYPE_WECHAT], ], PaymentService::PAYMENT_WECHAT_JSAPI => [ 'name' => '微信商户支付', @@ -51,7 +51,7 @@ abstract class PaymentService extends Service /** * 支付服务对象 - * @var JoinPayService|WechatPayService + * @var JoinPaymentService|WechatPaymentService */ protected static $driver = []; @@ -59,7 +59,7 @@ abstract class PaymentService extends Service /** * 根据配置实例支付服务 * @param string $payid 支付通道编号 - * @return JoinPayService|WechatPayService + * @return JoinPaymentService|WechatPaymentService * @throws \think\Exception */ public static function build(string $payid): PaymentService @@ -84,9 +84,9 @@ abstract class PaymentService extends Service // 实例化具体支付通道类型 static::$type = $payment['type']; if (stripos(static::$type, 'wechat_') === 0) { - return static::$driver[$payid] = WechatPayService::instance(); + return static::$driver[$payid] = WechatPaymentService::instance(); } else { - return static::$driver[$payid] = JoinPayService::instance(); + return static::$driver[$payid] = JoinPaymentService::instance(); } } diff --git a/app/data/service/UserService.php b/app/data/service/UserService.php index 7a5358989..5c5069b2f 100644 --- a/app/data/service/UserService.php +++ b/app/data/service/UserService.php @@ -16,18 +16,23 @@ class UserService extends Service const APITYPE_WXAPP = 'wxapp'; const APITYPE_WECHAT = 'wechat'; - const AUTHS = [ - UserService::APITYPE_WAP => 'phone,password', - UserService::APITYPE_WEB => 'phone,password', - UserService::APITYPE_WXAPP => 'openid1', - UserService::APITYPE_WECHAT => 'openid2', - ]; - const TYPES = [ - UserService::APITYPE_WAP => '手机浏览器访问', - UserService::APITYPE_WEB => '电脑浏览器访问', - UserService::APITYPE_WXAPP => '微信小程序访问', - UserService::APITYPE_WECHAT => '微信服务号访问', + UserService::APITYPE_WAP => [ + 'name' => '手机浏览器访问', + 'auth' => '', + ], + UserService::APITYPE_WEB => [ + 'name' => '电脑浏览器访问', + 'auth' => '', + ], + UserService::APITYPE_WXAPP => [ + 'name' => '微信小程序访问', + 'auth' => 'openid1', + ], + UserService::APITYPE_WECHAT => [ + 'name' => '微信服务号访问', + 'auth' => 'openid2', + ], ]; /** @@ -69,7 +74,7 @@ class UserService extends Service * @throws \think\db\exception\DbException * @throws \think\db\exception\ModelNotFoundException */ - public function save(array $map, array $data, string $type, bool $force = false): array + public function set(array $map, array $data, string $type, bool $force = false): array { unset($data['id'], $data['deleted'], $data['create_at']); if ($uuid = $this->app->db->name('DataUser')->where($map)->where(['deleted' => 0])->value('id')) { diff --git a/app/data/service/payment/JoinPayService.php b/app/data/service/payment/JoinPaymentService.php similarity index 96% rename from app/data/service/payment/JoinPayService.php rename to app/data/service/payment/JoinPaymentService.php index f9def49c4..2e84239ff 100644 --- a/app/data/service/payment/JoinPayService.php +++ b/app/data/service/payment/JoinPaymentService.php @@ -7,10 +7,10 @@ use think\admin\extend\HttpExtend; /** * 汇聚支付基础服务 - * Class JoinPayService + * Class JoinPaymentService * @package app\store\service\payment */ -class JoinPayService extends PaymentService +class JoinPaymentService extends PaymentService { /** * 请求地址 @@ -44,9 +44,9 @@ class JoinPayService extends PaymentService /** * 汇聚支付服务初始化 - * @return JoinPayService + * @return JoinPaymentService */ - protected function initialize(): JoinPayService + protected function initialize(): JoinPaymentService { $this->appid = static::$config['joinpay_appid']; $this->trade = static::$config['joinpay_trade'];; diff --git a/app/data/service/payment/WechatPayService.php b/app/data/service/payment/WechatPaymentService.php similarity index 92% rename from app/data/service/payment/WechatPayService.php rename to app/data/service/payment/WechatPaymentService.php index 6bae8a902..c09e6aaea 100644 --- a/app/data/service/payment/WechatPayService.php +++ b/app/data/service/payment/WechatPaymentService.php @@ -7,10 +7,10 @@ use WePay\Order; /** * 微信官方公众号支持 - * Class WechatPayService + * Class WechatPaymentService * @package app\store\service\payment */ -class WechatPayService extends PaymentService +class WechatPaymentService extends PaymentService { /** * 微信对象对象 @@ -20,15 +20,15 @@ class WechatPayService extends PaymentService /** * 微信支付服务初始化 - * @return WechatPayService + * @return WechatPaymentService */ - protected function initialize(): WechatPayService + protected function initialize(): WechatPaymentService { $this->payment = Order::instance([ 'appid' => static::$config['wechat_appid'], 'mch_id' => static::$config['wechat_mch_id'], 'mch_key' => static::$config['wechat_mch_key'], - 'cache_path' => $this->app->getRuntimePath() . 'wechat', + 'cache_path' => $this->app->getRootPath() . 'runtime' . DIRECTORY_SEPARATOR . 'wechat', ]); return $this; }