ShopAdminController.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. <?php
  2. namespace ghs\controllers;
  3. use biz\admin\classes\AdminClass;
  4. use biz\shop\classes\ShopClass;
  5. use bizHd\wx\classes\WxOpenClass;
  6. use bizGhs\shop\classes\ShopAdminClass;
  7. use bizGhs\admin\services\AdminRoleService;
  8. use bizGhs\shop\services\ShopAdminService;
  9. use common\components\dict;
  10. use common\components\imgUtil;
  11. use common\components\sms;
  12. use common\components\stringUtil;
  13. use common\components\util;
  14. use Yii;
  15. /**
  16. * 员工控制器(表 xhStaff )
  17. * Class ShopAdminController
  18. * @package ghs\controllers
  19. */
  20. class ShopAdminController extends BaseController
  21. {
  22. public $guestAccess = ['generate-admin'];
  23. //设置店长 ssh 20250711
  24. public function actionSetManager()
  25. {
  26. $get = Yii::$app->request->get();
  27. $id = $get['id'] ?? 0;
  28. $staff = ShopAdminClass::getById($id, true);
  29. if (empty($staff)) {
  30. util::fail('没有员工');
  31. }
  32. if ($staff->mainId != $this->mainId) {
  33. util::fail('不是你的');
  34. }
  35. $current = $this->shopAdmin;
  36. if ($current->mobile != 15280215347) {
  37. util::fail('无效操作');
  38. }
  39. $connection = Yii::$app->db;
  40. $transaction = $connection->beginTransaction();
  41. try {
  42. $staff->founder = 2;
  43. $staff->save();
  44. $newAdminId = $staff->adminId;
  45. $mainId = $staff->mainId;
  46. $shopList = ShopClass::getAllByCondition(['mainId' => $mainId], null, 'id,shopName,mainId,adminId', null, true);
  47. $adminId = 0;
  48. if (!empty($shopList)) {
  49. foreach ($shopList as $shop) {
  50. if (!empty($shop->adminId)) {
  51. $adminId = $shop->adminId;
  52. }
  53. $shop->adminId = $newAdminId;
  54. $shop->save();
  55. }
  56. if (!empty($adminId)) {
  57. ShopAdminClass::updateByCondition(['mainId' => $mainId, 'adminId' => $adminId], ['founder' => 1]);
  58. }
  59. }
  60. $transaction->commit();
  61. util::complete('操作成功');
  62. } catch (\Exception $e) {
  63. $transaction->rollBack();
  64. Yii::info("操作失败原因:" . $e->getMessage());
  65. util::fail('操作失败');
  66. }
  67. }
  68. //获取当前登录员工的信息 ssh 20230802
  69. public function actionGetStaffInfo()
  70. {
  71. $staff = $this->shopAdmin;
  72. util::success(['staff' => $staff]);
  73. }
  74. //获取所有在职员工 ssh 20220507
  75. public function actionGetAllStaff()
  76. {
  77. $where = ['mainId' => $this->mainId, 'delStatus' => 0, 'status' => 1, 'isPt' => 0,];
  78. $list = ShopAdminClass::getAllByCondition($where, 'inTurn DESC', 'id,name,mobile', null, true);
  79. util::success(['list' => $list]);
  80. }
  81. //获取添加员工的验证码 ssh 20210117
  82. public function actionGetStaffAuthCode()
  83. {
  84. $get = Yii::$app->request->get();
  85. $mobile = $get['mobile'] ?? '';
  86. if (stringUtil::isMobile($mobile) == false) {
  87. util::fail('请输入正确手机号');
  88. }
  89. $rand = rand(11111, 99999);
  90. $minute = 10;
  91. sms::send($mobile . ',' . $rand . ',' . $minute, '您的员工验证码:{$var},{$var}分钟内有效');
  92. $ptStyle = Yii::$app->params['ptStyle'];
  93. $cacheKey = 'add_staff_auth_code_' . $ptStyle . '_' . $mobile;
  94. Yii::$app->redis->executeCommand('SETEX', [$cacheKey, 600, $rand]);
  95. util::complete();
  96. }
  97. //添加员工
  98. public function actionAddStaff()
  99. {
  100. $shopAdmin = $this->shopAdmin;
  101. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  102. util::fail('没有权限');
  103. }
  104. $adminId = $this->adminId;
  105. $shop = $this->shop;
  106. $connection = Yii::$app->db;
  107. $transaction = $connection->beginTransaction();
  108. try {
  109. $post = Yii::$app->request->post();
  110. $authCode = $post['authCode'] ?? '';
  111. $mobile = $post['mobile'] ?? '';
  112. $name = $post['name'] ?? '';
  113. $finance = $post['finance'] ?? 0;
  114. $identity = $post['identity'] ?? 0;
  115. $switchShop = $post['switchShop'] ?? 0;
  116. $ptSuperAdminId = AdminClass::getPtSuperAdminId();
  117. if ($shop->adminId != $adminId && $adminId != $ptSuperAdminId) {
  118. if ($finance == 1) {
  119. util::fail('超管才能开启员工财务权限');
  120. }
  121. if ($switchShop == 1) {
  122. util::fail('超管才能开启员工分店管理权限');
  123. }
  124. }
  125. util::checkRepeatCommit($mobile, 10);
  126. $ptStyle = Yii::$app->params['ptStyle'];
  127. //验证码取消验证,临时使用的方法
  128. if ($authCode != 19640123) {
  129. $cacheKey = 'add_staff_auth_code_' . $ptStyle . '_' . $mobile;
  130. $saveAuthCode = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  131. if (empty($saveAuthCode)) {
  132. util::fail('请填写验证码哦');
  133. }
  134. if (empty($authCode)) {
  135. util::fail('请填写验证码');
  136. }
  137. if ($saveAuthCode != $authCode) {
  138. util::fail('验证码错误');
  139. }
  140. }
  141. $ptStyle = Yii::$app->params['ptStyle'];
  142. $adminInfo = ['name' => $name, 'mobile' => $mobile, 'style' => $ptStyle, 'currentGhsShopId' => $this->shopId, 'identity' => $identity];
  143. $fromApp = dict::getDict('userSourceGetId', 'app', 'id');
  144. $admin = \bizGhs\admin\classes\AdminClass::replaceAdmin($adminInfo, $fromApp);
  145. $post['shopId'] = $this->shopId;
  146. $respond = ShopAdminClass::appGenerateStaff($post, $admin);
  147. $transaction->commit();
  148. util::success($respond);
  149. } catch (\Exception $exception) {
  150. $transaction->rollBack();
  151. Yii::info("失败原因:" . $exception->getMessage());
  152. util::fail('添加失败');
  153. }
  154. }
  155. //创建管理员数据准备 ssh 2020.4.17
  156. public function actionPrepareBind()
  157. {
  158. $post = Yii::$app->request->post();
  159. $shopAdmin = $this->shopAdmin->attributes;
  160. if ($shopAdmin['super'] != 1) {
  161. util::fail('超管才能操作');
  162. }
  163. $roleId = isset($post['roleId']) ? $post['roleId'] : 0;
  164. $role = AdminRoleService::getById($roleId);
  165. if (empty($role) || isset($role['sjId']) != $this->sjId) {
  166. util::fail('请选择角色!');
  167. }
  168. unset($post['userId']);
  169. unset($post['status']);
  170. $post['shopId'] = $this->shopId;
  171. $post['sjId'] = $this->sjId;
  172. $post['adminId'] = $this->adminId;
  173. $post['mainId'] = $this->mainId;
  174. $merchant = WxOpenClass::getGhsWxInfo();
  175. $post['merchant'] = $merchant;
  176. $respond = ShopAdminService::prepareBindAdmin($post);
  177. $miniCode = isset($respond['miniCode']) ? imgUtil::getPrefix() . $respond['miniCode'] : '';
  178. util::success(['miniCode' => $miniCode]);
  179. }
  180. //创建员工 ssh 2020.4.20
  181. public function actionGenerateAdmin()
  182. {
  183. $post = Yii::$app->request->post();
  184. $post['adminId'] = $this->adminId;
  185. $post['mainId'] = $this->mainId;
  186. $admin = $this->admin->attributes;
  187. $currentShopId = $admin['currentGhsShopId'] ?? 0;
  188. if (!empty($currentShopId)) {
  189. $shop = ShopClass::getShopInfo($currentShopId);
  190. //一个人不能同时在二个商家门店下工作
  191. if (isset($shop['sjId']) && $shop['sjId'] != $this->sjId) {
  192. util::fail('您已经是别的商家员工');
  193. }
  194. }
  195. $openShop = $admin['openGhsShop'] ?? AdminClass::OPEN_SHOP_NO;
  196. if ($openShop != AdminClass::OPEN_SHOP_NO) {
  197. util::fail('您已申请开过店,不能成为别人的员工');
  198. }
  199. $respond = ShopAdminService::generateAdmin($post, $admin);
  200. util::success($respond);
  201. }
  202. //收款通知 ssh 2021.1.14
  203. public function actionUpdateRemind()
  204. {
  205. $get = Yii::$app->request->get();
  206. $id = $get['id'] ?? 0;
  207. $remind = $get['remind'] ?? 0;
  208. $relation = ShopAdminClass::getById($id, true);
  209. if (empty($relation)) {
  210. util::fail('没有找到员工');
  211. }
  212. ShopAdminClass::valid($relation->attributes, $this->mainId);
  213. $relation->remind = $remind;
  214. $relation->save();
  215. util::complete();
  216. }
  217. //启用与停用 ssh 2021.1.14
  218. public function actionUpdateStatus()
  219. {
  220. $get = Yii::$app->request->get();
  221. $id = $get['id'] ?? 0;
  222. $status = $get['status'] ?? 0;
  223. $relation = ShopAdminClass::getById($id, true);
  224. if (empty($relation)) {
  225. util::fail('没有找到员工');
  226. }
  227. ShopAdminClass::valid($relation->attributes, $this->mainId);
  228. $relation->status = $status;
  229. $relation->save();
  230. util::complete();
  231. }
  232. //员工列表 ssh 2019.12.8
  233. public function actionList()
  234. {
  235. $where = [];
  236. $where['mainId'] = $this->mainId;
  237. $where['delStatus'] = 0;
  238. $where['isPt'] = 0;
  239. $list = ShopAdminService::getAdminList($where);
  240. util::success($list);
  241. }
  242. //获取员工信息 ssh 2021.1.13
  243. public function actionDetail()
  244. {
  245. $get = Yii::$app->request->get();
  246. $id = isset($get['id']) ? $get['id'] : 0;
  247. $respond = ShopAdminService::getAdminInfo($id);
  248. ShopAdminClass::valid($respond, $this->mainId);
  249. util::success($respond);
  250. }
  251. //修改员工信息
  252. public function actionUpdate()
  253. {
  254. $post = Yii::$app->request->post();
  255. $id = $post['id'] ?? 0;
  256. $shopAdmin = $this->shopAdmin;
  257. $adminId = $this->adminId;
  258. $shop = $this->shop;
  259. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  260. util::fail('没有权限');
  261. }
  262. if (empty($id)) {
  263. util::fail('请选择员工');
  264. }
  265. $relation = ShopAdminClass::getById($id, true);
  266. if (empty($relation)) {
  267. util::fail('没有找到员工');
  268. }
  269. if ($relation->mainId != $this->mainId) {
  270. util::fail('不能修改');
  271. }
  272. $roleId = $post['roleId'] ?? 0;
  273. $remind = $post['remind'] ?? 0;
  274. $status = $post['status'] ?? 1;
  275. $super = $post['super'] ?? 0;
  276. $name = $post['name'] ?? '';
  277. $finance = $post['finance'] ?? 0;
  278. $switchShop = $post['switchShop'] ?? 0;
  279. $identity = $post['identity'] ?? 0;
  280. $founder = $relation->founder ?? 1;
  281. $ptSuperAdminId = AdminClass::getPtSuperAdminId();
  282. if ($shop->adminId != $adminId && $adminId != $ptSuperAdminId) {
  283. if ($finance == 1) {
  284. util::fail('超管才能修改财务权限');
  285. }
  286. if ($switchShop == 1) {
  287. util::fail('超管才能修改管理分店权限');
  288. }
  289. }
  290. if ($founder == 2) {
  291. if ($super == 0) {
  292. util::fail('超管不能关闭改库存改价权限');
  293. }
  294. }
  295. if (!empty($name)) {
  296. $relation->name = $name;
  297. }
  298. $relation->roleId = $roleId;
  299. $relation->remind = $remind;
  300. $relation->status = $status;
  301. $relation->super = $super;
  302. $relation->finance = $finance;
  303. $relation->switchShop = $switchShop;
  304. $relation->identity = $identity;
  305. $relation->save();
  306. //需要更改原名,下单环节的下单人取的是人家原名的名称
  307. $currentId = $relation->adminId ?? 0;
  308. $current = AdminClass::getById($currentId, true);
  309. if (!empty($current)) {
  310. $current->name = $name;
  311. $current->save();
  312. }
  313. util::complete('修改成功');
  314. }
  315. //删除员工关系 ssh 2021.1.14
  316. public function actionDelete()
  317. {
  318. $id = Yii::$app->request->get('id');
  319. $shopAdmin = $this->shopAdmin;
  320. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  321. util::fail('没有权限');
  322. }
  323. $connection = Yii::$app->db;
  324. $transaction = $connection->beginTransaction();
  325. try {
  326. $relation = ShopAdminClass::getById($id, true);
  327. \biz\shop\classes\ShopAdminClass::deleteShopAdmin($relation, $this->shopAdminId, $this->mainId);
  328. $transaction->commit();
  329. util::complete();
  330. } catch (\Exception $exception) {
  331. $transaction->rollBack();
  332. Yii::info("失败原因:" . $exception->getMessage());
  333. util::fail('删除失败');
  334. }
  335. }
  336. }