| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395 |
- <?php
- namespace mall\controllers;
- use biz\shop\classes\ShopExtClass;
- use bizHd\custom\classes\CustomClass;
- use bizHd\wx\classes\WxOpenClass;
- use bizMall\message\services\SmsService;
- use bizMall\user\classes\UserClass;
- use bizMall\user\classes\UserGrowthClass;
- use bizMall\user\classes\UserIntegralClass;
- use bizMall\user\services\UserAssetService;
- use bizMall\user\services\UserService;
- use biz\shop\classes\ShopClass;
- use common\components\dict;
- use common\components\imgUtil;
- use common\components\jwt;
- use common\components\noticeUtil;
- use common\components\stringUtil;
- use Yii;
- use common\components\util;
- use yii\helpers\Json;
- use Overtrue\ChineseCalendar\Calendar;
- class UserController extends BaseController
- {
- //二维码收款使用 ssh
- public $guestAccess = ['discount', 'mini-mobile', 'create', 'account-login', 'current-info'];
- //清空登录信息 ssh 20250215
- public function actionClearLogin()
- {
- $user = $this->user;
- $user->miniOpenId = '';
- $user->save();
- util::complete();
- }
- //修改信息 ssh 20221015
- public function actionUpdate()
- {
- $post = Yii::$app->request->post();
- $user = $this->user;
- if (empty($user)) {
- util::fail('请先登录');
- }
- $userId = $user->id ?? 0;
- $address = $post['address'] ?? '';
- $floor = $post['floor'] ?? '';
- $fullAddress = $address . $floor;
- $post['fullAddress'] = $fullAddress;
- UserClass::updateUserInfo($user, $post);
- util::complete();
- }
- //修改生日 ssh 20250830
- public function actionModifyBirthday()
- {
- date_default_timezone_set('PRC');
- $get = Yii::$app->request->get();
- $lunar = $get['lunar'] ?? 0;
- $birthdayMonth = $get['birthdayMonth'] ?? 1;
- if ($birthdayMonth > 12 || $birthdayMonth < 1) {
- util::fail('月份必须1到12月');
- }
- $birthdayDate = $get['birthdayDate'] ?? 1;
- if ($birthdayDate > 31 || $birthdayDate < 1) {
- util::fail('日期必须1到31号');
- }
- if ($lunar == 1 && $birthdayDate == 31) {
- util::fail('农历没有31号');
- }
- $user = $this->user;
- if ($user->birthdaySet == 1) {
- util::fail('你已经设置过了');
- }
- if ($lunar == 1) {
- try {
- //农历转阳历,有多处使用,需要同步修改,关键词 change_my_birthday
- $calendar = new Calendar();
- $result = $calendar->lunar(date("Y"), $birthdayMonth, $birthdayDate);
- $month = $result['gregorian_month'] ?? 1;
- $date = $result['gregorian_day'] ?? 1;
- $birthday = date("Y") . '-' . $month . '-' . $date;
- } catch (\InvalidArgumentException $e) {
- $msg = $e->getMessage();
- $birthday = '0000-00-00 00:00:00';
- util::fail($msg);
- }
- } else {
- $birthday = date("Y") . '-' . $birthdayMonth . '-' . $birthdayDate;
- }
- $birthdayTime = strtotime($birthday);
- $user->lunar = $lunar;
- $user->birthdayDate = $birthdayDate;
- $user->birthdayMonth = $birthdayMonth;
- $user->birthday = $birthday;
- $user->birthdayTime = $birthdayTime;
- $user->birthdaySet = 1;
- $user->save();
- $userId = $user->id;
- $customList = \bizMall\custom\classes\CustomClass::getAllByCondition(['userId' => $userId], null, '*', null, true);
- if (!empty($customList)) {
- foreach ($customList as $custom) {
- $custom->lunar = $lunar;
- $custom->birthdayDate = $birthdayDate;
- $custom->birthdayMonth = $birthdayMonth;
- $custom->birthday = $birthday;
- $custom->birthdayTime = $birthdayTime;
- $custom->save();
- }
- }
- util::complete('修改成功');
- }
- //获取当前登录用户的信息 ssh 20221014
- public function actionCurrentInfo()
- {
- $user = $this->user;
- if (!empty($user)) {
- $birthday = $user->birthday ?? '';
- //如果当年的生日时间没有更新,则需要给更新
- $year = date("Y", strtotime($birthday));
- $thisYear = date("Y");
- if ($year != $thisYear) {
- $lunar = $user->lunar ?? 0;
- $userId = $this->userId;
- $birthdayMonth = $user->birthdayMonth ?? 0;
- $birthdayDate = $user->birthdayDate ?? 0;
- if ($lunar == 1 && $birthdayMonth > 0 && $birthdayDate > 0) {
- try {
- //农历转阳历,有多处使用,需要同步修改,关键词change_my_birthday
- $calendar = new Calendar();
- $result = $calendar->lunar(date("Y"), $birthdayMonth, $birthdayDate);
- $month = $result['gregorian_month'] ?? 1;
- $date = $result['gregorian_day'] ?? 1;
- $birthday = date("Y") . '-' . $month . '-' . $date;
- $user->birthday = $birthday;
- $user->save();
- CustomClass::updateByCondition(['userId' => $userId], ['birthday' => $birthday]);
- } catch (\InvalidArgumentException $e) {
- $msg = $e->getMessage();
- noticeUtil::push("更新生日出错了呢,userId:" . $userId . ' ' . $msg, '15280215347');
- }
- } else {
- $birthday = date("Y") . '-' . $birthdayMonth . '-' . $birthdayDate;
- $user->birthday = $birthday;
- $user->save();
- CustomClass::updateByCondition(['userId' => $userId], ['birthday' => $birthday]);
- }
- }
- // 将 $user 转换为数组,然后添加 hasMap 属性(字典中的 hasMap)
- $user = $user->toArray();
- $shortAvatar = $user['avatar'] ?? '';
- $smallAvatar = imgUtil::groupImg($shortAvatar) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
- $avatar = imgUtil::groupImg($shortAvatar) . "?x-oss-process=image/resize,m_fill,h_700,w_700";
- $user['avatar'] = $avatar;
- $user['smallAvatar'] = $smallAvatar;
- $user['shortAvatar'] = $shortAvatar;
- }
- // 补充店铺信息
- $shop = [];
- if (!empty($this->shop)) {
- $intraCityRet = ShopClass::hasIntraCity($this->shop);
- $shop['openIntraCity'] = $intraCityRet['openIntraCity'];
- $shop['hcFreeKm'] = $intraCityRet['hcFreeKm'];
- $shop['hcMap'] = $intraCityRet['hcMap'];
- }
- util::success(['info' => $user, 'shop' => $shop]);
- }
- //使用账号登录 ssh 20220920
- public function actionAccountLogin()
- {
- $getParams = Yii::$app->request->get();
- $postParams = Yii::$app->request->post();
- $allParams = array_merge($getParams, $postParams);
- $mobile = $allParams['mobile'] ?? '';
- $password = $allParams['password'] ?? '';
- if ($password != 'a.1234') {
- util::fail('密码错误');
- }
- $user = UserClass::getByCondition(['mobile' => $mobile], true);
- if (empty($user)) {
- util::fail('账号无效');
- }
- $userId = $user->id ?? 0;
- $token = !empty($userId) ? jwt::getNewToken($userId) : '';
- // 将 $user 转换为数组,然后添加 hasMap 属性(字典中的 hasMap)
- $user = $user->toArray();
- $user['hasMap'] = dict::getDict('hasMap');
- util::success(['user' => $user, 'token' => $token]);
- }
- //生成用户和花店增加客户 ssh 20250307
- public function actionCreate()
- {
- $post = Yii::$app->request->post();
- $user = UserClass::replaceUser($post);
- if (!empty($this->shop)) {
- //花店增加客户
- CustomClass::buildRelation($this->shop, $user);
- }
- $userId = $user->id ?? 0;
- $token = !empty($userId) ? jwt::getNewToken($userId) : '';
- $fullUser = UserClass::getById($userId, true);
- // 将 $fullUser 转换为数组,然后添加 hasMap 属性(字典中的 hasMap)
- $fullUser = $fullUser->toArray();
- $fullUser['hasMap'] = dict::getDict('hasMap');
- util::success(['user' => $fullUser, 'token' => $token]);
- }
- //最近店铺 ssh 2021.2.27
- public function actionRecentShop()
- {
- $list = [];
- //显示国兰的信息 0不是 1是
- $showGlInfo = getenv('SHOW_GL_INFO');
- $info = $showGlInfo === false ? 1 : $showGlInfo;
- util::success(['list' => $list, 'showGlInfo' => $info]);
- }
- //获取登陆客户的资产 ssh 2019.11.22
- public function actionAsset()
- {
- $asset = UserAssetService::getByUserId($this->userId);
- util::success($asset);
- }
- //获取登陆客户的优惠情况 ssh 2019.12.3
- public function actionDiscount()
- {
- $discount = UserAssetService::getDiscount($this->userId);
- util::success($discount);
- }
- //小程序用户完整信息 ssh 2019.12.12
- public function actionMiniFullInfo()
- {
- $post = Yii::$app->request->post();
- $iv = $post['iv'] ?? '';
- $encryptedData = $post['encryptedData'] ?? '';
- $merchant = WxOpenClass::getMallWxMiniBase();
- $appId = $merchant['miniAppId'];
- $miniOpenId = $post['miniOpenId'];
- if (empty($miniOpenId)) {
- util::success(['hasNoOpenId' => 1], '登录失败');
- }
- $cacheKey = 'MALL_MINI_SESSION_KEY_' . $miniOpenId;
- $sessionKey = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
- $wxMiniSecret = Yii::getAlias("@vendor/weixinMiniSecret");
- require_once($wxMiniSecret . '/wxBizDataCrypt.php');
- $pc = new \WXBizDataCrypt($appId, $sessionKey);
- $errCode = $pc->decryptData($encryptedData, $iv, $result);
- if ($errCode != 0) {
- Yii::info($result . ' ' . $errCode);
- util::fail('获取用户信息失败');
- }
- Yii::info('小程序获取用户信息:' . $result);
- $originalInfo = Json::decode($result);
- util::success($originalInfo);
- }
- //小程序用户手机号
- public function actionMiniMobile()
- {
- $post = Yii::$app->request->post();
- $iv = $post['iv'];
- $encryptedData = $post['encryptedData'];
- $merchant = WxOpenClass::getMallWxMiniBase();
- $appId = $merchant['miniAppId'];
- $miniOpenId = $post['miniOpenId'];
- if (empty($miniOpenId)) {
- util::success(['hasNoOpenId' => 1], '登录失败');
- }
- $cacheKey = 'MALL_MINI_SESSION_KEY_' . $miniOpenId;
- $sessionKey = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
- if (empty($sessionKey)) {
- util::fail('系统错误,session empty');
- }
- $wxMiniSecret = Yii::getAlias("@vendor/weixinMiniSecret");
- require_once($wxMiniSecret . '/wxBizDataCrypt.php');
- $pc = new \WXBizDataCrypt($appId, $sessionKey);
- $errCode = $pc->decryptData($encryptedData, $iv, $result);
- if ($errCode != 0) {
- util::fail('解密失败');
- }
- $arr = Json::decode($result);
- $mobile = $arr['purePhoneNumber'] ?? '';
- if (empty($mobile)) {
- util::fail('没有取到手机号,请重试');
- }
- util::success(['mobile' => $mobile]);
- }
- //密码修改
- public function actionPassword()
- {
- $post = Yii::$app->request->post();
- $old = $post['old'] ?? '';
- $new = $post['new'] ?? '';
- $confirm = $post['confirm'] ?? '';
- if (empty($new)) {
- util::fail('请输入新密码');
- }
- if ($new != $confirm) {
- util::fail('二次密码不一致');
- }
- $userId = $this->userId;
- $user = UserService::getUserInfo($userId);
- if (isset($user['hasPayPwd']) && $user['hasPayPwd'] == 1) {
- if (empty($old)) {
- util::fail('请填写旧密码');
- }
- if (!password_verify($old, $user['payPassword'])) {
- util::fail('旧密码错误');
- }
- }
- UserService::updateById($userId, ['payPassword' => password_hash($new, PASSWORD_BCRYPT), 'hasPayPwd' => 1]);
- util::complete('修改成功');
- }
- //申请会员 ssh 2019.12.18
- public function actionApplyMember()
- {
- $post = Yii::$app->request->post();
- $mobile = $post['mobile'] ?? 0;
- if (!stringUtil::isMobile($mobile)) {
- util::fail('请输入手机号');
- }
- $userId = $this->userId;
- $asset = UserAssetService::getByUserId($userId);
- if (isset($asset['member']) && $asset['member'] != -1) {
- util::fail('已经是会员了');
- }
- $code = $post['code'] ?? '';
- $cacheCode = SmsService::getApplyMemberCode($userId, $mobile);
- if (empty($cacheCode) || $cacheCode != $code) {
- util::fail('验证码错误');
- }
- $update = [];
- if (!empty($post['realName'])) {
- $update['realName'] = $post['realName'];
- }
- if (!empty($post['birthday'])) {
- $update['birthday'] = $post['birthday'];
- }
- $user = UserService::getByCondition(['sjId' => $this->sjId, 'mobile' => $mobile]);
- if ($userId == $user['id']) {
- util::fail('手机号已验证');
- }
- if (!empty($user)) {
- util::fail('手机号已经注册过了');
- }
- $update['mobile'] = $mobile;
- $userInfo = $this->user->attributes ?? [];
- UserService::becomeMember($userInfo, $this->sj->attributes, $update);
- util::complete('注册成功');
- }
- //登陆客户的信息 ssh 2020.3.12
- public function actionLoginDetail()
- {
- $user = $this->user->attributes ?? [];
- util::success($user);
- }
- //成长值列表
- public function actionGrowthList()
- {
- $where = [];
- $where['shopId'] = $this->shopId;
- $where['userId'] = $this->userId;
- $list = UserGrowthClass::getGrowthList($where);
- util::success($list);
- }
- //积分列表
- public function actionIntegralList()
- {
- $where = [];
- $where['shopId'] = $this->shopId;
- $where['userId'] = $this->userId;
- $list = UserIntegralClass::getIntegralList($where);
- util::success($list);
- }
- }
|