Base.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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\exception\HttpResponseException;use think\facade\Db;
  7. use app\admin\model\DataGroup as DataGroupModel;
  8. use think\facade\Validate;use think\Response;
  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. public $white = ['login','child_out_list','salestock'];
  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)&&!in_array("*", $this->white)) $this->validateToken();
  22. }
  23. public function validateToken()
  24. {
  25. //这个地方不能用return json_show()方法,无法阻止代码继续执行
  26. $token = isset($this->post['token']) ? trim($this->post['token']) : "";
  27. $companyNo = isset($this->post['relaComNo']) ? trim($this->post['relaComNo']) : "";
  28. if($token==""){
  29. return error_show(101,"参数token不能为空");
  30. }
  31. $effetc = VerifyTokens($token);
  32. if(!empty($effetc) && $effetc['code']!=0) {
  33. return error_show($effetc['code'], $effetc['message']);
  34. }
  35. request()->uid = $this->uid=$effetc['data']['id']??"";//request()->uid主要是为了部分未继承Base控制器的方法使用,例如Model/OrderOutChild::BratchChild
  36. request()->uname = $this->uname=$effetc['data']['nickname']??"";
  37. request()->level = $this->level = $effetc['data']['level'];//request()->level主要是为了部分未继承Base控制器的方法使用,例如Model/ProcessOrder::adddProcess
  38. $where=[];
  39. if($companyNo!=""){
  40. $where=["companyNo"=>$companyNo];
  41. }
  42. if ($effetc['data']['level'] == 2) {
  43. $user_role = Db::name("user_role")
  44. ->field('id,roleid')
  45. ->where(["uid" => $this->uid, "is_del" => 0])
  46. ->where($where)
  47. ->findOrEmpty();
  48. if (empty($user_role)) return error_show(101, "没有该公司的默认角色");
  49. $this->roleid=$user_role['roleid'];
  50. }else $this->roleid=1;
  51. }
  52. public function checkRole()
  53. {
  54. if($this->level==1){
  55. return [
  56. 'action_conllect'=>'',
  57. 'write'=>[],
  58. 'platform'=>[],
  59. 'roleid'=>$this->roleid
  60. ];
  61. }else {
  62. $uid = Db::name("user_role")->where(['uid' => $this->uid, "is_del" => 0, "status" => 1])->find();
  63. if ($uid == false) {
  64. return [];
  65. }
  66. $action = [];
  67. $this->roleid = $uid['roleid'];
  68. $role = Db::name("role_action")->where(["role_id" => $uid['roleid'], "status" => 1])->json(["action_conllect"])->find();
  69. $action['action_conllect'] = isset($role['action_conllect']) ? implode(",", $role['action_conllect']) :"";
  70. $action['write'][] = $this->uid;
  71. $group = [];
  72. $group = Db::name("role_group")->where("FIND_IN_SET($this->uid,group_user) > 0")->column("id");
  73. $where = "to_user=$this->uid";
  74. if (!empty($group)) {
  75. $where .= " or to_group in (" . implode(",", $group) . ")";
  76. }
  77. $paltform = Db::name("user_platform")->where(["uid" => $this->uid, "is_del" => 0])->find();
  78. $action['platform'] = $paltform != false ? explode(",", $paltform['platform']) : [];
  79. $share = Db::name("role_share")->where(["is_del" => 0, "status" => 1])->where($where)->select();
  80. if (!empty($share)) {
  81. foreach ($share as $value) {
  82. if ($value['action'] == 1) {
  83. if ($value['share_user'] != "") {
  84. $action['write'][] = $value['share_user'];
  85. $user = Db::name("user_role")->where(['uid' => $value['share_user'], "is_del" => 0, "status" => 1])->find();
  86. if ($user != false) {
  87. $role = Db::name("role_action")->where(["role_id" => $user['roleid'], "status" => 1])->find();
  88. if ($role != false) {
  89. $conlect = Db::name("action")->where(['menuid' => explode(",", $value['action_collect'])])->column("id");
  90. $shar = explode(",", $value['action_collect']);
  91. $wish = array_intersect($conlect, $shar);
  92. $action['action_conllect'] .= empty($wish) ? "" : "," . implode(",", $wish);
  93. }
  94. }
  95. }
  96. }
  97. if ($value['action'] === 0) {
  98. if ($value['share_user'] != "") $action['write'][] = $value['share_user'];
  99. if ($value['action_collect'] != '') {
  100. $act = Db::name("action")->where(['menuid' => explode(",", $value['action_collect']), "action_code" => '001'])->column("id");
  101. $action['action_conllect'] .= empty($act) ? "" : "," . implode(",", $act);
  102. }
  103. }
  104. if ($value['action'] === '') {
  105. if ($value['share_user'] != "") $action['write'][] = $value['share_user'];
  106. if ($value['action_collect'] != '') {
  107. $act = Db::name("action")->where(['menuid' => explode(",", $value['action_collect']), "action_code" => '001'])->column("id");
  108. $action['action_conllect'] .= empty($act) ? "" : "," . implode(",", $act);
  109. }
  110. }
  111. }
  112. }
  113. //离职交接权限
  114. $resgin = Db::name("resign_info")->where([["hand_uid", "=", $this->uid], ["is_del", "=", 0], ['status', "=", 4]])
  115. ->column(['resign_uid']);
  116. if (!empty($resgin)) {
  117. $action['write'] = array_unique(array_merge($action['write'], $resgin));
  118. }
  119. // 超级管理员看到所有人的数据
  120. // if ($uid['roleid'] == 1 || $uid['roleid'] == 33) {
  121. // $action['write'] = [];
  122. // }
  123. $action['roleid'] = $uid['roleid'];
  124. $action['action_conllect'] = implode(",", array_unique(explode(",", $action['action_conllect'])));
  125. return $action;
  126. }
  127. }
  128. //用户数据权限检查
  129. protected function checkDataShare()
  130. {
  131. if ($this->roleid == 1 || $this->roleid == 33) return [];// 超级管理员(roleid==1)和最高领导(roleid==33)能看到所有人的数据
  132. $data = get_group_share_uid($this->uid,$this->level);
  133. /**
  134. * data[DataGroupModel::$type_全部] 0全部
  135. * data[DataGroupModel::$type_可编辑] 1可编辑
  136. * data[DataGroupModel::$type_只读] 2只读
  137. */
  138. return $data;
  139. }
  140. /**
  141. * @param $good
  142. * @param $isZx
  143. * @param string $thNo
  144. * @return mixed|string
  145. * @throws \think\Exception
  146. */
  147. public function CheckGoodZx($good, $isZx, $thNo = '')
  148. {
  149. $spuCode = makeNo("SKU");
  150. //查询是否存在转库存品记录 存在则返回库存编号
  151. $newCode = Db::name("good_change_log")->where(["oldCode" => $good['spuCode']])->value("newCode", '');
  152. if ($newCode == '') {
  153. $log = ["oldCode" => $good['spuCode'],
  154. "order_source" => $isZx,
  155. "newCode" => $spuCode,
  156. "thCode" => $thNo,
  157. "createrid" => $this->uid,
  158. "creater" => $this->uname,
  159. "addtime" => date("Y-m-d H:i:s")
  160. ];
  161. } else {
  162. return $newCode;
  163. }
  164. if ($isZx == 1) {
  165. // $supplier = Db::name("supplier")->where(["code" => $good['supplierNo']])->findOrEmpty();
  166. //
  167. // if (empty($supplier)) throw new \Exception('非库存商品转库存失败');
  168. $userCommon= \app\admin\common\User::getIns();
  169. $supplier_temp =$userCommon->handle("sInfo",["code"=>$good['supplierNo']]);
  170. if ($supplier_temp['code']!=0 || empty($supplier_temp['data'])) throw new \Exception('查不到该供应商的记录');
  171. $personid = $supplier_temp['data']['personid']??0;
  172. $person_name = $supplier_temp['data']['person']??'';
  173. $good = [
  174. 'spuCode' => $spuCode,
  175. 'good_code' => $good['good_code'],
  176. 'good_name' => $good['good_name'],
  177. 'cat_id' => $good['cat_id'],
  178. 'brand_id' => $good['brand_id'],
  179. 'companyNo' => $good['companyNo'],
  180. 'good_unit' => $good['good_unit'],
  181. 'good_type' => $good['good_type'],
  182. 'moq' => $good['moq'],
  183. 'customized' => $good['customized'],
  184. 'tax' => $good['tax'],
  185. 'supplierNo' => $good['supplierNo'],
  186. 'is_auth' => $good['is_auth'],
  187. 'auth_img' => $good['auth_img'],
  188. 'after_sales' => '',
  189. 'craft_desc' => $good['craft_desc'],
  190. 'good_remark' => $good['good_remark'],
  191. 'weight' => $good['weight'],
  192. 'packing_way' => '',
  193. 'packing_size' => '',
  194. 'packing_spec' => '',
  195. 'packing_list' => '',
  196. 'packing_weight' => '',
  197. 'good_bar' => '',
  198. 'supply_area' => $good['supply_area'],
  199. 'delivery_place' => '',
  200. 'origin_place' => '',
  201. 'delivery_day' => '',
  202. 'lead_time' => '',
  203. 'sample_day' => '',
  204. 'sample_fee' => '',
  205. 'good_img' => '',//$good['good_img'],
  206. 'good_thumb_img' => $good['good_thumb_img'],
  207. 'good_info_img' => $good['good_img'],//商品主图对应到商品详情
  208. 'cert_fee' => '',
  209. 'packing_fee' => '',
  210. 'cost_fee' => '',
  211. 'mark_fee' => '',
  212. 'demo_fee' => '',
  213. 'open_fee' => '',
  214. 'noble_metal' => $good['noble_metal'],
  215. 'noble_weight' => $good['good_weight'],
  216. 'is_gold_price' => $good['is_gold_price'],
  217. 'cgd_gold_price' => '',
  218. 'market_price' => '',
  219. 'nake_price' => '',
  220. 'is_step' => '',
  221. 'is_online' => '0',
  222. 'is_stock' => '1',
  223. 'status' => 7,//咨询商品待编辑
  224. 'createrid' => $good['createrid'],
  225. 'creater' => $good['creater'],
  226. 'field_change' => '',
  227. 'is_del' => 0,
  228. 'is_support_stock' => 0,
  229. 'addtime' => date('Y-m-d H:i:s'),
  230. 'updatetime' => date('Y-m-d H:i:s'),
  231. 'is_diff' => $good['is_diff'],
  232. 'config' => $good['config'],
  233. 'other_config' => $good['other_config'],
  234. 'chargerid' => $personid ?? $good['createrid'],
  235. 'charger' => $person_name ?? $good['creater'],//负责人跟创建人一致
  236. ];
  237. } else {
  238. $good['id'] = null;
  239. $good['spuCode'] = $spuCode;
  240. $good['is_stock'] = 1;
  241. $good['is_support_stock'] = 0;
  242. $good['status'] = 7;
  243. $good['addtime'] = date("Y-m-d H:i:s");
  244. $good['updatetime'] = date("Y-m-d H:i:s");
  245. }
  246. $insert = Db::name("good_basic")->insert($good, true);
  247. if ($insert == false) throw new \Exception("非库存商品转库存失败", 1005);
  248. //修改状态,添加待办
  249. ActionLog::logAdd($this->post['token'], [
  250. "order_code" => $newCode,//咨询单详情编号
  251. "status" => 0,//这里的status是之前的值
  252. "action_remark" => '非库存品退货转库存',//备注
  253. "action_type" => "create"//新建create,编辑edit,更改状态status
  254. ], "SPCB", 7, $good);
  255. ProcessOrder::AddProcess($this->post['token'], [
  256. "order_type" => 'SPCB',
  257. "order_code" => $newCode,//咨询单详情编号
  258. "order_id" => $insert,
  259. "order_status" => 7,
  260. "before_status" => 0,
  261. 'holder_id' => $good['createrid'],
  262. ]);
  263. $newAdd = Db::name("good_change_log")->insert($log);
  264. if ($newAdd == false) throw new \Exception("非库存商品转库存失败", 1005);
  265. return $spuCode;
  266. }
  267. /**
  268. * @param string $message
  269. * @param int $code
  270. * @param null $data
  271. */
  272. public function error($message='',$code=1003,$data=null){
  273. $this->result($message,$data,$code);
  274. }
  275. /**
  276. * @param string $msg
  277. * @param null $data
  278. * @param int $code
  279. * @param string|null $type
  280. * @param array $header
  281. * @param array $options
  282. */
  283. private function result(string $msg, $data = null, int $code = 0, string $type = 'json', array $header = [], array
  284. $options = [])
  285. {
  286. $result = [
  287. 'code' => $code,
  288. 'message' => $msg,
  289. 'data' => $data,
  290. ];
  291. $code = 200;
  292. if (isset($header['statuscode'])) {
  293. $code = $header['statuscode'];
  294. unset($header['statuscode']);
  295. }
  296. $response = Response::create($result, $type, $code)->header($header)->options($options);
  297. throw new HttpResponseException($response);
  298. }
  299. /**
  300. * @param string $message
  301. * @param int $code
  302. * @param null $data
  303. */
  304. public function success($message='',$data=null,$code=0){
  305. $this->result($message,$data,$code);
  306. }
  307. }