Base.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\model\ActionLog;
  4. use app\admin\model\ProcessOrder;
  5. use think\App;
  6. use think\facade\Db;
  7. use app\admin\model\DataGroup as DataGroupModel;
  8. use think\facade\Validate;
  9. class Base extends \app\BaseController
  10. {
  11. public $post = [];
  12. public $uid = 0;//账户id
  13. public $uname = '';//账户名称
  14. public $roleid = 0;//角色id
  15. public $level = 0;//角色等级,1.系统超级管理员,2.企业管理员,3.企业人员
  16. private $white = ['login'];
  17. public function __construct(App $app)
  18. {
  19. parent::__construct($app);
  20. $this->post = $this->request->post('',null,'trim');
  21. if (!in_array($this->request->pathinfo(), $this->white)) $this->validateToken();
  22. }
  23. public function validateToken()
  24. {
  25. //这个地方不能用return json_show()方法,无法阻止代码继续执行
  26. $val = Validate::rule([
  27. 'token' => 'require',
  28. // 'supplierNo'=>'length:18',
  29. ]);
  30. if (!$val->check($this->post)) return error_show(1004, $val->getError());
  31. // $token = isset($this->post['token']) ? trim($this->post['token']) : "";
  32. // if($token==""){
  33. // return error_show(101,'token不能为空');
  34. // }
  35. $effetc = VerifyTokens($this->post['token']);
  36. if (!empty($effetc) && $effetc['code'] != 0) return error_show($effetc['code'], $effetc['message']);
  37. if ((isset($this->post['supplierNo'])) && ($this->post['supplierNo'] != '') && !in_array($this->post['supplierNo'], array_column($effetc['data']['company_relaton'], 'companyCode'))) return error_show(1004, '尚未关联企业');
  38. $this->uid = isset($effetc['data']['id']) ? $effetc['data']['id'] : "";
  39. $this->uname = isset($effetc['data']['nickname']) ? $effetc['data']['nickname'] : "";
  40. $where_role = [['a.uid', '=', $this->uid], ['a.is_del', '=', 0], ['a.status', '=', 1]];
  41. if (isset($this->post['supplierNo']) && $this->post['supplierNo'] != '') {
  42. $where_role[] = ['a.companyNo', '=', $this->post['supplierNo']];
  43. $where_role[] = ['b.level', '<>', 1];
  44. } else $where_role[] = ['b.level', '=', 1];
  45. $role = Db::name('user_role')
  46. ->alias('a')
  47. ->field('a.id,a.status,a.roleid,b.role_name,b.level')
  48. ->leftJoin('role b', 'b.id=a.roleid')
  49. ->where($where_role)
  50. ->findOrEmpty();
  51. if (empty($role) || $role['status'] == 0) return error_show(102, '该企业尚未开通对应角色');
  52. $this->roleid = $role['roleid'];//权限id
  53. $this->level = $role['level'];//权限等级
  54. }
  55. public function checkRole()
  56. {
  57. $uid = Db::name("user_role")->where(['uid' => $this->uid, "is_del" => 0, "status" => 1])->find();
  58. if ($uid == false) {
  59. return [];
  60. }
  61. $action = [];
  62. $this->roleid = $uid['roleid'];
  63. $role = Db::name("role_action")->where(["role_id" => $uid['roleid'], "status" => 1])->find();
  64. $action['action_conllect'] = isset($role['action_conllect']) ? $role['action_conllect'] : '';
  65. $action['write'][] = $this->uid;
  66. $group = [];
  67. $group = Db::name("role_group")->where("FIND_IN_SET($this->uid,group_user) > 0")->column("id");
  68. $where = "to_user=$this->uid";
  69. if (!empty($group)) {
  70. $where .= " or to_group in (" . implode(",", $group) . ")";
  71. }
  72. $paltform = Db::name("user_platform")->where(["uid" => $this->uid, "is_del" => 0])->find();
  73. $action['platform'] = $paltform != false ? explode(",", $paltform['platform']) : [];
  74. $share = Db::name("role_share")->where(["is_del" => 0, "status" => 1])->where($where)->select();
  75. if (!empty($share)) {
  76. foreach ($share as $value) {
  77. if ($value['action'] == 1) {
  78. if ($value['share_user'] != "") {
  79. $action['write'][] = $value['share_user'];
  80. $user = Db::name("user_role")->where(['uid' => $value['share_user'], "is_del" => 0, "status" => 1])->find();
  81. if ($user != false) {
  82. $role = Db::name("role_action")->where(["role_id" => $user['roleid'], "status" => 1])->find();
  83. if ($role != false) {
  84. $conlect = Db::name("action")->where(['menuid' => explode(",", $value['action_collect'])])->column("id");
  85. $shar = explode(",", $value['action_collect']);
  86. $wish = array_intersect($conlect, $shar);
  87. $action['action_conllect'] .= empty($wish) ? "" : "," . implode(",", $wish);
  88. }
  89. }
  90. }
  91. }
  92. if ($value['action'] === 0) {
  93. if ($value['share_user'] != "") $action['write'][] = $value['share_user'];
  94. if ($value['action_collect'] != '') {
  95. $act = Db::name("action")->where(['menuid' => explode(",", $value['action_collect']), "action_code" => '001'])->column("id");
  96. $action['action_conllect'] .= empty($act) ? "" : "," . implode(",", $act);
  97. }
  98. }
  99. if ($value['action'] === '') {
  100. if ($value['share_user'] != "") $action['write'][] = $value['share_user'];
  101. if ($value['action_collect'] != '') {
  102. $act = Db::name("action")->where(['menuid' => explode(",", $value['action_collect']), "action_code" => '001'])->column("id");
  103. $action['action_conllect'] .= empty($act) ? "" : "," . implode(",", $act);
  104. }
  105. }
  106. }
  107. }
  108. //离职交接权限
  109. $resgin = Db::name("resign_info")->where([["hand_uid", "=", $this->uid], ["is_del", "=", 0], ['status', "=", 4]])
  110. ->column(['resign_uid']);
  111. if (!empty($resgin)) {
  112. $action['write'] = array_unique(array_merge($action['write'], $resgin));
  113. }
  114. // 超级管理员看到所有人的数据
  115. if ($uid['roleid'] == 1 || $uid['roleid'] == 33) {
  116. $action['write'] = [];
  117. }
  118. $action['roleid'] = $uid['roleid'];
  119. $action['action_conllect'] = implode(",", array_unique(explode(",", $action['action_conllect'])));
  120. return $action;
  121. }
  122. //用户数据权限检查
  123. protected function checkDataShare()
  124. {
  125. if ($this->roleid == 1 || $this->roleid == 33) return [];// 超级管理员(roleid==1)和最高领导(roleid==33)能看到所有人的数据
  126. $data = get_group_share_uid($this->uid);
  127. /**
  128. * data[DataGroupModel::$type_全部] 0全部
  129. * data[DataGroupModel::$type_可编辑] 1可编辑
  130. * data[DataGroupModel::$type_只读] 2只读
  131. */
  132. return $data;
  133. }
  134. /**
  135. * @param $good
  136. * @param $isZx
  137. * @param string $thNo
  138. * @return mixed|string
  139. * @throws \think\Exception
  140. */
  141. public function CheckGoodZx($good, $isZx, $thNo = '')
  142. {
  143. $spuCode = makeNo("SKU");
  144. //查询是否存在转库存品记录 存在则返回库存编号
  145. $newCode = Db::name("good_change_log")->where(["oldCode" => $good['spuCode']])->value("newCode", '');
  146. if ($newCode == '') {
  147. $log = ["oldCode" => $good['spuCode'],
  148. "order_source" => $isZx,
  149. "newCode" => $spuCode,
  150. "thCode" => $thNo,
  151. "createrid" => $this->uid,
  152. "creater" => $this->uname,
  153. "addtime" => date("Y-m-d H:i:s")
  154. ];
  155. } else {
  156. return $newCode;
  157. }
  158. if ($isZx == 1) {
  159. $supplier = Db::name("supplier")->where(["code" => $good['supplierNo']])->findOrEmpty();
  160. if (empty($supplier)) throw new \Exception('非库存商品转库存失败');
  161. $good = [
  162. 'spuCode' => $spuCode,
  163. 'good_code' => $good['good_code'],
  164. 'good_name' => $good['good_name'],
  165. 'cat_id' => $good['cat_id'],
  166. 'brand_id' => $good['brand_id'],
  167. 'companyNo' => $good['companyNo'],
  168. 'good_unit' => $good['good_unit'],
  169. 'good_type' => $good['good_type'],
  170. 'moq' => $good['moq'],
  171. 'customized' => $good['customized'],
  172. 'tax' => $good['tax'],
  173. 'supplierNo' => $good['supplierNo'],
  174. 'is_auth' => $good['is_auth'],
  175. 'auth_img' => $good['auth_img'],
  176. 'after_sales' => '',
  177. 'craft_desc' => $good['craft_desc'],
  178. 'good_remark' => $good['good_remark'],
  179. 'weight' => $good['weight'],
  180. 'packing_way' => '',
  181. 'packing_size' => '',
  182. 'packing_spec' => '',
  183. 'packing_list' => '',
  184. 'packing_weight' => '',
  185. 'good_bar' => '',
  186. 'supply_area' => $good['supply_area'],
  187. 'delivery_place' => '',
  188. 'origin_place' => '',
  189. 'delivery_day' => '',
  190. 'lead_time' => '',
  191. 'sample_day' => '',
  192. 'sample_fee' => '',
  193. 'good_img' => '',//$good['good_img'],
  194. 'good_thumb_img' => $good['good_thumb_img'],
  195. 'good_info_img' => $good['good_img'],//商品主图对应到商品详情
  196. 'cert_fee' => '',
  197. 'packing_fee' => '',
  198. 'cost_fee' => '',
  199. 'mark_fee' => '',
  200. 'demo_fee' => '',
  201. 'open_fee' => '',
  202. 'noble_metal' => $good['noble_metal'],
  203. 'noble_weight' => $good['good_weight'],
  204. 'is_gold_price' => $good['is_gold_price'],
  205. 'cgd_gold_price' => '',
  206. 'market_price' => '',
  207. 'nake_price' => '',
  208. 'is_step' => '',
  209. 'is_online' => '0',
  210. 'is_stock' => '1',
  211. 'status' => 7,//咨询商品待编辑
  212. 'createrid' => $good['createrid'],
  213. 'creater' => $good['creater'],
  214. 'field_change' => '',
  215. 'is_del' => 0,
  216. 'is_support_stock' => 0,
  217. 'addtime' => date('Y-m-d H:i:s'),
  218. 'updatetime' => date('Y-m-d H:i:s'),
  219. 'is_diff' => $good['is_diff'],
  220. 'config' => $good['config'],
  221. 'other_config' => $good['other_config'],
  222. 'chargerid' => $supplier['personid'] ?? $good['createrid'],
  223. 'charger' => $supplier['person'] ?? $good['creater'],//负责人跟创建人一致
  224. ];
  225. } else {
  226. $good['id'] = null;
  227. $good['spuCode'] = $spuCode;
  228. $good['is_stock'] = 1;
  229. $good['is_support_stock'] = 0;
  230. $good['status'] = 7;
  231. $good['addtime'] = date("Y-m-d H:i:s");
  232. $good['updatetime'] = date("Y-m-d H:i:s");
  233. }
  234. $insert = Db::name("good_basic")->insert($good, true);
  235. if ($insert == false) throw new \Exception("非库存商品转库存失败", 1005);
  236. //修改状态,添加待办
  237. ActionLog::logAdd($this->post['token'], [
  238. "order_code" => $newCode,//咨询单详情编号
  239. "status" => 0,//这里的status是之前的值
  240. "action_remark" => '非库存品退货转库存',//备注
  241. "action_type" => "create"//新建create,编辑edit,更改状态status
  242. ], "SPCB", 7, $good);
  243. ProcessOrder::AddProcess($this->post['token'], [
  244. "order_type" => 'SPCB',
  245. "order_code" => $newCode,//咨询单详情编号
  246. "order_id" => $insert,
  247. "order_status" => 7,
  248. "before_status" => 0,
  249. 'holder_id' => $good['createrid'],
  250. ]);
  251. $newAdd = Db::name("good_change_log")->insert($log);
  252. if ($newAdd == false) throw new \Exception("非库存商品转库存失败", 1005);
  253. return $spuCode;
  254. }
  255. }