OAuth.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | A3Mall
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2020 http://www.a3-mall.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Author: xzncit <158373108@qq.com>
  8. // +----------------------------------------------------------------------
  9. namespace app\api\service\wechat;
  10. use app\api\service\Service;
  11. use app\common\library\wechat\Factory;
  12. use app\common\service\wechat\WechatUser as WechatUserService;
  13. use mall\basic\Users;
  14. use mall\library\tools\jwt\Token;
  15. use think\facade\Config;
  16. use think\facade\Db;
  17. use app\common\models\Setting as SettingModel;
  18. use app\common\models\wechat\WechatUsers as WechatUsersModel;
  19. use app\common\service\Spread\Spread;
  20. use app\common\models\users\Users as UsersModel;
  21. class OAuth extends Service {
  22. /**
  23. * 登录
  24. * @param array $params
  25. * @return mixed
  26. * @throws \Exception
  27. */
  28. public static function login($params=[]){
  29. Db::startTrans();
  30. $result = [];
  31. switch ($params["source"]){
  32. case 1: // H5
  33. $result = self::mp($params);
  34. break;
  35. case 2: // mini
  36. break;
  37. default:
  38. Db::rollback();
  39. throw new \Exception("非法操作",0);
  40. }
  41. Db::commit();
  42. return $result;
  43. }
  44. /**
  45. * 公众号登录
  46. * @param $params
  47. * @return array
  48. * @throws \think\db\exception\DataNotFoundException
  49. * @throws \think\db\exception\DbException
  50. * @throws \think\db\exception\ModelNotFoundException
  51. */
  52. protected static function mp($params){
  53. $appid = SettingModel::getArrayData("wechat.appid");
  54. $state = $params["state"] ?? "";
  55. if($state != $appid){
  56. $domain = getDomain() . (in_array(Config::get("base.app_type"),["pc","page"]) ? "/wap" : "");
  57. return [ "url"=>$domain, "appid"=>$appid, "state"=>$appid ];
  58. }
  59. $wxToken = Factory::wechat()->oauth->getOauthAccessToken($params["code"]);
  60. if(!isset($wxToken['openid'])) {
  61. throw new \Exception("获取授权信息失败,openid为空",0);
  62. }
  63. $user = Factory::wechat()->oauth->getUserInfo($wxToken['access_token'],$wxToken['openid']);
  64. $condition = [];
  65. if(isset($user["unionid"])){
  66. $condition["unionid"] = $user["unionid"];
  67. }else{
  68. $condition["openid"] = $user['openid'];
  69. }
  70. // 如果用户不存在,注册新用户
  71. if(!$row=WechatUsersModel::where($condition)->find()){
  72. $user_id = WechatUserService::register($user);
  73. }
  74. if(!empty($row)) $user_id = $row["user_id"];
  75. $info = Users::info($user_id);
  76. $token = Token::get("id",$info["id"]);
  77. return [
  78. "id" => $info["id"],
  79. "token" => $token,
  80. "username" => $info["username"],
  81. "nickname" => $info["nickname"],
  82. "group_name" => $info["group_name"],
  83. "shop_count" => $info["shop_count"],
  84. "coupon_count" => $info["coupon_count"],
  85. "mobile" => $info["mobile"],
  86. "sex" => $info["sex"],
  87. "point" => $info["point"],
  88. "amount" => $info["amount"],
  89. "last_ip" => $info["last_ip"],
  90. "last_login" => $info["last_login"]
  91. ];
  92. }
  93. }