ShopAdminController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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,], null, 'id,name', 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. $switchShop = $post['switchShop'] ?? 0;
  65. $ptSuperAdminId = AdminClass::getPtSuperAdminId();
  66. if ($shop->adminId != $adminId && $adminId != $ptSuperAdminId) {
  67. if ($finance == 1) {
  68. util::fail('超管才能开启员工财务权限');
  69. }
  70. if ($switchShop == 1) {
  71. util::fail('超管才能开启员工分店管理权限');
  72. }
  73. }
  74. util::checkRepeatCommit($mobile, 10);
  75. $ptStyle = Yii::$app->params['ptStyle'];
  76. $cacheKey = 'add_staff_auth_code_' . $ptStyle . '_' . $mobile;
  77. $saveAuthCode = Yii::$app->redis->executeCommand('GET', [$cacheKey]);
  78. if (empty($saveAuthCode)) {
  79. util::fail('请填写验证码哦');
  80. }
  81. if (empty($authCode)) {
  82. util::fail('请填写验证码');
  83. }
  84. if ($saveAuthCode != $authCode) {
  85. util::fail('验证码错误');
  86. }
  87. $ptStyle = Yii::$app->params['ptStyle'];
  88. $adminInfo = ['name' => $name, 'mobile' => $mobile, 'style' => $ptStyle, 'currentGhsShopId' => $this->shopId];
  89. $fromApp = dict::getDict('userSourceGetId', 'app', 'id');
  90. $admin = \bizGhs\admin\classes\AdminClass::replaceAdmin($adminInfo, $fromApp);
  91. $post['shopId'] = $this->shopId;
  92. $respond = ShopAdminClass::appGenerateStaff($post, $admin);
  93. $transaction->commit();
  94. util::success($respond);
  95. } catch (\Exception $exception) {
  96. $transaction->rollBack();
  97. Yii::info("失败原因:" . $exception->getMessage());
  98. util::fail('添加失败');
  99. }
  100. }
  101. //创建管理员数据准备 ssh 2020.4.17
  102. public function actionPrepareBind()
  103. {
  104. $post = Yii::$app->request->post();
  105. $shopAdmin = $this->shopAdmin->attributes;
  106. if ($shopAdmin['super'] != 1) {
  107. util::fail('超管才能操作');
  108. }
  109. $roleId = isset($post['roleId']) ? $post['roleId'] : 0;
  110. $role = AdminRoleService::getById($roleId);
  111. if (empty($role) || isset($role['sjId']) != $this->sjId) {
  112. util::fail('请选择角色!');
  113. }
  114. unset($post['userId']);
  115. unset($post['status']);
  116. $post['shopId'] = $this->shopId;
  117. $post['sjId'] = $this->sjId;
  118. $post['adminId'] = $this->adminId;
  119. $post['mainId'] = $this->mainId;
  120. $merchant = WxOpenClass::getGhsWxInfo();
  121. $post['merchant'] = $merchant;
  122. $respond = ShopAdminService::prepareBindAdmin($post);
  123. $miniCode = isset($respond['miniCode']) ? imgUtil::getPrefix() . $respond['miniCode'] : '';
  124. util::success(['miniCode' => $miniCode]);
  125. }
  126. //创建员工 ssh 2020.4.20
  127. public function actionGenerateAdmin()
  128. {
  129. $post = Yii::$app->request->post();
  130. $post['adminId'] = $this->adminId;
  131. $post['mainId'] = $this->mainId;
  132. $admin = $this->admin->attributes;
  133. $currentShopId = $admin['currentGhsShopId'] ?? 0;
  134. if (!empty($currentShopId)) {
  135. $shop = ShopClass::getShopInfo($currentShopId);
  136. //一个人不能同时在二个商家门店下工作
  137. if (isset($shop['sjId']) && $shop['sjId'] != $this->sjId) {
  138. util::fail('您已经是别的商家员工');
  139. }
  140. }
  141. $openShop = $admin['openGhsShop'] ?? AdminClass::OPEN_SHOP_NO;
  142. if ($openShop != AdminClass::OPEN_SHOP_NO) {
  143. util::fail('您已申请开过店,不能成为别人的员工');
  144. }
  145. $respond = ShopAdminService::generateAdmin($post, $admin);
  146. util::success($respond);
  147. }
  148. //收款通知 ssh 2021.1.14
  149. public function actionUpdateRemind()
  150. {
  151. $get = Yii::$app->request->get();
  152. $id = $get['id'] ?? 0;
  153. $remind = $get['remind'] ?? 0;
  154. $relation = ShopAdminClass::getById($id, true);
  155. if (empty($relation)) {
  156. util::fail('没有找到员工');
  157. }
  158. ShopAdminClass::valid($relation->attributes, $this->mainId);
  159. $relation->remind = $remind;
  160. $relation->save();
  161. util::complete();
  162. }
  163. //启用与停用 ssh 2021.1.14
  164. public function actionUpdateStatus()
  165. {
  166. $get = Yii::$app->request->get();
  167. $id = $get['id'] ?? 0;
  168. $status = $get['status'] ?? 0;
  169. $relation = ShopAdminClass::getById($id, true);
  170. if (empty($relation)) {
  171. util::fail('没有找到员工');
  172. }
  173. ShopAdminClass::valid($relation->attributes, $this->mainId);
  174. $relation->status = $status;
  175. $relation->save();
  176. util::complete();
  177. }
  178. //员工列表 ssh 2019.12.8
  179. public function actionList()
  180. {
  181. $where = [];
  182. $where['mainId'] = $this->mainId;
  183. $where['delStatus'] = 0;
  184. $where['isPt'] = 0;
  185. $list = ShopAdminService::getAdminList($where);
  186. util::success($list);
  187. }
  188. //获取员工信息 ssh 2021.1.13
  189. public function actionDetail()
  190. {
  191. $get = Yii::$app->request->get();
  192. $id = isset($get['id']) ? $get['id'] : 0;
  193. $respond = ShopAdminService::getAdminInfo($id);
  194. ShopAdminClass::valid($respond, $this->mainId);
  195. util::success($respond);
  196. }
  197. //修改员工信息
  198. public function actionUpdate()
  199. {
  200. $post = Yii::$app->request->post();
  201. $id = $post['id'] ?? 0;
  202. $shopAdmin = $this->shopAdmin;
  203. $adminId = $this->adminId;
  204. $shop = $this->shop;
  205. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  206. util::fail('没有权限');
  207. }
  208. if (empty($id)) {
  209. util::fail('请选择员工');
  210. }
  211. $relation = ShopAdminClass::getById($id, true);
  212. if (empty($relation)) {
  213. util::fail('没有找到员工');
  214. }
  215. if ($relation->mainId != $this->mainId) {
  216. util::fail('不能修改');
  217. }
  218. $roleId = $post['roleId'] ?? 0;
  219. $remind = $post['remind'] ?? 0;
  220. $status = $post['status'] ?? 1;
  221. $super = $post['super'] ?? 0;
  222. $name = $post['name'] ?? '';
  223. $finance = $post['finance'] ?? 0;
  224. $switchShop = $post['switchShop'] ?? 0;
  225. $founder = $relation->founder ?? 1;
  226. $ptSuperAdminId = AdminClass::getPtSuperAdminId();
  227. if ($shop->adminId != $adminId && $adminId != $ptSuperAdminId) {
  228. if ($finance == 1) {
  229. util::fail('超管才能修改财务权限');
  230. }
  231. if ($switchShop == 1) {
  232. util::fail('超管才能修改管理分店权限');
  233. }
  234. }
  235. if ($founder == 2) {
  236. if ($super == 0) {
  237. util::fail('超管不能关闭改库存改价权限');
  238. }
  239. }
  240. if (!empty($name)) {
  241. $relation->name = $name;
  242. }
  243. $relation->roleId = $roleId;
  244. $relation->remind = $remind;
  245. $relation->status = $status;
  246. $relation->super = $super;
  247. $relation->finance = $finance;
  248. $relation->switchShop = $switchShop;
  249. $relation->save();
  250. util::complete('修改成功');
  251. }
  252. //删除员工关系 ssh 2021.1.14
  253. public function actionDelete()
  254. {
  255. $id = Yii::$app->request->get('id');
  256. $shopAdmin = $this->shopAdmin;
  257. if (isset($shopAdmin->super) == false || $shopAdmin->super != 1) {
  258. util::fail('没有权限');
  259. }
  260. $connection = Yii::$app->db;
  261. $transaction = $connection->beginTransaction();
  262. try {
  263. $relation = ShopAdminClass::getById($id, true);
  264. \biz\shop\classes\ShopAdminClass::deleteShopAdmin($relation, $this->shopAdminId, $this->mainId);
  265. $transaction->commit();
  266. util::complete();
  267. } catch (\Exception $exception) {
  268. $transaction->rollBack();
  269. Yii::info("失败原因:" . $exception->getMessage());
  270. util::fail('删除失败');
  271. }
  272. }
  273. }