123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- <?php
- 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 {
-
- public static function login($params=[]){
- Db::startTrans();
- $result = [];
- switch ($params["source"]){
- case 1:
- $result = self::mp($params);
- break;
- case 2:
- break;
- default:
- Db::rollback();
- throw new \Exception("非法操作",0);
- }
- Db::commit();
- return $result;
- }
-
- 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"]
- ];
- }
- }
|