ShopAdminController.php 11 KB

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