| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\client\models\v1;
- use app\models\Option;
- use app\models\SaasUser;
- use app\models\User;
- use app\models\WechatConfig;
- use app\modules\client\models\ApiModel;
- use EasyWeChat\Factory;
- use Think\Model;
- class PlatformLoginForm extends ApiModel
- {
- public $store_id;
- public $phone;
- // 微信小程序
- public $code;
- public $encryptedData;
- public $iv;
- public function rules()
- {
- return [
- [['code', 'encryptedData', 'iv', 'phone'], 'string'],
- [['code', 'encryptedData', 'iv'], 'trim'],
- [['store_id'], 'integer'],
- ];
- }
- public function login() {
- if (!$this->validate()) {
- return [
- 'code' => 1,
- 'msg' => $this->getErrorSummary(false)[0],
- ];
- }
- // 获取平台微信配置进行初始化
- $wechat_config = Option::getSaasWechat();
- // $config = [
- // 'app_id' => $wechat_config['sp_appid'],
- // 'secret' => $wechat_config['sp_key'],
- // 'response_type' => 'array'
- // ];
- $config = [
- 'app_id' => 'wx44a1c390fb6fbfdd',
- 'secret' => 'b39f12cac61ad5bf63506058752d5f49',
- 'response_type' => 'array'
- ];
- \Yii::error($config);
- $wechat = Factory::miniProgram($config);
- if (!$wechat) {
- throw new \Exception('Wechat App Is Null', 1);
- }
- $session = $wechat->auth->session($this->code);
- if (!$session || empty($session['openid'])) {
- throw new \Exception('获取openid失败.');
- }
- $saas_user = SaasUser::findOne(['mobile' => $this->phone]);
- if (!$saas_user) {
- $saas_user = new SaasUser();
- $saas_user->store_id = $this->store_id;
- $saas_user->mobile = $this->phone;
- $saas_user->platform_open_id = $session['openid'];
- } else {
- $saas_user->platform_open_id = $session['openid'];
- }
- if (!$saas_user->save()) {
- return [
- 'code' => 1,
- 'msg' => $saas_user->errors[0]
- ];
- }
- return [
- 'code' => 0,
- 'msg' => 'success'
- ];
- }
- }
|