AuthForm.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\alliance\models\alipay;
  8. use app\models\User;
  9. use app\modules\alliance\models\AlipayThirdForm;
  10. use app\utils\Alipay\Alipay;
  11. use Overtrue\Socialite\SocialiteManager;
  12. use yii\base\BaseObject;
  13. use yii\base\Model;
  14. use yii\helpers\Json;
  15. use app\models\SaasUser;
  16. use app\modules\alliance\models\BindForm;
  17. use app\models\Share;
  18. use yii\log\Logger;
  19. use app\utils\Alipay\alipaySdk\aop\request\AlipaySystemOauthTokenRequest;
  20. use AopClient;
  21. use app\models\StoreAliMini;
  22. use app\models\Option;
  23. include_once \Yii::$app->basePath . "/utils/Alipay/alipaySdk/aop/AopClient.php";;
  24. class AuthForm extends \app\modules\alliance\models\ApiModel
  25. {
  26. public $store_id;
  27. public $authCode;
  28. public $isv_appid;
  29. public $response;
  30. public $mini_id;
  31. public $share_user_id;
  32. /**
  33. * @var SocialiteManager $socialite
  34. */
  35. public $socialite;
  36. public function rules()
  37. {
  38. return [
  39. [['store_id', 'share_user_id','mini_id'], 'integer'],
  40. [['authCode', 'response'], 'required'],
  41. [['authCode', 'response'], 'trim'],
  42. [['authCode', 'response','isv_appid'], 'string']
  43. ];
  44. }
  45. /**
  46. *
  47. */
  48. public function codeAnalysis()
  49. {
  50. $user = $this->socialite->create('alipay')->tokenFromCode($this->authCode);
  51. \Yii::warning($user);
  52. }
  53. /**
  54. * 用户登录注册
  55. * @return array|mixed|null
  56. */
  57. public function analysisPhone()
  58. {
  59. \Yii::getLogger()->log("支付宝配置",Logger::LEVEL_ERROR);
  60. if (!$this->validate()) {
  61. return [
  62. 'code' => 1,
  63. 'msg' => $this->getErrorSummary(false)[0]
  64. ];
  65. }
  66. $response = Json::decode($this->response);
  67. if (empty($response['response'])) {
  68. return [
  69. 'code' => 1,
  70. 'msg' => 'response 不能为空'
  71. ];
  72. }
  73. // 获取user_id
  74. $token = $this->socialite->create('alipay')->tokenFromCode($this->authCode);
  75. \Yii::getLogger()->log($token,Logger::LEVEL_ERROR);
  76. if (empty($token['user_id'])) {
  77. return [
  78. 'code' => 1,
  79. 'msg' => '获取user_id失败'
  80. ];
  81. }
  82. $alipay_config = self::getAlipayConfig();
  83. if (empty($alipay_config) && !is_isv()) {
  84. return [
  85. 'code' => 1,
  86. 'msg' => '支付宝配置信息不能为空'
  87. ];
  88. }
  89. $aes_key = $alipay_config['aes_key'];
  90. $result = openssl_decrypt(base64_decode($response['response']), 'AES-128-CBC', base64_decode($aes_key), OPENSSL_RAW_DATA);
  91. // 获取手机号解密
  92. if (!$result) {
  93. return [
  94. 'code' => 1,
  95. 'msg' => '解密失败'
  96. ];
  97. }
  98. $res = Json::decode($result);
  99. if ($res['code'] != '10000') {
  100. return $res;
  101. }
  102. $mobile = $res['mobile'];
  103. // 创建平台会员saas_user
  104. $saas_user = SaasUser::find()->where(['mobile' => $mobile, 'is_delete' => SaasUser::DELETE_STATUS_FALSE])->one();
  105. if (!$saas_user) {
  106. $saas_user = new SaasUser();
  107. $saas_user->price = '0.00';
  108. $saas_user->access_token = \Yii::$app->security->generateRandomString();
  109. $saas_user->avatar = \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/avatar.png';
  110. $saas_user->mobile = $mobile;
  111. $saas_user->name = substr_replace($mobile, '******', 3, 6);
  112. $saas_user->store_id = $this->store_id;
  113. }
  114. if (!empty($token['user_id'])) {
  115. $saas_user->ali_user_id = $token['user_id'];
  116. }
  117. $saas_user->save();
  118. $user = User::findOne(['store_id' => $this->store_id, 'binding' => $mobile]);
  119. if (!$user) {
  120. $user = new User();
  121. $user->type = User::USER_TYPE_NORMAL;
  122. $user->binding = $mobile;
  123. $user->avatar_url = \Yii::$app->request->hostInfo . '/web/v1/statics/images/avatar.png';
  124. $user->username = \Yii::$app->security->generateRandomString();
  125. $user->nickname = substr_replace($mobile, '******', 3, 6);
  126. $user->password = \Yii::$app->security->generatePasswordHash(\Yii::$app->security->generateRandomString(), 5);
  127. $user->auth_key = \Yii::$app->security->generateRandomString();
  128. $user->access_token = \Yii::$app->security->generateRandomString();
  129. $user->is_delete = User::USER_NOT_DELETE;
  130. $user->alipay_open_id = $token['user_id'];
  131. $user->store_id = $this->store_id;
  132. $user->platform = User::USER_FROM_ALIPAY; // 支付宝
  133. if (!$user->save()) {
  134. return [
  135. 'code' => 1,
  136. 'msg' => '登录失败',
  137. 'data' => $user->getErrorSummary(false)[0]
  138. ];
  139. }
  140. } else {
  141. if (empty($user->alipay_open_id)) {
  142. $user->alipay_open_id = $token['user_id'];
  143. $user->save();
  144. }
  145. }
  146. if ($user->blacklist == '1') {
  147. return [
  148. 'code' => 1,
  149. 'msg' => '您的账号已被限制登录!',
  150. ];
  151. }
  152. $share = $share_user = null;
  153. if ($this->share_user_id > 0) {
  154. // 绑定上下级
  155. $bindForm = new BindForm();
  156. $bindForm->store_id = $this->store_id;
  157. $bindForm->user_id = $user->id;
  158. $bindForm->parent_id = $this->share_user_id;
  159. $bindForm->condition = 0;
  160. $bindForm->save();
  161. }
  162. if ($user->parent_id > 0) {
  163. $share = Share::findOne(['user_id' => $user->parent_id]);
  164. $share_user = User::findOne(['id' => $share->user_id]);
  165. }
  166. $data = [
  167. 'nickname' => $saas_user->name,
  168. 'binding' => $saas_user->mobile,
  169. 'id' => $user->id,
  170. 'is_distributor' => $user->is_distributor ? $user->is_distributor : 0,
  171. 'avatar_url' => $saas_user->avatar,
  172. 'errCode' => 0,
  173. 'parent' => $share ? ($share->name ? $share->name : $share_user->nickname) : '总店',
  174. 'money' => $user->money ? $user->money : 0,
  175. 'blacklist' => $user->blacklist ? $user->blacklist : 0,
  176. 'access_token' => $saas_user->access_token,
  177. 'level' => $user->level ? $user->level : -1,
  178. 'is_clerk' => $user->is_clerk === null ? 0 : $user->is_clerk,
  179. 'integral' => $user->integral === null ? 0 : $user->integral,
  180. 'is_saas_clerk' => (int)$user->is_saas_clerk,
  181. ];
  182. return [
  183. 'code' => 0,
  184. 'msg' => '登录成功',
  185. 'data' => $data,
  186. ];
  187. }
  188. }