Index.php 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547
  1. <?php
  2. namespace app\index\controller;
  3. use app\common\controller\Frontend;
  4. use think\Cache;
  5. use think\captcha\Captcha;
  6. use think\Config;
  7. use think\Cookie;
  8. use think\Db;
  9. use think\Session;
  10. use think\Validate;
  11. /**
  12. * 手机端:外发明细(purchase_order_detail)验证码 / 账号密码登录 + 列表
  13. * 普通用户:customer 表(手机号验证码 或 登录账号+密码);管理员:admin 表账号密码(看全部、仅查看)
  14. */
  15. class Index extends Frontend
  16. {
  17. protected $noNeedLogin = ['*'];
  18. protected $noNeedRight = ['*'];
  19. protected $layout = '';
  20. /** @var int 登录态有效天数 */
  21. protected $mprocTtlSeconds = 0;
  22. /** @var array<string, string>|null purchase_order_detail 表字段:小写 => 真实列名 */
  23. protected static $mprocProcuremenColumns = null;
  24. public function _initialize()
  25. {
  26. parent::_initialize();
  27. if (is_file(APP_PATH . 'extra/mproc.php')) {
  28. Config::load(APP_PATH . 'extra/mproc.php', 'mproc');
  29. }
  30. $days = (int)(Config::get('mproc.session_days') ?: 7);
  31. $days = max(1, min(30, $days));
  32. $this->mprocTtlSeconds = $days * 86400;
  33. }
  34. /**
  35. * 当前手机端登录用户;未登录返回 null
  36. */
  37. protected function mprocGetUser()
  38. {
  39. $token = Session::get('mproc_token');
  40. if ($token === null || $token === '') {
  41. $token = Cookie::get('mproc_token');
  42. }
  43. if ($token === null || $token === '') {
  44. return null;
  45. }
  46. $token = preg_replace('/[^a-f0-9]/i', '', (string)$token);
  47. if (strlen($token) < 16) {
  48. return null;
  49. }
  50. $user = Cache::get('mproc_u_' . $token);
  51. if (!is_array($user)) {
  52. return null;
  53. }
  54. // 手机登录必有 phone;账号密码登录必有 username
  55. if (empty($user['phone']) && empty($user['account']) && empty($user['username'])) {
  56. return null;
  57. }
  58. if (time() - (int)($user['login_time'] ?? 0) > $this->mprocTtlSeconds) {
  59. $this->mprocClearLogin($token);
  60. return null;
  61. }
  62. $user['token'] = $token;
  63. return $user;
  64. }
  65. protected function mprocClearLogin($token)
  66. {
  67. if ($token !== null && $token !== '') {
  68. Cache::rm('mproc_u_' . $token);
  69. }
  70. Session::delete('mproc_token');
  71. Cookie::delete('mproc_token');
  72. }
  73. /**
  74. * 登录成功后的回跳地址校验(仅允许本站「外发明细订单页」路径,防止开放重定向)
  75. *
  76. * @param string $raw GET/POST 的 redirect 或当前 REQUEST_URI
  77. */
  78. protected function mprocSanitizeRedirectUrl($raw)
  79. {
  80. $s = str_replace(["\r", "\n", "\0"], '', trim((string)$raw));
  81. if ($s === '') {
  82. return '';
  83. }
  84. if (preg_match('#^https?://#i', $s)) {
  85. $h = parse_url($s, PHP_URL_HOST);
  86. if (!is_string($h) || strcasecmp($h, (string)$this->request->host()) !== 0) {
  87. return '';
  88. }
  89. $path = parse_url($s, PHP_URL_PATH);
  90. $query = parse_url($s, PHP_URL_QUERY);
  91. $s = (is_string($path) && $path !== '' ? $path : '/');
  92. if (is_string($query) && $query !== '') {
  93. $s .= '?' . $query;
  94. }
  95. }
  96. if (strpos($s, '://') !== false) {
  97. return '';
  98. }
  99. if ($s === '' || ($s[0] !== '/' && stripos($s, 'index.php') !== 0)) {
  100. return '';
  101. }
  102. if ($s[0] !== '/') {
  103. $s = '/' . ltrim($s, '/');
  104. }
  105. if (strpos($s, '//') === 0) {
  106. return '';
  107. }
  108. if (stripos($s, 'index/index/index') === false) {
  109. return '';
  110. }
  111. if (stripos($s, 'index/index/login') !== false) {
  112. return '';
  113. }
  114. return $s;
  115. }
  116. /**
  117. * 手机端登录页 URL。注意:勿用 url('...login', ['redirect'=>]),在 url_html_suffix 下会把参数拼进 PATHINFO 导致 404。
  118. *
  119. * @param string $redirectPath 已通过 {@see mprocSanitizeRedirectUrl} 的回跳路径(含 query),空则不带参数
  120. */
  121. protected function mprocBuildLoginUrl($redirectPath = '')
  122. {
  123. $root = rtrim($this->request->root(), '/');
  124. $path = '/index/index/login';
  125. $rp = trim((string)$redirectPath);
  126. if ($rp === '') {
  127. return $root . $path;
  128. }
  129. return $root . $path . '?' . http_build_query(['redirect' => $rp], '', '&', PHP_QUERY_RFC3986);
  130. }
  131. /**
  132. * 登录成功后跳转到订单首页:用当前入口 {@see Request::root} 拼 URL,并从原 redirect 中只保留白名单 query(避免子目录部署丢参、开放重定向)
  133. *
  134. * @param string $redirectPathOrUrl 已通过 {@see mprocSanitizeRedirectUrl} 的路径或完整 URL(可含 ?focus_eid=)
  135. */
  136. protected function mprocBuildAfterLoginIndexUrl($redirectPathOrUrl)
  137. {
  138. $raw = trim((string)$redirectPathOrUrl);
  139. if ($raw === '') {
  140. return url('index/index/index', '', '', true);
  141. }
  142. $queryStr = '';
  143. if (preg_match('#^https?://#i', $raw)) {
  144. $pq = parse_url($raw);
  145. $queryStr = (is_array($pq) && !empty($pq['query']) && is_string($pq['query'])) ? $pq['query'] : '';
  146. } elseif (isset($raw[0]) && $raw[0] === '/') {
  147. $pq = parse_url('http://127.0.0.1' . $raw);
  148. $queryStr = (is_array($pq) && !empty($pq['query']) && is_string($pq['query'])) ? $pq['query'] : '';
  149. } else {
  150. $pq = parse_url('http://127.0.0.1/' . ltrim($raw, '/'));
  151. $queryStr = (is_array($pq) && !empty($pq['query']) && is_string($pq['query'])) ? $pq['query'] : '';
  152. }
  153. $q = [];
  154. if ($queryStr !== '') {
  155. parse_str($queryStr, $q);
  156. if (!is_array($q)) {
  157. $q = [];
  158. }
  159. }
  160. $allowed = [];
  161. if (isset($q['focus_eid'])) {
  162. $fe = (int)$q['focus_eid'];
  163. if ($fe > 0) {
  164. $allowed['focus_eid'] = $fe;
  165. }
  166. }
  167. $mt = isset($q['main_tab']) ? trim((string)$q['main_tab']) : '';
  168. if ($mt === 'me' || $mt === 'orders') {
  169. $allowed['main_tab'] = $mt;
  170. }
  171. $tb = isset($q['tab']) ? trim((string)$q['tab']) : '';
  172. if (in_array($tb, ['draft', 'submitted', 'done', 'me'], true)) {
  173. $allowed['tab'] = $tb;
  174. }
  175. if (isset($q['q']) && trim((string)$q['q']) !== '') {
  176. $allowed['q'] = substr(trim((string)$q['q']), 0, 120);
  177. }
  178. if (isset($allowed['focus_eid']) && !isset($allowed['main_tab'])) {
  179. $allowed['main_tab'] = 'orders';
  180. }
  181. $base = rtrim($this->request->root(true), '/');
  182. $path = '/index/index/index';
  183. $qs = $allowed !== [] ? ('?' . http_build_query($allowed, '', '&', PHP_QUERY_RFC3986)) : '';
  184. return $base . $path . $qs;
  185. }
  186. /**
  187. * 从 purchase_order_detail 表解析真实列名(SHOW COLUMNS 只查一次,按候选小写名匹配第一条)
  188. *
  189. * @param string[] $candidatesLower 如 ['status','istatus']
  190. * @return string|null
  191. */
  192. protected function mprocResolveProcuremenColumn(array $candidatesLower)
  193. {
  194. if (self::$mprocProcuremenColumns === null) {
  195. self::$mprocProcuremenColumns = [];
  196. try {
  197. $rows = Db::query('SHOW COLUMNS FROM `purchase_order_detail`');
  198. if (is_array($rows)) {
  199. foreach ($rows as $c) {
  200. $name = isset($c['Field']) ? (string)$c['Field'] : '';
  201. if ($name !== '') {
  202. self::$mprocProcuremenColumns[strtolower($name)] = $name;
  203. }
  204. }
  205. }
  206. } catch (\Throwable $e) {
  207. self::$mprocProcuremenColumns = [];
  208. }
  209. }
  210. foreach ($candidatesLower as $low) {
  211. $k = strtolower((string)$low);
  212. if (isset(self::$mprocProcuremenColumns[$k])) {
  213. return self::$mprocProcuremenColumns[$k];
  214. }
  215. }
  216. return null;
  217. }
  218. /**
  219. * 列表:非管理员按 company_name 与登录时解析的单位名一致;管理员不加条件
  220. *
  221. * @return array 可直接 $query->where($arr),空数组表示不加条件
  222. */
  223. protected function mprocListWhereForLoginUser(array $user)
  224. {
  225. if (!empty($user['is_admin'])) {
  226. return [];
  227. }
  228. $cCol = $this->mprocResolveProcuremenColumn(['company_name']);
  229. if ($cCol === null || $cCol === '') {
  230. return ['id' => 0];
  231. }
  232. $cn = trim((string)($user['company_name'] ?? ''));
  233. if ($cn === '') {
  234. $phone = trim((string)($user['phone'] ?? ''));
  235. if ($phone !== '') {
  236. $cn = $this->mprocResolveCompanyForLoginPhone($phone);
  237. }
  238. }
  239. if ($cn === '') {
  240. return ['id' => 0];
  241. }
  242. return [$cCol => $cn];
  243. }
  244. /**
  245. * customer 是否允许登录(status:1 / 正常;空视为可登录以兼容旧数据)
  246. */
  247. protected function mprocCustomerUserActive(array $row): bool
  248. {
  249. $st = $row['status'] ?? '';
  250. if ($st === '' || $st === null) {
  251. return true;
  252. }
  253. return $st === 1 || $st === '1';
  254. }
  255. /**
  256. * @return array<string, mixed>|null
  257. */
  258. protected function mprocFindCustomerUserByMobile(string $phone): ?array
  259. {
  260. return $this->mprocFindCustomerRowByPhone($phone);
  261. }
  262. /**
  263. * 按登录账号(account)查找 customer;兼容旧会话把 11 位手机号写在 username 里
  264. *
  265. * @return array<string, mixed>|null
  266. */
  267. protected function mprocFindCustomerUserByUsername(string $username): ?array
  268. {
  269. $username = trim($username);
  270. if ($username === '') {
  271. return null;
  272. }
  273. try {
  274. $row = Db::table('customer')->where('account', $username)->order('id', 'asc')->find();
  275. } catch (\Throwable $e) {
  276. $row = null;
  277. }
  278. if ((!is_array($row) || $row === []) && preg_match('/^1\d{10}$/', $username)) {
  279. $row = $this->mprocFindCustomerRowByPhone($username);
  280. }
  281. if (!is_array($row) || $row === [] || !$this->mprocCustomerUserActive($row)) {
  282. return null;
  283. }
  284. return $row;
  285. }
  286. /**
  287. * customer 密码校验(md5(md5) 无 salt;兼容 bcrypt)
  288. */
  289. protected function mprocVerifyCustomerUserPassword(array $row, string $password): bool
  290. {
  291. $stored = (string)($row['password'] ?? '');
  292. if ($stored === '' || $password === '') {
  293. return false;
  294. }
  295. if (preg_match('/^\$2[ayb]\$/', $stored)) {
  296. return password_verify($password, $stored);
  297. }
  298. return hash_equals($stored, md5(md5($password)));
  299. }
  300. /**
  301. * 生成 customer 登录密码密文
  302. */
  303. protected function mprocHashCustomerUserPassword(string $password, string $existingSalt = ''): string
  304. {
  305. unset($existingSalt);
  306. return md5(md5($password));
  307. }
  308. /**
  309. * @param array<string, mixed> $cu
  310. * @return array<string, mixed>
  311. */
  312. protected function mprocLoginPayloadFromCustomer(array $cu, string $loginType): array
  313. {
  314. $phone = trim((string)($cu['phone'] ?? ''));
  315. $account = trim((string)($cu['account'] ?? ''));
  316. if ($phone === '' && preg_match('/^1\d{10}$/', $account)) {
  317. $phone = $account;
  318. }
  319. if ($account === '' && preg_match('/^1\d{10}$/', $phone)) {
  320. $account = $phone;
  321. }
  322. $companyName = trim((string)($cu['company_name'] ?? ''));
  323. if ($companyName === '' && $phone !== '') {
  324. $companyName = $this->mprocResolveCompanyForLoginPhone($phone);
  325. }
  326. $id = (int)($cu['id'] ?? 0);
  327. return [
  328. 'phone' => $phone,
  329. 'account' => $account,
  330. 'company_name' => $companyName,
  331. 'username' => trim((string)($cu['username'] ?? '')),
  332. 'customer_id' => $id,
  333. 'customer_user_id' => $id,
  334. 'login_type' => $loginType,
  335. 'is_admin' => 0,
  336. ];
  337. }
  338. /**
  339. * 写入手机端登录态并返回跳转 URL
  340. *
  341. * @param array<string, mixed> $userData
  342. */
  343. protected function mprocFinishLogin(array $userData): void
  344. {
  345. $old = Session::get('mproc_token');
  346. if ($old) {
  347. Cache::rm('mproc_u_' . preg_replace('/[^a-f0-9]/i', '', (string)$old));
  348. }
  349. $token = bin2hex(random_bytes(16));
  350. $userData['login_time'] = time();
  351. Cache::set('mproc_u_' . $token, $userData, $this->mprocTtlSeconds + 86400);
  352. Session::set('mproc_token', $token);
  353. Cookie::set('mproc_token', $token, $this->mprocTtlSeconds);
  354. $postR = $this->mprocSanitizeRedirectUrl($this->request->post('redirect', ''));
  355. $sessR = $this->mprocSanitizeRedirectUrl((string)Session::get('mproc_intended_url', ''));
  356. Session::delete('mproc_intended_url');
  357. $raw = $postR !== '' ? $postR : $sessR;
  358. $jump = $this->mprocBuildAfterLoginIndexUrl($raw);
  359. $this->success('登录成功', $jump);
  360. }
  361. /**
  362. * 登录手机号对应的外协单位名称:customer 表;否则 purchase_order_detail
  363. */
  364. protected function mprocResolveCompanyForLoginPhone(string $phone): string
  365. {
  366. $phone = trim($phone);
  367. if ($phone === '' || !preg_match('/^1\d{10}$/', $phone)) {
  368. return '';
  369. }
  370. $cust = $this->mprocFindCustomerRowByPhone($phone);
  371. if (is_array($cust) && $cust !== []) {
  372. $co = $this->mprocCustomerPickField($cust, ['company_name', 'name']);
  373. if ($co !== '') {
  374. return $co;
  375. }
  376. }
  377. try {
  378. $one = Db::table('purchase_order_detail')
  379. ->where('phone', $phone)
  380. ->order('id', 'desc')
  381. ->find();
  382. if (is_array($one)) {
  383. $n = trim((string)($one['company_name'] ?? ''));
  384. if ($n !== '') {
  385. return $n;
  386. }
  387. }
  388. } catch (\Throwable $e) {
  389. }
  390. return '';
  391. }
  392. /**
  393. * 按手机号匹配 customer(phone 或 account 单值相等)
  394. *
  395. * @return array<string, mixed>|null
  396. */
  397. protected function mprocFindCustomerRowByPhone(string $phone): ?array
  398. {
  399. $phone = trim($phone);
  400. if ($phone === '' || !preg_match('/^1\d{10}$/', $phone)) {
  401. return null;
  402. }
  403. try {
  404. $row = Db::table('customer')
  405. ->where(function ($q) use ($phone) {
  406. $q->where('phone', $phone)->whereOr('account', $phone);
  407. })
  408. ->order('id', 'asc')
  409. ->find();
  410. } catch (\Throwable $e) {
  411. return null;
  412. }
  413. if (!is_array($row) || $row === [] || !$this->mprocCustomerUserActive($row)) {
  414. return null;
  415. }
  416. return $row;
  417. }
  418. /**
  419. * 管理员表 fa_admin.mobile 与当前手机号一致且未禁用(用于手机验证码管理员通道)
  420. *
  421. * @return array<string, mixed>|null
  422. */
  423. protected function mprocAdminRowByMobile(string $phone): ?array
  424. {
  425. $phone = trim($phone);
  426. if ($phone === '' || !preg_match('/^1\d{10}$/', $phone)) {
  427. return null;
  428. }
  429. try {
  430. $row = Db::name('admin')
  431. ->where('mobile', $phone)
  432. ->where('status', '<>', 'hidden')
  433. ->order('id', 'asc')
  434. ->find();
  435. } catch (\Throwable $e) {
  436. return null;
  437. }
  438. return is_array($row) && $row !== [] ? $row : null;
  439. }
  440. /**
  441. * 在 customer 表中匹配当前用户:优先手机号,否则按公司名
  442. *
  443. * @return array<string, mixed>|null
  444. */
  445. protected function mprocFindCustomerRowForUser(array $user): ?array
  446. {
  447. $phone = trim((string)($user['phone'] ?? ''));
  448. if ($phone === '') {
  449. $phone = trim((string)($user['account'] ?? ''));
  450. }
  451. $cn = trim((string)($user['company_name'] ?? ''));
  452. if ($phone !== '' && preg_match('/^1\d{10}$/', $phone)) {
  453. $byPhone = $this->mprocFindCustomerRowByPhone($phone);
  454. if ($byPhone !== null) {
  455. return $byPhone;
  456. }
  457. }
  458. if ($cn !== '') {
  459. try {
  460. $hit = Db::table('customer')
  461. ->where(function ($q) use ($cn) {
  462. $q->where('company_name', $cn)->whereOr('name', $cn);
  463. })
  464. ->order('id', 'desc')
  465. ->find();
  466. } catch (\Throwable $e) {
  467. $hit = null;
  468. }
  469. if (is_array($hit) && $hit !== []) {
  470. return $hit;
  471. }
  472. }
  473. return null;
  474. }
  475. /**
  476. * 从 customer 行取字段(兼容列名大小写)
  477. *
  478. * @param string[] $candidates
  479. */
  480. protected function mprocCustomerPickField(array $row, array $candidates): string
  481. {
  482. foreach ($candidates as $want) {
  483. $lw = strtolower($want);
  484. foreach ($row as $k => $v) {
  485. if (!is_string($k)) {
  486. continue;
  487. }
  488. if (strtolower($k) !== $lw) {
  489. continue;
  490. }
  491. $s = trim((string)$v);
  492. if ($s !== '') {
  493. return $s;
  494. }
  495. }
  496. }
  497. return '';
  498. }
  499. /**
  500. * 明细表关键字搜索:仅对 purchase_order_detail 真实存在的列 LIKE;
  501. * 主表 purchase_order 上的订单号、印件、工序等另查 scydgy_id 再 OR 进列表(避免引用不存在的列导致整页查失败)。
  502. *
  503. * @param \think\db\Query $query
  504. */
  505. protected function mprocApplySearchKeywordToDetailQuery($query, $search)
  506. {
  507. $kw = trim((string)$search);
  508. if ($kw === '') {
  509. return;
  510. }
  511. $map = self::$mprocProcuremenColumns;
  512. if (!is_array($map) || $map === []) {
  513. return;
  514. }
  515. $like = '%' . addcslashes($kw, '%_\\') . '%';
  516. $wantLower = ['ccydh', 'cyjmc', 'cdxmc', 'company_name', 'cgzzxmc', 'cgymc', 'cdf', 'phone', 'email'];
  517. $detailCols = [];
  518. foreach ($wantLower as $low) {
  519. if (isset($map[$low])) {
  520. $detailCols[] = $map[$low];
  521. }
  522. }
  523. $scydgyCol = isset($map['scydgy_id']) ? $map['scydgy_id'] : 'scydgy_id';
  524. $idCol = isset($map['id']) ? $map['id'] : 'id';
  525. $poSidList = [];
  526. $poWant = ['CCYDH', 'CYJMC', 'CDXMC', 'CGYMC', 'cGzzxMc', 'CDF'];
  527. try {
  528. $col = Db::table('purchase_order')
  529. ->where(function ($sub) use ($like, $poWant) {
  530. $firstPo = true;
  531. foreach ($poWant as $pf) {
  532. if ($firstPo) {
  533. $sub->where($pf, 'like', $like);
  534. $firstPo = false;
  535. } else {
  536. $sub->whereOr($pf, 'like', $like);
  537. }
  538. }
  539. })
  540. ->column('scydgy_id');
  541. if (is_array($col)) {
  542. foreach ($col as $v) {
  543. $id = (int)$v;
  544. if ($id > 0) {
  545. $poSidList[$id] = true;
  546. }
  547. }
  548. }
  549. $poSidList = array_keys($poSidList);
  550. } catch (\Throwable $e) {
  551. $poSidList = [];
  552. }
  553. $query->where(function ($q2) use ($like, $detailCols, $poSidList, $scydgyCol, $idCol) {
  554. $first = true;
  555. foreach ($detailCols as $col) {
  556. if ($first) {
  557. $q2->where($col, 'like', $like);
  558. $first = false;
  559. } else {
  560. $q2->whereOr($col, 'like', $like);
  561. }
  562. }
  563. if ($poSidList !== []) {
  564. if ($first) {
  565. $q2->where($scydgyCol, 'in', $poSidList);
  566. $first = false;
  567. } else {
  568. $q2->whereOr($scydgyCol, 'in', $poSidList);
  569. }
  570. }
  571. if ($first) {
  572. $q2->where($idCol, '=', 0);
  573. }
  574. });
  575. }
  576. /**
  577. * 列表:按左侧 Tab 追加 status_name 条件(与数值 status 0/1/2 无关;值由后端维护)
  578. * - draft:status_name = 未提交
  579. * - submitted:status_name = 已提交
  580. * - done:status_name = 已完成
  581. * 表无 status_name 列时不加条件(三个 Tab 数据相同,待库表补列后再筛)
  582. *
  583. * @param mixed $query
  584. * @param string $tab draft|submitted|done
  585. * @param string|null $statusNameCol 真实列名,如 status_name
  586. */
  587. protected function mprocApplyListTabConditions($query, $tab, $statusNameCol)
  588. {
  589. if ($statusNameCol === null) {
  590. return;
  591. }
  592. $map = [
  593. 'draft' => '未提交',
  594. 'submitted' => '已提交',
  595. 'done' => '已完成',
  596. ];
  597. $label = $map[$tab] ?? '未提交';
  598. $query->where($statusNameCol, '=', $label);
  599. }
  600. /**
  601. * 按登录态解析 customer 行
  602. *
  603. * @return array<string, mixed>|null
  604. */
  605. protected function mprocResolveCustomerUserForSession(array $user): ?array
  606. {
  607. if (!empty($user['is_admin'])) {
  608. return null;
  609. }
  610. $cuId = (int)($user['customer_id'] ?? $user['customer_user_id'] ?? 0);
  611. if ($cuId > 0) {
  612. try {
  613. $row = Db::table('customer')->where('id', $cuId)->find();
  614. } catch (\Throwable $e) {
  615. $row = null;
  616. }
  617. if (is_array($row) && $row !== [] && $this->mprocCustomerUserActive($row)) {
  618. return $row;
  619. }
  620. }
  621. $account = trim((string)($user['account'] ?? ''));
  622. if ($account !== '') {
  623. $byAcc = $this->mprocFindCustomerUserByUsername($account);
  624. if ($byAcc !== null) {
  625. return $byAcc;
  626. }
  627. }
  628. $phone = trim((string)($user['phone'] ?? ''));
  629. if ($phone !== '' && preg_match('/^1\d{10}$/', $phone)) {
  630. return $this->mprocFindCustomerUserByMobile($phone);
  631. }
  632. $uname = trim((string)($user['username'] ?? ''));
  633. if ($uname !== '' && preg_match('/^1\d{10}$/', $uname)) {
  634. return $this->mprocFindCustomerUserByMobile($uname);
  635. }
  636. return null;
  637. }
  638. /**
  639. * customer 表字段 →「我的」展示结构
  640. *
  641. * @param array<string, mixed> $cu
  642. * @return array{company_name:string,contact_name:string,phone:string,email:string}
  643. */
  644. protected function mprocProfileFromCustomerUserRow(array $cu): array
  645. {
  646. $nm = trim((string)($cu['username'] ?? ''));
  647. $phone = trim((string)($cu['phone'] ?? ''));
  648. if ($phone === '') {
  649. $phone = trim((string)($cu['account'] ?? ''));
  650. }
  651. return [
  652. 'company_name' => trim((string)($cu['company_name'] ?? '')),
  653. 'contact_name' => $nm,
  654. 'phone' => $phone,
  655. 'email' => trim((string)($cu['email'] ?? '')),
  656. ];
  657. }
  658. /**
  659. * 管理员「我的」:admin 表
  660. *
  661. * @return array{company_name:string,contact_name:string,phone:string,email:string}
  662. */
  663. protected function mprocProfileForAdmin(array $user): array
  664. {
  665. $uname = trim((string)($user['username'] ?? ''));
  666. $out = [
  667. 'company_name' => '管理员',
  668. 'contact_name' => $uname !== '' ? $uname : '管理员',
  669. 'phone' => trim((string)($user['phone'] ?? '')),
  670. 'email' => '',
  671. ];
  672. if ($uname === '') {
  673. return $out;
  674. }
  675. try {
  676. $row = Db::name('admin')->where('username', $uname)->find();
  677. } catch (\Throwable $e) {
  678. $row = null;
  679. }
  680. if (!is_array($row) || $row === []) {
  681. return $out;
  682. }
  683. $nick = trim((string)($row['nickname'] ?? ''));
  684. if ($nick !== '') {
  685. $out['contact_name'] = $nick;
  686. }
  687. $mob = trim((string)($row['mobile'] ?? ''));
  688. if ($mob !== '') {
  689. $out['phone'] = $mob;
  690. }
  691. $em = trim((string)($row['email'] ?? ''));
  692. if ($em !== '') {
  693. $out['email'] = $em;
  694. }
  695. return $out;
  696. }
  697. /**
  698. * 旧会话补全 customer_id 等字段
  699. */
  700. protected function mprocSyncSessionCustomerUser(array $user): array
  701. {
  702. if (!empty($user['is_admin'])) {
  703. return $user;
  704. }
  705. $cu = $this->mprocResolveCustomerUserForSession($user);
  706. if (!$cu) {
  707. return $user;
  708. }
  709. $id = (int)($cu['id'] ?? 0);
  710. $user['customer_id'] = $id;
  711. $user['customer_user_id'] = $id;
  712. $user['username'] = trim((string)($cu['username'] ?? $user['username'] ?? ''));
  713. $user['company_name'] = trim((string)($cu['company_name'] ?? ''));
  714. $user['account'] = trim((string)($cu['account'] ?? ''));
  715. $mob = trim((string)($cu['phone'] ?? ''));
  716. if ($mob === '') {
  717. $mob = trim((string)($cu['account'] ?? ''));
  718. }
  719. if ($mob !== '') {
  720. $user['phone'] = $mob;
  721. }
  722. $token = Session::get('mproc_token');
  723. if ($token) {
  724. $user['login_time'] = (int)($user['login_time'] ?? time());
  725. Cache::set('mproc_u_' . preg_replace('/[^a-f0-9]/i', '', (string)$token), $user, $this->mprocTtlSeconds + 86400);
  726. }
  727. return $user;
  728. }
  729. /**
  730. * 「我的」:普通用户 customer;管理员 admin
  731. */
  732. protected function mprocProfileForUser(array $user)
  733. {
  734. if (!empty($user['is_admin'])) {
  735. return $this->mprocProfileForAdmin($user);
  736. }
  737. $cu = $this->mprocResolveCustomerUserForSession($user);
  738. if (is_array($cu) && $cu !== []) {
  739. return $this->mprocProfileFromCustomerUserRow($cu);
  740. }
  741. $phone = trim((string)($user['phone'] ?? ''));
  742. if ($phone === '') {
  743. $phone = trim((string)($user['account'] ?? ''));
  744. }
  745. return [
  746. 'company_name' => trim((string)($user['company_name'] ?? '')),
  747. 'contact_name' => trim((string)($user['username'] ?? '')),
  748. 'phone' => $phone,
  749. 'email' => '',
  750. ];
  751. }
  752. /**
  753. * 将 purchase_order(工序行主表)快照合并进 purchase_order_detail 行:订单级信息以主表为准;
  754. * 金额、交期、外厂 company、明细 status 等仍保留明细表。
  755. *
  756. * @param array $row 引用:明细行
  757. * @param array $poRow purchase_order 一行
  758. */
  759. protected function mprocMergePurchaseOrderIntoDetail(array &$row, array $poRow)
  760. {
  761. $pl = array_change_key_case($poRow, CASE_LOWER);
  762. $hdr = [
  763. 'ccydh' => 'CCYDH',
  764. 'cyjmc' => 'CYJMC',
  765. 'cdf' => 'CDF',
  766. 'cgzzxmc' => 'cGzzxMc',
  767. 'cgymc' => 'CGYMC',
  768. 'cdxmc' => 'CDXMC',
  769. 'ngzl' => 'NGZL',
  770. 'cdw' => 'CDW',
  771. 'cgybh' => 'CGYBH',
  772. ];
  773. foreach ($hdr as $lk => $out) {
  774. if (!array_key_exists($lk, $pl)) {
  775. continue;
  776. }
  777. $v = $pl[$lk];
  778. if ($v !== null && $v !== '') {
  779. $row[$out] = $v;
  780. }
  781. }
  782. // 本次数量、最高限价:仅存在于主表;PDO 列名可能为小写 ceilingprice
  783. if (array_key_exists('this_quantity', $pl)) {
  784. $row['This_quantity'] = $pl['this_quantity'];
  785. }
  786. if (array_key_exists('ceilingprice', $pl)) {
  787. $row['ceilingPrice'] = $pl['ceilingprice'];
  788. } elseif (array_key_exists('ceiling_price', $pl)) {
  789. $row['ceilingPrice'] = $pl['ceiling_price'];
  790. }
  791. }
  792. /**
  793. * 查询 purchase_order_detail 列表(订单页)
  794. * 无搜索词时:左侧 Tab 按 status_name 筛选(未提交/已提交/已完成)
  795. * 有搜索词时:不按 Tab 筛选,在本单位可见数据内全局关键字匹配
  796. *
  797. * @param string $tab draft|submitted|done
  798. * @param string|null $statusNameCol status_name 真实列名;为 null 时不按 Tab 过滤
  799. * @return array{rows: array, done_no_status: int}
  800. */
  801. protected function mprocFetchProcuremenList(array $user, $tab, $q, $statusNameCol)
  802. {
  803. $query = Db::table('purchase_order_detail')->order('id', 'desc');
  804. $userWhere = $this->mprocListWhereForLoginUser($user);
  805. if ($userWhere !== []) {
  806. $query->where($userWhere);
  807. }
  808. $this->mprocApplySearchKeywordToDetailQuery($query, $q);
  809. // 有搜索词时不在此按 status_name 分栏筛选,全局匹配;无搜索词时仍按左侧 Tab(未提交/已提交/已完成)筛选
  810. if (trim((string)$q) === '') {
  811. $this->mprocApplyListTabConditions($query, $tab, $statusNameCol);
  812. }
  813. try {
  814. $rows = $query->limit(500)->select();
  815. } catch (\Throwable $e) {
  816. $rows = [];
  817. }
  818. if (!is_array($rows)) {
  819. $rows = [];
  820. }
  821. $poBySid = [];
  822. $sidList = [];
  823. foreach ($rows as $r0) {
  824. if (!is_array($r0)) {
  825. continue;
  826. }
  827. $sid0 = (int)($r0['scydgy_id'] ?? $r0['SCYDGY_ID'] ?? 0);
  828. if ($sid0 > 0) {
  829. $sidList[$sid0] = true;
  830. }
  831. }
  832. if ($sidList !== []) {
  833. try {
  834. $poRows = Db::table('purchase_order')
  835. ->where('scydgy_id', 'in', array_values(array_keys($sidList)))
  836. ->select();
  837. if (is_array($poRows)) {
  838. foreach ($poRows as $pr) {
  839. $sidk = (int)($pr['scydgy_id'] ?? $pr['SCYDGY_ID'] ?? 0);
  840. if ($sidk > 0) {
  841. $poBySid[$sidk] = $pr;
  842. }
  843. }
  844. }
  845. } catch (\Throwable $e) {
  846. }
  847. }
  848. foreach ($rows as &$row) {
  849. if (!is_array($row)) {
  850. continue;
  851. }
  852. $row['eid'] = (int)($row['id'] ?? $row['ID'] ?? 0);
  853. $sid = (int)($row['scydgy_id'] ?? $row['SCYDGY_ID'] ?? 0);
  854. if ($sid > 0 && isset($poBySid[$sid])) {
  855. $this->mprocMergePurchaseOrderIntoDetail($row, $poBySid[$sid]);
  856. }
  857. // status_name 由库表/后端维护,不在此根据 amount 覆盖
  858. if (!isset($row['status_name']) || $row['status_name'] === null) {
  859. $row['status_name'] = '';
  860. } else {
  861. $row['status_name'] = trim((string)$row['status_name']);
  862. }
  863. $row['mproc_can_edit'] = $this->mprocCanEditRow($user, $row) ? 1 : 0;
  864. $am = $row['amount'] ?? null;
  865. if ($am === null || $am === '' || (is_string($am) && trim($am) === '')) {
  866. $row['amount_display'] = '';
  867. } else {
  868. $row['amount_display'] = is_scalar($am) ? (string)$am : '';
  869. }
  870. $dv = isset($row['delivery']) ? trim((string)$row['delivery']) : '';
  871. if ($dv !== '' && preg_match('/^(\d{4}-\d{2}-\d{2})/', $dv, $m)) {
  872. $row['delivery_display'] = $m[1];
  873. } elseif ($dv !== '') {
  874. $row['delivery_display'] = $dv;
  875. } else {
  876. $row['delivery_display'] = '';
  877. }
  878. $row['amount_missing'] = ($am === null || $am === '' || (is_string($am) && trim($am) === '')) ? 1 : 0;
  879. $row['delivery_missing'] = ($dv === '' || preg_match('/^0000-00-00/i', $dv)) ? 1 : 0;
  880. $row['mproc_fill_hint'] = '';
  881. $row['mproc_this_quantity_display'] = $this->mprocResolveDisplayThisQuantity($row);
  882. }
  883. unset($row);
  884. return [
  885. 'rows' => $rows ?: [],
  886. 'done_no_status' => (int)($statusNameCol === null),
  887. ];
  888. }
  889. /**
  890. * 列表展示用「本次数量」:主表本次数量为空时回退显示 NGZL(工作量)
  891. *
  892. * @param array<string, mixed> $row
  893. */
  894. protected function mprocResolveDisplayThisQuantity(array $row): string
  895. {
  896. $qty = trim((string)($row['This_quantity'] ?? $row['this_quantity'] ?? ''));
  897. if ($qty !== '') {
  898. return $qty;
  899. }
  900. $gzl = $row['NGZL'] ?? $row['ngzl'] ?? '';
  901. if ($gzl === null || $gzl === '') {
  902. return '';
  903. }
  904. return is_scalar($gzl) ? trim((string)$gzl) : '';
  905. }
  906. /**
  907. * 外发明细首页(需登录)
  908. * GET:main_tab=orders|me,orders 时 tab=draft|submitted|done 对应 status_name:未提交|已提交|已完成;q 搜索词
  909. */
  910. public function index()
  911. {
  912. $user = $this->mprocGetUser();
  913. if (!$user) {
  914. $uri = isset($_SERVER['REQUEST_URI']) ? (string)$_SERVER['REQUEST_URI'] : '';
  915. $safe = $this->mprocSanitizeRedirectUrl($uri);
  916. if ($safe !== '') {
  917. Session::set('mproc_intended_url', $safe);
  918. }
  919. $this->redirect($this->mprocBuildLoginUrl(''));
  920. return;
  921. }
  922. $user = $this->mprocSyncSessionCustomerUser($user);
  923. $tabParam = trim((string)$this->request->get('tab', 'draft'));
  924. $mainTab = trim((string)$this->request->get('main_tab', 'orders'));
  925. // 旧地址 ?tab=me 表示「我的」
  926. if ($tabParam === 'me') {
  927. $mainTab = 'me';
  928. }
  929. if (!in_array($mainTab, ['orders', 'me'], true)) {
  930. $mainTab = 'orders';
  931. }
  932. $tab = $tabParam === 'me' ? 'draft' : $tabParam;
  933. if (!in_array($tab, ['draft', 'submitted', 'done'], true)) {
  934. $tab = 'draft';
  935. }
  936. $q = trim((string)$this->request->get('q', ''));
  937. $mprocFocusEid = 0;
  938. $focusEid = (int)$this->request->get('focus_eid', 0);
  939. if ($focusEid > 0 && $mainTab === 'orders') {
  940. $idCol = $this->mprocResolveProcuremenColumn(['id']);
  941. if ($idCol) {
  942. $qw = $this->mprocListWhereForLoginUser($user);
  943. $dr = null;
  944. try {
  945. $qrow = Db::table('purchase_order_detail')->where($idCol, $focusEid);
  946. if ($qw !== []) {
  947. $qrow->where($qw);
  948. }
  949. $dr = $qrow->find();
  950. } catch (\Throwable $e) {
  951. $dr = null;
  952. }
  953. if (is_array($dr) && $dr !== []) {
  954. $mprocFocusEid = $focusEid;
  955. $q = '';
  956. $sn = trim((string)($dr['status_name'] ?? $dr['STATUS_NAME'] ?? ''));
  957. $mapTab = ['未提交' => 'draft', '已提交' => 'submitted', '已完成' => 'done'];
  958. if ($sn !== '' && isset($mapTab[$sn])) {
  959. $tab = $mapTab[$sn];
  960. }
  961. }
  962. }
  963. }
  964. // 左侧 Tab 按 purchase_order_detail.status_name(未提交/已提交/已完成),与数值 status 无关
  965. $statusNameCol = $this->mprocResolveProcuremenColumn(['status_name', 'status_txt', 'status_text']);
  966. $profile = $this->mprocProfileForUser($user);
  967. $this->view->assign('mprocMainTab', $mainTab);
  968. $this->view->assign('mprocTab', $tab);
  969. $this->view->assign('mprocSearchQ', $q);
  970. $this->view->assign('mprocProfile', $profile);
  971. $this->view->assign('mprocIsAdmin', !empty($user['is_admin']) ? 1 : 0);
  972. $cid = (int)($user['customer_id'] ?? $user['customer_user_id'] ?? 0);
  973. $this->view->assign('mprocCanChangePwd', empty($user['is_admin']) && $cid > 0 ? 1 : 0);
  974. $this->view->assign('mprocFocusEid', $mprocFocusEid);
  975. if ($mainTab === 'me') {
  976. $this->view->assign('rows', []);
  977. return $this->view->fetch();
  978. }
  979. $bundle = $this->mprocFetchProcuremenList($user, $tab, $q, $statusNameCol);
  980. $this->view->assign('rows', $bundle['rows']);
  981. return $this->view->fetch();
  982. }
  983. /**
  984. * 外发明细列表 JSON(需登录)
  985. * main_tab=orders|me;orders 时 tab=draft|submitted|done、q=搜索词
  986. */
  987. public function mprocList()
  988. {
  989. $user = $this->mprocGetUser();
  990. if (!$user) {
  991. $this->error('请先登录', url('index/index/login'));
  992. }
  993. $user = $this->mprocSyncSessionCustomerUser($user);
  994. $tabParam = trim((string)$this->request->request('tab', 'draft'));
  995. $mainTab = trim((string)$this->request->request('main_tab', 'orders'));
  996. if ($tabParam === 'me') {
  997. $mainTab = 'me';
  998. }
  999. if (!in_array($mainTab, ['orders', 'me'], true)) {
  1000. $mainTab = 'orders';
  1001. }
  1002. $tab = $tabParam === 'me' ? 'draft' : $tabParam;
  1003. if (!in_array($tab, ['draft', 'submitted', 'done'], true)) {
  1004. $tab = 'draft';
  1005. }
  1006. $q = trim((string)$this->request->request('q', ''));
  1007. if ($mainTab === 'me') {
  1008. // Jump::success($msg, $url, $data, …) 第二参是 URL,数据必须放第三参
  1009. $this->success('ok', '', [
  1010. 'main_tab' => 'me',
  1011. 'tab' => $tab,
  1012. 'rows' => [],
  1013. 'profile' => $this->mprocProfileForUser($user),
  1014. 'done_no_status' => 0,
  1015. ]);
  1016. }
  1017. $statusNameCol = $this->mprocResolveProcuremenColumn(['status_name', 'status_txt', 'status_text']);
  1018. $bundle = $this->mprocFetchProcuremenList($user, $tab, $q, $statusNameCol);
  1019. $this->success('ok', '', array_merge([
  1020. 'main_tab' => 'orders',
  1021. 'tab' => $tab,
  1022. 'is_admin' => !empty($user['is_admin']) ? 1 : 0,
  1023. ], $bundle));
  1024. }
  1025. /**
  1026. * 登录页(手机号验证码 / 账号密码)
  1027. */
  1028. public function login()
  1029. {
  1030. if ($this->mprocGetUser()) {
  1031. $this->redirect(url('index/index/index'));
  1032. }
  1033. $redirect = $this->mprocSanitizeRedirectUrl($this->request->get('redirect', ''));
  1034. if ($redirect !== '') {
  1035. Session::set('mproc_intended_url', $redirect);
  1036. }
  1037. $this->view->assign('mprocLoginRedirect', $redirect);
  1038. $this->view->assign('mprocCaptchaUrl', url('index/index/captcha'));
  1039. $this->view->assign('mprocCaptchaLen', (int)(Config::get('captcha.length') ?: 4));
  1040. return $this->view->fetch();
  1041. }
  1042. /**
  1043. * 图形验证码(手机号登录用)
  1044. */
  1045. public function captcha($id = '')
  1046. {
  1047. $captcha = new Captcha((array)Config::get('captcha'));
  1048. return $captcha->entry($id);
  1049. }
  1050. /**
  1051. * 发送登录验证码(POST:phone、captcha)
  1052. */
  1053. public function sendSms()
  1054. {
  1055. if (!$this->request->isPost()) {
  1056. $this->error('请使用 POST');
  1057. }
  1058. $phone = trim((string)$this->request->post('phone', ''));
  1059. $captcha = trim((string)$this->request->post('captcha', ''));
  1060. if ($captcha === '') {
  1061. $this->error('请输入图形验证码');
  1062. }
  1063. if (!Validate::is($captcha, 'captcha')) {
  1064. $this->error('图形验证码不正确');
  1065. }
  1066. if (!preg_match('/^1\d{10}$/', $phone)) {
  1067. $this->error('请输入正确的11位手机号');
  1068. }
  1069. if (!$this->mprocFindCustomerUserByMobile($phone)) {
  1070. $this->error('该手机号未开通或已禁用,请联系管理员');
  1071. }
  1072. $cd = (int)(Config::get('mproc.sms_resend_cd') ?: 55);
  1073. if (Cache::get('mproc_sms_wait_' . $phone)) {
  1074. $this->error('发送过于频繁,请稍后再试');
  1075. }
  1076. $code = (string)random_int(100000, 999999);
  1077. $ttl = (int)(Config::get('mproc.sms_code_ttl') ?: 300);
  1078. $ttl = max(60, min(600, $ttl));
  1079. Cache::set('mproc_code_' . $phone, $code, $ttl);
  1080. Cache::set('mproc_sms_wait_' . $phone, 1, $cd);
  1081. try {
  1082. $this->mprocSmsSend($phone, '【登录验证】您的验证码为' . $code . ',' . (int)($ttl / 60) . '分钟内有效,请勿泄露。');
  1083. } catch (\Exception $e) {
  1084. Cache::rm('mproc_code_' . $phone);
  1085. Cache::rm('mproc_sms_wait_' . $phone);
  1086. $this->error($e->getMessage());
  1087. }
  1088. $this->success('验证码已发送');
  1089. }
  1090. /**
  1091. * 验证码登录(POST:phone、code)
  1092. */
  1093. public function doLogin()
  1094. {
  1095. if (!$this->request->isPost()) {
  1096. $this->error('请使用 POST');
  1097. }
  1098. $phone = trim((string)$this->request->post('phone', ''));
  1099. $code = trim((string)$this->request->post('code', ''));
  1100. if (!preg_match('/^1\d{10}$/', $phone)) {
  1101. $this->error('手机号格式不正确');
  1102. }
  1103. if (!preg_match('/^\d{6}$/', $code)) {
  1104. $this->error('请输入6位验证码');
  1105. }
  1106. // 本地调试:application/extra/mproc.php 中配置 mock_sms_code 与输入一致时,不校验短信缓存(生产务必留空)
  1107. $mock = Config::get('mproc.mock_sms_code');
  1108. if ($mock !== null && $mock !== '' && (string)$mock === $code) {
  1109. Cache::rm('mproc_code_' . $phone);
  1110. } else {
  1111. $cached = Cache::get('mproc_code_' . $phone);
  1112. if ($cached === false || $cached === null || (string)$cached !== $code) {
  1113. $this->error('验证码错误或已过期');
  1114. }
  1115. Cache::rm('mproc_code_' . $phone);
  1116. }
  1117. $cu = $this->mprocFindCustomerUserByMobile($phone);
  1118. if (!$cu) {
  1119. $this->error('该手机号未开通或已禁用,请联系管理员');
  1120. }
  1121. $this->mprocFinishLogin($this->mprocLoginPayloadFromCustomer($cu, 'sms'));
  1122. }
  1123. /**
  1124. * 账号密码登录(POST:username、password)
  1125. * 先 customer(account),未命中再 admin;admin 密码规则同 FastAdmin Auth::login
  1126. */
  1127. public function doLoginPwd()
  1128. {
  1129. if (!$this->request->isPost()) {
  1130. $this->error('请使用 POST');
  1131. }
  1132. $username = trim((string)$this->request->post('username', ''));
  1133. $password = (string)$this->request->post('password', '');
  1134. if ($username === '' || $password === '') {
  1135. $this->error('请输入账号和密码');
  1136. }
  1137. $cu = $this->mprocFindCustomerUserByUsername($username);
  1138. if ($cu) {
  1139. if (!$this->mprocVerifyCustomerUserPassword($cu, $password)) {
  1140. $this->error('账号或密码错误');
  1141. }
  1142. $this->mprocFinishLogin($this->mprocLoginPayloadFromCustomer($cu, 'pwd'));
  1143. }
  1144. // 管理员:表 admin
  1145. $row = null;
  1146. try {
  1147. $row = Db::name('admin')
  1148. ->field('id,username,password,salt,status,loginfailure,updatetime')
  1149. ->where('username', $username)
  1150. ->find();
  1151. } catch (\Throwable $e) {
  1152. $row = null;
  1153. }
  1154. if (!$row || !is_array($row)) {
  1155. $this->error('账号或密码错误');
  1156. }
  1157. $id = (int)($row['id'] ?? 0);
  1158. if (($row['status'] ?? '') == 'hidden') {
  1159. $this->error('该账号已禁用');
  1160. }
  1161. if (Config::get('fastadmin.login_failure_retry') && (int)($row['loginfailure'] ?? 0) >= 10 && time() - (int)($row['updatetime'] ?? 0) < 86400) {
  1162. $this->error('登录失败次数过多,请24小时后再试');
  1163. }
  1164. $salt = (string)($row['salt'] ?? '');
  1165. $hashStored = (string)($row['password'] ?? '');
  1166. $hashInput = md5(md5($password) . $salt);
  1167. if ($hashStored === '' || $hashInput !== $hashStored) {
  1168. if ($id > 0) {
  1169. try {
  1170. Db::name('admin')->where('id', $id)->update([
  1171. 'loginfailure' => (int)($row['loginfailure'] ?? 0) + 1,
  1172. 'updatetime' => time(),
  1173. ]);
  1174. } catch (\Throwable $e) {
  1175. }
  1176. }
  1177. $this->error('账号或密码错误');
  1178. }
  1179. if ($id > 0) {
  1180. try {
  1181. Db::name('admin')->where('id', $id)->update([
  1182. 'loginfailure' => 0,
  1183. 'updatetime' => time(),
  1184. ]);
  1185. } catch (\Throwable $e) {
  1186. }
  1187. }
  1188. $this->mprocFinishLogin([
  1189. 'phone' => trim((string)($row['mobile'] ?? '')),
  1190. 'company_name' => '',
  1191. 'username' => $username,
  1192. 'customer_user_id' => 0,
  1193. 'login_type' => 'pwd',
  1194. 'is_admin' => 1,
  1195. ]);
  1196. }
  1197. /**
  1198. * 是否允许当前登录用户修改该条 purchase_order_detail 的金额、交期
  1199. * 仅普通用户(customer)可改;管理员(admin)仅可查看
  1200. */
  1201. protected function mprocCanEditRow(array $user, array $row)
  1202. {
  1203. if (!empty($user['is_admin'])) {
  1204. return false;
  1205. }
  1206. $uCo = trim((string)($user['company_name'] ?? ''));
  1207. if ($uCo === '') {
  1208. $uPhone = trim((string)($user['phone'] ?? ''));
  1209. if ($uPhone !== '') {
  1210. $uCo = $this->mprocResolveCompanyForLoginPhone($uPhone);
  1211. }
  1212. }
  1213. $rCo = trim((string)($row['company_name'] ?? ''));
  1214. if ($uCo !== '' && $rCo !== '' && strcmp($rCo, $uCo) === 0) {
  1215. return true;
  1216. }
  1217. $uPhone = trim((string)($user['phone'] ?? ''));
  1218. $rPhone = trim((string)($row['phone'] ?? ''));
  1219. return $uPhone !== '' && $rPhone !== '' && strcasecmp($rPhone, $uPhone) === 0;
  1220. }
  1221. /**
  1222. * 明细行对应最高限价(来自 purchase_order;无或无效则返回 null,不校验)
  1223. *
  1224. * @param array<string, mixed> $detailRow
  1225. */
  1226. protected function mprocResolveCeilingPriceForDetailRow(array $detailRow): ?float
  1227. {
  1228. $sid = (int)($detailRow['scydgy_id'] ?? $detailRow['SCYDGY_ID'] ?? 0);
  1229. $raw = '';
  1230. if ($sid !== 0) {
  1231. try {
  1232. $po = Db::table('purchase_order')->where('scydgy_id', $sid)->find();
  1233. } catch (\Throwable $e) {
  1234. $po = null;
  1235. }
  1236. if (is_array($po)) {
  1237. $raw = trim((string)($po['ceilingPrice'] ?? $po['ceiling_price'] ?? ''));
  1238. }
  1239. }
  1240. if ($raw === '' || !preg_match('/^-?\d+(\.\d{1,5})?$/', $raw)) {
  1241. return null;
  1242. }
  1243. return (float)$raw;
  1244. }
  1245. protected function mprocFormatCeilingPriceDisplay(float $n): string
  1246. {
  1247. $s = rtrim(rtrim(sprintf('%.5F', $n), '0'), '.');
  1248. return $s === '' ? '0' : $s;
  1249. }
  1250. /**
  1251. * 保存单条外发明细的金额、交期(POST:id、amount、delivery)
  1252. */
  1253. public function mprocSave()
  1254. {
  1255. if (!$this->request->isPost()) {
  1256. $this->error('请使用 POST');
  1257. }
  1258. $user = $this->mprocGetUser();
  1259. if (!$user) {
  1260. $this->error('请先登录', url('index/index/login'));
  1261. }
  1262. $id = (int)$this->request->post('id', 0);
  1263. if ($id <= 0) {
  1264. $this->error('参数错误');
  1265. }
  1266. $row = null;
  1267. try {
  1268. $row = Db::table('purchase_order_detail')->where('id', $id)->find();
  1269. if (!$row) {
  1270. $row = Db::table('purchase_order_detail')->where('ID', $id)->find();
  1271. }
  1272. } catch (\Throwable $e) {
  1273. $row = null;
  1274. }
  1275. if (!$row || !is_array($row)) {
  1276. $this->error('记录不存在');
  1277. }
  1278. if (!$this->mprocCanEditRow($user, $row)) {
  1279. if (!empty($user['is_admin'])) {
  1280. $this->error('当前账号仅可查看,不能修改金额与交货日期');
  1281. }
  1282. $this->error('无权修改该记录');
  1283. }
  1284. $amountRaw = trim((string)$this->request->post('amount', ''));
  1285. $deliveryRaw = trim((string)$this->request->post('delivery', ''));
  1286. $data = [];
  1287. if ($amountRaw === '') {
  1288. $data['amount'] = null;
  1289. } else {
  1290. if (!preg_match('/^-?\d+(\.\d{1,5})?$/', $amountRaw)) {
  1291. $this->error('金额格式不正确,最多五位小数');
  1292. }
  1293. $ceilingLimit = $this->mprocResolveCeilingPriceForDetailRow($row);
  1294. if ($ceilingLimit !== null && (float)$amountRaw > $ceilingLimit) {
  1295. $this->error('金额不能超过最高限价 ' . $this->mprocFormatCeilingPriceDisplay($ceilingLimit));
  1296. }
  1297. $data['amount'] = $amountRaw;
  1298. }
  1299. if ($deliveryRaw === '') {
  1300. $data['delivery'] = null;
  1301. } elseif (preg_match('/^\d{4}-\d{2}-\d{2}$/', $deliveryRaw)) {
  1302. // 仅选年月日:存 DATETIME,禁止写成 00:00:00——原记录有非零点时间则沿用,否则用当前服务器时分秒
  1303. $existingDel = isset($row['delivery']) ? trim((string)$row['delivery']) : '';
  1304. $timePart = date('H:i:s');
  1305. if ($existingDel !== '') {
  1306. $tsEx = strtotime(str_replace('T', ' ', $existingDel));
  1307. if ($tsEx !== false) {
  1308. $hms = date('H:i:s', $tsEx);
  1309. if ($hms !== '00:00:00') {
  1310. $timePart = $hms;
  1311. }
  1312. }
  1313. }
  1314. $data['delivery'] = $deliveryRaw . ' ' . $timePart;
  1315. } else {
  1316. $deliveryRaw = str_replace('T', ' ', $deliveryRaw);
  1317. $ts = strtotime($deliveryRaw);
  1318. if ($ts === false) {
  1319. $this->error('交期时间格式不正确');
  1320. }
  1321. $data['delivery'] = date('Y-m-d H:i:s', $ts);
  1322. }
  1323. $dcCol = $this->mprocResolveProcuremenColumn(['delivery_createtime', 'deliverycreatetime']);
  1324. if ($dcCol !== null && array_key_exists('delivery', $data) && $data['delivery'] !== null && $data['delivery'] !== '') {
  1325. $data[$dcCol] = date('Y-m-d H:i:s');
  1326. }
  1327. // 同步 status_name(与列表 Tab 一致):金额或交期任一有有效数据 → 已提交,否则未提交;已是「已完成」不覆盖
  1328. $statusNameCol = $this->mprocResolveProcuremenColumn(['status_name', 'status_txt', 'status_text']);
  1329. if ($statusNameCol !== null) {
  1330. $curSn = '';
  1331. foreach ($row as $k => $v) {
  1332. if (strcasecmp((string)$k, $statusNameCol) === 0) {
  1333. $curSn = trim((string)$v);
  1334. break;
  1335. }
  1336. }
  1337. if ($curSn !== '已完成') {
  1338. $effAm = array_key_exists('amount', $data) ? $data['amount'] : ($row['amount'] ?? null);
  1339. $effDv = array_key_exists('delivery', $data) ? trim((string)$data['delivery']) : trim((string)($row['delivery'] ?? ''));
  1340. $amountFilled = !($effAm === null || $effAm === '' || (is_string($effAm) && trim($effAm) === ''));
  1341. $deliveryFilled = ($effDv !== '' && !preg_match('/^0000-00-00/i', $effDv));
  1342. $data[$statusNameCol] = ($amountFilled || $deliveryFilled) ? '已提交' : '未提交';
  1343. }
  1344. }
  1345. $pkField = isset($row['id']) ? 'id' : (isset($row['ID']) ? 'ID' : 'id');
  1346. $pkVal = (int)($row[$pkField] ?? $id);
  1347. try {
  1348. $aff = Db::table('purchase_order_detail')->where($pkField, $pkVal)->update($data);
  1349. } catch (\Throwable $e) {
  1350. $msg = $e->getMessage();
  1351. if (stripos($msg, 'Unknown column') !== false) {
  1352. $msg = '请确认数据表 purchase_order_detail 已包含 amount、delivery 字段';
  1353. }
  1354. $this->error('保存失败:' . $msg);
  1355. }
  1356. if ($aff === false) {
  1357. $this->error('保存失败');
  1358. }
  1359. $this->success('已保存');
  1360. }
  1361. /**
  1362. * 普通用户修改密码(POST:old_password、new_password、renew_password)
  1363. */
  1364. public function mprocChangePwd()
  1365. {
  1366. if (!$this->request->isPost()) {
  1367. $this->error('请使用 POST');
  1368. }
  1369. $user = $this->mprocGetUser();
  1370. if (!$user) {
  1371. $this->error('请先登录', url('index/index/login'));
  1372. }
  1373. $user = $this->mprocSyncSessionCustomerUser($user);
  1374. if (!empty($user['is_admin'])) {
  1375. $this->error('当前账号不支持修改密码');
  1376. }
  1377. $cu = $this->mprocResolveCustomerUserForSession($user);
  1378. if (!$cu) {
  1379. $this->error('账号不存在或已禁用');
  1380. }
  1381. $oldPwd = (string)$this->request->post('old_password', '');
  1382. $newPwd = (string)$this->request->post('new_password', '');
  1383. $renewPwd = (string)$this->request->post('renew_password', '');
  1384. if ($oldPwd === '' || $newPwd === '' || $renewPwd === '') {
  1385. $this->error('请填写完整');
  1386. }
  1387. if (strlen($newPwd) < 4) {
  1388. $this->error('新密码至少4位');
  1389. }
  1390. if ($newPwd !== $renewPwd) {
  1391. $this->error('两次输入的新密码不一致');
  1392. }
  1393. if ($oldPwd === $newPwd) {
  1394. $this->error('新密码不能与旧密码相同');
  1395. }
  1396. $cuId = (int)($cu['id'] ?? 0);
  1397. if (!$this->mprocVerifyCustomerUserPassword($cu, $oldPwd)) {
  1398. $this->error('原密码不正确');
  1399. }
  1400. $data = [
  1401. 'password' => $this->mprocHashCustomerUserPassword($newPwd),
  1402. 'updatetime' => date('Y-m-d H:i:s'),
  1403. ];
  1404. try {
  1405. Db::table('customer')->where('id', $cuId)->update($data);
  1406. } catch (\Throwable $e) {
  1407. $this->error('修改失败:' . $e->getMessage());
  1408. }
  1409. $this->success('密码已修改');
  1410. }
  1411. /**
  1412. * 退出登录
  1413. */
  1414. public function logout()
  1415. {
  1416. $token = Session::get('mproc_token');
  1417. if ($token === null || $token === '') {
  1418. $token = Cookie::get('mproc_token');
  1419. }
  1420. if ($token) {
  1421. $this->mprocClearLogin(preg_replace('/[^a-f0-9]/i', '', (string)$token));
  1422. }
  1423. $this->redirect(url('index/index/login'));
  1424. }
  1425. /**
  1426. * 短信宝(与后台外发审核一致,便于复用账号)
  1427. *
  1428. * @throws \Exception
  1429. */
  1430. protected function mprocSmsSend($phone, $content)
  1431. {
  1432. $smsapi = 'http://api.smsbao.com/';
  1433. $user = 'zhuwei123';
  1434. $pass = md5('1d1e605c101e4c1f8a156c6d7b19f126');
  1435. $sendurl = $smsapi . 'sms?u=' . $user . '&p=' . $pass . '&m=' . $phone . '&c=' . urlencode($content);
  1436. $result = @file_get_contents($sendurl);
  1437. if ($result === false) {
  1438. throw new \Exception('短信发送失败:网络异常');
  1439. }
  1440. $result = trim((string)$result);
  1441. if ($result !== '0') {
  1442. throw new \Exception('短信发送失败,错误码:' . $result);
  1443. }
  1444. }
  1445. }