ShopAdminController.php 8.9 KB

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