AuthLoginForm.php 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\client\models\v1;
  8. use app\constants\OptionSetting;
  9. use app\models\Option;
  10. use app\models\Share;
  11. use app\models\StoreMini;
  12. use app\models\User;
  13. use app\models\SaasUser;
  14. use app\models\UserAuditLog;
  15. use app\models\WechatConfig;
  16. use app\modules\client\controllers\BaseController;
  17. use app\modules\client\models\ApiModel;
  18. use app\modules\client\models\v1\LoginForm;
  19. use app\utils\Wechat\WechatMini;
  20. use EasyWeChat\Factory;
  21. use Exception;
  22. use yii\base\BaseObject;
  23. use app\utils\Douyin\Douyin;
  24. class AuthLoginForm extends ApiModel
  25. {
  26. // 商城id
  27. public $store_id;
  28. //绑定店铺来源 TODO 好像没用到
  29. public $store_id_first;
  30. // 供应链系统,将要切换的store_id
  31. public $switch_store_id;
  32. public $password; // 密码
  33. public $re_password; // 密码
  34. // 微信小程序
  35. public $code;
  36. public $encryptedData;
  37. public $iv;
  38. public $user_id;
  39. public $app_user_code;
  40. public $nickname;
  41. public $avatar_url;
  42. public $mobile;
  43. // 手机注册
  44. public $phone;
  45. public $verify_code;
  46. public $path;
  47. // 分销商user_id
  48. public $share_user_id;
  49. // 微信小程序
  50. const PLATFORM_WX = 'wx';
  51. // 微信公众号
  52. const PLATFORM_MP = 'mp';
  53. // app端微信
  54. const PLATFORM_APP = 'app';
  55. // 手机号注册登录
  56. const PLATFORM_PHONE = 'phone';
  57. public $_platform;
  58. public $platform; // wechat(微信), alipay(支付宝), douyin(抖音)
  59. /**
  60. * @var \EasyWeChat\MiniProgram\Application
  61. */
  62. public $wechat;
  63. public $form_value = 'h5';
  64. public function rules()
  65. {
  66. return [
  67. ['_platform', 'in', 'range' => [self::PLATFORM_WX, self::PLATFORM_MP, self::PLATFORM_APP, self::PLATFORM_PHONE]],
  68. ['_platform', 'required'],
  69. [['share_user_id', 'store_id', 'user_id', 'store_id_first', 'switch_store_id'], 'integer'],
  70. [['code', 'encryptedData', 'iv', 'platform', 'path', 'password', 're_password', 'form_value'], 'string'],
  71. [['code', 'encryptedData', 'iv', 'password', 're_password'], 'trim'],
  72. [['nickname', 'avatar_url', 'mobile', 'app_user_code', 'phone', 'verify_code'], 'safe'],
  73. ];
  74. }
  75. public function editPassword()
  76. {
  77. $user = get_user();
  78. if (!$user) {
  79. return [
  80. 'code' => 1,
  81. 'msg' => '用户不存在',
  82. ];
  83. }
  84. if (isset($this->re_password)) {
  85. if (empty($this->password) || empty($this->re_password)) {
  86. return [
  87. 'code' => 1,
  88. 'msg' => '密码不能为空',
  89. ];
  90. }
  91. if ($this->password != $this->re_password) {
  92. return [
  93. 'code' => 1,
  94. 'msg' => '两次密码输入不一致',
  95. ];
  96. }
  97. }
  98. $user->password = \Yii::$app->security->generatePasswordHash($this->password);
  99. if ($user->save()) {
  100. return [
  101. 'code' => 0,
  102. 'msg' => '修改成功',
  103. ];
  104. } else {
  105. return [
  106. 'code' => 1,
  107. 'msg' => '修改失败',
  108. ];
  109. }
  110. }
  111. //忘记密码
  112. public function forgetPassword() {
  113. try {
  114. $phone = $this->phone;
  115. $verify_code = $this->verify_code;
  116. $password = trim($this->password);
  117. $re_password = trim($this->re_password);
  118. $loginForm = new LoginForm([
  119. 'store_id' => $this->store_id,
  120. 'phone' => $phone,
  121. 'verify_code' => $verify_code,
  122. ]);
  123. $user = User::findOne(['binding' => $phone, 'is_delete' => User::USER_NOT_DELETE, 'store_id' => $this->store_id]);
  124. if (!$user) {
  125. throw new \Exception('用户信息不存在');
  126. }
  127. $result = $loginForm->verifySmsCode(LoginForm::CACHE_KEY_SMS_FORGET_PASSWORD);
  128. if ($result['code']) {
  129. return $result;
  130. }
  131. if (!empty($password) || !empty($re_password)) {
  132. if (empty($password) || empty($re_password)) {
  133. throw new \Exception('密码不能为空');
  134. }
  135. if ($password != $re_password) {
  136. throw new \Exception('两次密码输入不一致');
  137. }
  138. $user->password = \Yii::$app->security->generatePasswordHash($password);
  139. if (!$user->save()) {
  140. throw new \Exception(json_encode($user->errors, JSON_UNESCAPED_UNICODE));
  141. }
  142. return [
  143. 'code' => 0,
  144. 'msg' => '修改密码成功'
  145. ];
  146. }
  147. return [
  148. 'code' => 0,
  149. 'msg' => '验证通过'
  150. ];
  151. } catch (\Exception $e) {
  152. return [
  153. 'code' => 1,
  154. 'msg' => $e->getMessage()
  155. ];
  156. }
  157. }
  158. //用户密码注册
  159. public function userPasswordRegedit() {
  160. $t = \Yii::$app->db->beginTransaction();
  161. try {
  162. $phone = $this->phone;
  163. $verify_code = $this->verify_code;
  164. $password = trim($this->password);
  165. $re_password = trim($this->re_password);
  166. $store_id = $this->store_id;
  167. // //是否开启密码登录注册接口
  168. // $is_password_login = Option::get(OptionSetting::IS_PASSWORD_LOGIN, $store_id, '', 0)['value'];
  169. // if ($is_password_login) {
  170. // throw new \Exception('商城未开启密码注册方式');
  171. // }
  172. $user = User::findOne(['binding' => $phone, 'is_delete' => User::USER_NOT_DELETE, 'store_id' => $store_id]);
  173. if ($user) {
  174. throw new \Exception('账户已存在');
  175. }
  176. //商城是否开启密码登录验证手机号+验证码
  177. $is_password_login_phone_verify = Option::get(OptionSetting::IS_PASSWORD_LOGIN_PHONE_VERIFY, $store_id, 'display', 0)['value'];
  178. $is_password_login_phone_verify = Option::get(OptionSetting::IS_PASSWORD_LOGIN_PHONE_VERIFY, $store_id, 'reg', $is_password_login_phone_verify)['value'];
  179. if (intval($is_password_login_phone_verify)) {
  180. $loginForm = new LoginForm([
  181. 'store_id' => $store_id,
  182. 'phone' => $phone,
  183. 'verify_code' => $verify_code,
  184. ]);
  185. $result = $loginForm->verifySmsCode(LoginForm::CACHE_KEY_SMS_LOGIN);
  186. if ($result['code']) {
  187. $t->rollBack();
  188. return $result;
  189. }
  190. }
  191. if (empty($password) || empty($re_password)) {
  192. throw new \Exception('密码不能为空');
  193. }
  194. if ($password != $re_password) {
  195. throw new \Exception('两次密码输入不一致');
  196. }
  197. $saas_user = SaasUser::findOne(['mobile' => $phone, 'is_delete' => 0]);
  198. if (!$saas_user) {
  199. $saas_user = new SaasUser();
  200. $saas_user->access_token = \Yii::$app->security->generateRandomString();
  201. $saas_user->name = substr_replace($phone, '******', 3, 6);
  202. $saas_user->mobile = $phone ?? '';
  203. $saas_user->avatar = \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/avatar.png';
  204. $saas_user->store_id = $store_id;
  205. } else {
  206. if (empty($saas_user->name)) {
  207. $saas_user->name = substr_replace($phone, '****', 3, 4);
  208. }
  209. if (empty($saas_user->avatar)) {
  210. $saas_user->avatar = \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/avatar.png';
  211. }
  212. if (empty($saas_user->access_token)) {
  213. $saas_user->access_token = \Yii::$app->security->generateRandomString();
  214. }
  215. }
  216. if (!$saas_user->save()) {
  217. throw new \Exception(json_encode($saas_user->errors, JSON_UNESCAPED_UNICODE));
  218. }
  219. $isNew = 0;
  220. $user = User::findOne(['binding' => $phone, 'store_id' => $store_id, 'is_delete' => 0]);
  221. if (!$user) {
  222. $isNew = 1;
  223. $user = new User();
  224. $user->type = User::USER_TYPE_NORMAL;
  225. $user->binding = $phone ?? '';
  226. $user->nickname = $saas_user->name;
  227. $user->avatar_url = $saas_user->avatar;
  228. $user->username = \Yii::$app->security->generateRandomString();
  229. $user->password = \Yii::$app->security->generatePasswordHash($password);
  230. $user->auth_key = \Yii::$app->security->generateRandomString();
  231. $user->access_token = \Yii::$app->security->generateRandomString();
  232. $user->is_delete = User::USER_NOT_DELETE;
  233. $user->store_id = $store_id;
  234. $user->parent_id = 0;
  235. $user->old_parent_id = 0;
  236. if (!$user->save()) {
  237. throw new \Exception(json_encode($user->errors, JSON_UNESCAPED_UNICODE));
  238. }
  239. }
  240. if ($this->share_user_id && $this->share_user_id > 0) {
  241. try {
  242. $bind = new BindForm();
  243. $bind->user_id = $user->id;
  244. $bind->store_id = $store_id;
  245. $bind->parent_id = $this->share_user_id;
  246. $bind->condition = 0;
  247. $bind->isNew = $isNew;
  248. $bind->save();
  249. } catch (\Throwable $e){}
  250. }
  251. $t->commit();
  252. return [
  253. 'code' => 0,
  254. 'msg' => '创建账户成功'
  255. ];
  256. } catch (\Exception $e) {
  257. $t->rollBack();
  258. return [
  259. 'code' => 1,
  260. 'msg' => $e->getMessage()
  261. ];
  262. }
  263. }
  264. // public function phoneAuth() {
  265. // try {
  266. // $this->wechat = self::getWechat();
  267. // $session = $this->wechat->auth->session($this->code);
  268. // $decryptedData = $this->wechat->encryptor->decryptData($session['session_key'], $this->iv, $this->encryptedData);
  269. // $user = User::findOne(['binding' => $decryptedData['phoneNumber'], 'store_id' => get_store_id()]);
  270. // // 是否开启强制绑定手机号
  271. // $is_open_bind = Option::get(OptionSetting::STORE_LOGIN_FORCIBLY_BIND_MOBILE, $this->store_id, 'store')['value'];
  272. // $share = $share_user = null;
  273. // if ($this->share_user_id > 0) {
  274. // // 绑定上下级
  275. // $bindForm = new BindForm();
  276. // $bindForm->store_id = $this->store_id;
  277. // $bindForm->user_id = $user->id;
  278. // $bindForm->parent_id = $this->share_user_id;
  279. // $bindForm->condition = 0;
  280. // $bindForm->save();
  281. // }
  282. // if ($user) {
  283. // if ($user->parent_id > 0) {
  284. // $share = Share::findOne(['user_id' => $user->parent_id]);
  285. // $share_user = User::findOne(['id' => $share->user_id]);
  286. // }
  287. // $data = [
  288. // 'access_token' => $user->access_token,
  289. // 'nickname' => $user->nickname,
  290. // 'avatar_url' => $user->avatar_url,
  291. // 'is_distributor' => $user->is_distributor ? $user->is_distributor : 0,
  292. // 'errCode' => 0,
  293. // 'parent' => $share ? ($share->name ? $share->name : $share_user->nickname) : '总店',
  294. // 'id' => $user->id,
  295. // 'is_clerk' => $user->is_clerk === null ? 0 : $user->is_clerk,
  296. // 'integral' => $user->integral === null ? 0 : $user->integral,
  297. // 'money' => $user->money === null ? 0 : $user->money,
  298. // 'binding' => $user->binding,
  299. // 'level' => $user->level,
  300. // 'blacklist' => $user->blacklist,
  301. // 'is_open_bind' => $is_open_bind ? $is_open_bind : 0
  302. // ];
  303. // return [
  304. // 'code' => 0,
  305. // 'data' => $data,
  306. // 'msg' => '登录成功'
  307. // ];
  308. // } else {
  309. // return [
  310. // 'code' => 1,
  311. // 'data' => ['info' => $decryptedData['phoneNumber']],
  312. // 'msg' => ''
  313. // ];
  314. // }
  315. // } catch (\Exception $e) {
  316. // throw new \Exception($e->getMessage(), 1);
  317. // }
  318. // }
  319. public function getUserInfoByMobile()
  320. {
  321. $data = [
  322. 'avatar_url' => '',
  323. 'nickname' => '',
  324. 'mobile' => $this->mobile,
  325. 'id' => 0,
  326. ];
  327. $user = User::findOne(['binding' => $this->mobile, 'is_delete' => 0, 'store_id' => $this->store_id]);
  328. if ($user) {
  329. $data['id'] = $user->id;
  330. $saasUser = SaasUser::find()->where(['mobile' => $user->binding])->one();
  331. if ($saasUser) {
  332. $data['avatar_url'] = $saasUser->avatar;
  333. $data['nickname'] = $saasUser->name;
  334. $data['mobile'] = $saasUser->mobile;
  335. }
  336. }
  337. return [
  338. 'code' => 0,
  339. 'data' => $data,
  340. ];
  341. }
  342. public function getUserInfoById()
  343. {
  344. $data = [
  345. 'avatar_url' => '',
  346. 'nickname' => '',
  347. 'mobile' => '',
  348. 'id' => $this->user_id,
  349. ];
  350. $user = User::findOne(['id' => $this->user_id, 'is_delete' => 0, 'store_id' => $this->store_id]);
  351. if ($user) {
  352. $saasUser = SaasUser::find()->where(['mobile' => $user->binding])->one();
  353. if ($saasUser) {
  354. $data['avatar_url'] = $saasUser->avatar;
  355. $data['nickname'] = $saasUser->name;
  356. $data['mobile'] = $saasUser->mobile;
  357. }
  358. }
  359. return [
  360. 'code' => 0,
  361. 'data' => $data,
  362. ];
  363. }
  364. public function getOptionByH5()
  365. {
  366. $data = [
  367. 'is_exist_user' => 0,
  368. 'parent_avatar_url' => '',
  369. 'parent_nickname' => '',
  370. 'parent_mobile' => '',
  371. 'parent_id' => 0,
  372. ];
  373. // 获取是否强制绑定手机号配置
  374. $enforce_bind_parent = 0;
  375. $shareSetting = Option::get(OptionSetting::SHARE_BASIC_SETTING, $this->store_id, OptionSetting::SHARE_GROUP_NAME, null);
  376. if ($shareSetting = \json_decode($shareSetting['value'], true)) {
  377. $enforce_bind_parent = (int)$shareSetting['enforce_bind_parent']['value'];
  378. }
  379. $data['enforce_bind_parent'] = $enforce_bind_parent;
  380. $user = User::findOne(['binding' => $this->mobile, 'is_delete' => 0, 'store_id' => $this->store_id]);
  381. if ($user) {
  382. $data['is_exist_user'] = 1;
  383. }
  384. if ($this->share_user_id) {
  385. $parent = User::findOne(['id' => $this->share_user_id, 'is_delete' => 0]);
  386. if ($parent) {
  387. $parentSaasUser = SaasUser::find()->where(['mobile' => $parent->binding])->one();
  388. if ($parentSaasUser) {
  389. $data['parent_avatar_url'] = $parentSaasUser->avatar;
  390. $data['parent_nickname'] = $parentSaasUser->name;
  391. $data['parent_mobile'] = $parentSaasUser->mobile;
  392. $data['parent_id'] = $parent->id;
  393. }
  394. }
  395. }
  396. return [
  397. 'code' => 0,
  398. 'data' => $data,
  399. ];
  400. }
  401. public function code() {
  402. $login_use_mobile = Option::get(OptionSetting::STORE_LOGIN_USE_MOBILE, get_store_id(), 'store', 1)['value'];
  403. $login_use_mobile = Option::get(OptionSetting::STORE_LOGIN_USE_MOBILE, get_store_id(), 'reg', $login_use_mobile);
  404. $default_user_info_arr = OptionSetting::default_user_info_arr();
  405. $data = [
  406. 'access_token' => '',
  407. 'avatar_url' => $default_user_info_arr['default_user_pic'] ?? '',
  408. 'nickname' => $default_user_info_arr['default_user_nickname'] ?? '',
  409. 'login_use_mobile' => (int)$login_use_mobile['value'],
  410. 'is_exist_user' => 0,
  411. ];
  412. if (!intval($default_user_info_arr['isOpenDefault'])) {
  413. $data['avatar_url'] = '';
  414. $data['nickname'] = '';
  415. }
  416. try {
  417. if(is_isv()){
  418. $config = [
  419. 'app_id' => Option::get("platform_third_appid",0,'saas')['value'],
  420. 'secret' => Option::get("platform_third_secret",0,'saas')['value'],
  421. 'token' => Option::get("platform_token",0,'saas')['value'],
  422. 'aes_key' => Option::get("platform_encodingAesKey",0,'saas')['value']
  423. ];
  424. $openPlatform = Factory::openPlatform($config);
  425. if(!empty(get_mini_id())){
  426. $mini = StoreMini::findOne(get_mini_id());
  427. $app = $openPlatform->miniProgram($mini->appid,$mini->authorizer_refresh_token);
  428. $session = $app->auth->session($this->code);
  429. }else{
  430. throw new \Exception('code异常1');
  431. }
  432. }elseif ($this->platform == 'bytedance') {
  433. /**
  434. * @var \ByteDance\MiniProgram\Application $byteDance
  435. */
  436. $byteDance = \Yii::$app->controller->byteDance;
  437. if (!$byteDance) {
  438. return [
  439. 'code' => 1,
  440. 'msg' => '登录异常'
  441. ];
  442. }
  443. // $this->code = '121231231231';
  444. $session = $byteDance->auth->session($this->code);
  445. \Yii::warning($session);
  446. } else {
  447. if (is_h5()) {
  448. $app = WechatMini::getWechatConfig(get_store_id(), 0, WechatMini::TYPE_OFFICIAL);
  449. $session = $app->oauth->userFromCode($this->code)->getTokenResponse();
  450. } else {
  451. $this->wechat = self::getWechat();
  452. if (!$this->wechat) {
  453. throw new \Exception('code异常2');
  454. }
  455. $session = $this->wechat->auth->session($this->code);
  456. }
  457. }
  458. //获取session
  459. // $session = self::getWechat()->auth->session($this->code);
  460. if (!$session || empty($session['openid'])) {
  461. throw new \Exception('获取openid失败.');
  462. }
  463. $openid = $session['openid'];
  464. $store_user = User::find()->where(['or', ['wechat_open_id' => $openid], ['wechat_union_id' => $openid], ['bytedance_open_id' => $openid], ['wechat_platform_open_id' => $openid]])->andWhere(['store_id' => $this->store_id])->orderBy('id DESC')->one();
  465. if ($store_user) {
  466. $saas_user = SaasUser::find()->where(['mobile' => $store_user->binding])->one();
  467. // if ($saas_user) {
  468. // $data['avatar_url'] = $saas_user->avatar;
  469. // $data['nickname'] = $saas_user->name;
  470. // $data['id'] = $store_user->id;
  471. // $data['access_token'] = $saas_user['access_token'];
  472. // } else {
  473. $data['avatar_url'] = $store_user->avatar_url;
  474. $data['nickname'] = $store_user->nickname;
  475. $data['id'] = $store_user->id;
  476. // }
  477. $data['is_exist_user'] = 1;
  478. } else {
  479. // $user = User::find()->where(['or', ['wechat_open_id' => $openid], ['wechat_union_id' => $openid]])->orderBy('id DESC')->one();
  480. // if ($user) {
  481. // $data['avatar_url'] = $user->avatar_url;
  482. // $data['nickname'] = $user->nickname;
  483. // $data['id'] = $user->id;
  484. // }else{
  485. $saasuser = SaasUser::find()->where(['or', ['platform_open_id' => $openid], ['platform_open_id_merchant' => $openid], ['bytedance_open_id' => $openid]])->orderBy('id DESC')->one();
  486. if ($saasuser) {
  487. $data['id'] = $saasuser->id;
  488. $data['access_token'] = $saasuser->access_token;
  489. }
  490. // }
  491. }
  492. // 获取是否强制绑定手机号配置
  493. $enforce_bind_parent = 0;
  494. $shareSetting = Option::get(OptionSetting::SHARE_BASIC_SETTING, $this->store_id, OptionSetting::SHARE_GROUP_NAME, null);
  495. if ($shareSetting = \json_decode($shareSetting['value'], true)) {
  496. $enforce_bind_parent = (int)$shareSetting['enforce_bind_parent']['value'];
  497. }
  498. $data['enforce_bind_parent'] = $enforce_bind_parent;
  499. $data['openid'] = $openid;
  500. if($this->app_user_code){
  501. $user_id = cache()->get($this->app_user_code);
  502. cache()->delete($this->app_user_code);
  503. if(empty($user_id)){
  504. return [
  505. 'code' => 0,
  506. 'msg' => 'error',
  507. 'data' => $data,
  508. ];
  509. }
  510. $user = User::findOne($user_id);
  511. $user->wechat_open_id = $session['openid'];
  512. $user->save();
  513. $saas_user = SaasUser::findOne(['mobile' => $user->binding]);
  514. $saas_user->platform_open_id = $session['openid'];
  515. $saas_user->save();
  516. $data['access_token'] = $saas_user['access_token'];
  517. }
  518. return [
  519. 'code' => 0,
  520. 'data' => $data,
  521. 'msg' => 'ok'
  522. ];
  523. } catch (\Exception $e) {
  524. \Yii::error([__METHOD__, $e]);
  525. return [
  526. 'code' => 0,
  527. 'msg' => 'error1' . $e->getMessage(),
  528. 'data' => $data,
  529. ];
  530. }
  531. }
  532. public function phoneAuth()
  533. {
  534. try {
  535. $decryptedData = [];
  536. if($this->phone && $this->verify_code){
  537. // 验证码验证
  538. $loginForm = new LoginForm([
  539. 'store_id' => $this->store_id,
  540. 'phone' => $this->phone,
  541. 'verify_code' => $this->verify_code,
  542. ]);
  543. $result = $loginForm->verifySmsCode(LoginForm::CACHE_KEY_SMS_LOGIN);
  544. $result2 = $loginForm->verifySmsCode(LoginForm::CACHE_KEY_BIND_PHONE);
  545. if ($result['code'] != 0 && $result2['code'] != 0) {
  546. return $result;
  547. }
  548. $decryptedData['phoneNumber'] = $this->phone;
  549. }
  550. if(is_isv()){
  551. if ($this->platform == 'bytedance') {
  552. $session = Douyin::code2session(get_mini_id(), $this->code);
  553. if($session['code'] != 0){
  554. return $session;
  555. }else{
  556. $session = $session['data']['data'];
  557. }
  558. }else{
  559. $config = [
  560. 'app_id' => Option::get("platform_third_appid",0,'saas')['value'],
  561. 'secret' => Option::get("platform_third_secret",0,'saas')['value'],
  562. 'token' => Option::get("platform_token",0,'saas')['value'],
  563. 'aes_key' => Option::get("platform_encodingAesKey",0,'saas')['value']
  564. ];
  565. $openPlatform = Factory::openPlatform($config);
  566. if(!empty(get_mini_id())){
  567. $mini = StoreMini::findOne(get_mini_id());
  568. $app = $openPlatform->miniProgram($mini->appid,$mini->authorizer_refresh_token);
  569. $session = $app->auth->session($this->code);
  570. \Yii::error("今天是个好日子");
  571. \Yii::error($session);
  572. if (!empty($this->iv) && !empty($this->encryptedData)) {
  573. $decryptedData = $app->encryptor->decryptData($session['session_key'], $this->iv, $this->encryptedData);
  574. \Yii::error($decryptedData);
  575. }
  576. }else{
  577. return [
  578. 'code' => 1,
  579. 'msg' => '登录异常'
  580. ];
  581. }
  582. }
  583. }elseif ($this->platform == 'bytedance') {
  584. /**
  585. * @var \ByteDance\MiniProgram\Application $byteDance
  586. */
  587. $byteDance = \Yii::$app->controller->byteDance;
  588. if (!$byteDance) {
  589. return [
  590. 'code' => 1,
  591. 'msg' => '登录异常'
  592. ];
  593. }
  594. // $this->code = '121231231231';
  595. $session = $byteDance->auth->session($this->code);
  596. \Yii::warning($session);
  597. if (!empty($this->iv) && !empty($this->encryptedData)) {
  598. $decryptedData = $byteDance->encryptor->decryptData($session['session_key'], $this->iv, $this->encryptedData);
  599. \Yii::error($decryptedData);
  600. }
  601. } else {
  602. $this->wechat = self::getWechat();
  603. if (!$this->wechat) {
  604. return [
  605. 'code' => 1,
  606. 'msg' => '登录异常'
  607. ];
  608. }
  609. $session = $this->wechat->auth->session($this->code);
  610. \Yii::warning($session);
  611. if (!empty($this->iv) && !empty($this->encryptedData)) {
  612. $decryptedData = $this->wechat->encryptor->decryptData($session['session_key'], $this->iv, $this->encryptedData);
  613. \Yii::error($decryptedData);
  614. }
  615. }
  616. $saas_user = null;
  617. $login_use_mobile = Option::get(OptionSetting::STORE_LOGIN_USE_MOBILE, get_store_id(), 'store', 1)['value'];
  618. $login_use_mobile = Option::get(OptionSetting::STORE_LOGIN_USE_MOBILE, get_store_id(), 'reg', $login_use_mobile);
  619. if (intval($login_use_mobile['value']) === 1 && empty($decryptedData['phoneNumber'])) {
  620. return [
  621. 'code' => 1,
  622. 'msg' => '手机号获取失败'
  623. ];
  624. }
  625. if (intval($login_use_mobile['value']) === 0 && empty($decryptedData['phoneNumber'])) {
  626. $decryptedData['phoneNumber'] = $this->generateRandomString(10) . 'X';
  627. }
  628. if (isset($decryptedData['phoneNumber']) && !empty($decryptedData['phoneNumber'])) {
  629. // 创建平台会员saas_user
  630. $saas_user = SaasUser::findOne(['mobile' => $decryptedData['phoneNumber'], 'is_delete' => SaasUser::DELETE_STATUS_FALSE]);
  631. }
  632. if (!$saas_user && $session['openid']) {
  633. // 针对同一个微信号使用不同的手机号问题,直接更新最新的手机号
  634. $w = [
  635. 'and',
  636. ['or', ['platform_open_id' => $session['openid']], ['bytedance_open_id' => $session['openid']]],
  637. ['is_delete' => SaasUser::DELETE_STATUS_FALSE],
  638. ];
  639. $saas_user = SaasUser::find()->where($w)->limit(1)->one();
  640. }
  641. if (!$saas_user) {
  642. // 针对同一个微信号使用不同的手机号问题,直接更新最新的手机号
  643. // if ($session['openid']) {
  644. // $saas_user = SaasUser::find()->where(['platform_open_id' => $session['openid'], 'is_delete' => SaasUser::DELETE_STATUS_FALSE])->one();
  645. // }
  646. $saas_user = new SaasUser();
  647. $saas_user->access_token = \Yii::$app->security->generateRandomString();
  648. $saas_user->name = substr_replace($decryptedData['phoneNumber'], '******', 3, 6);
  649. $saas_user->mobile = $decryptedData['phoneNumber'] ?? '';
  650. // TODO: 独立小程序登录是不是无法获得平台小程序的openid? 暂时注释
  651. if ($this->platform == 'bytedance') {
  652. $saas_user->bytedance_open_id = $session['openid'];
  653. } else {
  654. $saas_user->platform_open_id = $session['openid'];
  655. }
  656. $saas_user->avatar = \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/avatar.png';
  657. $saas_user->store_id = $this->store_id;
  658. if($this->store_id_first > 0){
  659. $saas_user->store_id = $this->store_id_first;
  660. }
  661. if ($session['openid']) {
  662. $saas_user->platform_open_id = $session['openid'];
  663. }
  664. $saas_user->save();
  665. } else {
  666. if (empty($saas_user->name)) {
  667. $saas_user->name = substr_replace($decryptedData['phoneNumber'], '****', 3, 4);
  668. }
  669. if (empty($saas_user->avatar)) {
  670. $saas_user->avatar = \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/avatar.png';
  671. }
  672. if (empty($saas_user->access_token)) {
  673. $saas_user->access_token = \Yii::$app->security->generateRandomString();
  674. }
  675. if ($decryptedData['phoneNumber']) {
  676. $saas_user->mobile = $decryptedData['phoneNumber'];
  677. }
  678. if (is_platform()) {
  679. if ($this->platform == 'bytedance') {
  680. $saas_user->bytedance_open_id = $session['openid'];
  681. } else {
  682. $saas_user->platform_open_id = $session['openid'];
  683. }
  684. }
  685. if ($session['openid']) {
  686. if ($this->platform == 'bytedance') {
  687. $saas_user->bytedance_open_id = $session['openid'];
  688. }
  689. }
  690. $saas_user->save();
  691. }
  692. if($this->nickname){
  693. $saas_user->name = $this->nickname;
  694. }
  695. if($this->avatar_url){
  696. $saas_user->avatar = $this->avatar_url;
  697. }
  698. $saas_user->save();
  699. // 平台登录 todo: 后续补充其他数据
  700. if ($this->store_id == 0) {
  701. return [
  702. 'code' => 0,
  703. 'msg' => '登录成功',
  704. 'data' => [
  705. 'access_token' => $saas_user->access_token,
  706. 'session_key' => $session['session_key'],
  707. 'nickname' => $saas_user->name,
  708. 'avatar_url' => $saas_user->avatar,
  709. 'id' => $saas_user->id,
  710. 'money' => $saas_user->share_profit,
  711. 'integral' => $saas_user->integral,
  712. 'mobile' => $saas_user->mobile,
  713. 'binding' => $saas_user->mobile,
  714. ]
  715. ];
  716. }
  717. // else {
  718. // \Yii::error(['登录失败', $decryptedData, $session]);
  719. // return [
  720. // 'code' => 1,
  721. // 'msg' => '登录失败',
  722. // ];
  723. // }
  724. $user = User::findOne(['binding' => $decryptedData['phoneNumber'], 'store_id' => $this->store_id, 'is_delete' => 0]);
  725. if (!$user) {
  726. $user = User::findOne(['wechat_open_id' => $session['openid'], 'store_id' => $this->store_id, 'is_delete' => 0]);
  727. }
  728. $share = $share_user = null;
  729. $isNew = 0;
  730. if ($user) {
  731. if ($user->blacklist == '1') {
  732. return [
  733. 'code' => 1,
  734. 'msg' => '您的账号已被限制登录!',
  735. ];
  736. }
  737. $data = [
  738. 'access_token' => $saas_user->access_token,
  739. 'nickname' => $saas_user->name,
  740. 'avatar_url' => $saas_user->avatar,
  741. 'is_distributor' => $user->is_distributor ? $user->is_distributor : 0,
  742. 'errCode' => 0,
  743. 'id' => $user->id,
  744. 'is_clerk' => $user->is_clerk === null ? 0 : $user->is_clerk,
  745. 'integral' => $user->integral === null ? 0 : $user->integral,
  746. 'money' => $user->money === null ? 0 : $user->money,
  747. 'binding' => $user->binding,
  748. 'level' => $user->level,
  749. 'blacklist' => $user->blacklist,
  750. 'is_saas_clerk' => (int)$user->is_saas_clerk,
  751. 'store_id' => $saas_user->store_id,
  752. ];
  753. if ($this->platform == 'bytedance') {
  754. $user->bytedance_open_id = $session['openid'];
  755. } else {
  756. $user->wechat_open_id = $session['openid'];
  757. if($session['unionid']){
  758. $user->wechat_union_id = $session['unionid'];
  759. }
  760. }
  761. if($this->nickname){
  762. $user->nickname = $this->nickname;
  763. }
  764. if($this->avatar_url){
  765. $user->avatar_url = $this->avatar_url;
  766. }
  767. $user->save();
  768. } else {
  769. $isNew = 1;
  770. $data = [
  771. 'nickName' => substr_replace($decryptedData['phoneNumber'], '******', 3, 6),
  772. 'avatarUrl' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/avatar.png',
  773. ];
  774. $user = new User();
  775. $user->type = User::USER_TYPE_NORMAL;
  776. $user->binding = $decryptedData['phoneNumber'] ?? '';
  777. $user->nickname = $this->nickname ? $this->nickname : $data['nickName'];
  778. $user->avatar_url = $this->avatar_url ? $this->avatar_url : $data['avatarUrl'];
  779. $user->username = \Yii::$app->security->generateRandomString();
  780. $user->password = \Yii::$app->security->generatePasswordHash(\Yii::$app->security->generateRandomString(), 5);
  781. $user->auth_key = \Yii::$app->security->generateRandomString();
  782. $user->access_token = \Yii::$app->security->generateRandomString();
  783. $user->is_delete = User::USER_NOT_DELETE;
  784. $user->store_id = $this->store_id;
  785. $user->parent_id = 0;
  786. $user->old_parent_id = 0;
  787. if ($this->platform == 'bytedance') {
  788. $user->bytedance_open_id = $session['openid'];
  789. $user->platform = User::USER_FROM_BYTEDANCE; // 字节
  790. } else {
  791. $user->platform = User::USER_FROM_WECHAT; // 微信
  792. $user->wechat_open_id = $session['openid'];
  793. if($session['unionid']){
  794. $user->wechat_union_id = $session['unionid'];
  795. }
  796. }
  797. if (!$user->save()) {
  798. \Yii::error(['登录失败', $user]);
  799. return [
  800. 'code' => 1,
  801. 'msg' => '登陆失败',
  802. 'data' => $user->getErrorSummary(false)[0]
  803. ];
  804. }
  805. $data = [
  806. 'access_token' => $saas_user->access_token,
  807. 'nickname' => $saas_user->name,
  808. 'gender' => $saas_user->gender,
  809. 'avatar_url' => $saas_user->avatar,
  810. 'is_distributor' => $user->is_distributor ? $user->is_distributor : 0,
  811. 'errCode' => 0,
  812. 'id' => $user->id,
  813. 'is_clerk' => $user->is_clerk === null ? 0 : $user->is_clerk,
  814. 'integral' => $user->integral === null ? 0 : $user->integral,
  815. 'money' => $user->money === null ? 0 : $user->money,
  816. 'binding' => $user->binding,
  817. 'level' => $user->level,
  818. 'blacklist' => $user->blacklist,
  819. 'is_saas_clerk' => 0,
  820. 'store_id' => $saas_user->store_id,
  821. ];
  822. }
  823. $userAuditLog = UserAuditLog::findOne(['user_id' => $user->id, 'is_delete' => 0]);
  824. if (!$userAuditLog) {
  825. $userAuditLog = UserAuditLog::findOne(['wechat_open_id' => $user->wechat_open_id, 'is_delete' => 0]);
  826. if (!empty($userAuditLog)) {
  827. $userAuditLog->user_id = $user->id;
  828. $userAuditLog->save();
  829. }
  830. }
  831. if (isset($data['nickname'])) {
  832. $data['is_can_wechat'] = 0;
  833. if (strpos($data['nickname'], '****') !== false) {
  834. $data['is_can_wechat'] = 1;
  835. }
  836. }
  837. if ($this->share_user_id && $this->share_user_id > 0) {
  838. try {
  839. $bind = new BindForm();
  840. $bind->user_id = $data['id'];
  841. $bind->store_id = $this->store_id;
  842. $bind->parent_id = $this->share_user_id;
  843. $bind->condition = 0;
  844. $bind->isNew = $isNew;
  845. $bind->save();
  846. } catch (\Throwable $e){}
  847. }
  848. // 处理 供应链openid问题
  849. if (\Yii::$app->prod_is_dandianpu()) {
  850. if (self_mini()) {
  851. // 独立小程序openid保存在user表
  852. $user->wechat_open_id = $session['openid'];
  853. if($session['unionid']){
  854. $user->wechat_union_id = $session['unionid'];
  855. }
  856. $user->save();
  857. } else {
  858. // 平台小程序openid保存在saas_user表
  859. $saas_user->platform_open_id = $session['openid'];
  860. $saas_user->save();
  861. }
  862. }
  863. $data['session_key'] = $session['session_key'];
  864. return [
  865. 'code' => 0,
  866. 'data' => $data,
  867. 'msg' => '登录成功'
  868. ];
  869. } catch (\Throwable $e) {
  870. \Yii::error(['登录失败', $e]);
  871. \Yii::error($e->getMessage() . ' file => ' . $e->getFile() . ' line => ' . $e->getLine());
  872. return [
  873. 'code' => 1,
  874. 'msg' => '登录失败',
  875. ];
  876. }
  877. }
  878. public function generateRandomString($length = 10) {
  879. $characters = '1234567890';
  880. $charactersLength = strlen($characters);
  881. $randomString = '';
  882. for ($i = 0; $i < $length; $i++) {
  883. $randomString .= $characters[rand(0, $charactersLength - 1)];
  884. }
  885. $randomString = (string)$randomString;
  886. $user = User::findOne(['binding' => $randomString . 'X']);
  887. if ($user) {
  888. return $this->generateRandomString($length);
  889. }
  890. return $randomString;
  891. }
  892. public function platformPhoneAuth()
  893. {
  894. try {
  895. if(is_isv()){
  896. $config = [
  897. 'app_id' => Option::get("platform_third_appid",0,'saas')['value'],
  898. 'secret' => Option::get("platform_third_secret",0,'saas')['value'],
  899. 'token' => Option::get("platform_token",0,'saas')['value'],
  900. 'aes_key' => Option::get("platform_encodingAesKey",0,'saas')['value']
  901. ];
  902. $openPlatform = Factory::openPlatform($config);
  903. if(!empty(get_mini_id())){
  904. $mini = StoreMini::findOne(get_mini_id());
  905. $app = $openPlatform->miniProgram($mini->appid,$mini->authorizer_refresh_token);
  906. $session = $app->auth->session($this->code);
  907. \Yii::error("今天是个好日子");
  908. \Yii::error($session);
  909. $decryptedData = $app->encryptor->decryptData($session['session_key'], $this->iv, $this->encryptedData);
  910. \Yii::error($decryptedData);
  911. }else{
  912. return [
  913. 'code' => 1,
  914. 'msg' => '登录异常'
  915. ];
  916. }
  917. }elseif ($this->platform == 'bytedance') {
  918. /**
  919. * @var \ByteDance\MiniProgram\Application $byteDance
  920. */
  921. $byteDance = \Yii::$app->controller->byteDance;
  922. if (!$byteDance) {
  923. return [
  924. 'code' => 1,
  925. 'msg' => '登录异常'
  926. ];
  927. }
  928. $session = $byteDance->auth->session($this->code);
  929. $decryptedData = $byteDance->encryptor->decryptData($session['session_key'], $this->iv, $this->encryptedData);
  930. } else {
  931. $wechat = Option::getSaasPlatformWechat();
  932. $config = [
  933. 'app_id' => $wechat['appid'],
  934. 'secret' => $wechat['key'],
  935. 'response_type' => 'array'
  936. ];
  937. $this->wechat = Factory::miniProgram($config);
  938. if (!$this->wechat) {
  939. return [
  940. 'code' => 1,
  941. 'msg' => '登录异常'
  942. ];
  943. }
  944. $session = $this->wechat->auth->session($this->code);
  945. $decryptedData = $this->wechat->encryptor->decryptData($session['session_key'], $this->iv, $this->encryptedData);
  946. }
  947. if (isset($decryptedData['phoneNumber']) && !empty($decryptedData['phoneNumber'])) {
  948. // 创建平台会员saas_user
  949. $saas_user = SaasUser::find()->where(['mobile' => $decryptedData['phoneNumber'], 'is_delete' => SaasUser::DELETE_STATUS_FALSE])->one();
  950. if (!$saas_user) {
  951. $saas_user = new SaasUser();
  952. $saas_user->access_token = \Yii::$app->security->generateRandomString();
  953. $saas_user->name = substr_replace($decryptedData['phoneNumber'], '******', 3, 6);
  954. $saas_user->mobile = $decryptedData['phoneNumber'];
  955. if ($this->platform == 'bytedance') {
  956. $saas_user->bytedance_open_id = $session['openid'];
  957. } else {
  958. $saas_user->platform_open_id = $session['openid'];
  959. }
  960. $saas_user->avatar = \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/avatar.png';
  961. $saas_user->store_id = $this->store_id;
  962. $saas_user->save();
  963. } else {
  964. if (empty($saas_user->name)) {
  965. $saas_user->name = substr_replace($decryptedData['phoneNumber'], '****', 3, 4);
  966. }
  967. if (empty($saas_user->avatar)) {
  968. $saas_user->avatar = \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/avatar.png';
  969. }
  970. if (empty($saas_user->access_token)) {
  971. $saas_user->access_token = \Yii::$app->security->generateRandomString();
  972. }
  973. if ($this->platform == 'bytedance') {
  974. $saas_user->bytedance_open_id = $session['openid'];
  975. } else {
  976. $saas_user->platform_open_id = $session['openid'];
  977. }
  978. $saas_user->save();
  979. }
  980. // 平台登录 todo: 后续补充其他数据
  981. if ($this->store_id == 0) {
  982. return [
  983. 'code' => 0,
  984. 'msg' => '登录成功',
  985. 'data' => [
  986. 'access_token' => $saas_user->access_token,
  987. 'session_key' => $session['session_key'],
  988. 'nickname' => $saas_user->name,
  989. 'avatar_url' => $saas_user->avatar,
  990. 'id' => $saas_user->id,
  991. 'money' => $saas_user->share_profit,
  992. 'integral' => $saas_user->integral
  993. ]
  994. ];
  995. }
  996. } else {
  997. return [
  998. 'code' => 1,
  999. 'msg' => '登录失败',
  1000. ];
  1001. }
  1002. if($this->nickname){
  1003. $saas_user->name = $this->nickname;
  1004. }
  1005. if($this->avatar_url){
  1006. $saas_user->avatar = $this->avatar_url;
  1007. }
  1008. $saas_user->save();
  1009. $user = User::findOne(['binding' => $decryptedData['phoneNumber'], 'store_id' => $this->store_id, 'is_delete' => 0]);
  1010. $share = $share_user = null;
  1011. if ($user) {
  1012. if ($user->blacklist == '1') {
  1013. return [
  1014. 'code' => 1,
  1015. 'msg' => '您的账号已被限制登录!',
  1016. ];
  1017. }
  1018. $data = [
  1019. 'access_token' => $saas_user->access_token,
  1020. 'nickname' => $saas_user->name,
  1021. 'avatar_url' => $saas_user->avatar,
  1022. 'is_distributor' => $user->is_distributor ? $user->is_distributor : 0,
  1023. 'errCode' => 0,
  1024. 'id' => $user->id,
  1025. 'is_clerk' => $user->is_clerk === null ? 0 : $user->is_clerk,
  1026. 'integral' => $user->integral === null ? 0 : $user->integral,
  1027. 'money' => $user->money === null ? 0 : $user->money,
  1028. 'binding' => $user->binding,
  1029. 'level' => $user->level,
  1030. 'blacklist' => $user->blacklist,
  1031. 'is_saas_clerk' => (int)$user->is_saas_clerk,
  1032. ];
  1033. } else {
  1034. $data = [
  1035. 'nickName' => substr_replace($decryptedData['phoneNumber'], '******', 3, 6),
  1036. 'avatarUrl' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/avatar.png',
  1037. ];
  1038. $user = new User();
  1039. $user->type = User::USER_TYPE_NORMAL;
  1040. $user->binding = $decryptedData['phoneNumber'];
  1041. $user->nickname = $data['nickName'];
  1042. $user->avatar_url = $data['avatarUrl'];
  1043. $user->username = \Yii::$app->security->generateRandomString();
  1044. $user->password = \Yii::$app->security->generatePasswordHash(\Yii::$app->security->generateRandomString(), 5);
  1045. $user->auth_key = \Yii::$app->security->generateRandomString();
  1046. $user->access_token = \Yii::$app->security->generateRandomString();
  1047. $user->is_delete = User::USER_NOT_DELETE;
  1048. $user->store_id = $this->store_id;
  1049. if ($this->platform == 'bytedance') {
  1050. $user->platform = User::USER_FROM_BYTEDANCE; // 字节
  1051. } else {
  1052. $user->platform = User::USER_FROM_WECHAT; // 微信
  1053. }
  1054. if (!$user->save()) {
  1055. return [
  1056. 'code' => 1,
  1057. 'msg' => '登陆失败',
  1058. 'data' => $user->getErrorSummary(false)[0]
  1059. ];
  1060. }
  1061. $data = [
  1062. 'access_token' => $saas_user->access_token,
  1063. 'nickname' => $saas_user->name,
  1064. 'avatar_url' => $saas_user->avatar,
  1065. 'is_distributor' => $user->is_distributor ? $user->is_distributor : 0,
  1066. 'errCode' => 0,
  1067. 'id' => $user->id,
  1068. 'is_clerk' => $user->is_clerk === null ? 0 : $user->is_clerk,
  1069. 'integral' => $user->integral === null ? 0 : $user->integral,
  1070. 'money' => $user->money === null ? 0 : $user->money,
  1071. 'binding' => $user->binding,
  1072. 'level' => $user->level,
  1073. 'blacklist' => $user->blacklist,
  1074. 'is_saas_clerk' => 0,
  1075. ];
  1076. }
  1077. if($this->nickname){
  1078. $user->nickname = $this->nickname;
  1079. }
  1080. if($this->avatar_url){
  1081. $user->avatar_url = $this->avatar_url;
  1082. }
  1083. $user->save();
  1084. return [
  1085. 'code' => 0,
  1086. 'data' => $data,
  1087. 'msg' => '登录成功'
  1088. ];
  1089. } catch (\Exception $e) {
  1090. \Yii::error($e->getMessage() . ' file => ' . $e->getFile() . ' line => ' . $e->getLine());
  1091. return [
  1092. 'code' => 1,
  1093. 'msg' => '登录失败',
  1094. ];
  1095. }
  1096. }
  1097. // 使用openid登录
  1098. public function loginByOpenid()
  1099. {
  1100. try {
  1101. $this->wechat = self::getWechat();
  1102. if (!$this->wechat) {
  1103. return [
  1104. 'code' => 1,
  1105. 'msg' => ''
  1106. ];
  1107. }
  1108. $session = $this->wechat->auth->session($this->code);
  1109. $openid = $session['openid'];
  1110. // $openid = 'or1pO5a0oYCKlPm_wsfhdi6Ti4VQ';
  1111. $user = User::findOne(['wechat_open_id' => $openid, 'is_delete' => 0]);
  1112. if (!$user) {
  1113. throw new \Exception('');
  1114. }
  1115. $saas_user = SaasUser::findOne(['mobile' => $user->binding, 'is_delete' => 0]);
  1116. if (!$saas_user) {
  1117. throw new \Exception('');
  1118. }
  1119. $data = [
  1120. 'access_token' => $saas_user->access_token,
  1121. 'nickname' => $saas_user->name,
  1122. 'gender' => $saas_user->gender,
  1123. 'avatar_url' => $saas_user->avatar,
  1124. 'is_distributor' => $user->is_distributor ? $user->is_distributor : 0,
  1125. 'errCode' => 0,
  1126. 'id' => $user->id,
  1127. 'is_clerk' => $user->is_clerk === null ? 0 : $user->is_clerk,
  1128. 'integral' => $user->integral === null ? 0 : $user->integral,
  1129. 'money' => $user->money === null ? 0 : $user->money,
  1130. 'binding' => $user->binding,
  1131. 'level' => $user->level,
  1132. 'blacklist' => $user->blacklist,
  1133. 'is_saas_clerk' => 0,
  1134. 'store_id' => $saas_user->store_id,
  1135. ];
  1136. return [
  1137. 'code' => 0,
  1138. 'data' => $data,
  1139. 'msg' => '登录成功'
  1140. ];
  1141. } catch (\Exception $e) {
  1142. return [
  1143. 'code' => 1,
  1144. 'msg' => '',
  1145. ];
  1146. }
  1147. }
  1148. public function loginByUserId()
  1149. {
  1150. try {
  1151. // $user = User::findOne(['id' => $this->user_id, 'is_delete' => 0]);
  1152. // if (!$user) {
  1153. // throw new \Exception('');
  1154. // }
  1155. $saas_user = SaasUser::findOne(['id' => $this->user_id, 'is_delete' => 0]);
  1156. if (!$saas_user) {
  1157. throw new \Exception('');
  1158. }
  1159. $data = [
  1160. 'access_token' => $saas_user->access_token,
  1161. 'nickname' => $saas_user->name,
  1162. 'gender' => $saas_user->gender,
  1163. 'avatar_url' => $saas_user->avatar,
  1164. // 'is_distributor' => $user->is_distributor ? $user->is_distributor : 0,
  1165. 'errCode' => 0,
  1166. 'id' => $saas_user->id,
  1167. // 'is_clerk' => $user->is_clerk === null ? 0 : $user->is_clerk,
  1168. // 'integral' => $user->integral === null ? 0 : $user->integral,
  1169. // 'money' => $user->money === null ? 0 : $user->money,
  1170. 'binding' => $saas_user->mobile,
  1171. // 'level' => $user->level,
  1172. // 'blacklist' => $user->blacklist,
  1173. 'is_saas_clerk' => 0,
  1174. 'store_id' => $saas_user->store_id,
  1175. ];
  1176. return [
  1177. 'code' => 0,
  1178. 'data' => $data,
  1179. 'msg' => '登录成功'
  1180. ];
  1181. } catch (\Exception $e) {
  1182. return [
  1183. 'code' => 1,
  1184. 'msg' => '',
  1185. ];
  1186. }
  1187. }
  1188. // 用于供应链小程序切换商城后自动登录
  1189. public function loginByAuto()
  1190. {
  1191. try {
  1192. $saasUser = \get_saas_user();
  1193. $oldUser = \get_user();
  1194. $switchUser = User::find()->where([
  1195. 'is_delete' => 0,
  1196. 'binding' => $saasUser->mobile,
  1197. 'store_id' => $this->switch_store_id,
  1198. 'type' => User::USER_TYPE_NORMAL,
  1199. ])->one();
  1200. if (!$switchUser) {
  1201. $switchUser = new User();
  1202. $switchUser->type = User::USER_TYPE_NORMAL;
  1203. $switchUser->binding = $saasUser->mobile;
  1204. $switchUser->nickname = $saasUser->name;
  1205. $switchUser->avatar_url = $saasUser->avatar;
  1206. $switchUser->username = \Yii::$app->security->generateRandomString();
  1207. $switchUser->password = \Yii::$app->security->generatePasswordHash(\Yii::$app->security->generateRandomString(), 5);
  1208. $switchUser->auth_key = \Yii::$app->security->generateRandomString();
  1209. $switchUser->access_token = \Yii::$app->security->generateRandomString();
  1210. $switchUser->is_delete = User::USER_NOT_DELETE;
  1211. $switchUser->store_id = $this->switch_store_id;
  1212. if ($this->platform == 'bytedance') {
  1213. $switchUser->bytedance_open_id = $oldUser->bytedance_open_id;
  1214. $switchUser->platform = User::USER_FROM_BYTEDANCE; // 字节
  1215. } else {
  1216. $switchUser->platform = User::USER_FROM_WECHAT; // 微信
  1217. $switchUser->wechat_open_id = $oldUser->wechat_open_id;
  1218. }
  1219. $switchUser->save();
  1220. }
  1221. // 以下数据在访问用户中心时会被替换掉,所以这里不查询其他数据,只返回用户基本信息
  1222. $user_info = [
  1223. 'access_token' => $saasUser->access_token,
  1224. 'avatar_url' => $saasUser->avatar,
  1225. 'binding' => $switchUser->binding,
  1226. 'blacklist' => $switchUser->blacklist,
  1227. 'id' => $switchUser->id,
  1228. 'is_admin' => 0,
  1229. 'is_clerk' => '0',
  1230. 'is_saas_clerk' => '0',
  1231. 'is_delivery' => '0',
  1232. 'is_distributor' => '0',
  1233. 'is_live' => 0,
  1234. 'is_real' => '0',
  1235. 'level' => $switchUser->level,
  1236. 'level_name' => '普通用户',
  1237. 'nickname' => $saasUser->name,
  1238. 'parent' => '总店',
  1239. 'integral' => 0,
  1240. 'money' => "0.00",
  1241. 'price' => '0.00',
  1242. 'gender' => $saasUser->gender,
  1243. 'saas_money' => $saasUser->share_profit,
  1244. 'is_salesman' => $saasUser->is_salesman,
  1245. 'is_md_staff' => 0,
  1246. 'is_md_manager' => 0,
  1247. 'user_id' => $switchUser->id,
  1248. 'is_show_toggle_store' => 0,
  1249. 'has_store' => 0,
  1250. 'is_holder' => 0,
  1251. 'is_video_author' => 0,
  1252. ];
  1253. return [
  1254. 'code' => 0,
  1255. 'msg' => 'success',
  1256. 'data' => [
  1257. 'user_info' => $user_info,
  1258. ],
  1259. ];
  1260. } catch (\Exception $e) {
  1261. return [
  1262. 'code' => 1,
  1263. 'msg' => '自动登录失败',
  1264. ];
  1265. }
  1266. }
  1267. public function getRedirectUri() {
  1268. try {
  1269. $store_id = $this->store_id;
  1270. $wechat_config = WechatConfig::findOne(['store_id' => $store_id, 'is_delete' => 0, 'type' => 2]);
  1271. $one_store_wechat_official_appid = Option::get('one_store_wechat_official_appid', 0, 'saas', '')['value'];
  1272. if ((!empty($wechat_config) && $wechat_config->app_id) || !empty($one_store_wechat_official_appid)) {
  1273. $appid = $wechat_config->app_id;
  1274. $redirect_uri = \Yii::$app->request->hostInfo . '/'. $this->form_value .'/#' . $this->path ?: '/';
  1275. $state = 'store_id=' . $store_id;
  1276. // $redirect_uri = urlencode($redirect_uri);
  1277. $uri = getAuthLink($store_id, $redirect_uri, 0);
  1278. // $uri = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$appid}&redirect_uri={$redirect_uri}&response_type=code&scope=snsapi_base#wechat_redirect";
  1279. //https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx520c15f417810387&redirect_uri=https%3A%2F%2Fchong.qq.com%2Fphp%2Findex.php%3Fd%3D%26c%3DwxAdapter%26m%3DmobileDeal%26showwxpaytitle%3D1%26vb2ctag%3D4_2030_5_1194_60&response_type=code&scope=snsapi_base&state=123#wechat_redirect
  1280. return [
  1281. 'code' => 0,
  1282. 'msg' => '获取成功',
  1283. 'data' => [
  1284. 'uri' => $uri
  1285. ]
  1286. ];
  1287. }
  1288. return [
  1289. 'code' => 0,
  1290. 'msg' => '失败',
  1291. 'data' => [
  1292. 'uri' => ''
  1293. ]
  1294. ];
  1295. } catch (\Exception $e) {
  1296. return [
  1297. 'code' => 0,
  1298. 'msg' => $e->getMessage(),
  1299. 'data' => [
  1300. 'uri' => ''
  1301. ]
  1302. ];
  1303. }
  1304. }
  1305. public function setH5UserInfo() {
  1306. try {
  1307. $code = $this->code;
  1308. $saas_user = SaasUser::findOne(get_saas_user_id());
  1309. $user = User::findOne(get_user_id());
  1310. $app = WechatMini::getWechatConfig(get_store_id(), 0, WechatMini::TYPE_OFFICIAL);
  1311. if (!$app) {
  1312. return [
  1313. 'code' => 1,
  1314. 'msg' => '没有对应的配置项'
  1315. ];
  1316. }
  1317. $session_result = $app->oauth->userFromCode($code);
  1318. $session = $session_result->getTokenResponse();
  1319. if ($session['scope'] === 'snsapi_userinfo') {
  1320. $avatar = $session_result->getAvatar();
  1321. $nickname = $session_result->getNickname();
  1322. if ($saas_user) {
  1323. if (!empty($avatar)) {
  1324. $saas_user->avatar = $avatar;
  1325. }
  1326. if (!empty($nickname)) {
  1327. $saas_user->name = $nickname;
  1328. }
  1329. if (!$saas_user->save()) {
  1330. throw new \Exception(json_encode($saas_user->errors, JSON_UNESCAPED_UNICODE));
  1331. }
  1332. }
  1333. if ($user) {
  1334. if (!empty($avatar)) {
  1335. $user->avatar_url = $avatar;
  1336. }
  1337. if (!empty($nickname)) {
  1338. $user->nickname = $nickname;
  1339. }
  1340. if (!empty($session['openid'])) {
  1341. // $user->wechat_open_id = $session['openid'];
  1342. $user->wechat_platform_open_id = $session['openid'];
  1343. }
  1344. if (!$user->save()) {
  1345. throw new \Exception(json_encode($user->errors, JSON_UNESCAPED_UNICODE));
  1346. }
  1347. }
  1348. }
  1349. return [
  1350. 'code' => 0,
  1351. 'msg' => '设置成功',
  1352. 'data' => [
  1353. 'user_info' => [
  1354. 'avatar' => $saas_user->avatar,
  1355. 'name' => $saas_user->name
  1356. ]
  1357. ]
  1358. ];
  1359. } catch (\Exception $e) {
  1360. return [
  1361. 'code' => 0,
  1362. 'msg' => $e->getMessage()
  1363. ];
  1364. }
  1365. }
  1366. }