UserController.php 74 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654
  1. <?php
  2. namespace api\modules\v1\controllers\user;
  3. use addons\AlipayMiniInvite\common\events\InviteOldUserRewardEvent;
  4. use addons\Area\common\models\AreaCommunity;
  5. use addons\FunTown\common\helper\GameAsyncHelper;
  6. use addons\Recharge\common\models\AddonsRechargeActive;
  7. use addons\Store\common\models\Store;
  8. use addons\Store\common\models\StoreCollect;
  9. use addons\Store\common\models\StoreGoods;
  10. use api\controllers\OnAuthController;
  11. use api\services\WechatUserService;
  12. use common\enums\AddonsEnum;
  13. use common\enums\MemberLabelEnum;
  14. use common\enums\RabbitMqEnum;
  15. use common\enums\StatusEnum;
  16. use common\enums\UserWalletEnum;
  17. use common\events\user\UserModifyPayPwdEvent;
  18. use common\events\user\UserModifyPwdEvent;
  19. use common\events\user\UserUpdateEvent;
  20. use common\events\user\UserPositionChangeEvent;
  21. use common\globals\GlobalInvoke;
  22. use common\helpers\ApiCode;
  23. use common\helpers\sms\Sms;
  24. use common\logic\memberLabel\MemberLabelLogic;
  25. use common\models\common\BalanceLog;
  26. use common\models\common\CapitalLog;
  27. use common\models\common\GlobalVarMap;
  28. use common\models\common\MallSetting;
  29. use common\models\common\Region;
  30. use common\models\common\UserBankCard;
  31. use common\models\common\UserThirdPartyAddress;
  32. use common\models\goods\Goods;
  33. use common\models\goods\GoodsCollect;
  34. use common\models\mall\MallAddons;
  35. use common\models\statistics\UserStatisticsTotal;
  36. use common\models\user\MemberBenefit;
  37. use common\models\user\Town;
  38. use common\models\user\User;
  39. use common\models\user\UserAddress;
  40. use common\models\user\UserExtra;
  41. use common\models\user\UserFollow;
  42. use common\models\user\UserLevel;
  43. use common\models\user\UserInfo;
  44. use common\models\user\UserInviteCode;
  45. use common\models\user\UserLevelExpired;
  46. use common\models\user\UserPartitionRelationship;
  47. use common\models\user\UserSignAgreement;
  48. use common\models\user\UserTeamStatistics;
  49. use common\models\user\UserWallet;
  50. use common\models\user\UserWithdrawLog;
  51. use common\enums\PayTypeEnum;
  52. use common\helpers\DateHelper;
  53. use common\models\user\BalanceRechargeLog;
  54. use common\helpers\LockHelper;
  55. use common\models\user\UserFreezeCommission;
  56. use common\models\user\UserTeamProfitStatistics;
  57. use Yii;
  58. use yii\db\Exception;
  59. use yii\db\Expression;
  60. class UserController extends OnAuthController
  61. {
  62. public $modelClass = '';
  63. /**
  64. *
  65. * 不用进行登录验证的方法
  66. *
  67. * 例如: ['index', 'update', 'create', 'view', 'delete']
  68. * 默认全部需要验证
  69. *
  70. * @var array
  71. */
  72. protected $authOptional = ['login', 'mobile-login', 'sms-code', 'register', 'up-pwd','user-agree','visit', 'is-subscribe', 'address-info', 'update-user-position', 'user-info'];
  73. /**
  74. * 获取用户基本加额外信息
  75. * @return array|mixed
  76. */
  77. public function actionUserInfo()
  78. {
  79. $params = Yii::$app->request->get();
  80. $res = Yii::$app->services->validate->validate($params, [[['capital','extra','level_sel'], 'safe']]);
  81. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  82. if (empty($this->user_id)) return $this->success('请登录', ['is_login' => false]);
  83. $info = \Yii::$app->services->apiAccessToken->getTokenToCache($this->access_token, '');
  84. $user_level_arr = UserLevel::getUserLevelList($this->mall_id, 'id, name, pic_url, level');
  85. $data['user_id'] = empty($this->user_id) ? 0 : $this->user_id;
  86. $data['nickname'] = empty($info->user->nickname) ? '' : $info->user->nickname;
  87. $data['username'] = empty($info->user->username) ? '' : $info->user->username;
  88. $data['avatar_url'] = empty($info->user->avatar_url) ? '' : $info->user->avatar_url;
  89. $data['mobile'] = empty($info->user->mobile) ? '' : $info->user->mobile;
  90. $data['level_name'] = empty($user_level_arr[$info->user->level]['name']) ? MallSetting::getDefaultLevelName($this->mall_id) : $user_level_arr[$info->user->level]['name'];
  91. $data['level_pic_url'] = empty($user_level_arr[$info->user->level]['pic_url']) ? '' : $user_level_arr[$info->user->level]['pic_url'];
  92. $data['level'] = empty($info->user->level) ? 0 : $info->user->level;
  93. $data['tag'] = [];
  94. // 用户资产
  95. if (!empty($params['capital']) && !empty($this->user_id)) {
  96. $data['capital'] = UserInfo::getCapital($this->mall_id, $this->user_id, $params['capital']);
  97. }
  98. $data['extra_capital'] = UserInfo::getExtraCapital($this->user_id, $data);
  99. // 个人资料
  100. if (!empty($params['extra']) && !empty($this->user_id)) {
  101. $extra = UserExtra::getUserExtra($this->user_id);
  102. if ($extra == false) return $this->error(UserExtra::getStaticError());
  103. $extra['address'] = trim($extra['province'] .' '.$extra['city'] .' '.$extra['area']);
  104. $data = array_merge($data,$extra);
  105. }
  106. // 是否显示个人主页
  107. $personal_home_addons = MallAddons::getPersonalHomeAddons($this->mall_id,array_keys(AddonsEnum::getPersonalHomeAddonsMap()));
  108. $is_show_personal_home = !empty($personal_home_addons);
  109. $data['is_show_personal_home'] = $is_show_personal_home;
  110. if($is_show_personal_home){
  111. $extra = UserExtra::getUserExtra($this->user_id);
  112. $data['personal_home']['personal_home_bg'] = $extra['personal_home_bg'] ?? '';
  113. $data['personal_home']['personal_signature'] = $extra['personal_signature'] ?? '';
  114. }
  115. $data['third_party_address'] = '';
  116. $third_party_address = UserThirdPartyAddress::getOne([['mall_id' => $this->mall_id], ['user_id' => $this->user_id], ['status' => StatusEnum::ENABLED]], true, [], false, 'third_party_address');
  117. $data['third_party_address'] = $third_party_address['third_party_address'] ?? $data['third_party_address'];
  118. $data['invite_code'] = '';
  119. $relation_setting = \Yii::$app->services->setting->mall->get($this->mall_id, 'user_relation_setting.relation_setting');
  120. $setting = [];
  121. if (!empty($relation_setting)) $setting = json_decode($relation_setting, true);
  122. if(isset($setting['status']) && $setting['status'] == 1){
  123. if($setting['become_child_way'] == 5){
  124. $data['invite_code'] = UserInviteCode::find()->where(['mall_id' => $this->mall_id, 'user_id' => $this->user_id, 'status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]])->select('code')->scalar() ?? '';
  125. }
  126. }
  127. $model_s = UserTeamStatistics::findOne(['user_id'=>$this->user_id,'status'=>StatusEnum::ENABLED]);
  128. $data['user_direct'] =$model_s['user_direct_total']??0;
  129. $data['is_bind_popups'] = UserPartitionRelationship::getIsNeedPopups($this->mall_id, $this->user_id ?? 0);
  130. // 判断用户接收验证码的手机号是新还是旧手机号
  131. $is_accept_mobile_code = Yii::$app->services->setting->mall->get($this->mall_id, 'user.is_accept_mobile_code');
  132. $data['is_accept_mobile_code'] = !empty($is_accept_mobile_code) ? $is_accept_mobile_code : 0;
  133. $data['is_show_reserve_order_toolbar'] = MallAddons::isInstall($this->mall_id, AddonsEnum::ADDONS_NAME_RESERVATION_SERVICE) ||
  134. MallAddons::isInstall($this->mall_id, AddonsEnum::ADDONS_NAME_HOME_MAKING) ?
  135. StatusEnum::ENABLED : StatusEnum::DISABLED;
  136. $parent = [];
  137. if(!empty($this->user['parent_id'])){
  138. $parent = User::getOne([['id'=>$this->user['parent_id']]],true,[],false,'id,nickname,avatar_url,mobile');
  139. }
  140. $data['parent'] = $parent;
  141. // 返回过期时间
  142. $data['is_show_user_level_expired_at'] = (bool)Yii::$app->services->setting->mall->get($this->mall_id, 'user.is_show_user_level_expired_at');
  143. if ($data['is_show_user_level_expired_at']) {
  144. // 查询是否有过期时间
  145. $user_level_expired_at = UserLevelExpired::find()
  146. ->select('expired_at')
  147. ->where(['user_id' => $this->user_id, 'level' => $this->user->level, 'status' => StatusEnum::ENABLED])
  148. ->orderBy('id desc')
  149. ->scalar();
  150. $data['user_level_expired_at'] = $user_level_expired_at ? date('Y-m-d', $user_level_expired_at) : null;
  151. }
  152. // 用户插件身份
  153. if (!empty($params['level_sel']) && !empty($this->user_id)) {
  154. $data['level_sel'] = UserInfo::getExtraLevel($this->mall_id, $this->user_id, $params['level_sel']);
  155. }
  156. return $this->success(ApiCode::CODE_SUCCESS_TITLE, $data);
  157. }
  158. /**
  159. * 个人主页
  160. * @Author bing
  161. * @DateTime 2021-12-23 17:34:28
  162. * @copyright: Copyright (c) 2020 广东七件事集团
  163. * @return void
  164. */
  165. public function actionPersonalHome(){
  166. if (\Yii::$app->request->isGet) {
  167. $params = Yii::$app->request->get();
  168. $res = Yii::$app->services->validate->validate($params, [
  169. [['user_id','type'], 'safe'],
  170. ]);
  171. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  172. $data['personal_home_addons'] = MallAddons::getPersonalHomeAddons($this->mall_id,array_keys(AddonsEnum::getPersonalHomeAddonsMap()));
  173. //把指定得插件放到第一位
  174. if($data['personal_home_addons']) {
  175. $select_key = '';
  176. foreach ($data['personal_home_addons'] as $k => $v){
  177. if($params['type'] == $v){
  178. $select_key = $k;
  179. }
  180. }
  181. if($select_key>0){
  182. $data['personal_home_addons'][$select_key] = $data['personal_home_addons'][0];
  183. $data['personal_home_addons'][0] = $params['type'];
  184. }
  185. }
  186. if(empty($data['personal_home_addons'])) return $this->error('相关插件未安装或已禁用');
  187. $data['personal_home_addons_map'] = AddonsEnum::getPersonalHomeAddonsMap();
  188. //统计我关注的人数
  189. $user_id = $params['user_id'] ? $params['user_id']:$this->user_id;
  190. $userextra = UserExtra::getOne([['user_id' => $user_id]],true,['user']);
  191. $data['follow_count'] = $userextra['follow_count'] ? $userextra['follow_count']:0;
  192. $data['fans_count'] = $userextra['fans_count'] ? $userextra['fans_count']:0;
  193. $data['personal_home_bg'] = $userextra['personal_home_bg']?$userextra['personal_home_bg']:'';
  194. $data['personal_signature'] = $userextra['personal_signature']?$userextra['personal_signature']:'';
  195. $data['user']['username'] = $userextra['user']['username'];
  196. $data['user']['nickname'] = $userextra['user']['nickname'];
  197. $data['user']['avatar_url'] = $userextra['user']['avatar_url'];
  198. $follow = UserFollow::getOne([['follow_id' => $params['user_id']],['user_id' => $this->user_id]]);
  199. if($follow && $follow['status'] == StatusEnum::ENABLED) {
  200. $data['user']['is_follow'] = 1;
  201. }else{
  202. $data['user']['is_follow'] = 0;
  203. }
  204. return $this->success(ApiCode::CODE_SUCCESS_TITLE,$data);
  205. }
  206. }
  207. /**
  208. * @Author: ltm
  209. * @DateTime: 2021/12/24 0024 9:31
  210. * @Copyright: copyright (c) 2021 广东七件事集团
  211. * @param $data
  212. * @return bool
  213. */
  214. public function actionEditPersonalHome(){
  215. if (\Yii::$app->request->isPost) {
  216. try {
  217. $params = Yii::$app->request->post();
  218. $res = Yii::$app->services->validate->validate($params, [
  219. [['personal_home_bg','personal_signature'], 'safe'],
  220. ]);
  221. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  222. $model = UserExtra::findOne(['user_id' => $this->user_id]);
  223. $params = UserExtra::exceptNullVal($params);//去除参数的空
  224. if($params['personal_home_bg']) $model->personal_home_bg = $params['personal_home_bg'];
  225. if($params['personal_signature']) $model->personal_signature = $params['personal_signature'];
  226. if(!$model->save()){
  227. throw new Exception($model->getErrorMessage());
  228. }
  229. if ($model === false) return $this->error(UserInfo::getStaticError());
  230. return $this->success('操作成功');
  231. } catch (\Exception $e) {
  232. return $this->error($e->getMessage());
  233. }
  234. }
  235. }
  236. /**
  237. * 设置支付密码
  238. * @return mixed|void
  239. */
  240. public function actionSetTransactionPassword()
  241. {
  242. try {
  243. if (\Yii::$app->request->isPost) {
  244. $params = Yii::$app->request->post();
  245. $res = Yii::$app->services->validate->validate($params, [
  246. [['mobile', 'captcha', 'password', 'confirm_password'], 'required'],
  247. // ['mobile', 'match', 'pattern' => '/^1\d{10}$/', 'message' => '请输入正确的手机号码'],
  248. [['mobile'], 'integer'],
  249. ['password', 'string', 'length' => [6, 11]],
  250. ['confirm_password', 'compare', 'compareAttribute' => 'password', 'message' => '两次密码不一致'],
  251. ]);
  252. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  253. $user = $this->user;
  254. if($user['mobile']!=$params['mobile']) return $this->error('请输入绑定的手机号码');
  255. //短信验证码
  256. if (YII_ENV == 'prod') {
  257. if (!Sms::checkValidateCode($params['mobile'], $params['captcha'],Sms::SMS_TYPE_UPDATE_PAY_PASS)) return $this->error('验证码不正确');
  258. }
  259. $user->transaction_password = Yii::$app->getSecurity()->generatePasswordHash($params['password']);
  260. if ($user->save() === false) return $this->error(User::getStaticError());
  261. $event = new UserModifyPayPwdEvent($this->mall_id);
  262. Yii::$app->services->events->dispatch($event, [
  263. 'operator_user_id' => $this->user_id,
  264. 'mobile' => $user->mobile,
  265. 'new_pwd' => trim($params['password']),
  266. 'user_id' => $this->user_id,
  267. 'mall_id' => $this->mall_id
  268. ], $event->listeners);
  269. return $this->success('设置成功');
  270. }
  271. } catch (\Exception $e) {
  272. return $this->error($e->getMessage());
  273. }
  274. }
  275. /**
  276. * 设置登录密码
  277. * @return mixed|void
  278. */
  279. public function actionSetLoginPassword()
  280. {
  281. try {
  282. if (\Yii::$app->request->isPost) {
  283. $params = Yii::$app->request->post();
  284. $res = Yii::$app->services->validate->validate($params, [
  285. [['mobile', 'captcha', 'password', 'confirm_password'], 'required'],
  286. // ['mobile', 'match', 'pattern' => '/^1\d{10}$/', 'message' => '请输入正确的手机号码'],
  287. [['mobile'], 'integer'],
  288. ['password', 'string', 'length' => [6, 11]],
  289. ['confirm_password', 'compare', 'compareAttribute' => 'password', 'message' => '两次密码不一致'],
  290. ]);
  291. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  292. $user = $this->user;
  293. if($user['mobile']!=$params['mobile']) return $this->error('请输入绑定的手机号码');
  294. //短信验证码
  295. if (YII_ENV == 'prod') {
  296. if (!Sms::checkValidateCode($params['mobile'], $params['captcha'],Sms::SMS_TYPE_UPDATE_PASS)) return $this->error('验证码不正确');
  297. }
  298. $user->password = Yii::$app->getSecurity()->generatePasswordHash(trim($params['password']));
  299. if ($user->save() === false) return $this->error(User::getStaticError());
  300. $event = new UserModifyPwdEvent($this->mall_id);
  301. Yii::$app->services->events->dispatch($event, [
  302. 'operator_user_id' => $this->user_id,
  303. 'mobile' => $user->mobile,
  304. 'new_pwd' => trim($params['password']),
  305. 'user_id' => $this->user_id,
  306. 'mall_id' => $this->mall_id
  307. ], $event->listeners);
  308. return $this->success('设置成功');
  309. }
  310. } catch (\Exception $e) {
  311. return $this->error($e->getMessage());
  312. }
  313. }
  314. /**
  315. * 设置收货地址
  316. * @return mixed|void
  317. */
  318. public function actionUserAddressSave()
  319. {
  320. try {
  321. $params = Yii::$app->request->post();
  322. $res = Yii::$app->services->validate->validate($params, [
  323. [['province'], 'required', 'message' => '省份不能为空'],
  324. [['city'], 'required', 'message' => '城市不能为空'],
  325. [['district'], 'required', 'message' => '区县不能为空'],
  326. [['detail'], 'required', 'message' => '详细地址不能为空'],
  327. [['mobile'], 'required', 'message' => '手机号码不能为空'],
  328. [['name'], 'required', 'message' => '收货人不能为空'],
  329. ['name', 'string', 'length' => [2, 10]],
  330. // ['mobile', 'match', 'pattern' => '/^1\d{10}$/', 'message' => '请输入正确的手机号码'],
  331. [['id', 'is_default','town','town_id','province_id','city_id','district_id', 'community_id'], 'safe'],
  332. ]);
  333. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  334. $params['user_id'] = $this->user_id;
  335. if(empty($params['province_id'])&&!empty($params['province'])){
  336. $region = Region::findOne(['name'=>$params['province'],'level'=>1]);
  337. if(empty($region)) return $this->error('查无该省份');
  338. $params['province_id'] = $region['id'];
  339. }
  340. $order_config = \Yii::$app->services->setting->mall->get($this->mall_id, 'order');
  341. if (isset($order_config['limit_multi_province']) && $order_config['limit_multi_province']) {
  342. $user_address_list = UserAddress::lists([
  343. 'where' => [
  344. ['user_id' => $this->user_id],
  345. ['status' => StatusEnum::ENABLED]
  346. ],
  347. 'select' => 'province_id'
  348. ]);
  349. $address_province_ids = array_column($user_address_list, 'province_id') ?? [];
  350. $address_province_ids = array_unique($address_province_ids);
  351. $count = count($address_province_ids);
  352. if($count > 1){
  353. return $this->error('不可添加多个省份的地址,请确认~');
  354. }elseif($count == 1 && $address_province_ids[0] != $params['province_id']){
  355. return $this->error('不可添加多个省份的地址,请确认~');
  356. }
  357. }
  358. if(empty($params['city_id'])){
  359. if(empty($params['city'])) return $this->error('请填写城市');
  360. $region = Region::findOne(['name'=>$params['city'],'level'=>2]);
  361. if(empty($region)) return $this->error('查无该城市');
  362. $parent_region = Region::findOne(['id'=> $region['parent_id']]);
  363. $params['province'] = $parent_region['name'];
  364. $params['province_id'] = $region['parent_id'];
  365. $params['city_id'] = $region['id'];
  366. }
  367. if(empty($params['district_id'])&&!empty($params['district'])){
  368. $region = Region::findOne(['name'=>$params['district'],'level'=>3,'parent_id'=>$params['city_id']]);
  369. if(empty($region)) return $this->error('查无该区');
  370. $params['district_id'] = $region['id'];
  371. }
  372. // 是否有提交小区
  373. if (!empty($params['community_id'])) {
  374. $params['community'] = AreaCommunity::find()->where(['id' => $params['community_id']])
  375. ->andWhere(['status' => StatusEnum::ENABLED])->select('community_name')->scalar();
  376. if (empty($params['community'])) {
  377. return $this->error('查无该小区');
  378. }
  379. }
  380. $orderConfig = \Yii::$app->services->setting->mall->get($this->mall_id, 'order');
  381. // 必须在有第四级的情况下才需要校验
  382. if ($orderConfig['is_town'] && !$params['town'] &&
  383. !empty(Town::find()->where(['district_id' => $params['district_id']])->count())) throw new Exception('收货地址缺少镇乡');
  384. $params['detail'] = (string)$params['detail'];
  385. $address_str = $params['province'] . $params['city'] . $params['district'] . $params['town'] . $params['detail'];
  386. $result_data = \common\helpers\map\QqMapHelper::Geocoder($address_str);
  387. if($result_data['status']==0){
  388. $params['longitude'] = (string)$result_data['result']['location']['lng'];
  389. $params['latitude'] = (string)$result_data['result']['location']['lat'];
  390. }else{
  391. // throw new \Exception('获取配送地址的经纬度失败');
  392. }
  393. $res = UserAddress::setData($params);
  394. if ($res === false) return $this->error(UserAddress::getStaticError());
  395. return $this->success('操作成功',[$res->id]);
  396. } catch (\Exception $e) {
  397. return $this->error($e->getMessage());
  398. }
  399. }
  400. /**
  401. * 删除收货地址
  402. * @return mixed|void
  403. */
  404. public function actionUserAddressDelete()
  405. {
  406. try {
  407. $params = Yii::$app->request->get();
  408. $res = Yii::$app->services->validate->validate($params, [
  409. [['id'], 'required'],
  410. ]);
  411. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  412. $param = [
  413. 'id' => $params['id'],
  414. 'user_id' => $this->user_id,
  415. 'status' => StatusEnum::DELETE,
  416. ];
  417. $res = UserAddress::setData($param);
  418. if ($res === false) return $this->error(UserAddress::getStaticError());
  419. return $this->success('删除成功');
  420. } catch (\Exception $e) {
  421. return $this->error($e->getMessage());
  422. }
  423. }
  424. /**
  425. * 获取省市区数据
  426. * @return \yii\web\Response
  427. */
  428. public function actionAddressInfo()
  429. {
  430. $list = Region::getRegionList([['<>', 'parent_id', -1]], 'id,name,parent_id,level', [], 1);
  431. return $this->success('ok', $list);
  432. }
  433. /**
  434. * 收货地址列表
  435. * @return mixed|void
  436. */
  437. public function actionUserAddressList()
  438. {
  439. // 查询参数
  440. $params = Yii::$app->request->get();
  441. $where[] = ['in', 'status', [StatusEnum::DISABLED, StatusEnum::ENABLED]];
  442. $where[] = ['user_id' => $this->user_id];
  443. $params['order'] = 'id desc';
  444. $params['select'] = 'id,province,province_id,city,city_id,town,town_id,detail,district,district_id,
  445. is_default,latitude,location,longitude,mobile,name,community_id,community';
  446. $params['where'] = $where;
  447. $page_data = UserAddress::listPage($params, false);
  448. return $this->success('操作成功', $page_data);
  449. }
  450. /**
  451. * 地址详情
  452. * @return mixed|void
  453. */
  454. public function actionUserAddressDetail()
  455. {
  456. $request = Yii::$app->request;
  457. // 查询参数
  458. $where[] = ['in', 'status', [StatusEnum::DISABLED, StatusEnum::ENABLED]];
  459. $where[] = ['id' => $request->get('id', '')];
  460. $where[] = ['user_id' => $this->user_id];
  461. $user_address = UserAddress::getOne($where, true);
  462. unset($user_address['created_at']);
  463. unset($user_address['updated_at']);
  464. // 是否开启了小区代收货地址显示
  465. if (MallAddons::isInstall($this->mall_id, AddonsEnum::ADDONS_NAME_AREA)) {
  466. $enabled_community = \Yii::$app->services->setting
  467. ->addons->get($this->mall_id, AddonsEnum::ADDONS_NAME_AREA, 'basic.shopping_address_community_enable');
  468. $user_address['enabled_community'] = !empty($enabled_community) ? $enabled_community : StatusEnum::DISABLED;
  469. }
  470. return $this->success('操作成功', $user_address ? $user_address : []);
  471. }
  472. /**
  473. * 添加收货地址的时候是否需要展示小区
  474. * @return mixed|null
  475. */
  476. public function actionUserAddressCommunity()
  477. {
  478. $ret['enabled_community'] = StatusEnum::DISABLED;
  479. if (MallAddons::isInstall($this->mall_id, AddonsEnum::ADDONS_NAME_AREA)) {
  480. $enabled_community = \Yii::$app->services->setting
  481. ->addons->get($this->mall_id, AddonsEnum::ADDONS_NAME_AREA, 'basic.shopping_address_community_enable');
  482. $ret['enabled_community'] = !empty($enabled_community) ? $enabled_community : StatusEnum::DISABLED;
  483. }
  484. return $this->success('操作成功', $ret);
  485. }
  486. /**
  487. * 获取乡镇列表
  488. * @return mixed|void
  489. */
  490. public function actionGetTownList()
  491. {
  492. $params = Yii::$app->request->get();
  493. $res = Yii::$app->services->validate->validate($params, [
  494. [['district_id'], 'required', 'message' => 'district_id必传'],
  495. ]);
  496. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  497. $where[] = ['district_id' => !empty($params['district_id']) ? $params['district_id'] : ''];
  498. $params['order'] = 'id desc';
  499. $params['select'] = 'id,adcode,name';
  500. $params['limit'] = 100;
  501. $params['where'] = $where;
  502. $page_data = Town::listPage($params, false);
  503. return $this->success('操作成功', $page_data);
  504. }
  505. /**
  506. * 我的客户
  507. * @return mixed|void
  508. */
  509. public function actionMyCustomer()
  510. {
  511. $params = Yii::$app->request->get();
  512. $res = Yii::$app->services->validate->validate($params, [
  513. [['level'], 'required', 'message' => 'level,必传'],
  514. ]);
  515. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  516. //渠道数据配置
  517. $team_data_config = \Yii::$app->services->setting->addons->get($this->mall_id, AddonsEnum::ADDONS_NAME_TEAM_DATA, 'basic');
  518. $user_custom_text = \Yii::$app->services->setting->mall->get($this->mall_id, 'user_custom_text');
  519. $user_operate = \Yii::$app->services->setting->mall->get($this->mall_id, 'user_operate');
  520. $params['user_id'] = $this->user_id;
  521. $params['user_custom_text'] = $user_custom_text ?? [];
  522. // $data = UserRelationship::myCustomer($params);
  523. $data = UserPartitionRelationship::myCustomer($params, $team_data_config);
  524. $data['nickname'] = $this->user->nickname??'';
  525. $data['avatar_url'] = $this->user->avatar_url??'';
  526. $data['level_name'] = MallSetting::getDefaultLevelName($this->mall_id);
  527. $data['authority'] = '0';// 是否可查看运营数据0=否,1=是
  528. $data['is_show_user_parent'] = !empty($user_operate['is_show_user_parent']);
  529. if (!empty($data['is_show_user_parent']) && !empty($this->user['parent_id'])) {
  530. $data['p_user'] = User::getUser([['id' => $this->user['parent_id'], 'mall_id' => $this->mall_id]], 'id, mobile, level, nickname, avatar_url', [], 1);
  531. }
  532. $authority = Yii::$app->services->setting->mall->get($this->mall_id, 'user_operate.authority');
  533. if($authority){
  534. $authority = json_decode($authority, true);
  535. if (in_array($this->user->level, $authority) || in_array('-1', $authority)) $data['authority'] = '1';
  536. }
  537. // 获取用户权重列表
  538. $level_arr = UserLevel::getUserLevelArr($this->mall_id);
  539. $level_list = $level_arr;
  540. $level_list[0] = MallSetting::getDefaultLevelName($this->mall_id);
  541. $data['level_arr'] = array_merge([0 => MallSetting::getDefaultLevelName($this->mall_id)], $level_arr);
  542. if(!empty($this->user->level)) $data['level_name'] =$level_list[$this->user->level];
  543. if (!empty($data['p_user'])) {
  544. $data['p_user']['level_name'] = $level_list[$data['p_user']['level']] ?? '';
  545. }
  546. $data['team_info'] = UserTeamProfitStatistics::getTeamInfo($this->mall_id, $params['user_id']);
  547. $data['team_finish_order_price_total'] = UserTeamStatistics::getTeamFinishOrderPriceTotal($params['user_id']);
  548. return $this->success('操作成功',$data );
  549. }
  550. /**
  551. * 我的客户列表
  552. * @return mixed|void
  553. */
  554. public function actionMyChildrenList()
  555. {
  556. $params = Yii::$app->request->get();
  557. $res = Yii::$app->services->validate->validate($params, [
  558. [['level'], 'required', 'message' => 'level,必传'],
  559. [['sort', 'keyword', 'type', 'sort_type'], 'string'],
  560. [['user_level'], 'integer'],
  561. ]);
  562. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  563. $params['user_id'] = $this->user_id;
  564. $params['mall_id'] = $this->mall_id;
  565. $data = UserPartitionRelationship::myChildrenList($params);
  566. return $this->success('操作成功', $data);
  567. }
  568. /**
  569. * 我的客户的业绩订单
  570. *
  571. * @return void
  572. */
  573. public function actionMyChildrenOrders()
  574. {
  575. $params = Yii::$app->request->get();
  576. $res = Yii::$app->services->validate->validate($params, [
  577. [['level'], 'required', 'message' => 'level,必传'],
  578. ]);
  579. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  580. $params['user_id'] = $this->user_id;
  581. $params['mall_id'] = $this->mall_id;
  582. $data = UserPartitionRelationship::myChildrenOrders($params);
  583. return $this->success('操作成功', $data);
  584. }
  585. /**
  586. * 我的钱包
  587. * @return mixed|void
  588. */
  589. public function actionBalanceDetail()
  590. {
  591. $params = \Yii::$app->request->get();
  592. $where = [];
  593. $where[] = ['mall_id' => $this->mall_id];
  594. $where[] = ['user_id' => $this->user_id];
  595. $where[] = ['status' => [0,1]];
  596. if (!empty($params['search_type'])) $where[] = ['from_type' => $params['search_type']];
  597. if(!empty($params['date'])){
  598. $start_time = $params['date'] . ' 00:00:00';
  599. $end_time = strtotime(date('Ymd', strtotime($params['date'] . '+1 month -1 day')) . ' 23:59:59');
  600. $where[] = ['>=', 'created_at', $start_time];
  601. $where[] = ['<=', 'created_at', $end_time];
  602. }
  603. $params['order'] = 'id desc';
  604. $params['select'] = 'id,change_type,desc,change_num ,current_num ,created_at,from_type,capital_type';
  605. $params['where'] = $where;
  606. $list = BalanceLog::listPage($params);
  607. $list['from_type_map'] = GlobalVarMap::getEnumMapByType('FromTypeMap', $this->mall_id);
  608. $list['capital_type_map'] = GlobalVarMap::getEnumMapByType('CapitalTypeMap');
  609. // $list['from_type_map'] = UserWalletEnum::getFromTypeMapByBalance();
  610. // $list['capital_type_map'] = UserWalletEnum::getCapitalTypeMapByBalance();
  611. return $this->success('操作成功', $list);
  612. }
  613. /**
  614. * @return mixed|void
  615. */
  616. public function actionBalanceLogTotal()
  617. {
  618. $params = \Yii::$app->request->get();
  619. $res = Yii::$app->services->validate->validate($params, [
  620. [['date'], 'required', 'message' => 'date,必传'],
  621. [['search_type'], 'safe'],
  622. ]);
  623. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  624. $where = [];
  625. $where[] = ['mall_id' => $this->mall_id];
  626. $where[] = ['user_id' => $this->user_id];
  627. $where[] = ['status' => [0,1]];
  628. if (!empty($params['search_type'])) $where[] = ['from_type' => $params['search_type']];
  629. if(!empty($params['date'])){
  630. $start_time = strtotime($params['date'] . ' 00:00:00');
  631. $end_time = strtotime(date('Ymd', strtotime($params['date'] . '+1 month -1 day')) . ' 23:59:59');
  632. $where[] = ['>=', 'created_at', $start_time];
  633. $where[] = ['<=', 'created_at', $end_time];
  634. }
  635. $params['order'] = 'id desc';
  636. $params['select'] = 'change_type,sum(change_num) as change_num';
  637. $params['where'] = $where;
  638. $params['group'] = 'change_type';
  639. $list = BalanceLog::listPage($params);
  640. $add = $sub = 0;
  641. if (!empty($list['list'])) {
  642. foreach ($list['list'] as $v) {
  643. if ($v['change_type'] == 'add') $add = $v['change_num'];
  644. if ($v['change_type'] == 'sub') $sub = $v['change_num'];
  645. }
  646. }
  647. return $this->success('操作成功', ['add' => $add, 'sub' => abs($sub)]);
  648. }
  649. /**
  650. * 余额明细搜索类型
  651. * @return array
  652. */
  653. public function actionBalanceDetailFrom()
  654. {
  655. return $this->success('操作成功', UserWalletEnum::getFromTypeMapByBalance($this->mall_id));
  656. }
  657. /**
  658. * 获取用户信息
  659. * @return array|mixed
  660. */
  661. public function actionGetUser()
  662. {
  663. $params = \Yii::$app->request->get();
  664. $res = Yii::$app->services->validate->validate($params, [
  665. [['user_id'], 'required', 'message' => 'user_id,必传'],
  666. [['type'], 'safe'],
  667. ]);
  668. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  669. $where[] = ['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED];
  670. if($params['type'] == 1){
  671. $where[] = ['mobile' => $params['user_id']];
  672. }else if($params['type'] == 2){
  673. $where[] = [
  674. 'or',
  675. ['like', 'mobile', $params['user_id']],
  676. ['like', 'id', $params['user_id']],
  677. ];
  678. }else{
  679. $where[] = ['id' => $params['user_id']];
  680. }
  681. $user = User::getOne($where, true, [], false, 'id,avatar_url,mall_id,mobile,nickname,status,birthday');
  682. if (empty($user)) return $this->error('暂无此用户');
  683. return $this->success(ApiCode::CODE_SUCCESS_TITLE, $user);
  684. }
  685. /**
  686. * @desc:修改个人资料
  687. * @Author: hua
  688. * @Date: 2021/10/28
  689. * @Time: 9:56
  690. * @Copyright: copyright (c) 2021 广东七件事集团
  691. * @return mixed|void
  692. */
  693. public function actionUpdateData()
  694. {
  695. try {
  696. $params = Yii::$app->request->post();
  697. $res = Yii::$app->services->validate->validate($params, [
  698. [['nickname'], 'required'],
  699. [[ 'avatar_url','birthday','province','city','area', 'town'], 'string'],
  700. [['sex'], 'integer'],
  701. [['wechat', 'mobile'], 'safe'],
  702. [['new_mobile', 'captcha'], 'string'],
  703. ['new_mobile', 'match', 'pattern' => '/^1\d{10}$/', 'message' => '请输入正确的手机号码'],
  704. ]);
  705. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  706. $params = array_filter($params);
  707. $old_mobile = '';
  708. if(isset($params['new_mobile']) && !empty($params['new_mobile'])){
  709. //检验新的手机号
  710. if($params['new_mobile'] == $this->user['mobile']){
  711. throw new \Exception('手机号与原手机号一致');
  712. }
  713. $captcha = $params['captcha'] ?? '';
  714. if(!$captcha){
  715. throw new \Exception('请输入验证码');
  716. }
  717. $where[] = ['mall_id' => $this->mall_id];
  718. $where[] = ['mobile' => $params['new_mobile']];
  719. if (User::getUser($where)) throw new \Exception( '手机号码已注册');
  720. //验证码
  721. // 判断用户接收验证码的手机号是新还是旧手机号
  722. $is_accept_mobile_code = Yii::$app->services->setting->mall->get($this->mall_id, 'user.is_accept_mobile_code');
  723. $data['is_accept_mobile_code'] = !empty($is_accept_mobile_code) ? $is_accept_mobile_code : 0;
  724. $verification_mobile = $data['is_accept_mobile_code'] == 1 ? $params['new_mobile'] : $params['mobile'];
  725. if (!Sms::checkValidateCode($verification_mobile, $params['captcha'],Sms::SMS_TYPE_UPDATE_MOBILE)) throw new \Exception('验证码不正确');
  726. // 验证好玩小镇那边是否已经存在当前号码
  727. if (MallAddons::isInstall($this->mall_id, AddonsEnum::ADDONS_NAME_FUN_TOWN)) {
  728. GameAsyncHelper::validMobile($this->mall_id, $params['new_mobile']);
  729. }
  730. $params['mobile'] = $params['new_mobile'];
  731. $params['username'] = $params['new_mobile'];
  732. $params['mall_id'] = $this->mall_id;
  733. unset($params['captcha']);
  734. unset($params['new_mobile']);
  735. // 取出之前的手机号码
  736. $old_mobile = User::find()->where(['id' => $this->user_id])->select('mobile')->scalar();
  737. }
  738. $params['id'] = $this->user_id;
  739. $res = User::change($params);
  740. if ($res === false) return $this->error(User::getStaticError());
  741. // 保存额外数据
  742. $params['user_id'] = $this->user_id;
  743. $res = UserExtra::setData($params);
  744. if ($res === false) return $this->error(UserExtra::getStaticError());
  745. \Yii::$app->services->rabbitMq->push(RabbitMqEnum::EXCHANGE_TASK, RabbitMqEnum::TASK_QUEUE, [
  746. 'user_id'=>$this->user_id,
  747. 'mall_id'=>$this->mall_id,
  748. 'cate_ids'=>[MemberLabelEnum::USER_AGE]
  749. ], MemberLabelLogic::class, 'executeSyncCateId11');
  750. // 触发用户信息修改事件
  751. $event = new UserUpdateEvent($this->mall_id);
  752. Yii::$app->services->events->dispatch($event,['user_id'=>$this->user_id, 'mall_id' => $this->mall_id, 'old_mobile' => $old_mobile], $event->listeners);
  753. return $this->success('操作成功');
  754. } catch (\Exception $e) {
  755. return $this->error($e->getMessage());
  756. }
  757. }
  758. /**
  759. * @desc:会员权益
  760. * @Author: hua
  761. * @Date: 2021/10/29
  762. * @Time: 15:06
  763. * @Copyright: copyright (c) 2021 广东七件事集团
  764. * @return mixed|void
  765. */
  766. public function actionMemberBenefitList()
  767. {
  768. $params['mall_id'] = $this->mall_id;
  769. $params['user_id'] = $this->user_id;
  770. $params['level'] = $this->user->level;
  771. $result = MemberBenefit::memberBenefitList($params);
  772. return $this->success('操作成功', $result);
  773. }
  774. /**
  775. * @desc:充值配置
  776. * @Author: hua
  777. * @Date: 2021/10/29
  778. * @Time: 15:32
  779. * @Copyright: copyright (c) 2021 广东七件事集团
  780. * @return mixed|void
  781. */
  782. public function actionRechargeConfig()
  783. {
  784. $setting = Yii::$app->services->setting->mall->get($this->mall_id, 'recharge');
  785. $config['is_open_recharge'] = isset($setting['is_open_recharge']) ? $setting['is_open_recharge'] : 0;
  786. $config['is_user_recharge_num'] = isset($setting['is_user_recharge_num']) ? $setting['is_user_recharge_num'] : 0;
  787. $config['min_recharge_num'] = isset($setting['min_recharge_num']) ? $setting['min_recharge_num'] : 0.1;
  788. $list = isset($setting['def_recharge_num']) ? \Qiniu\json_decode($setting['def_recharge_num']) : [100, 200, 300];
  789. $config['has_give'] = 0;
  790. $has_addons = Yii::$app->services->addons->findByName(AddonsEnum::ADDONS_NAME_RECHARGE);
  791. $config['active'] = [];
  792. if ($has_addons) {
  793. if(MallAddons::isInstall($this->mall_id,'Recharge')){
  794. $active = new AddonsRechargeActive();
  795. $config['active'] = $active->getNowActive($this->mall_id);
  796. if (!empty($config['active'])) {
  797. $give_info = [];
  798. if(!empty($config['active']['give_info'])){
  799. foreach ($config['active']['give_info'] as $k=>$v){
  800. if(in_array($v['num'],$list)){
  801. $give_info[] = $v;
  802. }
  803. }
  804. $config['active']['give_info'] = $give_info;
  805. }
  806. $config['has_give'] = !empty($give_info)?1:0;
  807. }
  808. }
  809. }
  810. // $list = [];
  811. // for ($i = 1; $i <= 6; $i++) {
  812. // if ($setting['recharge_money' . $i] <= 0) continue;
  813. // $list[] = [
  814. // 'id' => $i,
  815. // 'recharge_money' => $setting['recharge_money' . $i],
  816. // 'give_money' => $setting['give_money' . $i],
  817. // ];
  818. // }
  819. $pay_type_list = Yii::$app->services->pay->getEnablePayment($this->mall_id, $this->platform);
  820. //线下充值
  821. $config['is_open_recharge_offline'] = $setting['is_open_recharge_offline'] ?? 0;
  822. if (!empty($setting['is_open_recharge_offline'])) {
  823. $protocol = ($_SERVER['SERVER_PORT'] == 443 ? 'https' : 'http') . '://';
  824. $host = $protocol.$_SERVER['HTTP_HOST'];
  825. $name = PayTypeEnum::getMap()[PayTypeEnum::OFFLINE] ?? '';
  826. $pay_type_list[] = [
  827. 'action' => "offline",
  828. 'img' => "{$host}/resources/img/payment/balance.png",
  829. 'name' => $name,
  830. 'pay_type' => PayTypeEnum::OFFLINE,
  831. 'trade_type' => "offline",
  832. 'qrcode_url' => $setting['recharge_offline_code'] ?? '',
  833. ];
  834. }
  835. $balance = UserWallet::find()->select('balance')->where(['user_id' => $this->user_id])->scalar();
  836. return $this->success('操作成功', ['list' => $list,'config'=>$config, 'payment' => $pay_type_list, 'balance' => $balance]);
  837. }
  838. /**
  839. * 保存提现银行卡信息
  840. * User: wniu
  841. * Date: 2021/11/4
  842. * Time: 10:27 上午
  843. *
  844. */
  845. public function actionAddUserBankCard()
  846. {
  847. $params = Yii::$app->request->post();
  848. $res = Yii::$app->services->validate->validate($params, [
  849. [['payer_name', 'bank', 'bank_card_no', 'id'], 'string'],
  850. ]);
  851. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  852. $params['user_id'] = $this->user_id;
  853. $params['mall_id'] = $this->mall_id;
  854. $model = new UserBankCard();
  855. $history = [];
  856. if ($params['id']) {
  857. $history = $model->getOne([['id' => $params['id']]]);
  858. }
  859. if ($history) {
  860. $history->bank = $params['bank'];
  861. $history->bank_card_no = $params['bank_card_no'];
  862. $history->payer_name = $params['payer_name'];
  863. $history->save();
  864. } else {
  865. $model->attributes = $params;
  866. $model->save();
  867. }
  868. $list = $model->lists(['where' => [['user_id' => $this->user_id], ['status' => StatusEnum::ENABLED]], 'select' => 'bank,bank_card_no,id,payer_name,is_delete']);
  869. return $this->success('操作成功', $list);
  870. }
  871. public function actionDelBankCard()
  872. {
  873. $params = Yii::$app->request->post();
  874. $res = Yii::$app->services->validate->validate($params, [
  875. [['id'], 'required', 'message' => 'id,必传'],
  876. ]);
  877. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  878. $card = UserBankCard::getOne([['id' => $params['id']]]);
  879. $card->status = StatusEnum::DELETE;
  880. $card->save();
  881. return $this->success('操作成功');
  882. }
  883. /**
  884. * @desc:推广中心
  885. * @Author: hua
  886. * @Date: 2021/11/12
  887. * @Time: 9:12
  888. * @Copyright: copyright (c) 2021 广东七件事集团
  889. * @return mixed|void
  890. */
  891. public function actionPromotionCenter()
  892. {
  893. $data = [];
  894. $user_wallet_model = UserWallet::findOne(['user_id'=>$this->user_id]);
  895. $total_commission = $user_wallet_model['total_commission']??0;
  896. $frozen_commission = $user_wallet_model['frozen_commission']??0;
  897. $data['frozen_commission'] = $frozen_commission;
  898. $data['total_commission'] = $total_commission+$frozen_commission;
  899. $data['commission'] = $user_wallet_model['commission']??0;
  900. $ori_data = $data;
  901. $data = GlobalInvoke::exec(GlobalInvoke::INVOKE_PROMOTION_CENTER, $this->mall_id,['mall_id' => $this->mall_id,'user_id' => $this->user_id, 'data' => $data]);
  902. if(is_array($data) && empty($data)){
  903. $data = $ori_data;
  904. }
  905. $data['ori_commission'] = $data['commission'];
  906. $freeze_commission = UserFreezeCommission::handlePromotionCenter($this->mall_id, $this->user_id);
  907. if(!empty($freeze_commission['is_freeze_commission'])){
  908. $data['commission'] -= $freeze_commission['freeze_amount'];
  909. $data['commission'] < 0 && $data['commission'] = 0;
  910. }
  911. $data = array_merge($data, $freeze_commission);
  912. $data['today_income'] = 0;
  913. //计算今日收益
  914. $today = date('Y-m-d',time());
  915. $start_time = strtotime($today.' 00:00:00');
  916. $end_time = strtotime($today.' 23:59:59');
  917. $wallet_type = 'commission';
  918. CapitalLog::$capitalType = "{$wallet_type}_frozen";
  919. $today_frozen_commission = CapitalLog::find()
  920. ->where(['user_id'=>$this->user_id,'status' => CapitalLog::STATUS_WAIT_SEND,'change_type'=>'add'])
  921. ->andWhere(['<=','created_at',$end_time])->andWhere(['>=','created_at',$start_time])->sum('change_num');
  922. CapitalLog::$capitalType = "{$wallet_type}";
  923. $today_commission = CapitalLog::find()
  924. ->where(['user_id' => $this->user_id, 'change_type' => 'add'])
  925. ->andWhere(['<=', 'created_at', $end_time])->andWhere(['>=', 'created_at', $start_time])
  926. ->andWhere(['<>','capital_type','withdrawal_refund'])
  927. ->sum('change_num');
  928. if($today_frozen_commission) $data['today_income']+=$today_frozen_commission;
  929. if($today_commission) $data['today_income'] += $today_commission;
  930. $data['today_frozen_income'] = $today_frozen_commission ?: 0; // 今日未结算收益
  931. $data['today_settled_income'] = $today_commission ?: 0; // 今日已结算收益
  932. //累计增值
  933. $total_added_num = CapitalLog::find()->where(['mall_id' => $this->mall_id, 'user_id' => $this->user_id, 'from_type' => 'ScoreGame', 'capital_type' => 'day_increment'])->sum('change_num');
  934. $yesterDay = DateHelper::yesterDay();
  935. $yesteday_added_num = CapitalLog::find()
  936. ->where(['mall_id' => $this->mall_id, 'user_id' => $this->user_id, 'from_type' => 'ScoreGame', 'capital_type' => 'day_increment'])
  937. ->andWhere(['>=', 'created_at', $yesterDay['start']])
  938. ->andWhere(['<=', 'created_at', $yesterDay['end']])
  939. ->sum('change_num');
  940. $data['total_added_num'] = $total_added_num ?? 0; //总增值
  941. $data['yesteday_added_num'] = $yesteday_added_num ?? 0; //昨日增值
  942. $data['total_cash'] = $user_wallet_model['commission_withdrawal']??0;
  943. // 公共调用获取个人应用佣金数额
  944. $data['list'] = GlobalInvoke::exec(GlobalInvoke::PROMOTION, $this->mall_id,['mall_id' => $this->mall_id,'user_id' => $this->user_id]);
  945. // 总收益减去提现驳回的
  946. // $back_m = UserWithdrawLog::find()->where(['user_id'=>$this->user_id,'status'=>UserWithdrawLog::STATUS_ALREADY_REJECT])->sum('num');
  947. // if($data['total_commission'] - $back_m > 0){
  948. // $data['total_commission'] -= $back_m;
  949. // }
  950. $show_type = \Yii::$app->services->setting->mall->get($this->mall_id, 'withdraw_income');
  951. $data['show_type'] = $show_type['show_type'] ?? 1;
  952. $data['is_show_today_income'] = $show_type['is_show_today_income'] ?? 1;
  953. $data['promotion_center_title'] = $show_type['promotion_center_title'] ?? '推广中心';
  954. return $this->success('操作成功',$data);
  955. }
  956. /**
  957. * @desc:我的收藏
  958. * @Author: hua
  959. * @Date: 2021/11/12
  960. * @Time: 14:20
  961. * @Copyright: copyright (c) 2021 广东七件事集团
  962. * @return mixed|void
  963. */
  964. public function actionCollectList(){
  965. $params = Yii::$app->request->get();
  966. $res = Yii::$app->services->validate->validate($params, [
  967. [['type'], 'required', 'message' => '类型必传'],
  968. ]);
  969. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  970. $list = [];
  971. switch ($params['type']){
  972. case 'goods':
  973. $params['where'] = [['gc.user_id'=>$this->user_id],['g.status'=>StatusEnum::ENABLED],['gc.status'=>StatusEnum::ENABLED]];
  974. $params['alias'] = 'gc';
  975. $params['join'] = [['left join',Goods::tableName().' as g','gc.goods_id=g.id']];
  976. $params['select'] = 'gc.goods_id,g.goods_name,g.cover_pic,g.price';
  977. $list = GoodsCollect::listPage($params);
  978. break;
  979. case 'store':
  980. if(MallAddons::isInstall($this->mall_id,'Store')){
  981. $params['where'] = [['sc.user_id'=>$this->user_id],['s.status'=>StatusEnum::ENABLED],['sc.status'=>StatusEnum::ENABLED]];
  982. $params['alias'] = 'sc';
  983. $params['join'] = [['left join',Store::tableName().' as s','sc.store_id=s.id']];
  984. $params['select'] = 's.id,s.store_name,s.logo_img,s.store_desc,s.sales_volume,s.praise,s.show_style,s.address,s.business_time_end,s.business_time_start';
  985. $list = StoreCollect::listPage($params);
  986. if(!empty($list['list'])){
  987. $store_ids = array_column($list['list'],'id');
  988. $goods_list =StoreGoods::lists([
  989. 'where'=>[['store_id'=>$store_ids]],
  990. 'select'=>'sum(virtual_sales) as virtual_sales,sum(sales_num) as sales_num,store_id',
  991. 'group'=>'store_id',
  992. 'index'=>'store_id'
  993. ]);
  994. foreach ($list['list'] as &$item){
  995. if(isset($goods_list[$item['id']])){
  996. $item['sales_volume'] =$goods_list[$item['id']]['virtual_sales']+$goods_list[$item['id']]['sales_num'];
  997. }
  998. }
  999. }
  1000. }
  1001. break;
  1002. }
  1003. return $this->success('操作成功',$list);
  1004. }
  1005. /**
  1006. * @desc:收益明细
  1007. * @Author: hua
  1008. * @Date: 2021/11/23
  1009. * @Time: 16:25
  1010. * @Copyright: copyright (c) 2021 广东七件事集团
  1011. * @return mixed|void
  1012. */
  1013. public function actionCommissionList(){
  1014. $get = Yii::$app->request->get();
  1015. $where[]=['mall_id'=>$this->mall_id];
  1016. $where[]=['user_id'=>$this->user_id];
  1017. // $where[]=['change_type'=>CapitalLog::CHANGE_TYPE_ADD];
  1018. if(!empty($get['from_type'])){
  1019. $where[]=['from_type'=>$get['from_type']];
  1020. }else{
  1021. // $from_type = GlobalVarMap::getEnumNameByType( 'IncomeMap',$this->mall_id);
  1022. // $from_type[] = 'StoreBoss'; //门店股东
  1023. // $where[]=['from_type'=>$from_type];
  1024. }
  1025. if(isset($get['show_type'])&&$get['show_type']==1){
  1026. $time_type = $get['time_type']??1;
  1027. $created_at_condition = time()-7*24*3600;
  1028. switch ($time_type){
  1029. case 2:
  1030. $created_at_condition = time()-30*24*3600;
  1031. break;
  1032. case 3:
  1033. $created_at_condition=strtotime(date("Y-m-d",mktime(0,0,0,date("m")-3,date("d"),date("Y")))." 00:00:00");
  1034. break;
  1035. }
  1036. if ($time_type == 4) {
  1037. if (!empty($get['start_time']) && !empty($get['end_time'])) {
  1038. $start_time = strtotime($get['start_time'] . '00:00:00');
  1039. $end_time = strtotime($get['end_time'] . '23:59:59');
  1040. $where[] = ['between', 'created_at', $start_time, $end_time];
  1041. }
  1042. } else {
  1043. $where[] = ['>=', 'created_at',$created_at_condition];
  1044. }
  1045. }
  1046. //过滤该插件
  1047. $where[]= ['!=', 'from_type', AddonsEnum::ADDONS_NAME_NI_BAO_ACHIEVEMENT];
  1048. if(!empty($get['date'])){
  1049. $start_time = strtotime($get['date'] . ' 00:00:00');
  1050. $end_time = strtotime(date('Ymd', strtotime($get['date'] . '+1 month -1 day')) . ' 23:59:59');
  1051. $where[] = ['>=', 'created_at', $start_time];
  1052. $where[] = ['<=', 'created_at', $end_time];
  1053. }
  1054. $wallet_type = 'commission';
  1055. $type = 'frozen';
  1056. if(!empty($get['type'])&&$get['type']==$type){
  1057. //冻结
  1058. CapitalLog::$capitalType = "{$wallet_type}_{$type}";
  1059. $where[] = ['status'=>StatusEnum::DISABLED];
  1060. }else{
  1061. CapitalLog::$capitalType = "{$wallet_type}";
  1062. }
  1063. $where[]=['change_type'=>CapitalLog::CHANGE_TYPE_ADD];
  1064. $params['where'] = $where;
  1065. $list = CapitalLog::listPage(array_merge($params, ['order' => 'id desc']));
  1066. if (!empty($list['list'])) {
  1067. foreach ($list['list'] as &$item) {
  1068. $item['show_detail'] = 0;
  1069. if(substr($item['order_no'], 0, 3) == 'CSF'){
  1070. $item['show_detail'] = 2;
  1071. } else if (substr($item['order_no'], 0, 1) == 's' && is_numeric(substr($item['order_no'], 1, 1))) {
  1072. $item['show_detail'] = 1;
  1073. }
  1074. }
  1075. }
  1076. $select = new Expression("sum(change_num) as total_commission,FROM_UNIXTIME(created_at,'%Y-%m') as times");
  1077. $params['select'] = $select;
  1078. $params['group'] = 'times';
  1079. $where[] = ['<>','capital_type','withdrawal_refund'];
  1080. $params['where'] = $where;
  1081. $result = CapitalLog::lists($params);
  1082. $list['total_list'] = array_column($result,'total_commission','times');
  1083. $list['form_type'] = GlobalVarMap::getEnumMapByType('IncomeMap', $this->mall_id);
  1084. foreach ($list['form_type'] as $addons=>$addons_name) {
  1085. if(in_array($addons,['ConsumeGift','MembersCard','OperatingNote','UserGift','GroupRush','Recommen','Sudoku','CloudStockTwo','CloudStock','LuckyGroup','Bargain','BlindBox','BlindBoxs',
  1086. 'TeamLevel','Seed','EightOutTwo','Rebate'])){
  1087. unset($list['form_type'][$addons]);
  1088. continue;
  1089. }
  1090. $configs = \Yii::$app->services->setting->addons->get($this->mall_id, $addons, 'basic');
  1091. if((isset($configs['is_show_at_center'])&&$configs['is_show_at_center']==0)){
  1092. unset($list['form_type'][$addons]);
  1093. }
  1094. }
  1095. return $this->success('操作成功',$list);
  1096. }
  1097. /**
  1098. * @desc:提现明细汇总部分
  1099. * @Author: hua
  1100. * @Date: 2021/11/24
  1101. * @Time: 14:35
  1102. * @Copyright: copyright (c) 2021 广东七件事集团
  1103. * @return mixed|void
  1104. */
  1105. public function actionCashDetail(){
  1106. $user_wallet = UserWallet::findOne(['mall_id'=>$this->mall_id,'user_id'=>$this->user_id]);
  1107. $data['commission'] = $user_wallet['commission']??0;//可用佣金
  1108. $freeze_commission = UserFreezeCommission::handlePromotionCenter($this->mall_id, $this->user_id);
  1109. if(!empty($freeze_commission['is_freeze_commission'])){
  1110. $data['commission'] -= $freeze_commission['freeze_amount'];
  1111. $data['commission'] < 0 && $data['commission'] = 0;
  1112. }
  1113. // $data['commission_withdrawal'] = $user_wallet['commission_withdrawal']??0;//已提现得总佣金
  1114. $start_time = strtotime(date('Y-m-1',time()). ' 00:00:00');
  1115. $end_time = strtotime(date('Y-m-t',time()). ' 23:59:59');
  1116. $where = ['user_id'=>$this->user_id,'mall_id'=>$this->mall_id,'from'=>1,'status'=>[UserWithdrawLog::STATUS_ALREADY_TRANS]];
  1117. $commission_withdrawal = UserWithdrawLog::find()->where($where)
  1118. ->sum('num');
  1119. $data['commission_withdrawal'] = $commission_withdrawal??0;//已提现得总佣金
  1120. $month_num = UserWithdrawLog::find()->where($where)
  1121. ->andWhere(['>=', 'created_at', $start_time])->andWhere(['<=', 'created_at', $end_time])
  1122. ->sum('num');
  1123. $monday_time = strtotime(date('Y-m-d', (time() - ((date('w') == 0 ? 7 : date('w')) - 1) * 24 * 3600)).' 00:00:00');
  1124. $sunday_time = strtotime(date('Y-m-d', (time() + (7 - (date('w') == 0 ? 7 : date('w'))) * 24 * 3600)).' 23:59:59');
  1125. $week_num = UserWithdrawLog::find()->where($where)
  1126. ->andWhere(['>=', 'created_at', $monday_time])->andWhere(['<=', 'created_at', $sunday_time])
  1127. ->sum('num');
  1128. $data['month_num'] = $month_num??0;
  1129. $data['week_num'] = $week_num??0;
  1130. //手续费名称
  1131. $payment = \Yii::$app->services->setting->mall->get($this->mall_id, 'withdraw_income');
  1132. $data['poundage_name'] = isset($payment['profit_withdrawal_rule']) ? $payment['profit_withdrawal_rule'] : '手续费';
  1133. return $this->success('操作成功',$data);
  1134. }
  1135. /**
  1136. * @desc:提现列表
  1137. * @Author: hua
  1138. * @Date: 2021/11/24
  1139. * @Time: 14:46
  1140. * @Copyright: copyright (c) 2021 广东七件事集团
  1141. * @return mixed|void
  1142. */
  1143. public function actionCashList(){
  1144. $params = $get = Yii::$app->request->get();
  1145. $where[]=['mall_id'=>$this->mall_id];
  1146. $where[]=['user_id'=>$this->user_id];
  1147. $where[]=['from'=>1];
  1148. if(isset($params['status'])&&$params['status']!=='') $where[]=['status'=>$params['status']];
  1149. $params['where'] = $where;
  1150. $params['select'] = 'id,mall_id,user_id,order_no,num,actual_num,poundage_num,poundage_ratio,content,status,type';
  1151. $params['order'] = 'id desc';
  1152. $list = UserWithdrawLog::listPage($params);
  1153. $list['status_map'] = UserWithdrawLog::$status_msg;
  1154. $list['type_map'] = UserWithdrawLog::$type_msg;
  1155. if (!empty($list['list'])) {
  1156. $ids = array_column($list['list'], 'id');
  1157. if ($this->platform == 'mp-wx') {
  1158. $mini_program = Yii::$app->services->setting->mall->get($this->mall_id, 'mini_program', true);
  1159. $appId = $mini_program['app_id'] ?? '';
  1160. } else {
  1161. $wechat_conf = Yii::$app->services->setting->mall->get($this->mall_id, 'wechat', true);
  1162. $appId = $wechat_conf['app_id'] ?? '';
  1163. }
  1164. $wechat_transfer_log_list = \common\models\wechat\WechatTransferLog::lists(['where' => [['cash_id' => $ids]], 'index' => 'cash_id']);
  1165. foreach ($list['list'] as &$v) {
  1166. $v['content'] = json_decode($v['content'], true);
  1167. $v['transfer_result'] = [];
  1168. if (isset($wechat_transfer_log_list[$v['id']])) {
  1169. $payment_conf = \Yii::$app->services->setting->mall->get($this->mall_id, 'pay.wechat', true);
  1170. $payment_conf = json_decode($payment_conf, true);
  1171. $mchId = $payment_conf['wechat_mch_id'] ?? '';
  1172. $wechat_transfer = $wechat_transfer_log_list[$v['id']];
  1173. $transfer_result = json_decode($wechat_transfer['transfer_result'], true);
  1174. $transfer_result['mchId'] = $mchId;
  1175. $transfer_result['appId'] = $appId;
  1176. $v['transfer_result'] = $transfer_result;
  1177. }
  1178. }
  1179. }
  1180. return $this->success('操作成功',$list);
  1181. }
  1182. /**
  1183. * 获取用户协议接口
  1184. * @Author: ltm
  1185. * @DateTime: 2021/12/8 0030 14:20
  1186. * @Copyright: copyright (c) 2021 广东七件事集团
  1187. * @return mixed|void
  1188. */
  1189. public function actionUserAgree()
  1190. {
  1191. $retuest = Yii::$app->request;
  1192. if ($retuest->isPost) {
  1193. try {
  1194. $user_setting = Yii::$app->services->setting->mall->get($this->mall_id, 'user_agree');
  1195. $this->success('获取成功', $user_setting);
  1196. } catch (\Exception $e) {
  1197. $this->error('获取失败,mag=' . $e->getMessage());
  1198. }
  1199. }
  1200. }
  1201. /**
  1202. * @Author: ltm
  1203. * @DateTime: 2022/1/7 0007 17:00
  1204. * @Copyright: copyright (c) 2021 广东七件事集团
  1205. * @param $data
  1206. * @return bool
  1207. */
  1208. public function actionFollow(){
  1209. if (\Yii::$app->request->isPost) {
  1210. $params = Yii::$app->request->post();
  1211. $res = Yii::$app->services->validate->validate($params, [
  1212. [['follow_id'], 'required']
  1213. ]);
  1214. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  1215. $params['mall_id'] = $this->mall_id;
  1216. $params['user_id'] = $this->user_id;
  1217. if($params['follow_id'] == $this->user_id) return $this->error('自己不可以关注自己');
  1218. $res = UserFollow::setData($params);
  1219. //记录我关注
  1220. $model_follow = UserExtra::findOne(['user_id'=>$this->user_id]);
  1221. if($model_follow){
  1222. if($res == StatusEnum::ENABLED){
  1223. $model_follow -> follow_count = $model_follow -> follow_count + 1;
  1224. }else{
  1225. $model_follow -> follow_count = $model_follow -> follow_count - 1;
  1226. }
  1227. $model_follow ->save();
  1228. }
  1229. //记录粉丝
  1230. $model_fans = UserExtra::findOne(['user_id'=>$params['follow_id']]);
  1231. if($model_fans){
  1232. if($res == StatusEnum::ENABLED){
  1233. $model_fans -> fans_count = $model_fans -> fans_count + 1;
  1234. }else{
  1235. $model_fans -> fans_count = $model_fans -> fans_count - 1;
  1236. }
  1237. $model_fans ->save();
  1238. }
  1239. if ($res === false) throw new Exception(UserFollow::getStaticCodeError()['error']);
  1240. return $this->success('操作成功');
  1241. }
  1242. }
  1243. /**
  1244. * @Author: ltm
  1245. * 我关注的记录
  1246. * @DateTime: 2022/1/10 0010 18:49
  1247. * @Copyright: copyright (c) 2021 广东七件事集团
  1248. * @param $data
  1249. * @return String
  1250. */
  1251. public function actionFollowList(){
  1252. if (\Yii::$app->request->isPost) {
  1253. $params = Yii::$app->request->post();
  1254. $res = Yii::$app->services->validate->validate($params, [
  1255. [['user_id'], 'safe'],
  1256. ]);
  1257. $params = UserFollow::exceptNullVal($params);
  1258. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  1259. $user_id = $params['user_id'] ? $params['user_id'] : $this->user_id;
  1260. $params['where'][] = ['status' => StatusEnum::ENABLED];
  1261. $params['where'][] = ['user_id' => $user_id];
  1262. $params['with'] = ['follow','userextra'];
  1263. $params['order'] = 'status desc';
  1264. $list = UserFollow::listPage($params);
  1265. if($list['list']) {
  1266. foreach ($list['list'] as $k => $v){
  1267. $list['list'][$k]['user'] = $v['follow'];
  1268. $list['list'][$k]['user']['signature_content'] = $v['userextra']['personal_signature'];
  1269. unset($list['list'][$k]['follow']);
  1270. }
  1271. }
  1272. return $this->success('操作成功',$list);
  1273. }
  1274. }
  1275. /**
  1276. * @Author: ltm
  1277. * 我关注的记录
  1278. * @DateTime: 2022/1/10 0010 18:49
  1279. * @Copyright: copyright (c) 2021 广东七件事集团
  1280. * @param $data
  1281. * @return String
  1282. */
  1283. public function actionFansList(){
  1284. if (\Yii::$app->request->isPost) {
  1285. $params = Yii::$app->request->post();
  1286. $res = Yii::$app->services->validate->validate($params, [
  1287. [['user_id'], 'safe'],
  1288. ]);
  1289. $params = UserFollow::exceptNullVal($params);
  1290. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  1291. $user_id = $params['user_id'] ? $params['user_id'] : $this->user_id;
  1292. $params['where'][] = ['follow_id' => $user_id];
  1293. $params['where'][] = ['status' => StatusEnum::ENABLED];
  1294. $params['with'] = ['fans','userextrafans','userFollow'];
  1295. $list = UserFollow::listPage($params);
  1296. if($list['list']) {
  1297. foreach ($list['list'] as $k => $v){
  1298. $list['list'][$k]['user'] = $v['fans'];
  1299. $list['list'][$k]['status'] = $v['userFollow']['status'];
  1300. $list['list'][$k]['user']['signature_content'] = $v['userextrafans']['personal_signature'];
  1301. unset($list['list'][$k]['fans']);
  1302. }
  1303. }
  1304. return $this->success('操作成功',$list);
  1305. }
  1306. }
  1307. /**
  1308. * 刷新最后活跃时间
  1309. * @return string
  1310. */
  1311. public function actionRefreshLastLoginAt()
  1312. {
  1313. try {
  1314. if (!MallAddons::isInstall($this->mall_id, AddonsEnum::ADDONS_NAME_ALIPAY_MINI_INVITE)) {
  1315. throw new \Exception("未安装当前插件");
  1316. }
  1317. // 这里执行刷新
  1318. // 判断今天有没有刷新过,有刷新过就不再进行刷新
  1319. $date = date("Y-m-d");
  1320. $key = "refresh:{$date}";
  1321. if (Yii::$app->redis->getbit($key, $this->user_id)) {
  1322. throw new \Exception('今天已经刷新过');
  1323. }
  1324. // 使用bit会比较合适
  1325. Yii::$app->redis->setbit($key, $this->user_id, StatusEnum::ENABLED);
  1326. if (!Yii::$app->redis->ttl($key)) {
  1327. Yii::$app->redis->expireat($key, date('Y-m-d') . " 23:59:59"); // 一天后过期
  1328. }
  1329. // 获取用户最后活跃时间
  1330. $last_login_at = $this->user['last_login_at'];
  1331. // 更新用户最后登录时间
  1332. User::updateAll(['last_login_at' => time()], ['id' => $this->user_id]);
  1333. $invite_user_id = intval(Yii::$app->request->get('invite', 0));
  1334. if ($invite_user_id > 0) { // 如果有带邀请激活人的ID那么就执行事件
  1335. // 执行事件
  1336. if (User::find()->where(['mall_id' => $this->mall_id])->andWhere(['id' => $invite_user_id])->count() > 0) {
  1337. $event = new InviteOldUserRewardEvent($this->mall_id);
  1338. Yii::$app->services->events->dispatch($event, [
  1339. 'last_login_at' => $last_login_at,
  1340. 'user_id' => $this->user_id,
  1341. 'invite_user_id'=> $invite_user_id,
  1342. 'mall_id' => $this->mall_id,
  1343. ], $event->listeners);
  1344. }
  1345. }
  1346. return $this->success('刷新成功');
  1347. } catch (\Exception $e) {
  1348. return $this->error($e->getMessage());
  1349. }
  1350. }
  1351. /**
  1352. * 地址解析
  1353. * @return mixed|void
  1354. */
  1355. public function actionAddressParse()
  1356. {
  1357. $params = Yii::$app->request->post();
  1358. $res = Yii::$app->services->validate->validate($params, [
  1359. [['address'], 'required'],
  1360. ]);
  1361. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  1362. $address = $params['address'];
  1363. $res = \common\helpers\AddressParse\AddressParseHelper::smart($address);
  1364. return $this->success('操作成功',$res);
  1365. }
  1366. public function actionVisit(){
  1367. if ($this->user_id) {
  1368. UserStatisticsTotal::setUserStatisticsTotal($this->mall_id,$this->user_id,['visit_nums'=>1]);
  1369. Yii::$app->services->rabbitMq->push(RabbitMqEnum::EXCHANGE_TASK, RabbitMqEnum::TASK_QUEUE, [
  1370. 'user_id'=>$this->user_id,
  1371. 'mall_id'=>$this->mall_id,
  1372. 'cate_ids'=>[MemberLabelEnum::ACTIVE]
  1373. ], MemberLabelLogic::class, 'executeSync');
  1374. }
  1375. return $this->success('操作成功',[]);
  1376. }
  1377. public function actionBindByCode(){
  1378. try{
  1379. $params = Yii::$app->request->post();
  1380. $res = Yii::$app->services->validate->validate($params, [
  1381. [['code'], 'required'],
  1382. [['code'], 'string'],
  1383. ]);
  1384. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  1385. $result = UserPartitionRelationship::bindUserByCode($this->mall_id, $this->user_id, $params['code']);
  1386. if($result === false) throw new \Exception(UserPartitionRelationship::getStaticError());
  1387. return $this->success('绑定成功');
  1388. }catch(\Exception $e){
  1389. return $this->error($e->getMessage());
  1390. }
  1391. }
  1392. /**
  1393. * 获取权重
  1394. * @return mixed|null
  1395. */
  1396. public function actionLevelList()
  1397. {
  1398. $list = UserLevel::getUserLevelArr($this->mall_id);
  1399. if (!empty(UserLevel::getStaticError())) return $this->error(UserLevel::getStaticError());
  1400. return $this->success('获取成功', $list);
  1401. }
  1402. public function actionUpdatePersonalSign()
  1403. {
  1404. try {
  1405. $params = Yii::$app->request->post();
  1406. $res = Yii::$app->services->validate->validate($params, [
  1407. [['personal_signature'], 'required']
  1408. ]);
  1409. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  1410. $params['mall_id'] = $this->mall_id;
  1411. $params['user_id'] = $this->user_id;
  1412. $res = UserExtra::setData($params);
  1413. if ($res === false) return $this->error(UserExtra::getStaticError());
  1414. return $this->success('操作成功');
  1415. } catch (\Exception $e) {
  1416. return $this->error($e->getMessage());
  1417. }
  1418. }
  1419. public function actionGetUpgradeAgree()
  1420. {
  1421. $retuest = Yii::$app->request;
  1422. if ($retuest->isGet) {
  1423. try {
  1424. $this->success('获取成功', ['upgrade_agree' => UserSignAgreement::getUpgradeAgree($this->mall_id)]);
  1425. } catch (\Exception $e) {
  1426. $this->error('获取失败,mag=' . $e->getMessage());
  1427. }
  1428. }
  1429. }
  1430. /**
  1431. * 上传支付凭证
  1432. * @return mixed|void
  1433. */
  1434. public function actionSaveEvidence(){
  1435. $key = sprintf('SaveEvidence:%s', $this->user_id);
  1436. if (LockHelper::fLock($key)) return $this->error('请勿频繁请求');
  1437. try {
  1438. $params = Yii::$app->request->post();
  1439. $res = Yii::$app->services->validate->validate($params, [
  1440. [['payment_evidence','num'], 'required']
  1441. ]);
  1442. if ($res === false) throw new \Exception(Yii::$app->services->validate->getErrorMessage());
  1443. $params['mall_id'] = $this->mall_id;
  1444. $params['user_id'] = $this->user_id;
  1445. $res = BalanceRechargeLog::setData($params);
  1446. if ($res === false) throw new \Exception(BalanceRechargeLog::getStaticError());
  1447. LockHelper::unFLock($key);
  1448. return $this->success('操作成功');
  1449. } catch (\Exception $e) {
  1450. LockHelper::unFLock($key);
  1451. return $this->error($e->getMessage());
  1452. }
  1453. }
  1454. /**
  1455. * 支付凭证列表
  1456. * @return mixed|void
  1457. */
  1458. public function actionEvidenceList(){
  1459. $get = Yii::$app->request->get();
  1460. $where[]=['mall_id'=>$this->mall_id];
  1461. $where[]=['user_id'=>$this->user_id];
  1462. $params['where'] = $where;
  1463. $params['select'] = 'created_at,num,withdraw_status,remark,payment_evidence';
  1464. $params['order'] = 'id desc';
  1465. $list = BalanceRechargeLog::listPage($params);
  1466. if(!empty($list['list'])){
  1467. foreach($list['list'] as &$item){
  1468. $item['created_at'] = date('Y-m-d H:i:s',$item['created_at']);
  1469. }
  1470. }
  1471. return $this->success('操作成功',$list);
  1472. }
  1473. /**
  1474. * 用户是否订阅公众号
  1475. *
  1476. * @return string
  1477. */
  1478. public function actionIsSubscribe()
  1479. {
  1480. // 未登录情况下返回未关注
  1481. if (empty($this->user_id)) return $this->success('ok', ['subscribe' => false]);
  1482. $service = new WechatUserService(['mall_id' => $this->mall_id]);
  1483. $subscribe = $service->isSubscribe((int)$this->user_id);
  1484. return $this->success('ok', ['subscribe' => $subscribe]);
  1485. }
  1486. /**
  1487. * 关于我们版权信息
  1488. * @return mixed|null
  1489. */
  1490. public function actionCopyright()
  1491. {
  1492. // 获取版权信息
  1493. $settings = MallSetting::getConfByGroup($this->mall_id, ['copyright'], true);
  1494. $certificates = $usageAgreement = [];
  1495. if (!empty($settings['copyright']['certificates'])) {
  1496. $certificates_images = !empty($settings['copyright']['certificates']['value']) ? json_decode($settings['copyright']['certificates']['value'], true) : [];
  1497. $certificates = [
  1498. 'name' => $settings['copyright']['certificates']['name'],
  1499. 'value' => $certificates_images,
  1500. ];
  1501. }
  1502. if (!empty($settings['copyright']['usage_agreement'])) {
  1503. $usageAgreement = [
  1504. 'name' => $settings['copyright']['usage_agreement']['name'],
  1505. 'value' => $settings['copyright']['usage_agreement']['value'] ?? '',
  1506. ];
  1507. }
  1508. return $this->success('请求成功', ['certificates' => $certificates, 'usageAgreement' => $usageAgreement]);
  1509. }
  1510. public function actionUpdateUserPosition()
  1511. {
  1512. try {
  1513. if (\Yii::$app->request->isPost) {
  1514. $params = Yii::$app->request->post();
  1515. $res = Yii::$app->services->validate->validate($params, [
  1516. [['longitude', 'latitude'], 'required'],
  1517. ]);
  1518. if ($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  1519. if (empty($this->user_id)) {
  1520. //未登录的不处理
  1521. return $this->success('操作成功');
  1522. }
  1523. $key = sprintf('Position:%s', $this->mall_id . '_' . $this->user_id);
  1524. //每个用户限制5分钟更新一次
  1525. if (LockHelper::fLock($key, 300)) $this->success('操作成功');
  1526. $params['mall_id'] = $this->mall_id;
  1527. $params['user_id'] = $this->user_id;
  1528. $event = new UserPositionChangeEvent($this->mall_id);
  1529. Yii::$app->services->events->dispatch($event, $params, $event->listeners);
  1530. return $this->success('操作成功', null);
  1531. }
  1532. } catch (\Exception $e) {
  1533. LockHelper::unFLock($key);
  1534. return $this->error($e->getMessage());
  1535. }
  1536. }
  1537. }