$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(), ]); } } }