AuthLoginForm.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\alliance\models;
  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\modules\alliance\models\ApiModel;
  15. use EasyWeChat\Factory;
  16. use yii\base\BaseObject;
  17. use app\models\StoreCloud;
  18. class AuthLoginForm extends ApiModel
  19. {
  20. // 商城id
  21. public $store_id;
  22. //绑定店铺来源
  23. public $store_id_first;
  24. public $password; // 密码
  25. // 微信小程序
  26. public $code;
  27. public $encryptedData;
  28. public $iv;
  29. public $mini_type;
  30. // 分销商user_id
  31. public $share_user_id;
  32. // 微信小程序
  33. const PLATFORM_WX = 'wx';
  34. // 微信公众号
  35. const PLATFORM_MP = 'mp';
  36. // app端微信
  37. const PLATFORM_APP = 'app';
  38. // 手机号注册登录
  39. const PLATFORM_PHONE = 'phone';
  40. public $_platform;
  41. public $platform; // wechat(微信), alipay(支付宝), douyin(抖音)
  42. public $nickname;
  43. public $avatar_url;
  44. /**
  45. * @var \EasyWeChat\MiniProgram\Application
  46. */
  47. public $wechat;
  48. public function rules()
  49. {
  50. return [
  51. ['_platform', 'in', 'range' => [self::PLATFORM_WX, self::PLATFORM_MP, self::PLATFORM_APP, self::PLATFORM_PHONE]],
  52. ['_platform', 'required'],
  53. [['share_user_id', 'store_id', 'store_id_first'], 'integer'],
  54. [['code', 'encryptedData', 'iv', 'platform', 'mini_type', 'avatar_url', 'nickname'], 'string'],
  55. [['code', 'encryptedData', 'iv', 'password'], 'trim'],
  56. ];
  57. }
  58. public function editPassword()
  59. {
  60. $user = get_saas_user();
  61. if (!$user) {
  62. return [
  63. 'code' => 1,
  64. 'msg' => '用户不存在',
  65. ];
  66. }
  67. $user = User::find()->where(['store_id' => -1, 'is_delete' => 0, 'binding' => $user->mobile])->one();
  68. if (!$user) {
  69. return [
  70. 'code' => 1,
  71. 'msg' => '用户不存在',
  72. ];
  73. }
  74. $user->password = \Yii::$app->security->generatePasswordHash($this->password);
  75. if ($user->save()) {
  76. return [
  77. 'code' => 0,
  78. 'msg' => '修改成功',
  79. ];
  80. } else {
  81. return [
  82. 'code' => 1,
  83. 'msg' => '修改失败',
  84. ];
  85. }
  86. }
  87. public function platformPhoneAuth()
  88. {
  89. try {
  90. if (is_isv()) {
  91. $config = [
  92. 'app_id' => Option::get("platform_third_appid",0,'saas')['value'],
  93. 'secret' => Option::get("platform_third_secret",0,'saas')['value'],
  94. 'token' => Option::get("platform_token",0,'saas')['value'],
  95. 'aes_key' => Option::get("platform_encodingAesKey",0,'saas')['value']
  96. ];
  97. $openPlatform = Factory::openPlatform($config);
  98. if (!empty(get_mini_id())) {
  99. $mini = StoreMini::findOne(get_mini_id());
  100. $app = $openPlatform->miniProgram($mini->appid,$mini->authorizer_refresh_token);
  101. $session = $app->auth->session($this->code);
  102. $decryptedData = $app->encryptor->decryptData($session['session_key'], $this->iv, $this->encryptedData);
  103. } else {
  104. return [
  105. 'code' => 1,
  106. 'msg' => '登录异常'
  107. ];
  108. }
  109. } elseif ($this->platform == 'bytedance') {
  110. /**
  111. * @var \ByteDance\MiniProgram\Application $byteDance
  112. */
  113. $byteDance = \Yii::$app->controller->byteDance;
  114. if (!$byteDance) {
  115. return [
  116. 'code' => 1,
  117. 'msg' => '登录异常'
  118. ];
  119. }
  120. $session = $byteDance->auth->session($this->code);
  121. $decryptedData = $byteDance->encryptor->decryptData($session['session_key'], $this->iv, $this->encryptedData);
  122. } else {
  123. if ($this->mini_type) {
  124. $wechat = Option::getSaasPlatformMchWechat();
  125. } else {
  126. $wechat = Option::getSaasPlatformWechat();
  127. }
  128. $config = [
  129. 'app_id' => $wechat['appid'],
  130. 'secret' => $wechat['key'],
  131. 'response_type' => 'array'
  132. ];
  133. $this->wechat = Factory::miniProgram($config);
  134. if (!$this->wechat) {
  135. return [
  136. 'code' => 1,
  137. 'msg' => '登录异常'
  138. ];
  139. }
  140. $session = $this->wechat->auth->session($this->code);
  141. $decryptedData = $this->wechat->encryptor->decryptData($session['session_key'], $this->iv, $this->encryptedData);
  142. }
  143. if (isset($decryptedData['phoneNumber']) && !empty($decryptedData['phoneNumber'])) {
  144. // 创建平台会员saas_user
  145. $saas_user = SaasUser::find()->where(['mobile' => $decryptedData['phoneNumber'], 'is_delete' => SaasUser::DELETE_STATUS_FALSE])->one();
  146. if (!$saas_user) {
  147. // 针对同一个微信号使用不同的手机号问题,直接更新最新的手机号
  148. if ($session['openid']) {
  149. if(is_merchant()){
  150. $saas_user = SaasUser::findOne(['platform_open_id_merchant' => $session['openid'], 'is_delete' => SaasUser::DELETE_STATUS_FALSE]);
  151. }else{
  152. $saas_user = SaasUser::findOne(['platform_open_id' => $session['openid'], 'is_delete' => SaasUser::DELETE_STATUS_FALSE]);
  153. }
  154. }
  155. if ($saas_user) {
  156. $saas_user->mobile = $decryptedData['phoneNumber'];
  157. if (empty($saas_user->name)) {
  158. $saas_user->name = substr_replace($decryptedData['phoneNumber'], '****', 3, 4);
  159. }
  160. if (empty($saas_user->avatar)) {
  161. $saas_user->avatar = \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/avatar.png';
  162. }
  163. if (empty($saas_user->access_token)) {
  164. $saas_user->access_token = \Yii::$app->security->generateRandomString();
  165. }
  166. if($this->store_id_first > 0){
  167. $saas_user->store_id = $this->store_id_first;
  168. }
  169. if(is_merchant()){
  170. $saas_user->platform_open_id_merchant = $session['openid'];
  171. }else{
  172. $saas_user->platform_open_id = $session['openid'];
  173. }
  174. $saas_user->save();
  175. } else {
  176. $saas_user = new SaasUser();
  177. $saas_user->price = '0.00';
  178. $saas_user->access_token = \Yii::$app->security->generateRandomString();
  179. $saas_user->name = substr_replace($decryptedData['phoneNumber'], '******', 3, 6);
  180. $saas_user->mobile = $decryptedData['phoneNumber'];
  181. if ($this->platform == 'bytedance') {
  182. $saas_user->bytedance_open_id = $session['openid'];
  183. } else {
  184. if(is_merchant()){
  185. $saas_user->platform_open_id_merchant = $session['openid'];
  186. }else{
  187. $saas_user->platform_open_id = $session['openid'];
  188. }
  189. }
  190. $saas_user->avatar = \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/avatar.png';
  191. $saas_user->store_id = $this->store_id;
  192. if($this->store_id_first > 0){
  193. $saas_user->store_id = $this->store_id_first;
  194. }
  195. $saas_user->save();
  196. }
  197. } else {
  198. if (empty($saas_user->name)) {
  199. $saas_user->name = substr_replace($decryptedData['phoneNumber'], '****', 3, 4);
  200. }
  201. if (empty($saas_user->avatar)) {
  202. $saas_user->avatar = \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl . '/web/v1/statics/images/avatar.png';
  203. }
  204. if (empty($saas_user->access_token)) {
  205. $saas_user->access_token = \Yii::$app->security->generateRandomString();
  206. }
  207. if ($this->platform == 'bytedance') {
  208. $saas_user->bytedance_open_id = $session['openid'];
  209. } else {
  210. if(is_merchant()){
  211. $saas_user->platform_open_id_merchant = $session['openid'];
  212. }else{
  213. $saas_user->platform_open_id = $session['openid'];
  214. }
  215. }
  216. $saas_user->save();
  217. }
  218. //绑定上下级
  219. if ($this->share_user_id && $this->share_user_id > 0) {
  220. $saas_user->parent_id = $this->share_user_id;
  221. $saas_user->save();
  222. }
  223. if($this->nickname || $this->avatar_url){
  224. $saas_user->name = $this->nickname;
  225. $saas_user->avatar = $this->avatar_url;
  226. if (!$saas_user->save()) {
  227. throw new \Exception(json_encode($saas_user->errors));
  228. }
  229. }
  230. $storeCloud = StoreCloud::find()
  231. ->where(['is_delete' => 0, 'is_enable' => 1, 'saas_user_id' => $saas_user->id])
  232. ->one();
  233. // 平台登录 todo: 后续补充其他数据
  234. return [
  235. 'code' => 0,
  236. 'msg' => '登录成功',
  237. 'data' => [
  238. 'access_token' => $saas_user->access_token,
  239. 'nickname' => $saas_user->name,
  240. 'avatar_url' => $saas_user->avatar,
  241. 'id' => $saas_user->id,
  242. 'money' => $saas_user->share_profit,
  243. 'integral' => $saas_user->integral,
  244. 'is_can_distribution' => $storeCloud && $storeCloud->store_id > 0 ? 1 : 0, // 是否有商城(是否可以铺货)
  245. 'mobile' => $saas_user->mobile
  246. ]
  247. ];
  248. } else {
  249. return [
  250. 'code' => 1,
  251. 'msg' => '登录失败',
  252. ];
  253. }
  254. } catch (\Exception $e) {
  255. \Yii::error($e->getMessage() . ' file => ' . $e->getFile() . ' line => ' . $e->getLine());
  256. return [
  257. 'code' => 1,
  258. 'msg' => '登录失败',
  259. ];
  260. }
  261. }
  262. public function code() {
  263. $data = [
  264. 'avatar_url' => '',
  265. 'nickname' => '',
  266. ];
  267. try {
  268. if (is_merchant()) {
  269. $wechat = Option::getSaasPlatformMchWechat();
  270. } else {
  271. $wechat = Option::getSaasPlatformWechat();
  272. }
  273. $config = [
  274. 'app_id' => $wechat['appid'],
  275. 'secret' => $wechat['key'],
  276. 'response_type' => 'array'
  277. ];
  278. $this->wechat = Factory::miniProgram($config);
  279. if (!$this->wechat) {
  280. return [
  281. 'code' => 1,
  282. 'msg' => '登录异常'
  283. ];
  284. }
  285. $session = $this->wechat->auth->session($this->code);
  286. //获取session
  287. // $session = self::getWechat()->auth->session($this->code);
  288. if (!$session || empty($session['openid'])) {
  289. throw new \Exception('获取openid失败.');
  290. }
  291. $openid = $session['openid'];
  292. if(is_merchant()){
  293. $saas_user = SaasUser::findOne(['platform_open_id_merchant' => $openid]);
  294. }else{
  295. $saas_user = SaasUser::findOne(['platform_open_id' => $openid]);
  296. }
  297. if ($saas_user) {
  298. $data['avatar_url'] = $saas_user->avatar;
  299. $data['nickname'] = $saas_user->name;
  300. $data['id'] = $saas_user->id;
  301. }
  302. return [
  303. 'code' => 0,
  304. 'data' => $data,
  305. 'msg' => 'ok'
  306. ];
  307. } catch (\Exception $e) {
  308. \Yii::error([__METHOD__, $e]);
  309. return [
  310. 'code' => 0,
  311. 'msg' => 'error',
  312. 'data' => $data,
  313. ];
  314. }
  315. }
  316. }