Index.php 52 KB

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