ShopAdminController.php 13 KB

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