ShopAdminController.php 8.0 KB

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