UploadForm.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. <?php
  2. namespace common\models\forms;
  3. use common\helpers\Cache;
  4. use common\components\Model;
  5. use common\helpers\Date;
  6. use common\helpers\Excel;
  7. use common\helpers\Form;
  8. use common\helpers\http\RemoteUploadApi;
  9. use common\helpers\ocr\OcrApi;
  10. use common\helpers\Tool;
  11. use common\helpers\user\Info;
  12. use common\models\ExcelAddUser;
  13. use common\models\ExcelImport;
  14. use common\models\InvoiceAudit;
  15. use common\models\Recharge;
  16. use common\models\Uploads;
  17. use common\models\User;
  18. use common\models\Withdraw;
  19. use yii\base\Exception;
  20. /**
  21. * Login form
  22. */
  23. class UploadForm extends Model {
  24. public $file;
  25. public $excelOption; // 导入的excel文件用来干什么,存入excel导入表
  26. public $token;
  27. public $remark;
  28. public $withdrawId;
  29. public $rechargeId;
  30. private $_ocrResult;
  31. private $_remoteScenario = [
  32. // 'idCardFront', 'idCardBack', 'ad', 'invoiceFront','proveFront','goodsImg'
  33. ];
  34. /**
  35. * @inheritdoc
  36. */
  37. public function rules() {
  38. return [
  39. [['remark', 'withdrawId', 'rechargeId'], 'trim'],
  40. [['file', 'token', 'excelOption'], 'required'],
  41. [['token'], 'isToken'],
  42. [['file'], 'file'],
  43. [['file'], 'file', 'mimeTypes' => ['image/jpeg', 'image/png', 'image/x-png'], 'on' => ['idCardFront', 'idCardBack', 'ad']],
  44. // todo 暂时屏蔽
  45. // [['file'], 'file', 'mimeTypes'=>['application/vnd.ms-excel', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'text/csv'], 'on'=>['excel']],
  46. [['file'], 'isIdCardFront', 'on' => ['idCardFront']],
  47. [['file'], 'isInvoiceFront', 'on' => ['invoiceFront']],
  48. //[['file'], 'isIdCardBack', 'on'=>['idCardBack']],
  49. ];
  50. }
  51. /**
  52. * 指定校验场景
  53. * @return array
  54. */
  55. public function scenarios() {
  56. $parentScenarios = parent::scenarios();
  57. $customScenarios = [
  58. 'idCardFront' => ['file', 'token'],
  59. 'idCardBack' => ['file', 'token'],
  60. 'invoiceFront' => ['file', 'token', 'remark', 'withdrawId'],
  61. 'proveFront' => ['file', 'token', 'rechargeId'],
  62. 'ad' => ['file', 'token'],
  63. 'excel' => ['file', 'token', 'excelOption'],
  64. 'goodsImg' => ['file', 'token'],
  65. ];
  66. return array_merge($parentScenarios, $customScenarios);
  67. }
  68. public function attributeLabels() {
  69. return [
  70. 'file' => '文件',
  71. ];
  72. }
  73. /**
  74. * 校验上传token
  75. * @param $attributes
  76. */
  77. public function isToken($attributes) {
  78. if (!Cache::getUploadToken($this->token)) {
  79. $this->addError($attributes, '上传token校验失败');
  80. }
  81. }
  82. /**
  83. * 是否是身份证
  84. * @param $attributes
  85. */
  86. public function isIdCardFront($attributes) {
  87. // 查看该用户是否已经上传过身份证照片
  88. $oneData = User::find()->select('ID_IMAGE')->where('ID=:ID', [':ID' => \Yii::$app->user->id])->asArray()->one();
  89. if ($oneData['ID_IMAGE']) {
  90. $this->addError($attributes, '您已上传过身份证照片');
  91. }
  92. $this->_ocrResult = OcrApi::instance()->idCard($this->file->tempName);
  93. if (!$this->_ocrResult['success']) {
  94. $this->addError($attributes, $this->_ocrResult['message']);
  95. }
  96. }
  97. /**
  98. * 前台上传发票
  99. * @param $attributes
  100. */
  101. public function isInvoiceFront($attributes) {
  102. $this->_ocrResult = OcrApi::instance()->vatInvoice($this->file->tempName);
  103. if (!$this->_ocrResult['success']) {
  104. $this->addError($attributes, $this->_ocrResult['message']);
  105. }
  106. }
  107. /**
  108. * 上传
  109. * @return bool
  110. * @throws \yii\db\Exception
  111. */
  112. public function upload() {
  113. if (!$this->validate()) {
  114. return false;
  115. }
  116. $oneUser = User::findOne(['ID' => \Yii::$app->user->id]);
  117. $db = \Yii::$app->db;
  118. $transaction = $db->beginTransaction();
  119. try {
  120. switch ($this->scenario) {
  121. case 'invoiceFront':
  122. $uploadCategory = Uploads::CATEGORY_INVOICE;
  123. $uploadRemark = $this->remark;
  124. break;
  125. case 'idCardFront':
  126. $uploadCategory = Uploads::CATEGORY_ID_CARD;
  127. $uploadRemark = $this->_ocrResult['realName'] . '身份证正面';
  128. break;
  129. case 'idCardBack':
  130. $uploadCategory = Uploads::CATEGORY_ID_CARD;
  131. $uploadRemark = $oneUser['REAL_NAME'] . '身份证背面';
  132. break;
  133. case 'ad':
  134. $uploadCategory = Uploads::CATEGORY_IMAGE;
  135. $uploadRemark = $this->file->baseName;
  136. break;
  137. case 'excel':
  138. $uploadCategory = Uploads::CATEGORY_EXCEL;
  139. $uploadRemark = $this->file->baseName;
  140. break;
  141. case 'proveFront':
  142. $uploadCategory = Uploads::CATEGORY_IMAGE;
  143. $uploadRemark = $this->file->baseName;
  144. break;
  145. case 'goodsImg':
  146. $uploadCategory = Uploads::CATEGORY_IMAGE;
  147. $uploadRemark = $this->file->baseName;
  148. break;
  149. default :
  150. $uploadCategory = Uploads::CATEGORY_UN_KNOW;
  151. $uploadRemark = $this->file->baseName;
  152. break;
  153. }
  154. // 上传图片到远程服务器
  155. if (in_array($this->scenario, $this->_remoteScenario)) {
  156. $remoteUploadApi = RemoteUploadApi::instance();
  157. if ($uploadResult = $remoteUploadApi->upload($this->file->tempName)) {
  158. $uploadInfo = [
  159. 'fileName' => $uploadResult['name'],
  160. 'category' => $uploadCategory,
  161. 'url' => $uploadResult['url'],
  162. 'fileSize' => $uploadResult['size'] ?? null,
  163. 'md5' => $uploadResult['md5'] ?? null,
  164. ];
  165. } else {
  166. throw new Exception('文件远程上传失败');
  167. }
  168. // 删除本地临时文件
  169. unlink($this->file->tempName);
  170. } else {
  171. // 生成文件名
  172. $fileName = Tool::generateId(false);
  173. // 保存在本地
  174. $localPath = '/Volumes/HDD/workshop/old/ar.upload.ming/files/' . $fileName . '.' . $this->file->extension;
  175. if (!$this->file->saveAs($localPath)) {
  176. throw new Exception('文件保存失败');
  177. }
  178. $uploadInfo = [
  179. 'fileName' => $fileName . '.' . $this->file->extension,
  180. 'category' => $uploadCategory,
  181. // 'url' => $localPath,
  182. 'url' => $fileName . '.' . $this->file->extension,
  183. 'fileSize' => null,
  184. 'md5' => null,
  185. ];
  186. }
  187. // 把上传的文件写入数据库记录中
  188. // 把文件对应的相关资料存入数据库中
  189. $uploads = new Uploads();
  190. $uploads->FILE_NAME = $uploadInfo['fileName'];
  191. $uploads->CATEGORY = $uploadInfo['category'];
  192. $uploads->URL = $uploadInfo['url'];
  193. $uploads->FILE_SIZE = $uploadInfo['fileSize'] ?? null;
  194. $uploads->MD5 = $uploadInfo['md5'] ?? null;
  195. $uploads->REMARK = $uploadRemark;
  196. $uploads->CREATED_AT = Date::nowTime();
  197. if (!$uploads->save()) {
  198. throw new Exception('上传文件信息保存失败');
  199. }
  200. // 如果是上传发票,更新发票信息,并绑定提现记录
  201. if ($this->scenario == 'invoiceFront') {
  202. $withdraw = Withdraw::findOne(['ID' => $this->withdrawId]);
  203. //判断金额是否一致
  204. if ($withdraw['AMOUNT'] != $this->_ocrResult['amount']) {
  205. throw new Exception('上传发票金额'.$this->_ocrResult['amount'].'与提现金额【'.$withdraw['AMOUNT'].'】不一致');
  206. }
  207. //发票内容是否与后台预置信息相符
  208. $configName = Cache::getSystemConfig()['invoiceItemName']['VALUE'];
  209. if (!in_array($this->_ocrResult['itemName'], explode("\n",$configName))) {
  210. throw new Exception('上传发票中货物或应税劳务服务名称与后台预置信息【' . $configName . '】不一致');
  211. }
  212. //写入发票数据
  213. if (InvoiceAudit::find()->where('INVOICE_NUM=:INVOICE_NUM AND WITHDRAW_ID!=:WITHDRAW_ID AND AUDIT_STATUS!=:AUDIT_STATUS', [':INVOICE_NUM' => $this->_ocrResult['invoiceNum'], ':WITHDRAW_ID' => $this->withdrawId, ':AUDIT_STATUS' => \Yii::$app->params['auditStatus']['reject']['value']])->exists()) {
  214. throw new Exception('该发票已被使用');
  215. }
  216. if(!$invoiceModel = InvoiceAudit::findOne(['WITHDRAW_ID' => $this->withdrawId])){
  217. $invoiceModel = new InvoiceAudit();
  218. }
  219. $invoiceModel->USER_ID = $oneUser['ID'];
  220. $invoiceModel->WITHDRAW_ID = $this->withdrawId;
  221. $invoiceModel->INVOICE_CODE = $this->_ocrResult['invoiceCode'];
  222. $invoiceModel->INVOICE_NUM = $this->_ocrResult['invoiceNum'];
  223. $invoiceModel->INVOICE_DATE = $this->_ocrResult['invoiceDate'];
  224. $invoiceModel->AMOUNT = $this->_ocrResult['amount'];
  225. $invoiceModel->TAX_RATE = $this->_ocrResult['taxRate'];
  226. $invoiceModel->PURCHASER_NAME = $this->_ocrResult['purchaserName'];
  227. $invoiceModel->PURCHASER_REGISTER_NUM = $this->_ocrResult['purchaserRegisterNum'];
  228. $invoiceModel->PURCHASER_ADDRESS = $this->_ocrResult['purchaserAddress'];
  229. $invoiceModel->PURCHASER_BANK = $this->_ocrResult['purchaserBank'];
  230. $invoiceModel->SELLER_NAME = $this->_ocrResult['sellerName'];
  231. $invoiceModel->SELLER_REGISTER_NUM = $this->_ocrResult['sellerRegisterNum'];
  232. $invoiceModel->SELLER_ADDRESS = $this->_ocrResult['sellerAddress'];
  233. $invoiceModel->SELLER_BANK = $this->_ocrResult['sellerBank'];
  234. $invoiceModel->ITEM_NAME = $this->_ocrResult['itemName'];
  235. $invoiceModel->INVOICE_REMARK = $this->_ocrResult['invoiceRemark'];
  236. $invoiceModel->UPLOAD_ID = $uploads->ID;
  237. $invoiceModel->CREATED_AT = Date::nowTime();
  238. if (!$invoiceModel->save()) {
  239. throw new Exception(Form::formatErrorsForApi($invoiceModel->getErrors()));
  240. }
  241. //写入提现表
  242. $withdraw->INVOICE_ID = $invoiceModel->ID;
  243. $withdraw->AUDIT_STATUS = Withdraw::STATUS_APPLIED;
  244. $withdraw->UPDATED_AT = Date::nowTime();
  245. if (!$withdraw->save()) {
  246. throw new Exception(Form::formatErrorsForApi($withdraw->getErrors()));
  247. }
  248. } // 如果是上传身份证正面的场景,则把识别出来的信息更新当前的会员信息
  249. elseif ($this->scenario == 'idCardFront') {
  250. // 修改用户基本信息
  251. $oneUser->REAL_NAME = $this->_ocrResult['realName'];
  252. $oneUser->ID_CARD = $this->_ocrResult['idCardNo'];
  253. $oneUser->ADDRESS = $this->_ocrResult['address'];
  254. $oneUser->NATION = Info::getNationCode($this->_ocrResult['nation']);
  255. $oneUser->ID_IMAGE = $uploadInfo['url'];
  256. if (!$oneUser->save()) {
  257. // throw new Exception('用户基本资料保存失败');
  258. throw new Exception(Form::formatErrorsForApi($oneUser->getErrors()));
  259. }
  260. // 更新会员信息缓存
  261. User::updateBaseInfoToRedis($oneUser['ID']);
  262. } elseif ($this->scenario == 'excel') {
  263. $excelTableClass = Excel::EXCEL_STRUCTURE[$this->excelOption]['excelTableClass'];
  264. // 把上传的文件写入到导入记录中
  265. $excelImport = new ExcelImport();
  266. $excelImport->OPTION_NAME = $this->excelOption;
  267. $excelImport->TABLE_NAME = $excelTableClass::tableName();
  268. $excelImport->UPLOAD_ID = $uploads->ID;
  269. $excelImport->AUDIT_STATUS = \Yii::$app->params['auditStatus']['true']['value'];
  270. $excelImport->IMPORT_ADMIN_ID = \Yii::$app->user->id;
  271. $excelImport->AUDIT_ADMIN_ID = \Yii::$app->user->id;
  272. $excelImport->CREATED_AT = Date::nowTime();
  273. if (!$excelImport->save()) {
  274. throw new Exception(Form::formatErrorsForApi($excelImport->getErrors()));
  275. }
  276. }elseif ($this->scenario == 'proveFront') {
  277. // 如果是上传充值凭证,绑定充值记录
  278. $recharge = Recharge::findOne(['ID' => $this->rechargeId]);
  279. if(!$recharge){
  280. throw new Exception("缺少参数ID");
  281. }
  282. //写入充值表
  283. $recharge->BANK_PROVE = $uploadInfo['url'];
  284. $recharge->AUDIT_STATUS = Recharge::STATUS_PROVED;
  285. if (!$recharge->save()) {
  286. throw new Exception(Form::formatErrorsForApi($recharge->getErrors()));
  287. }
  288. }
  289. $transaction->commit();
  290. } catch (Exception $e) {
  291. $transaction->rollBack();
  292. $this->addError('upload', $e->getMessage());
  293. return false;
  294. }
  295. return $uploads;
  296. }
  297. }