| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764 |
- <?php
- namespace api\modules\v1\controllers\user;
- use api\modules\v1\forms\user\AlipayMiniForm;
- use api\modules\v1\forms\user\WechatForm;
- use common\enums\AddonsEnum;
- use common\enums\EventNameEnum;
- use common\enums\RedisKeyEnum;
- use common\enums\StatusEnum;
- use common\events\user\UserCancellationEvent;
- use common\events\user\UserLoginEvent;
- use common\events\user\UserRegisterEvent;
- use common\expand\alipay\auth\src\OauthTokenService;
- use common\helpers\ArrayHelper;
- use common\helpers\sms\Sms;
- use common\models\common\MallSetting;
- use common\models\mall\MallAddons;
- use common\models\user\User;
- use common\models\user\UserInfo;
- use Yii;
- use api\controllers\OnAuthController;
- use common\enums\addons\MallAddonsEnum;
- use common\enums\ApiStatusEnum;
- use yii\db\Exception;
- use yii\helpers\Json;
- /**
- * Class IdentityController
- * @package api\modules\user\controllers
- */
- class IdentityController extends OnAuthController
- {
- public $modelClass = '';
- /**
- *
- * 不用进行登录验证的方法
- *
- * 例如: ['index', 'update', 'create', 'view', 'delete']
- * 默认全部需要验证
- *
- * @var array
- */
- protected $authOptional = ['login','bind', 'mobile-login', 'jssdk-config','register-sms-code', 'register', 'up-pwd', 'auth-login',
- 'mini-login','app-auth-login','auth-phone','auth-alipay-mini','mobile-alipay-mini','save-wechat',
- 'forget-password','mobile-alipay-mini-by-code'
- ];
- public function actionForgetPassword(){
- try {
- if (\Yii::$app->request->isPost) {
- $params = Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($params, [
- [['mobile', 'captcha', 'password'], 'required'],
- ['mobile', 'match', 'pattern' => '/[0-9]+$/', 'message' => '请输入正确的手机号码'],
- [['mobile'], 'integer'],
- ['password', 'string', 'length' => [6, 11]],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- //短信验证码
- if (YII_ENV == 'prod') {
- if (!Sms::checkValidateCode($params['mobile'], $params['captcha'],Sms::SMS_TYPE_UPDATE_PASS)) return $this->error('验证码不正确');
- }
- $user = User::findOne(['mobile'=>$params['mobile'],'mall_id'=>$this->mall_id,'status'=>[StatusEnum::ENABLED,StatusEnum::DISABLED]]);
- if(empty($user)) return $this->error('此手机号未注册');
- if(empty($user->status)) return $this->error('此手机号被禁用');
- $user->password = Yii::$app->getSecurity()->generatePasswordHash(trim($params['password']));
- if ($user->save() === false) return $this->error(User::getStaticError());
- return $this->success('设置成功');
- }
- } catch (\Exception $e) {
- return $this->error($e->getMessage());
- }
- }
- /**
- * h5注册
- * @return \yii\web\Response
- */
- public function actionRegister()
- {
- try {
- if (\Yii::$app->request->isPost) {
- $params = Yii::$app->request->post();
- $mall_id = $this->mall_id;
- $res = Yii::$app->services->validate->validate($params, [
- [['mobile', 'password', 'confirm_password'], 'required'],
- [['captcha'], 'safe'],
- ['mobile', 'match', 'pattern' => '/[0-9]+$/', 'message' => '请输入正确的手机号码'],
- ['invite_mobile', 'match', 'pattern' => '/[0-9]+$/', 'message' => '请输入正确的推荐人手机号码'],
- ['password', 'string', 'length' => [6, 11]],
- ['password', 'match', 'pattern' => '/^(?![0-9]+$)(?![a-zA-Z]+$)[0-9a-zA-Z]+$/', 'message' => '密码必须是字母加数字'],
- ['confirm_password', 'compare', 'compareAttribute' => 'password', 'message' => '两次密码不一致'],
- [['nickname'], 'string'],
- ['mobile', function ($attribute) use ($mall_id) {
- $where[] = ['mall_id' => $mall_id];
- $where[] = ['or', ['mobile' => trim($this->$attribute)], ['username' => trim($this->$attribute)]];
- if (User::getUser($where)) $this->addError($attribute, '手机号码已存在');
- }],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- // 获取公共配置
- $basic = Yii::$app->services->setting->mall->get($this->mall_id, 'basic');
- $basic['is_write_name'] = !isset($basic['is_write_name']) ? 0 : $basic['is_write_name']; // 要不要强制填写昵称
- if ($basic['is_write_name']) { // 要
- if (empty($params['nickname'])) {
- return $this->error('昵称不能为空');
- }
- } else { // 不要
- $params['nickname'] = '游客' . substr(time(), -4) . mt_rand(100, 999);
- }
- $mall_setting = Yii::$app->services->setting->mall->get($mall_id, 'user');
- $check_sms_code_value = $mall_setting['check_sms_code'] ?? 1;
- if(empty($params['captcha']) && $check_sms_code_value) return $this->error('请输入验证码');
- //短信验证码 开启验证码注册才检查
- if (YII_ENV == 'prod' && $check_sms_code_value) {
- //短信验证码
- if (!Sms::checkValidateCode($params['mobile'], $params['captcha'],Sms::SMS_TYPE_REGISTER)) return $this->error('验证码不正确');
- }
- $params['mall_id'] = $this->mall_id;
- $params['username'] = $params['mobile'];
- $params['parent_id'] = $this->parent_id;
- $params['password'] = Yii::$app->getSecurity()->generatePasswordHash($params['password']);
- $params['source'] = empty($this->source) ? User::SOURCE_SHARE_INDEX : $this->source;
- $params['platform'] = User::PLATFORM_H5;
- $res = User::setData($params);
- if ($res === false) return $this->error(User::getStaticError());
- // 触发注册成功事件,事务提交完成后触发
- $event = new UserRegisterEvent($this->mall_id);
- $data = ['user_id' => $res->id, 'platform' => 'h5'];
- Yii::$app->services->events->dispatch($event, $data, $event->listeners);
- return $this->success('注册成功');
- }
- } catch (\Exception $e) {
- return $this->error($e->getMessage());
- }
- }
- /**
- * h5登录
- * @return mixed|void
- */
- public function actionLogin()
- {
- try {
- if (Yii::$app->request->isPost) {
- $params = Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($params, [
- [['username'], 'required'],
- [['captcha', 'password', 'device_type', 'device_mac'], 'safe'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $params['mall_id'] = $this->mall_id;
- $user = User::loginByH5($params);
- if ($user === false) return $this->error(User::getStaticError());
- $token = Yii::$app->services->apiAccessToken->getAccessToken($user, 'api');
- // 触发登录成功事件,事务提交完成后触发
- $event = new UserLoginEvent($this->mall_id);
- Yii::$app->services->events->dispatch($event, [
- 'mall_id' => $this->mall_id,
- 'nickname' => $user->nickname,
- 'user_id' => $user->id,
- 'platform' => 'h5',
- 'ip' => ArrayHelper::get_client_ip(),
- 'device_type' => $params['device_type'] ?? '',
- 'device_mac' => $params['device_mac'] ?? '',
- ], $event->listeners);
- return $this->success('登录成功', $token);
- }
- } catch (\Exception $e) {
- return $this->error($e->getMessage());
- }
- }
- /**
- * 登出
- *
- * @return array|mixed
- */
- public function actionLogout()
- {
- if (Yii::$app->services->apiAccessToken->disableByAccessToken(Yii::$app->user->identity->access_token)) return $this->success('退出成功');
- return $this->error('退出失败');
- }
- /**
- * 权限验证
- *
- * @param string $action 当前的方法
- * @param null $model 当前的模型类
- * @param array $params $_GET变量
- * @throws \yii\web\BadRequestHttpException
- */
- public function checkAccess($action, $model = null, $params = [])
- {
- // 方法名称
- if (in_array($action, ['index', 'view', 'update', 'create', 'delete'])) {
- throw new \yii\web\BadRequestHttpException('权限不足');
- }
- }
- /**
- * 获取注册短信验证码
- * @return mixed|void
- * @throws \Overtrue\EasySms\Exceptions\NoGatewayAvailableException
- */
- public function actionRegisterSmsCode()
- {
- $params = Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($params, [
- [['mobile'], 'required', 'message' => '请输入手机号码'],
- [['mobile','area_code'], 'integer'],
- [['sms_type'], 'string'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $area_code = '';// 初始化区号
- $gateways = 'domestic';// 默认国内短信
- // 检测是登录并且区号不为空则使用国际短信
- if (!empty($this->user) && !empty($this->user->area_code)&&$this->user->area_code!=86) {
- $gateways = 'international';
- $area_code = $this->user->area_code;
- }
- // 判断是否有区号
- if (!empty($params['area_code']) && $params['area_code'] != 86) {
- $gateways = 'international';
- $area_code = $params['area_code'];
- }
- $notice_sms_status = \Yii::$app->services->setting->mall->get($this->mall_id, 'notice_sms.status');
- if(empty($notice_sms_status)) return $this->error('短信提醒未开启');
- //$params['sms_type'] = $params['sms_type'] ?: 'register';
- $params['sms_type'] = $params['sms_type'] ?: '';
- $sms = new Sms($this->mall_id, $gateways);
- try {
- if ($sms->sendCaptcha($params['mobile'], $area_code,$params['sms_type']) === false) return $this->error('发送失败');
- }catch (\Exception $e){
- return $this->error($e->getMessage());
- }
- return $this->success('发送成功');
- }
- /**
- * @desc:授权登录
- * @Author: hua
- * @Date: 2021/10/22
- * @Time: 16:51
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return mixed|void
- */
- public function actionAuthLogin()
- {
- return $this->_handleAuth('weixin');
- // $requests = Yii::$app->request;
- // $wechat_form = new WechatForm(['mall_id'=>$this->mall_id]);
- // $params_data = @file_get_contents('php://input');
- // if (!empty($params_data)) $json_data = json_decode($params_data, true);
- // $params = array_merge($requests->get(), $requests->post(), $json_data ?? []);
- // $res = Yii::$app->services->validate->validate($params, [
- // [['code', 'encryptedData', 'iv', 'key'], 'string'],
- // [['user_id', 'activity_id', 'share_user_id'], "integer"]
- // ]);
- // if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- // $params['mall_id'] = $this->mall_id;
- // $params['source'] = empty($this->source) ? User::SOURCE_SHARE_INDEX : $this->source;
- // $result = $wechat_form->authorized($params);
- // if ($result === false) return $this->error($wechat_form::getStaticError());
- // return $this->success('成功', $result);
- }
- /**
- * @desc:小程序授权登录
- * @Author: hua
- * @Date: 2021/10/22
- * @Time: 9:37
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return mixed|void
- */
- public function actionMiniLogin()
- {
- return $this->_handleAuth('mp-weixin');
- // $requests = Yii::$app->request;
- // $wechat_form = new WechatForm(['mall_id'=>$this->mall_id]);
- // $params_data = @file_get_contents('php://input');
- // if (!empty($params_data)) $json_data = json_decode($params_data, true);
- // $params = array_merge($requests->get(), $requests->post(), $json_data ?? []);
- // $res = Yii::$app->services->validate->validate($params, [
- // [['code', 'encryptedData', 'iv', 'key'], 'string'],
- // [['user_id', 'activity_id', 'share_user_id'], "integer"]
- // ]);
- // if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- // $params['mall_id'] = $this->mall_id;
- // $params['source'] = empty($this->source) ? User::SOURCE_SHARE_INDEX : $this->source;
- // $result = $wechat_form->miniAuthorized($params);
- // if ($result === false) return $this->error($wechat_form::getStaticError());
- // return $this->success('成功', $result);
- }
- /**
- * app端微信授权登录
- * @Author: zal
- * @Date: 2020-12-24
- * @Time: 17:33
- * @return array
- * @throws \Exception
- */
- public function actionAppAuthLogin()
- {
- return $this->_handleAuth('app-weixin');
- // $requests = Yii::$app->request;
- // $wechat_form = new WechatForm(['mall_id'=>$this->mall_id]);
- // $params_data = @file_get_contents('php://input');
- // if (!empty($params_data)) $json_data = json_decode($params_data, true);
- // $params = array_merge($requests->get(), $requests->post(), $json_data ?? []);
- // $res = Yii::$app->services->validate->validate($params, [
- // [['userData'], 'string'],
- // [['user_id', 'activity_id', 'share_user_id'], "integer"],
- // ]);
- // if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- // $params['mall_id'] = $this->mall_id;
- // $params['user_id'] = $this->user_id;
- // $params['source'] = empty($this->source) ? User::SOURCE_SHARE_INDEX : $this->source;
- // $result = $wechat_form->appAuthLogin($params);
- // if ($result === false) return $this->error($wechat_form::getStaticError());
- // return $this->success('成功', $result);
- }
- /**
- * 授权处理
- * @Author bing
- * @DateTime 2021-12-08 10:50:35
- * @copyright: Copyright (c) 2020 广东七件事集团
- * @return mixed/void
- * @param $scene
- */
- private function _handleAuth($scene){
- $requests = Yii::$app->request;
- $params_data = @file_get_contents('php://input');
- if (!empty($params_data)) $json_data = json_decode($params_data, true);
- $params = array_merge($requests->get(), $requests->post(), $json_data ?? []);
- $rules = [
- [['code', 'encryptedData', 'iv', 'key'], 'string'],
- [['user_id', 'activity_id', 'share_user_id'], "integer"],
- [['device_type', 'device_mac'], 'safe'],
- ];
- switch($scene){
- case 'weixin' :
- $method = 'authorized';
- break;
- case 'mp-weixin' :
- $method = 'miniAuthorized';
- break;
- case 'app-weixin' :
- $rules = [
- [['userData'], 'string'],
- [['user_id', 'activity_id', 'share_user_id'], "integer"],
- [['platform'], "string"],
- ];
- $params['user_id'] = $this->user_id;
- $method = 'appAuthLogin';
- break;
- }
- $res = Yii::$app->services->validate->validate($params,$rules);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $params['mall_id'] = $this->mall_id;
- $params['source'] = empty($this->source) ? User::SOURCE_SHARE_INDEX : $this->source;
- $wechat_form = new WechatForm(['mall_id'=>$this->mall_id]);
- $result = $wechat_form->$method($params);
- if ($result === false){
- $is_log_off_del = \Yii::$app->services->setting->mall->get($this->mall_id, 'user.is_log_off_del');
- if (!empty($is_log_off_del)) {
- if($wechat_form::getStaticError()=='账号已注销'){
- return $this->success($wechat_form::getStaticError());
- }
- }
- return $this->error($wechat_form::getStaticError());
- }
- if(isset($result['user_id'])) unset($result['user_id']);
- return $this->success('操作成功', $result);
- }
- /**
- * @desc:小程序授权手机号
- * @Author: hua
- * @Date: 2021/10/22
- * @Time: 14:20
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return mixed|void
- */
- public function actionAuthPhone()
- {
- return $this->_handleBindMobile('mp_weixin');
- // $requests = Yii::$app->request;
- // $wechat_form = new WechatForm(['mall_id'=>$this->mall_id]);
- // $params_data = @file_get_contents('php://input');
- // if (!empty($params_data)) $json_data = json_decode($params_data, true);
- // $params = array_merge($requests->get(), $requests->post(), $json_data ?? []);
- // $res = Yii::$app->services->validate->validate($params, [
- // [['code', 'encryptedData', 'iv', 'key'], 'string'],
- // [['user_id', 'activity_id', 'share_user_id'], "integer"]
- // ]);
- // if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- // $params['mall_id'] = $this->mall_id;
- // $params['user_id'] = $this->user_id;
- // $params['parent_id'] = $this->parent_id;
- // $params['source'] = empty($this->source) ? User::SOURCE_SHARE_INDEX : $this->source;
- // $result = $wechat_form->authorizedMobilePhone($params);
- // if ($result === false) return $this->error($wechat_form::getStaticError());
- // return $this->success('成功', $result);
- }
- /**
- * @desc:绑定绑定手机号码
- * @Author: hua
- * @Date: 2021/10/22
- * @Time: 14:32
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return mixed
- */
- public function actionBind()
- {
- return $this->_handleBindMobile();
- // $requests = Yii::$app->request;
- // $params =$requests->post();
- // $res = Yii::$app->services->validate->validate($params, [
- // [["mobile",'captcha','openid','platform'],'string']
- // ]);
- // if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- // $params['user_id'] = $this->user_id;
- // $params['mall_id'] = $this->mall_id;
- // $params['parent_id'] = $this->parent_id;
- // $result = User::bind($params);
- // if ($result === false) return $this->error(User::getStaticError());
- // return $this->success('成功', $result);
- }
- private function _handleBindMobile($scene=''){
- $requests = Yii::$app->request;
- switch ($scene){
- case "mp_weixin":
- $params_data = @file_get_contents('php://input');
- Yii::error("小程序授权信息:" . $params_data);
- if (!empty($params_data)) $json_data = json_decode($params_data, true);
- $params = array_merge($requests->get(), $requests->post(), $json_data ?? []);
- $rules = [
- [['code', 'encryptedData', 'iv', 'key'], 'string'],
- [['user_id', 'activity_id', 'share_user_id', 'source'], "integer"],
- [['invite_mobile'], "safe"],
- ['invite_mobile', 'match', 'pattern' => '/[0-9]+$/', 'message' => '请输入正确的推荐人手机号码'],
- [['encryptedData'],'required']
- ];
- $params['source'] = empty($this->source) ? User::SOURCE_SHARE_INDEX : $this->source;
- $obj = new WechatForm(['mall_id'=>$this->mall_id]);
- $method = 'authorizedMobilePhone';
- break;
- default:
- $params =$requests->post();
- $rules = [
- [["mobile",'captcha','openid','platform'],'string'],
- [['invite_mobile'], "safe"],
- ['invite_mobile', 'match', 'pattern' => '/[0-9]+$/', 'message' => '请输入正确的推荐人手机号码'],
- ];
- $obj = new User();
- $method = 'bind';
- break;
- }
- $res = Yii::$app->services->validate->validate($params, $rules);
- $params['mall_id'] = $this->mall_id;
- $params['user_id'] = $this->user_id;
- $params['parent_id'] = $this->parent_id;
- Yii::error("注册的参数:" . Json::encode($params));
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $result = $obj->$method($params);
- if ($result === false) return $this->error($obj::getStaticError());
- return $this->success('操作成功', $result);
- }
- /**
- * @desc:desc
- * @Author: hua
- * @Date: 2021/10/26
- * @Time: 14:15
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @return mixed|void
- * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
- * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
- * @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
- * @throws \Psr\SimpleCache\InvalidArgumentException
- */
- public function actionJssdkConfig()
- {
- try{
- $wechat_form = new WechatForm(['mall_id'=>$this->mall_id]);
- if (empty(Yii::$app->params['wechatConfig']['app_id'])) return $this->error('请设置公众号配置');
- $result = $wechat_form->getJsSdkConfig();
- return $this->success('操作成功', $result);
- }catch(\Exception $e){
- return $this->error($e->getMessage());
- }
- }
- public function actionCancellation()
- {
- try {
- $user_id = $this->user_id;
- if(empty($user_id)) return $this->error('请先登录');
- $params = Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($params, [
- [['captcha'], 'required'],
- [['captcha'], 'safe'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- if (!Sms::checkValidateCode($this->user['mobile'], $params['captcha'], Sms::SMS_TYPE_USER_CANCELLATION)) throw new Exception('验证码不正确');
- Yii::$app->services->apiAccessToken->disableByAccessToken(Yii::$app->user->identity->access_token);
- $res = User::updateAll(['status' => StatusEnum::DISABLED], ['id'=>$user_id]);
- if ($res === false) return $this->error(User::getStaticError());
- $event = new UserCancellationEvent($this->mall_id);
- \Yii::$app->services->events->dispatch($event, [
- 'mall_id' => $this->mall_id,
- 'user_id' => $user_id,
- 'operator_user_id' => $user_id,
- ], $event->listeners);
- return $this->success('操作成功');
- }catch (Exception $e){
- return $this->error($e->getMessage());
- }
- }
- public function actionSmsCode()
- {
- $params = Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($params, [
- [['area_code'], 'integer'],
- [['sms_type'], 'string'],
- ]);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $area_code = '';// 初始化区号
- $gateways = 'domestic';// 默认国内短信
- // 检测是登录并且区号不为空则使用国际短信
- if (!empty($this->user) && !empty($this->user->area_code)&&$this->user->area_code!=86) {
- $gateways = 'international';
- $area_code = $this->user->area_code;
- }
- // 判断是否有区号
- if (!empty($params['area_code']) && $params['area_code'] != 86) {
- $gateways = 'international';
- $area_code = $params['area_code'];
- }
- $notice_sms_status = \Yii::$app->services->setting->mall->get($this->mall_id, 'notice_sms.status');
- if(empty($notice_sms_status)) return $this->error('短信提醒未开启');
- $params['mobile'] = $this->user['mobile'];
- $params['sms_type'] = $params['sms_type'] ?: '';
- $sms = new Sms($this->mall_id, $gateways);
- try {
- if ($sms->sendCaptcha($params['mobile'], $area_code,$params['sms_type']) === false) return $this->error('发送失败');
- }catch (\Exception $e){
- return $this->error($e->getMessage());
- }
- return $this->success('发送成功');
- }
- /**
- * 支付宝小程序授权登录
- * @desc 根据authCode 获取用户信息调用 alipay.system.oauth.token 取得 user_id 和 token(授权令牌)。
- * 使用的 scope 包含 auth_user,则服务端继续使用所取得的 token 调用 alipay.user.info.share 最终获得用户信息。
- * @return mixed|void
- * @throws \Exception
- */
- public function actionAuthAlipayMini()
- {
- $params = Yii::$app->request->post();
- $rules = [
- [['authCode'], 'string'],
- ];
- $res = Yii::$app->services->validate->validate($params, $rules);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $form = new AlipayMiniForm($this->mall_id);
- $result = $form->authorize($params['authCode'], $this->parent_id);
- if(!$result){
- \Yii::error('actionAuthAlipayMini:'.$form->getError());
- return $this->error($form->getError());
- }
- return $this->success('操作成功',$result);
- }
- /**
- * 支付宝小程序授权手机号
- * @return mixed|void
- */
- public function actionMobileAlipayMini()
- {
- $params = Yii::$app->request->post();
- $rules = [
- [['data','openid'], 'string'],
- ];
- $res = Yii::$app->services->validate->validate($params, $rules);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $data = json_decode($params['data'],true);
- $new_params['mall_id'] = $this->mall_id;
- $new_params['user_id'] = $this->user_id;
- $new_params['parent_id'] = $this->parent_id;
- $new_params['encryptData'] = $data['response'];
- $new_params['openid'] = $params['openid'];
- $form = new AlipayMiniForm($this->mall_id);
- $result = $form->authorizedMobilePhone($new_params);
- if(!$result){
- return $this->error($form->getError());
- }
- return $this->success('操作成功',$result);
- }
- public function actionMobileAlipayMiniByCode()
- {
- try{
- $requests = Yii::$app->request;
- $params =$requests->post();
- $rules = [
- [['mobile','captcha','open_id'],'string'],
- [['invite_mobile'], "safe"],
- ['invite_mobile', 'match', 'pattern' => '/[0-9]+$/', 'message' => '请输入正确的推荐人手机号码'],
- [['mobile','captcha','open_id'],'required'],
- ];
- $res = Yii::$app->services->validate->validate($params, $rules);
- if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $form = new AlipayMiniForm($this->mall_id);
- $result = $form->bindMobileBySms($params['open_id'],$params['mobile'],$params['captcha']);
- if(!$result){
- return $this->error($form->getError());
- }
- return $this->success('操作成功',$result);
- }catch (\Exception $e){
- return $this->error($e->getMessage());
- }
- }
- /**
- * 根据open_id 修改微信头像昵称
- * @return mixed|void
- */
- public function actionSaveWechat()
- {
- try{
- $rules = [
- [['openid', 'nickName', 'avatarUrl'], 'string'],
- // [['openid', 'nickName', 'avatarUrl'], 'required'],
- [['openid'], 'required'],
- [['nickName'], 'required', 'message' => '请输入昵称'],
- [['avatarUrl'], 'required', 'message' => '请上传头像'],
- ];
- $params = Yii::$app->request->post();
- $res = Yii::$app->services->validate->validate($params, $rules);
- if ($res === false) throw new \Exception(Yii::$app->services->validate->getErrorMessage());
- $where = [['mall_id' => $this->mall_id], ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]];
- $where[] = ['platform' => 'mp-wx'];
- if (!empty($params["openid"])) $where[] = ['openid' => $params["openid"]];
- //if (!empty($params["platform"])) $where[] = ['platform' => $params["platform"]];
- $user_info = UserInfo::getOne($where);
- if(!$user_info){
- $info = [
- 'user_id' => 0,
- 'mall_id' => $this->mall_id,
- 'openid' => $params['openid'],
- 'platform_data' => json_encode($params),
- 'platform' => User::PLATFORM_MP_WX
- ];
- UserInfo::setData($info);
- }else{
- if(!$user_info->user_id){
- $platform_data = json_decode($user_info->platform_data,true);
- $platform_data['headimgurl'] = $params['avatarUrl'];
- $platform_data['avatarUrl'] = $params['avatarUrl'];
- $platform_data['nickName'] = $params['nickName'];
- $user_info->platform_data = json_encode($platform_data);
- $user_info->save();
- }
- }
- return $this->success('操作成功');
- }catch(\Exception $e){
- return $this->error($e->getMessage());
- }
- }
- /**
- * 获取支付宝优惠券access token
- * @return mixed|void|null
- */
- public function actionGetAlipayVoucherAccessToken()
- {
- try {
- $rules = [
- [['code'], 'string'],
- [['code'], 'required', 'message' => '请输入code'],
- ];
- $params = Yii::$app->request->get();
- $res = Yii::$app->services->validate->validate($params, $rules);
- if ($res === false) throw new \Exception(Yii::$app->services->validate->getErrorMessage());
- // 校验当前系统是否安装了支付宝商家券插件
- if (!MallAddons::isInstall($this->mall_id, AddonsEnum::ADDONS_NAME_ALIPAY_MCH_COUPON)) {
- throw new \Exception('您还未安装当前插件');
- }
- $res = (new OauthTokenService([
- 'mall_id' => $this->mall_id,
- 'code' => $params['code']
- ]))->exec();
- if (empty($res['access_token'])) throw new \Exception('获取token失败');
- // 缓存
- // "alipay:voucher:access_token:{$this->user_id}"
- Yii::$app->cache->set(RedisKeyEnum::suffix(RedisKeyEnum::ALIPAY_ACCESS_TOKEN, "voucher:{$this->user_id}"),
- $res['access_token'], $res['expires_in'] - 10);
- return $this->success('获取成功');
- } catch (\Exception $e) {
- return $this->error($e->getMessage());
- }
- }
- }
|