ShopAdminController.php 12 KB

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