UserController.php 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\admin\controllers;
  8. use app\models\Level;
  9. use app\models\LevelOrder;
  10. use app\models\Mch;
  11. use app\models\MchBrands;
  12. use app\models\MchStaff;
  13. use app\models\Salesman;
  14. use app\models\Share;
  15. use app\models\ShareHolder;
  16. use app\models\StoreAudit;
  17. use app\models\User;
  18. use app\models\SaasUser;
  19. use app\models\UserGrowthValueLog;
  20. use app\models\UserShareMoney;
  21. use app\models\Worker;
  22. use app\modules\admin\models\AuthRoleForm;
  23. use app\modules\admin\models\cashier\CashierUserForm;
  24. use app\modules\admin\models\LevelForm;
  25. use app\modules\admin\models\SaasStaffForm;
  26. use app\modules\admin\models\UserForm;
  27. use app\models\Admin;
  28. use app\models\StoreCloud;
  29. use app\models\Store;
  30. use Yii;
  31. use app\models\Option;
  32. use app\constants\OptionSetting;
  33. use app\models\DeliveryInfo;
  34. use app\models\LocalDeliveryCourier;
  35. use app\models\Order;
  36. use app\models\AccountLog;
  37. use app\models\UserPayMonth;
  38. use yii\base\BaseObject;
  39. use yii\helpers\Json;
  40. class UserController extends BaseController
  41. {
  42. public function actionSaasInfo()
  43. {
  44. $md_id = get_md_id();
  45. $admin = get_admin();
  46. $mch_id = get_mch_id();
  47. $adminPermission = AuthRoleForm::getCommonPermission();
  48. $store_name = '';
  49. $store = Store::findOne($admin->store_id);
  50. if ($store) {
  51. $store_name = $store->name;
  52. }
  53. $typename = '商城后台';
  54. if ($md_id > 0) {
  55. $typename = '门店后台';
  56. }
  57. if ($mch_id > 0) {
  58. $typename = '入驻商后台';
  59. }
  60. $result = [
  61. 'username' => $admin->username,
  62. 'name' => $admin->name,
  63. 'mobile' => $admin->mobile,
  64. 'email' => $admin->email,
  65. 'remark' => $admin->remark,
  66. 'avatar' => $admin->avatar ?: '/avatar2.jpg',
  67. 'store_id' => $admin->store_id,
  68. 'mch_id' => $mch_id,
  69. 'store_name' => $store_name,
  70. 'md_id' => $md_id,
  71. 'role' => [
  72. 'permissions' => $adminPermission,
  73. ],
  74. 'is_saas' => Yii::$app->isSaas(),
  75. 'is_platform' => false,
  76. 'is_supplier' => false,
  77. 'is_dandianpu' => \Yii::$app->prod_is_dandianpu(),
  78. 'is_expired' => \Yii::$app->isExpired() && \Yii::$app->getAuthSwitch('is_can'),
  79. 'typename' => $typename
  80. ];
  81. return $this->asJson([
  82. 'code' => 0,
  83. 'msg' => 'success',
  84. 'data' => $result,
  85. ]);
  86. }
  87. public function actionInfo()
  88. {
  89. try {
  90. $admin = get_admin();
  91. $mch_id = get_mch_id();
  92. $supplier_id = get_supplier_id();
  93. $md_id = get_md_id();
  94. $mch_brands_id = get_mch_brands_id();
  95. $is_platform = false; //
  96. $is_supplier = false;
  97. $is_area_platform = false;
  98. $typename = '平台后台';
  99. if (\Yii::$app->isSaas()) {
  100. if ($admin->type == Admin::ADMIN_TYPE_MD) {
  101. $md_id = $admin->type_id;
  102. }
  103. if ($md_id > 0) {
  104. $params = require Yii::$app->basePath . '/config/saas_md_permission.php';
  105. $md = \app\models\Md::findOne($md_id);
  106. if ($md && $md->is_single == 0) { // 非独立运营门店去除同城配送
  107. \Yii::$app->removeMenu($params, ['WechatNewDelivery']);
  108. }
  109. } else if ($admin->type == Admin::ADMIN_TYPE_STORE || $admin->type == Admin::ADMIN_TYPE_STAFF) {
  110. $typename = '商城后台';
  111. $params = require Yii::$app->basePath . '/config/saas_store_permission.php';
  112. if (is_open_platform()) {
  113. //设置小程序配置
  114. $WechatConfig = \app\models\WechatConfig::findOne(['store_id' => get_store_id()]);
  115. $store_mini = \app\models\StoreMini::find()->where(['appid' => $WechatConfig->app_id, 'store_id' => get_store_id()])->select('id, appid, authorizer_refresh_token')->one();
  116. if(empty($store_mini->appid) || empty($store_mini->authorizer_refresh_token)){
  117. // return [
  118. // 'code'=>1,
  119. // 'msg'=>"参数配置错误"
  120. // ];
  121. \Yii::$app->removeMenu($params, ['dataStatistic']);
  122. }
  123. }
  124. if($admin->username != 'admin'){
  125. if ($admin->type == Admin::ADMIN_TYPE_STAFF) {
  126. $result = null;
  127. } else {
  128. $store_id = get_store_id();
  129. $storeInfo = Store::find()->where(['id'=> $store_id,"is_delete"=>0])->asArray()->one();
  130. if (!empty($storeInfo['auth']) && $storeInfo['auth'] != '[]') {
  131. $result = json_decode($storeInfo['auth'], true);
  132. $mho = Option::get(OptionSetting::MCH_HIDE_OSS, 0, 'saas', '0')['value'];
  133. if ($mho && \in_array('config_baseConfig_uploadConfig', $result)) {
  134. // 删除数组中config_baseConfig_uploadConfig
  135. $result = array_diff($result, ['config_baseConfig_uploadConfig']);
  136. }
  137. }
  138. }
  139. $adminPermission = AuthRoleForm::getAdminPermission($result);
  140. //20240816原来是在当前控制器 给挪到AuthRoleForm模型了 这里忘了改了
  141. $adminPermission = AuthRoleForm::handlePermission($adminPermission);
  142. }
  143. } else if ($admin->type == Admin::ADMIN_TYPE_SUPPLIER ) {
  144. //平台权限
  145. $is_platform = true;
  146. //云仓供货商
  147. $is_supplier = true;
  148. $typename = '供货商后台';
  149. $params = require Yii::$app->basePath . '/config/saas_supplier_permission.php';
  150. } else if ($admin->type == Admin::ADMIN_TYPE_FRONT_AGENT ) {
  151. //平台权限
  152. $is_platform = true;
  153. $typename = '仓库后台';
  154. $params = require Yii::$app->basePath . '/config/saas_front_agent_permission.php';
  155. } else if ($admin->type == Admin::ADMIN_TYPE_BD_AGENT ) {
  156. //平台权限
  157. $is_platform = true;
  158. $typename = '推广代理后台';
  159. $params = require Yii::$app->basePath . '/config/saas_bd_agent_permission.php';
  160. } else if ($admin->type == Admin::ADMIN_TYPE_GOODS_AGENT ) {
  161. //平台权限
  162. $is_platform = true;
  163. $typename = '线下产品代理后台';
  164. $params = require Yii::$app->basePath . '/config/saas_goods_agent_permission.php';
  165. }else if ($admin->type == Admin::ADMIN_TYPE_MCH_BRANDS ) {
  166. //平台权限
  167. $is_platform = true;
  168. $typename = '品牌管理员后台';
  169. $params = require Yii::$app->basePath . '/config/saas_mch_brands_permission.php';
  170. $mch_brands_id = $admin->type_id;
  171. } elseif ($mch_id && $mch_id > 0) {
  172. $params = require Yii::$app->basePath . '/config/saas_mch_permission.php';
  173. $typename = '入驻商后台';
  174. $params = \Yii::$app->filterMenu($params);
  175. AuthRoleForm::formatPermission($params);
  176. $result = [];
  177. AuthRoleForm::getAllPermission($params, $result);
  178. } else {
  179. $is_platform = true;
  180. if ($admin->username != 'admin' && $admin->type != Admin::ADMIN_TYPE_SAAS_STAFF) {
  181. $typename = '区域代理后台';
  182. $is_area_platform = true;
  183. $params = require Yii::$app->basePath . '/config/saas_permission.php';
  184. } else {
  185. $params = require Yii::$app->basePath . '/config/saas_admin_permission.php';
  186. if (!\Yii::$app->prod_is_dandianpu()) {
  187. //非单店铺时去除商城提现
  188. // \Yii::$app->removeMenu($params, ['SaasSmsSetting']);
  189. } else {
  190. //TODO 12/29老板说让单店铺给放开联盟券设置
  191. \Yii::$app->removeMenu($params, []);
  192. }
  193. if($admin->username != 'admin'){
  194. $adminPermission = SaasStaffForm::getAdminPermission();
  195. }
  196. }
  197. }
  198. } else {
  199. if ($admin->type == Admin::ADMIN_TYPE_MD) {
  200. $md_id = $admin->type_id;
  201. }else if ($admin->type == Admin::ADMIN_TYPE_MCH_BRANDS ) {
  202. //平台权限
  203. $is_platform = true;
  204. $typename = '品牌管理员后台';
  205. $params = require Yii::$app->basePath . '/config/saas_mch_brands_permission.php';
  206. $mch_brands_id = $admin->type_id;
  207. }
  208. $typename = '商城后台';
  209. if ($mch_id > 0) {
  210. $params = require Yii::$app->basePath . '/config/saas_mch_permission.php';
  211. $typename = '入驻商后台';
  212. } else {
  213. $params = require Yii::$app->basePath . '/config/permission.php';
  214. }
  215. if ($md_id > 0) {
  216. $typename = '门店后台';
  217. $params = require Yii::$app->basePath . '/config/saas_md_permission.php';
  218. }
  219. }
  220. \Yii::$app->cache->set('is_platform', $is_platform);
  221. $params = AuthRoleForm::delEmptyAuth($params);
  222. if (!$adminPermission) {
  223. if (
  224. $admin->username == 'admin' ||
  225. \Yii::$app->isSaas()
  226. ) {
  227. if (\Yii::$app->isSaas() && $admin->type != Admin::ADMIN_TYPE_STAFF) {
  228. //非独立部署时 去除小程序发布
  229. \Yii::$app->removeMenu($params, ['uploadWechat','uploadAlipay','storeUpgrade']);
  230. // 超级管理员 or 入驻商 or 供货商 拥有所有权限
  231. $params = \Yii::$app->filterMenu($params);
  232. AuthRoleForm::formatPermission($params);
  233. $result = [];
  234. AuthRoleForm::getAllPermission($params, $result);
  235. $adminPermission = AuthRoleForm::getAdminPermission($result);
  236. } else {
  237. if ($admin->username == 'admin') {
  238. $params = \Yii::$app->filterMenu($params);
  239. AuthRoleForm::formatPermission($params);
  240. $result = [];
  241. AuthRoleForm::getAllPermission($params, $result);
  242. $adminPermission = AuthRoleForm::getAdminPermission($result);
  243. } else {
  244. $adminPermission = AuthRoleForm::getAdminPermission();
  245. }
  246. }
  247. } else {
  248. if ($admin->type == Admin::ADMIN_TYPE_STAFF) {
  249. $adminPermission = AuthRoleForm::getAdminPermission();
  250. } else {
  251. $params = \Yii::$app->filterMenu($params);
  252. AuthRoleForm::formatPermission($params);
  253. $result = [];
  254. AuthRoleForm::getAllPermission($params, $result);
  255. $adminPermission = AuthRoleForm::getAdminPermission($result);
  256. if ($md_id > 0) {
  257. $params = \Yii::$app->filterMenu($params);
  258. AuthRoleForm::formatPermission($params);
  259. $result = [];
  260. AuthRoleForm::getAllPermission($params, $result);
  261. $adminPermission = AuthRoleForm::getAdminPermission($result);
  262. }
  263. }
  264. }
  265. }
  266. $store_name = '';
  267. $store = Store::findOne($admin->store_id);
  268. if ($store) {
  269. $store_name = $store->name;
  270. }
  271. $result = [
  272. 'type' => $admin->type,
  273. 'typename' => $typename,
  274. 'mch_id' => $mch_id,
  275. 'mch_brands_id' => $mch_brands_id,
  276. 'username' => $admin->username,
  277. 'name' => $admin->name,
  278. 'mobile' => $admin->mobile,
  279. 'email' => $admin->email,
  280. 'remark' => $admin->remark,
  281. 'avatar' => $admin->avatar ?: '/avatar2.jpg',
  282. 'store_id' => $admin->store_id,
  283. 'store_name' => $store_name,
  284. 'md_id' => $md_id,
  285. 'role' => [
  286. 'permissions' => $adminPermission,
  287. ],
  288. 'is_saas' => Yii::$app->isSaas(),
  289. 'is_platform' => $is_platform,
  290. 'is_supplier' => $is_supplier,
  291. 'is_dandianpu' => \Yii::$app->prod_is_dandianpu(),
  292. 'is_expired' => \Yii::$app->isExpired() && \Yii::$app->getAuthSwitch('is_can'),
  293. 'is_area_platform' => $is_area_platform,
  294. ];
  295. return $this->asJson([
  296. 'code' => 0,
  297. 'msg' => 'success',
  298. 'data' => $result,
  299. ]);
  300. } catch (\Throwable $throwable) {
  301. return $this->asJson([
  302. 'code' => 1,
  303. 'msg' => 'error',
  304. 'data' => $throwable->getMessage(),
  305. 'line' => $throwable->getLine(),
  306. 'file' => $throwable->getFile()
  307. ]);
  308. }
  309. }
  310. /**
  311. * 用户列表
  312. * @return \yii\web\Response
  313. */
  314. public function actionList() {
  315. $userForm = new UserForm();
  316. $userForm->attributes = get_params();
  317. $store_id = get_store_id();
  318. $userForm->store_id = $store_id;
  319. return $this->asJson($userForm->getUserList());
  320. }
  321. /**
  322. * 更新用户信息
  323. * @return \yii\web\Response
  324. * @throws \yii\base\Exception
  325. */
  326. public function actionUpdateInfo() {
  327. $userForm = new UserForm();
  328. $userForm->store_id = get_store_id();
  329. $userForm->attributes = post_params();
  330. return $this->asJson($userForm->updateUserInfo());
  331. }
  332. /**
  333. * 会员等级
  334. */
  335. public function actionLevelList()
  336. {
  337. $form = new LevelForm();
  338. $form->store_id = get_store_id();
  339. $form->attributes = get_params();
  340. return $this->asJson($form->search());
  341. }
  342. /**
  343. * 获取所有会员等级
  344. */
  345. public function actionAllLevelList()
  346. {
  347. $form = new LevelForm();
  348. $form->store_id = get_store_id();
  349. $form->attributes = get_params();
  350. return $this->asJson($form->getAllLevelList());
  351. }
  352. /**
  353. * 获取所有会员等级
  354. */
  355. public function actionSelectLevel()
  356. {
  357. $form = new \app\modules\admin\models\AccountLogForm();
  358. $form->store_id = get_store_id();
  359. $list = $form->getLevelList();
  360. return $this->asJson([
  361. 'code' => 0,
  362. 'data' => $list,
  363. ]);
  364. }
  365. /**
  366. * 会员等级启用/禁用
  367. */
  368. public function actionLevelStatus()
  369. {
  370. $data = post_params();
  371. $store_id = get_store_id();
  372. $level = Level::find()->where(['id' => $data['id'], 'store_id' => $store_id])->one();
  373. if (!$level) {
  374. return $this->asJson([
  375. 'code' => 1,
  376. 'msg' => '会员等级不存在',
  377. ]);
  378. }
  379. $level->status = $data['status'];
  380. if ($data['status'] == Level::STATUS_FALSE) {
  381. $exit = User::find()->where(['store_id' => $store_id, 'level' => $level->level])->exists();
  382. if ($exit) {
  383. return $this->asJson([
  384. 'code' => 1,
  385. 'msg' => '该会员等级下有会员,不可禁用',
  386. ]);
  387. }
  388. }
  389. if ($level->save()) {
  390. return $this->asJson([
  391. 'code' => 0,
  392. 'msg' => '成功',
  393. ]);
  394. } else {
  395. return $this->asJson([
  396. 'code' => 1,
  397. 'msg' => '网络异常',
  398. ]);
  399. }
  400. }
  401. /**
  402. * 会员等级启用/禁用
  403. */
  404. public function actionLevelStudent()
  405. {
  406. $data = post_params();
  407. $store_id = get_store_id();
  408. $level = Level::find()->where(['id' => $data['id'], 'store_id' => $store_id])->one();
  409. if (!$level) {
  410. return $this->asJson([
  411. 'code' => 1,
  412. 'msg' => '会员等级不存在',
  413. ]);
  414. }
  415. if ($data['is_student'] == Level::STUDENT_TRUE) {
  416. Level::updateAll(['is_student' => 0]);
  417. }
  418. $level->is_student = $data['is_student'];
  419. if ($level->save()) {
  420. return $this->asJson([
  421. 'code' => 0,
  422. 'msg' => '成功',
  423. ]);
  424. } else {
  425. return $this->asJson([
  426. 'code' => 1,
  427. 'msg' => '网络异常',
  428. ]);
  429. }
  430. }
  431. public function actionGetLevelItem(){
  432. $data = get_params();
  433. $levelForm = new LevelForm();
  434. $levelForm->id = $data['id'];
  435. return $this->asJson($levelForm->getLevelItem());
  436. }
  437. /**
  438. * 更新会员等级信息
  439. * @return \yii\web\Response
  440. */
  441. public function actionUpdateLevelInfo() {
  442. $data = post_params();
  443. $store_id = get_store_id();
  444. if (!empty($data['id'])) {
  445. $level = Level::findOne(['id' => $data['id'], 'is_delete' => Level::NOT_DELETE, 'store_id' => $store_id]);
  446. } else {
  447. $level = new Level();
  448. }
  449. $levelForm = new LevelForm();
  450. $levelForm->scenario = 'edit';
  451. $levelForm->model = $level;
  452. $levelForm->store_id = $store_id;
  453. $levelForm->attributes = $data;
  454. return $this->asJson($levelForm->save());
  455. }
  456. /**
  457. * 会员等级删除
  458. */
  459. public function actionDeleteLevel()
  460. {
  461. $data = post_params();
  462. $store_id = get_store_id();
  463. $level = Level::findOne(['id' => $data['id'], 'store_id' => $store_id, 'is_delete' => Level::NOT_DELETE]);
  464. if (!$level) {
  465. return $this->asJson([
  466. 'code' => 1,
  467. 'msg' => '会员等级不存在',
  468. ]);
  469. }
  470. $exit = User::find()->where(['store_id' => $store_id, 'level' => $level->level])->exists();
  471. if ($exit) {
  472. return $this->asJson([
  473. 'code' => 1,
  474. 'msg' => '该会员等级下有会员,不可删除',
  475. ]);
  476. }
  477. $level->is_delete = 1;
  478. if ($level->save()) {
  479. return $this->asJson([
  480. 'code' => 0,
  481. 'msg' => '成功',
  482. ]);
  483. } else {
  484. return $this->asJson([
  485. 'code' => 1,
  486. 'msg' => '网络异常',
  487. ]);
  488. }
  489. }
  490. /**
  491. * 会员购买记录
  492. */
  493. public function actionLevelOrder()
  494. {
  495. $params = get_params();
  496. $store_id = get_store_id();
  497. if ($params['user_id']) {
  498. $query = LevelOrder::find()->alias('ro')->where(['ro.store_id' => $store_id,
  499. 'ro.is_delete' => LevelOrder::NOT_DELETE, 'ro.is_pay' => LevelOrder::IS_PAY])
  500. ->leftJoin(['u' => User::tableName()], 'u.id='.$params['user_id'])
  501. ->leftJoin(['su' => SaasUser::tableName()], 'su.mobile=u.binding');
  502. } else {
  503. $query = LevelOrder::find()->alias('ro')->where(['ro.store_id' => $store_id,
  504. 'ro.is_delete' => LevelOrder::NOT_DELETE, 'ro.is_pay' => LevelOrder::IS_PAY])
  505. ->leftJoin(['u' => User::tableName()], 'u.id=ro.user_id')
  506. ->leftJoin(['su' => SaasUser::tableName()], 'su.mobile=u.binding');
  507. }
  508. if (!empty($params['keyword'])) {
  509. $query->andWhere(['like', 'u.nickname', $params['keyword']]);
  510. }
  511. if (!empty($params['dateStart'])) {
  512. $query->andWhere(['>', 'ro.created_at', strtotime($params['dateStart'])]);
  513. }
  514. if (!empty($params['dateEnd'])) {
  515. $query->andWhere(['<', 'ro.created_at', strtotime($params['dateEnd'])]);
  516. }
  517. $currentQuery = Level::find()->where(['store_id' => $store_id, 'is_delete' => Level::NOT_DELETE])
  518. ->andWhere('level=ro.current_level')->select('name');
  519. $afterQuery = Level::find()->where(['store_id' => $store_id,'is_delete' => Level::NOT_DELETE])
  520. ->andWhere('level=ro.after_level')->select('name');
  521. $query->select(['ro.*', 'su.name as nickname', 'u.platform', 'after_name' => $afterQuery, 'current_name' => $currentQuery])->orderBy('ro.id desc');
  522. $pagination = pagination_make($query);
  523. $list = $pagination['list'];
  524. foreach ($list as $k => $v) {
  525. $list[$k]['pay_time'] = date('Y-m-d H:i:s', $v['pay_time']);
  526. if (!$v['current_name']) {
  527. $list[$k]['current_name'] = '普通会员';
  528. }
  529. $list[$k]['share_money_log'] = UserShareMoney::find()->alias('usm')
  530. ->leftJoin(['u' => User::tableName()], 'usm.user_id = u.id')
  531. ->leftJoin(['su' => SaasUser::tableName()], 'su.mobile=u.binding')
  532. ->where(['usm.order_id' => $v['id'], 'usm.order_type' => UserShareMoney::LEVEL_ORDER_TYPE, 'usm.is_delete' => 0])
  533. ->select('u.id, su.name, su.avatar, usm.money, usm.desc, usm.created_at')->orderBy('usm.created_at')->asArray()->all();
  534. }
  535. $this->asJson([
  536. 'code' => 0,
  537. 'msg' => 'success',
  538. 'data' => [
  539. 'data' => $list,
  540. 'pageNo' => $pagination['pageNo'],
  541. 'totalCount' => $pagination['totalCount']
  542. ],
  543. ]);
  544. }
  545. /**
  546. * 获取用户列表(saas_user)
  547. */
  548. public function actionSelectList()
  549. {
  550. $type = get_params("type");
  551. $scene = get_params("scene");
  552. $admin = get_admin();
  553. $keyword = get_params('keyword');
  554. if ((($admin->type === 'admin' && $admin->username !== 'admin') || $admin->type === 'bd_agent') && empty($keyword)) {
  555. return $this->asJson([
  556. 'code' => 0,
  557. 'msg' => 'success',
  558. 'data' => [
  559. 'data' => [],
  560. 'status' => 1,
  561. 'pageNo' => 0,
  562. 'totalCount' => 0
  563. ],
  564. ]);
  565. }
  566. $mobiles = [];
  567. //排除推荐人
  568. if ($type === "user" && $scene === "recommend") {
  569. $mobiles = Salesman::find()->alias('s')->where(['s.is_delete'=>0])
  570. ->leftJoin(['su'=>SaasUser::tableName()],'s.saas_user_id = su.id')
  571. ->select("su.mobile")->column();
  572. }
  573. $select = 'su.name, su.id, su.avatar';
  574. if ($type === "u_id") {
  575. $select = 'u.nickname as name, u.id, u.avatar_url as avatar';
  576. }
  577. //用户
  578. $store_id = get_params('storeId', get_store_id());
  579. $query = User::find()->where(['u.is_delete' => 0, 'u.store_id' => $store_id])->alias('u')
  580. ->leftJoin(['su' => SaasUser::tableName()], 'su.mobile=u.binding')
  581. ->andWhere(['and', ['not', ['u.nickname' => '']], ['not', ['su.name' => '']],['not in','binding',$mobiles], ['<>','binding','']])->select($select);
  582. if (!empty(get_params('keyword'))) {
  583. if($type === "u_id"){
  584. $query->andWhere(['like', 'u.nickname', get_params('keyword')]);
  585. }else{
  586. $query->andWhere(['like', 'su.name', get_params('keyword')]);
  587. }
  588. }
  589. //筛选有公众号openid的用户
  590. if ($scene === "hasMpOpenid") {
  591. $query->andWhere(['!=', 'wechat_platform_open_id', ''])
  592. ->addSelect(['wechat_platform_open_id']);
  593. }
  594. //选择股东
  595. if ($type === "shareHolder") {
  596. $query = ShareHolder::find()->alias('sh')->where(['sh.is_delete' => 0, 'sh.store_id' => $store_id])
  597. ->leftJoin(['u' => User::tableName()], 'sh.user_id = u.id')
  598. ->leftJoin(['su' => SaasUser::tableName()], 'su.mobile = u.binding')
  599. ->andWhere(['and', ['<>', 'u.nickname', ''], ['<>', 'sh.name', ''],['not in','u.binding',$mobiles]])
  600. ->select('su.name, u.id, u.price, su.avatar');
  601. if (!empty(get_params('keyword'))) {
  602. $query->andWhere(['OR', ['like', 'sh.name', get_params('keyword')], ['like', 'su.mobile', get_params('keyword')]]);
  603. }
  604. }
  605. //选择股东
  606. if ($type === "admin") {
  607. if (empty($scene)) {
  608. $scene = 'admin';
  609. }
  610. $query = Admin::find()->alias('a')->where(['a.is_delete' => 0, 'a.type' => $scene])
  611. ->leftJoin(['su' => SaasUser::tableName()], 'su.id = a.saas_user_id')
  612. // ->leftJoin(['u' => User::tableName()], 'u.binding = su.mobile')
  613. ->andWhere(['and', ['<>', 'a.name', ''],['<>','a.id',1]])
  614. ->select('a.username name, a.id, su.avatar')->groupBy('su.id');
  615. if (!empty(get_params('keyword'))) {
  616. $query->andWhere(['like', 'a.name', get_params('keyword')]);
  617. }
  618. //var_dump($query->createCommand()->getRawSql());die;
  619. }
  620. //股东
  621. if ($type === "user" && $scene === "shareHolder") {
  622. //排除股东
  623. $mobiles = ShareHolder::find()->alias('sh')->where(['sh.is_delete'=>0, 'sh.store_id' => get_store_id()])
  624. ->leftJoin(['u'=>User::tableName()],'sh.user_id = u.id')
  625. ->select("u.binding")->column();
  626. $query = User::find()->where(['u.is_delete' => 0, 'u.store_id' => $store_id])->alias('u')
  627. ->leftJoin(['su' => SaasUser::tableName()], 'su.mobile=u.binding')
  628. ->andWhere(['and', ['not', ['u.nickname' => '']], ['not', ['su.name' => '']],['not in','binding',$mobiles]])->select('su.name, u.id, su.avatar');
  629. if (!empty(get_params('keyword'))) {
  630. $query->andWhere(['OR', ['like', 'su.name', get_params('keyword')], ['like', 'su.mobile', get_params('keyword')]]);
  631. }
  632. $query->groupBy('u.id');
  633. }
  634. //股东
  635. if ($type === "area" && $scene === "shareHolder") {
  636. //排除股东
  637. $share_holder_user_id = ShareHolder::find()->where(['is_delete' => 0, 'store_id' => $store_id])->andWhere(['>', 'agent_type', 0])
  638. ->select("user_id")->column();
  639. $query = User::find()->where(['u.is_delete' => 0, 'u.store_id' => $store_id])->alias('u')
  640. ->leftJoin(['su' => SaasUser::tableName()], 'su.mobile=u.binding')
  641. ->andWhere(['and', ['not', ['u.nickname' => '']], ['not', ['su.name' => '']],['NOT IN','u.id',$share_holder_user_id]])
  642. ->select('su.name, u.id, su.avatar');
  643. if (!empty(get_params('keyword'))) {
  644. $query->andWhere(['OR', ['like', 'su.name', get_params('keyword')], ['like', 'su.mobile', get_params('keyword')]]);
  645. }
  646. $query->groupBy('u.id');
  647. }
  648. //分销商
  649. if ($type === "share") {
  650. $query = User::find()->where(['u.is_delete' => 0, 'u.store_id' => $store_id, 'u.is_distributor' => 0])->alias('u')
  651. ->andWhere( ['not', ['u.nickname' => '']])->select('u.nickname as name, u.id, u.avatar_url as avatar');
  652. if (!empty(get_params('keyword'))) {
  653. $query->andWhere(['like', 'u.nickname', get_params('keyword')]);
  654. }
  655. $query->groupBy('u.id');
  656. }
  657. //联盟用户
  658. if ($type === 'user' && empty($scene)) {
  659. $query = SaasUser::find()->where(['is_delete' => 0])
  660. ->select('name, id, avatar, mobile');
  661. if (!empty(get_params('keyword'))) {
  662. $query->andWhere(['OR', ['like', 'name', get_params('keyword')], ['like', 'mobile', get_params('keyword')]]);
  663. }
  664. }
  665. //业务员
  666. if ($type === 'user' && $scene === "user") {
  667. $user_id = Salesman::find()->where(['is_delete' => 0])->select('saas_user_id')->column();
  668. $query = SaasUser::find()->where(['is_delete' => 0])->andWhere(['not in', 'id', $user_id])
  669. ->select('name, id, avatar');
  670. }
  671. //服务人员
  672. if ($type === 'user' && $scene === "worker") {
  673. $user_id = Worker::find()->where(['status' => [0, 1]])->select('user_id')->column();
  674. $query = User::find()->alias('u')->leftJoin(['su' => SaasUser::tableName()], 'u.binding = su.mobile')
  675. ->where(['su.is_delete' => 0, 'u.store_id' => get_store_id(), 'u.is_delete' => 0])->andWhere(['not in', 'u.id', $user_id])
  676. ->select('su.name, u.id, su.avatar')->orderBy('id desc');
  677. if (!empty(get_params('keyword'))) {
  678. $query->andWhere(['OR', ['like', 'su.name', get_params('keyword')], ['like', 'su.mobile', get_params('keyword')]]);
  679. }
  680. }
  681. /* 2023年6月29日18:04:05 begin WPing id891新增功能 */
  682. //排除配送员
  683. if ($type === "user" && $scene === "courier") {
  684. $mobiles = LocalDeliveryCourier::find()->alias('c')->where(['c.is_delete'=>0])
  685. ->leftJoin(['su'=>SaasUser::tableName()],'c.saas_user_id = su.id')
  686. ->select("su.mobile")->column();
  687. $query = SaasUser::find()->where(['is_delete' => 0])->where(['not in','mobile',$mobiles])
  688. ->select('name, id, avatar, mobile');
  689. if (!empty(get_params('keyword'))) {
  690. $query->andWhere(['OR', ['like', 'name', get_params('keyword')], ['like', 'mobile', get_params('keyword')]]);
  691. }
  692. }
  693. //选择配送员
  694. if ($type === "courier") {
  695. $query = LocalDeliveryCourier::find()->alias('c')->where(['c.is_delete' => 0, 'status' => 1, 'state' => 2])
  696. ->leftJoin(['su' => SaasUser::tableName()], 'c.saas_user_id = su.id')
  697. ->andWhere(['<>', 'c.real_name', ''])
  698. ->select('c.real_name, c.id, su.avatar, c.mobile, c.max_num');
  699. if (!empty(get_params('keyword'))) {
  700. $query->andWhere(['OR', ['like', 'c.real_name', get_params('keyword')], ['like', 'c.mobile', get_params('keyword')]]);
  701. }
  702. if ($store_id > 0) {
  703. $query->andWhere(['c.store_id' => $store_id, 'type' => 2]);
  704. } else {
  705. $query->andWhere(['type' => 1]);
  706. }
  707. }
  708. if ($type === "bd_agent") {
  709. $db_agent = Admin::find()->where(['type' => Admin::ADMIN_TYPE_BD_AGENT, 'is_delete' => 0])->select('saas_user_id')->column();
  710. $query = SaasUser::find()->where(['is_delete' => 0])->andWhere(['AND', ['<>', 'name', ''], ['not in', 'id', $db_agent]])
  711. ->select('name, id, avatar, mobile');
  712. if (!empty(get_params('keyword'))) {
  713. $query->andWhere(['like', 'name', get_params('keyword')]);
  714. }
  715. }
  716. /* end */
  717. // 搜索品牌分类管理员
  718. if($type === "u_id" && $scene === "mch-brands-user"){
  719. // 排出已绑定的品牌分类管理员
  720. $has = MchBrands::find()->where(['store_id' => $store_id, 'is_delete' => 0])->select('user_id')->column();
  721. $query = User::find()->where(['u.is_delete' => 0, 'u.store_id' => $store_id])->alias('u')
  722. ->leftJoin(['su' => SaasUser::tableName()], 'su.mobile=u.binding')
  723. ->andWhere(['and', ['not', ['u.nickname' => '']], ['not', ['su.name' => '']],['not in','u.id',$has]])->select('u.nickname, u.id, u.avatar_url');
  724. if (!empty(get_params('keyword'))) {
  725. $query->andWhere(['OR', ['like', 'u.nickname', get_params('keyword')], ['like', 'u.binding', get_params('keyword')]]);
  726. }
  727. $query->groupBy('u.id');
  728. }
  729. // 入住商员工
  730. if($type === "u_id" && $scene === "mch-staff-user"){
  731. // 排出已绑定的品牌分类管理员
  732. $has = MchStaff::find()->where(['store_id' => $store_id, 'is_delete' => 0])->select('user_id')->column();
  733. $query = User::find()->where(['u.is_delete' => 0, 'u.store_id' => $store_id])->alias('u')
  734. ->leftJoin(['su' => SaasUser::tableName()], 'su.mobile=u.binding')
  735. ->andWhere(['and', ['not', ['u.nickname' => '']], ['not', ['su.name' => '']],['not in','u.id',$has]])->select('u.nickname, u.id, u.avatar_url');
  736. if (!empty(get_params('keyword'))) {
  737. $query->andWhere(['OR', ['like', 'u.nickname', get_params('keyword')], ['like', 'u.binding', get_params('keyword')]]);
  738. }
  739. $query->groupBy('u.id');
  740. }
  741. $pagination = pagination_make($query);
  742. if($type === "u_id" && $scene === "mch-staff-user"){
  743. // 排出入住商管理员
  744. $hasMch = Mch::find()->where(['is_delete'=>0,'store_id' => $store_id])->select('user_id')->column();
  745. foreach($pagination['list'] as &$item){
  746. $item['is_disable'] = in_array($item['id'],$hasMch) ? 1 : 0;
  747. }
  748. }
  749. if ($type === "courier") {
  750. foreach($pagination['list'] as &$v) {
  751. $now_num = DeliveryInfo::find()->alias('di')
  752. ->leftJoin(['o' => Order::tableName()], 'di.order_no = o.order_no')
  753. ->where(['o.trade_status' => 2, 'is_local' => 1, 'rider_id' => $v['id']])
  754. ->count();
  755. $v['now_order_status'] = $v['max_num'] == 0 ? '无限制' : $now_num.'/'.$v['max_num'];
  756. }
  757. }
  758. //增加分享员逻辑
  759. if ($scene === 'share') {
  760. foreach($pagination['list'] as &$v) {
  761. $share = Share::findOne(['user_id' => $v['id'], 'store_id' => get_store_id(), 'is_delete' => 0]);
  762. $v['level'] = $share->level ?: 0;
  763. }
  764. }
  765. return $this->asJson([
  766. 'code' => 0,
  767. 'msg' => 'success',
  768. 'data' => [
  769. 'data' => $pagination['list'],
  770. 'pageNo' => $pagination['pageNo'],
  771. 'totalCount' => $pagination['totalCount']
  772. ],
  773. ]);
  774. }
  775. // 获取user表用户列表
  776. public function actionGetUserList()
  777. {
  778. $type = get_params("type", 'user');
  779. if ($type == 'shareholder') {
  780. $parentId = \get_params('parent_id', -1);
  781. $query = User::find()->alias('u')
  782. ->where(['u.is_delete' => 0, 'u.store_id' => get_store_id(), 'u.is_holder' => 1])
  783. ->andWhere(['not', ['u.nickname' => '']])
  784. ->select('u.id, u.avatar_url avatar, u.nickname name');
  785. if (!empty(get_params('keyword'))) {
  786. $query->andWhere(['like', 'u.nickname', get_params('keyword')]);
  787. }
  788. if ($parentId > -1) {
  789. $query->andWhere(['u.old_parent_id' => $parentId]);
  790. $query->andWhere(['<>', 'u.id', $parentId]);
  791. }
  792. $pagination = pagination_make($query);
  793. } else {
  794. $query = User::find()->alias('u')
  795. ->leftJoin(['su' => SaasUser::tableName()], 'u.binding=su.mobile')
  796. ->where(['u.is_delete' => 0, 'u.store_id' => get_store_id()])
  797. ->andWhere(['not', ['u.nickname' => '']])
  798. ->andWhere(['AND', ['<>', 'u.binding', ''], ['su.is_delete' => 0]])
  799. ->select('u.id, u.avatar_url avatar, u.nickname name');
  800. if (!empty(get_params('keyword'))) {
  801. $query->andWhere(['OR', ['like', 'u.nickname', get_params('keyword')], ['like', 'u.binding', get_params('keyword')]]);
  802. }
  803. $pagination = pagination_make($query);
  804. if($pagination['pageNo'] == 1){
  805. $temp[0] = ['id'=>0,'avatar'=>'','name'=>'总店'];
  806. $pagination['list'] = array_merge($temp,$pagination['list']);
  807. }
  808. }
  809. $this->asJson([
  810. 'code' => 0,
  811. 'msg' => 'success',
  812. 'data' => [
  813. 'data' => $pagination['list'],
  814. 'pageNo' => $pagination['pageNo'],
  815. 'totalCount' => $pagination['totalCount']
  816. ],
  817. ]);
  818. }
  819. /**
  820. * 管理员列表
  821. * @return \yii\web\Response
  822. */
  823. public function actionAdminUser() {
  824. $userForm = new UserForm();
  825. $userForm->attributes = get_params();
  826. // $userForm->is_admin = get_params('is_admin', 0);
  827. $store_id = get_store_id();
  828. $userForm->store_id = $store_id;
  829. return $this->asJson($userForm->getUserList());
  830. }
  831. /**
  832. * 添加管理员
  833. * @return \yii\web\Response
  834. */
  835. public function actionAddAdmin() {
  836. try {
  837. $user = User::findOne(['id' => get_params('id'), 'store_id' => get_store_id()]);
  838. //查询是否已经绑定商城管理员
  839. if (empty($user->binding)) {
  840. throw new \Exception('添加失败,用户未绑定手机号,请更换用户或使用手机端重新登录更新手机号信息');
  841. }
  842. $saas_user = SaasUser::findOne(['mobile' => $user->binding, 'is_delete' => 0]);
  843. $user_admin = User::find()->where(['binding' => $saas_user->mobile, 'is_delete' => 0, 'is_admin' => 1])->one();
  844. if ($user_admin) {
  845. throw new \Exception('添加失败,用户已经绑定其他商城管理员');
  846. } else {
  847. $admin = Admin::find()->where(['or',
  848. ['saas_user_id' => $saas_user->id],
  849. ['mobile' => $saas_user->mobile]])->andWhere(['is_delete' => 0, 'type' => 'store'])->one();
  850. if ($admin) {
  851. throw new \Exception('添加失败,用户已经绑定其他商城管理员');
  852. }
  853. $store_audit = StoreAudit::findOne(['saas_user_id' => $saas_user->id]);
  854. if ($store_audit) {
  855. throw new \Exception('添加失败,用户已经绑定其他商城管理员');
  856. }
  857. }
  858. $user->is_admin = 1;
  859. if ($user->save()) {
  860. return $this->asJson([
  861. 'code' => 0,
  862. 'msg' => '添加成功'
  863. ]);
  864. } else {
  865. throw new \Exception('添加失败');
  866. }
  867. } catch (\Exception $e) {
  868. return $this->asJson([
  869. 'code' => 1,
  870. 'msg' => $e->getMessage()
  871. ]);
  872. }
  873. }
  874. /**
  875. * 删除管理员
  876. * @return \yii\web\Response
  877. */
  878. public function actionDelAdmin() {
  879. $userForm = new UserForm();
  880. $userForm->attributes = get_params();
  881. $store_id = get_store_id();
  882. $userForm->store_id = $store_id;
  883. return $this->asJson($userForm->delAdmin());
  884. }
  885. /**
  886. * 设置推荐人
  887. */
  888. public function actionSetParent()
  889. {
  890. $user_id = post_params('user_id');
  891. $parent_id = post_params('parent_id', 0);
  892. if (!$user_id) {
  893. return $this->asJson([
  894. 'code' => 1,
  895. 'msg' => '参数错误',
  896. ]);
  897. }
  898. $user = User::findOne($user_id);
  899. if (!$user) {
  900. return $this->asJson([
  901. 'code' => 1,
  902. 'msg' => '用户未找到',
  903. ]);
  904. }
  905. if ($parent_id > 0) {
  906. $parent = User::find()->where([
  907. 'id' => $parent_id,
  908. 'store_id' => $user->store_id,
  909. ])->one();
  910. if (!$parent) {
  911. return $this->asJson([
  912. 'code' => 1,
  913. 'msg' => '推荐人未找到',
  914. ]);
  915. }
  916. }
  917. $user->parent_id = $parent_id;
  918. $user->old_parent_id = $parent_id;
  919. $user->save();
  920. return $this->asJson([
  921. 'code' => 0,
  922. 'msg' => '保存成功',
  923. ]);
  924. }
  925. /**
  926. * 用户默认设置
  927. */
  928. public function actionUserDefaultInfo() {
  929. return;
  930. if (\Yii::$app->request->isPost) {
  931. $data = post_params();
  932. if (intval($data['isOpenDefault']) &&
  933. (empty($data['default_user_nickname']) || empty($data['default_user_pic']))
  934. ) {
  935. return $this->asJson([
  936. 'code' => 1,
  937. 'msg' => '请填写头像和昵称',
  938. ]);
  939. }
  940. Option::set('default_user_info', Json::encode($data), get_store_id(), 'store');
  941. return $this->asJson([
  942. 'code' => 0,
  943. 'msg' => '修改成功',
  944. ]);
  945. }
  946. $baseUrl = \Yii::$app->request->hostInfo . \Yii::$app->request->baseUrl;
  947. $wxappUrl = $baseUrl . '/web/v1/statics/clientImg';
  948. $default = Json::encode(['default_user_nickname' => '微信用户', 'default_user_pic' => $wxappUrl . '/default_wechat_avatar.jpeg']);
  949. $option = Option::get('default_user_info', get_store_id(), 'store', $default);
  950. $default_info = Json::decode($option['value']);
  951. $default_info['default_user_nickname'] = $default_info['default_user_nickname'] ?: '微信用户';
  952. $default_info['default_user_pic'] = $default_info['default_user_pic'] ?: $wxappUrl . '/default_wechat_avatar.jpeg';
  953. return $this->asJson([
  954. 'code' => 0,
  955. 'data' => $default_info,
  956. 'msg' => '',
  957. ]);
  958. }
  959. //设置用户收货地址
  960. public function actionSetUserAddress() {
  961. $form = new UserForm();
  962. $form->attributes = post_params();
  963. $form->is_default = post_params('is_default', 0);
  964. $form->store_id = get_store_id();
  965. return $this->asJson($form->addAddress());
  966. }
  967. //设置用户收货地址(0=设置默认 1=删除)
  968. public function actionSetUserAddressStatus() {
  969. $form = new UserForm();
  970. $form->attributes = post_params();
  971. $form->is_default = post_params('is_default', 0);
  972. $form->store_id = get_store_id();
  973. return $this->asJson($form->setUserAddressStatus());
  974. }
  975. //获取用户收货地址
  976. public function actionGetUserAddress() {
  977. $form = new UserForm();
  978. $form->attributes = get_params();
  979. return $this->asJson($form->getUserAddress());
  980. }
  981. //获取用户佣金
  982. public function actionUserPriceData() {
  983. $id = input_params('id');
  984. $mobile = input_params('mobile');
  985. $name = input_params('name');
  986. $store_id = get_store_id();
  987. $query = User::find()->where(['store_id' => $store_id])->orderBy("id desc");
  988. $queryCount = clone $query;
  989. if ($id) {
  990. $query->andWhere(['id' => $id]);
  991. }
  992. if ($mobile) {
  993. $query->andWhere(['like', 'binding', $mobile]);
  994. }
  995. $query->andWhere(['!=', 'binding', '']);
  996. if ($name) {
  997. $query->andWhere(['binding' => SaasUser::find()->where(['like', 'name', $name])->select('mobile')]);
  998. }
  999. $query->select('id, binding, total_price, price');
  1000. $list = pagination_make($query);
  1001. foreach($list['list'] as &$item){
  1002. $su = SaasUser::findOne(['mobile' => $item['binding']]);
  1003. $item['name'] = $su['name'];
  1004. $item['avatar'] = $su['avatar'];
  1005. $item['userCashSum'] = floor_num(\app\models\Cash::find()->where(['is_delete' => 0, 'user_id' => $item['id'], 'status' => 2])->andWhere(['!=', 'cash_type', 2])->sum('price'));
  1006. $team = new \app\modules\client\models\v1\TeamForm();
  1007. $team->user_id = $item['id'];
  1008. $team->store_id = $store_id;
  1009. $arr = $team->getOrderCount();
  1010. $item['order_money_un'] = floor_num($arr['order_money_un']);
  1011. }
  1012. if($list['pageNo'] == 1){
  1013. $count = [];
  1014. $count['userCount'] = (int)$queryCount->count();
  1015. $count['userPriceSum'] = (int)$queryCount->sum('price');
  1016. $count['userCashSum'] = floor_num(\app\models\Cash::find()->where(['is_delete' => 0, 'store_id' => $store_id, 'status' => 2])->andWhere(['!=', 'cash_type', 2])->sum('price'));
  1017. $count['order_money_un'] = floor_num(array_sum(Order::find()->where(['store_id' => $store_id, 'trade_status' => [0, 2, 3], 'is_sale' => 0])
  1018. ->select('SUM(limit_price), SUM(rebate), SUM(first_price), SUM(second_price), SUM(third_price)')
  1019. ->addSelect('SUM(holder_first_price), SUM(holder_second_price), SUM(holder_third_price)')
  1020. ->addSelect('SUM(old_holder_first_price), SUM(old_holder_second_price), SUM(old_holder_third_price)')
  1021. ->asArray()->one()));
  1022. }
  1023. return $this->asJson([
  1024. 'code' => 0,
  1025. 'msg' => 'success',
  1026. 'data' => $list,
  1027. 'count' => $count,
  1028. ]);
  1029. }
  1030. //获取用户提现
  1031. public function actionUserCashLog() {
  1032. $user_id = input_params('user_id');
  1033. $user = User::findOne($user_id);
  1034. $su = SaasUser::findOne(['mobile' => $user['binding']]);
  1035. $store_id = get_store_id();
  1036. $query = \app\models\Cash::find()->alias('c')->where(['c.is_delete' => 0, 'c.user_id' => $user_id, 'c.status' => 2])->andWhere(['!=', 'cash_type', 2]);
  1037. $list = pagination_make($query);
  1038. foreach($list['list'] as &$item){
  1039. $item['name'] = $su['name'];
  1040. }
  1041. return $this->asJson([
  1042. 'code' => 0,
  1043. 'msg' => 'success',
  1044. 'data' => $list,
  1045. ]);
  1046. }
  1047. //获取用户未到账佣金
  1048. public function actionUserPriceOrderLog() {
  1049. $user_id = input_params('user_id');
  1050. $user = User::findOne($user_id);
  1051. $su = SaasUser::findOne(['mobile' => $user['binding']]);
  1052. $store_id = get_store_id();
  1053. $team = new \app\modules\client\models\v1\TeamForm();
  1054. $team->user_id = $user_id;
  1055. $team->store_id = $store_id;
  1056. $team->status = 1;
  1057. $res = $team->getOrder();
  1058. return $this->asJson($res);
  1059. }
  1060. //获取用户佣金
  1061. public function actionUserPriceLog() {
  1062. $user_id = input_params('user_id');
  1063. $user = User::findOne($user_id);
  1064. $su = SaasUser::findOne(['mobile' => $user['binding']]);
  1065. $store_id = get_store_id();
  1066. $query = UserShareMoney::find()->alias('usm')
  1067. ->leftJoin(['o' => Order::tableName()], 'usm.order_id = o.id')
  1068. ->leftJoin(['u' => User::tableName()], 'o.user_id = u.id')
  1069. ->leftJoin(['su' => SaasUser::tableName()], 'u.binding = su.mobile')
  1070. ->where(['usm.user_id' => $user_id])->orderBy("usm.id desc");
  1071. $query->select('usm.*, o.order_no, o.total_price order_total_price, su.name order_user_name');
  1072. $list = pagination_make($query);
  1073. foreach($list['list'] as &$item){
  1074. $item['name'] = $su['name'];
  1075. $item['source_name'] = UserShareMoney::SOURCE_NAME[$item['source']] ?? '--';
  1076. }
  1077. return $this->asJson([
  1078. 'code' => 0,
  1079. 'msg' => 'success',
  1080. 'data' => $list,
  1081. ]);
  1082. }
  1083. //获取用户余额
  1084. public function actionUserMoneyData() {
  1085. $id = input_params('id');
  1086. $mobile = input_params('mobile');
  1087. $name = input_params('name');
  1088. $store_id = get_store_id();
  1089. $query = User::find()->where(['store_id' => $store_id])->orderBy("id desc");
  1090. $queryCount = clone $query;
  1091. if ($id) {
  1092. $query->andWhere(['id' => $id]);
  1093. }
  1094. if ($mobile) {
  1095. $query->andWhere(['like', 'binding', $mobile]);
  1096. }
  1097. $query->andWhere(['!=', 'binding', '']);
  1098. if ($name) {
  1099. $query->andWhere(['binding' => SaasUser::find()->where(['like', 'name', $name])->select('mobile')]);
  1100. }
  1101. $query->select('id, binding, money');
  1102. $list = pagination_make($query);
  1103. foreach($list['list'] as &$item){
  1104. $su = SaasUser::findOne(['mobile' => $item['binding']]);
  1105. $item['name'] = $su['name'];
  1106. $item['avatar'] = $su['avatar'];
  1107. $item['userRechargeSum'] = floor_num(\app\models\ReOrder::find()->where(['is_delete' => 0, 'user_id' => $item['id'], 'is_pay' => 1])->sum('pay_price'));
  1108. }
  1109. if($list['pageNo'] == 1){
  1110. $count = [];
  1111. $count['userCount'] = (int)$queryCount->count();
  1112. $count['userMoneySum'] = floor_num($queryCount->sum('money'));
  1113. $count['userRechargeSum'] = floor_num(\app\models\ReOrder::find()->where(['is_delete' => 0, 'store_id' => $store_id, 'is_pay' => 1])->sum('pay_price'));
  1114. }
  1115. return $this->asJson([
  1116. 'code' => 0,
  1117. 'msg' => 'success',
  1118. 'data' => $list,
  1119. 'count' => $count,
  1120. ]);
  1121. }
  1122. //获取佣金 余额 积分 转赠记录
  1123. public function actionGetAccountLog() {
  1124. try {
  1125. $type = (int)get_params('type', 0);
  1126. $log_type = get_params('log_type');
  1127. $user_name = get_params('user_name');
  1128. $from_user_name = get_params('from_user_name');
  1129. $start_time = get_params('start_time');
  1130. $end_time = get_params('end_time');
  1131. $store_id = get_store_id();
  1132. switch ($type) {
  1133. case 1: //余额 且 赠送
  1134. $type = 2;
  1135. $from = [1, 2];
  1136. break;
  1137. case 2://余额 且 佣金转
  1138. $type = 2;
  1139. $from = 3;
  1140. break;
  1141. default: //积分 且 赠送
  1142. $type = 1;
  1143. $from = [1, 2];
  1144. break;
  1145. }
  1146. $query = AccountLog::find()->where([
  1147. 'store_id' => $store_id,
  1148. 'type' => $type, // 余额
  1149. 'from' =>$from
  1150. ]);
  1151. if (in_array($log_type, [1, 2])) {
  1152. $query->andWhere(['log_type' => $log_type]);
  1153. }
  1154. if($user_name){
  1155. $user_id = User::find()->alias('u')->leftJoin(['su' => SaasUser::tableName()], 'u.binding = su.mobile')
  1156. ->where(['u.store_id' => $store_id])->andWhere(['like', 'su.name', $user_name])->select('u.id');
  1157. $query->andWhere(['user_id' => $user_id]);
  1158. }
  1159. if($from_user_name){
  1160. $user_id = User::find()->alias('u')->leftJoin(['su' => SaasUser::tableName()], 'u.binding = su.mobile')
  1161. ->where(['u.store_id' => $store_id])->andWhere(['like', 'su.name', $from_user_name])->select('u.id');
  1162. $query->andWhere(['from_user_id' => $user_id]);
  1163. }
  1164. if (!empty($start_time)) {
  1165. $query->andWhere(['>' , 'created_at', strtotime($start_time)]);
  1166. }
  1167. if (!empty($end_time)) {
  1168. $query->andWhere(['<' , 'created_at', strtotime($end_time)]);
  1169. }
  1170. $query->orderBy('id DESC');
  1171. $list = pagination_make($query);
  1172. $userIds = array_unique(array_merge(array_column($list['list'], 'user_id'), array_column($list['list'], 'from_user_id')));
  1173. $arr = User::find()->alias('u')->leftJoin(['su' => SaasUser::tableName()], 'u.binding = su.mobile')
  1174. ->where(['u.store_id' => $store_id, 'u.id' => $userIds])->select('u.id, su.name, su.mobile, su.avatar')->asArray()->all();
  1175. $users = array_combine(array_column($arr, 'id'), $arr);
  1176. foreach ($list['list'] as &$value) {
  1177. $value['date'] = date('Y-m-d H:i:s', $value['created_at']);
  1178. $value['content'] = $value['desc'];
  1179. $value['money_base'] = $value['amount'];
  1180. $value['money_received'] = $value['amount'];
  1181. if($value['from'] == 1){
  1182. $value['money_received'] = $value['amount'] - $value['profit'];
  1183. }
  1184. if($value['from'] == 2){
  1185. $value['money_base'] = $value['amount'] + $value['profit'];
  1186. }
  1187. if($value['from'] == 3){
  1188. $value['money_base'] = $value['amount'] + $value['profit'];
  1189. }
  1190. $value['user'] = $users[$value['user_id']];
  1191. $value['from_user'] = $users[$value['from_user_id']];
  1192. }
  1193. return $this->asJson([
  1194. 'code' => 0,
  1195. 'msg' => 'success',
  1196. 'data' => $list,
  1197. 'q' => $query->createCommand()->getRawSql(),
  1198. ]);
  1199. }catch (\Exception $e) {
  1200. return $this->asJson([
  1201. 'code' => 1,
  1202. 'msg' => $e->getMessage()
  1203. ]);
  1204. }
  1205. }
  1206. //月付客户额度设置
  1207. public function actionMoneyMonthSet() {
  1208. $user_id = input_params('user_id');
  1209. if(!is_array($user_id)){
  1210. $user_id = explode(',', $user_id);
  1211. }
  1212. $money_month = input_params('money_month');
  1213. if($money_month <= 0){
  1214. return $this->asJson([
  1215. 'code' => 1,
  1216. 'msg' => '额度数值错误',
  1217. ]);
  1218. }
  1219. $update = User::updateAll(['money_month' => $money_month], ['id' => $user_id, 'store_id' => get_store_id()]);
  1220. return $this->asJson([
  1221. 'code' => 0,
  1222. 'msg' => '操作成功,修改数据' . $update . '条',
  1223. ]);
  1224. }
  1225. //月付客户删除
  1226. public function actionMoneyMonthDel() {
  1227. $user_id = input_params('user_id');
  1228. if(!is_array($user_id)){
  1229. $user_id = explode(',', $user_id);
  1230. }
  1231. $user_err = User::find()->where(['id' => $user_id])->andWhere(['!=', 'money_month_used', 0])->limit(1)->one();
  1232. if($user_err){
  1233. return $this->asJson([
  1234. 'code' => 1,
  1235. 'msg' => '操作失败,存在已用额度非零用户:' . $user_err->binding,
  1236. ]);
  1237. }
  1238. $update = User::updateAll(['money_month' => 0], ['id' => $user_id, 'store_id' => get_store_id()]);
  1239. return $this->asJson([
  1240. 'code' => 0,
  1241. 'msg' => '操作成功,修改数据' . $update . '条',
  1242. ]);
  1243. }
  1244. //月付客户还款
  1245. public function actionMoneyMonthRepay() {
  1246. $user_id = input_params('user_id');
  1247. $money = input_params('money');
  1248. $user = User::findOne(['id' => $user_id, 'store_id' => get_store_id()]);
  1249. if($user->money_month_used == 0){
  1250. return $this->asJson([
  1251. 'code' => 1,
  1252. 'msg' => '操作失败,未使用额度,无需还款',
  1253. ]);
  1254. }
  1255. $res = UserPayMonth::repay($user->id, $money);
  1256. return $this->asJson($res);
  1257. }
  1258. //月付客户额度记录
  1259. public function actionMoneyMonthLog() {
  1260. $store_id = get_store_id();
  1261. $type = input_params('type');
  1262. $user_id = input_params('user_id');
  1263. $begin_time = input_params('begin_time');
  1264. $end_time = input_params('end_time');
  1265. $query = UserPayMonth::find()->where(['store_id' => $store_id]);
  1266. if(isset($user_id) && $user_id > 0){
  1267. $query->andWhere(['user_id' => $user_id]);
  1268. }
  1269. if(isset($type) && $type > -1){
  1270. $query->andWhere(['type' => $type]);
  1271. }
  1272. if($begin_time){
  1273. $query->andWhere(['>=', 'created_at', strtotime($begin_time . ' 00:00:00')]);
  1274. }
  1275. if($end_time){
  1276. $query->andWhere(['<=', 'created_at', strtotime($end_time . ' 23:59:59')]);
  1277. }
  1278. $querySum = clone $query;
  1279. $sumMoney = $querySum->sum('money');
  1280. $query->orderBy('id DESC');
  1281. $data = pagination_make($query);
  1282. return $this->asJson([
  1283. 'q' => $query->createCommand()->getRawSql(),
  1284. 'code' => 0,
  1285. 'msg' => 'ok',
  1286. 'data' => $data,
  1287. 'sumMoney' => $sumMoney,
  1288. ]);
  1289. }
  1290. public function actionUserInfo() {
  1291. $userForm = new UserForm();
  1292. $userForm->attributes = all_params();
  1293. $store_id = get_store_id();
  1294. $userForm->store_id = $store_id;
  1295. $userForm->user_id = input_params('user_id');
  1296. $res = $userForm->getUserList();
  1297. if(!$res['data']['data']){
  1298. return $this->asJson([
  1299. 'code' => 1,
  1300. 'msg' => '用户不存在',
  1301. ]);
  1302. }
  1303. return $this->asJson([
  1304. 'code' => 0,
  1305. 'msg' => 'ok',
  1306. 'data' => $res['data']['data'][0],
  1307. ]);
  1308. }
  1309. public function actionGetUserPrice() {
  1310. $userForm = new UserForm();
  1311. $userForm->attributes = get_params();
  1312. $userForm->store_id = get_store_id();
  1313. return $this->asJson($userForm->getUserPrice());
  1314. }
  1315. public function actionUserPriceTotal() {
  1316. $userForm = new UserForm();
  1317. $params = all_params();
  1318. $store_id = get_store_id();
  1319. $userForm->store_id = $store_id;
  1320. $userForm->user_id = input_params('user_id');
  1321. $res = $userForm->userPriceTotal($params);
  1322. return $this->asJson([
  1323. 'code' => 0,
  1324. 'msg' => 'ok',
  1325. 'data' => $res,
  1326. ]);
  1327. }
  1328. public function actionGrowthSetting(){
  1329. $store_id = get_store_id();
  1330. if(\Yii::$app->request->isPost){
  1331. $params = all_params();
  1332. $res = Option::set('growth_setting', json_encode($params), $store_id, 'level');
  1333. if($res){
  1334. return $this->asJson([
  1335. 'code' => 0,
  1336. 'msg' => 'ok',
  1337. ]);
  1338. }else{
  1339. return $this->asJson([
  1340. 'code' => 1,
  1341. 'msg' => '保存失败',
  1342. ]);
  1343. }
  1344. }else{
  1345. $default = json_encode([
  1346. 'buy_give' => 0,
  1347. 'share_give' => 0,
  1348. 'sign_give' => 0
  1349. ]);
  1350. $result = Option::get('growth_setting', $store_id, 'level', $default);
  1351. return $this->asJson([
  1352. 'code' => 0,
  1353. 'msg' => 'ok',
  1354. 'data' => json_decode($result['value'],true),
  1355. ]);
  1356. }
  1357. }
  1358. public function actionGrowthLog(){
  1359. $storeId = get_store_id();
  1360. $userId = get_params('user_id');
  1361. $query = UserGrowthValueLog::find()->where(['user_id' => $userId,'store_id' => $storeId]);
  1362. $list = pagination_make($query,true,'created_at DESC');
  1363. foreach($list['list'] as &$item){
  1364. $item['created_at'] = date('Y-m-d H:i:s',$item['created_at']);
  1365. }
  1366. return $this->asJson([
  1367. 'code' => 0,
  1368. 'msg' => 'ok',
  1369. 'data' => $list,
  1370. ]);
  1371. }
  1372. }