Index.php 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254
  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 登记手机号为普通用户(按 customer 公司名筛明细、可填金额/交期);否则 admin.mobile 命中为管理员(看全部、仅查看);账号密码为 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 表公司名/名称;否则取 purchase_order_detail 该手机最新一条
  244. */
  245. protected function mprocResolveCompanyForLoginPhone(string $phone): string
  246. {
  247. $phone = trim($phone);
  248. if ($phone === '' || !preg_match('/^1\d{10}$/', $phone)) {
  249. return '';
  250. }
  251. $cust = $this->mprocFindCustomerRowByPhone($phone);
  252. if (is_array($cust) && $cust !== []) {
  253. $co = $this->mprocCustomerPickField($cust, ['company_name', 'name']);
  254. if ($co !== '') {
  255. return $co;
  256. }
  257. }
  258. try {
  259. $one = Db::table('purchase_order_detail')
  260. ->where('phone', $phone)
  261. ->order('id', 'desc')
  262. ->find();
  263. if (is_array($one)) {
  264. $n = trim((string)($one['company_name'] ?? ''));
  265. if ($n !== '') {
  266. return $n;
  267. }
  268. }
  269. } catch (\Throwable $e) {
  270. }
  271. return '';
  272. }
  273. /**
  274. * 仅按手机号在 customer.phone(多号逗号分隔)中匹配一条客户记录
  275. *
  276. * @return array<string, mixed>|null
  277. */
  278. protected function mprocFindCustomerRowByPhone(string $phone): ?array
  279. {
  280. $phone = trim($phone);
  281. if ($phone === '' || !preg_match('/^1\d{10}$/', $phone)) {
  282. return null;
  283. }
  284. try {
  285. $rows = Db::table('customer')->where('phone', '<>', '')->select();
  286. } catch (\Throwable $e) {
  287. return null;
  288. }
  289. if (!is_array($rows)) {
  290. return null;
  291. }
  292. foreach ($rows as $r) {
  293. if (!is_array($r)) {
  294. continue;
  295. }
  296. $raw = (string)($r['phone'] ?? '');
  297. $raw = str_replace(["\r", "\n", "\t", ' ', ' ', ','], ['', '', '', '', '', ','], $raw);
  298. foreach (explode(',', $raw) as $seg) {
  299. if (trim($seg) === $phone) {
  300. return $r;
  301. }
  302. }
  303. }
  304. return null;
  305. }
  306. /**
  307. * 管理员表 fa_admin.mobile 与当前手机号一致且未禁用(用于手机验证码管理员通道)
  308. *
  309. * @return array<string, mixed>|null
  310. */
  311. protected function mprocAdminRowByMobile(string $phone): ?array
  312. {
  313. $phone = trim($phone);
  314. if ($phone === '' || !preg_match('/^1\d{10}$/', $phone)) {
  315. return null;
  316. }
  317. try {
  318. $row = Db::name('admin')
  319. ->where('mobile', $phone)
  320. ->where('status', '<>', 'hidden')
  321. ->order('id', 'asc')
  322. ->find();
  323. } catch (\Throwable $e) {
  324. return null;
  325. }
  326. return is_array($row) && $row !== [] ? $row : null;
  327. }
  328. /**
  329. * 在 customer 表中匹配当前用户:优先手机号(支持多号逗号分隔),否则按公司名与 company_name / name
  330. *
  331. * @return array<string, mixed>|null
  332. */
  333. protected function mprocFindCustomerRowForUser(array $user): ?array
  334. {
  335. $phone = trim((string)($user['phone'] ?? ''));
  336. $cn = trim((string)($user['company_name'] ?? ''));
  337. if ($phone !== '' && preg_match('/^1\d{10}$/', $phone)) {
  338. $byPhone = $this->mprocFindCustomerRowByPhone($phone);
  339. if ($byPhone !== null) {
  340. return $byPhone;
  341. }
  342. }
  343. if ($cn !== '') {
  344. try {
  345. $hit = Db::table('customer')
  346. ->where(function ($q) use ($cn) {
  347. $q->where('company_name', $cn)->whereOr('name', $cn);
  348. })
  349. ->order('id', 'desc')
  350. ->find();
  351. } catch (\Throwable $e) {
  352. $hit = null;
  353. }
  354. if (is_array($hit) && $hit !== []) {
  355. return $hit;
  356. }
  357. }
  358. return null;
  359. }
  360. /**
  361. * 从 customer 行取字段(兼容列名大小写)
  362. *
  363. * @param string[] $candidates
  364. */
  365. protected function mprocCustomerPickField(array $row, array $candidates): string
  366. {
  367. foreach ($candidates as $want) {
  368. $lw = strtolower($want);
  369. foreach ($row as $k => $v) {
  370. if (!is_string($k)) {
  371. continue;
  372. }
  373. if (strtolower($k) !== $lw) {
  374. continue;
  375. }
  376. $s = trim((string)$v);
  377. if ($s !== '') {
  378. return $s;
  379. }
  380. }
  381. }
  382. return '';
  383. }
  384. /**
  385. * 明细表关键字搜索:仅对 purchase_order_detail 真实存在的列 LIKE;
  386. * 主表 purchase_order 上的订单号、印件、工序等另查 scydgy_id 再 OR 进列表(避免引用不存在的列导致整页查失败)。
  387. *
  388. * @param \think\db\Query $query
  389. */
  390. protected function mprocApplySearchKeywordToDetailQuery($query, $search)
  391. {
  392. $kw = trim((string)$search);
  393. if ($kw === '') {
  394. return;
  395. }
  396. $map = self::$mprocProcuremenColumns;
  397. if (!is_array($map) || $map === []) {
  398. return;
  399. }
  400. $like = '%' . addcslashes($kw, '%_\\') . '%';
  401. $wantLower = ['ccydh', 'cyjmc', 'cdxmc', 'company_name', 'cgzzxmc', 'cgymc', 'cdf', 'phone', 'email'];
  402. $detailCols = [];
  403. foreach ($wantLower as $low) {
  404. if (isset($map[$low])) {
  405. $detailCols[] = $map[$low];
  406. }
  407. }
  408. $scydgyCol = isset($map['scydgy_id']) ? $map['scydgy_id'] : 'scydgy_id';
  409. $idCol = isset($map['id']) ? $map['id'] : 'id';
  410. $poSidList = [];
  411. $poWant = ['CCYDH', 'CYJMC', 'CDXMC', 'CGYMC', 'cGzzxMc', 'CDF'];
  412. try {
  413. $col = Db::table('purchase_order')
  414. ->where(function ($sub) use ($like, $poWant) {
  415. $firstPo = true;
  416. foreach ($poWant as $pf) {
  417. if ($firstPo) {
  418. $sub->where($pf, 'like', $like);
  419. $firstPo = false;
  420. } else {
  421. $sub->whereOr($pf, 'like', $like);
  422. }
  423. }
  424. })
  425. ->column('scydgy_id');
  426. if (is_array($col)) {
  427. foreach ($col as $v) {
  428. $id = (int)$v;
  429. if ($id > 0) {
  430. $poSidList[$id] = true;
  431. }
  432. }
  433. }
  434. $poSidList = array_keys($poSidList);
  435. } catch (\Throwable $e) {
  436. $poSidList = [];
  437. }
  438. $query->where(function ($q2) use ($like, $detailCols, $poSidList, $scydgyCol, $idCol) {
  439. $first = true;
  440. foreach ($detailCols as $col) {
  441. if ($first) {
  442. $q2->where($col, 'like', $like);
  443. $first = false;
  444. } else {
  445. $q2->whereOr($col, 'like', $like);
  446. }
  447. }
  448. if ($poSidList !== []) {
  449. if ($first) {
  450. $q2->where($scydgyCol, 'in', $poSidList);
  451. $first = false;
  452. } else {
  453. $q2->whereOr($scydgyCol, 'in', $poSidList);
  454. }
  455. }
  456. if ($first) {
  457. $q2->where($idCol, '=', 0);
  458. }
  459. });
  460. }
  461. /**
  462. * 列表:按左侧 Tab 追加 status_name 条件(与数值 status 0/1/2 无关;值由后端维护)
  463. * - draft:status_name = 未提交
  464. * - submitted:status_name = 已提交
  465. * - done:status_name = 已完成
  466. * 表无 status_name 列时不加条件(三个 Tab 数据相同,待库表补列后再筛)
  467. *
  468. * @param mixed $query
  469. * @param string $tab draft|submitted|done
  470. * @param string|null $statusNameCol 真实列名,如 status_name
  471. */
  472. protected function mprocApplyListTabConditions($query, $tab, $statusNameCol)
  473. {
  474. if ($statusNameCol === null) {
  475. return;
  476. }
  477. $map = [
  478. 'draft' => '未提交',
  479. 'submitted' => '已提交',
  480. 'done' => '已完成',
  481. ];
  482. $label = $map[$tab] ?? '未提交';
  483. $query->where($statusNameCol, '=', $label);
  484. }
  485. /**
  486. * 「我的」:公司名称、姓名、手机、邮箱以 customer 表为准;无匹配时回退 purchase_order_detail
  487. */
  488. protected function mprocProfileForUser(array $user)
  489. {
  490. $phone = trim((string)($user['phone'] ?? ''));
  491. $out = [
  492. 'company_name' => trim((string)($user['company_name'] ?? '')),
  493. 'contact_name' => '',
  494. 'phone' => $phone,
  495. 'email' => '',
  496. ];
  497. if ($phone === '' && !empty($user['username'])) {
  498. if ($out['company_name'] === '') {
  499. $out['company_name'] = '账号:' . (string)$user['username'];
  500. }
  501. return $out;
  502. }
  503. $cust = $this->mprocFindCustomerRowForUser($user);
  504. if (is_array($cust) && $cust !== []) {
  505. $co = $this->mprocCustomerPickField($cust, ['company_name', 'name']);
  506. if ($co !== '') {
  507. $out['company_name'] = $co;
  508. }
  509. $out['contact_name'] = $this->mprocCustomerPickField($cust, ['username', 'contact', 'linkman', 'contacts']);
  510. $em = $this->mprocCustomerPickField($cust, ['email']);
  511. if ($em !== '') {
  512. $out['email'] = $em;
  513. }
  514. $rawP = $this->mprocCustomerPickField($cust, ['phone']);
  515. if ($rawP !== '') {
  516. $norm = str_replace(["\r", "\n", "\t", ' ', ' ', ','], ['', '', '', '', '', ','], $rawP);
  517. $segs = [];
  518. foreach (explode(',', $norm) as $seg) {
  519. $seg = trim($seg);
  520. if ($seg !== '') {
  521. $segs[] = $seg;
  522. }
  523. }
  524. if ($phone !== '' && $segs !== [] && in_array($phone, $segs, true)) {
  525. $out['phone'] = $phone;
  526. } elseif ($segs !== []) {
  527. $out['phone'] = implode('、', $segs);
  528. } else {
  529. $out['phone'] = $rawP;
  530. }
  531. }
  532. return $out;
  533. }
  534. try {
  535. $one = null;
  536. $cCol = $this->mprocResolveProcuremenColumn(['company_name']);
  537. $co = $out['company_name'];
  538. if ($cCol && $co !== '') {
  539. $one = Db::table('purchase_order_detail')->where($cCol, $co)->order('id', 'desc')->find();
  540. }
  541. if (!is_array($one) && $phone !== '') {
  542. $one = Db::table('purchase_order_detail')
  543. ->where('phone', $phone)
  544. ->order('id', 'desc')
  545. ->find();
  546. }
  547. if (is_array($one)) {
  548. if ($out['company_name'] === '') {
  549. $out['company_name'] = trim((string)($one['company_name'] ?? ''));
  550. }
  551. $out['email'] = trim((string)($one['email'] ?? ''));
  552. $rp = trim((string)($one['phone'] ?? ''));
  553. if ($rp !== '') {
  554. $out['phone'] = $rp;
  555. }
  556. }
  557. } catch (\Throwable $e) {
  558. }
  559. return $out;
  560. }
  561. /**
  562. * 将 purchase_order(工序行主表)快照合并进 purchase_order_detail 行:订单级信息以主表为准;
  563. * 金额、交期、外厂 company、明细 status 等仍保留明细表。
  564. *
  565. * @param array $row 引用:明细行
  566. * @param array $poRow purchase_order 一行
  567. */
  568. protected function mprocMergePurchaseOrderIntoDetail(array &$row, array $poRow)
  569. {
  570. $pl = array_change_key_case($poRow, CASE_LOWER);
  571. $hdr = [
  572. 'ccydh' => 'CCYDH',
  573. 'cyjmc' => 'CYJMC',
  574. 'cdf' => 'CDF',
  575. 'cgzzxmc' => 'cGzzxMc',
  576. 'cgymc' => 'CGYMC',
  577. 'cdxmc' => 'CDXMC',
  578. 'ngzl' => 'NGZL',
  579. 'cdw' => 'CDW',
  580. 'cgybh' => 'CGYBH',
  581. ];
  582. foreach ($hdr as $lk => $out) {
  583. if (!array_key_exists($lk, $pl)) {
  584. continue;
  585. }
  586. $v = $pl[$lk];
  587. if ($v !== null && $v !== '') {
  588. $row[$out] = $v;
  589. }
  590. }
  591. // 本次数量、最高限价:仅存在于主表;PDO 列名可能为小写 ceilingprice
  592. if (array_key_exists('this_quantity', $pl)) {
  593. $row['This_quantity'] = $pl['this_quantity'];
  594. }
  595. if (array_key_exists('ceilingprice', $pl)) {
  596. $row['ceilingPrice'] = $pl['ceilingprice'];
  597. } elseif (array_key_exists('ceiling_price', $pl)) {
  598. $row['ceilingPrice'] = $pl['ceiling_price'];
  599. }
  600. }
  601. /**
  602. * 查询 purchase_order_detail 列表(订单页)
  603. * 无搜索词时:左侧 Tab 按 status_name 筛选(未提交/已提交/已完成)
  604. * 有搜索词时:不按 Tab 筛选,在本单位可见数据内全局关键字匹配
  605. *
  606. * @param string $tab draft|submitted|done
  607. * @param string|null $statusNameCol status_name 真实列名;为 null 时不按 Tab 过滤
  608. * @return array{rows: array, done_no_status: int}
  609. */
  610. protected function mprocFetchProcuremenList(array $user, $tab, $q, $statusNameCol)
  611. {
  612. $query = Db::table('purchase_order_detail')->order('id', 'desc');
  613. $userWhere = $this->mprocListWhereForLoginUser($user);
  614. if ($userWhere !== []) {
  615. $query->where($userWhere);
  616. }
  617. $this->mprocApplySearchKeywordToDetailQuery($query, $q);
  618. // 有搜索词时不在此按 status_name 分栏筛选,全局匹配;无搜索词时仍按左侧 Tab(未提交/已提交/已完成)筛选
  619. if (trim((string)$q) === '') {
  620. $this->mprocApplyListTabConditions($query, $tab, $statusNameCol);
  621. }
  622. try {
  623. $rows = $query->limit(500)->select();
  624. } catch (\Throwable $e) {
  625. $rows = [];
  626. }
  627. if (!is_array($rows)) {
  628. $rows = [];
  629. }
  630. $poBySid = [];
  631. $sidList = [];
  632. foreach ($rows as $r0) {
  633. if (!is_array($r0)) {
  634. continue;
  635. }
  636. $sid0 = (int)($r0['scydgy_id'] ?? $r0['SCYDGY_ID'] ?? 0);
  637. if ($sid0 > 0) {
  638. $sidList[$sid0] = true;
  639. }
  640. }
  641. if ($sidList !== []) {
  642. try {
  643. $poRows = Db::table('purchase_order')
  644. ->where('scydgy_id', 'in', array_values(array_keys($sidList)))
  645. ->select();
  646. if (is_array($poRows)) {
  647. foreach ($poRows as $pr) {
  648. $sidk = (int)($pr['scydgy_id'] ?? $pr['SCYDGY_ID'] ?? 0);
  649. if ($sidk > 0) {
  650. $poBySid[$sidk] = $pr;
  651. }
  652. }
  653. }
  654. } catch (\Throwable $e) {
  655. }
  656. }
  657. foreach ($rows as &$row) {
  658. if (!is_array($row)) {
  659. continue;
  660. }
  661. $row['eid'] = (int)($row['id'] ?? $row['ID'] ?? 0);
  662. $sid = (int)($row['scydgy_id'] ?? $row['SCYDGY_ID'] ?? 0);
  663. if ($sid > 0 && isset($poBySid[$sid])) {
  664. $this->mprocMergePurchaseOrderIntoDetail($row, $poBySid[$sid]);
  665. }
  666. // status_name 由库表/后端维护,不在此根据 amount 覆盖
  667. if (!isset($row['status_name']) || $row['status_name'] === null) {
  668. $row['status_name'] = '';
  669. } else {
  670. $row['status_name'] = trim((string)$row['status_name']);
  671. }
  672. $row['mproc_can_edit'] = $this->mprocCanEditRow($user, $row) ? 1 : 0;
  673. $am = $row['amount'] ?? null;
  674. if ($am === null || $am === '' || (is_string($am) && trim($am) === '')) {
  675. $row['amount_display'] = '';
  676. } else {
  677. $row['amount_display'] = is_scalar($am) ? (string)$am : '';
  678. }
  679. $dv = isset($row['delivery']) ? trim((string)$row['delivery']) : '';
  680. if ($dv !== '' && preg_match('/^(\d{4}-\d{2}-\d{2})/', $dv, $m)) {
  681. $row['delivery_display'] = $m[1];
  682. } elseif ($dv !== '') {
  683. $row['delivery_display'] = $dv;
  684. } else {
  685. $row['delivery_display'] = '';
  686. }
  687. $row['amount_missing'] = ($am === null || $am === '' || (is_string($am) && trim($am) === '')) ? 1 : 0;
  688. $row['delivery_missing'] = ($dv === '' || preg_match('/^0000-00-00/i', $dv)) ? 1 : 0;
  689. $row['mproc_fill_hint'] = '';
  690. }
  691. unset($row);
  692. return [
  693. 'rows' => $rows ?: [],
  694. 'done_no_status' => (int)($statusNameCol === null),
  695. ];
  696. }
  697. /**
  698. * 外发明细首页(需登录)
  699. * GET:main_tab=orders|me,orders 时 tab=draft|submitted|done 对应 status_name:未提交|已提交|已完成;q 搜索词
  700. */
  701. public function index()
  702. {
  703. $user = $this->mprocGetUser();
  704. if (!$user) {
  705. $uri = isset($_SERVER['REQUEST_URI']) ? (string)$_SERVER['REQUEST_URI'] : '';
  706. $safe = $this->mprocSanitizeRedirectUrl($uri);
  707. if ($safe !== '') {
  708. Session::set('mproc_intended_url', $safe);
  709. }
  710. $this->redirect($this->mprocBuildLoginUrl(''));
  711. return;
  712. }
  713. $tabParam = trim((string)$this->request->get('tab', 'draft'));
  714. $mainTab = trim((string)$this->request->get('main_tab', 'orders'));
  715. // 旧地址 ?tab=me 表示「我的」
  716. if ($tabParam === 'me') {
  717. $mainTab = 'me';
  718. }
  719. if (!in_array($mainTab, ['orders', 'me'], true)) {
  720. $mainTab = 'orders';
  721. }
  722. $tab = $tabParam === 'me' ? 'draft' : $tabParam;
  723. if (!in_array($tab, ['draft', 'submitted', 'done'], true)) {
  724. $tab = 'draft';
  725. }
  726. $q = trim((string)$this->request->get('q', ''));
  727. $mprocFocusEid = 0;
  728. $focusEid = (int)$this->request->get('focus_eid', 0);
  729. if ($focusEid > 0 && $mainTab === 'orders') {
  730. $idCol = $this->mprocResolveProcuremenColumn(['id']);
  731. if ($idCol) {
  732. $qw = $this->mprocListWhereForLoginUser($user);
  733. $dr = null;
  734. try {
  735. $qrow = Db::table('purchase_order_detail')->where($idCol, $focusEid);
  736. if ($qw !== []) {
  737. $qrow->where($qw);
  738. }
  739. $dr = $qrow->find();
  740. } catch (\Throwable $e) {
  741. $dr = null;
  742. }
  743. if (is_array($dr) && $dr !== []) {
  744. $mprocFocusEid = $focusEid;
  745. $q = '';
  746. $sn = trim((string)($dr['status_name'] ?? $dr['STATUS_NAME'] ?? ''));
  747. $mapTab = ['未提交' => 'draft', '已提交' => 'submitted', '已完成' => 'done'];
  748. if ($sn !== '' && isset($mapTab[$sn])) {
  749. $tab = $mapTab[$sn];
  750. }
  751. }
  752. }
  753. }
  754. // 左侧 Tab 按 purchase_order_detail.status_name(未提交/已提交/已完成),与数值 status 无关
  755. $statusNameCol = $this->mprocResolveProcuremenColumn(['status_name', 'status_txt', 'status_text']);
  756. $profile = $this->mprocProfileForUser($user);
  757. $this->view->assign('mprocMainTab', $mainTab);
  758. $this->view->assign('mprocTab', $tab);
  759. $this->view->assign('mprocSearchQ', $q);
  760. $this->view->assign('mprocProfile', $profile);
  761. $this->view->assign('mprocIsAdmin', !empty($user['is_admin']) ? 1 : 0);
  762. $this->view->assign('mprocFocusEid', $mprocFocusEid);
  763. if ($mainTab === 'me') {
  764. $this->view->assign('rows', []);
  765. return $this->view->fetch();
  766. }
  767. $bundle = $this->mprocFetchProcuremenList($user, $tab, $q, $statusNameCol);
  768. $this->view->assign('rows', $bundle['rows']);
  769. return $this->view->fetch();
  770. }
  771. /**
  772. * 外发明细列表 JSON(需登录)
  773. * main_tab=orders|me;orders 时 tab=draft|submitted|done、q=搜索词
  774. */
  775. public function mprocList()
  776. {
  777. $user = $this->mprocGetUser();
  778. if (!$user) {
  779. $this->error('请先登录', url('index/index/login'));
  780. }
  781. $tabParam = trim((string)$this->request->request('tab', 'draft'));
  782. $mainTab = trim((string)$this->request->request('main_tab', 'orders'));
  783. if ($tabParam === 'me') {
  784. $mainTab = 'me';
  785. }
  786. if (!in_array($mainTab, ['orders', 'me'], true)) {
  787. $mainTab = 'orders';
  788. }
  789. $tab = $tabParam === 'me' ? 'draft' : $tabParam;
  790. if (!in_array($tab, ['draft', 'submitted', 'done'], true)) {
  791. $tab = 'draft';
  792. }
  793. $q = trim((string)$this->request->request('q', ''));
  794. if ($mainTab === 'me') {
  795. // Jump::success($msg, $url, $data, …) 第二参是 URL,数据必须放第三参
  796. $this->success('ok', '', [
  797. 'main_tab' => 'me',
  798. 'tab' => $tab,
  799. 'rows' => [],
  800. 'profile' => $this->mprocProfileForUser($user),
  801. 'done_no_status' => 0,
  802. ]);
  803. }
  804. $statusNameCol = $this->mprocResolveProcuremenColumn(['status_name', 'status_txt', 'status_text']);
  805. $bundle = $this->mprocFetchProcuremenList($user, $tab, $q, $statusNameCol);
  806. $this->success('ok', '', array_merge([
  807. 'main_tab' => 'orders',
  808. 'tab' => $tab,
  809. 'is_admin' => !empty($user['is_admin']) ? 1 : 0,
  810. ], $bundle));
  811. }
  812. /**
  813. * 登录页(手机号验证码 / 账号密码)
  814. */
  815. public function login()
  816. {
  817. if ($this->mprocGetUser()) {
  818. $this->redirect(url('index/index/index'));
  819. }
  820. $redirect = $this->mprocSanitizeRedirectUrl($this->request->get('redirect', ''));
  821. if ($redirect !== '') {
  822. Session::set('mproc_intended_url', $redirect);
  823. }
  824. $this->view->assign('mprocLoginRedirect', $redirect);
  825. return $this->view->fetch();
  826. }
  827. /**
  828. * 发送登录验证码(POST:phone)
  829. */
  830. public function sendSms()
  831. {
  832. if (!$this->request->isPost()) {
  833. $this->error('请使用 POST');
  834. }
  835. $phone = trim((string)$this->request->post('phone', ''));
  836. if (!preg_match('/^1\d{10}$/', $phone)) {
  837. $this->error('请输入正确的11位手机号');
  838. }
  839. if (!$this->mprocFindCustomerRowByPhone($phone) && !$this->mprocAdminRowByMobile($phone)) {
  840. $this->error('账号未开通权限,请联系管理员开通');
  841. }
  842. $cd = (int)(Config::get('mproc.sms_resend_cd') ?: 55);
  843. if (Cache::get('mproc_sms_wait_' . $phone)) {
  844. $this->error('发送过于频繁,请稍后再试');
  845. }
  846. $code = (string)random_int(100000, 999999);
  847. $ttl = (int)(Config::get('mproc.sms_code_ttl') ?: 300);
  848. $ttl = max(60, min(600, $ttl));
  849. Cache::set('mproc_code_' . $phone, $code, $ttl);
  850. Cache::set('mproc_sms_wait_' . $phone, 1, $cd);
  851. try {
  852. $this->mprocSmsSend($phone, '【登录验证】您的验证码为' . $code . ',' . (int)($ttl / 60) . '分钟内有效,请勿泄露。');
  853. } catch (\Exception $e) {
  854. Cache::rm('mproc_code_' . $phone);
  855. Cache::rm('mproc_sms_wait_' . $phone);
  856. $this->error($e->getMessage());
  857. }
  858. $this->success('验证码已发送');
  859. }
  860. /**
  861. * 验证码登录(POST:phone、code)
  862. */
  863. public function doLogin()
  864. {
  865. if (!$this->request->isPost()) {
  866. $this->error('请使用 POST');
  867. }
  868. $phone = trim((string)$this->request->post('phone', ''));
  869. $code = trim((string)$this->request->post('code', ''));
  870. if (!preg_match('/^1\d{10}$/', $phone)) {
  871. $this->error('手机号格式不正确');
  872. }
  873. if (!preg_match('/^\d{6}$/', $code)) {
  874. $this->error('请输入6位验证码');
  875. }
  876. // 本地调试:application/extra/mproc.php 中配置 mock_sms_code 与输入一致时,不校验短信缓存(生产务必留空)
  877. $mock = Config::get('mproc.mock_sms_code');
  878. if ($mock !== null && $mock !== '' && (string)$mock === $code) {
  879. Cache::rm('mproc_code_' . $phone);
  880. } else {
  881. $cached = Cache::get('mproc_code_' . $phone);
  882. if ($cached === false || $cached === null || (string)$cached !== $code) {
  883. $this->error('验证码错误或已过期');
  884. }
  885. Cache::rm('mproc_code_' . $phone);
  886. }
  887. $cust = $this->mprocFindCustomerRowByPhone($phone);
  888. if (is_array($cust) && $cust !== []) {
  889. $isAdmin = 0;
  890. $companyName = $this->mprocCustomerPickField($cust, ['company_name', 'name']);
  891. if ($companyName === '') {
  892. try {
  893. $one = Db::table('purchase_order_detail')->where('phone', $phone)->order('id', 'desc')->find();
  894. if (is_array($one)) {
  895. $companyName = trim((string)($one['company_name'] ?? ''));
  896. }
  897. } catch (\Throwable $e) {
  898. }
  899. }
  900. } elseif ($this->mprocAdminRowByMobile($phone)) {
  901. $isAdmin = 1;
  902. $companyName = '';
  903. } else {
  904. $this->error('账号未开通权限,请联系管理员开通');
  905. }
  906. $old = Session::get('mproc_token');
  907. if ($old) {
  908. Cache::rm('mproc_u_' . preg_replace('/[^a-f0-9]/i', '', (string)$old));
  909. }
  910. $token = bin2hex(random_bytes(16));
  911. $userData = [
  912. 'phone' => $phone,
  913. 'company_name' => $companyName,
  914. 'username' => '',
  915. 'login_type' => 'sms',
  916. 'is_admin' => $isAdmin ? 1 : 0,
  917. 'login_time' => time(),
  918. ];
  919. // 缓存略长于逻辑有效期,过期以 login_time 为准
  920. Cache::set('mproc_u_' . $token, $userData, $this->mprocTtlSeconds + 86400);
  921. Session::set('mproc_token', $token);
  922. Cookie::set('mproc_token', $token, $this->mprocTtlSeconds);
  923. $postR = $this->mprocSanitizeRedirectUrl($this->request->post('redirect', ''));
  924. $sessR = $this->mprocSanitizeRedirectUrl((string)Session::get('mproc_intended_url', ''));
  925. Session::delete('mproc_intended_url');
  926. $raw = $postR !== '' ? $postR : $sessR;
  927. $jump = $this->mprocBuildAfterLoginIndexUrl($raw);
  928. $this->success('登录成功', $jump);
  929. }
  930. /**
  931. * 账号密码登录(POST:username、password)
  932. * 与后台 FastAdmin 一致:表 admin、密码 md5(md5(明文)+salt)、状态禁用与失败锁定规则同 admin/library/Auth::login
  933. * 成功后仅建立手机端外发明细会话(不写后台 Session,避免与 PC 后台互踢登录态)
  934. */
  935. public function doLoginPwd()
  936. {
  937. if (!$this->request->isPost()) {
  938. $this->error('请使用 POST');
  939. }
  940. $username = trim((string)$this->request->post('username', ''));
  941. $password = (string)$this->request->post('password', '');
  942. if ($username === '' || $password === '') {
  943. $this->error('请输入账号和密码');
  944. }
  945. // 直接用 Db 查 admin,避免加载 Admin 模型与 AdminAuth(连带 fast\Auth),缩短首包时间
  946. $row = null;
  947. try {
  948. $row = Db::name('admin')
  949. ->field('id,username,password,salt,status,loginfailure,updatetime')
  950. ->where('username', $username)
  951. ->find();
  952. } catch (\Throwable $e) {
  953. $row = null;
  954. }
  955. if (!$row || !is_array($row)) {
  956. $this->error('账号或密码错误');
  957. }
  958. $id = (int)($row['id'] ?? 0);
  959. if (($row['status'] ?? '') == 'hidden') {
  960. $this->error('该账号已禁用');
  961. }
  962. if (Config::get('fastadmin.login_failure_retry') && (int)($row['loginfailure'] ?? 0) >= 10 && time() - (int)($row['updatetime'] ?? 0) < 86400) {
  963. $this->error('登录失败次数过多,请24小时后再试');
  964. }
  965. $salt = (string)($row['salt'] ?? '');
  966. $hashStored = (string)($row['password'] ?? '');
  967. $hashInput = md5(md5($password) . $salt);
  968. if ($hashStored === '' || $hashInput !== $hashStored) {
  969. if ($id > 0) {
  970. try {
  971. Db::name('admin')->where('id', $id)->update([
  972. 'loginfailure' => (int)($row['loginfailure'] ?? 0) + 1,
  973. 'updatetime' => time(),
  974. ]);
  975. } catch (\Throwable $e) {
  976. }
  977. }
  978. $this->error('账号或密码错误');
  979. }
  980. if ($id > 0) {
  981. try {
  982. Db::name('admin')->where('id', $id)->update([
  983. 'loginfailure' => 0,
  984. 'updatetime' => time(),
  985. ]);
  986. } catch (\Throwable $e) {
  987. }
  988. }
  989. $old = Session::get('mproc_token');
  990. if ($old) {
  991. Cache::rm('mproc_u_' . preg_replace('/[^a-f0-9]/i', '', (string)$old));
  992. }
  993. $token = bin2hex(random_bytes(16));
  994. $userData = [
  995. 'phone' => '',
  996. 'company_name' => '',
  997. 'username' => $username,
  998. 'login_type' => 'pwd',
  999. 'is_admin' => 1,
  1000. 'login_time' => time(),
  1001. ];
  1002. Cache::set('mproc_u_' . $token, $userData, $this->mprocTtlSeconds + 86400);
  1003. Session::set('mproc_token', $token);
  1004. Cookie::set('mproc_token', $token, $this->mprocTtlSeconds);
  1005. $postR = $this->mprocSanitizeRedirectUrl($this->request->post('redirect', ''));
  1006. $sessR = $this->mprocSanitizeRedirectUrl((string)Session::get('mproc_intended_url', ''));
  1007. Session::delete('mproc_intended_url');
  1008. $raw = $postR !== '' ? $postR : $sessR;
  1009. $jump = $this->mprocBuildAfterLoginIndexUrl($raw);
  1010. $this->success('登录成功', $jump);
  1011. }
  1012. /**
  1013. * 是否允许当前登录用户修改该条 purchase_order_detail 的金额、交期
  1014. * 仅普通外协用户(短信登录且非管理员)可改;管理员手机号、账号密码登录仅可查看
  1015. */
  1016. protected function mprocCanEditRow(array $user, array $row)
  1017. {
  1018. if (!empty($user['is_admin'])) {
  1019. return false;
  1020. }
  1021. if (($user['login_type'] ?? '') === 'pwd') {
  1022. return false;
  1023. }
  1024. $uCo = trim((string)($user['company_name'] ?? ''));
  1025. if ($uCo === '') {
  1026. $uPhone = trim((string)($user['phone'] ?? ''));
  1027. if ($uPhone !== '') {
  1028. $uCo = $this->mprocResolveCompanyForLoginPhone($uPhone);
  1029. }
  1030. }
  1031. $rCo = trim((string)($row['company_name'] ?? ''));
  1032. if ($uCo !== '' && $rCo !== '' && strcmp($rCo, $uCo) === 0) {
  1033. return true;
  1034. }
  1035. $uPhone = trim((string)($user['phone'] ?? ''));
  1036. $rPhone = trim((string)($row['phone'] ?? ''));
  1037. return $uPhone !== '' && $rPhone !== '' && strcasecmp($rPhone, $uPhone) === 0;
  1038. }
  1039. /**
  1040. * 保存单条外发明细的金额、交期(POST:id、amount、delivery)
  1041. */
  1042. public function mprocSave()
  1043. {
  1044. if (!$this->request->isPost()) {
  1045. $this->error('请使用 POST');
  1046. }
  1047. $user = $this->mprocGetUser();
  1048. if (!$user) {
  1049. $this->error('请先登录', url('index/index/login'));
  1050. }
  1051. $id = (int)$this->request->post('id', 0);
  1052. if ($id <= 0) {
  1053. $this->error('参数错误');
  1054. }
  1055. $row = null;
  1056. try {
  1057. $row = Db::table('purchase_order_detail')->where('id', $id)->find();
  1058. if (!$row) {
  1059. $row = Db::table('purchase_order_detail')->where('ID', $id)->find();
  1060. }
  1061. } catch (\Throwable $e) {
  1062. $row = null;
  1063. }
  1064. if (!$row || !is_array($row)) {
  1065. $this->error('记录不存在');
  1066. }
  1067. if (!$this->mprocCanEditRow($user, $row)) {
  1068. if (!empty($user['is_admin']) || (($user['login_type'] ?? '') === 'pwd')) {
  1069. $this->error('当前账号仅可查看,不能修改金额与交货日期');
  1070. }
  1071. $this->error('无权修改该记录');
  1072. }
  1073. $amountRaw = trim((string)$this->request->post('amount', ''));
  1074. $deliveryRaw = trim((string)$this->request->post('delivery', ''));
  1075. $data = [];
  1076. if ($amountRaw === '') {
  1077. $data['amount'] = null;
  1078. } else {
  1079. if (!preg_match('/^-?\d+(\.\d{1,5})?$/', $amountRaw)) {
  1080. $this->error('金额格式不正确,最多五位小数');
  1081. }
  1082. $data['amount'] = $amountRaw;
  1083. }
  1084. if ($deliveryRaw === '') {
  1085. $data['delivery'] = null;
  1086. } elseif (preg_match('/^\d{4}-\d{2}-\d{2}$/', $deliveryRaw)) {
  1087. // 仅选年月日:存 DATETIME,禁止写成 00:00:00——原记录有非零点时间则沿用,否则用当前服务器时分秒
  1088. $existingDel = isset($row['delivery']) ? trim((string)$row['delivery']) : '';
  1089. $timePart = date('H:i:s');
  1090. if ($existingDel !== '') {
  1091. $tsEx = strtotime(str_replace('T', ' ', $existingDel));
  1092. if ($tsEx !== false) {
  1093. $hms = date('H:i:s', $tsEx);
  1094. if ($hms !== '00:00:00') {
  1095. $timePart = $hms;
  1096. }
  1097. }
  1098. }
  1099. $data['delivery'] = $deliveryRaw . ' ' . $timePart;
  1100. } else {
  1101. $deliveryRaw = str_replace('T', ' ', $deliveryRaw);
  1102. $ts = strtotime($deliveryRaw);
  1103. if ($ts === false) {
  1104. $this->error('交期时间格式不正确');
  1105. }
  1106. $data['delivery'] = date('Y-m-d H:i:s', $ts);
  1107. }
  1108. $dcCol = $this->mprocResolveProcuremenColumn(['delivery_createtime', 'deliverycreatetime']);
  1109. if ($dcCol !== null && array_key_exists('delivery', $data) && $data['delivery'] !== null && $data['delivery'] !== '') {
  1110. $data[$dcCol] = date('Y-m-d H:i:s');
  1111. }
  1112. // 同步 status_name(与列表 Tab 一致):金额或交期任一有有效数据 → 已提交,否则未提交;已是「已完成」不覆盖
  1113. $statusNameCol = $this->mprocResolveProcuremenColumn(['status_name', 'status_txt', 'status_text']);
  1114. if ($statusNameCol !== null) {
  1115. $curSn = '';
  1116. foreach ($row as $k => $v) {
  1117. if (strcasecmp((string)$k, $statusNameCol) === 0) {
  1118. $curSn = trim((string)$v);
  1119. break;
  1120. }
  1121. }
  1122. if ($curSn !== '已完成') {
  1123. $effAm = array_key_exists('amount', $data) ? $data['amount'] : ($row['amount'] ?? null);
  1124. $effDv = array_key_exists('delivery', $data) ? trim((string)$data['delivery']) : trim((string)($row['delivery'] ?? ''));
  1125. $amountFilled = !($effAm === null || $effAm === '' || (is_string($effAm) && trim($effAm) === ''));
  1126. $deliveryFilled = ($effDv !== '' && !preg_match('/^0000-00-00/i', $effDv));
  1127. $data[$statusNameCol] = ($amountFilled || $deliveryFilled) ? '已提交' : '未提交';
  1128. }
  1129. }
  1130. $pkField = isset($row['id']) ? 'id' : (isset($row['ID']) ? 'ID' : 'id');
  1131. $pkVal = (int)($row[$pkField] ?? $id);
  1132. try {
  1133. $aff = Db::table('purchase_order_detail')->where($pkField, $pkVal)->update($data);
  1134. } catch (\Throwable $e) {
  1135. $msg = $e->getMessage();
  1136. if (stripos($msg, 'Unknown column') !== false) {
  1137. $msg = '请确认数据表 purchase_order_detail 已包含 amount、delivery 字段';
  1138. }
  1139. $this->error('保存失败:' . $msg);
  1140. }
  1141. if ($aff === false) {
  1142. $this->error('保存失败');
  1143. }
  1144. $this->success('已保存');
  1145. }
  1146. /**
  1147. * 退出登录
  1148. */
  1149. public function logout()
  1150. {
  1151. $token = Session::get('mproc_token');
  1152. if ($token === null || $token === '') {
  1153. $token = Cookie::get('mproc_token');
  1154. }
  1155. if ($token) {
  1156. $this->mprocClearLogin(preg_replace('/[^a-f0-9]/i', '', (string)$token));
  1157. }
  1158. $this->redirect(url('index/index/login'));
  1159. }
  1160. /**
  1161. * 短信宝(与后台外发审核一致,便于复用账号)
  1162. *
  1163. * @throws \Exception
  1164. */
  1165. protected function mprocSmsSend($phone, $content)
  1166. {
  1167. $smsapi = 'http://api.smsbao.com/';
  1168. $user = 'zhuwei123';
  1169. $pass = md5('1d1e605c101e4c1f8a156c6d7b19f126');
  1170. $sendurl = $smsapi . 'sms?u=' . $user . '&p=' . $pass . '&m=' . $phone . '&c=' . urlencode($content);
  1171. $result = @file_get_contents($sendurl);
  1172. if ($result === false) {
  1173. throw new \Exception('短信发送失败:网络异常');
  1174. }
  1175. $result = trim((string)$result);
  1176. if ($result !== '0') {
  1177. throw new \Exception('短信发送失败,错误码:' . $result);
  1178. }
  1179. }
  1180. }