123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- <?php
- // +----------------------------------------------------------------------
- // | A3Mall
- // +----------------------------------------------------------------------
- // | Copyright (c) 2020 http://www.a3-mall.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Author: xzncit <158373108@qq.com>
- // +----------------------------------------------------------------------
- namespace app\api\service\wechat;
- use app\api\service\Service;
- use app\common\library\wechat\Factory;
- use app\common\service\wechat\WechatUser as WechatUserService;
- use mall\basic\Users;
- use mall\library\tools\jwt\Token;
- use think\facade\Config;
- use think\facade\Db;
- use app\common\models\Setting as SettingModel;
- use app\common\models\wechat\WechatUsers as WechatUsersModel;
- use app\common\service\Spread\Spread;
- use app\common\models\users\Users as UsersModel;
- class OAuth extends Service {
- /**
- * 登录
- * @param array $params
- * @return mixed
- * @throws \Exception
- */
- public static function login($params=[]){
- Db::startTrans();
- $result = [];
- switch ($params["source"]){
- case 1: // H5
- $result = self::mp($params);
- break;
- case 2: // mini
- break;
- default:
- Db::rollback();
- throw new \Exception("非法操作",0);
- }
- Db::commit();
- return $result;
- }
- /**
- * 公众号登录
- * @param $params
- * @return array
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- protected static function mp($params){
- $appid = SettingModel::getArrayData("wechat.appid");
- $state = $params["state"] ?? "";
- if($state != $appid){
- $domain = getDomain() . (in_array(Config::get("base.app_type"),["pc","page"]) ? "/wap" : "");
- return [ "url"=>$domain, "appid"=>$appid, "state"=>$appid ];
- }
- $wxToken = Factory::wechat()->oauth->getOauthAccessToken($params["code"]);
- if(!isset($wxToken['openid'])) {
- throw new \Exception("获取授权信息失败,openid为空",0);
- }
- $user = Factory::wechat()->oauth->getUserInfo($wxToken['access_token'],$wxToken['openid']);
- $condition = [];
- if(isset($user["unionid"])){
- $condition["unionid"] = $user["unionid"];
- }else{
- $condition["openid"] = $user['openid'];
- }
- // 如果用户不存在,注册新用户
- if(!$row=WechatUsersModel::where($condition)->find()){
- $user_id = WechatUserService::register($user);
- }
- if(!empty($row)) $user_id = $row["user_id"];
- $info = Users::info($user_id);
- $token = Token::get("id",$info["id"]);
- return [
- "id" => $info["id"],
- "token" => $token,
- "username" => $info["username"],
- "nickname" => $info["nickname"],
- "group_name" => $info["group_name"],
- "shop_count" => $info["shop_count"],
- "coupon_count" => $info["coupon_count"],
- "mobile" => $info["mobile"],
- "sex" => $info["sex"],
- "point" => $info["point"],
- "amount" => $info["amount"],
- "last_ip" => $info["last_ip"],
- "last_login" => $info["last_login"]
- ];
- }
- }
|