auth->session($this->code); $decryptedData = self::getWechat()->encryptor->decryptData($session['session_key'], $this->iv, $this->encryptedData); $user = User::findOne(['id' => get_user_id()]); $user->binding = $decryptedData['phoneNumber']; if (!$user->save()) { throw new Exception("保存失败"); } } catch (\Exception $e) { return [ 'code' => 1, 'msg' => $e->getMessage() ]; } return [ 'code' => 0, 'msg' => '绑定成功', 'data' => [ 'dataObj' => $decryptedData['phoneNumber'] ] ]; } public function userEmpower() { if (!$this->validate()) { return [ 'code' => 1, 'msg' => $this->getErrorSummary(false)[0], ]; } if ($this->bind_type != 2) { return [ 'code' => 1, 'msg' => '参数错误' ]; } $user = User::find()->where(['store_id' => $this->store_id, 'id' => $this->user_id])->one(); $mobile = $this->phone; if (!$mobile) { return [ 'code'=>1, 'msg'=>'请输入手机号' ]; } $message = \Yii::$app->cache->get('code_cache'.$mobile); if (!$this->phone_code) { return [ 'code'=>1, 'msg'=>'请输入验证码' ]; } if (!$message) { return [ 'code'=>1, 'msg'=>'请先发送短信' ]; } if (!$message->validate(intval($this->phone_code))) { return [ 'code'=>1, 'msg'=>'验证码不正确' ]; } $user->binding = $this->phone; if ($user->save()) { \Yii::$app->cache->delete('code_cache'.$mobile); return [ 'code' => 0, 'msg'=>'保存成功' ]; } else { return [ 'code' => 1, 'msg' => 'fail' ]; } } public function getOpenId($code, $params = []) { $mini_id = get_mini_id(); $store_id = get_store_id(); if (!empty($code)) { if (is_wechat_platform()) { $app = WechatMini::getWechatConfig($store_id, $mini_id); $result = $app->auth->session($code); $params['wechat_open_id'] = $result['openid']; $params['session_key'] = $result['session_key']; } if (is_alipay_platform()) { $form = new AlipayThirdForm(); $form->method = "alipay.open.auth.token.app"; $form->bind_store_id = get_store_id(); if (is_isv()) { $request = new AlipaySystemOauthTokenRequest(); $request->setGrantType("authorization_code"); $request->setCode($code); $result = $form->miniCommon($request, 1, $mini_id, 0, 1); $params['ali_user_id'] = $result->user_id; $params['ali_open_id'] = $result->open_id; } else { if (\Yii::$app->prod_is_dandianpu() && self_mini() === false) { $alipay_config = Json::decode(Option::get('one_store_alipay_config', 0, 'saas', '')['value']); } else { $config_cache = \Yii::$app->cache->get('alipay_config_cache_' . $store_id); if ($config_cache) { $alipay_config = Json::decode($config_cache); } else { $alipay_config = Json::decode(Option::get(Option::OPTOPN_KEY, $store_id, 'alipay')['value']); } } $config = [ 'alipay' => [ 'client_id' => $alipay_config['app_id'], 'rsa_private_key' => $alipay_config['app_private_key'], ] ]; $socialite = new SocialiteManager($config); $res_code = $socialite->create('alipay')->tokenFromCode($code); $params['ali_user_id'] = $res_code['user_id']; $params['ali_open_id'] = $res_code['open_id']; } } } return [ $params['wechat_open_id'], $params['ali_user_id'], $params['ali_open_id'], $params['session_key'] ]; } }