ShopAdminController.php 10 KB

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