| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\alliance\controllers;
- use app\models\Admin;
- use app\models\AgentApply;
- use app\models\BrowseLog;
- use app\models\District;
- use app\models\Favorite;
- use app\models\Level;
- use app\models\MdStaff;
- use app\models\SaaSLeaguePriceLog;
- use app\models\SaasUser;
- use app\models\ShareHolder;
- use app\models\Store;
- use app\models\User;
- use app\models\UserCard;
- use app\models\UserCoupon;
- use app\models\VerifyCardSale;
- use app\models\VideoGoodsAuthor;
- use app\modules\alliance\models\UserCenterForm;
- use app\modules\alliance\controllers\BaseController;
- use app\modules\alliance\models\OrderListForm;
- use app\modules\alliance\models\ShareForm;
- use app\modules\alliance\models\FavoriteListForm;
- use app\utils\ShareQrcode;
- use EasyWeChat\Kernel\Exceptions\HttpException;
- /**
- * 用户
- */
- class UserController extends BaseController
- {
- public function behaviors()
- {
- return parent::behaviors();
- }
- /**
- * 获取店铺联盟券
- *
- */
- public function actionGetStoreLeaguePrice()
- {
- $form = new FavoriteListForm();
- $access_token = get_params('access_token');
- $saas_user_model = SaasUser::findOne(['access_token' => $access_token]);
- $saas_user_id = $saas_user_model->id;
- return $this->asJson($form->league_search($saas_user_id));
- }
- public function actionGetUserName()
- {
- $mobile = get_params('mobile');
- $saas_user = SaasUser::findOne(['mobile' => $mobile]);
- if($saas_user){
- return $this->asJson([
- 'code' => 0,
- 'data' => $saas_user->name,
- 'msg' => '查找成功'
- ]);
- }else{
- return $this->asJson([
- 'code' => 1,
- 'data' => null,
- 'msg' => '未找到'
- ]);
- }
- }
- /**
- * 转送联盟券
- */
- public function actionPostSwitchLeaguePrice()
- {
- $access_token = post_params('access_token');
- $from_saas_user_model = SaasUser::findOne(['access_token' => $access_token]);
- $from_saas_user_id = $from_saas_user_model->id;
- $admin = Admin::findOne(['saas_user_id' => $from_saas_user_id, 'is_delete' => 0, 'type' => 'store']);
- $store = Store::findOne($admin->type_id);
- $league_price = post_params('league_price');
- $to_saas_user_mobile = post_params('mobile');
- $saas_user_model = SaasUser::findOne(['mobile' => $to_saas_user_mobile]);
- if($league_price <= 0){
- return $this->asJson([
- 'code' => 1,
- 'msg' => '提交联盟券金额不正确'
- ]);
- }
- if($saas_user_model){
- if($store->league_price < $league_price){
- return $this->asJson([
- 'code' => 1,
- 'data' => $store->id,
- 'msg' => '当前店铺联盟券金额不足'
- ]);
- }else{
- //给商家扣减 (用户赠送的联盟券钱从商家从扣减)
- $before = $store->league_price;
- $store->updateCounters(['league_price' => -floatval($league_price)]);
- \app\models\SaaSLeaguePriceLog::setLeaguePriceLog(
- $store->id,
- $saas_user_model->id,
- $league_price,
- $before,
- \app\models\SaaSLeaguePriceLog::TYPE_SEND,
- \app\models\SaaSLeaguePriceLog::TAKE_TYPE,
- \app\models\SaaSLeaguePriceLog::ROLE_STORE
- );
- $before = $saas_user_model->league_price;
- $saas_user_model->updateCounters(['league_price' => floatval($league_price)]);
- $id = 0;
- \app\models\SaaSLeaguePriceLog::setLeaguePriceLog(
- $store->id,
- $saas_user_model->id,
- $league_price,
- $before,
- \app\models\SaaSLeaguePriceLog::TYPE_SEND,
- \app\models\SaaSLeaguePriceLog::SEND_TYPE,
- \app\models\SaaSLeaguePriceLog::ROLE_USER,
- 0,
- $id
- );
- return $this->asJson([
- 'code' => 0,
- 'data' => $id,
- 'msg' => '转赠成功!'
- ]);
- }
- }else{
- return $this->asJson([
- 'code' => 1,
- 'msg' => '未找到用户'
- ]);
- }
- }
- /**
- * 喜欢的商品或店铺列表
- */
- public function actionFavoriteList()
- {
- $form = new FavoriteListForm();
- $form->attributes = get_params();
- $form->user_id = get_user_id();
- $form->store_id = get_store_id();
- return $this->asJson($form->search());
- }
- /**
- * 修改用户信息
- */
- public function actionEditUserInfo()
- {
- $saasUser = get_saas_user();
- if ($saasUser) {
- $avatar_url = input_params('avatar_url', '');
- $nickname = input_params('nickname', '');
- $gender = input_params('gender', '');
- if (!empty($avatar_url)) {
- $saasUser->avatar = $avatar_url;
- }
- if (!empty($nickname)) {
- $saasUser->name = $nickname;
- }
- if (!empty($gender) && in_array($gender, [0, 1, 2])) {
- $saasUser->gender = $gender;
- }
- if ($saasUser->save()) {
- return $this->asJson([
- 'code' => 0,
- 'msg' => '保存成功',
- ]);
- }
- return $this->asJson([
- 'code' => 1,
- 'msg' => '保存失败',
- ]);
- }
- return $this->asJson([
- 'code' => 1,
- 'msg' => '非法访问',
- ]);
- }
- /**
- * 积分记录
- */
- public function actionUserIntegralLog()
- {
- $form = new UserCenterForm();
- $result = $form->getIntegralLog();
- return $this->asJson($result);
- }
- /**
- * 更新用户wechat_app_open_id
- */
- public function actionUpdateWechatAppOpenId()
- {
- $id = input_params('user_id');
- $openid = input_params('open_id');
- if (!$id || !$openid) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '缺少参数',
- ]);
- }
- $user = User::findOne($id);
- if (!$user) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => '用户未找到',
- ]);
- }
- $user->wechat_app_open_id = $openid;
- $user->save();
- return $this->asJson([
- 'code' => 0,
- 'msg' => '更新成功',
- ]);
- }
- public function actionGetAgentApply() {
- try {
- $saas_user = get_saas_user();
- $params = post_params();
- $apply_ = AgentApply::findOne(['saas_user_id' => $saas_user->id, 'agent_type' => $params['type'] ?: 0, 'is_delete' => 0]);
- $readme = [
- 'area_agent_apply_agreement' => \app\models\Option::get('area_agent_apply_agreement', 0, 'saas', '')['value'],
- 'bd_agent_apply_agreement' => \app\models\Option::get('bd_agent_apply_agreement', 0, 'saas', '')['value'],
- 'goods_agent_apply_agreement' => \app\models\Option::get('goods_agent_apply_agreement', 0, 'saas', '')['value'],
- ];
- if (!$apply_) {
- return $this->asJson(array_merge([
- 'code' => 0,
- 'msg' => '获取成功',
- 'data' => [
- 'province_id' => '',
- 'city_id' => '',
- 'district_id' => '',
- 'address' => '',
- 'lat' => '',
- 'lng' => '',
- 'area_level' => '',
- 'agent_type' => 0,
- 'status' => -1
- ]
- ], $readme));
- }
- $city_arr = District::find()->where(['id' => [$apply_->province_id, $apply_->city_id, $apply_->district_id]])
- ->select('name')->column();
- $city = implode('/', $city_arr);
- $data = [
- "province_id" => $apply_->province_id,
- "city_id" => $apply_->city_id,
- "district_id" => $apply_->district_id,
- "address" => $apply_->address,
- "lat" => $apply_->lat,
- "lng" => $apply_->lng,
- "area_level" => $apply_->area_level,
- "status" => (string)$apply_->status,
- 'agent_type' => $apply_->agent_type,
- 'city' => $city
- ];
- return $this->asJson(array_merge([
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $data
- ], $readme));
- } catch (\Exception $e) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => $e->getMessage(),
- ]);
- }
- }
- public function actionApplyAgent() {
- try {
- $saas_user = get_saas_user();
- $params = post_params();
- $username = $saas_user->mobile . "_area_agent";
- //区域代理
- $type = 'admin';
- //推广代理
- if ((int)$params['type'] === 1) {
- $username = $saas_user->mobile . "_bd_agent";
- $type = 'bd_agent';
- }
- //产品代理
- if ((int)$params['type'] === 2) {
- $username = $saas_user->mobile . "_g_agent";
- $type = 'goods_agent';
- }
- $apply_ = AgentApply::findOne(['saas_user_id' => $saas_user->id, 'agent_type' => $params['type'], 'is_delete' => 0]);
- $admin_ = Admin::findOne(['saas_user_id' => $saas_user->id, 'type' => $type, 'is_delete' => 0]);
- if ($admin_) {
- throw new \Exception('当前用户已经绑定代理');
- }
- if ($apply_ && in_array($apply_->status, [0, 1])) {
- throw new \Exception('当前用户已经绑定代理或审核中');
- }
- //判断是否存在同区域同等级代理
- if ((int)$params['type'] === 0) {
- $models = [
- [
- 'type' => 'admin',
- 'model' => Admin::find()
- ], [
- 'type' => 'apply',
- 'model' => AgentApply::find()
- ]
- ];
- foreach ($models as $item) {
- $admin_query = $item['model']->where(['province_id' => $params['province_id'], 'is_delete' => 0]);
- if ($item['type'] === 'admin') {
- $admin_query->andWhere(['type' => 'admin']);
- } else {
- $admin_query->andWhere(['agent_type' => 0, 'status' => [0]]);
- }
- if ((int)$params['area_level'] === 3) {
- $admin_query->andWhere(['area_level' => 3]);
- } elseif ((int)$params['area_level'] === 2) {
- $admin_query->andWhere(['city_id' => $params['city_id'], 'area_level' => 2]);
- } elseif ((int)$params['area_level'] === 1) {
- $admin_query->andWhere(['city_id' => $params['city_id'], 'district_id' => $params['district_id'], 'area_level' => 1]);
- }
- $admin = $admin_query->asArray()->one();
- if ($admin) {
- throw new \Exception('当前区域内已存在同等级代理');
- }
- }
- }
- if (Admin::findOne(['username' => $username, 'is_delete' => 0])) {
- throw new \Exception('用户名已经存在');
- }
- // if (!empty($saas_user->mobile)) {
- // if (Admin::findOne(['mobile' => $saas_user->mobile, 'is_delete' => 0, 'type' => $type])) {
- // throw new \Exception('手机号已经存在');
- // }
- // } else {
- // throw new \Exception('当前用户手机号为空');
- // }
- //产品代理自动选择区域代理
- if (in_array((int)$params['type'], [0, 2])) {
- if (empty($params['province_id']) || empty($params['city_id']) || empty($params['district_id'])) {
- throw new \Exception('参数不全');
- }
- if ((int)$params['type'] === 2) {
- $areaAdmin = Admin::find()->where(['AND', [
- "province_id" => $params['province_id'],
- "city_id" => $params['city_id'],
- "district_id" => $params['district_id'],
- 'area_level' => 1,
- 'is_delete' => 0,
- 'type' => 'admin',
- ], ['<>', 'username', 'admin']])->asArray()->one();
- if (empty($areaAdmin)) {
- throw new \Exception('当前区域下没有区域代理');
- }
- $params['area_level'] = $areaAdmin['area_level'];
- }
- if (empty($params['area_level'])) {
- throw new \Exception('请选择代理等级');
- }
- }
- $agent_apply = $apply_ ?: new AgentApply();
- if (isset($params['province_id']) && $params['province_id']) {
- $agent_apply->province_id = $params['province_id'];
- }
- if (isset($params['city_id']) && $params['city_id']) {
- $agent_apply->city_id = $params['city_id'];
- }
- if (isset($params['district_id']) && $params['district_id']) {
- $agent_apply->district_id = $params['district_id'];
- }
- if (isset($params['address']) && $params['address']) {
- $agent_apply->address = $params['address'];
- }
- if (isset($params['lat']) && $params['lat']) {
- $agent_apply->lat = $params['lat'];
- }
- if (isset($params['lng']) && $params['lng']) {
- $agent_apply->lng = $params['lng'];
- }
- $agent_apply->area_level = !empty($params['area_level']) ? $params['area_level'] : 0;
- $agent_apply->username = $username;
- if (strlen($saas_user->mobile) < 8) {
- throw new \Exception('手机号信息错误,请清除小程序重新授权');
- }
- $mobile = (string)$saas_user->mobile;
- $password = substr($mobile, -8, 8);
- $agent_apply->password = $password;
- $agent_apply->mobile = $mobile;
- $agent_apply->name = $saas_user->name;//((int)$params['type'] === 1 ? '推广代理-' : '区域代理-') . $saas_user->name;
- $agent_apply->saas_user_id = $saas_user->id;
- $agent_apply->agent_type = (int)$params['type'];
- $agent_apply->status = 0;
- if (!$agent_apply->save()) {
- throw new \Exception(json_encode($agent_apply->errors));
- }
- return $this->asJson([
- 'code' => 0,
- 'msg' => '申请成功, 请等待审核'
- ]);
- } catch (\Exception $e) {
- return $this->asJson([
- 'code' => 1,
- 'msg' => $e->getMessage(),
- ]);
- }
- }
- }
|