LoginForm.php 75 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840
  1. <?php
  2. /**
  3. * 厦门云联储网络科技有限公司
  4. * https://www.baokuaiyun.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\Admin;
  10. use app\models\Option;
  11. use app\models\Share;
  12. use app\models\StoreAdmin;
  13. use app\models\User;
  14. use app\models\WechatConfig;
  15. use app\modules\client\models\ApiModel;
  16. use app\utils\Notice\NoticeSend;
  17. use app\utils\Wechat\WechatMini;
  18. use yii\helpers\Json;
  19. use app\models\SaasUser;
  20. use app\modules\admin\models\GolangOrderForm;
  21. use Overtrue\Socialite\Providers\WeChat;
  22. use Overtrue\Socialite\SocialiteManager;
  23. include_once \Yii::$app->basePath . "/utils/Alipay/alipaySdk/aop/AopClient.php";;
  24. class LoginForm extends ApiModel
  25. {
  26. // 前端类型
  27. public $_platform;
  28. // 商城id
  29. public $store_id;
  30. public $login_type;
  31. public $password;
  32. // 微信小程序
  33. public $code;
  34. public $authCode;
  35. public $user_info;
  36. public $encrypted_data;
  37. public $iv;
  38. public $signature;
  39. // 手机注册
  40. public $phone;
  41. public $verify_code;
  42. public $isLogin; //直接绑定上下级
  43. // 分销商user_id
  44. public $share_user_id;
  45. /**
  46. * @var User $user
  47. */
  48. public $user;
  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. // 短信登录验证码
  58. const CACHE_KEY_SMS_LOGIN = '_mobile_sms_login_';
  59. // 绑定手机号验证码
  60. const CACHE_KEY_BIND_PHONE = '_mobile_bind_';
  61. // 短信忘记密码验证码
  62. const CACHE_KEY_SMS_FORGET_PASSWORD = '_mobile_sms_forget_password_';
  63. // 验证码类型
  64. // 手机登录
  65. const TYPE_VERIFY_CODE_LOGIN = 'login';
  66. // 手机绑定
  67. const TYPE_VERIFY_CODE_BIND = 'bind';
  68. // 清除数据
  69. const TYPE_VERIFY_CODE_CLEAR = 'clear';
  70. // 手机绑定
  71. const TYPE_VERIFY_CODE_FORGET_PASSWORD = 'forget_password';
  72. public $parent_id;
  73. public $appCid;
  74. public function rules()
  75. {
  76. return [
  77. ['_platform', 'in', 'range' => [self::PLATFORM_WX, self::PLATFORM_MP, self::PLATFORM_APP, self::PLATFORM_PHONE]],
  78. ['_platform', 'required'],
  79. [['code', 'user_info', 'encrypted_data', 'iv', 'signature'], 'required', 'on' => self::PLATFORM_WX],
  80. [['user_info'], 'required', 'on' => self::PLATFORM_APP],
  81. [['phone'], 'required', 'on' => self::PLATFORM_PHONE],
  82. [['phone'], 'match', 'pattern' => '/^(111\d{8}|1[3-9]\d{9})$/', 'message' => '手机号错误'],
  83. [['verify_code', 'parent_id', 'share_user_id', 'isLogin', 'login_type'], 'integer'],
  84. [['code', 'user_info', 'encrypted_data', 'iv', 'signature', 'phone', 'password'], 'string'],
  85. [['code', 'user_info', 'encrypted_data', 'iv', 'signature', 'phone'], 'trim'],
  86. ];
  87. }
  88. public function attributeLabels()
  89. {
  90. return [
  91. 'login_type' => 'login_type',
  92. '_platform' => '_platform',
  93. 'store_id' => 'store_id',
  94. 'code' => 'code',
  95. 'user_info' => 'user_info',
  96. 'encrypted_data' => 'encrypted_data',
  97. 'iv' => 'iv',
  98. 'signature' => 'signature',
  99. 'share_user_id' => 'share_user_id',
  100. ];
  101. }
  102. public function scenarios()
  103. {
  104. $scenarios = parent::scenarios();
  105. return $scenarios;
  106. }
  107. /**
  108. * 登录统一入口
  109. * @return array
  110. * @throws \yii\base\Exception
  111. */
  112. public function login($_alipay = false, $authCode = '')
  113. {
  114. if ($this->_platform == self::PLATFORM_WX) {
  115. // 微信小程序登录
  116. return $this->loginWX();
  117. } else if ($this->_platform == self::PLATFORM_APP) {
  118. // app端微信登录
  119. return $this->loginAppWechat();
  120. } else if ($this->_platform == self::PLATFORM_PHONE) {
  121. // 手机号登录,没有找到手机号自动注册
  122. // return $this->phoneLogin();
  123. return $this->phoneLoginCode($_alipay, $authCode);
  124. } else {
  125. return [
  126. 'code' => 1,
  127. 'msg' => '登录失败,请检查参数是否正确'
  128. ];
  129. }
  130. }
  131. public function loginnew()
  132. {
  133. if ($this->_platform == self::PLATFORM_WX) {
  134. // 微信小程序登录
  135. return $this->loginWX();
  136. } else if ($this->_platform == self::PLATFORM_APP) {
  137. // app端微信登录
  138. return $this->loginAppWechat();
  139. } else if ($this->_platform == self::PLATFORM_PHONE) {
  140. // 手机号登录,没有找到手机号自动注册
  141. // return $this->phoneLogin();
  142. return $this->phoneLoginCodeNew();
  143. } else {
  144. return [
  145. 'code' => 1,
  146. 'msg' => '登录失败,请检查参数是否正确'
  147. ];
  148. }
  149. }
  150. // 后台参数token
  151. public function getToKen($saasuserid, $mobile)
  152. {
  153. $where = [];
  154. // 判断 saasuserid 是否为空并添加到查询条件中
  155. if (!empty($saasuserid)) {
  156. $where['id'] = $saasuserid;
  157. }
  158. // 判断 mobile 是否为空并添加到查询条件中
  159. if (!empty($mobile)) {
  160. $where['mobile'] = $mobile;
  161. }
  162. // 查找符合条件的 SaasUser
  163. $saas_user = SaasUser::find()->where($where)->andWhere(['is_delete' => SaasUser::DELETE_STATUS_FALSE])->one();
  164. // 判断是否找到符合条件的用户
  165. if (!$saas_user) {
  166. // 处理未找到用户的情况
  167. return [
  168. 'code' => 1,
  169. 'msg' => '未找到当前用户'
  170. ];
  171. }
  172. if(empty(get_params('store_id'))){
  173. $saas_user->store_id = -1;
  174. }
  175. if (empty($saasuserid)) {
  176. $user = User::findOne(['binding' => $mobile, 'store_id' => $saas_user->store_id, 'is_delete' => 0]);
  177. if (!$user) {
  178. // 处理未找到用户的情况
  179. return [
  180. 'code' => 1,
  181. 'msg' => '未找到当前用户'
  182. ];
  183. }
  184. }
  185. // 是否开启强制绑定手机号
  186. $is_open_bind = Option::get(OptionSetting::STORE_LOGIN_FORCIBLY_BIND_MOBILE, $this->store_id, 'store')['value'];
  187. $share = $share_user = null;
  188. if ($user->parent_id > 0) {
  189. $share = Share::findOne(['user_id' => $user->parent_id]);
  190. $share_user = User::findOne(['id' => $share->user_id]);
  191. }
  192. $data = [
  193. 'access_token' => $saas_user->access_token,
  194. 'nickname' => $saas_user->name,
  195. 'avatar_url' => $saas_user->avatar,
  196. // 'access_token' => $user->access_token,
  197. // 'nickname' => $user->nickname,
  198. // 'avatar_url' => $user->avatar_url,
  199. 'is_distributor' => $user->is_distributor ? $user->is_distributor : 0,
  200. 'errCode' => 0,
  201. 'parent' => $share ? ($share->name ? $share->name : $share_user->nickname) : '总店',
  202. 'id' => $user->id,
  203. 'is_clerk' => $user->is_clerk === null ? 0 : $user->is_clerk,
  204. 'integral' => $user->integral === null ? 0 : $user->integral,
  205. 'money' => $user->money === null ? 0 : $user->money,
  206. 'binding' => $user->binding,
  207. 'level' => $user->level,
  208. 'blacklist' => $user->blacklist,
  209. 'is_open_bind' => $is_open_bind ? $is_open_bind : 0,
  210. 'store_id' => $saas_user->store_id,
  211. ];
  212. // 如果找到了用户,继续后续逻辑
  213. return [
  214. 'code' => 0,
  215. 'msg' => '获取成功',
  216. 'data' => $data
  217. ];
  218. }
  219. /**saas端获取uninid*/
  220. public function getsaasunid(){
  221. $session = self::getWechat()->auth->session($this->code);
  222. if (!$session || empty($session['openid'])) {
  223. throw new \Exception('获取openid失败.');
  224. }
  225. $decryptedData = self::getWechat()->encryptor->decryptData($session['session_key'], $this->iv, $this->encrypted_data);
  226. $unionId = isset($session['unionid']) ? $session['unionid'] : '';
  227. }
  228. /**
  229. * 微信登录方法
  230. * @return array
  231. */
  232. private function loginWX()
  233. {
  234. if ($this->validate()) {
  235. try {
  236. //获取session
  237. $session = self::getWechat()->auth->session($this->code);
  238. if (!$session || empty($session['openid'])) {
  239. throw new \Exception('获取openid失败.');
  240. }
  241. // 验证
  242. $decryptedData = self::getWechat()->encryptor->decryptData($session['session_key'], $this->iv, $this->encrypted_data);
  243. $user_info = json_decode($this->user_info, true);
  244. $user_info['nickName'] = preg_replace('/[\xf0-\xf7].{3}/', '', $user_info['nickName']);
  245. $data = [
  246. 'openId' => $session['openid'],
  247. 'nickName' => isset($user_info['nickName']) ? $user_info['nickName'] : $this->getNickName(),
  248. 'gender' => $user_info['gender'],
  249. 'city' => $user_info['city'],
  250. 'province' => $user_info['province'],
  251. 'country' => $user_info['country'],
  252. 'avatarUrl' => $user_info['avatarUrl'] ?: \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/statics/images/avatar.png',
  253. 'unionId' => isset($session['unionid']) ? $session['unionid'] : '',
  254. ];
  255. $isNew = 0;
  256. $user = User::find()->where(['store_id' => $this->store_id])->andWhere(['binding' => $this->phone])->orderBy('id desc')->one();//, 'platform' => 0
  257. if (!$user) {
  258. $isNew = 1;
  259. $user = new User();
  260. $user->type = User::USER_TYPE_NORMAL;
  261. $user->binding = isset($decryptedData['phoneNumber']) ? $decryptedData['phoneNumber'] : '';
  262. $user->username = $data['openId'];
  263. $user->password = \Yii::$app->security->generatePasswordHash(\Yii::$app->security->generateRandomString(), 5);
  264. $user->auth_key = \Yii::$app->security->generateRandomString();
  265. $user->access_token = \Yii::$app->security->generateRandomString();
  266. $user->is_delete = User::USER_NOT_DELETE;
  267. $user->wechat_open_id = $data['openId'];
  268. $user->wechat_union_id = $data['unionId'];
  269. $user->store_id = $this->store_id;
  270. $user->platform = User::USER_FROM_WECHAT; // 微信
  271. }
  272. $user->binding = $this->phone;
  273. $user->nickname = $data['nickName'];
  274. $user->avatar_url = $data['avatarUrl'];
  275. if (empty($user->wechat_open_id) || (!empty($data['openId']) && $user->wechat_open_id !== $data['openId'])) {
  276. $user->wechat_open_id = $data['openId'];
  277. }
  278. if (empty($user->wechat_union_id) || (!empty($data['unionId']) && $user->wechat_union_id !== $data['unionId'])) {
  279. $user->wechat_union_id = $data['unionId'];
  280. }
  281. if (!$user->save()) {
  282. return [
  283. 'code' => 1,
  284. 'msg' => '登陆失败',
  285. 'data' => $user->getErrorSummary(false)[0]
  286. ];
  287. }
  288. // 创建平台会员saas_user
  289. $saas_user = SaasUser::find()->where(['mobile' => $user->binding, 'is_delete' => SaasUser::DELETE_STATUS_FALSE])->one();
  290. if (!$saas_user) {
  291. $saas_user = new SaasUser();
  292. $saas_user->mobile = $user->binding;
  293. $saas_user->store_id = $this->store_id;
  294. $saas_user->save();
  295. }
  296. // 是否开启强制绑定手机号
  297. $is_open_bind = Option::get(OptionSetting::STORE_LOGIN_FORCIBLY_BIND_MOBILE, $this->store_id, 'store')['value'];
  298. $share = $share_user = null;
  299. if ($user->parent_id > 0) {
  300. $share = Share::findOne(['user_id' => $user->parent_id]);
  301. $share_user = User::findOne(['id' => $share->user_id]);
  302. }
  303. if ($this->share_user_id > 0) {
  304. // 绑定上下级
  305. $bindForm = new BindForm();
  306. $bindForm->store_id = $this->store_id;
  307. $bindForm->user_id = $user->id;
  308. $bindForm->parent_id = $this->share_user_id;
  309. $bindForm->condition = 0;
  310. $bindForm->isNew = $isNew;
  311. $res = $bindForm->save();
  312. //debug_log([__METHOD__, __LINE__, $res], "app_debug.log");
  313. }
  314. //go用户同步
  315. GolangOrderForm::userSyncUserInfo([
  316. "nick_name"=>(string)$user->binding,
  317. "store_id"=>(int)$this->store_id,
  318. "user_name"=>(string)$user->binding,
  319. "phone"=>(string)$user->binding,
  320. "parent_id"=>(int)$this->share_user_id,
  321. 'avatar'=>(string)$user->avatar_url,
  322. ]);
  323. $data = [
  324. 'access_token' => $user->access_token,
  325. 'nickname' => $user->nickname,
  326. 'avatar_url' => $user->avatar_url,
  327. 'is_distributor' => $user->is_distributor ? $user->is_distributor : 0,
  328. 'errCode' => 0,
  329. 'parent' => $share ? ($share->name ? $share->name : $share_user->nickname) : '总店',
  330. 'id' => $user->id,
  331. 'is_clerk' => $user->is_clerk === null ? 0 : $user->is_clerk,
  332. 'integral' => $user->integral === null ? 0 : $user->integral,
  333. 'money' => $user->money === null ? 0 : $user->money,
  334. 'binding' => $user->binding,
  335. 'level' => $user->level,
  336. 'blacklist' => $user->blacklist,
  337. 'is_open_bind' => $is_open_bind ? $is_open_bind : 0
  338. ];
  339. return [
  340. 'code' => 0,
  341. 'data' => $data,
  342. 'msg' => '登陆成功'
  343. ];
  344. } catch (\Exception $e) {
  345. return [
  346. 'code' => 1,
  347. 'msg' => '登录失败',
  348. 'data' => $e->getMessage() . 'line:' . $e->getLine() . 'file:' . $e->getFile()
  349. ];
  350. }
  351. } else {
  352. // 验证失败:$errors 是一个包含错误信息的数组
  353. return [
  354. 'code' => 1,
  355. 'msg' => $this->getErrorSummary(false)[0]
  356. ];
  357. }
  358. }
  359. /**
  360. * app 微信登录
  361. * @return array
  362. * @throws \yii\base\Exception
  363. */
  364. public function loginAppWechat()
  365. {
  366. if (!$this->validate()) {
  367. return [
  368. 'code' => 1,
  369. 'msg' => $this->getErrorSummary(false)[0]
  370. ];
  371. }
  372. $user_info = Json::decode($this->user_info);
  373. $user = null;
  374. if (isset($user_info['unionId']) && $user_info['unionId'] != '') {
  375. $user = User::findOne(['wechat_union_id' => $user_info['unionId'], 'store_id' => $this->store_id]);
  376. }
  377. if (!$user) {
  378. $user = User::findOne(['wechat_open_id' => $user_info['openId'], 'store_id' => $this->store_id]);
  379. }
  380. // 是否开启强制绑定手机号
  381. $is_open_bind = Option::get(OptionSetting::STORE_LOGIN_FORCIBLY_BIND_MOBILE, $this->store_id, 'store')['value'];
  382. if (!$user) {
  383. $user = new User();
  384. $user->type = 1;
  385. $user->username = $user_info['openId'];
  386. $user->password = \Yii::$app->security->generatePasswordHash(\Yii::$app->security->generateRandomString(), 5);
  387. $user->auth_key = \Yii::$app->security->generateRandomString();
  388. $user->access_token = \Yii::$app->security->generateRandomString();
  389. $user->created_at = time();
  390. $user->is_delete = 0;
  391. $user->wechat_open_id = $user_info['openId'];
  392. $user->wechat_union_id = isset($user_info['unionId']) ? $user_info['unionId'] : '';
  393. $user->nickname = preg_replace('/[\xf0-\xf7].{3}/', '', $user_info['nickName']);
  394. $user->avatar_url = $user_info['avatarUrl'] ? $user_info['avatarUrl'] : \Yii::$app->request->hostInfo . '/web/v1/statics/images/avatar.png';
  395. $user->store_id = 1;
  396. $user->platform = 3; // APP
  397. $user->level = -1;
  398. if (isset($user_info['appCid']) && !empty($user_info['appCid'])) {
  399. $user->appcid = $user_info['appCid'];
  400. }
  401. if ($user->save()) {
  402. // \Yii::$app->user->login($user);
  403. $parent = User::findOne($user->parent_id);
  404. $share = Share::findOne(['user_id' => $user->parent_id]);
  405. $data = [
  406. 'to_bind' => 1,
  407. 'nickname' => $user->nickname,
  408. 'binding' => $user->binding ? $user->binding : '',
  409. 'id' => $user->id,
  410. 'avatar_url' => $user->avatar_url,
  411. 'is_distributor' => $user->is_distributor ? $user->is_distributor : 0,
  412. 'integral' => $user->integral ? $user->integral : 0,
  413. 'money' => $user->money ? $user->money : 0,
  414. 'blacklist' => $user->blacklist ? $user->blacklist : 0,
  415. 'access_token' => $user->access_token,
  416. 'is_clerk' => $user->is_clerk === null ? 0 : $user->is_clerk,
  417. 'parent' => $share ? ($share->name ? $share->name : $parent->nickname) : "总店",
  418. 'level' => $user->level,
  419. 'is_open_bind' => $is_open_bind ? $is_open_bind : 0
  420. ];
  421. } else {
  422. return [
  423. 'code' => 1,
  424. 'msg' => '登录失败',
  425. 'data' => $user->errors[0]
  426. ];
  427. }
  428. } else {
  429. if (isset($user_info['appCid']) && !empty($user_info['appCid']) && $user->appcid != $user_info['appCid']) {
  430. $user->appcid = $user_info['appCid'];
  431. }
  432. if (empty($user->wechat_union_id)) {
  433. $user->wechat_union_id = isset($user_info['unionId']) ? $user_info['unionId'] : '';
  434. }
  435. $user->nickname = preg_replace('/[\xf0-\xf7].{3}/', '', $user_info['nickName']);
  436. $user->avatar_url = $user_info['avatarUrl'];
  437. $user->save();
  438. // \Yii::$app->user->login($user);
  439. $parent = User::findOne($user->parent_id);
  440. $share = Share::findOne(['user_id' => $user->parent_id]);
  441. $data = [
  442. 'to_bind' => 0,
  443. 'nickname' => $user->nickname,
  444. 'binding' => $user->binding === null ? '' : $user->binding,
  445. 'id' => $user->id,
  446. 'avatar_url' => $user->avatar_url,
  447. 'is_distributor' => $user->is_distributor,
  448. 'integral' => $user->integral,
  449. 'money' => $user->money,
  450. 'blacklist' => $user->blacklist,
  451. 'access_token' => $user->access_token,
  452. 'is_clerk' => $user->is_clerk === null ? 0 : $user->is_clerk,
  453. 'parent' => $share ? ($share->name ? $share->name : $parent->nickname) : "总店",
  454. 'level' => $user->level,
  455. 'is_open_bind' => $is_open_bind ? $is_open_bind : 0
  456. ];
  457. }
  458. $saas_user = SaasUser::find()->where(['mobile'=>$user->binding,'is_delete' => SaasUser::DELETE_STATUS_FALSE])->one();
  459. $store_admin = StoreAdmin::findOne(['id' => $saas_user->store_id, 'is_delete' => 0, 'status' => 1]);
  460. $data['Authorization'] = $this->setToken($store_admin->id);
  461. return [
  462. 'code' => 0,
  463. 'msg' => '登录成功',
  464. 'data' => $data
  465. ];
  466. }
  467. /**
  468. * 手机号登录
  469. * @return array
  470. * @throws \yii\base\Exception
  471. */
  472. public function loginAppPhone()
  473. {
  474. if (!$this->validate()) {
  475. return [
  476. 'code' => 1,
  477. 'msg' => $this->getErrorSummary(false)[0]
  478. ];
  479. }
  480. // 验证码验证
  481. $result = $this->verifySmsCode(self::CACHE_KEY_SMS_LOGIN);
  482. if ($result['code'] == 1) {
  483. return $result;
  484. }
  485. $parent_name = '总店';
  486. /**
  487. * @var User $user
  488. */
  489. $user = User::find()->where([
  490. 'store_id' => $this->store_id,
  491. 'type' => 1,
  492. 'is_delete' => 0,
  493. 'blacklist' => 0,
  494. 'binding' => $this->phone
  495. ])->one();
  496. if (!$user) {
  497. $password = '0';
  498. $user = new User();
  499. $user->type = 1;
  500. $user->username = \Yii::$app->security->generateRandomString();
  501. $user->password = $password;
  502. $user->auth_key = \Yii::$app->security->generateRandomString();
  503. $user->access_token = \Yii::$app->security->generateRandomString();
  504. $user->created_at = time();
  505. $user->is_delete = 0;
  506. $user->binding = $this->phone;
  507. $user->nickname = $this->getDefaultNickname($this->phone);
  508. $user->avatar_url = $this->getDefaultAvatar();
  509. $user->store_id = $this->store_id;
  510. $user->platform = User::USER_FROM_PHONE;
  511. if ($this->appCid) {
  512. $user->appcid = $this->appCid;
  513. }
  514. if ($this->parent_id > 0) {
  515. $parent_id = $this->parent_id;
  516. $setting = Option::get('share_basic_setting', get_store_id());
  517. $setting = $setting ? Json::decode($setting['value']) : [];
  518. if (!$setting || $setting['level']['value'] == 0) {
  519. $parent_id = 0;
  520. }
  521. if ($parent_id > 0) {
  522. $exists = Share::find()->andWhere([
  523. 'user_id' => $this->parent_id,
  524. 'store_id' => $this->store_id,
  525. 'is_delete' => 0,
  526. 'status' => 1
  527. ]);
  528. if ($exists->exists()) {
  529. $parent_name = $exists->one()->name;
  530. $user->parent_id = $parent_id;
  531. $user->old_parent_id = $parent_id;
  532. } else {
  533. $parent_name = User::findOne(['id' => $this->parent_id])->nickname;
  534. }
  535. }
  536. }
  537. if ($user->save()) {
  538. $data = [
  539. 'nickname' => $user->nickname,
  540. 'binding' => $user->binding,
  541. 'id' => $user->id,
  542. 'avatar_url' => $user->avatar_url,
  543. 'is_distributor' => $user->is_distributor ? $user->is_distributor : 0,
  544. 'integral' => $user->integral ? $user->integral : 0,
  545. 'money' => $user->money ? $user->money : 0,
  546. 'blacklist' => $user->blacklist ? $user->blacklist : 0,
  547. 'access_token' => $user->access_token,
  548. 'is_clerk' => $user->is_clerk === null ? 0 : $user->is_clerk,
  549. 'parent' => $parent_name,
  550. 'level' => -1
  551. ];
  552. return [
  553. 'code' => 0,
  554. 'msg' => '注册成功',
  555. 'data' => $data,
  556. ];
  557. } else {
  558. foreach ($user->errors as $error) {
  559. return [
  560. 'code' => 1,
  561. 'msg' => $error
  562. ];
  563. }
  564. }
  565. }
  566. if ($user->parent_id > 0) {
  567. $share = Share::findOne(['user_id' => $user->parent_id]);
  568. $parent_user = User::findOne($user->parent_id);
  569. $parent_name = $share ? ($share->name ? $share->name : $parent_user->nickname) : "总店";
  570. }
  571. $data = [
  572. 'nickname' => $user->nickname,
  573. 'binding' => $user->binding,
  574. 'id' => $user->id,
  575. 'avatar_url' => $user->avatar_url,
  576. 'is_distributor' => $user->is_distributor,
  577. 'integral' => $user->integral,
  578. 'money' => $user->money,
  579. 'blacklist' => $user->blacklist,
  580. 'access_token' => $user->access_token,
  581. 'is_clerk' => $user->is_clerk === null ? 0 : $user->is_clerk,
  582. 'parent' => $parent_name,
  583. 'level' => $user->level
  584. ];
  585. return [
  586. 'code' => 0,
  587. 'msg' => '登录成功',
  588. 'data' => $data,
  589. ];
  590. }
  591. public function phoneLogin()
  592. {
  593. if (!$this->validate()) {
  594. return [
  595. 'code' => 1,
  596. 'msg' => $this->getErrorSummary(false)[0]
  597. ];
  598. }
  599. if ($this->login_type == 1) {
  600. // $is_password_login = intval(Option::get(OptionSetting::IS_PASSWORD_LOGIN, $this->store_id, 'display', 0)['value']);
  601. // if ($is_password_login) {
  602. // return [
  603. // 'code' => 1,
  604. // 'msg' => '商城开启密码登录方式,请使用密码登录'
  605. // ];
  606. // }
  607. // 验证码验证
  608. $result = $this->verifySmsCode(self::CACHE_KEY_SMS_LOGIN);
  609. if ($result['code'] == 1) {
  610. return $result;
  611. }
  612. } else {
  613. // $is_password_login = intval(Option::get(OptionSetting::IS_PASSWORD_LOGIN, $this->store_id, 'display', 0)['value']);
  614. // if (!$is_password_login) {
  615. // return [
  616. // 'code' => 1,
  617. // 'msg' => '商城未开启密码登录方式,请使用验证码登录'
  618. // ];
  619. // }
  620. // 密码验证
  621. $user = User::find()->where(['store_id' => $this->store_id, 'type' => 1, 'is_delete' => 0, 'binding' => $this->phone])->one();
  622. if (!$user) {
  623. return [
  624. 'code' => 1,
  625. 'msg' => '用户不存在'
  626. ];
  627. }
  628. if (false === \Yii::$app->security->validatePassword($this->password, $user->password)) {
  629. return [
  630. 'code' => 1,
  631. 'msg' => '密码错误'
  632. ];
  633. }
  634. }
  635. ////debug_log('公众号登录01', 'off.log');
  636. $plat_openid = '';
  637. $openid = '';
  638. ////debug_log('公众号登录02_code=' . $this->code, 'off.log');
  639. if ($this->code) {
  640. try {
  641. ////debug_log('公众号登录03', 'off.log');
  642. if (is_wechat_platform()) {
  643. $wechat = self::getWechat();
  644. if (is_h5()) {
  645. ////debug_log('公众号登录04', 'off.log');
  646. $session = $wechat->oauth->userFromCode($this->code)->getTokenResponse();
  647. $plat_openid = $session['openid'];
  648. ////debug_log('公众号登录05'. json_encode($session, JSON_UNESCAPED_UNICODE), 'off.log');
  649. } else {
  650. $app = WechatMini::getWechatConfig($this->store_id);
  651. if (empty($app)) {
  652. return [
  653. 'code' => 1,
  654. 'msg' => '小程序参数获取失败!',
  655. ];
  656. }
  657. $session = $app->auth->session($this->code);
  658. if (empty($session)) {
  659. return [
  660. 'code' => 1,
  661. 'msg' => '小程序参数异常!',
  662. ];
  663. }
  664. if (!$session || empty($session['openid'])) {
  665. throw new \Exception('获取openid失败.');
  666. }
  667. $openid = $session['openid'];
  668. }
  669. } elseif (is_alipay_platform()) {
  670. }
  671. } catch (\Exception $e) {
  672. return [
  673. 'code' => 1,
  674. 'msg' => $e->getMessage(),
  675. ];
  676. //debug_log('公众号登录06' . $e->getMessage(), 'off.log');
  677. }
  678. ////debug_log('公众号登录07', 'off.log');
  679. }
  680. ////debug_log('公众号登录08', 'off.log');
  681. // 创建平台会员saas_user
  682. $saas_user = SaasUser::find()->where(['mobile' => $this->phone, 'is_delete' => SaasUser::DELETE_STATUS_FALSE])->one();
  683. if (!$saas_user) {
  684. $saas_user = new SaasUser();
  685. $saas_user->access_token = \Yii::$app->security->generateRandomString();
  686. $saas_user->name = substr_replace($this->phone, '******', 3, 6);
  687. $saas_user->mobile = $this->phone;
  688. $saas_user->platform_open_id = '';
  689. $saas_user->avatar = \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/avatar.png';
  690. $saas_user->store_id = $this->store_id;
  691. $saas_user->save();
  692. } else {
  693. if (empty($saas_user->name)) {
  694. $saas_user->name = substr_replace($this->phone, '****', 3, 4);
  695. }
  696. if (empty($saas_user->avatar)) {
  697. $saas_user->avatar = \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/avatar.png';
  698. }
  699. if (empty($saas_user->access_token)) {
  700. $saas_user->access_token = \Yii::$app->security->generateRandomString();
  701. }
  702. // $saas_user->platform_open_id = $this->phone;
  703. $saas_user->save();
  704. }
  705. // 平台登录 todo: 后续补充其他数据
  706. if ($this->store_id == 0) {
  707. return [
  708. 'code' => 0,
  709. 'msg' => '登录成功',
  710. 'data' => [
  711. 'access_token' => $saas_user->access_token,
  712. 'nickname' => $saas_user->name,
  713. 'avatar_url' => $saas_user->avatar,
  714. 'id' => $saas_user->id,
  715. 'money' => $saas_user->share_profit,
  716. 'integral' => $saas_user->integral
  717. ]
  718. ];
  719. }
  720. $user = User::findOne(['binding' => $this->phone, 'store_id' => $this->store_id, 'is_delete' => 0]);
  721. $share = $share_user = null;
  722. $isNew = 0;
  723. if ($user) {
  724. if ($user->blacklist == '1') {
  725. return [
  726. 'code' => 1,
  727. 'msg' => '您的账号已被限制登录!',
  728. ];
  729. }
  730. if ($plat_openid) {
  731. $user->wechat_platform_open_id = $plat_openid;
  732. $user->save();
  733. }
  734. if ($openid) {
  735. $user->wechat_open_id = $openid;
  736. $user->save();
  737. }
  738. $data = [
  739. 'access_token' => $saas_user->access_token,
  740. 'nickname' => $saas_user->name,
  741. 'avatar_url' => $saas_user->avatar,
  742. 'is_distributor' => $user->is_distributor ? $user->is_distributor : 0,
  743. 'errCode' => 0,
  744. 'id' => $user->id,
  745. 'is_clerk' => $user->is_clerk === null ? 0 : $user->is_clerk,
  746. 'integral' => $user->integral === null ? 0 : $user->integral,
  747. 'money' => $user->money === null ? 0 : $user->money,
  748. 'binding' => $user->binding,
  749. 'level' => $user->level,
  750. 'blacklist' => $user->blacklist,
  751. 'is_saas_clerk' => (int)$user->is_saas_clerk,
  752. 'wechat_app_open_id' => $user->wechat_app_open_id,
  753. 'wechat_platform_open_id' => $user->wechat_platform_open_id,
  754. 'h5_auth_link' => self::getAuthLink(),
  755. 'store_id' => $this->store_id,
  756. ];
  757. } else {
  758. $isNew = 1;
  759. $data = [
  760. 'nickName' => substr_replace($this->phone, '******', 3, 6),
  761. 'avatarUrl' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/avatar.png',
  762. ];
  763. $user = new User();
  764. $user->type = User::USER_TYPE_NORMAL;
  765. $user->binding = $this->phone;
  766. $user->nickname = $data['nickName'];
  767. $user->avatar_url = $data['avatarUrl'];
  768. $user->username = \Yii::$app->security->generateRandomString();
  769. $user->password = \Yii::$app->security->generatePasswordHash(\Yii::$app->security->generateRandomString(), 5);
  770. $user->auth_key = \Yii::$app->security->generateRandomString();
  771. $user->access_token = \Yii::$app->security->generateRandomString();
  772. $user->is_delete = User::USER_NOT_DELETE;
  773. $user->store_id = $this->store_id;
  774. $user->parent_id = 0;
  775. $user->old_parent_id = 0;
  776. $user->platform = is_alipay_platform() ? User::USER_FROM_ALIPAY : User::USER_FROM_PHONE; // 手机号
  777. if ($openid) {
  778. $user->wechat_open_id = $openid;
  779. }
  780. if ($plat_openid) {
  781. $user->wechat_platform_open_id = $plat_openid;
  782. }
  783. if (!$user->save()) {
  784. return [
  785. 'code' => 1,
  786. 'msg' => '登陆失败',
  787. 'data' => $user->getErrorSummary(false)[0]
  788. ];
  789. }
  790. $data = [
  791. 'access_token' => $saas_user->access_token,
  792. 'nickname' => $saas_user->name,
  793. 'avatar_url' => $saas_user->avatar,
  794. 'is_distributor' => $user->is_distributor ? $user->is_distributor : 0,
  795. 'errCode' => 0,
  796. 'id' => $user->id,
  797. 'is_clerk' => $user->is_clerk === null ? 0 : $user->is_clerk,
  798. 'integral' => $user->integral === null ? 0 : $user->integral,
  799. 'money' => $user->money === null ? 0 : $user->money,
  800. 'binding' => $user->binding,
  801. 'level' => $user->level,
  802. 'blacklist' => $user->blacklist,
  803. 'is_saas_clerk' => 0,
  804. 'wechat_app_open_id' => $user->wechat_app_open_id,
  805. 'wechat_platform_open_id' => $user->wechat_platform_open_id,
  806. 'alipay_open_id' => $user->alipay_open_id,
  807. 'h5_auth_link' => self::getAuthLink(),
  808. 'store_id' => $saas_user->store_id,
  809. ];
  810. }
  811. if ($this->share_user_id && $this->share_user_id > 0) {
  812. $bind = new BindForm();
  813. $bind->user_id = $data['id'];
  814. $bind->store_id = $this->store_id;
  815. $bind->isLogin = $this->isLogin;
  816. $bind->parent_id = $this->share_user_id;
  817. $bind->condition = 0;
  818. $bind->isNew = $isNew;
  819. $res = $bind->save();
  820. //debug_log([__METHOD__, __LINE__, $res], "app_debug.log");
  821. }
  822. return [
  823. 'code' => 0,
  824. 'data' => $data,
  825. 'msg' => '登录成功'
  826. ];
  827. }
  828. public function phoneLoginCode($_alipay, $authCode)
  829. {
  830. $login_data = all_params();
  831. if (!$this->validate()) {
  832. return [
  833. 'code' => 1,
  834. 'msg' => $this->getErrorSummary(false)[0]
  835. ];
  836. }
  837. if ($this->login_type == 1) {
  838. // $is_password_login = intval(Option::get(OptionSetting::IS_PASSWORD_LOGIN, $this->store_id, 'display', 0)['value']);
  839. // if ($is_password_login) {
  840. // return [
  841. // 'code' => 1,
  842. // 'msg' => '商城开启密码登录方式,请使用密码登录'
  843. // ];
  844. // }
  845. // 验证码验证
  846. $result = $this->verifySmsCode(self::CACHE_KEY_SMS_LOGIN);
  847. if ($result['code'] == 1) {
  848. return $result;
  849. }
  850. } else {
  851. // $is_password_login = intval(Option::get(OptionSetting::IS_PASSWORD_LOGIN, $this->store_id, 'display', 0)['value']);
  852. // if (!$is_password_login) {
  853. // return [
  854. // 'code' => 1,
  855. // 'msg' => '商城未开启密码登录方式,请使用验证码登录'
  856. // ];
  857. // }
  858. // 密码验证
  859. $user = User::find()->where(['store_id' => $this->store_id, 'type' => 1, 'is_delete' => 0, 'binding' => $this->phone])->one();
  860. if (!$user) {
  861. return [
  862. 'code' => 1,
  863. 'msg' => '用户不存在'
  864. ];
  865. }
  866. if (false === \Yii::$app->security->validatePassword($this->password, $user->password)) {
  867. return [
  868. 'code' => 1,
  869. 'msg' => '密码错误'
  870. ];
  871. }
  872. }
  873. ////debug_log('公众号登录01', 'off.log');
  874. $plat_openid = '';
  875. $openid = '';
  876. $aplipay_user_id = '';
  877. ////debug_log('公众号登录02_code=' . $this->code, 'off.log');
  878. $this->code = "";
  879. if ($this->code) {
  880. try {
  881. ////debug_log('公众号登录03', 'off.log');
  882. if (is_wechat_platform()) {
  883. $wechat = self::getWechat();
  884. if (is_h5()) {
  885. ////debug_log('公众号登录04', 'off.log');
  886. $session = $wechat->oauth->userFromCode($this->code)->getTokenResponse();
  887. $plat_openid = $session['openid'];
  888. ////debug_log('公众号登录05'. json_encode($session, JSON_UNESCAPED_UNICODE), 'off.log');
  889. } else {
  890. $app = WechatMini::getWechatConfig($this->store_id);
  891. if (empty($app)) {
  892. return [
  893. 'code' => 1,
  894. 'msg' => '小程序参数获取失败!',
  895. ];
  896. }
  897. $session = $app->auth->session($this->code);
  898. if (empty($session)) {
  899. return [
  900. 'code' => 1,
  901. 'msg' => '小程序参数异常!',
  902. ];
  903. }
  904. if (!$session || empty($session['openid'])) {
  905. throw new \Exception('获取openid失败11.'.json_encode($app));
  906. }
  907. $openid = $session['openid'];
  908. }
  909. } elseif (is_alipay_platform()) {
  910. $keys = [
  911. 'alipay_platform'
  912. ];
  913. $data = Option::get($keys, 0, 'saas');
  914. $alipay_config = json_decode($data[0]['value'], true);
  915. // 获取user_id
  916. $config = [
  917. 'alipay' => [
  918. 'client_id' => $alipay_config['app_id'],
  919. 'rsa_private_key' => $alipay_config['app_private_key'],
  920. ]
  921. ];
  922. //获取支付宝用户信息
  923. $socialite = new SocialiteManager($config);
  924. $token = $socialite->create('alipay')->tokenFromCode($authCode);
  925. // if (empty($token['user_id'])) {
  926. if (empty($token['user_id']) && empty($token['open_id'])) {
  927. return [
  928. 'code' => 1,
  929. 'msg' => '获取user_id失败'
  930. ];
  931. }
  932. $aplipay_user_id = isset($token['user_id']) ? $token['user_id'] : $token['open_id'];
  933. }
  934. } catch (\Exception $e) {
  935. return [
  936. 'code' => 1,
  937. 'msg' => $e->getMessage(),
  938. ];
  939. //debug_log('公众号登录06' . $e->getMessage(), 'off.log');
  940. }
  941. ////debug_log('公众号登录07', 'off.log');
  942. }
  943. ////debug_log('公众号登录08', 'off.log');
  944. // 创建平台会员saas_user
  945. $saas_user = SaasUser::find()->where(['mobile' => $this->phone, 'is_delete' => SaasUser::DELETE_STATUS_FALSE])->one();
  946. if (!$saas_user) {
  947. $saas_user = new SaasUser();
  948. $saas_user->access_token = \Yii::$app->security->generateRandomString();
  949. $saas_user->name = substr_replace($this->phone, '******', 3, 6);
  950. $saas_user->mobile = $this->phone;
  951. //$saas_user->platform_open_id = $openid;
  952. $saas_user->platform_open_id = '';
  953. $saas_user->avatar = \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/avatar.png';
  954. $saas_user->store_id = $this->store_id;
  955. $saas_user->ali_user_id = $aplipay_user_id;
  956. $saas_user->parent_id = empty($this->parent_id) ? 0 : $this->parent_id;
  957. $saas_user->save();
  958. } else {
  959. if (empty($saas_user->name)) {
  960. $saas_user->name = substr_replace($this->phone, '****', 3, 4);
  961. }
  962. if (empty($saas_user->avatar)) {
  963. $saas_user->avatar = \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/avatar.png';
  964. }
  965. if (empty($saas_user->access_token)) {
  966. $saas_user->access_token = \Yii::$app->security->generateRandomString();
  967. }
  968. //$saas_user->platform_open_id = $openid;
  969. $saas_user->ali_user_id = $aplipay_user_id;
  970. // $saas_user->platform_open_id = $this->phone;
  971. $saas_user->save();
  972. }
  973. // 平台登录 todo: 后续补充其他数据
  974. if ($this->store_id == 0) {
  975. return [
  976. 'code' => 0,
  977. 'msg' => '登录成功',
  978. 'data' => [
  979. 'access_token' => $saas_user->access_token,
  980. 'nickname' => $saas_user->name,
  981. 'avatar_url' => $saas_user->avatar,
  982. 'id' => $saas_user->id,
  983. 'money' => $saas_user->share_profit,
  984. 'integral' => $saas_user->integral
  985. ]
  986. ];
  987. }
  988. $user = User::findOne(['binding' => $this->phone, 'store_id' => $this->store_id, 'is_delete' => 0]);
  989. $share = $share_user = null;
  990. $isNew = 0;
  991. if ($user) {
  992. if ($user->blacklist == '1') {
  993. return [
  994. 'code' => 1,
  995. 'msg' => '您的账号已被限制登录!',
  996. ];
  997. }
  998. if ($plat_openid) {
  999. $user->wechat_platform_open_id = $plat_openid;
  1000. $user->save();
  1001. }
  1002. if ($openid) {
  1003. $user->wechat_open_id = $openid;
  1004. $user->save();
  1005. }
  1006. if ($aplipay_user_id) {
  1007. $user->alipay_open_id = $aplipay_user_id;
  1008. $user->save();
  1009. }
  1010. $data = [
  1011. 'access_token' => $saas_user->access_token,
  1012. 'nickname' => $saas_user->name,
  1013. 'avatar_url' => $saas_user->avatar,
  1014. 'is_distributor' => $user->is_distributor ? $user->is_distributor : 0,
  1015. 'errCode' => 0,
  1016. 'id' => $user->id,
  1017. 'is_clerk' => $user->is_clerk === null ? 0 : $user->is_clerk,
  1018. 'integral' => $user->integral === null ? 0 : $user->integral,
  1019. 'money' => $user->money === null ? 0 : $user->money,
  1020. 'binding' => $user->binding,
  1021. 'level' => $user->level,
  1022. 'blacklist' => $user->blacklist,
  1023. 'is_saas_clerk' => (int)$user->is_saas_clerk,
  1024. 'wechat_app_open_id' => $user->wechat_app_open_id,
  1025. 'wechat_platform_open_id' => $user->wechat_platform_open_id,
  1026. 'alipay_open_id' => $user->alipay_open_id,
  1027. 'h5_auth_link' => self::getAuthLink(),
  1028. 'store_id' => $this->store_id,
  1029. ];
  1030. } else {
  1031. $isNew = 1;
  1032. $data = [
  1033. 'nickName' => substr_replace($this->phone, '******', 3, 6),
  1034. 'avatarUrl' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/avatar.png',
  1035. ];
  1036. $user = new User();
  1037. $user->type = User::USER_TYPE_NORMAL;
  1038. $user->binding = $this->phone;
  1039. $user->nickname = $data['nickName'];
  1040. $user->avatar_url = $data['avatarUrl'];
  1041. $user->username = \Yii::$app->security->generateRandomString();
  1042. $user->password = \Yii::$app->security->generatePasswordHash(\Yii::$app->security->generateRandomString(), 5);
  1043. $user->auth_key = \Yii::$app->security->generateRandomString();
  1044. $user->access_token = \Yii::$app->security->generateRandomString();
  1045. $user->is_delete = User::USER_NOT_DELETE;
  1046. $user->store_id = $this->store_id;
  1047. $user->parent_id = empty($login_data['share_user_id']) ? 0 : $login_data['share_user_id'];
  1048. $user->old_parent_id = empty($login_data['share_user_id']) ? 0 : $login_data['share_user_id'];
  1049. $user->platform = 0;//is_alipay_platform() ? User::USER_FROM_ALIPAY : User::USER_FROM_PHONE; // 手机号
  1050. if ($openid) {
  1051. $user->wechat_open_id = $openid;
  1052. }
  1053. if ($aplipay_user_id) {
  1054. $user->alipay_open_id = $aplipay_user_id;
  1055. }
  1056. if ($plat_openid) {
  1057. $user->wechat_platform_open_id = $plat_openid;
  1058. }
  1059. if (!$user->save()) {
  1060. return [
  1061. 'code' => 1,
  1062. 'msg' => '登陆失败',
  1063. 'data' => $user->getErrorSummary(false)[0]
  1064. ];
  1065. }
  1066. $data = [
  1067. 'access_token' => $saas_user->access_token,
  1068. 'nickname' => $saas_user->name,
  1069. 'avatar_url' => $saas_user->avatar,
  1070. 'is_distributor' => $user->is_distributor ? $user->is_distributor : 0,
  1071. 'errCode' => 0,
  1072. 'id' => $user->id,
  1073. 'is_clerk' => $user->is_clerk === null ? 0 : $user->is_clerk,
  1074. 'integral' => $user->integral === null ? 0 : $user->integral,
  1075. 'money' => $user->money === null ? 0 : $user->money,
  1076. 'binding' => $user->binding,
  1077. 'level' => $user->level,
  1078. 'blacklist' => $user->blacklist,
  1079. 'is_saas_clerk' => 0,
  1080. 'wechat_app_open_id' => $user->wechat_app_open_id,
  1081. 'wechat_platform_open_id' => $user->wechat_platform_open_id,
  1082. 'alipay_open_id' => $user->alipay_open_id,
  1083. 'h5_auth_link' => self::getAuthLink(),
  1084. 'store_id' => $saas_user->store_id,
  1085. ];
  1086. }
  1087. if ($this->share_user_id && $this->share_user_id > 0) {
  1088. $bind = new BindForm();
  1089. $bind->user_id = $data['id'];
  1090. $bind->store_id = $this->store_id;
  1091. $bind->isLogin = $this->isLogin;
  1092. $bind->parent_id = $this->share_user_id;
  1093. $bind->condition = 0;
  1094. $bind->isNew = $isNew;
  1095. $res = $bind->save();
  1096. //debug_log([__METHOD__, __LINE__, $res], "app_debug.log");
  1097. }
  1098. return [
  1099. 'code' => 0,
  1100. 'data' => $data,
  1101. 'msg' => '登录成功'
  1102. ];
  1103. }
  1104. public function phoneLoginCodeNew()
  1105. {
  1106. if (!$this->validate()) {
  1107. return [
  1108. 'code' => 1,
  1109. 'msg' => $this->getErrorSummary(false)[0]
  1110. ];
  1111. }
  1112. if ($this->login_type == 1) {
  1113. // $is_password_login = intval(Option::get(OptionSetting::IS_PASSWORD_LOGIN, $this->store_id, 'display', 0)['value']);
  1114. // if ($is_password_login) {
  1115. // return [
  1116. // 'code' => 1,
  1117. // 'msg' => '商城开启密码登录方式,请使用密码登录'
  1118. // ];
  1119. // }
  1120. // 验证码验证
  1121. $result = $this->verifySmsCode(self::CACHE_KEY_SMS_LOGIN);
  1122. if ($result['code'] == 1) {
  1123. return $result;
  1124. }
  1125. } else {
  1126. // $is_password_login = intval(Option::get(OptionSetting::IS_PASSWORD_LOGIN, $this->store_id, 'display', 0)['value']);
  1127. // if (!$is_password_login) {
  1128. // return [
  1129. // 'code' => 1,
  1130. // 'msg' => '商城未开启密码登录方式,请使用验证码登录'
  1131. // ];
  1132. // }
  1133. // 密码验证
  1134. $user = User::find()->where(['store_id' => $this->store_id, 'type' => 1, 'is_delete' => 0, 'binding' => $this->phone])->one();
  1135. if (!$user) {
  1136. return [
  1137. 'code' => 1,
  1138. 'msg' => '用户不存在'
  1139. ];
  1140. }
  1141. if(!preg_match("/^111\d{8}$/", $this->phone)){
  1142. if (false === \Yii::$app->security->validatePassword($this->password, $user->password)) {
  1143. return [
  1144. 'code' => 1,
  1145. 'msg' => '密码错误'
  1146. ];
  1147. }
  1148. }
  1149. }
  1150. ////debug_log('公众号登录01', 'off.log');
  1151. $plat_openid = '';
  1152. $openid = '';
  1153. ////debug_log('公众号登录02_code=' . $this->code, 'off.log');
  1154. if ($this->code) {
  1155. try {
  1156. ////debug_log('公众号登录03', 'off.log');
  1157. if (is_wechat_platform()) {
  1158. // $wechat = self::getWechat();
  1159. $wechat = WechatMini::getWechatConfig(0, 0, 0, 0, 1);
  1160. if (is_h5()) {
  1161. ////debug_log('公众号登录04', 'off.log');
  1162. $session = $wechat->oauth->userFromCode($this->code)->getTokenResponse();
  1163. $plat_openid = $session['openid'];
  1164. ////debug_log('公众号登录05'. json_encode($session, JSON_UNESCAPED_UNICODE), 'off.log');
  1165. } else {
  1166. $app = WechatMini::getWechatConfig(0, 0, 0, 0, 1);
  1167. if (empty($app)) {
  1168. return [
  1169. 'code' => 1,
  1170. 'msg' => '小程序参数获取失败!',
  1171. ];
  1172. }
  1173. $session = $app->auth->session($this->code);
  1174. if (empty($session)) {
  1175. return [
  1176. 'code' => 1,
  1177. 'msg' => '小程序参数异常!',
  1178. ];
  1179. }
  1180. if (!$session || empty($session['openid'])) {
  1181. throw new \Exception('获取openid失败.');
  1182. }
  1183. $openid = $session['openid'];
  1184. }
  1185. } elseif (is_alipay_platform()) {
  1186. }
  1187. } catch (\Exception $e) {
  1188. return [
  1189. 'code' => 1,
  1190. 'msg' => $e->getMessage(),
  1191. ];
  1192. //debug_log('公众号登录06' . $e->getMessage(), 'off.log');
  1193. }
  1194. ////debug_log('公众号登录07', 'off.log');
  1195. }
  1196. ////debug_log('公众号登录08', 'off.log');
  1197. // 创建平台会员saas_user
  1198. $saas_user = SaasUser::find()->where(['mobile' => $this->phone, 'is_delete' => SaasUser::DELETE_STATUS_FALSE])->one();
  1199. if (!$saas_user) {
  1200. $saas_user = new SaasUser();
  1201. $saas_user->access_token = \Yii::$app->security->generateRandomString();
  1202. $saas_user->name = substr_replace($this->phone, '******', 3, 6);
  1203. $saas_user->mobile = $this->phone;
  1204. $saas_user->platform_open_id = $openid;
  1205. $saas_user->avatar = \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/avatar.png';
  1206. $saas_user->store_id = $this->store_id;
  1207. $saas_user->save();
  1208. } else {
  1209. if (empty($saas_user->name)) {
  1210. $saas_user->name = substr_replace($this->phone, '****', 3, 4);
  1211. }
  1212. if (empty($saas_user->avatar)) {
  1213. $saas_user->avatar = \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/avatar.png';
  1214. }
  1215. if (empty($saas_user->access_token)) {
  1216. $saas_user->access_token = \Yii::$app->security->generateRandomString();
  1217. }
  1218. $saas_user->platform_open_id = $openid;
  1219. // $saas_user->platform_open_id = $this->phone;
  1220. $saas_user->save();
  1221. }
  1222. // 平台登录 todo: 后续补充其他数据
  1223. if ($this->store_id == 0) {
  1224. return [
  1225. 'code' => 0,
  1226. 'msg' => '登录成功',
  1227. 'data' => [
  1228. 'access_token' => $saas_user->access_token,
  1229. 'nickname' => $saas_user->name,
  1230. 'avatar_url' => $saas_user->avatar,
  1231. 'id' => $saas_user->id,
  1232. 'money' => $saas_user->share_profit,
  1233. 'integral' => $saas_user->integral
  1234. ]
  1235. ];
  1236. }
  1237. $user = User::findOne(['binding' => $this->phone, 'store_id' => $this->store_id, 'is_delete' => 0]);
  1238. $share = $share_user = null;
  1239. $isNew = 0;
  1240. if ($user) {
  1241. if ($user->blacklist == '1') {
  1242. return [
  1243. 'code' => 1,
  1244. 'msg' => '您的账号已被限制登录!',
  1245. ];
  1246. }
  1247. if ($plat_openid) {
  1248. $user->wechat_platform_open_id = $plat_openid;
  1249. $user->save();
  1250. }
  1251. if ($openid) {
  1252. $user->wechat_open_id = $openid;
  1253. $user->save();
  1254. }
  1255. $data = [
  1256. 'access_token' => $saas_user->access_token,
  1257. 'nickname' => $saas_user->name,
  1258. 'avatar_url' => $saas_user->avatar,
  1259. 'is_distributor' => $user->is_distributor ? $user->is_distributor : 0,
  1260. 'errCode' => 0,
  1261. 'id' => $user->id,
  1262. 'is_clerk' => $user->is_clerk === null ? 0 : $user->is_clerk,
  1263. 'integral' => $user->integral === null ? 0 : $user->integral,
  1264. 'money' => $user->money === null ? 0 : $user->money,
  1265. 'binding' => $user->binding,
  1266. 'level' => $user->level,
  1267. 'blacklist' => $user->blacklist,
  1268. 'is_saas_clerk' => (int)$user->is_saas_clerk,
  1269. 'wechat_app_open_id' => $user->wechat_app_open_id,
  1270. 'wechat_platform_open_id' => $user->wechat_platform_open_id,
  1271. 'h5_auth_link' => self::getAuthLink(),
  1272. 'store_id' => $this->store_id
  1273. ];
  1274. } else {
  1275. $isNew = 1;
  1276. $data = [
  1277. 'nickName' => substr_replace($this->phone, '******', 3, 6),
  1278. 'avatarUrl' => \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/avatar.png',
  1279. ];
  1280. $user = new User();
  1281. $user->type = User::USER_TYPE_NORMAL;
  1282. $user->binding = $this->phone;
  1283. $user->nickname = $data['nickName'];
  1284. $user->avatar_url = $data['avatarUrl'];
  1285. $user->username = \Yii::$app->security->generateRandomString();
  1286. $user->password = \Yii::$app->security->generatePasswordHash(\Yii::$app->security->generateRandomString(), 5);
  1287. // $user->password = \Yii::$app->security->generatePasswordHash("123456");
  1288. $user->auth_key = \Yii::$app->security->generateRandomString();
  1289. $user->access_token = \Yii::$app->security->generateRandomString();
  1290. $user->is_delete = User::USER_NOT_DELETE;
  1291. $user->store_id = $this->store_id;
  1292. $user->parent_id = 0;
  1293. $user->old_parent_id = 0;
  1294. $user->platform = is_alipay_platform() ? User::USER_FROM_ALIPAY : User::USER_FROM_PHONE; // 手机号
  1295. if ($openid) {
  1296. $user->wechat_open_id = $openid;
  1297. }
  1298. if ($plat_openid) {
  1299. $user->wechat_platform_open_id = $plat_openid;
  1300. }
  1301. if (!$user->save()) {
  1302. return [
  1303. 'code' => 1,
  1304. 'msg' => '登陆失败',
  1305. 'data' => $user->getErrorSummary(false)[0]
  1306. ];
  1307. }
  1308. $data = [
  1309. 'access_token' => $saas_user->access_token,
  1310. 'nickname' => $saas_user->name,
  1311. 'avatar_url' => $saas_user->avatar,
  1312. 'is_distributor' => $user->is_distributor ? $user->is_distributor : 0,
  1313. 'errCode' => 0,
  1314. 'id' => $user->id,
  1315. 'is_clerk' => $user->is_clerk === null ? 0 : $user->is_clerk,
  1316. 'integral' => $user->integral === null ? 0 : $user->integral,
  1317. 'money' => $user->money === null ? 0 : $user->money,
  1318. 'binding' => $user->binding,
  1319. 'level' => $user->level,
  1320. 'blacklist' => $user->blacklist,
  1321. 'is_saas_clerk' => 0,
  1322. 'wechat_app_open_id' => $user->wechat_app_open_id,
  1323. 'wechat_platform_open_id' => $user->wechat_platform_open_id,
  1324. 'alipay_open_id' => $user->alipay_open_id,
  1325. 'h5_auth_link' => self::getAuthLink(),
  1326. 'store_id' => $saas_user->store_id
  1327. ];
  1328. }
  1329. if ($this->share_user_id && $this->share_user_id > 0) {
  1330. $bind = new BindForm();
  1331. $bind->user_id = $data['id'];
  1332. $bind->store_id = $this->store_id;
  1333. $bind->isLogin = $this->isLogin;
  1334. $bind->parent_id = $this->share_user_id;
  1335. $bind->condition = 0;
  1336. $bind->isNew = $isNew;
  1337. $res = $bind->save();
  1338. //debug_log([__METHOD__, __LINE__, $res], "app_debug.log");
  1339. }
  1340. return [
  1341. 'code' => 0,
  1342. 'data' => $data,
  1343. 'msg' => '登录成功'
  1344. ];
  1345. }
  1346. /**
  1347. * 验证验证码
  1348. * @param string $key
  1349. * @return array
  1350. */
  1351. public function verifySmsCode($key)
  1352. {
  1353. $smsCode = cache()->get($this->phone . $key . $this->store_id);
  1354. //debug_log([__METHOD__, __LINE__, "KEY:".$this->phone . $key . $this->store_id.',code:'.$smsCode], "app_debug.log");
  1355. if (!$smsCode) {
  1356. return [
  1357. 'code' => 1,
  1358. 'msg' => '验证码错误',
  1359. ];
  1360. }
  1361. if (strval($this->verify_code) !== strval($smsCode)) {
  1362. return [
  1363. 'code' => 1,
  1364. 'msg' => '验证码错误',
  1365. ];
  1366. }
  1367. cache()->delete($this->phone . $key . $this->store_id);
  1368. return [
  1369. 'code' => 0,
  1370. 'msg' => '验证成功',
  1371. ];
  1372. }
  1373. /**
  1374. * 返回默认头像
  1375. */
  1376. protected function getDefaultAvatar()
  1377. {
  1378. return \Yii::$app->request->hostInfo . '/web/v1/statics/images/avatar.png';
  1379. }
  1380. /**
  1381. * 返回默认用户名
  1382. */
  1383. protected function getDefaultNickname($phone)
  1384. {
  1385. $randStr = \Yii::$app->security->generateRandomString(5);
  1386. return $randStr . '_' . substr($phone, -4, 4);
  1387. }
  1388. /**
  1389. * 发送验证码
  1390. * @param string $key
  1391. * @return array
  1392. */
  1393. public function sendCode($key)
  1394. {
  1395. $is_debug=Option::get('is_debug', 0, 'saas', 0)['value'];
  1396. if(preg_match("/^111\d{8}$/", $this->phone) && $is_debug == 1){
  1397. $sms_code = 999999;
  1398. // 验证码有效期5分钟
  1399. cache()->set($this->phone . $key . $this->store_id, $sms_code, 600);
  1400. //debug_log([__METHOD__, __LINE__, "KEY:".$this->phone . $key . $this->store_id.',code:'.$sms_code], "app_debug.log");
  1401. return [
  1402. 'code' => 0,
  1403. 'msg' => '发送成功',
  1404. ];
  1405. }
  1406. else{
  1407. if (!$this->phone || !preg_match("/^1[3456789]\d{9}$/", $this->phone)) {
  1408. return [
  1409. 'code' => 1,
  1410. 'msg' => '参数不正确2',
  1411. ];
  1412. }
  1413. }
  1414. $sms_code = mt_rand(100000, 999999);
  1415. $sendResult = NoticeSend::VerifyCode($this->phone, $sms_code,true);
  1416. if ($sendResult['code'] == 1) {
  1417. return $sendResult;
  1418. }
  1419. // 验证码有效期5分钟
  1420. cache()->set($this->phone . $key . $this->store_id, $sms_code, 600);
  1421. return [
  1422. 'code' => 0,
  1423. 'msg' => '发送成功',
  1424. ];
  1425. }
  1426. /**
  1427. * 绑定手机号
  1428. * @return array
  1429. */
  1430. public function bindPhone()
  1431. {
  1432. if (!trim($this->phone) || !$this->verify_code || !preg_match("/^1[3456789]\d{9}$/", $this->phone)) {
  1433. return [
  1434. 'code' => 1,
  1435. 'msg' => '参数不正确',
  1436. ];
  1437. }
  1438. $user = User::find()->where([
  1439. 'store_id' => $this->store_id,
  1440. 'type' => 1,
  1441. 'is_delete' => 0,
  1442. 'blacklist' => 0,
  1443. 'binding' => $this->phone
  1444. ])->one();
  1445. if ($user) {
  1446. return [
  1447. 'code' => 1,
  1448. 'msg' => '该手机号已经被绑定',
  1449. ];
  1450. }
  1451. // 验证码验证
  1452. $result = $this->verifySmsCode(self::CACHE_KEY_BIND_PHONE);
  1453. if ($result['code'] == 1) {
  1454. return $result;
  1455. }
  1456. $this->user->binding = trim($this->phone);
  1457. if ($this->user->save()) {
  1458. return [
  1459. 'code' => 0,
  1460. 'msg' => '绑定成功',
  1461. ];
  1462. } else {
  1463. foreach ($this->user->errors as $error) {
  1464. return [
  1465. 'code' => 1,
  1466. 'msg' => $error
  1467. ];
  1468. }
  1469. }
  1470. }
  1471. /**
  1472. * 微信浏览器h5授权链接
  1473. */
  1474. public static function getAuthLink()
  1475. {
  1476. $redirect_url = \Yii::$app->request->hostInfo . '/h5/#/user/my/setting';
  1477. return getAuthLink(get_store_id(), $redirect_url, 1);
  1478. }
  1479. // 微信公众号获取openid
  1480. public function getWeixinOpenid()
  1481. {
  1482. /*
  1483. AppID:wx46115cd199a3ad95
  1484. AppSecret:3e3dc22f7ec78d0ae9e7b2b0118ceff8
  1485. */
  1486. // $keys = [
  1487. // 'wechat_platform'
  1488. // ];
  1489. // $data = Option::get($keys, 0, 'saas');
  1490. // $wechat_config = json_decode($data[0]['value'], true);
  1491. // // 你的微信公众号AppID和AppSecret
  1492. // $appid = $wechat_config['appid'];
  1493. // $appsecret = $wechat_config['appsecret'];
  1494. $appid = 'wx46115cd199a3ad95';
  1495. $appsecret = '3e3dc22f7ec78d0ae9e7b2b0118ceff8';
  1496. // 请求URL
  1497. $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" . $appid . "&secret=" . $appsecret . "&code=" . $this->code . "&grant_type=authorization_code";
  1498. // 获取access_token和openid
  1499. $response = file_get_contents($url);
  1500. $refdata = json_decode($response, true);
  1501. if (isset($refdata['access_token']) && isset($refdata['openid'])) {
  1502. $openid = $refdata['openid'];
  1503. $data['openid']=$openid;
  1504. return [
  1505. 'code' => 0,
  1506. 'data' => $data,
  1507. 'msg' => '登录成功',
  1508. 'othen' => $refdata,
  1509. ];
  1510. } else {
  1511. return ['code' => 1, 'msg' => '参数不正确'];
  1512. }
  1513. }
  1514. // 微信小程序获取openid 需要带入store_id 默认=-1
  1515. public function getWeixinMiniOpenid($mobile='')
  1516. {
  1517. if ($this->code) {
  1518. try {
  1519. if (empty($this->store_id)) {
  1520. $this->store_id = -1;
  1521. }
  1522. $app = WechatMini::getWechatConfig($this->store_id);
  1523. if (empty($app)) {
  1524. if (is_isv()) {
  1525. $wechat_config = WechatConfig::find()->where(['store_id' => $this->store_id, 'type' => 1])->select(['mch_id','app_id'])->asArray()->one();
  1526. //小程序登陆获取openid
  1527. $app = (new NewMerchantForm())->miniProgram($wechat_config['app_id']);
  1528. }
  1529. if (empty($app)) {
  1530. return [
  1531. 'code' => 1,
  1532. 'msg' => '小程序参数获取失败!',
  1533. ];
  1534. }
  1535. }
  1536. $session = $app->auth->session($this->code);
  1537. if (empty($session)) {
  1538. return [
  1539. 'code' => 1,
  1540. 'msg' => '小程序参数异常!',
  1541. ];
  1542. }
  1543. if (!$session || empty($session['openid'])) {
  1544. throw new \Exception('获取openid失败.');
  1545. }
  1546. $data['openid'] = $session['openid'];
  1547. //如果店铺id=-1
  1548. if($this->store_id=-1){
  1549. if (!empty($mobile) && !empty($data['openid'])) {
  1550. $saas_user = SaasUser::findOne(['mobile' => $mobile]);
  1551. if ($saas_user) {
  1552. $saas_user->platform_open_id = $data['openid'];
  1553. $saas_user->save();
  1554. }
  1555. }
  1556. }
  1557. } catch (\Exception $e) {
  1558. return [
  1559. 'code' => 1,
  1560. 'msg' => $e->getMessage(),
  1561. ];
  1562. }
  1563. }
  1564. return [
  1565. 'code' => 0,
  1566. 'data' => $data,
  1567. 'msg' => '登录成功',
  1568. 'othen' => $session,
  1569. ];
  1570. }
  1571. // 微信小程序获取openid 需要带入store_id 默认=-1(本获取openid来分享商品二维码)
  1572. public function getWeixinMiniOpenidShareCode($mobile='')
  1573. {
  1574. if ($this->code) {
  1575. try {
  1576. if (empty($this->store_id)) {
  1577. $this->store_id = -1;
  1578. }
  1579. $app = WechatMini::getWechatConfig($this->store_id);
  1580. if (empty($app)) {
  1581. return [
  1582. 'code' => 1,
  1583. 'msg' => '小程序参数获取失败!',
  1584. ];
  1585. }
  1586. $session = $app->auth->session($this->code);
  1587. if (empty($session)) {
  1588. return [
  1589. 'code' => 1,
  1590. 'msg' => '小程序参数异常!',
  1591. ];
  1592. }
  1593. if (!$session || empty($session['openid'])) {
  1594. throw new \Exception('获取openid失败.');
  1595. }
  1596. $data['openid'] = $session['openid'];
  1597. return [
  1598. 'code' => 0,
  1599. 'data' => $data,
  1600. 'msg' => '获取成功',
  1601. ];
  1602. } catch (\Exception $e) {
  1603. return [
  1604. 'code' => 1,
  1605. 'msg' => $e->getMessage(),
  1606. ];
  1607. }
  1608. }
  1609. }
  1610. // 支付宝小程序获取userid
  1611. public function getAlipayMiniUserid($authCode)
  1612. {
  1613. if ($authCode) {
  1614. try {
  1615. $keys = [
  1616. 'alipay_platform'
  1617. ];
  1618. $data = Option::get($keys, 0, 'saas');
  1619. $alipay_config = json_decode($data[0]['value'], true);
  1620. // 获取user_id
  1621. $config = [
  1622. 'alipay' => [
  1623. 'client_id' => $alipay_config['app_id'],
  1624. 'rsa_private_key' => $alipay_config['app_private_key'],
  1625. ]
  1626. ];
  1627. //获取支付宝用户信息
  1628. $socialite = new SocialiteManager($config);
  1629. $token = $socialite->create('alipay')->tokenFromCode($authCode);
  1630. // if (empty($token['user_id'])) {
  1631. if (empty($token['user_id']) && empty($token['open_id'])) {
  1632. return [
  1633. 'code' => 1,
  1634. 'msg' => '获取user_id失败'
  1635. ];
  1636. }
  1637. // $aplipay_user_id = isset($token['user_id']) ? $token['user_id'] : $token['open_id'];
  1638. $dataref['openid'] = isset($token['user_id']) ? $token['user_id'] : '';
  1639. } catch (\Exception $e) {
  1640. return [
  1641. 'code' => 1,
  1642. 'msg' => $e->getMessage(),
  1643. ];
  1644. }
  1645. }
  1646. return [
  1647. 'code' => 0,
  1648. 'data' => $dataref,
  1649. 'msg' => '登录成功',
  1650. 'othen' => $token,
  1651. ];
  1652. } //生成token
  1653. public function setToken($id, $type = 0) {
  1654. if ($type) {
  1655. $admin_type = Admin::ADMIN_TYPE_STORE;
  1656. $store_admin_id = 0;
  1657. } else {
  1658. $store_admin = StoreAdmin::findOne($id);
  1659. $admin_type = Admin::ADMIN_TYPE_MINI_ADMIN;
  1660. $store_admin_id = $store_admin->id;
  1661. }
  1662. $admin = Admin::findOne(['type' => $admin_type, 'type_id' => $id, 'is_delete' => 0]);
  1663. $time = time();
  1664. $payload = [
  1665. 'iss' => \Yii::$app->request->hostInfo,
  1666. 'iat' => $time,
  1667. 'exp' => $time + 86400,
  1668. 'admin_id' => $admin->id,
  1669. 'username' => $admin->username,
  1670. 'store_admin_id' => $store_admin_id
  1671. ];
  1672. return \Yii::$app->jwt->createToken($payload);
  1673. }
  1674. // 后台用户关联商城token
  1675. public function getRelatedToken($saasUserId,$mobile,$storeId)
  1676. {
  1677. $where = [];
  1678. // 判断 saasuserid 是否为空并添加到查询条件中
  1679. if (!empty($saasUserId)) {
  1680. $where['id'] = $saasUserId;
  1681. }
  1682. // 判断 mobile 是否为空并添加到查询条件中
  1683. if (!empty($mobile)) {
  1684. $where['mobile'] = $mobile;
  1685. }
  1686. // 查找符合条件的 SaasUser
  1687. $saas_user = SaasUser::find()->where($where)->andWhere(['is_delete' => SaasUser::DELETE_STATUS_FALSE])->one();
  1688. // 判断是否找到符合条件的用户
  1689. if (!$saas_user) {
  1690. // 处理未找到用户的情况
  1691. return [
  1692. 'code' => 1,
  1693. 'msg' => '未找到当前用户'
  1694. ];
  1695. }
  1696. $user = User::findOne(['binding' => $mobile, 'store_id' => $storeId, 'is_delete' => 0]);
  1697. if (!$user) {
  1698. // 处理未找到用户的情况
  1699. return [
  1700. 'code' => 1,
  1701. 'msg' => '未找到当前用户'
  1702. ];
  1703. }
  1704. // 是否开启强制绑定手机号
  1705. $is_open_bind = Option::get(OptionSetting::STORE_LOGIN_FORCIBLY_BIND_MOBILE, $this->store_id, 'store')['value'];
  1706. $share = $share_user = null;
  1707. if ($user->parent_id > 0) {
  1708. $share = Share::findOne(['user_id' => $user->parent_id]);
  1709. $share_user = User::findOne(['id' => $share->user_id]);
  1710. }
  1711. $data = [
  1712. // 'access_token' => $user->access_token,
  1713. // 'nickname' => $user->nickname,
  1714. // 'avatar_url' => $user->avatar_url,
  1715. 'access_token' => $saas_user->access_token,
  1716. 'nickname' => $saas_user->name,
  1717. 'avatar_url' => $saas_user->avatar,
  1718. 'is_distributor' => $user->is_distributor ? $user->is_distributor : 0,
  1719. 'errCode' => 0,
  1720. 'parent' => $share ? ($share->name ? $share->name : $share_user->nickname) : '总店',
  1721. 'id' => $user->id,
  1722. 'is_clerk' => $user->is_clerk === null ? 0 : $user->is_clerk,
  1723. 'integral' => $user->integral === null ? 0 : $user->integral,
  1724. 'money' => $user->money === null ? 0 : $user->money,
  1725. 'binding' => $user->binding,
  1726. 'level' => $user->level,
  1727. 'blacklist' => $user->blacklist,
  1728. 'is_open_bind' => $is_open_bind ? $is_open_bind : 0,
  1729. 'store_id' => $saas_user->store_id,
  1730. ];
  1731. return [
  1732. 'code' => 0,
  1733. 'msg' => '获取成功',
  1734. 'data' => $data
  1735. ];
  1736. }
  1737. }